• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Dialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.UserInfo;
25 import android.graphics.drawable.Drawable;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.widget.ImageView;
31 import android.widget.TextView;
32 
33 import androidx.appcompat.app.AlertDialog;
34 
35 import com.android.settings.R;
36 import com.android.settings.Utils;
37 
38 /**
39  * Helper class for displaying dialogs related to user settings.
40  */
41 public final class UserDialogs {
42 
43     /**
44      * Creates a dialog to confirm with the user if it's ok to remove the user
45      * and delete all the data.
46      *
47      * @param context a Context object
48      * @param removingUserId The userId of the user to remove
49      * @param onConfirmListener Callback object for positive action
50      * @return the created Dialog
51      */
createRemoveDialog(Context context, int removingUserId, DialogInterface.OnClickListener onConfirmListener)52     public static Dialog createRemoveDialog(Context context, int removingUserId,
53             DialogInterface.OnClickListener onConfirmListener) {
54         UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
55         UserInfo userInfo = um.getUserInfo(removingUserId);
56         AlertDialog.Builder builder = new AlertDialog.Builder(context)
57                 .setPositiveButton(R.string.user_delete_button, onConfirmListener)
58                 .setNegativeButton(android.R.string.cancel, null);
59         if (userInfo.isManagedProfile()) {
60             builder.setTitle(R.string.work_profile_confirm_remove_title);
61             View view = createRemoveManagedUserDialogView(context, removingUserId);
62             if (view != null) {
63                 builder.setView(view);
64             } else {
65                 builder.setMessage(R.string.work_profile_confirm_remove_message);
66             }
67         } else if (UserHandle.myUserId() == removingUserId) {
68             builder.setTitle(R.string.user_confirm_remove_self_title);
69             builder.setMessage(R.string.user_confirm_remove_self_message);
70         } else if (userInfo.isRestricted()) {
71             builder.setTitle(R.string.user_profile_confirm_remove_title);
72             builder.setMessage(R.string.user_profile_confirm_remove_message);
73         } else {
74             builder.setTitle(R.string.user_confirm_remove_title);
75             builder.setMessage(R.string.user_confirm_remove_message);
76         }
77         return builder.create();
78     }
79 
80     /**
81      * Creates a view to be used in the confirmation dialog for removing work profile.
82      */
createRemoveManagedUserDialogView(Context context, int userId)83     private static View createRemoveManagedUserDialogView(Context context, int userId) {
84         PackageManager packageManager = context.getPackageManager();
85         ApplicationInfo mdmApplicationInfo = Utils.getAdminApplicationInfo(context, userId);
86         if (mdmApplicationInfo == null) {
87             return null;
88         }
89         LayoutInflater inflater =
90                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
91 
92         View view = inflater.inflate(R.layout.delete_managed_profile_dialog, null);
93         ImageView imageView =
94                 (ImageView) view.findViewById(R.id.delete_managed_profile_mdm_icon_view);
95         Drawable badgedApplicationIcon = packageManager.getUserBadgedIcon(
96                 packageManager.getApplicationIcon(mdmApplicationInfo), new UserHandle(userId));
97         imageView.setImageDrawable(badgedApplicationIcon);
98 
99         CharSequence appLabel = packageManager.getApplicationLabel(mdmApplicationInfo);
100         CharSequence badgedAppLabel = packageManager.getUserBadgedLabel(appLabel,
101                 new UserHandle(userId));
102         TextView textView =
103                 (TextView) view.findViewById(R.id.delete_managed_profile_device_manager_name);
104         textView.setText(appLabel);
105         if (!appLabel.toString().contentEquals(badgedAppLabel)) {
106             textView.setContentDescription(badgedAppLabel);
107         }
108 
109         return view;
110     }
111 
112     /**
113      * Creates a dialog to confirm that the user is ok to enable phone calls and SMS.
114      *
115      * @param onConfirmListener Callback object for positive action
116      */
createEnablePhoneCallsAndSmsDialog(Context context, DialogInterface.OnClickListener onConfirmListener)117     public static Dialog createEnablePhoneCallsAndSmsDialog(Context context,
118             DialogInterface.OnClickListener onConfirmListener) {
119         return new AlertDialog.Builder(context)
120                 .setTitle(R.string.user_enable_calling_and_sms_confirm_title)
121                 .setMessage(R.string.user_enable_calling_and_sms_confirm_message)
122                 .setPositiveButton(R.string.okay, onConfirmListener)
123                 .setNegativeButton(android.R.string.cancel, null)
124                 .create();
125     }
126 
127     /**
128      * Creates a dialog to confirm that the user is ok to enable phone calls (no SMS).
129      *
130      * @param onConfirmListener Callback object for positive action
131      */
createEnablePhoneCallsDialog(Context context, DialogInterface.OnClickListener onConfirmListener)132     public static Dialog createEnablePhoneCallsDialog(Context context,
133             DialogInterface.OnClickListener onConfirmListener) {
134         return new AlertDialog.Builder(context)
135                 .setTitle(R.string.user_enable_calling_confirm_title)
136                 .setMessage(R.string.user_enable_calling_confirm_message)
137                 .setPositiveButton(R.string.okay, onConfirmListener)
138                 .setNegativeButton(android.R.string.cancel, null)
139                 .create();
140     }
141 }
142