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
Merged-In: Iec0f57b988e1d4fd08bca040abd1bb30b6991507
(cherry picked from commit 56e8278498c7ede8fad8bb5e0b781d34c425995f)
diff --git a/src/com/android/settings/accounts/AccountRestrictionHelper.java b/src/com/android/settings/accounts/AccountRestrictionHelper.java
index 05b27aa..c67f5d1 100644
--- a/src/com/android/settings/accounts/AccountRestrictionHelper.java
+++ b/src/com/android/settings/accounts/AccountRestrictionHelper.java
@@ -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 @@
             return;
         }
         if (hasBaseUserRestriction(userRestriction, userId)) {
-            preference.setEnabled(false);
+            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 @@
                 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);
     }