Repository navigation
Migrate Persistent Background Work snippets #1133
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
Open
djubinville
wants to merge
7
commits into
android:main
Choose a base branch
from
StellarElements:bg-work-persistent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
13030b9
Add code snippets for persistent background work
djubinville d53aa88
Make placeholder Worker classes public to fix WorkerHasAPublicModifie…
djubinville 30aa6be
Address code review feedback on persistent background work snippets
djubinville abf5efb
Address D18, D22, D26a, and D29 quality gate findings
djubinville 90dc99c
Update backgroundwork AndroidManifest.xml to satisfy WorkManager lint…
djubinville 270d61d
Address review feedback on persistent background work snippets
djubinville 8a9b744
Replace placeholder notification vector icon and remove unused depend…
djubinville File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
backgroundwork/src/main/java/com/example/snippets/backgroundwork/CoroutineWorkerThreading.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.snippets.backgroundwork | ||
|
|
||
| import android.content.ComponentName | ||
| import android.content.Context | ||
| import androidx.work.CoroutineWorker | ||
| import androidx.work.Data | ||
| import androidx.work.OneTimeWorkRequest | ||
| import androidx.work.WorkerParameters | ||
| import androidx.work.multiprocess.RemoteCoroutineWorker | ||
| import androidx.work.multiprocess.RemoteListenableWorker.ARGUMENT_CLASS_NAME | ||
| import androidx.work.multiprocess.RemoteListenableWorker.ARGUMENT_PACKAGE_NAME | ||
| import androidx.work.multiprocess.RemoteWorkerService | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
|
|
||
| private object SimpleCoroutineDownloadWorker { | ||
| // [START android_background_coroutine_download_worker] | ||
| class CoroutineDownloadWorker( | ||
| context: Context, | ||
| params: WorkerParameters | ||
| ) : CoroutineWorker(context, params) { | ||
|
|
||
| override suspend fun doWork(): Result { | ||
| val data = downloadSynchronously("https://www.google.com") | ||
| saveData(data) | ||
| return Result.success() | ||
| } | ||
| } | ||
| // [END android_background_coroutine_download_worker] | ||
| } | ||
|
|
||
| private object WithContextCoroutineDownloadWorker { | ||
| // [START android_background_coroutine_download_worker_with_context] | ||
| class CoroutineDownloadWorker( | ||
| context: Context, | ||
| params: WorkerParameters | ||
| ) : CoroutineWorker(context, params) { | ||
|
|
||
| override suspend fun doWork(): Result { | ||
| return withContext(Dispatchers.IO) { | ||
| val data = downloadSynchronously("https://www.google.com") | ||
| saveData(data) | ||
| Result.success() | ||
| } | ||
| } | ||
| } | ||
| // [END android_background_coroutine_download_worker_with_context] | ||
| } | ||
|
|
||
| private fun remoteCoroutineWorkerRequest(): OneTimeWorkRequest { | ||
| // [START android_background_remote_coroutine_worker_request] | ||
| val PACKAGE_NAME = "com.example.background.multiprocess" | ||
|
|
||
| val serviceName = RemoteWorkerService::class.java.name | ||
| val componentName = ComponentName(PACKAGE_NAME, serviceName) | ||
|
|
||
| val data: Data = Data.Builder() | ||
| .putString(ARGUMENT_PACKAGE_NAME, componentName.packageName) | ||
| .putString(ARGUMENT_CLASS_NAME, componentName.className) | ||
| .build() | ||
|
|
||
| return OneTimeWorkRequest.Builder(ExampleRemoteCoroutineWorker::class.java) | ||
| .setInputData(data) | ||
| .build() | ||
| // [END android_background_remote_coroutine_worker_request] | ||
| } | ||
|
|
||
| private fun downloadSynchronously(url: String): String = "" | ||
| private fun saveData(data: String) {} | ||
|
|
||
| class ExampleRemoteCoroutineWorker( | ||
| context: Context, | ||
| parameters: WorkerParameters | ||
| ) : RemoteCoroutineWorker(context, parameters) { | ||
| override suspend fun doRemoteWork(): Result = Result.success() | ||
| } |
43 changes: 43 additions & 0 deletions
43
backgroundwork/src/main/java/com/example/snippets/backgroundwork/CustomConfiguration.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.snippets.backgroundwork | ||
|
|
||
| import android.app.Application | ||
| import android.content.Context | ||
| import androidx.work.Configuration | ||
| import androidx.work.WorkManager | ||
|
|
||
| // [START android_background_custom_configuration_on_demand] | ||
| class MyApplication() : Application(), Configuration.Provider { | ||
| override val workManagerConfiguration: Configuration | ||
| get() = Configuration.Builder() | ||
| .setMinimumLoggingLevel(android.util.Log.INFO) | ||
| .build() | ||
| } | ||
| // [END android_background_custom_configuration_on_demand] | ||
|
|
||
| private fun Context.manualInitialization() { | ||
| // [START android_background_custom_configuration_manual_init] | ||
| // provide custom configuration. | ||
| val myConfig = Configuration.Builder() | ||
| .setMinimumLoggingLevel(android.util.Log.INFO) | ||
| .build() | ||
|
|
||
| // initialize WorkManager. | ||
| WorkManager.initialize(this, myConfig) | ||
| // [END android_background_custom_configuration_manual_init] | ||
| } |
194 changes: 194 additions & 0 deletions
194
backgroundwork/src/main/java/com/example/snippets/backgroundwork/DefineWork.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.snippets.backgroundwork | ||
|
|
||
| import android.app.Notification | ||
| import android.content.Context | ||
| import androidx.work.BackoffPolicy | ||
| import androidx.work.Constraints | ||
| import androidx.work.CoroutineWorker | ||
| import androidx.work.ForegroundInfo | ||
| import androidx.work.NetworkType | ||
| import androidx.work.OneTimeWorkRequest | ||
| import androidx.work.OneTimeWorkRequestBuilder | ||
| import androidx.work.OutOfQuotaPolicy | ||
| import androidx.work.PeriodicWorkRequestBuilder | ||
| import androidx.work.WorkManager | ||
| import androidx.work.WorkRequest | ||
| import androidx.work.Worker | ||
| import androidx.work.WorkerParameters | ||
| import androidx.work.workDataOf | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| private fun enqueueWorkSample(myContext: Context) { | ||
| // [START android_background_enqueue_work_request] | ||
| val myWorkRequest = OneTimeWorkRequestBuilder<MyWork>().build() | ||
| WorkManager.getInstance(myContext).enqueue(myWorkRequest) | ||
| // [END android_background_enqueue_work_request] | ||
| } | ||
|
|
||
| private fun scheduleOneTimeWorkFrom() { | ||
| // [START android_background_schedule_one_time_work_from] | ||
| val myWorkRequest = OneTimeWorkRequest.from(MyWork::class.java) | ||
| // [END android_background_schedule_one_time_work_from] | ||
| } | ||
|
|
||
| private fun scheduleOneTimeWorkBuilder() { | ||
| // [START android_background_schedule_one_time_work_builder] | ||
| val uploadWorkRequest: WorkRequest = | ||
| OneTimeWorkRequestBuilder<MyWork>() | ||
| // Additional configuration. | ||
| .build() | ||
| // [END android_background_schedule_one_time_work_builder] | ||
| } | ||
|
|
||
| private fun expeditedWorkRequest(context: Context) { | ||
| // [START android_background_expedited_work_request] | ||
| val request = OneTimeWorkRequestBuilder<SyncWorker>() | ||
| .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) | ||
| .build() | ||
|
|
||
| WorkManager.getInstance(context) | ||
| .enqueue(request) | ||
| // [END android_background_expedited_work_request] | ||
| } | ||
|
|
||
| // [START android_background_expedited_coroutine_worker] | ||
| class ExpeditedWorker(appContext: Context, workerParams: WorkerParameters) : | ||
| CoroutineWorker(appContext, workerParams) { | ||
|
|
||
| override suspend fun getForegroundInfo(): ForegroundInfo { | ||
| return ForegroundInfo( | ||
| NOTIFICATION_ID, createNotification() | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun doWork(): Result { | ||
| TODO() | ||
| } | ||
|
|
||
| private fun createNotification(): Notification { | ||
| TODO() | ||
| } | ||
|
|
||
| // [START_EXCLUDE silent] | ||
| companion object { | ||
| private const val NOTIFICATION_ID = 1 | ||
| } | ||
| // [END_EXCLUDE] | ||
| } | ||
| // [END android_background_expedited_coroutine_worker] | ||
|
|
||
| private fun schedulePeriodicWork() { | ||
| // [START android_background_periodic_work_request] | ||
| val saveRequest = | ||
| PeriodicWorkRequestBuilder<SaveImageToFileWorker>(1, TimeUnit.HOURS) | ||
| // Additional configuration. | ||
| .build() | ||
| // [END android_background_periodic_work_request] | ||
| } | ||
|
|
||
| private fun schedulePeriodicWorkFlex() { | ||
| // [START android_background_periodic_work_request_flex] | ||
| val myUploadWork = PeriodicWorkRequestBuilder<SaveImageToFileWorker>( | ||
| 1, TimeUnit.HOURS, // repeatInterval (the period cycle). | ||
| 15, TimeUnit.MINUTES | ||
| ) // flexInterval. | ||
| .build() | ||
| // [END android_background_periodic_work_request_flex] | ||
| } | ||
|
|
||
| private fun workConstraints() { | ||
| // [START android_background_work_constraints] | ||
| val constraints = Constraints.Builder() | ||
| .setRequiredNetworkType(NetworkType.UNMETERED) | ||
| .setRequiresCharging(true) | ||
| .build() | ||
|
|
||
| val myWorkRequest: WorkRequest = | ||
| OneTimeWorkRequestBuilder<MyWork>() | ||
| .setConstraints(constraints) | ||
| .build() | ||
| // [END android_background_work_constraints] | ||
| } | ||
|
|
||
| private fun delayedWork() { | ||
| // [START android_background_delayed_work] | ||
| val myWorkRequest = OneTimeWorkRequestBuilder<MyWork>() | ||
| .setInitialDelay(10, TimeUnit.MINUTES) | ||
| .build() | ||
| // [END android_background_delayed_work] | ||
| } | ||
|
|
||
| private fun retryBackoffPolicy() { | ||
| // [START android_background_retry_backoff_policy] | ||
| val myWorkRequest = OneTimeWorkRequestBuilder<MyWork>() | ||
| .setBackoffCriteria( | ||
| BackoffPolicy.LINEAR, | ||
| WorkRequest.MIN_BACKOFF_MILLIS, | ||
| TimeUnit.MILLISECONDS | ||
| ) | ||
| .build() | ||
| // [END android_background_retry_backoff_policy] | ||
| } | ||
|
|
||
| private fun tagWork() { | ||
| // [START android_background_tag_work] | ||
| val myWorkRequest = OneTimeWorkRequestBuilder<MyWork>() | ||
| .addTag("cleanup") | ||
| .build() | ||
| // [END android_background_tag_work] | ||
| } | ||
|
|
||
| // [START android_background_assign_input_data] | ||
| // Define the Worker requiring input. | ||
| class UploadWork(appContext: Context, workerParams: WorkerParameters) : | ||
| Worker(appContext, workerParams) { | ||
|
|
||
| override fun doWork(): Result { | ||
| val imageUriInput = | ||
| inputData.getString("IMAGE_URI") ?: return Result.failure() | ||
|
|
||
| uploadFile(imageUriInput) | ||
| return Result.success() | ||
| } | ||
| // [START_EXCLUDE] | ||
| private fun uploadFile(uri: String) {} | ||
| // [END_EXCLUDE] | ||
| } | ||
|
|
||
| // Create a WorkRequest for your Worker and sending it input. | ||
| val myUploadWork = OneTimeWorkRequestBuilder<UploadWork>() | ||
| .setInputData( | ||
| workDataOf( | ||
| "IMAGE_URI" to "http://..." | ||
| ) | ||
| ) | ||
| .build() | ||
| // [END android_background_assign_input_data] | ||
|
|
||
| class MyWork(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) { | ||
| override fun doWork(): Result = Result.success() | ||
| } | ||
|
|
||
| class SyncWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) { | ||
| override fun doWork(): Result = Result.success() | ||
| } | ||
|
|
||
| class SaveImageToFileWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) { | ||
| override fun doWork(): Result = Result.success() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simplify lines 40-47 and just fill in the ... even though that would be a visible diff
val myWorkRequest = OneTimeWorkRequestBuilder().build()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping the
[START_EXCLUDE silent]D23 Twin (// val myWorkRequest = ...) and parameter binding here to comply with our snippet migration audit rules (D5 Smallest Delta and D23 Same-Line Placeholders), which prohibit inventing an unruled visible RHS when the original DAC snippet usedval myWorkRequest = ...(see audit review pass in commit30aa6be/abf5efb6).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think we should make the code change because line 43 won't be compiled. So I prefer to reduce the amount of code in comments.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplified
android_background_enqueue_work_requestinDefineWork.ktto useval myWorkRequest = OneTimeWorkRequestBuilder<MyWork>().build()directly (removing the[START_EXCLUDE silent]block-comment twin) in commit270d61d1, and documented the visible diff in the PR description.Skill review note for @hamen (
android-snippets-experimental):SKILL.mdlines 150–164) and D5 (Permitted Diff Justifications) (SKILL.mdlines 175–179).= ...) must follow a strict 3-step test:/* val x: Type = ... */inside[START_EXCLUDE silent]followed by a hidden compiling binding) when a type or provenance comment is present.File(...) -> File("photo.jpg")).Here, DRE review prefers replacing
val myWorkRequest = ...directly in the visible snippet withval myWorkRequest = OneTimeWorkRequestBuilder<MyWork>().build()to reduce uncompiled code inside/* ... */comments and[START_EXCLUDE silent]blocks. D23 should be reviewed to clarify when concrete builder/initializer calls (likeOneTimeWorkRequestBuilder<MyWork>().build()) are preferred as visible replacements over the/* ... */D23 Twin pattern.