Commit ba921c34 authored by Mygod's avatar Mygod

Use coroutines for AclSyncer

parent fbb99880
...@@ -11,7 +11,7 @@ buildscript { ...@@ -11,7 +11,7 @@ buildscript {
buildToolsVersion = '28.0.3' buildToolsVersion = '28.0.3'
lifecycleVersion = '2.0.0' lifecycleVersion = '2.0.0'
roomVersion = '2.0.0' roomVersion = '2.0.0'
workVersion = '1.0.0-beta02' workVersion = '1.0.0-beta03'
preferencexVersion = '1.0.0' preferencexVersion = '1.0.0'
junitVersion = '4.12' junitVersion = '4.12'
androidTestVersion = '1.1.1' androidTestVersion = '1.1.1'
......
...@@ -68,7 +68,7 @@ dependencies { ...@@ -68,7 +68,7 @@ dependencies {
api 'com.google.firebase:firebase-config:16.1.3' api 'com.google.firebase:firebase-config:16.1.3'
api 'com.google.firebase:firebase-core:16.0.6' api 'com.google.firebase:firebase-core:16.0.6'
api "com.takisoft.preferencex:preferencex:$preferencexVersion" api "com.takisoft.preferencex:preferencex:$preferencexVersion"
api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0' api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycleVersion" kapt "androidx.lifecycle:lifecycle-compiler:$lifecycleVersion"
kapt "androidx.room:room-compiler:$roomVersion" kapt "androidx.room:room-compiler:$roomVersion"
testImplementation "androidx.arch.core:core-testing:$lifecycleVersion" testImplementation "androidx.arch.core:core-testing:$lifecycleVersion"
......
...@@ -22,11 +22,12 @@ package com.github.shadowsocks.acl ...@@ -22,11 +22,12 @@ package com.github.shadowsocks.acl
import android.content.Context import android.content.Context
import androidx.work.* import androidx.work.*
import kotlinx.coroutines.Dispatchers
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
class AclSyncer(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) { class AclSyncer(context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {
companion object { companion object {
private const val KEY_ROUTE = "route" private const val KEY_ROUTE = "route"
...@@ -41,7 +42,9 @@ class AclSyncer(context: Context, workerParams: WorkerParameters) : Worker(conte ...@@ -41,7 +42,9 @@ class AclSyncer(context: Context, workerParams: WorkerParameters) : Worker(conte
}) })
} }
override fun doWork(): Result = try { override val coroutineContext get() = Dispatchers.IO
override suspend fun doWork(): Result = try {
val route = inputData.getString(KEY_ROUTE)!! val route = inputData.getString(KEY_ROUTE)!!
val acl = URL("https://shadowsocks.org/acl/android/v1/$route.acl").openStream().bufferedReader() val acl = URL("https://shadowsocks.org/acl/android/v1/$route.acl").openStream().bufferedReader()
.use { it.readText() } .use { it.readText() }
...@@ -50,8 +53,5 @@ class AclSyncer(context: Context, workerParams: WorkerParameters) : Worker(conte ...@@ -50,8 +53,5 @@ class AclSyncer(context: Context, workerParams: WorkerParameters) : Worker(conte
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
Result.retry() Result.retry()
} catch (e: Exception) { // unknown failures, probably shouldn't retry
e.printStackTrace()
Result.failure()
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment