diff --git a/library/src/main/AndroidManifest.xml b/library/src/main/AndroidManifest.xml
index 7377946..2df9c5b 100644
--- a/library/src/main/AndroidManifest.xml
+++ b/library/src/main/AndroidManifest.xml
@@ -1,4 +1,7 @@
+
+
+
diff --git a/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt b/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt
index 0fb517b..98081f6 100644
--- a/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt
+++ b/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt
@@ -11,12 +11,15 @@ private class ExampleAllTogether {
init {
KillerTask(
- "test", {
- result: String ->
+ // task
+ { "test" },
+ // onSuccess
+ { result: String ->
Log.wtf("result", result)
signal.countDown()
- }, {
- e: Exception? ->
+ },
+ // onFailed
+ { e: Exception? ->
Log.wtf("result", e.toString())
e?.printStackTrace()
signal.countDown()
diff --git a/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt
index 7f63a27..319eb3f 100644
--- a/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt
+++ b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt
@@ -1,6 +1,9 @@
package com.inaka.killertask
import android.util.Log
+import java.net.HttpURLConnection
+import java.net.URL
+import java.net.URLConnection
import java.util.concurrent.CountDownLatch
/**
@@ -9,25 +12,43 @@ import java.util.concurrent.CountDownLatch
private class ExampleFunctionsRefactor {
val signal = CountDownLatch(1);
+ // onSuccess function
val onSuccess: (String) -> Unit = {
result: String ->
- Log.wtf("result", result)
+ Log.wtf("success result", result)
signal.countDown()
}
+ // onFailed function
val onFailed: (Exception?) -> Unit = {
e: Exception? ->
- Log.wtf("result", e.toString())
+ Log.wtf("error result", e.toString())
e?.printStackTrace()
signal.countDown()
}
+ // task function
+ val doWork: () -> String = {
+ var connection: URLConnection? = null;
+
+ try {
+ var url = URL("https://api.github.com/gists")
+ connection = url.openConnection();
+ } catch (e: Exception) {
+ e.printStackTrace()
+ }
+
+ var httpConn = connection as HttpURLConnection;
+ httpConn.connectTimeout = 3000;
+ httpConn.readTimeout = 5000;
+
+ // return
+ httpConn.responseCode.toString() + " " + httpConn.responseMessage
+ }
+
init {
- KillerTask(doWork(), onSuccess, onFailed).go()
+ KillerTask(doWork, onSuccess, onFailed).go()
signal.await()
}
- fun doWork(): String {
- return "test"
- }
}
\ No newline at end of file
diff --git a/library/src/main/java/com/inaka/killertask/KillerTask.kt b/library/src/main/java/com/inaka/killertask/KillerTask.kt
index c94a78a..dff5dda 100644
--- a/library/src/main/java/com/inaka/killertask/KillerTask.kt
+++ b/library/src/main/java/com/inaka/killertask/KillerTask.kt
@@ -3,7 +3,7 @@ package com.inaka.killertask
import android.os.AsyncTask
import android.util.Log
-class KillerTask(val action: T, val onSuccess: (T) -> Any, val onFailed: (Exception?) -> Any) : AsyncTask() {
+class KillerTask(val task: () -> T, val onSuccess: (T) -> Any, val onFailed: (Exception?) -> Any) : AsyncTask() {
private var exception: Exception? = null
@@ -11,10 +11,13 @@ class KillerTask(val action: T, val onSuccess: (T) -> Any, val onFailed: (Exc
private val TAG = "KillerTask"
}
+ /**
+ * Override AsyncTask's function doInBackground
+ */
override fun doInBackground(vararg params: Void): T? {
try {
Log.wtf(TAG, "Enter to doInBackground")
- return run { action }
+ return run { task() }
} catch (e: Exception) {
Log.wtf(TAG, "Error in background task")
exception = e
@@ -22,26 +25,35 @@ class KillerTask(val action: T, val onSuccess: (T) -> Any, val onFailed: (Exc
}
}
+ /**
+ * Override AsyncTask's function onPostExecute
+ */
override fun onPostExecute(result: T) {
Log.wtf(TAG, "Enter to onPostExecute")
- if (!isCancelled) {
- if (exception != null) {
+ if (!isCancelled) { // task not cancelled
+ if (exception != null) { // fail
Log.wtf(TAG, "Failure with Exception")
run { onFailed(exception) }
- } else {
+ } else { // success
Log.wtf(TAG, "Success")
run { onSuccess(result) }
}
- } else {
+ } else { // task cancelled
Log.wtf(TAG, "Failure with RuntimeException caused by task cancelled")
run { onFailed(RuntimeException("Task was cancelled")) }
}
}
+ /**
+ * Execute AsyncTask
+ */
fun go() {
execute()
}
+ /**
+ * Cancel AsyncTask
+ */
fun cancel() {
cancel(true)
}
diff --git a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt
index 61c1d4e..99cf8b5 100644
--- a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt
+++ b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt
@@ -8,31 +8,35 @@ import org.junit.Test
class KillerMainTest_KotlinVersion {
@Test
fun createKillerTask() {
- KillerTask(doWork(), onSuccess, onFailed).go()
-
- /*
- That is the same as:
-
- KillerTask("test", {
- result: String ->
- assert(result.equals("test"))
- }, {
- e: Exception? ->
- e?.printStackTrace()
- print(e?.message)
- }).go()
+ KillerTask(doWork, onSuccess, onFailed).go()
+
+ /**
+ * That is the same as:
+ *
+ * KillerTask(
+ * { "test" },
+ * { result: String ->
+ * assert(result.equals("test"))
+ * },
+ * { e: Exception? ->
+ * e?.printStackTrace()
+ * print(e?.message)
+ * }).go()
*/
}
- fun doWork(): String {
- return "test"
+ // task function
+ val doWork: () -> String = {
+ "test"
}
+ // onSuccess function
val onSuccess: (String) -> Unit = {
result: String ->
assert(result.equals("test"))
}
+ // onFailed function
val onFailed: (Exception?) -> Unit = {
e: Exception? ->
e?.printStackTrace()