Update warning dialog on work profile deletion.

Bug: 20024761
Change-Id: I23b0eaa7742d9d23a29a2a48ab6e3d92fe310560
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index d05160c..4ddba80 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -22,9 +22,11 @@
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
 import android.app.AlertDialog;
+import android.app.AppGlobals;
 import android.app.Dialog;
 import android.app.Fragment;
 import android.app.IActivityManager;
+import android.app.admin.DevicePolicyManager;
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -32,6 +34,7 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.IPackageManager;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
@@ -1153,4 +1156,27 @@
             view.setVisibility(shown ? View.VISIBLE : View.INVISIBLE);
         }
     }
-}
+
+    /**
+     * Returns the application info of the currently installed MDM package.
+     */
+    public static ApplicationInfo getAdminApplicationInfo(Context context, int profileId) {
+        DevicePolicyManager dpm =
+                (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
+        ComponentName mdmPackage = dpm.getProfileOwnerAsUser(profileId);
+        if (mdmPackage == null) {
+            return null;
+        }
+        String mdmPackageName = mdmPackage.getPackageName();
+        try {
+            IPackageManager ipm = AppGlobals.getPackageManager();
+            ApplicationInfo mdmApplicationInfo =
+                    ipm.getApplicationInfo(mdmPackageName, 0, profileId);
+            return mdmApplicationInfo;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error while retrieving application info for package " + mdmPackageName
+                    + ", userId " + profileId, e);
+            return null;
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/users/UserDialogs.java b/src/com/android/settings/users/UserDialogs.java
index 2d92464..23012b3 100644
--- a/src/com/android/settings/users/UserDialogs.java
+++ b/src/com/android/settings/users/UserDialogs.java
@@ -20,11 +20,19 @@
 import android.app.Dialog;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.content.pm.UserInfo;
+import android.graphics.drawable.Drawable;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.ImageView;
+import android.widget.TextView;
 
 import com.android.settings.R;
+import com.android.settings.Utils;
 
 /**
  * Helper class for displaying dialogs related to user settings.
@@ -44,27 +52,60 @@
             DialogInterface.OnClickListener onConfirmListener) {
         UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
         UserInfo userInfo = um.getUserInfo(removingUserId);
-        int titleResId;
-        int messageResId;
-        if (UserHandle.myUserId() == removingUserId) {
-            titleResId = R.string.user_confirm_remove_self_title;
-            messageResId = R.string.user_confirm_remove_self_message;
-        } else if (userInfo.isRestricted()) {
-            titleResId = R.string.user_profile_confirm_remove_title;
-            messageResId = R.string.user_profile_confirm_remove_message;
-        } else if (userInfo.isManagedProfile()) {
-            titleResId = R.string.work_profile_confirm_remove_title;
-            messageResId = R.string.work_profile_confirm_remove_message;
-        } else {
-            titleResId = R.string.user_confirm_remove_title;
-            messageResId = R.string.user_confirm_remove_message;
-        }
-        return new AlertDialog.Builder(context)
-                .setTitle(titleResId)
-                .setMessage(messageResId)
+        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                 .setPositiveButton(R.string.user_delete_button, onConfirmListener)
-                .setNegativeButton(android.R.string.cancel, null)
-                .create();
+                .setNegativeButton(android.R.string.cancel, null);
+        if (UserHandle.myUserId() == removingUserId) {
+            builder.setTitle(R.string.user_confirm_remove_self_title);
+            builder.setMessage(R.string.user_confirm_remove_self_message);
+        } else if (userInfo.isRestricted()) {
+            builder.setTitle(R.string.user_profile_confirm_remove_title);
+            builder.setMessage(R.string.user_profile_confirm_remove_message);
+        } else if (userInfo.isManagedProfile()) {
+            builder.setTitle(R.string.work_profile_confirm_remove_title);
+            View view = createRemoveManagedUserDialogView(context, removingUserId);
+            if (view != null) {
+                builder.setView(view);
+            } else {
+                builder.setMessage(R.string.work_profile_confirm_remove_message);
+            }
+        } else {
+            builder.setTitle(R.string.user_confirm_remove_title);
+            builder.setMessage(R.string.user_confirm_remove_message);
+        }
+        return builder.create();
+    }
+
+    /**
+     * Creates a view to be used in the confirmation dialog for removing work profile.
+     */
+    private static View createRemoveManagedUserDialogView(Context context, int userId) {
+        PackageManager packageManager = context.getPackageManager();
+        ApplicationInfo mdmApplicationInfo = Utils.getAdminApplicationInfo(context, userId);
+        if (mdmApplicationInfo == null) {
+            return null;
+        }
+        LayoutInflater inflater =
+                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+
+        View view = inflater.inflate(R.layout.delete_managed_profile_dialog, null);
+        ImageView imageView =
+                (ImageView) view.findViewById(R.id.delete_managed_profile_mdm_icon_view);
+        Drawable badgedApplicationIcon = packageManager.getUserBadgedIcon(
+                packageManager.getApplicationIcon(mdmApplicationInfo), new UserHandle(userId));
+        imageView.setImageDrawable(badgedApplicationIcon);
+
+        CharSequence appLabel = packageManager.getApplicationLabel(mdmApplicationInfo);
+        CharSequence badgedAppLabel = packageManager.getUserBadgedLabel(appLabel,
+                new UserHandle(userId));
+        TextView textView =
+                (TextView) view.findViewById(R.id.delete_managed_profile_device_manager_name);
+        textView.setText(appLabel);
+        if (!appLabel.toString().contentEquals(badgedAppLabel)) {
+            textView.setContentDescription(badgedAppLabel);
+        }
+
+        return view;
     }
 
     /**