Migrate Persistent Background Work snippets - #1133
djubinville wants to merge 4 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
a9055f8 to
d53aa88
Compare
|
This commit is to address linter issue as a standalone. The following were resolved:
Two lint errors are left, and they still fail It appears that these errors may be due to the snippet extraction skill leaving the cc: @kkuan2011 @erikrodriguez-se Proposed Fix1.
|
- ObserveWork.kt: Remove duplicate [START android_background_observe_progress_worker]
region tag around imports, delete unused androidx.work.Data import, and sort imports.
- LongRunningWorker.kt: Remove stray [START_EXCLUDE]/[END_EXCLUDE] comments outside
the android_background_long_running_foreground_service_type region tag, and add
@SuppressLint("ObsoleteSdkInt") outside the DownloadWorker region tag.
- CustomConfiguration.kt: Change manualInitialization to a Context extension function
(private fun Context.manualInitialization()) so WorkManager.initialize(this, myConfig)
matches the DAC snippet verbatim.
- DefineWork.kt: Remove private fun createUploadWork() wrapper inside
android_background_assign_input_data and keep val myUploadWork at top-level scope
so both class UploadWork and val myUploadWork align at column 0 for DevSite rendering
while satisfying Spotless (ktlint).
- UpdateWork.kt: Restore suspend fun updatePhotoUploadWork() signature to match DAC
by moving context to a file-level property with @SuppressLint("StaticFieldLeak")
outside the region tag.
Code Review Resolution Summary (
|
- ObserveWork.kt: Preserve the 5 instructional import statements inside
android_background_observe_progress_worker as commented imports (// import ...) per D18.
- CoroutineWorkerThreading.kt, DefineWork.kt, ListenableWorkerThreading.kt,
LongRunningWorker.kt, ManageWork.kt, UpdateWork.kt: Mark top-level helper
classes outside region tags private with @SuppressLint("WorkerHasAPublicModifier") per D26a.
- DefineWork.kt: Use plain // [START_EXCLUDE] around uploadFile stub in UploadWork
instead of silent exclude + hand-written // ... per D22.
- CustomConfiguration.kt, DefineWork.kt, LongRunningWorker.kt, ManageWork.kt,
ObserveWork.kt: Ensure all natural-language comments end with a full stop (.) per D29.
Summary
Extracts Kotlin code snippets for 8 persistent background work (WorkManager) documentation guides into
:backgroundwork(backgroundwork/src/main/java/com/example/snippets/backgroundwork/) as region-tagged source code.Pages covered:
Files & Region Tags Added
CustomConfiguration.kt:android_background_custom_configuration_on_demand,android_background_custom_configuration_manual_initDefineWork.kt(12 snippets):android_background_enqueue_work_request,android_background_schedule_one_time_work_from,android_background_schedule_one_time_work_builder,android_background_expedited_work_request,android_background_expedited_coroutine_worker,android_background_periodic_work_request,android_background_periodic_work_request_flex,android_background_work_constraints,android_background_delayed_work,android_background_retry_backoff_policy,android_background_tag_work,android_background_assign_input_dataLongRunningWorker.kt:android_background_long_running_coroutine_worker,android_background_long_running_foreground_service_typeManageWork.kt(6 snippets):android_background_manage_work_enqueue,android_background_unique_periodic_work,android_background_observe_work_query,android_background_observe_work_flow,android_background_complex_work_queries,android_background_cancel_workObserveWork.kt(3 snippets):android_background_observe_progress_worker,android_background_observe_progress_flow,android_background_observe_stop_reasonUpdateWork.kt(2 snippets):android_background_update_photo_upload_work,android_background_track_work_generationCoroutineWorkerThreading.kt(3 snippets):android_background_coroutine_download_worker,android_background_coroutine_download_worker_with_context,android_background_remote_coroutine_worker_requestListenableWorkerThreading.kt(3 snippets):android_background_callback_worker,android_background_callback_worker_cancellation,android_background_remote_listenable_worker_requestbackgroundwork/src/main/res/drawable/ic_work_notification.xml(no region tag — Required for the Kotlin snippet to compile)backgroundwork/src/main/res/values/strings.xml(no region tag — Required for the Kotlin snippet to compile)Compile-Driven Supporting Resources (D11, D27)
Two untagged XML resource files (
backgroundwork/src/main/res/drawable/ic_work_notification.xmlandbackgroundwork/src/main/res/values/strings.xml) are included in this PR strictly because tagged Kotlin regions reference them and will not compile without them. Specifically,LongRunningWorker.kt(android_background_long_running_coroutine_worker) referencessetSmallIcon(R.drawable.ic_work_notification)(which forces the drawable filenameic_work_notification.xml, implemented as a Material work/notification vector icon) as well asR.string.notification_channel_id,R.string.notification_title, andR.string.cancel_download, whileManageWork.kt(android_background_observe_work_flow) referencesR.string.work_completed. Neither resource file is printed on the DAC pages, so neither carries a region tag.List of modifications
To compile cleanly against Kotlin and WorkManager 2.9+ while adhering to D1–D32 quality gates, the following minimal adjustments were made to the original DAC snippets:
CustomConfiguration.kt(android_background_custom_configuration_on_demand): Updatedoverride fun getWorkManagerConfiguration()tooverride val workManagerConfiguration: Configuration get() = ...becauseConfiguration.ProviderdefinesworkManagerConfigurationas a Kotlin property in WorkManager 2.9+ (D5/D14).UpdateWork.kt(android_background_track_work_generation): ChangedworkManager.getWorkInfoById(oldWorkRequestId)toworkManager.getWorkInfoById(oldWorkRequestId).get()andworkInfo.getGeneration()toworkInfo?.generation, becausegetWorkInfoById()returns aListenableFuture<WorkInfo?>(D5/D14).CoroutineWorkerThreading.kt(android_background_coroutine_download_worker_with_context): ChangedwithContext(Dispatchers.IO) { ... return Result.success() }toreturn withContext(Dispatchers.IO) { ... Result.success() }because non-localreturninsidewithContextis prohibited in Kotlin (D5/D14).DefineWork.kt(android_background_expedited_work_request,android_background_assign_input_data) &LongRunningWorker.kt(android_background_long_running_foreground_service_type): Stripped inline HTML formatting tags (<b>,</b>,<var>,</var>), wrapped the hiddenuploadFile(uri: String)stub inUploadWorkinside plain// [START_EXCLUDE]...// [END_EXCLUDE]so DevSite renders the ellipsis automatically (D22), and keptval myUploadWorkinandroid_background_assign_input_dataat top-level scope so bothclass UploadWorkandval myUploadWorkshare column-0 indentation for DevSite rendering while satisfying Spotless (ktlint).ObserveWork.kt(android_background_observe_progress_worker): Preserved the 5 page-visibleimportstatements (Context,CoroutineWorker,Data,WorkerParameters,delay) at their exact relative position inside the region tag as commented imports (// import ...), while placing the real compiling imports at the file header (D18).CustomConfiguration.kt,DefineWork.kt,LongRunningWorker.kt,ManageWork.kt,ObserveWork.kt,UpdateWork.kt,CoroutineWorkerThreading.kt,ListenableWorkerThreading.kt): Added trailing full stops (.) to natural-language inline comments carried over from DAC that omitted them (D29), and marked all top-level helper classes outside region tagsprivateacross sibling files incom.example.snippets.backgroundwork(D26a).backgroundwork/src/main/res/drawable/ic_work_notification.xml(no region tag): Added supporting vector drawable forced bysetSmallIcon(R.drawable.ic_work_notification)inLongRunningWorker.kt(android_background_long_running_coroutine_worker). Required for the Kotlin snippet to compile (D11, D27).backgroundwork/src/main/res/values/strings.xml(no region tag): Added supporting string definitions forced byR.string.notification_channel_id,R.string.notification_title, andR.string.cancel_downloadinLongRunningWorker.kt(android_background_long_running_coroutine_worker) andR.string.work_completedinManageWork.kt(android_background_observe_work_flow). Required for the Kotlin snippet to compile (D27).Snippets not migrated
AndroidManifest.xmlconfiguration snippets (custom-configuration.md,long-running.md,coroutineworker.md,listenableworker.md) remain inline on the DAC pages with{# disableFinding(SNIPPET_GITHUB) #}(D27).Verification
./gradlew :backgroundwork:compileDebugKotlin- passes (0 errors)./gradlew :backgroundwork:spotlessCheck- formatted cleanly