Skip to content

Commit c3ed96d

Browse files
committed
add cancelling and constraints
1 parent 084a541 commit c3ed96d

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

11-blur-o-matic/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
An app that blurs photos and saves the result to a file, using WorkManager.
44

5-
<!-- <p align="center">
5+
<p align="center">
66
<img src="screenshot.png" style="width:528px;max-width: 100%;">
7-
</p> -->
7+
</p>
88

99
## Features
1010

1111
- adding WorkManager to a project.
1212
- scheduling a simple task.
1313
- handling input and output parameters.
1414
- chaining work.
15-
- ensuring unique work.
15+
- naming unique work.
16+
- tagging work.
1617
- displaying work status in the UI.
1718
- showing final output.
1819
- cancelling work.
20+
- adding constraints.
1921

2022
Based on [Background work with WorkManager - Kotlin](https://developer.android.com/codelabs/android-workmanager) by Google Codelabs (2022).

11-blur-o-matic/app/src/main/java/com/example/background/BlurActivity.kt

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.background
1818

19+
import android.content.Intent
1920
import android.os.Bundle
2021
import android.view.View
2122
import androidx.activity.viewModels
@@ -39,8 +40,22 @@ class BlurActivity : AppCompatActivity() {
3940
setContentView(binding.root)
4041

4142
binding.goButton.setOnClickListener { viewModel.applyBlur(blurLevel) }
43+
4244
// observe work status
4345
viewModel.outputWorkInfos.observe(this, workInfosObserver())
46+
47+
// setup view output image file button
48+
binding.seeFileButton.setOnClickListener {
49+
viewModel.outputUri?.let { currentUri ->
50+
val actionView = Intent(Intent.ACTION_VIEW, currentUri)
51+
actionView.resolveActivity(packageManager)?.run {
52+
startActivity(actionView)
53+
}
54+
}
55+
}
56+
57+
// hookup the cancel button
58+
binding.cancelButton.setOnClickListener { viewModel.cancelWork() }
4459
}
4560

4661
/**
@@ -84,6 +99,11 @@ class BlurActivity : AppCompatActivity() {
8499
val workInfo = listOfWorkInfo[0]
85100
if (workInfo.state.isFinished) {
86101
showWorkFinished()
102+
val outputImageUri = workInfo.outputData.getString(KEY_IMAGE_URI)
103+
if (!outputImageUri.isNullOrEmpty()) {
104+
viewModel.setOutputUri(outputImageUri)
105+
binding.seeFileButton.visibility = View.VISIBLE
106+
}
87107
} else {
88108
showWorkInProgress()
89109
}

11-blur-o-matic/app/src/main/java/com/example/background/BlurViewModel.kt

+13
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,17 @@ class BlurViewModel(application: Application) : ViewModel() {
7676
}
7777
continuation = continuation.then(blurBuilder.build())
7878
}
79+
80+
// create charging constraint
81+
val constraints = Constraints.Builder()
82+
.setRequiresCharging(true)
83+
//.setRequiresStorageNotLow(true)
84+
//.setRequiredNetworkType(NetworkType.CONNECTED)
85+
.build()
86+
7987
// add work request to save the image to the filesystem
8088
val save = OneTimeWorkRequest.Builder(SaveImageToFileWorker::class.java)
89+
.setConstraints(constraints)
8190
.addTag(TAG_OUTPUT)
8291
.build()
8392
continuation = continuation.then(save)
@@ -119,6 +128,10 @@ class BlurViewModel(application: Application) : ViewModel() {
119128
outputUri = uriOrNull(outputImageUri)
120129
}
121130

131+
internal fun cancelWork() {
132+
workManager.cancelUniqueWork(IMAGE_MANIPULATION_WORK_NAME)
133+
}
134+
122135
class BlurViewModelFactory(private val application: Application) : ViewModelProvider.Factory {
123136

124137
override fun <T : ViewModel?> create(modelClass: Class<T>): T {

11-blur-o-matic/screenshot.png

498 KB
Loading

0 commit comments

Comments
 (0)