Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a7f8eaa1 authored by Hui Yu's avatar Hui Yu
Browse files

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
parent 4c1410fa
Loading
Loading
Loading
Loading
+3 −6
Original line number Diff line number Diff line
@@ -32,12 +32,8 @@ public interface DeviceIdleInternal {

    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 @@ public interface DeviceIdleInternal {
     * @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();
+14 −19
Original line number Diff line number Diff line
@@ -1943,19 +1943,11 @@ public class DeviceIdleController extends SystemService
        }

        // 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 @@ public class DeviceIdleController extends SystemService
        @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 @@ public class DeviceIdleController extends SystemService
    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 @@ public class DeviceIdleController extends SystemService
        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;
+1 −0
Original line number Diff line number Diff line
@@ -628,6 +628,7 @@ message ActivityManagerServiceDumpProcessesProto {
        optional string tag = 3;
        optional int32 type = 4;
        optional int32 reason_code = 5;
        optional int32 calling_uid = 6;
    }
    repeated PendingTempWhitelist pending_temp_whitelist = 26;

+11 −4
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_HIGH;
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 @@ public class ActivityManagerService extends IActivityManager.Stub
        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 @@ public class ActivityManagerService extends IActivityManager.Stub
            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 @@ public class ActivityManagerService extends IActivityManager.Stub
                    pw.print(ptw.type);
                    pw.print(" ");
                    pw.print(ptw.reasonCode);
                    pw.print(" ");
                    pw.print(ptw.callingUid);
                }
            }
        }
@@ -14510,7 +14515,8 @@ public class ActivityManagerService extends IActivityManager.Stub
            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 @@ public class ActivityManagerService extends IActivityManager.Stub
            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);
            }
        }
+2 −0
Original line number Diff line number Diff line
@@ -930,6 +930,8 @@ public final class BroadcastQueue {
        } 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());