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

Commit 92b32639 authored by Felipe Leme's avatar Felipe Leme Committed by Android (Google) Code Review
Browse files

Merge "Added managed profile owners on 'cmd device_policy list-owners'" into sc-v2-dev

parents e2942cb6 9ff2a43a
Loading
Loading
Loading
Loading
+23 −4
Original line number Diff line number Diff line
@@ -327,7 +327,6 @@ import com.android.server.PersistentDataBlockManagerInternal;
import com.android.server.SystemServerInitThreadPool;
import com.android.server.SystemService;
import com.android.server.devicepolicy.ActiveAdmin.TrustAgentInfo;
import com.android.server.devicepolicy.Owners.OwnerDto;
import com.android.server.inputmethod.InputMethodManagerInternal;
import com.android.server.net.NetworkPolicyManagerInternal;
import com.android.server.pm.RestrictionsSet;
@@ -1259,17 +1258,37 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    // Used by DevicePolicyManagerServiceShellCommand
    List<OwnerDto> listAllOwners() {
    List<OwnerShellData> listAllOwners() {
        Preconditions.checkCallAuthorization(
                hasCallingOrSelfPermission(permission.MANAGE_DEVICE_ADMINS));
        return mInjector.binderWithCleanCallingIdentity(() -> {
            List<OwnerDto> owners = mOwners.listAllOwners();
            SparseArray<DevicePolicyData> userData;
            // Gets the owners of "full users" first (device owner and profile owners)
            List<OwnerShellData> owners = mOwners.listAllOwners();
            synchronized (getLockObject()) {
                for (int i = 0; i < owners.size(); i++) {
                    OwnerDto owner = owners.get(i);
                    OwnerShellData owner = owners.get(i);
                    owner.isAffiliated = isUserAffiliatedWithDeviceLocked(owner.userId);
                }
                userData = mUserData;
            }
            // Then the owners of profile users (managed profiles)
            for (int i = 0; i < userData.size(); i++) {
                DevicePolicyData policyData = mUserData.valueAt(i);
                int userId = userData.keyAt(i);
                int parentUserId = mUserManagerInternal.getProfileParentId(userId);
                boolean isProfile = parentUserId != userId;
                if (!isProfile) continue;
                for (int j = 0; j < policyData.mAdminList.size(); j++) {
                    ActiveAdmin admin = policyData.mAdminList.get(j);
                    OwnerShellData owner = OwnerShellData.forManagedProfileOwner(userId,
                            parentUserId, admin.info.getComponent());
                    owners.add(owner);
                }
            }
            return owners;
        });
    }
+5 −4
Original line number Diff line number Diff line
@@ -22,8 +22,6 @@ import android.os.ShellCommand;
import android.os.SystemClock;
import android.os.UserHandle;

import com.android.server.devicepolicy.Owners.OwnerDto;

import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
@@ -205,12 +203,12 @@ final class DevicePolicyManagerServiceShellCommand extends ShellCommand {
    }

    private int runListOwners(PrintWriter pw) {
        List<OwnerDto> owners = mService.listAllOwners();
        List<OwnerShellData> owners = mService.listAllOwners();
        int size = printAndGetSize(pw, owners, "owner");
        if (size == 0) return 0;

        for (int i = 0; i < size; i++) {
            OwnerDto owner = owners.get(i);
            OwnerShellData owner = owners.get(i);
            pw.printf("User %2d: admin=%s", owner.userId, owner.admin.flattenToShortString());
            if (owner.isDeviceOwner) {
                pw.print(",DeviceOwner");
@@ -218,6 +216,9 @@ final class DevicePolicyManagerServiceShellCommand extends ShellCommand {
            if (owner.isProfileOwner) {
                pw.print(",ProfileOwner");
            }
            if (owner.isManagedProfileOwner) {
                pw.printf(",ManagedProfileOwner(parentUserId=%d)", owner.parentUserId);
            }
            if (owner.isAffiliated) {
                pw.print(",Affiliated");
            }
+98 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.server.devicepolicy;

import static android.os.UserHandle.USER_NULL;

import android.annotation.UserIdInt;
import android.content.ComponentName;

import com.android.internal.util.Preconditions;

import java.util.Objects;

/**
 * Data-transfer object used by {@link DevicePolicyManagerServiceShellCommand}.
 */
final class OwnerShellData {

    public final @UserIdInt int userId;
    public final @UserIdInt int parentUserId;
    public final ComponentName admin;
    public final boolean isDeviceOwner;
    public final boolean isProfileOwner;
    public final boolean isManagedProfileOwner;
    public boolean isAffiliated;

    // NOTE: class is too simple to require a Builder (not to mention isAffiliated is mutable)
    private OwnerShellData(@UserIdInt int userId, @UserIdInt int parentUserId, ComponentName admin,
            boolean isDeviceOwner, boolean isProfileOwner, boolean isManagedProfileOwner) {
        Preconditions.checkArgument(userId != USER_NULL, "userId cannot be USER_NULL");
        this.userId = userId;
        this.parentUserId = parentUserId;
        this.admin = Objects.requireNonNull(admin, "admin must not be null");
        this.isDeviceOwner = isDeviceOwner;
        this.isProfileOwner = isProfileOwner;
        this.isManagedProfileOwner = isManagedProfileOwner;
        if (isManagedProfileOwner) {
            Preconditions.checkArgument(parentUserId != USER_NULL,
                    "parentUserId cannot be USER_NULL for managed profile owner");
            Preconditions.checkArgument(parentUserId != userId,
                    "cannot be parent of itself (%d)", userId);
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(getClass().getSimpleName())
                .append("[userId=").append(userId)
                .append(",admin=").append(admin.flattenToShortString());
        if (isDeviceOwner) {
            sb.append(",deviceOwner");
        }
        if (isProfileOwner) {
            sb.append(",isProfileOwner");
        }
        if (isManagedProfileOwner) {
            sb.append(",isManagedProfileOwner");
        }
        if (parentUserId != USER_NULL) {
            sb.append(",parentUserId=").append(parentUserId);
        }
        if (isAffiliated) {
            sb.append(",isAffiliated");
        }
        return sb.append(']').toString();
    }

    static OwnerShellData forDeviceOwner(@UserIdInt int userId, ComponentName admin) {
        return new OwnerShellData(userId, /* parentUserId= */ USER_NULL, admin,
                /* isDeviceOwner= */ true, /* isProfileOwner= */ false,
                /* isManagedProfileOwner= */ false);
    }

    static OwnerShellData forUserProfileOwner(@UserIdInt int userId, ComponentName admin) {
        return new OwnerShellData(userId, /* parentUserId= */ USER_NULL, admin,
                /* isDeviceOwner= */ false, /* isProfileOwner= */ true,
                /* isManagedProfileOwner= */ false);
    }

    static OwnerShellData forManagedProfileOwner(@UserIdInt int userId, @UserIdInt int parentUserId,
            ComponentName admin) {
        return new OwnerShellData(userId, parentUserId, admin, /* isDeviceOwner= */ false,
                /* isProfileOwner= */ false, /* isManagedProfileOwner= */ true);
    }
}
+4 −24
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.server.devicepolicy;
import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_DEFAULT;

import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ActivityManagerInternal;
import android.app.AppOpsManagerInternal;
import android.app.admin.DevicePolicyManager.DeviceOwnerType;
@@ -476,17 +475,16 @@ class Owners {
        }
    }

    List<OwnerDto> listAllOwners() {
        List<OwnerDto> owners = new ArrayList<>();
    List<OwnerShellData> listAllOwners() {
        List<OwnerShellData> owners = new ArrayList<>();
        synchronized (mLock) {
            if (mDeviceOwner != null) {
                owners.add(new OwnerDto(mDeviceOwnerUserId, mDeviceOwner.admin,
                        /* isDeviceOwner= */ true));
                owners.add(OwnerShellData.forDeviceOwner(mDeviceOwnerUserId, mDeviceOwner.admin));
            }
            for (int i = 0; i < mProfileOwners.size(); i++) {
                int userId = mProfileOwners.keyAt(i);
                OwnerInfo info = mProfileOwners.valueAt(i);
                owners.add(new OwnerDto(userId, info.admin, /* isDeviceOwner= */ false));
                owners.add(OwnerShellData.forUserProfileOwner(userId, info.admin));
            }
        }
        return owners;
@@ -1236,24 +1234,6 @@ class Owners {
        }
    }

    /**
     * Data-transfer object used by {@link DevicePolicyManagerServiceShellCommand}.
     */
    static final class OwnerDto {
        public final @UserIdInt int userId;
        public final ComponentName admin;
        public final boolean isDeviceOwner;
        public final boolean isProfileOwner;
        public boolean isAffiliated;

        private OwnerDto(@UserIdInt int userId, ComponentName admin, boolean isDeviceOwner) {
            this.userId = userId;
            this.admin = Objects.requireNonNull(admin, "admin must not be null");
            this.isDeviceOwner = isDeviceOwner;
            this.isProfileOwner = !isDeviceOwner;
        }
    }

    public void dump(IndentingPrintWriter pw) {
        boolean needBlank = false;
        if (mDeviceOwner != null) {
+1 −1
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ import org.mockito.MockitoSession;
import org.mockito.quality.Strictness;

/**
 * Run it as {@code atest FrameworksMockingCoreTests:FactoryResetterTest}
 * Run it as {@code atest FrameworksMockingServicesTests:FactoryResetterTest}
 */
@Presubmit
public final class FactoryResetterTest {
Loading