Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit d70aea3

Browse files
Update to 2.7.0 stable and target 31
1 parent efe4f71 commit d70aea3

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

WorkManagerSample/app/src/main/java/com/example/background/FilterActivity.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ class FilterActivity : AppCompatActivity() {
7575
output.setOnClickListener {
7676
if (outputImageUri != null) {
7777
val viewOutput = Intent(Intent.ACTION_VIEW, outputImageUri)
78-
if (viewOutput.resolveActivity(packageManager) != null) {
79-
startActivity(viewOutput)
80-
}
78+
startActivity(viewOutput)
8179
}
8280
}
8381
cancel.setOnClickListener { viewModel.cancel() }

WorkManagerSample/app/src/main/java/com/example/background/FilterViewModel.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package com.example.background
1919
import android.app.Application
2020
import androidx.lifecycle.ViewModel
2121
import androidx.lifecycle.ViewModelProvider
22-
import androidx.lifecycle.map
23-
import androidx.work.WorkInfo
2422
import androidx.work.WorkManager
2523

2624
/**
@@ -48,6 +46,7 @@ class FilterViewModelFactory(private val application: Application) : ViewModelPr
4846

4947
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
5048
return if (modelClass.isAssignableFrom(FilterViewModel::class.java)) {
49+
@Suppress("UNCHECKED_CAST")
5150
FilterViewModel(application) as T
5251
} else {
5352
throw IllegalArgumentException("Unknown ViewModel class")

WorkManagerSample/lib/src/main/java/com/example/background/workers/filters/BaseFilterWorker.kt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

1717
package com.example.background.workers.filters
1818

19-
import android.app.NotificationManager
19+
import android.app.ForegroundServiceStartNotAllowedException
2020
import android.content.Context
2121
import android.graphics.Bitmap
2222
import android.graphics.BitmapFactory
2323
import android.net.Uri
2424
import android.util.Log
2525
import androidx.annotation.VisibleForTesting
26-
import androidx.work.*
26+
import androidx.core.os.BuildCompat
27+
import androidx.work.CoroutineWorker
28+
import androidx.work.ForegroundInfo
29+
import androidx.work.WorkerParameters
30+
import androidx.work.workDataOf
2731
import com.example.background.Constants
2832
import com.example.background.library.R
2933
import com.example.background.workers.createNotification
@@ -38,8 +42,8 @@ abstract class BaseFilterWorker(context: Context, parameters: WorkerParameters)
3842
CoroutineWorker(context, parameters) {
3943

4044
override suspend fun doWork(): Result {
41-
val resourceUri = inputData.getString(Constants.KEY_IMAGE_URI) ?:
42-
throw IllegalArgumentException("Invalid input uri")
45+
val resourceUri = inputData.getString(Constants.KEY_IMAGE_URI)
46+
?: throw IllegalArgumentException("Invalid input uri")
4347
return try {
4448
val inputStream = inputStreamFor(applicationContext, resourceUri)
4549
val bitmap = BitmapFactory.decodeStream(inputStream)
@@ -50,6 +54,19 @@ abstract class BaseFilterWorker(context: Context, parameters: WorkerParameters)
5054
} catch (fileNotFoundException: FileNotFoundException) {
5155
Log.e(TAG, "Failed to decode input stream", fileNotFoundException)
5256
Result.failure()
57+
} catch (e: IllegalStateException) {
58+
// Check if this is a ForegroundServiceStartNotAllowedException and handle accordingly.
59+
val isForegroundStartNotAllowed =
60+
BuildCompat.isAtLeastS() && e is ForegroundServiceStartNotAllowedException
61+
val logMessage = if (isForegroundStartNotAllowed) {
62+
"Couldn't start a foreground service"
63+
} else {
64+
"An error occured"
65+
}
66+
Log.e(TAG, logMessage, e)
67+
// TODO Handle depending on the Worker's use case.
68+
// e.g. Batch long running work, clean up work or in this example fail the worker.
69+
Result.failure()
5370
} catch (throwable: Throwable) {
5471
Log.e(TAG, "Error applying filter", throwable)
5572
Result.failure()
@@ -95,13 +112,18 @@ abstract class BaseFilterWorker(context: Context, parameters: WorkerParameters)
95112
* Create ForegroundInfo required to run a Worker in a foreground service.
96113
*/
97114
override suspend fun getForegroundInfo(): ForegroundInfo {
98-
return ForegroundInfo(NOTIFICATION_ID, createNotification(applicationContext, id,
99-
applicationContext.getString(R.string.notification_title_filtering_image)))
115+
return ForegroundInfo(
116+
NOTIFICATION_ID, createNotification(
117+
applicationContext, id,
118+
applicationContext.getString(R.string.notification_title_filtering_image)
119+
)
120+
)
100121
}
101122

102123
companion object {
103124
const val TAG = "BaseFilterWorker"
104125
const val ASSET_PREFIX = "file:///android_asset/"
126+
105127
// For a real world app you might want to use a different id for each Notification.
106128
const val NOTIFICATION_ID = 1
107129

WorkManagerSample/versions.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ versions.rxjava2 = "2.1.3"
6262
versions.timber = "4.7.1"
6363
versions.transition = "1.3.0"
6464
versions.truth = "1.0.1"
65-
versions.work = "2.7.0-rc01"
65+
versions.work = "2.7.0"
6666
ext.versions = versions
6767

6868
def build_versions = [:]
69-
build_versions.min_sdk = 21
69+
build_versions.min_sdk = 14
7070
build_versions.compile_sdk = 31
71-
build_versions.target_sdk = 29
71+
build_versions.target_sdk = 31
7272
build_versions.build_tools = "29.0.3"
7373
ext.build_versions = build_versions
7474

0 commit comments

Comments
 (0)