File tree 4 files changed +38
-3
lines changed
app/src/main/java/com/example/background
4 files changed +38
-3
lines changed Original file line number Diff line number Diff line change 2
2
3
3
An app that blurs photos and saves the result to a file, using WorkManager.
4
4
5
- <!-- < p align="center">
5
+ <p align =" center " >
6
6
<img src =" screenshot.png " style =" width :528px ;max-width : 100% ;" >
7
- </p> -->
7
+ </p >
8
8
9
9
## Features
10
10
11
11
- adding WorkManager to a project.
12
12
- scheduling a simple task.
13
13
- handling input and output parameters.
14
14
- chaining work.
15
- - ensuring unique work.
15
+ - naming unique work.
16
+ - tagging work.
16
17
- displaying work status in the UI.
17
18
- showing final output.
18
19
- cancelling work.
20
+ - adding constraints.
19
21
20
22
Based on [ Background work with WorkManager - Kotlin] ( https://developer.android.com/codelabs/android-workmanager ) by Google Codelabs (2022).
Original file line number Diff line number Diff line change 16
16
17
17
package com.example.background
18
18
19
+ import android.content.Intent
19
20
import android.os.Bundle
20
21
import android.view.View
21
22
import androidx.activity.viewModels
@@ -39,8 +40,22 @@ class BlurActivity : AppCompatActivity() {
39
40
setContentView(binding.root)
40
41
41
42
binding.goButton.setOnClickListener { viewModel.applyBlur(blurLevel) }
43
+
42
44
// observe work status
43
45
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() }
44
59
}
45
60
46
61
/* *
@@ -84,6 +99,11 @@ class BlurActivity : AppCompatActivity() {
84
99
val workInfo = listOfWorkInfo[0 ]
85
100
if (workInfo.state.isFinished) {
86
101
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
+ }
87
107
} else {
88
108
showWorkInProgress()
89
109
}
Original file line number Diff line number Diff line change @@ -76,8 +76,17 @@ class BlurViewModel(application: Application) : ViewModel() {
76
76
}
77
77
continuation = continuation.then(blurBuilder.build())
78
78
}
79
+
80
+ // create charging constraint
81
+ val constraints = Constraints .Builder ()
82
+ .setRequiresCharging(true )
83
+ // .setRequiresStorageNotLow(true)
84
+ // .setRequiredNetworkType(NetworkType.CONNECTED)
85
+ .build()
86
+
79
87
// add work request to save the image to the filesystem
80
88
val save = OneTimeWorkRequest .Builder (SaveImageToFileWorker ::class .java)
89
+ .setConstraints(constraints)
81
90
.addTag(TAG_OUTPUT )
82
91
.build()
83
92
continuation = continuation.then(save)
@@ -119,6 +128,10 @@ class BlurViewModel(application: Application) : ViewModel() {
119
128
outputUri = uriOrNull(outputImageUri)
120
129
}
121
130
131
+ internal fun cancelWork () {
132
+ workManager.cancelUniqueWork(IMAGE_MANIPULATION_WORK_NAME )
133
+ }
134
+
122
135
class BlurViewModelFactory (private val application : Application ) : ViewModelProvider.Factory {
123
136
124
137
override fun <T : ViewModel ?> create (modelClass : Class <T >): T {
You can’t perform that action at this time.
0 commit comments