Fix issue #31403654: Elevation of privilege vulnerability in deviceidle service

Whoops, cleared the calling identity too soon.

Change-Id: I93e3daeafafe8ee8ac92b81011f50f08a83e5229
diff --git a/services/core/java/com/android/server/DeviceIdleController.java b/services/core/java/com/android/server/DeviceIdleController.java
index 488f0e7..4405c1b 100644
--- a/services/core/java/com/android/server/DeviceIdleController.java
+++ b/services/core/java/com/android/server/DeviceIdleController.java
@@ -2726,12 +2726,12 @@
                 }
             }
         } else if ("whitelist".equals(cmd)) {
-            long token = Binder.clearCallingIdentity();
-            try {
-                String arg = shell.getNextArg();
-                if (arg != null) {
-                    getContext().enforceCallingOrSelfPermission(
-                            android.Manifest.permission.DEVICE_POWER, null);
+            String arg = shell.getNextArg();
+            if (arg != null) {
+                getContext().enforceCallingOrSelfPermission(
+                        android.Manifest.permission.DEVICE_POWER, null);
+                long token = Binder.clearCallingIdentity();
+                try {
                     do {
                         if (arg.length() < 1 || (arg.charAt(0) != '-'
                                 && arg.charAt(0) != '+' && arg.charAt(0) != '=')) {
@@ -2754,30 +2754,30 @@
                             pw.println(getPowerSaveWhitelistAppInternal(pkg));
                         }
                     } while ((arg=shell.getNextArg()) != null);
-                } else {
-                    synchronized (this) {
-                        for (int j=0; j<mPowerSaveWhitelistAppsExceptIdle.size(); j++) {
-                            pw.print("system-excidle,");
-                            pw.print(mPowerSaveWhitelistAppsExceptIdle.keyAt(j));
-                            pw.print(",");
-                            pw.println(mPowerSaveWhitelistAppsExceptIdle.valueAt(j));
-                        }
-                        for (int j=0; j<mPowerSaveWhitelistApps.size(); j++) {
-                            pw.print("system,");
-                            pw.print(mPowerSaveWhitelistApps.keyAt(j));
-                            pw.print(",");
-                            pw.println(mPowerSaveWhitelistApps.valueAt(j));
-                        }
-                        for (int j=0; j<mPowerSaveWhitelistUserApps.size(); j++) {
-                            pw.print("user,");
-                            pw.print(mPowerSaveWhitelistUserApps.keyAt(j));
-                            pw.print(",");
-                            pw.println(mPowerSaveWhitelistUserApps.valueAt(j));
-                        }
+                } finally {
+                    Binder.restoreCallingIdentity(token);
+                }
+            } else {
+                synchronized (this) {
+                    for (int j=0; j<mPowerSaveWhitelistAppsExceptIdle.size(); j++) {
+                        pw.print("system-excidle,");
+                        pw.print(mPowerSaveWhitelistAppsExceptIdle.keyAt(j));
+                        pw.print(",");
+                        pw.println(mPowerSaveWhitelistAppsExceptIdle.valueAt(j));
+                    }
+                    for (int j=0; j<mPowerSaveWhitelistApps.size(); j++) {
+                        pw.print("system,");
+                        pw.print(mPowerSaveWhitelistApps.keyAt(j));
+                        pw.print(",");
+                        pw.println(mPowerSaveWhitelistApps.valueAt(j));
+                    }
+                    for (int j=0; j<mPowerSaveWhitelistUserApps.size(); j++) {
+                        pw.print("user,");
+                        pw.print(mPowerSaveWhitelistUserApps.keyAt(j));
+                        pw.print(",");
+                        pw.println(mPowerSaveWhitelistUserApps.valueAt(j));
                     }
                 }
-            } finally {
-                Binder.restoreCallingIdentity(token);
             }
         } else if ("tempwhitelist".equals(cmd)) {
             String opt;