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

Commit 9cc471ce authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Fix wrong use if "!UserHandle.isApp()"

Turned out !UserHandle.isApp() doesn't mean "is system" because of isolated UIDs.

Bug: 72174012
Test: atest $ANDROID_BUILD_TOP/frameworks/base/services/tests/servicestests/src/com/android/server/ForceAppStandbyTrackerTest.java
Change-Id: Ibf6175d550ecc74677877c74db2473bf3f30132d
parent bab202f7
Loading
Loading
Loading
Loading
+8 −0
Original line number Original line Diff line number Diff line
@@ -573,6 +573,14 @@ public class Process {
        return UserHandle.of(UserHandle.getUserId(myUid()));
        return UserHandle.of(UserHandle.getUserId(myUid()));
    }
    }


    /**
     * Returns whether the given uid belongs to a system core component or not.
     * @hide
     */
    public static boolean isCoreUid(int uid) {
        return UserHandle.isCore(uid);
    }

    /**
    /**
     * Returns whether the given uid belongs to an application.
     * Returns whether the given uid belongs to an application.
     * @param uid A kernel uid.
     * @param uid A kernel uid.
+22 −2
Original line number Original line Diff line number Diff line
@@ -126,7 +126,10 @@ public final class UserHandle implements Parcelable {
        return getAppId(uid1) == getAppId(uid2);
        return getAppId(uid1) == getAppId(uid2);
    }
    }


    /** @hide */
    /**
     * Whether a UID is an "isolated" UID.
     * @hide
     */
    public static boolean isIsolated(int uid) {
    public static boolean isIsolated(int uid) {
        if (uid > 0) {
        if (uid > 0) {
            final int appId = getAppId(uid);
            final int appId = getAppId(uid);
@@ -136,7 +139,11 @@ public final class UserHandle implements Parcelable {
        }
        }
    }
    }


    /** @hide */
    /**
     * Whether a UID belongs to a regular app. *Note* "Not a regular app" does not mean
     * "it's system", because of isolated UIDs. Use {@link #isCore} for that.
     * @hide
     */
    public static boolean isApp(int uid) {
    public static boolean isApp(int uid) {
        if (uid > 0) {
        if (uid > 0) {
            final int appId = getAppId(uid);
            final int appId = getAppId(uid);
@@ -146,6 +153,19 @@ public final class UserHandle implements Parcelable {
        }
        }
    }
    }


    /**
     * Whether a UID belongs to a system core component or not.
     * @hide
     */
    public static boolean isCore(int uid) {
        if (uid > 0) {
            final int appId = getAppId(uid);
            return appId < Process.FIRST_APPLICATION_UID;
        } else {
            return false;
        }
    }

    /**
    /**
     * Returns the user for a given uid.
     * Returns the user for a given uid.
     * @param uid A uid for an application running in a particular user.
     * @param uid A uid for an application running in a particular user.
+3 −3
Original line number Original line Diff line number Diff line
@@ -504,7 +504,7 @@ public class ForceAppStandbyTracker {
     */
     */
    void uidToForeground(int uid) {
    void uidToForeground(int uid) {
        synchronized (mLock) {
        synchronized (mLock) {
            if (!UserHandle.isApp(uid)) {
            if (UserHandle.isCore(uid)) {
                return;
                return;
            }
            }
            // TODO This can be optimized by calling indexOfKey and sharing the index for get and
            // TODO This can be optimized by calling indexOfKey and sharing the index for get and
@@ -522,7 +522,7 @@ public class ForceAppStandbyTracker {
     */
     */
    void uidToBackground(int uid, boolean remove) {
    void uidToBackground(int uid, boolean remove) {
        synchronized (mLock) {
        synchronized (mLock) {
            if (!UserHandle.isApp(uid)) {
            if (UserHandle.isCore(uid)) {
                return;
                return;
            }
            }
            // TODO This can be optimized by calling indexOfKey and sharing the index for get and
            // TODO This can be optimized by calling indexOfKey and sharing the index for get and
@@ -864,7 +864,7 @@ public class ForceAppStandbyTracker {
     * Note clients normally shouldn't need to access it. It's only for dumpsys.
     * Note clients normally shouldn't need to access it. It's only for dumpsys.
     */
     */
    public boolean isInForeground(int uid) {
    public boolean isInForeground(int uid) {
        if (!UserHandle.isApp(uid)) {
        if (UserHandle.isCore(uid)) {
            return true;
            return true;
        }
        }
        synchronized (mLock) {
        synchronized (mLock) {
+2 −3
Original line number Original line Diff line number Diff line
@@ -425,7 +425,6 @@ import com.android.server.SystemServiceManager;
import com.android.server.ThreadPriorityBooster;
import com.android.server.ThreadPriorityBooster;
import com.android.server.Watchdog;
import com.android.server.Watchdog;
import com.android.server.am.ActivityStack.ActivityState;
import com.android.server.am.ActivityStack.ActivityState;
import com.android.server.am.EventLogTags;
import com.android.server.am.proto.ActivityManagerServiceProto;
import com.android.server.am.proto.ActivityManagerServiceProto;
import com.android.server.am.proto.BroadcastProto;
import com.android.server.am.proto.BroadcastProto;
import com.android.server.am.proto.GrantUriProto;
import com.android.server.am.proto.GrantUriProto;
@@ -8834,7 +8833,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            case AppOpsManager.MODE_ALLOWED:
            case AppOpsManager.MODE_ALLOWED:
                // If force-background-check is enabled, restrict all apps that aren't whitelisted.
                // If force-background-check is enabled, restrict all apps that aren't whitelisted.
                if (mForceBackgroundCheck &&
                if (mForceBackgroundCheck &&
                        UserHandle.isApp(uid) &&
                        !UserHandle.isCore(uid) &&
                        !isOnDeviceIdleWhitelistLocked(uid)) {
                        !isOnDeviceIdleWhitelistLocked(uid)) {
                    if (DEBUG_BACKGROUND_CHECK) {
                    if (DEBUG_BACKGROUND_CHECK) {
                        Slog.i(TAG, "Force background check: " +
                        Slog.i(TAG, "Force background check: " +
@@ -24086,7 +24085,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        final int size = mActiveUids.size();
        final int size = mActiveUids.size();
        for (int i = 0; i < size; i++) {
        for (int i = 0; i < size; i++) {
            final int uid = mActiveUids.keyAt(i);
            final int uid = mActiveUids.keyAt(i);
            if (!UserHandle.isApp(uid)) {
            if (UserHandle.isCore(uid)) {
                continue;
                continue;
            }
            }
            final UidRecord uidRec = mActiveUids.valueAt(i);
            final UidRecord uidRec = mActiveUids.valueAt(i);