Fix issue #21930140: Add config to turn off auto power features

Doze and app standby are now off in the default platform config.
The Google overlay turns them on.  Other people can do that as
well, if they are feeling like it.

Change-Id: Ic8a87f696df94f2d8354fe0772d03b672f464e32
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 424a0b7..edbe130 100755
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -157,6 +157,15 @@
          ActivityManager based on screen size. -->
     <integer name="config_extraFreeKbytesAdjust">0</integer>
 
+    <!-- Set this to true to enable the platform's auto-power-save modes like doze and
+         app standby.  These are not enabled by default because they require a standard
+         cloud-to-device messaging service for apps to interact correctly with the modes
+         (such as to be able to deliver an instant message to the device even when it is
+         dozing).  This should be enabled if you have such services and expect apps to
+         correctly use them when installed on your device.  Otherwise, keep this disabled
+         so that applications can still use their own mechanisms. -->
+    <bool name="config_enableAutoPowerModes">false</bool>
+
     <!-- The duration (in milliseconds) that the radio will scan for a signal
          when there's no network connection. If the scan doesn't timeout, use zero -->
     <integer name="config_radioScanningTimeout">0</integer>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 295251c..e6f54ee 100755
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -251,6 +251,7 @@
   <java-symbol type="bool" name="config_bluetooth_le_peripheral_mode_supported" />
   <java-symbol type="bool" name="config_cellBroadcastAppLinks" />
   <java-symbol type="bool" name="config_duplicate_port_omadm_wappush" />
+  <java-symbol type="bool" name="config_enableAutoPowerModes" />
   <java-symbol type="bool" name="config_enable_emergency_call_while_sim_locked" />
   <java-symbol type="bool" name="config_enable_puk_unlock_screen" />
   <java-symbol type="bool" name="config_enableBurnInProtection" />
diff --git a/services/core/java/com/android/server/DeviceIdleController.java b/services/core/java/com/android/server/DeviceIdleController.java
index 82b334a..dc203ff 100644
--- a/services/core/java/com/android/server/DeviceIdleController.java
+++ b/services/core/java/com/android/server/DeviceIdleController.java
@@ -97,9 +97,6 @@
     private static final String ACTION_STEP_IDLE_STATE =
             "com.android.server.device_idle.STEP_IDLE_STATE";
 
-    private static final String ACTION_ENTER_INACTIVE_STATE =
-        "com.android.server.device_idle.ENTER_INACTIVE_STATE";
-
     private AlarmManager mAlarmManager;
     private IBatteryStats mBatteryStats;
     private PowerManagerInternal mLocalPowerManager;
@@ -112,7 +109,7 @@
     private Intent mIdleIntent;
     private Display mCurDisplay;
     private AnyMotionDetector mAnyMotionDetector;
-    private boolean mIdleDisabled;
+    private boolean mEnabled;
     private boolean mScreenOn;
     private boolean mCharging;
     private boolean mSigMotionActive;
@@ -191,10 +188,6 @@
                 synchronized (DeviceIdleController.this) {
                     stepIdleStateLocked();
                 }
-            } else if (ACTION_ENTER_INACTIVE_STATE.equals(intent.getAction())) {
-                synchronized (DeviceIdleController.this) {
-                    enterInactiveStateLocked();
-                }
             }
         }
     };
@@ -612,6 +605,8 @@
         final PackageManager pm = getContext().getPackageManager();
 
         synchronized (this) {
+            mEnabled = getContext().getResources().getBoolean(
+                    com.android.internal.R.bool.config_enableAutoPowerModes);
             SystemConfig sysConfig = SystemConfig.getInstance();
             ArraySet<String> allowPower = sysConfig.getAllowInPowerSave();
             for (int i=0; i<allowPower.size(); i++) {
@@ -881,7 +876,7 @@
 
     void becomeInactiveIfAppropriateLocked() {
         if (DEBUG) Slog.d(TAG, "becomeInactiveIfAppropriateLocked()");
-        if (!mScreenOn && !mCharging && !mIdleDisabled && mState == STATE_ACTIVE) {
+        if (!mScreenOn && !mCharging && mEnabled && mState == STATE_ACTIVE) {
             // Screen has turned off; we are now going to become inactive and start
             // waiting to see if we will ultimately go idle.
             mState = STATE_INACTIVE;
@@ -1216,8 +1211,12 @@
         pw.println("    Completely disable device idle mode.");
         pw.println("  enable");
         pw.println("    Re-enable device idle mode after it had previously been disabled.");
-        pw.println("  whitelist");
+        pw.println("  enabled");
+        pw.println("    Print 1 if device idle mode is currently enabled, else 0.");
+        pw.println("  whitelist [package ...]");
         pw.println("    Add (prefix with +) or remove (prefix with -) packages.");
+        pw.println("  tempwhitelist [package ..]");
+        pw.println("    Temporarily place packages in whitelist for 10 seconds.");
     }
 
     void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
@@ -1252,8 +1251,8 @@
                     return;
                 } else if ("disable".equals(arg)) {
                     synchronized (this) {
-                        if (!mIdleDisabled) {
-                            mIdleDisabled = true;
+                        if (mEnabled) {
+                            mEnabled = false;
                             becomeActiveLocked("disabled", Process.myUid());
                             pw.println("Idle mode disabled");
                         }
@@ -1261,13 +1260,18 @@
                     return;
                 } else if ("enable".equals(arg)) {
                     synchronized (this) {
-                        if (mIdleDisabled) {
-                            mIdleDisabled = false;
+                        if (!mEnabled) {
+                            mEnabled = true;
                             becomeInactiveIfAppropriateLocked();
                             pw.println("Idle mode enabled");
                         }
                     }
                     return;
+                } else if ("enabled".equals(arg)) {
+                    synchronized (this) {
+                        pw.println(mEnabled ? "1" : " 0");
+                    }
+                    return;
                 } else if ("whitelist".equals(arg)) {
                     i++;
                     while (i < args.length) {
@@ -1364,9 +1368,9 @@
                 }
             }
 
+            pw.print("  mEnabled="); pw.println(mEnabled);
             pw.print("  mSigMotionSensor="); pw.println(mSigMotionSensor);
             pw.print("  mCurDisplay="); pw.println(mCurDisplay);
-            pw.print("  mIdleDisabled="); pw.println(mIdleDisabled);
             pw.print("  mScreenOn="); pw.println(mScreenOn);
             pw.print("  mCharging="); pw.println(mCharging);
             pw.print("  mSigMotionActive="); pw.println(mSigMotionActive);
diff --git a/services/usage/java/com/android/server/usage/UsageStatsService.java b/services/usage/java/com/android/server/usage/UsageStatsService.java
index 3b7ed91..490236e 100644
--- a/services/usage/java/com/android/server/usage/UsageStatsService.java
+++ b/services/usage/java/com/android/server/usage/UsageStatsService.java
@@ -35,8 +35,6 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.SyncAdapterType;
-import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ParceledListSlice;
@@ -74,7 +72,6 @@
 import com.android.internal.os.BackgroundThread;
 import com.android.internal.os.SomeArgs;
 import com.android.internal.util.IndentingPrintWriter;
-import com.android.server.DeviceIdleController;
 import com.android.server.SystemService;
 
 import java.io.BufferedReader;
@@ -138,6 +135,7 @@
     long mRealTimeSnapshot;
     long mSystemTimeSnapshot;
 
+    boolean mAppIdleEnabled;
     boolean mAppIdleParoled;
     private boolean mScreenOn;
     private long mLastAppIdleParoledTime;
@@ -175,10 +173,15 @@
         getContext().registerReceiverAsUser(new UserActionsReceiver(), UserHandle.ALL, userActions,
                 null, null);
 
-        IntentFilter deviceStates = new IntentFilter(BatteryManager.ACTION_CHARGING);
-        deviceStates.addAction(BatteryManager.ACTION_DISCHARGING);
-        deviceStates.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
-        getContext().registerReceiver(new DeviceStateReceiver(), deviceStates);
+        mAppIdleEnabled = getContext().getResources().getBoolean(
+                com.android.internal.R.bool.config_enableAutoPowerModes);
+        if (mAppIdleEnabled) {
+            IntentFilter deviceStates = new IntentFilter(BatteryManager.ACTION_CHARGING);
+            deviceStates.addAction(BatteryManager.ACTION_DISCHARGING);
+            deviceStates.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
+            getContext().registerReceiver(new DeviceStateReceiver(), deviceStates);
+        }
+
         synchronized (mLock) {
             cleanUpRemovedUsersLocked();
         }
@@ -186,7 +189,6 @@
         mRealTimeSnapshot = SystemClock.elapsedRealtime();
         mSystemTimeSnapshot = System.currentTimeMillis();
 
-
         publishLocalService(UsageStatsManagerInternal.class, new LocalService());
         publishBinderService(Context.USAGE_STATS_SERVICE, new BinderService());
     }
@@ -342,6 +344,10 @@
 
     /** Check all running users' or specified user's apps to see if they enter an idle state. */
     void checkIdleStates(int checkUserId) {
+        if (!mAppIdleEnabled) {
+            return;
+        }
+
         final int[] userIds;
         try {
             if (checkUserId == UserHandle.USER_ALL) {
@@ -772,6 +778,10 @@
     private boolean isAppIdleFiltered(String packageName, int userId,
             UserUsageStatsService userService, long timeNow, long screenOnTime) {
         if (packageName == null) return false;
+        // If not enabled at all, of course nobody is ever idle.
+        if (!mAppIdleEnabled) {
+            return false;
+        }
         synchronized (mLock) {
             // Temporary exemption, probably due to device charging or occasional allowance to
             // be allowed to sync, etc.
@@ -900,6 +910,19 @@
             pw.print("  mAppIdleParoleDurationMillis=");
             TimeUtils.formatDuration(mAppIdleParoleDurationMillis, pw);
             pw.println();
+
+            pw.println();
+            pw.print("mAppIdleEnabled="); pw.print(mAppIdleEnabled);
+            pw.print(" mAppIdleParoled="); pw.print(mAppIdleParoled);
+            pw.print(" mScreenOn="); pw.println(mScreenOn);
+            pw.print("mLastAppIdleParoledTime=");
+            TimeUtils.formatDuration(mLastAppIdleParoledTime, pw);
+            pw.println();
+            pw.print("mScreenOnTime="); TimeUtils.formatDuration(mScreenOnTime, pw);
+            pw.println();
+            pw.print("mScreenOnSystemTimeSnapshot=");
+            TimeUtils.formatDuration(mScreenOnSystemTimeSnapshot, pw);
+            pw.println();
         }
     }