Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use WorkManager in place of Firebase Job Dispatcher
Use WorkManager's support for content URI listening to replace use of Firebase Job Dispatcher.
- Loading branch information
1 parent
b8339d7
commit 948a83d
Showing
6 changed files
with
83 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 0 additions & 81 deletions
81
...rc/main/java/com/google/android/apps/muzei/complications/ArtworkComplicationJobService.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...le/src/main/java/com/google/android/apps/muzei/complications/ArtworkComplicationWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |