updateWakefulnessLocked
更新设备的唤醒状态,根据当前的wake locks和用户活动状态决定设备是否需要开始进入屏保状态。
如果wakefullness改变,可能会修改mDirty的值。
2112 /**
2113 * Updates the wakefulness of the device.
2114 *
2115 * This is the function that decides whether the device should start dreaming
2116 * based on the current wake locks and user activity state. It may modify mDirty
2117 * if the wakefulness changes.
2118 *
2119 * Returns true if the wakefulness changed and we need to restart power state calculation.
2120 */
2121 private boolean updateWakefulnessLocked(int dirty) {
2122 boolean changed = false;
2123 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_BOOT_COMPLETED
2124 | DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE
2125 | DIRTY_DOCK_STATE)) != 0) {
//当前屏幕保持唤醒&&设备将要进入睡眠or屏保状态
2126 if (mWakefulness == WAKEFULNESS_AWAKE && isItBedTimeYetLocked()) {
2127 if (DEBUG_SPEW) {
2128 Slog.d(TAG, "updateWakefulnessLocked: Bed time...");
2129 }
2130 final long time = SystemClock.uptimeMillis();
//用户活动超时了应该自动的进入屏保模式
2131 if (shouldNapAtBedTimeLocked()) {
//进入屏保模式
2132 changed = napNoUpdateLocked(time, Process.SYSTEM_UID);
2133 } else {
//进入休眠模式
2134 changed = goToSleepNoUpdateLocked(time,
2135 PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID);
2136 }
2137 }
2138 }
2139 return changed;
2140 }
isItBedTimeYetLocked:设备将进入休眠模式时,该函数返回true。或在屏保状态下进入休眠或者唤醒也会调用这个函数。
2152 /**
2153 * Returns true if the device should go to sleep now.
2154 * Also used when exiting a dream to determine whether we should go back
2155 * to being fully awake or else go to sleep for good.
2156 */
2157 private boolean isItBedTimeYetLocked() {
2158 return mBootCompleted && !isBeingKeptAwakeLocked();
2159 }
isBeingKeptAwakeLocked:
这个函数判断设备是否需要保持亮屏。
由mStayOn(是否屏幕常亮)、wakelockSummary、userActivitySummary、mProximityPositive等决定,只要满足其中之一为ture,则说明无法进入睡眠,也就说,要满足进入睡眠,相关属性值都为false。
2161 /**
2162 * Returns true if the device is being kept awake by a wake lock, user activity
2163 * or the stay on while powered setting. We also keep the phone awake when
2164 * the proximity sensor returns a positive result so that the device does not
2165 * lock while in a phone call. This function only controls whether the device
2166 * will go to sleep or dream which is independent of whether it will be allowed
2167 * to suspend.
2168 */
2169 private boolean isBeingKeptAwakeLocked() {
2170 return mStayOn //屏幕是否保持常亮
2171 || mProximityPositive //接近传感器接近屏幕时为true
2172 || (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0 //处于awake状态
//屏幕处于亮屏或dim状态
2173 || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT
2174 | USER_ACTIVITY_SCREEN_DIM)) != 0
2175 || mScreenBrightnessBoostInProgress;//处于亮度增强中
2176 }