Skip to content

Commit

Permalink
Use WorkManager in place of Firebase Job Dispatcher
Browse files Browse the repository at this point in the history
Use WorkManager's support for content URI listening to replace use of Firebase Job Dispatcher.
  • Loading branch information
ianhanniballake committed May 24, 2018
1 parent b8339d7 commit 948a83d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 92 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -27,11 +27,11 @@ buildscript {
lifecycleVersion = "1.1.1"
roomVersion = "1.1.1-rc1"
pagingVersion = "1.0.0"
workManagerVersion = "1.0.0-alpha02"
playServicesWearableVersion = "15.0.1"
firebaseCoreVersion = "16.0.0"
firebasePerfVersion = "16.0.0"
crashlyticsVersion = "2.9.2"
jobDispatcherVersion = "0.8.5"
wearableVersion = "2.3.0"
okhttpVersion = "3.10.0"
picassoVersion = "2.71828"
Expand Down
2 changes: 1 addition & 1 deletion wearable/build.gradle
Expand Up @@ -85,6 +85,7 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "androidx.core:core-ktx:$rootProject.ext.ktxVersion"
implementation "android.arch.work:work-runtime-ktx:$workManagerVersion"
implementation "com.android.support:support-compat:$supportLibraryVersion"
implementation "com.android.support:exifinterface:$supportLibraryVersion"
implementation "com.android.support:wear:$supportLibraryVersion"
Expand All @@ -98,7 +99,6 @@ dependencies {
implementation "com.google.firebase:firebase-core:$firebaseCoreVersion"
implementation "com.google.firebase:firebase-perf:$firebasePerfVersion"
implementation "com.crashlytics.sdk.android:crashlytics:$crashlyticsVersion"
implementation "com.firebase:firebase-jobdispatcher:$jobDispatcherVersion"

implementation project(':api')
implementation project(':extensions')
Expand Down
7 changes: 0 additions & 7 deletions wearable/src/main/AndroidManifest.xml
Expand Up @@ -91,13 +91,6 @@
android:name="android.support.wearable.complications.UPDATE_PERIOD_SECONDS"
android:value="0"/>
</service>
<service
android:name="com.google.android.apps.muzei.complications.ArtworkComplicationJobService"
android:exported="false">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>

<service
android:name="com.google.android.apps.muzei.MuzeiWatchFace"
Expand Down

This file was deleted.

Expand Up @@ -71,7 +71,7 @@ class ArtworkComplicationProviderService : ComplicationProviderService() {
if (BuildConfig.DEBUG) {
Log.d(TAG, "addComplication: $complications")
}
ArtworkComplicationJobService.scheduleComplicationUpdateJob(this)
ArtworkComplicationWorker.scheduleComplicationUpdate()
}

override fun onComplicationDeactivated(complicationId: Int) {
Expand All @@ -87,7 +87,7 @@ class ArtworkComplicationProviderService : ComplicationProviderService() {
Log.d(TAG, "Current complications: $complications")
}
if (complications.isEmpty()) {
ArtworkComplicationJobService.cancelComplicationUpdateJob(this)
ArtworkComplicationWorker.cancelComplicationUpdate()
}
}

Expand Down
@@ -0,0 +1,79 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.apps.muzei.complications

import android.content.ComponentName
import android.os.Build
import android.preference.PreferenceManager
import android.support.annotation.RequiresApi
import android.support.wearable.complications.ProviderUpdateRequester
import android.util.Log
import androidx.work.Constraints
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.Worker
import com.google.android.apps.muzei.api.MuzeiContract
import net.nurik.roman.muzei.BuildConfig
import java.util.TreeSet

/**
* Worker which listens for artwork change events and updates the Artwork Complication
*/
@RequiresApi(Build.VERSION_CODES.N)
class ArtworkComplicationWorker : Worker() {

companion object {
private const val TAG = "ArtworkComplication"

internal fun scheduleComplicationUpdate() {
val workManager = WorkManager.getInstance()
workManager.beginUniqueWork(TAG, ExistingWorkPolicy.REPLACE,
OneTimeWorkRequestBuilder<ArtworkComplicationWorker>()
.setConstraints(Constraints.Builder()
.addContentUriTrigger(MuzeiContract.Artwork.CONTENT_URI, true)
.build())
.build()
).enqueue()
if (BuildConfig.DEBUG) {
Log.d(TAG, "Work scheduled")
}
}

internal fun cancelComplicationUpdate() {
val workManager = WorkManager.getInstance()
workManager.cancelUniqueWork(TAG)
if (BuildConfig.DEBUG) {
Log.d(TAG, "Work cancelled")
}
}
}

override fun doWork(): WorkerResult {
val providerUpdateRequester = ProviderUpdateRequester(applicationContext,
ComponentName(applicationContext, ArtworkComplicationProviderService::class.java))
val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
val complicationSet = preferences.getStringSet(
ArtworkComplicationProviderService.KEY_COMPLICATION_IDS, TreeSet())
if (complicationSet?.isNotEmpty() == true) {
providerUpdateRequester.requestUpdate(*complicationSet.map { Integer.parseInt(it) }.toIntArray())
}
// Reschedule the job to listen for the next change
scheduleComplicationUpdate()
return WorkerResult.SUCCESS
}
}

0 comments on commit 948a83d

Please sign in to comment.