-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated
OoniRunActivity
to fetch v2 descriptor async
- Loading branch information
Showing
3 changed files
with
95 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/src/main/java/org/openobservatory/ooniprobe/common/TaskExecutor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.openobservatory.ooniprobe.common | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import java.util.concurrent.Callable | ||
import java.util.concurrent.Executors | ||
|
||
abstract class ProgressTask<P, R> { | ||
abstract fun runTask(progressToken: OnTaskProgressUpdate<P>): R | ||
} | ||
|
||
typealias Task<R> = Callable<R> | ||
|
||
typealias OnTaskProgressUpdate<P> = (P) -> Unit | ||
|
||
typealias OnTaskComplete<R> = (R) -> Void | ||
|
||
class TaskExecutor { | ||
private val executor = Executors.newSingleThreadExecutor() | ||
private val handler = Handler(Looper.getMainLooper()) | ||
|
||
fun <R> executeTask(task: Task<R>, onComplete: OnTaskComplete<R>) { | ||
executor.execute { | ||
val result = task.call() | ||
handler.post { | ||
onComplete(result) | ||
} | ||
} | ||
} | ||
|
||
fun <P, R> executeProgressTask( | ||
progressTask: ProgressTask<P, R>, | ||
onProgress: OnTaskProgressUpdate<P>, | ||
onComplete: OnTaskComplete<R> | ||
) { | ||
executor.execute { | ||
val result = progressTask.runTask(progressToken = { progress -> | ||
handler.post { | ||
onProgress(progress) | ||
} | ||
}) | ||
|
||
handler.post { | ||
onComplete(result) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters