callingUid needs to be passed from AMS to DeviceIdleController.

callingUid is the UID of the app who added the temp-allowlist entry, it
needs to be passed from AMS to DeviceIdleController.

The callingUid will go into Westworld metric
ForegroundServiceStateChanged.

Bug: 171305836
Test: Observe the callingUid while grepping for "tempAllowListReason" in adb logcat, it should be a non-zero value.
BYPASS_INCLUSIVE_LANGUAGE_REASON=Existing public API.

Change-Id: I19f5cf8f72d5fbfb3fd6caefe6978e75d4c1a801
diff --git a/apex/jobscheduler/framework/java/com/android/server/DeviceIdleInternal.java b/apex/jobscheduler/framework/java/com/android/server/DeviceIdleInternal.java
index 5e5717d11..0dde546 100644
--- a/apex/jobscheduler/framework/java/com/android/server/DeviceIdleInternal.java
+++ b/apex/jobscheduler/framework/java/com/android/server/DeviceIdleInternal.java
@@ -32,12 +32,8 @@
 
     void exitIdle(String reason);
 
-    // duration in milliseconds
     void addPowerSaveTempWhitelistApp(int callingUid, String packageName,
-            long duration, int userId, boolean sync, String reason);
-
-    void addPowerSaveTempWhitelistApp(int callingUid, String packageName,
-            long duration, int userId, boolean sync, @ReasonCode int reasonCode,
+            long durationMs, int userId, boolean sync, @ReasonCode int reasonCode,
             @Nullable String reason);
 
     /**
@@ -49,10 +45,11 @@
      * @param sync
      * @param reasonCode one of {@link ReasonCode}
      * @param reason
+     * @param callingUid UID of app who added this temp-allowlist.
      */
     void addPowerSaveTempWhitelistAppDirect(int uid, long duration,
             @TempAllowListType int type, boolean sync, @ReasonCode int reasonCode,
-            @Nullable String reason);
+            @Nullable String reason, int callingUid);
 
     // duration in milliseconds
     long getNotificationAllowlistDuration();
diff --git a/apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java b/apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java
index ac28e82..84fb39b 100644
--- a/apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java
+++ b/apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java
@@ -1943,19 +1943,11 @@
         }
 
         // duration in milliseconds
-        @Deprecated
         @Override
         public void addPowerSaveTempWhitelistApp(int callingUid, String packageName,
-                long duration, int userId, boolean sync, @Nullable String reason) {
-            addPowerSaveTempAllowlistAppInternal(callingUid, packageName, duration,
-                    userId, sync, REASON_UNKNOWN, reason);
-        }
-
-        @Override
-        public void addPowerSaveTempWhitelistApp(int callingUid, String packageName,
-                long duration, int userId, boolean sync, @ReasonCode int reasonCode,
+                long durationMs, int userId, boolean sync, @ReasonCode int reasonCode,
                 @Nullable String reason) {
-            addPowerSaveTempAllowlistAppInternal(callingUid, packageName, duration,
+            addPowerSaveTempAllowlistAppInternal(callingUid, packageName, durationMs,
                     userId, sync, reasonCode, reason);
         }
 
@@ -1963,8 +1955,8 @@
         @Override
         public void addPowerSaveTempWhitelistAppDirect(int uid, long duration,
                 @TempAllowListType int type, boolean sync, @ReasonCode int reasonCode,
-                @Nullable String reason) {
-            addPowerSaveTempWhitelistAppDirectInternal(0, uid, duration, type, sync,
+                @Nullable String reason, int callingUid) {
+            addPowerSaveTempWhitelistAppDirectInternal(callingUid, uid, duration, type, sync,
                     reasonCode, reason);
         }
 
@@ -2741,6 +2733,16 @@
     void addPowerSaveTempAllowlistAppInternal(int callingUid, String packageName,
             long duration, int userId, boolean sync, @ReasonCode int reasonCode,
             @Nullable String reason) {
+        synchronized (this) {
+            int callingAppId = UserHandle.getAppId(callingUid);
+            if (callingAppId >= Process.FIRST_APPLICATION_UID) {
+                if (!mPowerSaveWhitelistSystemAppIds.get(callingAppId)) {
+                    throw new SecurityException(
+                            "Calling app " + UserHandle.formatUid(callingUid)
+                                    + " is not on whitelist");
+                }
+            }
+        }
         try {
             int uid = getContext().getPackageManager().getPackageUidAsUser(packageName, userId);
             addPowerSaveTempWhitelistAppDirectInternal(callingUid, uid, duration,
@@ -2760,13 +2762,6 @@
         boolean informWhitelistChanged = false;
         int appId = UserHandle.getAppId(uid);
         synchronized (this) {
-            int callingAppId = UserHandle.getAppId(callingUid);
-            if (callingAppId >= Process.FIRST_APPLICATION_UID) {
-                if (!mPowerSaveWhitelistSystemAppIds.get(callingAppId)) {
-                    throw new SecurityException("Calling app " + UserHandle.formatUid(callingUid)
-                            + " is not on whitelist");
-                }
-            }
             duration = Math.min(duration, mConstants.MAX_TEMP_APP_ALLOWLIST_DURATION_MS);
             Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.get(appId);
             final boolean newEntry = entry == null;
diff --git a/core/proto/android/server/activitymanagerservice.proto b/core/proto/android/server/activitymanagerservice.proto
index ec41a47..74a37ca 100644
--- a/core/proto/android/server/activitymanagerservice.proto
+++ b/core/proto/android/server/activitymanagerservice.proto
@@ -628,6 +628,7 @@
         optional string tag = 3;
         optional int32 type = 4;
         optional int32 reason_code = 5;
+        optional int32 calling_uid = 6;
     }
     repeated PendingTempWhitelist pending_temp_whitelist = 26;
 
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 874e527..1480f7a 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -50,7 +50,6 @@
 import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_NORMAL;
 import static android.os.IServiceManager.DUMP_FLAG_PROTO;
 import static android.os.PowerWhitelistManager.REASON_SYSTEM_ALLOW_LISTED;
-import static android.os.PowerWhitelistManager.REASON_UNKNOWN;
 import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED;
 import static android.os.Process.BLUETOOTH_UID;
 import static android.os.Process.FIRST_APPLICATION_UID;
@@ -1178,14 +1177,16 @@
         final String tag;
         final int type;
         final @ReasonCode int reasonCode;
+        final int callingUid;
 
         PendingTempAllowlist(int targetUid, long duration, @ReasonCode int reasonCode, String tag,
-                int type) {
+                int type, int callingUid) {
             this.targetUid = targetUid;
             this.duration = duration;
             this.tag = tag;
             this.type = type;
             this.reasonCode = reasonCode;
+            this.callingUid = callingUid;
         }
 
         void dumpDebug(ProtoOutputStream proto, long fieldId) {
@@ -1198,6 +1199,8 @@
             proto.write(ActivityManagerServiceDumpProcessesProto.PendingTempWhitelist.TYPE, type);
             proto.write(ActivityManagerServiceDumpProcessesProto.PendingTempWhitelist.REASON_CODE,
                     reasonCode);
+            proto.write(ActivityManagerServiceDumpProcessesProto.PendingTempWhitelist.CALLING_UID,
+                    callingUid);
             proto.end(token);
         }
     }
@@ -9231,6 +9234,8 @@
                     pw.print(ptw.type);
                     pw.print(" ");
                     pw.print(ptw.reasonCode);
+                    pw.print(" ");
+                    pw.print(ptw.callingUid);
                 }
             }
         }
@@ -14510,7 +14515,8 @@
             String reason, int type, int callingUid) {
         synchronized (mProcLock) {
             mPendingTempAllowlist.put(targetUid,
-                    new PendingTempAllowlist(targetUid, duration, reasonCode, reason, type));
+                    new PendingTempAllowlist(targetUid, duration, reasonCode, reason, type,
+                            callingUid));
             setUidTempAllowlistStateLSP(targetUid, true);
             mUiHandler.obtainMessage(PUSH_TEMP_ALLOWLIST_UI_MSG).sendToTarget();
 
@@ -14541,7 +14547,8 @@
             for (int i = 0; i < N; i++) {
                 PendingTempAllowlist ptw = list[i];
                 mLocalDeviceIdleController.addPowerSaveTempWhitelistAppDirect(ptw.targetUid,
-                        ptw.duration, ptw.type, true, ptw.reasonCode, ptw.tag);
+                        ptw.duration, ptw.type, true, ptw.reasonCode, ptw.tag,
+                        ptw.callingUid);
             }
         }
 
diff --git a/services/core/java/com/android/server/am/BroadcastQueue.java b/services/core/java/com/android/server/am/BroadcastQueue.java
index 06cacc7..81c4c86 100644
--- a/services/core/java/com/android/server/am/BroadcastQueue.java
+++ b/services/core/java/com/android/server/am/BroadcastQueue.java
@@ -930,6 +930,8 @@
         } else if (r.intent.getData() != null) {
             b.append(r.intent.getData());
         }
+        b.append(",reason:");
+        b.append(reason);
         if (DEBUG_BROADCAST) {
             Slog.v(TAG, "Broadcast temp allowlist uid=" + uid + " duration=" + duration
                     + " type=" + type + " : " + b.toString());