1 /* 2 * Copyright (C) 2016 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.packageinstaller.handheld; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.app.DialogFragment; 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.os.Bundle; 27 import android.os.UserManager; 28 29 import com.android.packageinstaller.R; 30 import com.android.packageinstaller.UninstallerActivity; 31 32 public class UninstallAlertDialogFragment extends DialogFragment implements 33 DialogInterface.OnClickListener { 34 35 @Override onCreateDialog(Bundle savedInstanceState)36 public Dialog onCreateDialog(Bundle savedInstanceState) { 37 final PackageManager pm = getActivity().getPackageManager(); 38 final UninstallerActivity.DialogInfo dialogInfo = 39 ((UninstallerActivity) getActivity()).getDialogInfo(); 40 final CharSequence appLabel = dialogInfo.appInfo.loadSafeLabel(pm); 41 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()); 42 StringBuilder messageBuilder = new StringBuilder(); 43 44 // If the Activity label differs from the App label, then make sure the user 45 // knows the Activity belongs to the App being uninstalled. 46 if (dialogInfo.activityInfo != null) { 47 final CharSequence activityLabel = dialogInfo.activityInfo.loadSafeLabel(pm); 48 if (!activityLabel.equals(appLabel)) { 49 messageBuilder.append( 50 getString(R.string.uninstall_activity_text, activityLabel)); 51 messageBuilder.append(" ").append(appLabel).append(".\n\n"); 52 } 53 } 54 55 final boolean isUpdate = 56 ((dialogInfo.appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); 57 UserManager userManager = UserManager.get(getActivity()); 58 if (isUpdate) { 59 if (isSingleUser(userManager)) { 60 messageBuilder.append(getString(R.string.uninstall_update_text)); 61 } else { 62 messageBuilder.append(getString(R.string.uninstall_update_text_multiuser)); 63 } 64 } else { 65 if (dialogInfo.allUsers && !isSingleUser(userManager)) { 66 messageBuilder.append(getString(R.string.uninstall_application_text_all_users)); 67 } else if (!dialogInfo.user.equals(android.os.Process.myUserHandle())) { 68 UserInfo userInfo = userManager.getUserInfo(dialogInfo.user.getIdentifier()); 69 messageBuilder.append( 70 getString(R.string.uninstall_application_text_user, userInfo.name)); 71 } else { 72 messageBuilder.append(getString(R.string.uninstall_application_text)); 73 } 74 } 75 76 dialogBuilder.setTitle(appLabel); 77 dialogBuilder.setIcon(dialogInfo.appInfo.loadIcon(pm)); 78 dialogBuilder.setPositiveButton(android.R.string.ok, this); 79 dialogBuilder.setNegativeButton(android.R.string.cancel, this); 80 dialogBuilder.setMessage(messageBuilder.toString()); 81 return dialogBuilder.create(); 82 } 83 84 @Override onClick(DialogInterface dialog, int which)85 public void onClick(DialogInterface dialog, int which) { 86 if (which == Dialog.BUTTON_POSITIVE) { 87 ((UninstallerActivity) getActivity()).startUninstallProgress(); 88 } else { 89 ((UninstallerActivity) getActivity()).dispatchAborted(); 90 } 91 } 92 93 @Override onDismiss(DialogInterface dialog)94 public void onDismiss(DialogInterface dialog) { 95 super.onDismiss(dialog); 96 if (isAdded()) { 97 getActivity().finish(); 98 } 99 } 100 101 /** 102 * Returns whether there is only one user on this device, not including 103 * the system-only user. 104 */ isSingleUser(UserManager userManager)105 private boolean isSingleUser(UserManager userManager) { 106 final int userCount = userManager.getUserCount(); 107 return userCount == 1 108 || (UserManager.isSplitSystemUser() && userCount == 2); 109 } 110 } 111