Doze: Add a user opt-out setting.

When devices are configured with a doze component, the user
now has the option of disabling doze mode with a secure setting.

Bug:16703536
Change-Id: Ieab6ee8d2acf54580b86fb16e9d77b52080fb3b1
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 5d5be4a..4b7e615 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -4607,6 +4607,12 @@
         public static final String WAKE_GESTURE_ENABLED = "wake_gesture_enabled";
 
         /**
+         * Whether the device should doze if configured.
+         * @hide
+         */
+        public static final String DOZE_ENABLED = "doze_enabled";
+
+        /**
          * The current night mode that has been selected by the user.  Owned
          * and controlled by UiModeManagerService.  Constants are as per
          * UiModeManager.
diff --git a/services/core/java/com/android/server/dreams/DreamManagerService.java b/services/core/java/com/android/server/dreams/DreamManagerService.java
index b894304..985f77a7 100644
--- a/services/core/java/com/android/server/dreams/DreamManagerService.java
+++ b/services/core/java/com/android/server/dreams/DreamManagerService.java
@@ -249,7 +249,7 @@
 
     private ComponentName chooseDreamForUser(boolean doze, int userId) {
         if (doze) {
-            ComponentName dozeComponent = getDozeComponent();
+            ComponentName dozeComponent = getDozeComponent(userId);
             return validateDream(dozeComponent) ? dozeComponent : null;
         }
         ComponentName[] dreams = getDreamComponentsForUser(userId);
@@ -314,6 +314,10 @@
     }
 
     private ComponentName getDozeComponent() {
+        return getDozeComponent(ActivityManager.getCurrentUser());
+    }
+
+    private ComponentName getDozeComponent(int userId) {
         // Read the component from a system property to facilitate debugging.
         // Note that for production devices, the dream should actually be declared in
         // a config.xml resource.
@@ -324,7 +328,9 @@
             name = mContext.getResources().getString(
                     com.android.internal.R.string.config_dozeComponent);
         }
-        return TextUtils.isEmpty(name) ? null : ComponentName.unflattenFromString(name);
+        boolean enabled = Settings.Secure.getIntForUser(mContext.getContentResolver(),
+                Settings.Secure.DOZE_ENABLED, 1, userId) != 0;
+        return TextUtils.isEmpty(name) || !enabled ? null : ComponentName.unflattenFromString(name);
     }
 
     private ServiceInfo getServiceInfo(ComponentName name) {