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

Commit ca84501f authored by Jay Sullivan's avatar Jay Sullivan Committed by Android (Google) Code Review
Browse files

Merge "Make isDeviceOwnerApp user aware" into main

parents 66498042 2bf8ceb8
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
@@ -81,6 +81,9 @@ import android.app.Activity;
import android.app.IServiceConnection;
import android.app.KeyguardManager;
import android.app.admin.SecurityLog.SecurityEvent;
import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.ComponentName;
import android.content.Context;
@@ -9117,6 +9120,19 @@ public class DevicePolicyManager {
        return false;
    }
    /**
     * For apps targeting {@link Build.VERSION_CODES#VANILLA_ICE_CREAM} and above, the
     * {@link #isDeviceOwnerApp} method will use the user contained within the
     * context.
     * For apps targeting an SDK version <em>below</em> this, the user of the calling process will
     * be used (Process.myUserHandle()).
     *
     * @hide
     */
    @ChangeId
    @EnabledSince(targetSdkVersion = Build.VERSION_CODES.VANILLA_ICE_CREAM)
    public static final long IS_DEVICE_OWNER_USER_AWARE = 307233716L;
    /**
     * Used to determine if a particular package has been registered as a Device Owner app.
     * A device owner app is a special device admin that cannot be deactivated by the user, once
@@ -9130,8 +9146,13 @@ public class DevicePolicyManager {
     * app, if any.
     * @return whether or not the package is registered as the device owner app.
     */
    @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.VANILLA_ICE_CREAM)
    public boolean isDeviceOwnerApp(String packageName) {
        throwIfParentInstance("isDeviceOwnerApp");
        if (android.permission.flags.Flags.roleControllerInSystemServer()
                && CompatChanges.isChangeEnabled(IS_DEVICE_OWNER_USER_AWARE)) {
            return isDeviceOwnerAppOnContextUser(packageName);
        }
        return isDeviceOwnerAppOnCallingUser(packageName);
    }
@@ -9192,6 +9213,24 @@ public class DevicePolicyManager {
        return packageName.equals(deviceOwner.getPackageName());
    }
    private boolean isDeviceOwnerAppOnContextUser(String packageName) {
        if (packageName == null) {
            return false;
        }
        ComponentName deviceOwner = null;
        if (mService != null) {
            try {
                deviceOwner = mService.getDeviceOwnerComponentOnUser(myUserId());
            } catch (RemoteException re) {
                throw re.rethrowFromSystemServer();
            }
        }
        if (deviceOwner == null) {
            return false;
        }
        return packageName.equals(deviceOwner.getPackageName());
    }
    private ComponentName getDeviceOwnerComponentInner(boolean callingUserOnly) {
        if (mService != null) {
            try {
@@ -9608,6 +9647,7 @@ public class DevicePolicyManager {
     * @param packageName The package name of the app to compare with the registered profile owner.
     * @return Whether or not the package is registered as the profile owner.
     */
    @UserHandleAware
    public boolean isProfileOwnerApp(String packageName) {
        throwIfParentInstance("isProfileOwnerApp");
        if (mService != null) {
+1 −0
Original line number Diff line number Diff line
@@ -179,6 +179,7 @@ interface IDevicePolicyManager {

    boolean setDeviceOwner(in ComponentName who, int userId, boolean setProfileOwnerOnCurrentUserIfNecessary);
    ComponentName getDeviceOwnerComponent(boolean callingUserOnly);
    ComponentName getDeviceOwnerComponentOnUser(int userId);
    boolean hasDeviceOwner();
    String getDeviceOwnerName();
    void clearDeviceOwner(String packageName);
+20 −0
Original line number Diff line number Diff line
@@ -9667,6 +9667,26 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        }
    }
    @Override
    public ComponentName getDeviceOwnerComponentOnUser(int userId) {
        if (!mHasFeature) {
            return null;
        }
        if (mInjector.userHandleGetCallingUserId() != userId) {
            Preconditions.checkCallAuthorization(canManageUsers(getCallerIdentity())
                    || hasCallingOrSelfPermission(MANAGE_PROFILE_AND_DEVICE_OWNERS));
        }
        synchronized (getLockObject()) {
            // There is only ever one device owner on a device so if the passed userId is the same
            // as the device owner userId we know that the componentName returned by
            // getDeviceOwnerComponent will be the correct one.
            if (mOwners.getDeviceOwnerUserId() == userId || userId == UserHandle.USER_ALL) {
                return mOwners.getDeviceOwnerComponent();
            }
        }
        return null;
    }
    private int getDeviceOwnerUserIdUncheckedLocked() {
        return mOwners.hasDeviceOwner() ? mOwners.getDeviceOwnerUserId() : UserHandle.USER_NULL;
    }