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

Commit 56e82784 authored by Alex Johnston's avatar Alex Johnston
Browse files

Add policy transparency dialog to 'Remove work profile'

* In New COPE mode, no explanation was being shown to the
  user as to why the work profile cannot be removed.
* This CL adds a policy transparency dialog when the
  'Remove work profile' preference is selected.
* This was done by modifying AccountRestrictionHelper to
  set the preference to disabled by admin if the device is
  in New COPE mode and the base restriction
  DISALLOW_REMOVE_MANAGED_PROFILE is enforced.

Bug: 149391073
Test: Manual testing
Change-Id: Iec0f57b988e1d4fd08bca040abd1bb30b6991507
parent 149508de
Loading
Loading
Loading
Loading
+54 −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 (isDisallowRemoveManagedProfileRestriction(userRestriction)
                    && isOrganizationOwnedDevice()) {
                preference.setDisabledByAdmin(getEnforcedAdmin(userRestriction, userId));
            } else {
                preference.setEnabled(false);
            }
        } else {
            preference.checkRestrictionAndSetDisabled(userRestriction, userId);
        }
@@ -55,6 +69,45 @@ public class AccountRestrictionHelper {
                userId);
    }

    private boolean isDisallowRemoveManagedProfileRestriction(String userRestriction) {
        return userRestriction.equals(DISALLOW_REMOVE_MANAGED_PROFILE);
    }

    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);
    }