@@ -142,10 +142,12 @@ public final class PowerManagerService extends SystemService
142142 // Summarizes the user activity state.
143143 private static final int USER_ACTIVITY_SCREEN_BRIGHT = 1 << 0 ;
144144 private static final int USER_ACTIVITY_SCREEN_DIM = 1 << 1 ;
145+ private static final int USER_ACTIVITY_SCREEN_DREAM = 1 << 2 ;
145146
146147 // Default timeout in milliseconds. This is only used until the settings
147148 // provider populates the actual default value (R.integer.def_screen_off_timeout).
148149 private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000 ;
150+ private static final int DEFAULT_SLEEP_TIMEOUT = -1 ;
149151
150152 // Power hints defined in hardware/libhardware/include/hardware/power.h.
151153 private static final int POWER_HINT_INTERACTION = 2 ;
@@ -214,7 +216,6 @@ public final class PowerManagerService extends SystemService
214216 private long mLastInteractivePowerHintTime ;
215217
216218 // A bitfield that summarizes the effect of the user activity timer.
217- // A zero value indicates that the user activity timer has expired.
218219 private int mUserActivitySummary ;
219220
220221 // The desired display power state. The actual state may lag behind the
@@ -340,6 +341,9 @@ public final class PowerManagerService extends SystemService
340341 // The screen off timeout setting value in milliseconds.
341342 private int mScreenOffTimeoutSetting ;
342343
344+ // The sleep timeout setting value in milliseconds.
345+ private int mSleepTimeoutSetting ;
346+
343347 // The maximum allowable screen off timeout according to the device
344348 // administration policy. Overrides other settings.
345349 private int mMaximumScreenOffTimeoutFromDeviceAdmin = Integer .MAX_VALUE ;
@@ -543,6 +547,9 @@ mAppOps, createSuspendBlockerLocked("PowerManagerService.Broadcasts"),
543547 resolver .registerContentObserver (Settings .System .getUriFor (
544548 Settings .System .SCREEN_OFF_TIMEOUT ),
545549 false , mSettingsObserver , UserHandle .USER_ALL );
550+ resolver .registerContentObserver (Settings .Secure .getUriFor (
551+ Settings .Secure .SLEEP_TIMEOUT ),
552+ false , mSettingsObserver , UserHandle .USER_ALL );
546553 resolver .registerContentObserver (Settings .Global .getUriFor (
547554 Settings .Global .STAY_ON_WHILE_PLUGGED_IN ),
548555 false , mSettingsObserver , UserHandle .USER_ALL );
@@ -624,6 +631,9 @@ private void updateSettingsLocked() {
624631 mScreenOffTimeoutSetting = Settings .System .getIntForUser (resolver ,
625632 Settings .System .SCREEN_OFF_TIMEOUT , DEFAULT_SCREEN_OFF_TIMEOUT ,
626633 UserHandle .USER_CURRENT );
634+ mSleepTimeoutSetting = Settings .Secure .getIntForUser (resolver ,
635+ Settings .Secure .SLEEP_TIMEOUT , DEFAULT_SLEEP_TIMEOUT ,
636+ UserHandle .USER_CURRENT );
627637 mStayOnWhilePluggedInSetting = Settings .Global .getInt (resolver ,
628638 Settings .Global .STAY_ON_WHILE_PLUGGED_IN , BatteryManager .BATTERY_PLUGGED_AC );
629639
@@ -1431,19 +1441,20 @@ private void updateUserActivitySummaryLocked(long now, int dirty) {
14311441 if (mWakefulness == WAKEFULNESS_AWAKE
14321442 || mWakefulness == WAKEFULNESS_DREAMING
14331443 || mWakefulness == WAKEFULNESS_DOZING ) {
1434- final int screenOffTimeout = getScreenOffTimeoutLocked ();
1444+ final int sleepTimeout = getSleepTimeoutLocked ();
1445+ final int screenOffTimeout = getScreenOffTimeoutLocked (sleepTimeout );
14351446 final int screenDimDuration = getScreenDimDurationLocked (screenOffTimeout );
14361447
14371448 mUserActivitySummary = 0 ;
14381449 if (mLastUserActivityTime >= mLastWakeTime ) {
14391450 nextTimeout = mLastUserActivityTime
14401451 + screenOffTimeout - screenDimDuration ;
14411452 if (now < nextTimeout ) {
1442- mUserActivitySummary | = USER_ACTIVITY_SCREEN_BRIGHT ;
1453+ mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT ;
14431454 } else {
14441455 nextTimeout = mLastUserActivityTime + screenOffTimeout ;
14451456 if (now < nextTimeout ) {
1446- mUserActivitySummary | = USER_ACTIVITY_SCREEN_DIM ;
1457+ mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM ;
14471458 }
14481459 }
14491460 }
@@ -1458,7 +1469,22 @@ private void updateUserActivitySummaryLocked(long now, int dirty) {
14581469 }
14591470 }
14601471 }
1461- if (mUserActivitySummary != 0 ) {
1472+ if (mUserActivitySummary == 0 ) {
1473+ if (sleepTimeout >= 0 ) {
1474+ final long anyUserActivity = Math .max (mLastUserActivityTime ,
1475+ mLastUserActivityTimeNoChangeLights );
1476+ if (anyUserActivity >= mLastWakeTime ) {
1477+ nextTimeout = anyUserActivity + sleepTimeout ;
1478+ if (now < nextTimeout ) {
1479+ mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM ;
1480+ }
1481+ }
1482+ } else {
1483+ mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM ;
1484+ nextTimeout = -1 ;
1485+ }
1486+ }
1487+ if (mUserActivitySummary != 0 && nextTimeout >= 0 ) {
14621488 Message msg = mHandler .obtainMessage (MSG_USER_ACTIVITY_TIMEOUT );
14631489 msg .setAsynchronous (true );
14641490 mHandler .sendMessageAtTime (msg , nextTimeout );
@@ -1495,14 +1521,25 @@ private void handleUserActivityTimeout() { // runs on handler thread
14951521 }
14961522 }
14971523
1498- private int getScreenOffTimeoutLocked () {
1524+ private int getSleepTimeoutLocked () {
1525+ int timeout = mSleepTimeoutSetting ;
1526+ if (timeout <= 0 ) {
1527+ return -1 ;
1528+ }
1529+ return Math .max (timeout , mMinimumScreenOffTimeoutConfig );
1530+ }
1531+
1532+ private int getScreenOffTimeoutLocked (int sleepTimeout ) {
14991533 int timeout = mScreenOffTimeoutSetting ;
15001534 if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked ()) {
15011535 timeout = Math .min (timeout , mMaximumScreenOffTimeoutFromDeviceAdmin );
15021536 }
15031537 if (mUserActivityTimeoutOverrideFromWindowManager >= 0 ) {
15041538 timeout = (int )Math .min (timeout , mUserActivityTimeoutOverrideFromWindowManager );
15051539 }
1540+ if (sleepTimeout >= 0 ) {
1541+ timeout = Math .min (timeout , sleepTimeout );
1542+ }
15061543 return Math .max (timeout , mMinimumScreenOffTimeoutConfig );
15071544 }
15081545
@@ -1619,8 +1656,7 @@ private void handleSandman() { // runs on handler thread
16191656 mSandmanScheduled = false ;
16201657 wakefulness = mWakefulness ;
16211658 if (mSandmanSummoned && mDisplayReady ) {
1622- startDreaming = ((wakefulness == WAKEFULNESS_DREAMING && canDreamLocked ())
1623- || wakefulness == WAKEFULNESS_DOZING );
1659+ startDreaming = canDreamLocked () || canDozeLocked ();
16241660 mSandmanSummoned = false ;
16251661 } else {
16261662 startDreaming = false ;
@@ -1708,13 +1744,14 @@ private void handleSandman() { // runs on handler thread
17081744
17091745 /**
17101746 * Returns true if the device is allowed to dream in its current state.
1711- * This function is not called when dozing.
17121747 */
17131748 private boolean canDreamLocked () {
17141749 if (mWakefulness != WAKEFULNESS_DREAMING
17151750 || !mDreamsSupportedConfig
17161751 || !mDreamsEnabledSetting
17171752 || !mDisplayPowerRequest .isBrightOrDim ()
1753+ || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT
1754+ | USER_ACTIVITY_SCREEN_DIM | USER_ACTIVITY_SCREEN_DREAM )) == 0
17181755 || !mBootCompleted ) {
17191756 return false ;
17201757 }
@@ -1736,6 +1773,13 @@ private boolean canDreamLocked() {
17361773 return true ;
17371774 }
17381775
1776+ /**
1777+ * Returns true if the device is allowed to doze in its current state.
1778+ */
1779+ private boolean canDozeLocked () {
1780+ return mWakefulness == WAKEFULNESS_DOZING ;
1781+ }
1782+
17391783 /**
17401784 * Updates the display power state asynchronously.
17411785 * When the update is finished, mDisplayReady will be set to true. The display
@@ -2343,6 +2387,7 @@ private void dumpInternal(PrintWriter pw) {
23432387 pw .println (" mMaximumScreenDimDurationConfig=" + mMaximumScreenDimDurationConfig );
23442388 pw .println (" mMaximumScreenDimRatioConfig=" + mMaximumScreenDimRatioConfig );
23452389 pw .println (" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting );
2390+ pw .println (" mSleepTimeoutSetting=" + mSleepTimeoutSetting );
23462391 pw .println (" mMaximumScreenOffTimeoutFromDeviceAdmin="
23472392 + mMaximumScreenOffTimeoutFromDeviceAdmin + " (enforced="
23482393 + isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked () + ")" );
@@ -2367,9 +2412,11 @@ private void dumpInternal(PrintWriter pw) {
23672412 pw .println (" mScreenBrightnessSettingMaximum=" + mScreenBrightnessSettingMaximum );
23682413 pw .println (" mScreenBrightnessSettingDefault=" + mScreenBrightnessSettingDefault );
23692414
2370- final int screenOffTimeout = getScreenOffTimeoutLocked ();
2415+ final int sleepTimeout = getSleepTimeoutLocked ();
2416+ final int screenOffTimeout = getScreenOffTimeoutLocked (sleepTimeout );
23712417 final int screenDimDuration = getScreenDimDurationLocked (screenOffTimeout );
23722418 pw .println ();
2419+ pw .println ("Sleep timeout: " + sleepTimeout + " ms" );
23732420 pw .println ("Screen off timeout: " + screenOffTimeout + " ms" );
23742421 pw .println ("Screen dim duration: " + screenDimDuration + " ms" );
23752422
0 commit comments