Ensure that dreams show while docked.

Fixed a race between the UiModeManagerService and PowerManagerService
both of which are trying to wake the device when docked / powered.

Bug: 7281240
Change-Id: Ia41fef48f17f2a2eb56549437d295f9a86c95af2
diff --git a/services/java/com/android/server/UiModeManagerService.java b/services/java/com/android/server/UiModeManagerService.java
index d1af2b0..e9e3163 100644
--- a/services/java/com/android/server/UiModeManagerService.java
+++ b/services/java/com/android/server/UiModeManagerService.java
@@ -37,6 +37,7 @@
 import android.os.PowerManager;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.SystemClock;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.service.dreams.DreamService;
@@ -90,6 +91,8 @@
     private NotificationManager mNotificationManager;
 
     private StatusBarManager mStatusBarManager;
+
+    private final PowerManager mPowerManager;
     private final PowerManager.WakeLock mWakeLock;
 
     static Intent buildHomeIntent(String category) {
@@ -163,8 +166,8 @@
         mContext.registerReceiver(mBatteryReceiver,
                 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
 
-        PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
-        mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
+        mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
+        mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
 
         mConfiguration.setToDefaults();
 
@@ -502,7 +505,17 @@
             try {
                 IDreamManager dreamManagerService = IDreamManager.Stub.asInterface(
                         ServiceManager.getService(DreamService.DREAM_SERVICE));
-                dreamManagerService.dream();
+                if (dreamManagerService != null && !dreamManagerService.isDreaming()) {
+                    // Wake up.
+                    // The power manager will wake up the system when it starts receiving power
+                    // but there is a race between that happening and the UI mode manager
+                    // starting a dream.  We want the system to already be awake
+                    // by the time this happens.  Otherwise the dream may not start.
+                    mPowerManager.wakeUp(SystemClock.uptimeMillis());
+
+                    // Dream.
+                    dreamManagerService.dream();
+                }
             } catch (RemoteException ex) {
                 Slog.e(TAG, "Could not start dream when docked.", ex);
             }