Unverified Commit d27497f8 authored by Mygod's avatar Mygod Committed by GitHub

Replace android-job with WorkManager (#1867)

parent c996fa26
...@@ -12,6 +12,7 @@ buildscript { ...@@ -12,6 +12,7 @@ buildscript {
supportLibraryVersion = '28.0.0-alpha3' supportLibraryVersion = '28.0.0-alpha3'
takisoftFixVersion = '27.1.1.2' takisoftFixVersion = '27.1.1.2'
roomVersion = '1.1.1' roomVersion = '1.1.1'
workVersion = '1.0.0-alpha04'
junitVersion = '4.12' junitVersion = '4.12'
androidTestVersion = '1.0.2' androidTestVersion = '1.0.2'
androidEspressoVersion = '3.0.2' androidEspressoVersion = '3.0.2'
......
...@@ -61,12 +61,12 @@ android { ...@@ -61,12 +61,12 @@ android {
dependencies { dependencies {
implementation project(':core') implementation project(':core')
implementation "android.arch.persistence.room:runtime:$roomVersion" implementation "android.arch.persistence.room:runtime:$roomVersion"
implementation "android.arch.work:work-runtime-ktx:$workVersion"
implementation "com.android.support:customtabs:$supportLibraryVersion" implementation "com.android.support:customtabs:$supportLibraryVersion"
implementation "com.android.support:design:$supportLibraryVersion" implementation "com.android.support:design:$supportLibraryVersion"
implementation "com.android.support:gridlayout-v7:$supportLibraryVersion" implementation "com.android.support:gridlayout-v7:$supportLibraryVersion"
implementation "com.android.support:support-v4:$supportLibraryVersion" implementation "com.android.support:support-v4:$supportLibraryVersion"
implementation 'com.futuremind.recyclerfastscroll:fastscroll:0.2.5' implementation 'com.futuremind.recyclerfastscroll:fastscroll:0.2.5'
implementation 'com.evernote:android-job:1.2.6'
implementation "com.google.android.gms:play-services-ads:15.0.1" implementation "com.google.android.gms:play-services-ads:15.0.1"
implementation "com.google.android.gms:play-services-analytics:16.0.0" implementation "com.google.android.gms:play-services-analytics:16.0.0"
implementation "com.google.android.gms:play-services-vision:15.0.2" implementation "com.google.android.gms:play-services-vision:15.0.2"
...@@ -83,6 +83,7 @@ dependencies { ...@@ -83,6 +83,7 @@ dependencies {
kapt "android.arch.persistence.room:compiler:$roomVersion" kapt "android.arch.persistence.room:compiler:$roomVersion"
testImplementation "android.arch.persistence.room:testing:$roomVersion" testImplementation "android.arch.persistence.room:testing:$roomVersion"
testImplementation "junit:junit:$junitVersion" testImplementation "junit:junit:$junitVersion"
androidTestImplementation "android.arch.work:work-testing:$workVersion"
androidTestImplementation "com.android.support.test:runner:$androidTestVersion" androidTestImplementation "com.android.support.test:runner:$androidTestVersion"
androidTestImplementation "com.android.support.test.espresso:espresso-core:$androidEspressoVersion" androidTestImplementation "com.android.support.test.espresso:espresso-core:$androidEspressoVersion"
} }
......
/*******************************************************************************
* *
* Copyright (C) 2017 by Max Lv <max.c.lv@gmail.com> *
* Copyright (C) 2017 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
package com.evernote.android.job
object JobConstants {
const val DATABASE_NAME = JobStorage.DATABASE_NAME
const val PREF_FILE_NAME = JobStorage.PREF_FILE_NAME
}
...@@ -38,12 +38,8 @@ import android.os.UserManager ...@@ -38,12 +38,8 @@ import android.os.UserManager
import android.support.annotation.RequiresApi import android.support.annotation.RequiresApi
import android.support.v7.app.AppCompatDelegate import android.support.v7.app.AppCompatDelegate
import android.util.Log import android.util.Log
import android.widget.Toast import androidx.work.WorkManager
import com.evernote.android.job.JobConstants
import com.evernote.android.job.JobManager
import com.evernote.android.job.JobManagerCreateException
import com.github.shadowsocks.acl.Acl import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.acl.AclSyncJob
import com.github.shadowsocks.bg.BaseService import com.github.shadowsocks.bg.BaseService
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager import com.github.shadowsocks.database.ProfileManager
...@@ -122,8 +118,6 @@ class App : Application() { ...@@ -122,8 +118,6 @@ class App : Application() {
if (Build.VERSION.SDK_INT >= 24) { // migrate old files if (Build.VERSION.SDK_INT >= 24) { // migrate old files
deviceContext.moveDatabaseFrom(this, Key.DB_PUBLIC) deviceContext.moveDatabaseFrom(this, Key.DB_PUBLIC)
deviceContext.moveDatabaseFrom(this, JobConstants.DATABASE_NAME)
deviceContext.moveSharedPreferencesFrom(this, JobConstants.PREF_FILE_NAME)
val old = Acl.getFile(Acl.CUSTOM_RULES, this) val old = Acl.getFile(Acl.CUSTOM_RULES, this)
if (old.canRead()) { if (old.canRead()) {
Acl.getFile(Acl.CUSTOM_RULES).writeText(old.readText()) Acl.getFile(Acl.CUSTOM_RULES).writeText(old.readText())
...@@ -136,12 +130,7 @@ class App : Application() { ...@@ -136,12 +130,7 @@ class App : Application() {
remoteConfig.fetch().addOnCompleteListener { remoteConfig.fetch().addOnCompleteListener {
if (it.isSuccessful) remoteConfig.activateFetched() else Log.e(TAG, "Failed to fetch config") if (it.isSuccessful) remoteConfig.activateFetched() else Log.e(TAG, "Failed to fetch config")
} }
try { WorkManager.initialize(deviceContext, androidx.work.Configuration.Builder().build())
JobManager.create(deviceContext).addJobCreator(AclSyncJob)
} catch (e: JobManagerCreateException) {
Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
app.track(e)
}
// handle data restored/crash // handle data restored/crash
if (Build.VERSION.SDK_INT >= 24 && DataStore.directBootAware && if (Build.VERSION.SDK_INT >= 24 && DataStore.directBootAware &&
......
...@@ -20,47 +20,32 @@ ...@@ -20,47 +20,32 @@
package com.github.shadowsocks.acl package com.github.shadowsocks.acl
import android.util.Log import androidx.work.*
import com.evernote.android.job.Job
import com.evernote.android.job.JobCreator
import com.evernote.android.job.JobRequest
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 AclSyncJob(private val route: String) : Job() { class AclSyncer : Worker() {
companion object : JobCreator { companion object {
private const val TAG = "AclSyncJob" fun schedule(route: String) = WorkManager.getInstance()!!.enqueue(OneTimeWorkRequestBuilder<AclSyncer>()
private const val MAX_RESCHEDULE = 3 .addTag(route)
.setConstraints(Constraints.Builder()
override fun create(tag: String): AclSyncJob? { .setRequiredNetworkType(NetworkType.UNMETERED)
val parts = tag.split(':', limit = 2) .setRequiresCharging(true)
return when (parts[0]) { .build())
TAG -> AclSyncJob(parts[1]) .setInitialDelay(10, TimeUnit.SECONDS)
else -> { .build())
Log.w(TAG, "Unknown job tag: $tag")
null
}
}
}
fun schedule(route: String): Int = JobRequest.Builder("$TAG:$route")
.setExecutionWindow(TimeUnit.SECONDS.toMillis(10), TimeUnit.DAYS.toMillis(28))
.setRequirementsEnforced(true)
.setRequiredNetworkType(JobRequest.NetworkType.UNMETERED)
.setRequiresCharging(true)
.setUpdateCurrent(true)
.build().schedule()
} }
override fun onRunJob(params: Params): Result = try { override fun doWork(): Result = try {
val route = tags.asIterable().single()!!
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() }
Acl.getFile(route).printWriter().use { it.write(acl) } Acl.getFile(route).printWriter().use { it.write(acl) }
Result.SUCCESS Result.SUCCESS
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
if (params.failureCount < AclSyncJob.MAX_RESCHEDULE) Result.RESCHEDULE else Result.FAILURE Result.RETRY
} catch (e: Exception) { // unknown failures, probably shouldn't retry } catch (e: Exception) { // unknown failures, probably shouldn't retry
e.printStackTrace() e.printStackTrace()
Result.FAILURE Result.FAILURE
......
...@@ -34,7 +34,7 @@ import android.util.Log ...@@ -34,7 +34,7 @@ import android.util.Log
import com.github.shadowsocks.App.Companion.app import com.github.shadowsocks.App.Companion.app
import com.github.shadowsocks.R import com.github.shadowsocks.R
import com.github.shadowsocks.acl.Acl import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.acl.AclSyncJob import com.github.shadowsocks.acl.AclSyncer
import com.github.shadowsocks.aidl.IShadowsocksService import com.github.shadowsocks.aidl.IShadowsocksService
import com.github.shadowsocks.aidl.IShadowsocksServiceCallback import com.github.shadowsocks.aidl.IShadowsocksServiceCallback
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
...@@ -407,7 +407,7 @@ object BaseService { ...@@ -407,7 +407,7 @@ object BaseService {
startNativeProcesses() startNativeProcesses()
if (profile.route !in arrayOf(Acl.ALL, Acl.CUSTOM_RULES)) AclSyncJob.schedule(profile.route) if (profile.route !in arrayOf(Acl.ALL, Acl.CUSTOM_RULES)) AclSyncer.schedule(profile.route)
data.changeState(CONNECTED) data.changeState(CONNECTED)
} catch (_: UnknownHostException) { } catch (_: UnknownHostException) {
......
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