• 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.getApplicationIcon(mdmApplicationInfo);
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     /**
142      * Creates a dialog to confirm that the user is ok to start setting up a new user.
143      *
144      * @param onConfirmListener Callback object for positive action
145      */
createSetupUserDialog(Context context, DialogInterface.OnClickListener onConfirmListener)146     public static Dialog createSetupUserDialog(Context context,
147             DialogInterface.OnClickListener onConfirmListener) {
148         return new AlertDialog.Builder(context)
149                 .setTitle(com.android.settingslib.R.string.user_setup_dialog_title)
150                 .setMessage(com.android.settingslib.R.string.user_setup_dialog_message)
151                 .setPositiveButton(com.android.settingslib.R.string.user_setup_button_setup_now,
152                         onConfirmListener)
153                 .setNegativeButton(com.android.settingslib.R.string.user_setup_button_setup_later,
154                         null)
155                 .create();
156     }
157 
158     /**
159      * Creates a dialog to confirm with the user if it's ok to reset the guest user, which will
160      * delete all the guest user's data.
161      *
162      * @param context a Context object
163      * @param onConfirmListener Callback object for positive action
164      * @return the created Dialog
165      */
createResetGuestDialog(Context context, DialogInterface.OnClickListener onConfirmListener)166     public static Dialog createResetGuestDialog(Context context,
167             DialogInterface.OnClickListener onConfirmListener) {
168         return new AlertDialog.Builder(context)
169                 .setTitle(com.android.settingslib.R.string.guest_reset_guest_dialog_title)
170                 .setMessage(R.string.user_exit_guest_confirm_message)
171                 .setPositiveButton(
172                         com.android.settingslib.R.string.guest_reset_guest_confirm_button,
173                         onConfirmListener)
174                 .setNegativeButton(android.R.string.cancel, null)
175                 .create();
176     }
177 }
178