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

Commit c371b1d4 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Added a DevicePolicyManager.setDeviceOwnerOnly() method."

parents baa37ca7 68061836
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -455,6 +455,7 @@ package android.app.admin {
    method @RequiresPermission("android.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS") public void resetDefaultCrossProfileIntentFilters(int);
    method @RequiresPermission(allOf={"android.permission.MANAGE_DEVICE_ADMINS", android.Manifest.permission.INTERACT_ACROSS_USERS_FULL}) public void setActiveAdmin(@NonNull android.content.ComponentName, boolean, int);
    method @RequiresPermission("android.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS") public boolean setDeviceOwner(@NonNull android.content.ComponentName, @Nullable String, int);
    method @RequiresPermission("android.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS") public boolean setDeviceOwnerOnly(@NonNull android.content.ComponentName, @Nullable String, int);
    method @RequiresPermission("android.permission.MANAGE_DEVICE_ADMINS") public void setNextOperationSafety(int, int);
    field public static final String ACTION_DATA_SHARING_RESTRICTION_APPLIED = "android.app.action.DATA_SHARING_RESTRICTION_APPLIED";
    field public static final String ACTION_DEVICE_POLICY_CONSTANTS_CHANGED = "android.app.action.DEVICE_POLICY_CONSTANTS_CHANGED";
+47 −10
Original line number Diff line number Diff line
@@ -7708,27 +7708,64 @@ public class DevicePolicyManager {
    }
    /**
     * @hide
     * Sets the given package as the device owner. The package must already be installed. There
     * must not already be a device owner.
     * Only apps with the MANAGE_PROFILE_AND_DEVICE_OWNERS permission and the shell uid can call
     * this method.
     * Calling this after the setup phase of the primary user has completed is allowed only if
     * the caller is the shell uid, and there are no additional users and no accounts.
     * Sets the given package as the device owner.
     *
     * <p>Preconditions:
     * <ul>
     *   <li>The package must already be installed.
     *   <li>There must not already be a device owner.
     *   <li>Only apps with the {@code MANAGE_PROFILE_AND_DEVICE_OWNERS} permission or the
     *       {@link Process#SHELL_UID Shell UID} can call this method.
     * </ul>
     *
     * <p>Calling this after the setup phase of the device owner user has completed is allowed only
     * if the caller is the {@link Process#SHELL_UID Shell UID}, and there are no additional users
     * (except when the device runs on headless system user mode, in which case it could have exact
     * one extra user, which is the current user - the device owner will be set in the
     * {@link UserHandle#SYSTEM system} user and a profile owner will be set in the current user)
     * and no accounts.
     *
     * @param who the component name to be registered as device owner.
     * @param ownerName the human readable name of the institution that owns this device.
     * @param userId ID of the user on which the device owner runs.
     *
     * @return whether the package was successfully registered as the device owner.
     * @throws IllegalArgumentException if the package name is null or invalid
     *
     * @throws IllegalArgumentException if the package name is {@code null} or invalid.
     * @throws IllegalStateException If the preconditions mentioned are not met.
     *
     * @hide
     */
    @TestApi
    @RequiresPermission(android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS)
    public boolean setDeviceOwner(@NonNull ComponentName who, @Nullable String ownerName,
            @UserIdInt int userId) {
        if (mService != null) {
            try {
                return mService.setDeviceOwner(who, ownerName, userId,
                        /* setProfileOwnerOnCurrentUserIfNecessary= */ true);
            } catch (RemoteException re) {
                throw re.rethrowFromSystemServer();
            }
        }
        return false;
    }
    /**
     * Same as {@link #setDeviceOwner(ComponentName, String, int)}, but without setting the profile
     * owner on current user when running on headless system user mode - should be used only by
     * testing infra.
     *
     * @hide
     */
    @TestApi
    @RequiresPermission(android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS)
    public boolean setDeviceOwner(
    public boolean setDeviceOwnerOnly(
            @NonNull ComponentName who, @Nullable String ownerName, @UserIdInt int userId) {
        if (mService != null) {
            try {
                return mService.setDeviceOwner(who, ownerName, userId);
                return mService.setDeviceOwner(who, ownerName, userId,
                        /* setProfileOwnerOnCurrentUserIfNecessary= */ false);
            } catch (RemoteException re) {
                throw re.rethrowFromSystemServer();
            }
+1 −1
Original line number Diff line number Diff line
@@ -159,7 +159,7 @@ interface IDevicePolicyManager {
    void reportKeyguardDismissed(int userHandle);
    void reportKeyguardSecured(int userHandle);

    boolean setDeviceOwner(in ComponentName who, String ownerName, int userId);
    boolean setDeviceOwner(in ComponentName who, String ownerName, int userId, boolean setProfileOwnerOnCurrentUserIfNecessary);
    ComponentName getDeviceOwnerComponent(boolean callingUserOnly);
    boolean hasDeviceOwner();
    String getDeviceOwnerName();
+6 −3
Original line number Diff line number Diff line
@@ -8353,7 +8353,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    @Override
    public boolean setDeviceOwner(ComponentName admin, String ownerName, int userId) {
    public boolean setDeviceOwner(ComponentName admin, String ownerName, int userId,
            boolean setProfileOwnerOnCurrentUserIfNecessary) {
        if (!mHasFeature) {
            logMissingFeatureAction("Cannot set " + ComponentName.flattenToShortString(admin)
                    + " as device owner for user " + userId);
@@ -8417,7 +8418,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
            Slogf.i(LOG_TAG, "Device owner set: %s on user %d", admin.flattenToShortString(),
                    userId);
            if (mInjector.userManagerIsHeadlessSystemUserMode()) {
            if (setProfileOwnerOnCurrentUserIfNecessary
                    && mInjector.userManagerIsHeadlessSystemUserMode()) {
                int currentForegroundUser = getCurrentForegroundUserId();
                Slogf.i(LOG_TAG, "setDeviceOwner(): setting %s as profile owner on user %d",
                        admin.flattenToShortString(), currentForegroundUser);
@@ -17477,7 +17479,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
        // TODO(b/178187130): Directly set DO and remove the check once silent provisioning is no
        //  longer used.
        if (getDeviceOwnerComponent(/* callingUserOnly= */ true) == null) {
            return setDeviceOwner(adminComponent, name, userId);
            return setDeviceOwner(adminComponent, name, userId,
                    /* setProfileOwnerOnCurrentUserIfNecessary= */ true);
        }
        return true;
    }
+8 −3
Original line number Diff line number Diff line
@@ -46,11 +46,13 @@ final class DevicePolicyManagerServiceShellCommand extends ShellCommand {

    private static final String USER_OPTION = "--user";
    private static final String NAME_OPTION = "--name";
    private static final String DO_ONLY_OPTION = "--device-owner-only";

    private final DevicePolicyManagerService mService;
    private int mUserId = UserHandle.USER_SYSTEM;
    private String mName = "";
    private ComponentName mComponent;
    private boolean mSetDoOnly;

    DevicePolicyManagerServiceShellCommand(DevicePolicyManagerService service) {
        mService = Objects.requireNonNull(service);
@@ -130,8 +132,8 @@ final class DevicePolicyManagerServiceShellCommand extends ShellCommand {
        pw.printf("  %s [ %s <USER_ID> | current ] <COMPONENT>\n",
                CMD_SET_ACTIVE_ADMIN, USER_OPTION);
        pw.printf("    Sets the given component as active admin for an existing user.\n\n");
        pw.printf("  %s [ %s <USER_ID> | current *EXPERIMENTAL* ] [ %s <NAME> ] "
                + "<COMPONENT>\n", CMD_SET_DEVICE_OWNER, USER_OPTION, NAME_OPTION);
        pw.printf("  %s [ %s <USER_ID> | current *EXPERIMENTAL* ] [ %s <NAME> ] [ %s ]"
                + "<COMPONENT>\n", CMD_SET_DEVICE_OWNER, USER_OPTION, NAME_OPTION, DO_ONLY_OPTION);
        pw.printf("    Sets the given component as active admin, and its package as device owner."
                + "\n\n");
        pw.printf("  %s [ %s <USER_ID> | current ] [ %s <NAME> ] <COMPONENT>\n",
@@ -254,7 +256,8 @@ final class DevicePolicyManagerServiceShellCommand extends ShellCommand {
        mService.setActiveAdmin(mComponent, /* refreshing= */ true, mUserId);

        try {
            if (!mService.setDeviceOwner(mComponent, mName, mUserId)) {
            if (!mService.setDeviceOwner(mComponent, mName, mUserId,
                    /* setProfileOwnerOnCurrentUserIfNecessary= */ !mSetDoOnly)) {
                throw new RuntimeException(
                        "Can't set package " + mComponent + " as device owner.");
            }
@@ -351,6 +354,8 @@ final class DevicePolicyManagerServiceShellCommand extends ShellCommand {
                if (mUserId == UserHandle.USER_CURRENT) {
                    mUserId = ActivityManager.getCurrentUser();
                }
            } else if (DO_ONLY_OPTION.equals(opt)) {
                mSetDoOnly = true;
            } else if (canHaveName && NAME_OPTION.equals(opt)) {
                mName = getNextArgRequired();
            } else {