Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to 2.7.0 stable and target 31 #1032

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ class FilterActivity : AppCompatActivity() {
output.setOnClickListener {
if (outputImageUri != null) {
val viewOutput = Intent(Intent.ACTION_VIEW, outputImageUri)
if (viewOutput.resolveActivity(packageManager) != null) {
startActivity(viewOutput)
}
startActivity(viewOutput)
}
}
cancel.setOnClickListener { viewModel.cancel() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package com.example.background
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.map
import androidx.work.WorkInfo
import androidx.work.WorkManager

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

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return if (modelClass.isAssignableFrom(FilterViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
FilterViewModel(application) as T
} else {
throw IllegalArgumentException("Unknown ViewModel class")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

package com.example.background.workers.filters

import android.app.NotificationManager
import android.app.ForegroundServiceStartNotAllowedException
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.util.Log
import androidx.annotation.VisibleForTesting
import androidx.work.*
import androidx.core.os.BuildCompat
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import com.example.background.Constants
import com.example.background.library.R
import com.example.background.workers.createNotification
Expand All @@ -38,8 +42,8 @@ abstract class BaseFilterWorker(context: Context, parameters: WorkerParameters)
CoroutineWorker(context, parameters) {

override suspend fun doWork(): Result {
val resourceUri = inputData.getString(Constants.KEY_IMAGE_URI) ?:
throw IllegalArgumentException("Invalid input uri")
val resourceUri = inputData.getString(Constants.KEY_IMAGE_URI)
?: throw IllegalArgumentException("Invalid input uri")
return try {
val inputStream = inputStreamFor(applicationContext, resourceUri)
val bitmap = BitmapFactory.decodeStream(inputStream)
Expand All @@ -50,6 +54,19 @@ abstract class BaseFilterWorker(context: Context, parameters: WorkerParameters)
} catch (fileNotFoundException: FileNotFoundException) {
Log.e(TAG, "Failed to decode input stream", fileNotFoundException)
Result.failure()
} catch (e: IllegalStateException) {
// Check if this is a ForegroundServiceStartNotAllowedException and handle accordingly.
val isForegroundStartNotAllowed =
BuildCompat.isAtLeastS() && e is ForegroundServiceStartNotAllowedException
val logMessage = if (isForegroundStartNotAllowed) {
"Couldn't start a foreground service"
} else {
"An error occured"
}
Log.e(TAG, logMessage, e)
// TODO Handle depending on the Worker's use case.
// e.g. Batch long running work, clean up work or in this example fail the worker.
Result.failure()
} catch (throwable: Throwable) {
Log.e(TAG, "Error applying filter", throwable)
Result.failure()
Expand Down Expand Up @@ -95,13 +112,18 @@ abstract class BaseFilterWorker(context: Context, parameters: WorkerParameters)
* Create ForegroundInfo required to run a Worker in a foreground service.
*/
override suspend fun getForegroundInfo(): ForegroundInfo {
return ForegroundInfo(NOTIFICATION_ID, createNotification(applicationContext, id,
applicationContext.getString(R.string.notification_title_filtering_image)))
return ForegroundInfo(
NOTIFICATION_ID, createNotification(
applicationContext, id,
applicationContext.getString(R.string.notification_title_filtering_image)
)
)
}

companion object {
const val TAG = "BaseFilterWorker"
const val ASSET_PREFIX = "file:///android_asset/"

// For a real world app you might want to use a different id for each Notification.
const val NOTIFICATION_ID = 1

Expand Down
6 changes: 3 additions & 3 deletions WorkManagerSample/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ versions.rxjava2 = "2.1.3"
versions.timber = "4.7.1"
versions.transition = "1.3.0"
versions.truth = "1.0.1"
versions.work = "2.7.0-rc01"
versions.work = "2.7.0"
ext.versions = versions

def build_versions = [:]
build_versions.min_sdk = 21
build_versions.min_sdk = 14
build_versions.compile_sdk = 31
build_versions.target_sdk = 29
build_versions.target_sdk = 31
build_versions.build_tools = "29.0.3"
ext.build_versions = build_versions

Expand Down