Permalink
Browse files

Safe initialize WorkManager

Instead of relying only the WorkManagerInitializer ContentProvider, manually initialize WorkManager as the first LifecycleObserver of the MuzeiWallpaperService.
  • Loading branch information...
ianhanniballake committed Jan 22, 2019
1 parent 5820c84 commit a6616452bf9a3569b43ba220f654523682343400
@@ -26,6 +26,7 @@ dependencies {
implementation project(':extensions')
implementation "androidx.core:core-ktx:$coreVersion"
implementation "androidx.exifinterface:exifinterface:$exifInterfaceVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
api "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycleVersion"
implementation "android.arch.work:work-runtime-ktx:$workManagerVersion"
@@ -10,6 +10,8 @@ import android.os.Build
import android.preference.PreferenceManager
import android.util.Log
import androidx.core.content.edit
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.work.Configuration
import androidx.work.WorkManager
import net.nurik.roman.muzei.androidclientcommon.BuildConfig
@@ -20,42 +22,57 @@ class WorkManagerInitializer : ContentProvider() {
private const val TAG = "WorkManagerInitializer"
private const val KEY_RESET_VERSION_CODE = "RESET_REQUIRED_VERSION_CODE"
private const val RESET_REQUIRED_VERSION_CODE = 300019L
}

override fun onCreate(): Boolean {
val context = context ?: return true
fun initializeObserver(context: Context) = object : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
safeInitialize(context)
}
}

// Clear out all system jobs if coming from an older version than RESET_REQUIRED_VERSION_CODE
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val resetVersionCode = sharedPreferences.getLong(KEY_RESET_VERSION_CODE, 0L)
if (resetVersionCode < RESET_REQUIRED_VERSION_CODE) {
val jobScheduler = context.getSystemService(
Context.JOB_SCHEDULER_SERVICE) as JobScheduler?
jobScheduler?.cancelAll()
sharedPreferences.edit {
putLong(KEY_RESET_VERSION_CODE, context.packageManager.getPackageInfo(context
.packageName, 0)?.run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
longVersionCode
} else {
@Suppress("DEPRECATION")
versionCode.toLong()
}
} ?: 0L)
}
if (BuildConfig.DEBUG) {
Log.d(TAG, "Cancelled all JobScheduler jobs due to version code of $resetVersionCode")
}
} else {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Reset not needed")
/**
* Safely initialize the [WorkManager] with the default [Configuration].
*/
internal fun safeInitialize(context: Context) {
// Clear out all system jobs if coming from an older version than RESET_REQUIRED_VERSION_CODE
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val resetVersionCode = sharedPreferences.getLong(KEY_RESET_VERSION_CODE, 0L)
if (resetVersionCode < RESET_REQUIRED_VERSION_CODE) {
val jobScheduler = context.getSystemService(
Context.JOB_SCHEDULER_SERVICE) as JobScheduler?
jobScheduler?.cancelAll()
sharedPreferences.edit {
putLong(KEY_RESET_VERSION_CODE, context.packageManager.getPackageInfo(context
.packageName, 0)?.run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
longVersionCode
} else {
@Suppress("DEPRECATION")
versionCode.toLong()
}
} ?: 0L)
}
if (BuildConfig.DEBUG) {
Log.d(TAG, "Cancelled all JobScheduler jobs due to version code of $resetVersionCode")
}
} else {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Reset not needed")
}
}
}

try {
WorkManager.initialize(context, Configuration.Builder().build())
} catch(e: IllegalStateException) {
// Ignore errors arising from multiple initializations
}
}
}

// Initialize WorkManager with the default configuration.
WorkManager.initialize(context, Configuration.Builder().build())
override fun onCreate(): Boolean {
val context = context ?: return true
safeInitialize(context)
return true
}

@@ -89,6 +89,7 @@ class MuzeiWallpaperService : GLWallpaperService(), LifecycleOwner {
@SuppressLint("InlinedApi")
override fun onCreate() {
super.onCreate()
wallpaperLifecycle.addObserver(WorkManagerInitializer.initializeObserver(this))
wallpaperLifecycle.addObserver(SourceManager(this))
wallpaperLifecycle.addObserver(NotificationUpdater(this))
wallpaperLifecycle.addObserver(WearableController(this))

0 comments on commit a661645

Please sign in to comment.