PMS更新各个状态,最终都会调用updatePowerStateLocked函数,下面我们来分析下
private void updatePowerStateLocked() { if (!mSystemReady || mDirty == 0) {//mDirty=0代表没有变化,或者系统没有准备好,直接退出 return; } if (!Thread.holdsLock(mLock)) { Slog.wtf(TAG, "Power manager lock was not held when calling updatePowerStateLocked"); } Trace.traceBegin(Trace.TRACE_TAG_POWER, "updatePowerState"); try { // Phase 0: Basic state updates. updateIsPoweredLocked(mDirty);//更新电源状态 updateStayOnLocked(mDirty); updateScreenBrightnessBoostLocked(mDirty);
updateIsPoweredLocked函数,先是要dirty有DIRTY_BATTERY_STATE标志位。我们在下面分析下,什么时候会有这个标志位
private void updateIsPoweredLocked(int dirty) { if ((dirty & DIRTY_BATTERY_STATE) != 0) { final boolean wasPowered = mIsPowered; final int oldPlugType = mPlugType; final boolean oldLevelLow = mBatteryLevelLow; mIsPowered = mBatteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY);//这些都是从BatteryService获取 mPlugType = mBatteryManagerInternal.getPlugType(); mBatteryLevel = mBatteryManagerInternal.getBatteryLevel(); mBatteryLevelLow = mBatteryManagerInternal.getBatteryLevelLow(); if (DEBUG_SPEW) { Slog.d(TAG, "updateIsPoweredLocked: wasPowered=" + wasPowered + ", mIsPowered=" + mIsPowered + ", oldPlugType=" + oldPlugType + ", mPlugType=" + mPlugType + ", mBatteryLevel=" + mBatteryLevel); } if (wasPowered != mIsPowered || oldPlugType != mPlugType) {//是否充电或者充电类型改变了 mDirty |= DIRTY_IS_POWERED;//mDirty置位 // Update wireless dock detection state. final boolean dockedOnWirelessCharger = mWirelessChargerDetector.update(//无线充电相关 mIsPowered, mPlugType, mBatteryLevel); // Treat plugging and unplugging the devices as a user activity. // Users find it disconcerting when they plug or unplug the device // and it shuts off right away. // Some devices also wake the device when plugged or unplugged because // they don't have a charging LED. final long now = SystemClock.uptimeMillis(); if (shouldWakeUpWhenPluggedOrUnpluggedLocked(wasPowered, oldPlugType, dockedOnWirelessCharger)) {//是否需要唤醒设备 wakeUpNoUpdateLocked(now, Process.SYSTEM_UID); } userActivityNoUpdateLocked(//触发userActivity now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); // Tell the notifier whether wireless charging has started so that // it can provide feedback to the user. if (dockedOnWirelessCharger) {//无线充电相关 mNotifier.onWirelessChargingStarted(); } } if (wasPowered != mIsPowered || oldLevelLow != mBatteryLevelLow) { if (oldLevelLow != mBatteryLevelLow && !mBatteryLevelLow) { if (DEBUG_SPEW) { Slog.d(TAG, "updateIsPoweredLocked: resetting low power snooze"); } mAutoLowPowerModeSnoozing = false; } updateLowPowerModeLocked();//更新低功耗模式 } } }
首先systemReady函数最终会把mDirty置位为DIRTY_BATTERY_STATE,还有收BatterySevice发出来的广播,最总也会置这个标志位。
private final class BatteryReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { synchronized (mLock) { handleBatteryStateChangedLocked(); } } }
private void handleBatteryStateChangedLocked() { mDirty |= DIRTY_BATTERY_STATE; updatePowerStateLocked(); }
再来看看电池状态发生什么变化要唤醒设备:
private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked( boolean wasPowered, int oldPlugType, boolean dockedOnWirelessCharger) { // Don't wake when powered unless configured to do so. if (!mWakeUpWhenPluggedOrUnpluggedConfig) {//如果资源中没有配置这项,直接退出不唤醒设备 return false; } // Don't wake when undocked from wireless charger. // See WirelessChargerDetector for justification. if (wasPowered && !mIsPowered && oldPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {//这是一个 拔出的工作,然后之前的无线充电 return false; } // Don't wake when docked on wireless charger unless we are certain of it. // See WirelessChargerDetector for justification. if (!wasPowered && mIsPowered && mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS//插入动作,现在是无线充电 && !dockedOnWirelessCharger) { return false; } // If already dreaming and becoming powered, then don't wake. if (mIsPowered && mWakefulness == WAKEFULNESS_DREAMING) {//正在充电,但是mWakefulness 是做梦状态 return false; } // Don't wake while theater mode is enabled. if (mTheaterModeEnabled && !mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig) { return false; } // Otherwise wake up! return true;//其他就唤醒设备 }
继续分析updatePowerStateLocked函数,分析updateStayOnLocked函数。一般这个函数mStatyon为fasle,除非在资源中设置哪种充电状态下可以长亮。
private void updateStayOnLocked(int dirty) { if ((dirty & (DIRTY_BATTERY_STATE | DIRTY_SETTINGS)) != 0) {//当dirty是电池状态和设置的状态改变时 final boolean wasStayOn = mStayOn; if (mStayOnWhilePluggedInSetting != 0//这个值从资源中读取,一般设置的话代表哪种充电时可以常亮 && !isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {//看有没有设mMaximumScreenOffTimeoutFromDeviceAdmin屏幕最大亮屏时间,没设默认最大。那么这个函数返回是false mStayOn = mBatteryManagerInternal.isPowered(mStayOnWhilePluggedInSetting); } else { mStayOn = false; } if (mStayOn != wasStayOn) { mDirty |= DIRTY_STAY_ON; } } }
继续分析updatePowerStateLocked函数的updateScreenBrightnessBoostLocked函数,这个函数是更新屏幕是否保持最亮状态
private void updateScreenBrightnessBoostLocked(int dirty) { if ((dirty & DIRTY_SCREEN_BRIGHTNESS_BOOST) != 0) {//这个状态实在boostScreenBrightnessInternal函数中设置最亮时置位,当然下面发送消息MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT也会将这个标志位置位 if (mScreenBrightnessBoostInProgress) {//当前正在最亮屏幕这个状态 final long now = SystemClock.uptimeMillis(); mHandler.removeMessages(MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT); if (mLastScreenBrightnessBoostTime > mLastSleepTime) { final long boostTimeout = mLastScreenBrightnessBoostTime + SCREEN_BRIGHTNESS_BOOST_TIMEOUT; if (boostTimeout > now) {//看当前时间是否小于最亮屏幕结束的时间 Message msg = mHandler.obtainMessage(MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT); msg.setAsynchronous(true); mHandler.sendMessageAtTime(msg, boostTimeout);//发送一个延迟的消息,到最亮屏幕结束的时候接受到消息,将标志位置为DIRTY_SCREEN_BRIGHTNESS_BOOST后,重新再回到这个函数 return; } } mScreenBrightnessBoostInProgress = false;//回到这个函数时,直接将这个屏幕最亮状态的标志位改成false userActivityNoUpdateLocked(now,//触发一个userActivity PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); } } }
下面看看MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT消息的处理函数。
case MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT: handleScreenBrightnessBoostTimeout(); break;
private void handleScreenBrightnessBoostTimeout() { // runs on handler thread synchronized (mLock) { if (DEBUG_SPEW) { Slog.d(TAG, "handleScreenBrightnessBoostTimeout"); } mDirty |= DIRTY_SCREEN_BRIGHTNESS_BOOST; updatePowerStateLocked(); } }
// Phase 1: Update wakefulness. // Loop because the wake lock and user activity computations are influenced // by changes in wakefulness. final long now = SystemClock.uptimeMillis(); int dirtyPhase2 = 0; for (;;) { int dirtyPhase1 = mDirty;//这个循环中每次新的mDirty全部给dirtyPhase1,并且在这个循环中使用这个dirty dirtyPhase2 |= dirtyPhase1;//dirtyPhase2 会把之前所有的mDirty状态全部或上,然后用这个状态继续下面的函数 mDirty = 0;//mDirty会在这里清零 updateWakeLockSummaryLocked(dirtyPhase1); updateUserActivitySummaryLocked(now, dirtyPhase1); if (!updateWakefulnessLocked(dirtyPhase1)) { break; } }
先看下updateWakeLockSummaryLocked函数,更新wakelock的函数
private void updateWakeLockSummaryLocked(int dirty) { if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_WAKEFULNESS)) != 0) { mWakeLockSummary = 0; final int numWakeLocks = mWakeLocks.size(); for (int i = 0; i < numWakeLocks; i++) { final WakeLock wakeLock = mWakeLocks.get(i); switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {//先根据wakelock的flag,mWakeLockSummary或上各种状态 case PowerManager.PARTIAL_WAKE_LOCK: mWakeLockSummary |= WAKE_LOCK_CPU; break; case PowerManager.FULL_WAKE_LOCK: mWakeLockSummary |= WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_BUTTON_BRIGHT; break; case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: mWakeLockSummary |= WAKE_LOCK_SCREEN_BRIGHT; break; case PowerManager.SCREEN_DIM_WAKE_LOCK: mWakeLockSummary |= WAKE_LOCK_SCREEN_DIM; break; case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK://距离传感器相关 mWakeLockSummary |= WAKE_LOCK_PROXIMITY_SCREEN_OFF; break; case PowerManager.DOZE_WAKE_LOCK: mWakeLockSummary |= WAKE_LOCK_DOZE; break; } } // Cancel wake locks that make no sense based on the current state. if (mWakefulness != WAKEFULNESS_DOZING) {//下面根据各种状态,将上面或上的mWakeLockSummary,有的减去 mWakeLockSummary &= ~WAKE_LOCK_DOZE; } if (mWakefulness == WAKEFULNESS_ASLEEP || (mWakeLockSummary & WAKE_LOCK_DOZE) != 0) { mWakeLockSummary &= ~(WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM | WAKE_LOCK_BUTTON_BRIGHT); if (mWakefulness == WAKEFULNESS_ASLEEP) { mWakeLockSummary &= ~WAKE_LOCK_PROXIMITY_SCREEN_OFF; } } // Infer implied wake locks where necessary based on the current state. if ((mWakeLockSummary & (WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM)) != 0) {//只要有屏幕锁,cpu锁必须持有 if (mWakefulness == WAKEFULNESS_AWAKE) { mWakeLockSummary |= WAKE_LOCK_CPU | WAKE_LOCK_STAY_AWAKE; } else if (mWakefulness == WAKEFULNESS_DREAMING) { mWakeLockSummary |= WAKE_LOCK_CPU; } } if (DEBUG_SPEW) { Slog.d(TAG, "updateWakeLockSummaryLocked: mWakefulness=" + PowerManagerInternal.wakefulnessToString(mWakefulness) + ", mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary)); } } }
下面分析下updateUserActivitySummaryLocked函数
private void updateUserActivitySummaryLocked(long now, int dirty) { // Update the status of the user activity timeout timer. if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS | DIRTY_SETTINGS)) != 0) { mHandler.removeMessages(MSG_USER_ACTIVITY_TIMEOUT); long nextTimeout = 0; if (mWakefulness == WAKEFULNESS_AWAKE//状态为awake,dreaming,dozing,否则mUserActivitySummary为0 || mWakefulness == WAKEFULNESS_DREAMING || mWakefulness == WAKEFULNESS_DOZING) { final int sleepTimeout = getSleepTimeoutLocked();//睡眠timeout final int screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout);//灭屏的timeout final int screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);//屏幕由亮变暗的timeout mUserActivitySummary = 0; if (mLastUserActivityTime >= mLastWakeTime) {//userActivity的time大于上次唤醒时间 nextTimeout = mLastUserActivityTime//这个时间代表是比较亮的一个时间的结束点 + screenOffTimeout - screenDimDuration; if (now < nextTimeout) { mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT;//mUserActivitySummary赋值 } else { nextTimeout = mLastUserActivityTime + screenOffTimeout;//这个时间代表屏幕暗的一个时间结束点 if (now < nextTimeout) { mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM; } } } if (mUserActivitySummary == 0 && mLastUserActivityTimeNoChangeLights >= mLastWakeTime) {//这个time是userActivity一个特殊的flag才会设置 nextTimeout = mLastUserActivityTimeNoChangeLights + screenOffTimeout; if (now < nextTimeout) { if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_BRIGHT) { mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT; } else if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) { mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM; } } } if (mUserActivitySummary == 0) { if (sleepTimeout >= 0) { final long anyUserActivity = Math.max(mLastUserActivityTime, mLastUserActivityTimeNoChangeLights); if (anyUserActivity >= mLastWakeTime) { nextTimeout = anyUserActivity + sleepTimeout; if (now < nextTimeout) { mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM; } } } else { mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM; nextTimeout = -1;//该值为-1,下面就不会发送消息了 } } if (mUserActivitySummary != 0 && nextTimeout >= 0) { Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT); msg.setAsynchronous(true); mHandler.sendMessageAtTime(msg, nextTimeout);//到下个时间timeout结束的时候发送消息,重新进入这个函数 } } else { mUserActivitySummary = 0; }
MSG_USER_ACTIVITY_TIMEOUT的消息处理
@Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_USER_ACTIVITY_TIMEOUT: handleUserActivityTimeout(); break;
private void handleUserActivityTimeout() { // runs on handler thread synchronized (mLock) { if (DEBUG_SPEW) { Slog.d(TAG, "handleUserActivityTimeout"); } mDirty |= DIRTY_USER_ACTIVITY; updatePowerStateLocked(); } }
继续分析函数updateWakefulnessLocked
private boolean updateWakefulnessLocked(int dirty) { boolean changed = false; if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_BOOT_COMPLETED | DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE | DIRTY_DOCK_STATE)) != 0) { if (mWakefulness == WAKEFULNESS_AWAKE && isItBedTimeYetLocked()) {//当mWakefulness不是awake的时候,直接返回false,也就是上面的循环退出 if (DEBUG_SPEW) { Slog.d(TAG, "updateWakefulnessLocked: Bed time..."); } final long time = SystemClock.uptimeMillis(); if (shouldNapAtBedTimeLocked()) { changed = napNoUpdateLocked(time, Process.SYSTEM_UID); } else { changed = goToSleepNoUpdateLocked(time, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID); } } } return changed; }
再来看看isItBedTimeYetLocked函数
private boolean isItBedTimeYetLocked() { return mBootCompleted && !isBeingKeptAwakeLocked(); }
isBeingKeptAwakeLocked函数,如果isBeingKeptAwakeLocked返回true代表处于亮屏状态,isItBedTimeYetLocked返回false,上面的for死循环退出
private boolean isBeingKeptAwakeLocked() { return mStayOn || mProximityPositive//距离传感器有关 || (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0 || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT | USER_ACTIVITY_SCREEN_DIM)) != 0 || mScreenBrightnessBoostInProgress;//屏幕处于最亮状态 }
如果isBeingKeptAwakeLocked返回false,并且mBootCompleted 为true开机完成了。那么updateWakefulnessLocked函数进入判断条件
那再来看看shouldNapAtBedTimeLocked函数,就是看有没有做梦这个设置,如果没有直接到goToSleepNoUpdateLocked函数,到了goToSleepNoUpdateLocked函数后将mWakefulness 设为WAKEFULNESS_DOZING显示返回true还要继续循环,下次到updateWakefulnessLocked这个函数,不是awake类型的,也返回false,直接退出死循环了。到napNoUpdateLocked函数也是一样的先是将mWakefulness置为WAKEFULNESS_DREAMING,返回true,继续循环,下次不是awake类型就返回false,退出循环了。
private boolean shouldNapAtBedTimeLocked() { return mDreamsActivateOnSleepSetting || (mDreamsActivateOnDockSetting && mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED); }
下面分析就是updateDisplayPowerStateLocked函数要和Display交互。放在下一篇博客分析。