Skip to content

Commit

Permalink
Do not use WorkManager in device storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Jun 11, 2020
1 parent 70457b4 commit 400e156
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ tasks.clean.dependsOn("cargoClean")
dependencies {
val coroutinesVersion = "1.3.6"
val roomVersion = "2.2.5"
val workVersion = "2.3.4"
val workVersion = "2.4.0-beta01"

api(project(":plugin"))
api("androidx.fragment:fragment-ktx:1.2.4")
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/com/github/shadowsocks/BootReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.UserManager
import androidx.core.content.getSystemService
import com.github.shadowsocks.Core.app
import com.github.shadowsocks.preference.DataStore

Expand All @@ -50,8 +48,7 @@ class BootReceiver : BroadcastReceiver() {
val doStart = when (intent.action) {
Intent.ACTION_BOOT_COMPLETED -> !DataStore.directBootAware
Intent.ACTION_LOCKED_BOOT_COMPLETED -> DataStore.directBootAware
else -> DataStore.directBootAware ||
Build.VERSION.SDK_INT >= 24 && app.getSystemService<UserManager>()?.isUserUnlocked != false
else -> DataStore.directBootAware || Build.VERSION.SDK_INT >= 24 && Core.user.isUserUnlocked
}
if (doStart) Core.startService()
}
Expand Down
19 changes: 11 additions & 8 deletions core/src/main/java/com/github/shadowsocks/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.work.Configuration
import androidx.work.WorkManager
import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.aidl.ShadowsocksConnection
import com.github.shadowsocks.core.BuildConfig
Expand All @@ -62,14 +61,15 @@ import java.io.File
import java.io.IOException
import kotlin.reflect.KClass

object Core {
object Core : Configuration.Provider {
lateinit var app: Application
@VisibleForTesting set
lateinit var configureIntent: (Context) -> PendingIntent
val activity by lazy { app.getSystemService<ActivityManager>()!! }
val clipboard by lazy { app.getSystemService<ClipboardManager>()!! }
val connectivity by lazy { app.getSystemService<ConnectivityManager>()!! }
val notification by lazy { app.getSystemService<NotificationManager>()!! }
val user by lazy { app.getSystemService<UserManager>()!! }
val packageInfo: PackageInfo by lazy { getPackageInfo(app.packageName) }
val deviceStorage by lazy { if (Build.VERSION.SDK_INT < 24) app else DeviceStorageApp(app) }
val directBootSupported by lazy {
Expand Down Expand Up @@ -121,14 +121,11 @@ object Core {
}
}
})
WorkManager.initialize(deviceStorage, Configuration.Builder().apply {
setExecutor { GlobalScope.launch { it.run() } }
setTaskExecutor { GlobalScope.launch { it.run() } }
}.build())

// handle data restored/crash
if (Build.VERSION.SDK_INT >= 24 && DataStore.directBootAware &&
app.getSystemService<UserManager>()?.isUserUnlocked == true) DirectBoot.flushTrafficStats()
if (Build.VERSION.SDK_INT >= 24 && DataStore.directBootAware && user.isUserUnlocked) {
DirectBoot.flushTrafficStats()
}
if (DataStore.publicStore.getLong(Key.assetUpdateTime, -1) != packageInfo.lastUpdateTime) {
val assetManager = app.assets
try {
Expand All @@ -143,6 +140,12 @@ object Core {
updateNotificationChannels()
}

override fun getWorkManagerConfiguration() = Configuration.Builder().apply {
setMinimumLoggingLevel(if (BuildConfig.DEBUG) Log.VERBOSE else Log.INFO)
setExecutor { GlobalScope.launch { it.run() } }
setTaskExecutor { GlobalScope.launch { it.run() } }
}.build()

fun updateNotificationChannels() {
if (Build.VERSION.SDK_INT >= 26) @RequiresApi(26) {
notification.createNotificationChannels(listOf(
Expand Down
25 changes: 15 additions & 10 deletions core/src/main/java/com/github/shadowsocks/acl/AclSyncer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package com.github.shadowsocks.acl

import android.content.Context
import android.os.Build
import androidx.work.*
import com.github.shadowsocks.Core
import com.github.shadowsocks.Core.app
import com.github.shadowsocks.utils.useCancellable
import timber.log.Timber
import java.io.IOException
Expand All @@ -34,16 +36,19 @@ class AclSyncer(context: Context, workerParams: WorkerParameters) : CoroutineWor
companion object {
private const val KEY_ROUTE = "route"

fun schedule(route: String) = WorkManager.getInstance(Core.deviceStorage).enqueueUniqueWork(
route, ExistingWorkPolicy.REPLACE, OneTimeWorkRequestBuilder<AclSyncer>().run {
setInputData(Data.Builder().putString(KEY_ROUTE, route).build())
setConstraints(Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresCharging(true)
.build())
setInitialDelay(10, TimeUnit.SECONDS)
build()
})
fun schedule(route: String) {
if (Build.VERSION.SDK_INT >= 24 && !Core.user.isUserUnlocked) return // work does not support this
WorkManager.getInstance(app).enqueueUniqueWork(
route, ExistingWorkPolicy.REPLACE, OneTimeWorkRequestBuilder<AclSyncer>().run {
setInputData(Data.Builder().putString(KEY_ROUTE, route).build())
setConstraints(Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresCharging(true)
.build())
setInitialDelay(10, TimeUnit.SECONDS)
build()
})
}
}

override suspend fun doWork(): Result = try {
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.*
import androidx.core.content.getSystemService
import android.os.Build
import android.os.IBinder
import android.os.RemoteCallbackList
import android.os.RemoteException
import com.github.shadowsocks.BootReceiver
import com.github.shadowsocks.Core
import com.github.shadowsocks.Core.app
Expand Down Expand Up @@ -238,8 +240,8 @@ object BaseService {
val isVpnService get() = false

suspend fun startProcesses() {
val configRoot = (if (Build.VERSION.SDK_INT < 24 || app.getSystemService<UserManager>()
?.isUserUnlocked != false) app else Core.deviceStorage).noBackupFilesDir
val context = if (Build.VERSION.SDK_INT < 24 || Core.user.isUserUnlocked) app else Core.deviceStorage
val configRoot = context.noBackupFilesDir
val udpFallback = data.udpFallback
data.proxy!!.start(this,
File(Core.deviceStorage.noBackupFilesDir, "stat_main"),
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/main/java/com/github/shadowsocks/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import android.app.Application
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatDelegate

class App : Application() {
class App : Application(), androidx.work.Configuration.Provider by Core {
override fun onCreate() {
super.onCreate()
Core.init(this, MainActivity::class)
Expand Down
2 changes: 1 addition & 1 deletion tv/src/main/java/com/github/shadowsocks/tv/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import android.app.Application
import android.content.res.Configuration
import com.github.shadowsocks.Core

class App : Application() {
class App : Application(), androidx.work.Configuration.Provider by Core {
override fun onCreate() {
super.onCreate()
Core.init(this, MainActivity::class)
Expand Down

0 comments on commit 400e156

Please sign in to comment.