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