Repository navigation
Migrate snippets Background work - #1119
barbaralaw wants to merge 11 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. |
603bead to
4dbfefe
Compare
kkuan2011
left a comment
There was a problem hiding this comment.
Could you run spotless to confirm it passes successfully?
Spotless / ktlint indentation & straddled region tags (android_background_alarms_elapsed_realtime_one_time & android_background_alarms_rtc_repeating_precise):
In both ElapsedRealtimeOneTimeSnippet (lines 48–50) and RtcRepeatingPreciseSnippet (lines 70–72), the class-level properties private var alarmMgr: AlarmManager? = null and private lateinit var alarmIntent: PendingIntent are indented by 8 spaces (instead of 4 spaces) so they visually align with statements inside fun schedule(), while [START ...] opens at class scope and [END ...] closes inside fun schedule().
Running ./gradlew :backgroundwork:spotlessCheck (ktlint) flags 8-space indentation on class-level properties (or spotlessApply de-indents them to 4 spaces, misaligning the rendered snippet).
To keep both spotlessApply and the rendered DAC indentation clean, please move [START ...] inside fun schedule() (or declare var alarmMgr: AlarmManager? = null and lateinit var alarmIntent: PendingIntent as local variables/comments inside fun schedule()) and verify ./gradlew :backgroundwork:spotlessApply passes cleanly.
Summary
Extracts the Kotlin samples from four background-work guides into
:backgroundwork(com.example.snippets.backgroundwork) as region-tagged source, so the guides can import them instead of hardcoding them. Wake-lock samples for the same DAC area already live in that module.20 snippets across 4 pages. 16 publish exactly what the page shows. 4 differ, every one listed below with a reason. Line wrapping is not counted as a difference:
spotlessApplyruns before submission and rewraps long calls without changing what a reader reads.Region tags all begin
android_background_; the tables drop that prefix.How to read the "why" column
// ......, which Kotlin will not compile. The region publishes// ...as a line comment.Per page
1. Keep the screen on
https://developer.android.com/develop/background-work/background-tasks/awake/screen-on
1 snippet, all 1 match as published:
awake_screen_on.MainActivityis registered in this module’s manifest (MAIN/LAUNCHER).setContentView(R.layout.activity_main)inflates a compile-onlyFrameLayoutstub in:backgroundwork; that layout is not a published region.2. System restrictions on background tasks
https://developer.android.com/develop/background-work/background-tasks/bg-work-restrictions
2 snippets, 1 match, 1 differ.
restrictions_worker_triggered_content{;private val paramssoparams.triggeredContentAuthorities/params.triggeredContentUriscompileMatching as published:
restrictions_schedule_work.3. Testing Worker implementation
https://developer.android.com/develop/background-work/background-tasks/testing/persistent/worker-impl
8 snippets, all 8 match as published:
testing_sleep_worker_basic,testing_sleep_worker_test_basic,testing_sleep_worker_input_data,testing_sleep_worker_test_input_data,testing_coroutine_worker,testing_coroutine_worker_test,testing_rx_worker,testing_rx_worker_test.These snippets live in
:backgroundworkandroidTest. Catalog aliasesandroidx-work-testingandandroidx-work-rxjava2shareversion.ref = "androidx-work-runtime"and areandroidTestImplementation, withandroidx.test.ext:junit/core/runneron the same configuration. Comments outside the regions note that eachSleepWorkerwould normally live insrc/main.4. Schedule alarms
https://developer.android.com/develop/background-work/services/alarms
9 snippets, 6 match, 3 differ.
alarms_cancel_servicePendingIntent.FLAG_NO_CREATE or FLAG_IMMUTABLEinstead ofFLAG_NO_CREATEalonealarms_elapsed_realtime_one_time// ...for the page’s...;getBroadcast(..., FLAG_IMMUTABLE)instead of flag0;var/lateinit varinstead ofprivate var/private lateinit// ..., hardcoded snippet defect fixedalarms_rtc_repeating_precise// ..., hardcoded snippet defect fixedMatching as published:
alarms_boot_receiver,alarms_elapsed_realtime_repeating,alarms_rtc_repeating,alarms_cancel_direct,alarms_enable_receiver,alarms_disable_receiver.The one-time elapsed and precise RTC samples live in a compile-only
fun schedule()so every published line is at function-body indent (ktlint and the rendered block stay aligned). Locals cannot beprivate, so those two modifiers are dropped; alarm behavior is unchanged.From API 31 a
PendingIntentmust setFLAG_IMMUTABLEorFLAG_MUTABLE; these samples do not need a mutable intent.Not extracted
Kotlin only. Blocks that are not Kotlin samples stay on the page; each kind has a verdict below.
Keep the screen on
awake_screen_on— remove with the page edit that retires Java.RelativeLayoutwithandroid:keepScreenOn="true") — keep hardcoded. It was not given a region tag.System restrictions on background tasks
No unclaimed blocks. Both Kotlin samples are extracted.
Testing Worker implementation
Schedule alarms
SCHEDULE_EXACT_ALARM/USE_EXACT_ALARM,RECEIVE_BOOT_COMPLETED, bootreceiver) — keep hardcoded. Manifest samples sit outside/build/.Handler.postAtTime,WorkManager,setAndAllowWhileIdle,set,setWindow,setExact,setExactAndAllowWhileIdle,setAlarmClock) — out of scope. They are prose API lists, not copy-paste Kotlin samples.Live snippet fixes this surfaced
MyWorkeris missing{after the constructor. Constructorparamsis not a property, soparams.triggeredContentAuthorities/params.triggeredContentUrisindoWorkwould not compile. The region addsprivate val paramsand keeps thoseWorkerParameterscalls.PendingIntent.getService/getBroadcastwithout a mutability flag (FLAG_NO_CREATEalone, or0). Invalid from API 31.Verification
./gradlew :backgroundwork:compileDebugKotlin— passes./gradlew :backgroundwork:compileDebugAndroidTestKotlin— passes./gradlew :backgroundwork:lintDebug— passes./gradlew spotlessApply— run before submitting; the branch is clean afterwards