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

Commit 2149d18f authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "Add policy transparency dialog to 'Remove work profile'" into rvc-dev...

Merge "Add policy transparency dialog to 'Remove work profile'" into rvc-dev am: cf470fab am: b5191ad9

Change-Id: I110603e8a64b7dea0bf49728f7fb3df460598207
parents 6757a04c b5191ad9
Loading
Loading
Loading
Loading
+50 −1
Original line number Diff line number Diff line
@@ -15,8 +15,17 @@
 */
package com.android.settings.accounts;

import static android.os.UserManager.DISALLOW_REMOVE_MANAGED_PROFILE;

import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

import android.annotation.UserIdInt;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;

import com.android.settings.AccessiblePreferenceCategory;
import com.android.settingslib.RestrictedLockUtilsInternal;
@@ -44,7 +53,12 @@ public class AccountRestrictionHelper {
            return;
        }
        if (hasBaseUserRestriction(userRestriction, userId)) {
            if (userRestriction.equals(DISALLOW_REMOVE_MANAGED_PROFILE)
                    && isOrganizationOwnedDevice()) {
                preference.setDisabledByAdmin(getEnforcedAdmin(userRestriction, userId));
            } else {
                preference.setEnabled(false);
            }
        } else {
            preference.checkRestrictionAndSetDisabled(userRestriction, userId);
        }
@@ -55,6 +69,41 @@ public class AccountRestrictionHelper {
                userId);
    }

    private boolean isOrganizationOwnedDevice() {
        final DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
                Context.DEVICE_POLICY_SERVICE);
        if (dpm == null) {
            return false;
        }
        return dpm.isOrganizationOwnedDeviceWithManagedProfile();
    }

    private EnforcedAdmin getEnforcedAdmin(String userRestriction, int userId) {
        final DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
                Context.DEVICE_POLICY_SERVICE);
        if (dpm == null) {
            return null;
        }
        final int managedUsedId = getManagedUserId(userId);
        ComponentName adminComponent = dpm.getProfileOwnerAsUser(managedUsedId);
        if (adminComponent != null) {
            return new EnforcedAdmin(adminComponent, userRestriction,
                    UserHandle.of(managedUsedId));
        }
        return null;
    }

    private int getManagedUserId(int userId) {
        final UserManager um = UserManager.get(mContext);
        for (UserInfo ui : um.getProfiles(userId)) {
            if (ui.id == userId || !ui.isManagedProfile()) {
                continue;
            }
            return ui.id;
        }
        return -1;
    }

    public AccessiblePreferenceCategory createAccessiblePreferenceCategory(Context context) {
        return new AccessiblePreferenceCategory(context);
    }