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.television; 18 19 import static android.os.UserManager.USER_TYPE_PROFILE_MANAGED; 20 21 import android.app.Activity; 22 import android.content.pm.ApplicationInfo; 23 import android.content.pm.PackageManager; 24 import android.os.Bundle; 25 import android.os.Process; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 29 import androidx.leanback.app.GuidedStepFragment; 30 import androidx.leanback.widget.GuidanceStylist; 31 import androidx.leanback.widget.GuidedAction; 32 33 import com.android.packageinstaller.R; 34 import com.android.packageinstaller.UninstallerActivity; 35 36 import java.util.List; 37 38 public class UninstallAlertFragment extends GuidedStepFragment { 39 @Override onProvideTheme()40 public int onProvideTheme() { 41 return R.style.Theme_Leanback_GuidedStep; 42 } 43 44 @Override onCreateGuidance(Bundle savedInstanceState)45 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 46 final PackageManager pm = getActivity().getPackageManager(); 47 final UninstallerActivity.DialogInfo dialogInfo = 48 ((UninstallerActivity) getActivity()).getDialogInfo(); 49 final CharSequence appLabel = dialogInfo.appInfo.loadSafeLabel(pm); 50 51 StringBuilder messageBuilder = new StringBuilder(); 52 53 // If the Activity label differs from the App label, then make sure the user 54 // knows the Activity belongs to the App being uninstalled. 55 if (dialogInfo.activityInfo != null) { 56 final CharSequence activityLabel = dialogInfo.activityInfo.loadSafeLabel(pm); 57 if (!activityLabel.equals(appLabel)) { 58 messageBuilder.append( 59 getString(R.string.uninstall_activity_text, activityLabel)); 60 messageBuilder.append(" ").append(appLabel).append(".\n\n"); 61 } 62 } 63 64 final boolean isUpdate = 65 ((dialogInfo.appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); 66 final UserHandle myUserHandle = Process.myUserHandle(); 67 UserManager userManager = getContext().getSystemService(UserManager.class); 68 if (isUpdate) { 69 if (isSingleUser(userManager)) { 70 messageBuilder.append(getString(R.string.uninstall_update_text)); 71 } else { 72 messageBuilder.append(getString(R.string.uninstall_update_text_multiuser)); 73 } 74 } else { 75 if (dialogInfo.allUsers && !isSingleUser(userManager)) { 76 messageBuilder.append(getString(R.string.uninstall_application_text_all_users)); 77 } else if (!dialogInfo.user.equals(myUserHandle)) { 78 int userId = dialogInfo.user.getIdentifier(); 79 UserManager customUserManager = getContext() 80 .createContextAsUser(UserHandle.of(userId), 0) 81 .getSystemService(UserManager.class); 82 String userName = customUserManager.getUserName(); 83 84 if (customUserManager.isUserOfType(USER_TYPE_PROFILE_MANAGED) 85 && customUserManager.isSameProfileGroup(dialogInfo.user, myUserHandle)) { 86 87 messageBuilder.append( 88 getString(R.string.uninstall_application_text_current_user_work_profile, 89 userName)); 90 } else { 91 messageBuilder.append( 92 getString(R.string.uninstall_application_text_user, userName)); 93 } 94 } else { 95 messageBuilder.append(getString(R.string.uninstall_application_text)); 96 } 97 } 98 99 return new GuidanceStylist.Guidance( 100 appLabel.toString(), 101 messageBuilder.toString(), 102 null, 103 dialogInfo.appInfo.loadIcon(pm)); 104 } 105 106 @Override onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)107 public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) { 108 actions.add(new GuidedAction.Builder(getContext()) 109 .clickAction(GuidedAction.ACTION_ID_OK) 110 .build()); 111 actions.add(new GuidedAction.Builder(getContext()) 112 .clickAction(GuidedAction.ACTION_ID_CANCEL) 113 .build()); 114 } 115 116 @Override onGuidedActionClicked(GuidedAction action)117 public void onGuidedActionClicked(GuidedAction action) { 118 if (isAdded()) { 119 if (action.getId() == GuidedAction.ACTION_ID_OK) { 120 ((UninstallerActivity) getActivity()).startUninstallProgress(false); 121 getActivity().finish(); 122 } else { 123 ((UninstallerActivity) getActivity()).dispatchAborted(); 124 getActivity().setResult(Activity.RESULT_FIRST_USER); 125 getActivity().finish(); 126 } 127 } 128 } 129 130 /** 131 * Returns whether there is only one user on this device. 132 */ isSingleUser(UserManager userManager)133 private boolean isSingleUser(UserManager userManager) { 134 final int userCount = userManager.getUserCount(); 135 return userCount == 1 || (UserManager.isHeadlessSystemUserMode() && userCount == 2); 136 } 137 } 138