Skip to content

Instantly share code, notes, and snippets.

@hkurokawa
Last active August 29, 2015 14:23
Show Gist options
  • Save hkurokawa/8e2a1e90e2fb23711d7b to your computer and use it in GitHub Desktop.
Save hkurokawa/8e2a1e90e2fb23711d7b to your computer and use it in GitHub Desktop.
DeviceIdleController を読んでみた

DeviceIdleController とは

SystemService で常に実行されている。M Preview の Doze モードを実装するためのもの。

挙動

以下のイベントを監視する。

  1. Screen on/off
  2. Charging status
  3. Significant motion detect

上記の状態に応じて、以下のステートを遷移する。

ACTIVE
Device is currently active.
INACTIVE
Device is inactve (screen off, no motion) and we are waiting to for idle.
IDLE_PENDING
Device is past the initial inactive period, and waiting for the next idle period.
IDLE
Device is in the idle state, trying to stay asleep as much as possible.
IDLE_MAINTENANCE
Device is in the idle state, but temporarily out of idle to do regular maintenance.

流れ

  1. onBootPhase(int)

    • 初期化
    • ディスプレイの状態をチェック
  2. ディスプレイの状態をチェック

    ディスプレイが OFF → becomeInactiveIfAppropriateLocked

    ディスプレイが ON → becomeActiveLocked("screen")

  3. アラームを AlarmManager#set() もしくは AlarmManager#setIdleUntil() メソッドでセットする

  4. アラームで起きるたびに以下の遷移を行う

    STATE_INACTIVE → STATE_IDLE_PENDING → STATE_IDLE → STATE_IDLE_MAINTAINANCE → STATE_IDLE → STATE_IDLE_MAINTAINANCE

ホワイトリスト

システムアプリとユーザーアプリをホワイトリストに登録している。

システムアプリ

System#getAllowInPowerSave() で取れるっぽい。

ユーザーアプリ

system/deviceidle.xml にまとめられているっぽい。

メモ書き

  • becomeInactiveIfAppropriateLocked スクリーンが OFF で充電中でなければ INACTIVE に移行して、アラームをセット
  • becomeActiveLocked アラームをキャンセルして、motion detection のリスナーを外す
  • stepIdleStateLocked
  • AlarmManager#setIdleUntil 次のアラームの時間まで AlarmManager を idle にする(?)

参考

Cross Reference: /com/android/server/DeviceIdleController.java Diving Into Android 'M' Doze - NewCircle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment