1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.settings.users;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.UserInfo;
26 import android.graphics.drawable.Drawable;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.widget.ImageView;
32 import android.widget.TextView;
33 
34 import com.android.settings.R;
35 import com.android.settings.Utils;
36 
37 /**
38  * Helper class for displaying dialogs related to user settings.
39  */
40 public final class UserDialogs {
41 
42     /**
43      * Creates a dialog to confirm with the user if it's ok to remove the user
44      * and delete all the data.
45      *
46      * @param context a Context object
47      * @param removingUserId The userId of the user to remove
48      * @param onConfirmListener Callback object for positive action
49      * @return the created Dialog
50      */
createRemoveDialog(Context context, int removingUserId, DialogInterface.OnClickListener onConfirmListener)51     public static Dialog createRemoveDialog(Context context, int removingUserId,
52             DialogInterface.OnClickListener onConfirmListener) {
53         UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
54         UserInfo userInfo = um.getUserInfo(removingUserId);
55         AlertDialog.Builder builder = new AlertDialog.Builder(context)
56                 .setPositiveButton(R.string.user_delete_button, onConfirmListener)
57                 .setNegativeButton(android.R.string.cancel, null);
58         if (userInfo.isManagedProfile()) {
59             builder.setTitle(R.string.work_profile_confirm_remove_title);
60             View view = createRemoveManagedUserDialogView(context, removingUserId);
61             if (view != null) {
62                 builder.setView(view);
63             } else {
64                 builder.setMessage(R.string.work_profile_confirm_remove_message);
65             }
66         } else if (UserHandle.myUserId() == removingUserId) {
67             builder.setTitle(R.string.user_confirm_remove_self_title);
68             builder.setMessage(R.string.user_confirm_remove_self_message);
69         } else if (userInfo.isRestricted()) {
70             builder.setTitle(R.string.user_profile_confirm_remove_title);
71             builder.setMessage(R.string.user_profile_confirm_remove_message);
72         } else {
73             builder.setTitle(R.string.user_confirm_remove_title);
74             builder.setMessage(R.string.user_confirm_remove_message);
75         }
76         return builder.create();
77     }
78 
79     /**
80      * Creates a view to be used in the confirmation dialog for removing work profile.
81      */
createRemoveManagedUserDialogView(Context context, int userId)82     private static View createRemoveManagedUserDialogView(Context context, int userId) {
83         PackageManager packageManager = context.getPackageManager();
84         ApplicationInfo mdmApplicationInfo = Utils.getAdminApplicationInfo(context, userId);
85         if (mdmApplicationInfo == null) {
86             return null;
87         }
88         LayoutInflater inflater =
89                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
90 
91         View view = inflater.inflate(R.layout.delete_managed_profile_dialog, null);
92         ImageView imageView =
93                 (ImageView) view.findViewById(R.id.delete_managed_profile_mdm_icon_view);
94         Drawable badgedApplicationIcon = packageManager.getUserBadgedIcon(
95                 packageManager.getApplicationIcon(mdmApplicationInfo), new UserHandle(userId));
96         imageView.setImageDrawable(badgedApplicationIcon);
97 
98         CharSequence appLabel = packageManager.getApplicationLabel(mdmApplicationInfo);
99         CharSequence badgedAppLabel = packageManager.getUserBadgedLabel(appLabel,
100                 new UserHandle(userId));
101         TextView textView =
102                 (TextView) view.findViewById(R.id.delete_managed_profile_device_manager_name);
103         textView.setText(appLabel);
104         if (!appLabel.toString().contentEquals(badgedAppLabel)) {
105             textView.setContentDescription(badgedAppLabel);
106         }
107 
108         return view;
109     }
110 
111     /**
112      * Creates a dialog to confirm that the user is ok to enable phone calls and SMS.
113      *
114      * @param onConfirmListener Callback object for positive action
115      */
createEnablePhoneCallsAndSmsDialog(Context context, DialogInterface.OnClickListener onConfirmListener)116     public static Dialog createEnablePhoneCallsAndSmsDialog(Context context,
117             DialogInterface.OnClickListener onConfirmListener) {
118         return new AlertDialog.Builder(context)
119                 .setTitle(R.string.user_enable_calling_and_sms_confirm_title)
120                 .setMessage(R.string.user_enable_calling_and_sms_confirm_message)
121                 .setPositiveButton(R.string.okay, onConfirmListener)
122                 .setNegativeButton(android.R.string.cancel, null)
123                 .create();
124     }
125 
126     /**
127      * Creates a dialog to confirm that the user is ok to enable phone calls (no SMS).
128      *
129      * @param onConfirmListener Callback object for positive action
130      */
createEnablePhoneCallsDialog(Context context, DialogInterface.OnClickListener onConfirmListener)131     public static Dialog createEnablePhoneCallsDialog(Context context,
132             DialogInterface.OnClickListener onConfirmListener) {
133         return new AlertDialog.Builder(context)
134                 .setTitle(R.string.user_enable_calling_confirm_title)
135                 .setMessage(R.string.user_enable_calling_confirm_message)
136                 .setPositiveButton(R.string.okay, onConfirmListener)
137                 .setNegativeButton(android.R.string.cancel, null)
138                 .create();
139     }
140 }
141