Upgrade to Pro — share decks privately, control downloads, hide ads and more …

App hibernation🥶

E77b6a5f919f7366d94f21eee9a014f5?s=47 operandoOS
February 24, 2022

App hibernation🥶

E77b6a5f919f7366d94f21eee9a014f5?s=128

operandoOS

February 24, 2022
Tweet

More Decks by operandoOS

Other Decks in Technology

Transcript

  1. App hibernation🥶 DroidKaigi: ϥϯνλΠϜLTձ Vol.1

  2. ࣗݾ঺հ • Shinobu Okano • @operandoOS / shinobu.dart • 10X,

    Inc. / Software Engineer • Flutter / Dart / Android / Automation
  3. App hibernationͱ͸ʁ • Android 12͔Βೖͬͨ࢓૊Έ • ਺ϲ݄ར༻͞Ε͍ͯͳ͍ΞϓϦΛγεςϜ͕hibernation stateʹ 
 ͠·͢Αʔͬͯ΍ͭ

    • Android 11Ͱಋೖ͞ΕͨAuto-reset permissionsΛਐԽͤͨ͞΍ͭ
  4. App hibernationͱ͸ʁ https://developer.android.com/topic/ performance/app-hibernation

  5. Android11͔Βಋೖ͞ΕͨAuto-reset permissionsΛௐ΂ͯΈͯΘ͔ͬͨ͜ͱ https://hack-it-iron.hatenablog.com/entry/ 2020/12/14/081217

  6. hibernation͕ΞϓϦʹ༩͑ΔӨڹ

  7. Making permissions auto-reset available to billions more devices https://android- developers.googleblog.com/2021/09/

    making-permissions-auto-reset- available.html
  8. γεςϜ͕ΞϓϦΛ࢖༻͍ͯ͠ΔͱΈͳ͢Ξ Ϋγϣϯ͸ͳʹ͔ʁ

  9. γεςϜ͕ΞϓϦΛ࢖༻͍ͯ͠ΔͱΈͳ͢Ξ Ϋγϣϯ͸ͳʹ͔ʁ

  10. υΩϡϝϯτΛಡΜͰٙ໰ʹࢥͬͨ͜ͱ • ਺ϲ݄ར༻͞Ε͍ͯͳ͍ͬͯ۩ମతͳ਺ࣈ͸ʁ • Ͳͷ͘Β͍ͷස౓ͰhibernationͷνΣοΫ͕ߦΘΕΔͷ͔ʁ • App usageͬͯͲ͏΍ͬͯΧ΢ϯτͯ͠Δͷ͔ʁ • hibernation

    statusͳΞϓϦͷ࣮ߦ੍ޚΛͲ͏΍ͬͯ΍ͬͯΔͷ ͔ʁ
  11. ਺ϲ݄ར༻͞Ε͍ͯͳ͍ͬͯ۩ମతͳ਺ࣈ ͸ʁ • σϑΥϧτͰ͸90೔ؒ = ໿ࡾϲ݄ • ݁ߏ௕͍Ͷʂ private val

    DEFAULT_UNUSED_THRESHOLD_MS = TimeUnit.DAYS.toMillis(90)
  12. ਺ϲ݄ར༻͞Ε͍ͯͳ͍ͬͯ۩ମతͳ਺ࣈ ͸ʁ • ظؒͷ͖͍͠஋Λมߋ͢Δ͜ͱ͸Ͱ͖Δ // ະ࢖༻ظؒͷ͖͍͠஋Λ1෼ʹมߋ adb shell device_config put

    permissions auto_revoke_unused_threshold_millis2 60000 // ઃఆͰ͖͍ͯΔ͔Λ֬ೝ adb shell device_config get permissions auto_revoke_unused_threshold_millis2
  13. Ͳͷ͘Β͍ͷස౓ͰhibernationͷνΣοΫ ͕ߦΘΕΔͷ͔ʁ • σϑΥϧτͰ͸15೔ִؒ • ʢ͋Ε...ʁͦΜͳߴස౓͡Όͳ͍ͷͶ...😂ʣ private val DEFAULT_CHECK_FREQUENCY_MS =

    TimeUnit.DAYS.toMillis(15)
  14. Ͳͷ͘Β͍ͷස౓ͰhibernationͷνΣοΫ ͕ߦΘΕΔͷ͔ʁ • ͪ͜Β΋ස౓Λมߋ͢Δ͜ͱ͕Ͱ͖Δ // ࣮ߦपظΛ15෼ʹมߋ adb shell device_config put

    permissions auto_revoke_check_frequency_millis 900000 // ઃఆͰ͖͍ͯΔ͔Λ֬ೝ adb shell device_config get permissions auto_revoke_check_frequency_millis
  15. App usageͬͯͲ͏΍ͬͯΧ΢ϯτͯ͠Δͷ ͔ʁ • UsageStatsManager͔ΒͷϦεφʔΛड͚औΕΔ࢓૊Έ͕͋ ΓɺͦΕͰΧ΢ϯτ͍ͯ͠Δ • ͪͳΈʹΞϓϦ͕ར༻͞Ε͍ͯΔΠϕϯτ͕ඈΜͰ͖ͨΒɺ͙͢ ʹhibernation͔Βͷ෮ؼ͕࣮ߦ͞ΕΔ

  16. App usageͬͯͲ͏΍ͬͯΧ΢ϯτͯ͠Δͷ ͔ʁ private final UsageEventListener mUsageEventListener = (userId, event)

    -> { if (!isAppHibernationEnabled()) { return; } final int eventType = event.mEventType; if (eventType == UsageEvents.Event.USER_INTERACTION || eventType == UsageEvents.Event.ACTIVITY_RESUMED || eventType == UsageEvents.Event.APP_COMPONENT_USED) { final String pkgName = event.mPackage; setHibernatingForUser(pkgName, userId, false); setHibernatingGlobally(pkgName, false); } };
  17. hibernation statusͳΞϓϦͷ࣮ߦ੍ޚΛͲ ͏΍ͬͯ΍ͬͯΔͷ͔ʁ • Θ͔Βͳ͔ͬͨ...🙏

  18. ͦͷଞ ಺෦ߏ଄Λௐ΂ͯΘ͔ͬͨ͜ͱ • Android 12Ͱ͸Ancroid 11ͰͷAuto-reset permissionsͷ࣮ ૷͕HibernationPolicyͱ͔ʹมΘͬͯͦ͏ • AppHibernationService͸؅ཧ͢Δ໾ׂΛ΍͍ͬͯΔɻ

    HibernationController͸PermissionͳͲͷແޮԽͳͲͷॲཧ Λ͍ͯ͠Δɻ؅ཧͱ࣮ߦͰ໾ׂ͕෼͔Ε͍ͯΔͬΆ͍ɻ
  19. ͦͷଞ ಺෦ߏ଄Λௐ΂ͯΘ͔ͬͨ͜ͱ • adb shell dumpsys app_hibernation͢Ε͹ AppHibernationServiceΛdumpͰ͖Δ • hibernation͔Β෮ؼ͢ΔͱɺACTION_BOOT_COMPLETED

    ͱACTION_LOCKED_BOOT_COMPLETED͕broadcastͰൃՐ ͢Δ
  20. ͦͷଞ ಺෦ߏ଄Λௐ΂ͯΘ͔ͬͨ͜ͱ • hibernationͷ৘ใ͸ҎԼΈ͍ͨͳ৔ॴʹอଘ͍ͯ͠Δ • /data/system/hibernation/states • /data/system_ce/0/hibernation/states

  21. ࢀߟίʔυʢ͋Γ͗͢…😇ʣ • https://cs.android.com/android/platform/superproject/+/master:packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/hibernation/ • https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/apphibernation/AppHibernationManager.java • https://cs.android.com/android/platform/superproject/+/master:packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/hibernation/HibernationController.kt • https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/apphibernation/ •

    https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/apphibernation/AppHibernationService.java • https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/apphibernation/AppHibernationShellCommand.java • https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/apphibernation/GlobalLevelState.java • https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/apphibernation/UserLevelState.java • https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/apphibernation/HibernationStateDiskStore.java • https://cs.android.com/android/platform/superproject/+/master:packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/hibernation/HibernationPolicy.kt • https://cs.android.com/android/platform/superproject/+/master:packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/permission/service/ AutoRevokePermissions.kt • https://cs.android.com/android/platform/superproject/+/master:packages/apps/Settings/src/com/android/settings/applications/HibernatedAppsPreferenceController.java • https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/apphibernation/AppHibernationManager.java
  22. App hibernationͷ಺෦ߏ଄Ͳ͏ௐ΂͔ͨ • DroidKaigi 2021 ʮͰ͖ΔʂAndroid Framework Code Readingʯ •

    https://speakerdeck.com/operando/dekiru-android-framework- code-reading • https://www.youtube.com/watch? v=44ChiZXry1E&list=PLaOdaBFokChxuuWf0eXWaf5H35R1Aroaz • ͪ͜ΒΛͥͻࢀߟʹ🙏
  23. Thanks🙏