@@ -107,6 +107,8 @@ public final class PowerManagerService extends IPowerManager.Stub
107107 private static final int DIRTY_PROXIMITY_POSITIVE = 1 << 9 ;
108108 // Dirty bit: screen on blocker state became held or unheld
109109 private static final int DIRTY_SCREEN_ON_BLOCKER_RELEASED = 1 << 10 ;
110+ // Dirty bit: dock state changed
111+ private static final int DIRTY_DOCK_STATE = 1 << 11 ;
110112
111113 // Wakefulness: The device is asleep and can only be awoken by a call to wakeUp().
112114 // The screen should be off or in the process of being turned off by the display controller.
@@ -269,6 +271,9 @@ public final class PowerManagerService extends IPowerManager.Stub
269271 // draining faster than it is charging and the user activity timeout has expired.
270272 private int mBatteryLevelWhenDreamStarted ;
271273
274+ // The current dock state.
275+ private int mDockState = Intent .EXTRA_DOCK_STATE_UNDOCKED ;
276+
272277 // True if the device should wake up when plugged or unplugged.
273278 private boolean mWakeUpWhenPluggedOrUnpluggedConfig ;
274279
@@ -281,6 +286,9 @@ public final class PowerManagerService extends IPowerManager.Stub
281286 // True if dreams should be activated on sleep.
282287 private boolean mDreamsActivateOnSleepSetting ;
283288
289+ // True if dreams should be activated on dock.
290+ private boolean mDreamsActivateOnDockSetting ;
291+
284292 // The screen off timeout setting value in milliseconds.
285293 private int mScreenOffTimeoutSetting ;
286294
@@ -440,6 +448,10 @@ public void systemReady(TwilightService twilight, DreamManagerService dreamManag
440448 filter .addAction (Intent .ACTION_USER_SWITCHED );
441449 mContext .registerReceiver (new UserSwitchedReceiver (), filter , null , mHandler );
442450
451+ filter = new IntentFilter ();
452+ filter .addAction (Intent .ACTION_DOCK_EVENT );
453+ mContext .registerReceiver (new DockReceiver (), filter , null , mHandler );
454+
443455 // Register for settings changes.
444456 final ContentResolver resolver = mContext .getContentResolver ();
445457 resolver .registerContentObserver (Settings .Secure .getUriFor (
@@ -448,6 +460,9 @@ public void systemReady(TwilightService twilight, DreamManagerService dreamManag
448460 resolver .registerContentObserver (Settings .Secure .getUriFor (
449461 Settings .Secure .SCREENSAVER_ACTIVATE_ON_SLEEP ),
450462 false , mSettingsObserver , UserHandle .USER_ALL );
463+ resolver .registerContentObserver (Settings .Secure .getUriFor (
464+ Settings .Secure .SCREENSAVER_ACTIVATE_ON_DOCK ),
465+ false , mSettingsObserver , UserHandle .USER_ALL );
451466 resolver .registerContentObserver (Settings .System .getUriFor (
452467 Settings .System .SCREEN_OFF_TIMEOUT ),
453468 false , mSettingsObserver , UserHandle .USER_ALL );
@@ -487,6 +502,9 @@ private void updateSettingsLocked() {
487502 mDreamsActivateOnSleepSetting = (Settings .Secure .getIntForUser (resolver ,
488503 Settings .Secure .SCREENSAVER_ACTIVATE_ON_SLEEP , 0 ,
489504 UserHandle .USER_CURRENT ) != 0 );
505+ mDreamsActivateOnDockSetting = (Settings .Secure .getIntForUser (resolver ,
506+ Settings .Secure .SCREENSAVER_ACTIVATE_ON_DOCK , 0 ,
507+ UserHandle .USER_CURRENT ) != 0 );
490508 mScreenOffTimeoutSetting = Settings .System .getIntForUser (resolver ,
491509 Settings .System .SCREEN_OFF_TIMEOUT , DEFAULT_SCREEN_OFF_TIMEOUT ,
492510 UserHandle .USER_CURRENT );
@@ -1339,13 +1357,14 @@ private int getScreenDimDurationLocked(int screenOffTimeout) {
13391357 private boolean updateWakefulnessLocked (int dirty ) {
13401358 boolean changed = false ;
13411359 if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_BOOT_COMPLETED
1342- | DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE )) != 0 ) {
1360+ | DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE
1361+ | DIRTY_DOCK_STATE )) != 0 ) {
13431362 if (mWakefulness == WAKEFULNESS_AWAKE && isItBedTimeYetLocked ()) {
13441363 if (DEBUG_SPEW ) {
13451364 Slog .d (TAG , "updateWakefulnessLocked: Bed time..." );
13461365 }
13471366 final long time = SystemClock .uptimeMillis ();
1348- if (mDreamsActivateOnSleepSetting ) {
1367+ if (shouldNapAtBedTimeLocked () ) {
13491368 changed = napNoUpdateLocked (time );
13501369 } else {
13511370 changed = goToSleepNoUpdateLocked (time ,
@@ -1356,6 +1375,16 @@ private boolean updateWakefulnessLocked(int dirty) {
13561375 return changed ;
13571376 }
13581377
1378+ /**
1379+ * Returns true if the device should automatically nap and start dreaming when the user
1380+ * activity timeout has expired and it's bedtime.
1381+ */
1382+ private boolean shouldNapAtBedTimeLocked () {
1383+ return mDreamsActivateOnSleepSetting
1384+ || (mDreamsActivateOnDockSetting
1385+ && mDockState != Intent .EXTRA_DOCK_STATE_UNDOCKED );
1386+ }
1387+
13591388 /**
13601389 * Returns true if the device should go to sleep now.
13611390 * Also used when exiting a dream to determine whether we should go back
@@ -2124,6 +2153,7 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
21242153 pw .println (" mPlugType=" + mPlugType );
21252154 pw .println (" mBatteryLevel=" + mBatteryLevel );
21262155 pw .println (" mBatteryLevelWhenDreamStarted=" + mBatteryLevelWhenDreamStarted );
2156+ pw .println (" mDockState=" + mDockState );
21272157 pw .println (" mStayOn=" + mStayOn );
21282158 pw .println (" mProximityPositive=" + mProximityPositive );
21292159 pw .println (" mBootCompleted=" + mBootCompleted );
@@ -2149,6 +2179,7 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
21492179 pw .println (" mDreamsSupportedConfig=" + mDreamsSupportedConfig );
21502180 pw .println (" mDreamsEnabledSetting=" + mDreamsEnabledSetting );
21512181 pw .println (" mDreamsActivateOnSleepSetting=" + mDreamsActivateOnSleepSetting );
2182+ pw .println (" mDreamsActivateOnDockSetting=" + mDreamsActivateOnDockSetting );
21522183 pw .println (" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting );
21532184 pw .println (" mMaximumScreenOffTimeoutFromDeviceAdmin="
21542185 + mMaximumScreenOffTimeoutFromDeviceAdmin + " (enforced="
@@ -2267,6 +2298,21 @@ public void onReceive(Context context, Intent intent) {
22672298 }
22682299 }
22692300
2301+ private final class DockReceiver extends BroadcastReceiver {
2302+ @ Override
2303+ public void onReceive (Context context , Intent intent ) {
2304+ synchronized (mLock ) {
2305+ int dockState = intent .getIntExtra (Intent .EXTRA_DOCK_STATE ,
2306+ Intent .EXTRA_DOCK_STATE_UNDOCKED );
2307+ if (mDockState != dockState ) {
2308+ mDockState = dockState ;
2309+ mDirty |= DIRTY_DOCK_STATE ;
2310+ updatePowerStateLocked ();
2311+ }
2312+ }
2313+ }
2314+ }
2315+
22702316 private final class SettingsObserver extends ContentObserver {
22712317 public SettingsObserver (Handler handler ) {
22722318 super (handler );
0 commit comments