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

Commit 5691ba29 authored by Felipe Leme's avatar Felipe Leme
Browse files

Log calls to isMainUser() and other deprecated methods.

Test: adb shell setprop log.tag.MultiuserDeprecation VERBOSE && adb logcat MultiuserDeprecation *:s
Bug: 414326600
Bug: 418215389
Flag: EXEMPT debugging changes

Change-Id: I15625b5671b3ef23f3de621fd98aaa8562f84623
parent acee2b54
Loading
Loading
Loading
Loading
+24 −2
Original line number Diff line number Diff line
@@ -369,6 +369,14 @@ public class UserInfo implements Parcelable {
    @UnsupportedAppUsage
    @Deprecated
    public boolean isPrimary() {
        UserManager.logStaticDeprecation();
        return isPrimaryUnlogged();
    }

    /**
     * @hide
     */
    public boolean isPrimaryUnlogged() {
        return (flags & FLAG_PRIMARY) == FLAG_PRIMARY;
    }

@@ -451,6 +459,20 @@ public class UserInfo implements Parcelable {
     * @see #FLAG_MAIN
     */
    public boolean isMain() {
        UserManager.logStaticDeprecation();
        return isMainUnlogged();
    }

    /**
     * Same as {@link #isMain()}, but doesn't log the call (when logging multi-user violations is
     * enabled).
     *
     * <p>Should be called by methods that don't need to log (for example, because they're internal
     * to user infra) or already logged (to avoid duplicate entries).
     *
     * @hide
     */
    public boolean isMainUnlogged() {
        return (flags & FLAG_MAIN) == FLAG_MAIN;
    }

@@ -504,7 +526,7 @@ public class UserInfo implements Parcelable {
        // NOTE: profiles used to be restricted just to the system user (and later to the main
        // user), but from the framework point of view there is no need for such restriction, hence
        // it's lifted
        return isMain()
        return isMainUnlogged()
                || (android.multiuser.Flags.profilesForAll()
                        && Resources.getSystem().getBoolean(
                                com.android.internal.R.bool.config_supportProfilesOnNonMainUser));
@@ -521,7 +543,7 @@ public class UserInfo implements Parcelable {
        if (UserManager.isUserTypePrivateProfile(userType)) {
            // Even if we eventually allow other users to have profiles too, only MainUsers are
            // eligible to have a Private Space, for some reason.
            return isMain();
            return isMainUnlogged();
        }
        return true;
    }
+40 −2
Original line number Diff line number Diff line
@@ -101,6 +101,10 @@ public class UserManager {

    private static final String TAG = "UserManager";

    private static final String DEPRECATION_LOG_TAG = "MultiuserDeprecation";
    private static final boolean DEBUG_LOG_DEPRECATION =
            Build.isDebuggable() && Log.isLoggable(DEPRECATION_LOG_TAG, Log.VERBOSE);

    @UnsupportedAppUsage
    private final IUserManager mService;
    /** Holding the Application context (not constructor param context). */
@@ -3027,8 +3031,9 @@ public class UserManager {
            Manifest.permission.QUERY_USERS})
    @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU)
    public boolean isPrimaryUser() {
        logDeprecation();
        final UserInfo user = getUserInfo(getContextUserIfAppropriate());
        return user != null && user.isPrimary();
        return user != null && user.isPrimaryUnlogged();
    }

    /**
@@ -3072,8 +3077,9 @@ public class UserManager {
            Manifest.permission.QUERY_USERS})
    @UserHandleAware
    public boolean isMainUser() {
        logDeprecation();
        final UserInfo user = getUserInfo(mUserId);
        return user != null && user.isMain();
        return user != null && user.isMainUnlogged();
    }

    /**
@@ -3094,6 +3100,7 @@ public class UserManager {
            Manifest.permission.CREATE_USERS,
            Manifest.permission.QUERY_USERS})
    public @Nullable UserHandle getMainUser() {
        logDeprecation();
        try {
            final int mainUserId = mService.getMainUserId();
            if (mainUserId == UserHandle.USER_NULL) {
@@ -3104,6 +3111,7 @@ public class UserManager {
            throw re.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the designated "communal profile" of the device, or {@code null} if there is none.
     * @hide
@@ -6964,6 +6972,36 @@ public class UserManager {
        }
    }

    /**
     * Logs a call to a deprecated multi-user API.
     *
     * <p>NOTE: it's a no-op if build is not debuggable or log level of
     * {@value #DEPRECATION_LOG_TAG} is not verbose.
     *
     * @hide
     */
    public void logDeprecation() {
        if (!DEBUG_LOG_DEPRECATION) {
            return;
        }
        Log.v(DEPRECATION_LOG_TAG, "deprecated call on pkg " + mContext.getPackageName()
                + " (from user " + mUserId + "):", new Exception());
    }

    /**
     * Same as {@link #logDeprecation()}, but for cases where the caller doesn't have a reference
     * to a {@link UserManager} (like methods from system server components or POJOs like
     * {@code UserInfo}.
     *
     * @hide
     */
    public static void logStaticDeprecation() {
        if (!DEBUG_LOG_DEPRECATION) {
            return;
        }
        Log.v(DEPRECATION_LOG_TAG, "deprecated call:", new Exception());
    }

    /* Cache key for anything that assumes that userIds cannot be re-used without rebooting. */
    private static final String CACHE_KEY_STATIC_USER_PROPERTIES =
        PropertyInvalidatedCache.createPropertyName(
+3 −2
Original line number Diff line number Diff line
@@ -1385,7 +1385,7 @@ public class UserManagerService extends IUserManager.Stub {
        int userSize = mUsers.size();
        for (int i = 0; i < userSize; i++) {
            UserInfo user = mUsers.valueAt(i).info;
            if (user.isMain() && !mRemovingUserIds.get(user.id)) {
            if (user.isMainUnlogged() && !mRemovingUserIds.get(user.id)) {
                return user;
            }
        }
@@ -2530,7 +2530,7 @@ public class UserManagerService extends IUserManager.Stub {
            }
            return getOwnerName();
        }
        if (user.isMain()) {
        if (user.isMainUnlogged()) {
            return getOwnerName();
        }
        if (user.isGuest()) {
@@ -8710,6 +8710,7 @@ public class UserManagerService extends IUserManager.Stub {

        @Override
        public @UserIdInt int getMainUserId() {
            UserManager.logStaticDeprecation();
            return getMainUserIdUnchecked();
        }