Permalink
Browse files

Update to WorkManager 1.0.0-alpha12

Use the new Result class in place of Payload.
  • Loading branch information...
ianhanniballake committed Dec 6, 2018
1 parent 7bcdc92 commit ab70bf81aa627aa597e3f03ff438dab65b821f95
@@ -29,6 +29,7 @@ import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.Result
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.google.android.apps.muzei.api.internal.ProtocolConstants
@@ -98,24 +99,21 @@ class ArtworkLoadWorker(
override val coroutineContext = syncSingleThreadContext
override suspend fun doWork(): Payload {
override suspend fun doWork(): Result {
// Throttle artwork loads
delay(ARTWORK_LOAD_THROTTLE)
return Payload(loadArtwork())
}
private suspend fun loadArtwork(): Result {
// Now actually load the artwork
val database = MuzeiDatabase.getInstance(applicationContext)
val (authority) = database.providerDao()
.getCurrentProvider() ?: return Result.FAILURE
.getCurrentProvider() ?: return Result.failure()
if (BuildConfig.DEBUG) {
Log.d(TAG, "Artwork Load for $authority")
}
val contentUri = ProviderContract.getContentUri(authority)
try {
ContentProviderClientCompat.getClient(applicationContext, contentUri)?.use { client ->
val result = client.call(METHOD_GET_LOAD_INFO)
?: return Result.FAILURE
?: return Result.failure()
val maxLoadedArtworkId = result.getLong(KEY_MAX_LOADED_ARTWORK_ID, 0L)
val recentArtworkIds = RecentArtworkIdsConverter.fromString(
result.getString(KEY_RECENT_ARTWORK_IDS, ""))
@@ -144,7 +142,7 @@ class ArtworkLoadWorker(
}
client.call(METHOD_REQUEST_LOAD)
}
return Result.SUCCESS
return Result.success()
}
}
if (BuildConfig.DEBUG) {
@@ -155,7 +153,7 @@ class ArtworkLoadWorker(
// Is there any artwork at all?
if (allArtwork.count == 0) {
Log.w(TAG, "Unable to find any artwork for $authority")
return Result.FAILURE
return Result.failure()
}
// Okay so there's at least some artwork.
// Is it just the one artwork we're already showing?
@@ -167,7 +165,7 @@ class ArtworkLoadWorker(
if (BuildConfig.DEBUG) {
Log.i(TAG, "Provider $authority only has one artwork")
}
return Result.FAILURE
return Result.failure()
}
}
// At this point, we know there must be some artwork that isn't the current
@@ -202,7 +200,7 @@ class ArtworkLoadWorker(
Log.d(TAG, "Loaded $imageUri into id $artworkId")
}
client.call(METHOD_MARK_ARTWORK_LOADED, imageUri.toString())
return Result.SUCCESS
return Result.success()
}
}
}
@@ -215,7 +213,7 @@ class ArtworkLoadWorker(
} catch (e: Exception) {
Log.i(TAG, "Provider $authority crashed while retrieving artwork: ${e.message}")
}
return Result.RETRY
return Result.retry()
}
private suspend fun checkForValidArtwork(
@@ -31,6 +31,7 @@ import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.Result
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import androidx.work.workDataOf
@@ -170,7 +171,7 @@ class ProviderChangedWorker(
override val coroutineContext = syncSingleThreadContext
override suspend fun doWork(): Payload {
override suspend fun doWork(): Result {
val tag = inputData.getString(TAG) ?: ""
// First schedule the observer to pick up any changes fired
// by the work done in handleProviderChange
@@ -181,21 +182,17 @@ class ProviderChangedWorker(
}
}
// Now actually handle the provider change
return Payload(handleProviderChange(tag))
}
private suspend fun handleProviderChange(tag: String): Result {
val database = MuzeiDatabase.getInstance(applicationContext)
val provider = database.providerDao()
.getCurrentProvider() ?: return Result.FAILURE
.getCurrentProvider() ?: return Result.failure()
if (BuildConfig.DEBUG) {
Log.d(TAG, "Provider Change ($tag) for ${provider.authority}")
}
val contentUri = ProviderContract.getContentUri(provider.authority)
try {
ContentProviderClientCompat.getClient(applicationContext, contentUri)?.use { client ->
val result = client.call(METHOD_GET_LOAD_INFO)
?: return Result.RETRY
?: return Result.retry()
val lastLoadedTime = result.getLong(KEY_LAST_LOADED_TIME, 0L)
client.query(contentUri)?.use { allArtwork ->
val providerManager = ProviderManager.getInstance(applicationContext)
@@ -245,13 +242,13 @@ class ProviderChangedWorker(
// and haven't just called enqueueNext / enqueuePeriodic
client.call(ProtocolConstants.METHOD_REQUEST_LOAD)
}
return Result.SUCCESS
return Result.success()
}
}
} catch (e: Exception) {
Log.i(TAG, "Provider ${provider.authority} crashed while retrieving artwork: ${e.message}")
}
return Result.RETRY
return Result.retry()
}
private suspend fun isCurrentArtworkValid(
@@ -35,7 +35,7 @@ buildscript {
roomVersion = "2.0.0"
pagingVersion = "2.0.0"
navigationVersion = "1.0.0-alpha07"
workManagerVersion = "1.0.0-alpha11"
workManagerVersion = "1.0.0-alpha12"
playServicesWearableVersion = "16.0.1"
firebaseCoreVersion = "16.0.4"
firebasePerfVersion = "16.2.0"
@@ -22,6 +22,7 @@ import androidx.core.net.toUri
import androidx.work.Constraints
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.Result
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
@@ -53,12 +54,12 @@ class UnsplashExampleWorker(
UnsplashService.popularPhotos()
} catch (e: IOException) {
Log.w(TAG, "Error reading Unsplash response", e)
return Result.RETRY
return Result.retry()
}
if (photos.isEmpty()) {
Log.w(TAG, "No photos returned from API.")
return Result.FAILURE
return Result.failure()
}
val providerClient = ProviderContract.getProviderClient(
@@ -75,6 +76,6 @@ class UnsplashExampleWorker(
metadata = photo.user.links.webUri.toString()
}
})
return Result.SUCCESS
return Result.success()
}
}
@@ -27,6 +27,7 @@ import androidx.work.CoroutineWorker
import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.Result
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.google.android.apps.muzei.api.provider.Artwork
@@ -109,13 +110,11 @@ class FeaturedArtWorker(
override val coroutineContext = SINGLE_THREAD_CONTEXT
override suspend fun doWork(): Payload = Payload(loadFeaturedArt())
private fun loadFeaturedArt(): Result {
override suspend fun doWork(): Result {
val jsonObject: JSONObject?
try {
jsonObject = fetchJsonObject(QUERY_URL)
val imageUri = jsonObject.optString(KEY_IMAGE_URI) ?: return Result.SUCCESS
val imageUri = jsonObject.optString(KEY_IMAGE_URI) ?: return Result.success()
val artwork = Artwork().apply {
persistentUri = imageUri.toUri()
token = jsonObject.optString(KEY_TOKEN).takeUnless { it.isEmpty() } ?: imageUri
@@ -132,10 +131,10 @@ class FeaturedArtWorker(
.addArtwork(artwork)
} catch (e: JSONException) {
Log.e(TAG, "Error reading JSON", e)
return Result.RETRY
return Result.retry()
} catch (e: IOException) {
Log.e(TAG, "Error reading JSON", e)
return Result.RETRY
return Result.retry()
}
val nextTime: Date? = jsonObject.optString("nextTime")?.takeUnless {
@@ -170,7 +169,7 @@ class FeaturedArtWorker(
sp.edit {
putLong(PREF_NEXT_UPDATE_MILLIS, nextUpdateMillis)
}
return Result.SUCCESS
return Result.success()
}
@Throws(IOException::class, JSONException::class)
@@ -35,6 +35,7 @@ import androidx.core.content.ContextCompat
import androidx.exifinterface.media.ExifInterface
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.Result
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
@@ -99,9 +100,9 @@ class GalleryScanWorker(
return if (chosenPhoto != null) {
scanChosenPhoto(providerClient, chosenPhoto)
deleteMediaUris(providerClient)
Result.SUCCESS
Result.success()
} else {
Result.FAILURE
Result.failure()
}
}
val chosenPhotos = GalleryDatabase.getInstance(applicationContext)
@@ -113,7 +114,7 @@ class GalleryScanWorker(
scanChosenPhoto(providerClient, chosenPhoto)
}
deleteMediaUris(providerClient)
return Result.SUCCESS
return Result.success()
}
return addMediaUri(providerClient)
}
@@ -230,7 +231,7 @@ class GalleryScanWorker(
if (ContextCompat.checkSelfPermission(applicationContext,
android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Log.w(TAG, "Missing read external storage permission.")
return Result.FAILURE
return Result.failure()
}
applicationContext.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
@@ -240,7 +241,7 @@ class GalleryScanWorker(
val count = data.count
if (count == 0) {
Log.d(TAG, "No photos in the gallery.")
return Result.FAILURE
return Result.failure()
}
val lastToken = providerClient.lastAddedArtwork?.token
@@ -259,17 +260,17 @@ class GalleryScanWorker(
providerClient.addArtwork(createArtwork(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
imageUri))
return Result.SUCCESS
return Result.success()
}
}
}
if (BuildConfig.DEBUG) {
Log.i(TAG, "Unable to find any other valid photos in the gallery")
}
return Result.FAILURE
return Result.failure()
} ?: run {
Log.w(TAG, "Empty cursor.")
return Result.FAILURE
return Result.failure()
}
}
@@ -26,6 +26,7 @@ import androidx.annotation.RequiresApi
import androidx.work.Constraints
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.Result
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
@@ -78,6 +79,6 @@ class ArtworkComplicationWorker(
}
// Reschedule the job to listen for the next change
scheduleComplicationUpdate()
return Result.SUCCESS
return Result.success()
}
}
@@ -22,6 +22,7 @@ import android.content.pm.PackageManager
import android.net.Uri
import android.util.Log
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.Result
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
@@ -57,9 +58,7 @@ class DataLayerLoadWorker(
}
}
override fun doWork(): Result = loadFromDataLayer()
private fun loadFromDataLayer(): Result {
override fun doWork(): Result {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Loading artwork from the DataLayer")
}
@@ -71,7 +70,7 @@ class DataLayerLoadWorker(
if (BuildConfig.DEBUG) {
Log.i(TAG, "Error getting artwork DataItem")
}
return Result.FAILURE
return Result.failure()
}
val dataMap = dataItemBuffer.map {
DataMapItem.fromDataItem(it).dataMap
@@ -81,7 +80,7 @@ class DataLayerLoadWorker(
if (BuildConfig.DEBUG) {
Log.w(TAG, "No artwork datamap found.")
}
return Result.FAILURE
return Result.failure()
}
val result = Tasks.await<DataClient.GetFdForAssetResponse>(
dataClient.getFdForAsset(dataMap.getAsset("image")))
@@ -93,7 +92,7 @@ class DataLayerLoadWorker(
}
} catch (e: IOException) {
Log.e(TAG, "Unable to save asset to local storage", e)
return Result.FAILURE
return Result.failure()
} finally {
result.release()
}
@@ -112,13 +111,13 @@ class DataLayerLoadWorker(
Log.d(TAG, "Successfully wrote artwork to $artworkUri")
}
}
return Result.SUCCESS
return Result.success()
} catch (e: ExecutionException) {
Log.w(TAG, "Error getting artwork from Wear Data Layer", e)
return Result.FAILURE
return Result.failure()
} catch (e: InterruptedException) {
Log.w(TAG, "Error getting artwork from Wear Data Layer", e)
return Result.FAILURE
return Result.failure()
}
}
}

0 comments on commit ab70bf8

Please sign in to comment.