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