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 static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_CONFIRM_REMOVE_MESSAGE; 20 import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_CONFIRM_REMOVE_TITLE; 21 import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_MANAGED_BY; 22 23 import android.app.Dialog; 24 import android.app.admin.DevicePolicyManager; 25 import android.content.Context; 26 import android.content.DialogInterface; 27 import android.content.pm.ApplicationInfo; 28 import android.content.pm.PackageManager; 29 import android.content.pm.UserInfo; 30 import android.graphics.drawable.Drawable; 31 import android.os.UserHandle; 32 import android.os.UserManager; 33 import android.view.LayoutInflater; 34 import android.view.View; 35 import android.widget.ImageView; 36 import android.widget.TextView; 37 38 import androidx.appcompat.app.AlertDialog; 39 40 import com.android.settings.R; 41 import com.android.settings.Utils; 42 43 /** 44 * Helper class for displaying dialogs related to user settings. 45 */ 46 public final class UserDialogs { 47 48 /** 49 * Creates a dialog to confirm with the user if it's ok to remove the user 50 * and delete all the data. 51 * 52 * @param context a Context object 53 * @param removingUserId The userId of the user to remove 54 * @param onConfirmListener Callback object for positive action 55 * @return the created Dialog 56 */ createRemoveDialog(Context context, int removingUserId, DialogInterface.OnClickListener onConfirmListener)57 public static Dialog createRemoveDialog(Context context, int removingUserId, 58 DialogInterface.OnClickListener onConfirmListener) { 59 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); 60 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); 61 UserInfo userInfo = um.getUserInfo(removingUserId); 62 AlertDialog.Builder builder = new AlertDialog.Builder(context) 63 .setPositiveButton(R.string.user_delete_button, onConfirmListener) 64 .setNegativeButton(android.R.string.cancel, null); 65 if (userInfo.isManagedProfile()) { 66 builder.setTitle(dpm.getResources().getString(WORK_PROFILE_CONFIRM_REMOVE_TITLE, 67 () -> context.getString(R.string.work_profile_confirm_remove_title))); 68 View view = createRemoveManagedUserDialogView(context, removingUserId); 69 if (view != null) { 70 builder.setView(view); 71 } else { 72 builder.setMessage(dpm.getResources().getString(WORK_PROFILE_CONFIRM_REMOVE_MESSAGE, 73 () -> context.getString(R.string.work_profile_confirm_remove_message))); 74 } 75 } else if (UserHandle.myUserId() == removingUserId) { 76 builder.setTitle(R.string.user_confirm_remove_self_title); 77 builder.setMessage(R.string.user_confirm_remove_self_message); 78 } else if (userInfo.isRestricted()) { 79 builder.setTitle(R.string.user_profile_confirm_remove_title); 80 builder.setMessage(R.string.user_profile_confirm_remove_message); 81 } else { 82 builder.setTitle(R.string.user_confirm_remove_title); 83 builder.setMessage(R.string.user_confirm_remove_message); 84 } 85 return builder.create(); 86 } 87 88 /** 89 * Creates a view to be used in the confirmation dialog for removing work profile. 90 */ createRemoveManagedUserDialogView(Context context, int userId)91 private static View createRemoveManagedUserDialogView(Context context, int userId) { 92 PackageManager packageManager = context.getPackageManager(); 93 DevicePolicyManager devicePolicyManager = 94 context.getSystemService(DevicePolicyManager.class); 95 ApplicationInfo mdmApplicationInfo = Utils.getAdminApplicationInfo(context, userId); 96 if (mdmApplicationInfo == null) { 97 return null; 98 } 99 LayoutInflater inflater = 100 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 101 102 View view = inflater.inflate(R.layout.delete_managed_profile_dialog, null); 103 ImageView imageView = 104 (ImageView) view.findViewById(R.id.delete_managed_profile_mdm_icon_view); 105 Drawable badgedApplicationIcon = packageManager.getApplicationIcon(mdmApplicationInfo); 106 imageView.setImageDrawable(badgedApplicationIcon); 107 108 TextView openingParagraph = (TextView) 109 view.findViewById(R.id.delete_managed_profile_opening_paragraph); 110 openingParagraph.setText(devicePolicyManager.getResources().getString( 111 WORK_PROFILE_MANAGED_BY, 112 () -> context.getString( 113 R.string.opening_paragraph_delete_profile_unknown_company))); 114 TextView closingParagraph = (TextView) 115 view.findViewById(R.id.delete_managed_profile_closing_paragraph); 116 closingParagraph.setText(devicePolicyManager.getResources().getString( 117 WORK_PROFILE_CONFIRM_REMOVE_MESSAGE, 118 () -> context.getString(R.string.work_profile_confirm_remove_message))); 119 120 CharSequence appLabel = packageManager.getApplicationLabel(mdmApplicationInfo); 121 CharSequence badgedAppLabel = packageManager.getUserBadgedLabel(appLabel, 122 new UserHandle(userId)); 123 TextView textView = 124 (TextView) view.findViewById(R.id.delete_managed_profile_device_manager_name); 125 textView.setText(appLabel); 126 if (!appLabel.toString().contentEquals(badgedAppLabel)) { 127 textView.setContentDescription(badgedAppLabel); 128 } 129 130 return view; 131 } 132 133 /** 134 * Creates a dialog to confirm that the user is ok to enable phone calls and SMS. 135 * 136 * @param onConfirmListener Callback object for positive action 137 */ createEnablePhoneCallsAndSmsDialog(Context context, DialogInterface.OnClickListener onConfirmListener)138 public static Dialog createEnablePhoneCallsAndSmsDialog(Context context, 139 DialogInterface.OnClickListener onConfirmListener) { 140 return new AlertDialog.Builder(context) 141 .setTitle(R.string.user_enable_calling_and_sms_confirm_title) 142 .setMessage(R.string.user_enable_calling_and_sms_confirm_message) 143 .setPositiveButton(R.string.okay, onConfirmListener) 144 .setNegativeButton(android.R.string.cancel, null) 145 .create(); 146 } 147 148 /** 149 * Creates a dialog to confirm that the user is ok to start setting up a new user. 150 * 151 * @param onConfirmListener Callback object for positive action 152 */ createSetupUserDialog(Context context, DialogInterface.OnClickListener onConfirmListener)153 public static Dialog createSetupUserDialog(Context context, 154 DialogInterface.OnClickListener onConfirmListener) { 155 return new AlertDialog.Builder(context) 156 .setTitle(com.android.settingslib.R.string.user_setup_dialog_title) 157 .setMessage(com.android.settingslib.R.string.user_setup_dialog_message) 158 .setPositiveButton(com.android.settingslib.R.string.user_setup_button_setup_now, 159 onConfirmListener) 160 .setNegativeButton(com.android.settingslib.R.string.user_setup_button_setup_later, 161 null) 162 .create(); 163 } 164 165 /** 166 * Creates a dialog to confirm with the user if it's ok to reset the guest user, which will 167 * delete all the guest user's data. 168 * 169 * @param context a Context object 170 * @param onConfirmListener Callback object for positive action 171 * @return the created Dialog 172 */ createResetGuestDialog(Context context, DialogInterface.OnClickListener onConfirmListener)173 public static Dialog createResetGuestDialog(Context context, 174 DialogInterface.OnClickListener onConfirmListener) { 175 return new AlertDialog.Builder(context) 176 .setTitle(com.android.settingslib.R.string.guest_reset_guest_dialog_title) 177 .setMessage(com.android.settingslib.R.string.guest_exit_dialog_message) 178 .setPositiveButton( 179 com.android.settingslib.R.string.guest_reset_guest_confirm_button, 180 onConfirmListener) 181 .setNegativeButton(android.R.string.cancel, null) 182 .create(); 183 } 184 185 186 /** 187 * Creates a dialog to confirm with the user if it's ok to remove the guest user, which will 188 * delete all the guest user's data. 189 * 190 * @param context a Context object 191 * @param onConfirmListener Callback object for positive action 192 * @return the created Dialog 193 */ createRemoveGuestDialog(Context context, DialogInterface.OnClickListener onConfirmListener)194 public static Dialog createRemoveGuestDialog(Context context, 195 DialogInterface.OnClickListener onConfirmListener) { 196 return new AlertDialog.Builder(context) 197 .setTitle(com.android.settingslib.R.string.guest_remove_guest_dialog_title) 198 .setMessage(R.string.user_exit_guest_confirm_message) 199 .setPositiveButton( 200 com.android.settingslib.R.string.guest_remove_guest_confirm_button, 201 onConfirmListener) 202 .setNegativeButton(android.R.string.cancel, null) 203 .create(); 204 } 205 } 206