• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.tv.settings.device.apps;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ApplicationInfo;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.support.annotation.NonNull;
25 import android.support.v17.leanback.widget.GuidanceStylist;
26 
27 import com.android.settingslib.applications.ApplicationsState;
28 import com.android.tv.settings.R;
29 
30 public class UninstallPreference extends AppActionPreference {
31 
UninstallPreference(Context context, ApplicationsState.AppEntry entry)32     public UninstallPreference(Context context,
33             ApplicationsState.AppEntry entry) {
34         super(context, entry);
35         ConfirmationFragment.prepareArgs(getExtras(), entry.info.packageName);
36         refresh();
37     }
38 
refresh()39     public void refresh() {
40         if (canUninstall()) {
41             setVisible(true);
42             setTitle(R.string.device_apps_app_management_uninstall);
43         } else if (canUninstallUpdates()) {
44             setVisible(true);
45             setTitle(R.string.device_apps_app_management_uninstall_updates);
46         } else {
47             setVisible(false);
48         }
49     }
50 
canUninstall()51     public boolean canUninstall() {
52         return canUninstall(mEntry);
53     }
54 
canUninstall(ApplicationsState.AppEntry entry)55     public static boolean canUninstall(ApplicationsState.AppEntry entry) {
56         return (entry.info.flags &
57                 (ApplicationInfo.FLAG_UPDATED_SYSTEM_APP|ApplicationInfo.FLAG_SYSTEM)) == 0;
58     }
59 
canUninstallUpdates()60     public boolean canUninstallUpdates() {
61         return (mEntry.info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
62     }
63 
getUninstallIntent()64     public Intent getUninstallIntent() {
65         final Uri packageURI = Uri.parse("package:" + mEntry.info.packageName);
66         final Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
67         uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, true);
68         uninstallIntent.putExtra(Intent.EXTRA_KEY_CONFIRM, true);
69         return uninstallIntent;
70     }
71 
72     @Override
getFragment()73     public String getFragment() {
74         return ConfirmationFragment.class.getName();
75     }
76 
77     public static class ConfirmationFragment extends AppActionPreference.ConfirmationFragment {
78         private static final String ARG_PACKAGE_NAME = "packageName";
79 
prepareArgs(@onNull Bundle args, String packageName)80         private static void prepareArgs(@NonNull Bundle args, String packageName) {
81             args.putString(ARG_PACKAGE_NAME, packageName);
82         }
83 
84         @NonNull
85         @Override
onCreateGuidance(Bundle savedInstanceState)86         public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
87             final AppManagementFragment fragment = (AppManagementFragment) getTargetFragment();
88             return new GuidanceStylist.Guidance(
89                     getString(R.string.device_apps_app_management_uninstall),
90                     getString(R.string.device_apps_app_management_uninstall_desc),
91                     fragment.getAppName(),
92                     fragment.getAppIcon());
93         }
94 
95         @Override
onOk()96         public void onOk() {
97             final AppManagementFragment fragment =
98                     (AppManagementFragment) getTargetFragment();
99             fragment.launchUninstall();
100         }
101     }
102 }
103