2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
17 package com.android.settings.applications;
19 import android.Manifest.permission;
20 import android.app.Activity;
21 import android.app.ActivityManager;
22 import android.app.AlertDialog;
23 import android.app.LoaderManager.LoaderCallbacks;
24 import android.app.Notification;
25 import android.app.admin.DevicePolicyManager;
26 import android.content.ActivityNotFoundException;
27 import android.content.BroadcastReceiver;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.DialogInterface;
31 import android.content.Intent;
32 import android.content.Loader;
33 import android.content.pm.ActivityInfo;
34 import android.content.pm.ApplicationInfo;
35 import android.content.pm.PackageInfo;
36 import android.content.pm.PackageManager;
37 import android.content.pm.PackageManager.NameNotFoundException;
38 import android.content.pm.ResolveInfo;
39 import android.content.pm.UserInfo;
40 import android.content.res.Resources;
41 import android.graphics.drawable.Drawable;
42 import android.icu.text.ListFormatter;
43 import android.net.INetworkStatsService;
44 import android.net.INetworkStatsSession;
45 import android.net.NetworkTemplate;
46 import android.net.TrafficStats;
47 import android.net.Uri;
48 import android.os.AsyncTask;
49 import android.os.BatteryStats;
50 import android.os.Bundle;
51 import android.os.RemoteException;
52 import android.os.ServiceManager;
53 import android.os.UserHandle;
54 import android.os.UserManager;
55 import android.provider.Settings;
56 import android.service.notification.NotificationListenerService.Ranking;
57 import android.support.v7.preference.Preference;
58 import android.support.v7.preference.Preference.OnPreferenceClickListener;
59 import android.support.v7.preference.PreferenceCategory;
60 import android.support.v7.preference.PreferenceScreen;
61 import android.text.TextUtils;
62 import android.text.format.DateUtils;
63 import android.text.format.Formatter;
64 import android.util.Log;
65 import android.view.LayoutInflater;
66 import android.view.Menu;
67 import android.view.MenuInflater;
68 import android.view.MenuItem;
69 import android.view.View;
70 import android.view.View.OnClickListener;
71 import android.view.ViewGroup;
72 import android.webkit.IWebViewUpdateService;
73 import android.widget.Button;
74 import android.widget.ImageView;
75 import android.widget.TextView;
76 import com.android.settings.cyanogenmod.ProtectedAppsReceiver;
78 import com.android.internal.logging.MetricsProto.MetricsEvent;
79 import com.android.internal.os.BatterySipper;
80 import com.android.internal.os.BatteryStatsHelper;
81 import com.android.internal.widget.LockPatternUtils;
82 import com.android.settings.AppHeader;
83 import com.android.settings.DeviceAdminAdd;
84 import com.android.settings.R;
85 import com.android.settings.SettingsActivity;
86 import com.android.settings.SettingsPreferenceFragment;
87 import com.android.settings.Utils;
88 import com.android.settings.applications.PermissionsSummaryHelper.PermissionsResultCallback;
89 import com.android.settings.datausage.AppDataUsage;
90 import com.android.settings.datausage.DataUsageList;
91 import com.android.settings.datausage.DataUsageSummary;
92 import com.android.settings.fuelgauge.BatteryEntry;
93 import com.android.settings.fuelgauge.PowerUsageDetail;
94 import com.android.settings.notification.AppNotificationSettings;
95 import com.android.settings.notification.NotificationBackend;
96 import com.android.settings.notification.NotificationBackend.AppRow;
97 import com.android.settingslib.AppItem;
98 import com.android.settingslib.RestrictedLockUtils;
99 import com.android.settingslib.applications.AppUtils;
100 import com.android.settingslib.applications.ApplicationsState;
101 import com.android.settingslib.applications.ApplicationsState.AppEntry;
102 import com.android.settingslib.net.ChartData;
103 import com.android.settingslib.net.ChartDataLoader;
105 import java.lang.ref.WeakReference;
106 import java.util.ArrayList;
107 import java.util.Arrays;
108 import java.util.HashSet;
109 import java.util.List;
111 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
114 * Activity to display application information from Settings. This activity presents
115 * extended information associated with a package like code, data, total size, permissions
116 * used by the application and also the set of default launchable activities.
117 * For system applications, an option to clear user data is displayed only if data size is > 0.
118 * System applications that do not want clear user data do not have this option.
119 * For non-system applications, there is no option to clear data. Instead there is an option to
120 * uninstall the application.
122 public class InstalledAppDetails extends AppInfoBase
123 implements View.OnClickListener, OnPreferenceClickListener {
125 private static final String LOG_TAG = "InstalledAppDetails";
128 public static final int UNINSTALL_ALL_USERS_MENU = 1;
129 public static final int UNINSTALL_UPDATES = 2;
130 public static final int OPEN_PROTECTED_APPS = 3;
132 // Result code identifiers
133 public static final int REQUEST_UNINSTALL = 0;
134 private static final int REQUEST_REMOVE_DEVICE_ADMIN = 1;
136 private static final int SUB_INFO_FRAGMENT = 1;
137 public static final int REQUEST_TOGGLE_PROTECTION = 3;
139 private static final int LOADER_CHART_DATA = 2;
141 private static final int DLG_FORCE_STOP = DLG_BASE + 1;
142 private static final int DLG_DISABLE = DLG_BASE + 2;
143 private static final int DLG_SPECIAL_DISABLE = DLG_BASE + 3;
145 private static final String KEY_HEADER = "header_view";
146 private static final String KEY_NOTIFICATION = "notification_settings";
147 private static final String KEY_STORAGE = "storage_settings";
148 private static final String KEY_PERMISSION = "permission_settings";
149 private static final String KEY_DATA = "data_settings";
150 private static final String KEY_LAUNCH = "preferred_settings";
151 private static final String KEY_BATTERY = "battery";
152 private static final String KEY_MEMORY = "memory";
154 private static final String NOTIFICATION_TUNER_SETTING = "show_importance_slider";
156 private final HashSet<String> mHomePackages = new HashSet<String>();
158 private boolean mInitialized;
159 private boolean mShowUninstalled;
160 private LayoutPreference mHeader;
161 private Button mUninstallButton;
162 private boolean mUpdatedSysApp = false;
163 private Button mForceStopButton;
164 private Preference mNotificationPreference;
165 private Preference mStoragePreference;
166 private Preference mPermissionsPreference;
167 private Preference mLaunchPreference;
168 private Preference mDataPreference;
169 private Preference mMemoryPreference;
171 private boolean mDisableAfterUninstall;
172 // Used for updating notification preference.
173 private final NotificationBackend mBackend = new NotificationBackend();
175 private ChartData mChartData;
176 private INetworkStatsSession mStatsSession;
178 private Preference mBatteryPreference;
180 private BatteryStatsHelper mBatteryHelper;
181 private BatterySipper mSipper;
183 protected ProcStatsData mStatsManager;
184 protected ProcStatsPackageEntry mStats;
186 private boolean handleDisableable(Button button) {
187 boolean disableable = false;
188 // Try to prevent the user from bricking their phone
189 // by not allowing disabling of apps signed with the
190 // system cert and any launcher app in the system.
191 if (mHomePackages.contains(mAppEntry.info.packageName)
192 || Utils.isSystemPackage(mPm, mPackageInfo)) {
193 // Disable button for core system applications.
194 button.setText(R.string.disable_text);
195 } else if (mAppEntry.info.enabled && !isDisabledUntilUsed()) {
196 button.setText(R.string.disable_text);
199 button.setText(R.string.enable_text);
206 private boolean isDisabledUntilUsed() {
207 return mAppEntry.info.enabledSetting
208 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
211 private void initUninstallButtons() {
212 final boolean isBundled = (mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
213 boolean enabled = true;
215 enabled = handleDisableable(mUninstallButton);
217 if ((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) == 0
218 && mUserManager.getUsers().size() >= 2) {
219 // When we have multiple users, there is a separate menu
220 // to uninstall for all users.
223 mUninstallButton.setText(R.string.uninstall_text);
225 // If this is a device admin, it can't be uninstalled or disabled.
226 // We do this here so the text of the button is still set correctly.
227 if (isBundled && mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
231 // We don't allow uninstalling DO/PO on *any* users, because if it's a system app,
232 // "uninstall" is actually "downgrade to the system version + disable", and "downgrade"
233 // will clear data on all users.
234 if (isProfileOrDeviceOwner(mPackageInfo.packageName)) {
238 // If the uninstall intent is already queued, disable the uninstall button
239 if (mDpm.isUninstallInQueue(mPackageName)) {
243 // Home apps need special handling. Bundled ones we don't risk downgrading
244 // because that can interfere with home-key resolution. Furthermore, we
245 // can't allow uninstallation of the only home app, and we don't want to
246 // allow uninstallation of an explicitly preferred one -- the user can go
247 // to Home settings and pick a different one, after which we'll permit
248 // uninstallation of the now-not-default one.
249 if (enabled && mHomePackages.contains(mPackageInfo.packageName)) {
253 ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
254 ComponentName currentDefaultHome = mPm.getHomeActivities(homeActivities);
255 if (currentDefaultHome == null) {
256 // No preferred default, so permit uninstall only when
257 // there is more than one candidate
258 enabled = (mHomePackages.size() > 1);
260 // There is an explicit default home app -- forbid uninstall of
261 // that one, but permit it for installed-but-inactive ones.
262 enabled = !mPackageInfo.packageName.equals(currentDefaultHome.getPackageName());
267 if (mAppsControlDisallowedBySystem) {
272 IWebViewUpdateService webviewUpdateService =
273 IWebViewUpdateService.Stub.asInterface(ServiceManager.getService("webviewupdate"));
274 if (webviewUpdateService.isFallbackPackage(mAppEntry.info.packageName)) {
277 } catch (RemoteException e) {
278 throw new RuntimeException(e);
281 // This is a protected app component.
282 // You cannot a uninstall a protected component
283 if (mPackageInfo.applicationInfo.protect) {
287 mUninstallButton.setEnabled(enabled);
290 mUninstallButton.setOnClickListener(this);
294 /** Returns if the supplied package is device owner or profile owner of at least one user */
295 private boolean isProfileOrDeviceOwner(String packageName) {
296 List<UserInfo> userInfos = mUserManager.getUsers();
297 DevicePolicyManager dpm = (DevicePolicyManager)
298 getContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
299 if (dpm.isDeviceOwnerAppOnAnyUser(packageName)) {
302 for (UserInfo userInfo : userInfos) {
303 ComponentName cn = dpm.getProfileOwnerAsUser(userInfo.id);
304 if (cn != null && cn.getPackageName().equals(packageName)) {
311 /** Called when the activity is first created. */
313 public void onCreate(Bundle icicle) {
314 super.onCreate(icicle);
316 setHasOptionsMenu(true);
317 addPreferencesFromResource(R.xml.installed_app_details);
320 if (Utils.isBandwidthControlEnabled()) {
321 INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
322 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
324 mStatsSession = statsService.openSession();
325 } catch (RemoteException e) {
326 throw new RuntimeException(e);
329 removePreference(KEY_DATA);
331 mBatteryHelper = new BatteryStatsHelper(getActivity(), true);
335 protected int getMetricsCategory() {
336 return MetricsEvent.APPLICATIONS_INSTALLED_APP_DETAILS;
340 public void onResume() {
345 mState.requestSize(mPackageName, mUserId);
346 AppItem app = new AppItem(mAppEntry.info.uid);
347 app.addUid(mAppEntry.info.uid);
348 if (mStatsSession != null) {
349 getLoaderManager().restartLoader(LOADER_CHART_DATA,
350 ChartDataLoader.buildArgs(getTemplate(getContext()), app),
353 new BatteryUpdater().execute();
354 new MemoryUpdater().execute();
355 updateDynamicPrefs();
359 public void onPause() {
360 getLoaderManager().destroyLoader(LOADER_CHART_DATA);
365 public void onDestroy() {
366 TrafficStats.closeQuietly(mStatsSession);
370 public void onActivityCreated(Bundle savedInstanceState) {
371 super.onActivityCreated(savedInstanceState);
377 mNotificationPreference = findPreference(KEY_NOTIFICATION);
378 mNotificationPreference.setOnPreferenceClickListener(this);
379 mStoragePreference = findPreference(KEY_STORAGE);
380 mStoragePreference.setOnPreferenceClickListener(this);
381 mPermissionsPreference = findPreference(KEY_PERMISSION);
382 mPermissionsPreference.setOnPreferenceClickListener(this);
383 mDataPreference = findPreference(KEY_DATA);
384 if (mDataPreference != null) {
385 mDataPreference.setOnPreferenceClickListener(this);
387 mBatteryPreference = findPreference(KEY_BATTERY);
388 mBatteryPreference.setEnabled(false);
389 mBatteryPreference.setOnPreferenceClickListener(this);
390 mMemoryPreference = findPreference(KEY_MEMORY);
391 mMemoryPreference.setOnPreferenceClickListener(this);
393 mLaunchPreference = findPreference(KEY_LAUNCH);
394 if (mAppEntry != null && mAppEntry.info != null) {
395 if ((mAppEntry.info.flags&ApplicationInfo.FLAG_INSTALLED) == 0 ||
396 !mAppEntry.info.enabled) {
397 mLaunchPreference.setEnabled(false);
399 mLaunchPreference.setOnPreferenceClickListener(this);
402 mLaunchPreference.setEnabled(false);
406 private void handleHeader() {
407 mHeader = (LayoutPreference) findPreference(KEY_HEADER);
409 // Get Control button panel
410 View btnPanel = mHeader.findViewById(R.id.control_buttons_panel);
411 mForceStopButton = (Button) btnPanel.findViewById(R.id.right_button);
412 mForceStopButton.setText(R.string.force_stop);
413 mUninstallButton = (Button) btnPanel.findViewById(R.id.left_button);
414 mForceStopButton.setEnabled(false);
416 View gear = mHeader.findViewById(R.id.gear);
417 Intent i = new Intent(Intent.ACTION_APPLICATION_PREFERENCES);
418 i.setPackage(mPackageName);
419 final Intent intent = resolveIntent(i);
420 if (intent != null) {
421 gear.setVisibility(View.VISIBLE);
422 gear.setOnClickListener(new OnClickListener() {
424 public void onClick(View v) {
425 startActivity(intent);
429 gear.setVisibility(View.GONE);
433 private Intent resolveIntent(Intent i) {
434 ResolveInfo result = getContext().getPackageManager().resolveActivity(i, 0);
435 return result != null ? new Intent(i.getAction())
436 .setClassName(result.activityInfo.packageName, result.activityInfo.name) : null;
440 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
441 menu.add(0, UNINSTALL_UPDATES, 0, R.string.app_factory_reset)
442 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
443 menu.add(0, UNINSTALL_ALL_USERS_MENU, 1, R.string.uninstall_all_users_text)
444 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
445 menu.add(0, OPEN_PROTECTED_APPS, Menu.NONE, R.string.protected_apps)
446 .setIcon(getResources().getDrawable(R.drawable.folder_lock))
447 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
451 public void onPrepareOptionsMenu(Menu menu) {
455 boolean showIt = true;
456 if (mUpdatedSysApp) {
458 } else if (mAppEntry == null) {
460 } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
462 } else if (mPackageInfo == null || mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
464 } else if (UserHandle.myUserId() != 0) {
466 } else if (mUserManager.getUsers().size() < 2) {
469 menu.findItem(UNINSTALL_ALL_USERS_MENU).setVisible(showIt);
470 mUpdatedSysApp = (mAppEntry.info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
471 MenuItem uninstallUpdatesItem = menu.findItem(UNINSTALL_UPDATES);
472 uninstallUpdatesItem.setVisible(mUpdatedSysApp && !mAppsControlDisallowedBySystem);
473 if (uninstallUpdatesItem.isVisible()) {
474 RestrictedLockUtils.setMenuItemAsDisabledByAdmin(getActivity(),
475 uninstallUpdatesItem, mAppsControlDisallowedAdmin);
478 menu.findItem(OPEN_PROTECTED_APPS).setVisible(mPackageInfo.applicationInfo.protect);
482 public boolean onOptionsItemSelected(MenuItem item) {
483 switch (item.getItemId()) {
484 case UNINSTALL_ALL_USERS_MENU:
485 uninstallPkg(mAppEntry.info.packageName, true, false);
487 case UNINSTALL_UPDATES:
488 uninstallPkg(mAppEntry.info.packageName, false, false);
490 case OPEN_PROTECTED_APPS:
491 // Verify protection for toggling protected component status
492 Intent protectedApps = new Intent(getActivity(), LockPatternActivity.class);
493 startActivityForResult(protectedApps, REQUEST_TOGGLE_PROTECTION);
499 public void onActivityResult(int requestCode, int resultCode, Intent data) {
500 super.onActivityResult(requestCode, resultCode, data);
501 if (requestCode == REQUEST_UNINSTALL) {
502 if (mDisableAfterUninstall) {
503 mDisableAfterUninstall = false;
504 new DisableChanger(this, mAppEntry.info,
505 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER)
506 .execute((Object)null);
509 setIntentAndFinish(true, true);
512 if (requestCode == REQUEST_REMOVE_DEVICE_ADMIN) {
514 setIntentAndFinish(true, true);
517 if (requestCode == REQUEST_TOGGLE_PROTECTION) {
518 switch (resultCode) {
519 case Activity.RESULT_OK:
520 new ToggleProtectedAppComponents().execute();
522 case Activity.RESULT_CANCELED:
523 // User failed to enter/confirm a lock pattern, do nothing
529 private class ToggleProtectedAppComponents extends AsyncTask<Void, Void, Void> {
531 protected void onPostExecute(Void aVoid) {
532 getActivity().invalidateOptionsMenu();
534 setIntentAndFinish(true, true);
539 protected Void doInBackground(Void... params) {
540 ArrayList<ComponentName> components = new ArrayList<ComponentName>();
541 for (ActivityInfo aInfo : mPackageInfo.activities) {
542 components.add(new ComponentName(aInfo.packageName, aInfo.name));
545 ProtectedAppsReceiver.updateProtectedAppComponentsAndNotify(getActivity(),
546 components, PackageManager.COMPONENT_VISIBLE_STATUS);
551 // Utility method to set application label and icon.
552 private void setAppLabelAndIcon(PackageInfo pkgInfo) {
553 final View appSnippet = mHeader.findViewById(R.id.app_snippet);
554 mState.ensureIcon(mAppEntry);
555 setupAppSnippet(appSnippet, mAppEntry.label, mAppEntry.icon,
556 pkgInfo != null ? pkgInfo.versionName : null);
559 private boolean signaturesMatch(String pkg1, String pkg2) {
560 if (pkg1 != null && pkg2 != null) {
562 final int match = mPm.checkSignatures(pkg1, pkg2);
563 if (match >= PackageManager.SIGNATURE_MATCH) {
566 } catch (Exception e) {
567 // e.g. named alternate package not found during lookup;
568 // this is an expected case sometimes
575 protected boolean refreshUi() {
577 if (mAppEntry == null) {
578 return false; // onCreate must have failed, make sure to exit
581 if (mPackageInfo == null) {
582 return false; // onCreate must have failed, make sure to exit
585 // Get list of "home" apps and trace through any meta-data references
586 List<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
587 mPm.getHomeActivities(homeActivities);
588 mHomePackages.clear();
589 for (int i = 0; i< homeActivities.size(); i++) {
590 ResolveInfo ri = homeActivities.get(i);
591 final String activityPkg = ri.activityInfo.packageName;
592 mHomePackages.add(activityPkg);
594 // Also make sure to include anything proxying for the home app
595 final Bundle metadata = ri.activityInfo.metaData;
596 if (metadata != null) {
597 final String metaPkg = metadata.getString(ActivityManager.META_HOME_ALTERNATE);
598 if (signaturesMatch(metaPkg, activityPkg)) {
599 mHomePackages.add(metaPkg);
605 setAppLabelAndIcon(mPackageInfo);
606 initUninstallButtons();
608 // Update the preference summaries.
609 Activity context = getActivity();
610 mStoragePreference.setSummary(AppStorageSettings.getSummary(mAppEntry, context));
612 PermissionsSummaryHelper.getPermissionSummary(getContext(),
613 mPackageName, mPermissionCallback);
614 mLaunchPreference.setSummary(AppUtils.getLaunchByDefaultSummary(mAppEntry, mUsbManager,
616 mNotificationPreference.setSummary(getNotificationSummary(mAppEntry, context,
618 if (mDataPreference != null) {
619 mDataPreference.setSummary(getDataSummary());
625 // First time init: are we displaying an uninstalled app?
627 mShowUninstalled = (mAppEntry.info.flags&ApplicationInfo.FLAG_INSTALLED) == 0;
629 // All other times: if the app no longer exists then we want
632 ApplicationInfo ainfo = context.getPackageManager().getApplicationInfo(
633 mAppEntry.info.packageName, PackageManager.GET_UNINSTALLED_PACKAGES
634 | PackageManager.GET_DISABLED_COMPONENTS);
635 if (!mShowUninstalled) {
636 // If we did not start out with the app uninstalled, then
637 // it transitioning to the uninstalled state for the current
638 // user means we should go away as well.
639 return (ainfo.flags&ApplicationInfo.FLAG_INSTALLED) != 0;
641 } catch (NameNotFoundException e) {
649 private void updateBattery() {
650 if (mSipper != null) {
651 mBatteryPreference.setEnabled(true);
652 int dischargeAmount = mBatteryHelper.getStats().getDischargeAmount(
653 BatteryStats.STATS_SINCE_CHARGED);
654 final int percentOfMax = (int) ((mSipper.totalPowerMah)
655 / mBatteryHelper.getTotalPower() * dischargeAmount + .5f);
656 mBatteryPreference.setSummary(getString(R.string.battery_summary, percentOfMax));
658 mBatteryPreference.setEnabled(false);
659 mBatteryPreference.setSummary(getString(R.string.no_battery_summary));
663 private CharSequence getDataSummary() {
664 if (mChartData != null) {
665 long totalBytes = mChartData.detail.getTotalBytes();
666 if (totalBytes == 0) {
667 return getString(R.string.no_data_usage);
669 Context context = getActivity();
670 return getString(R.string.data_summary_format,
671 Formatter.formatFileSize(context, totalBytes),
672 DateUtils.formatDateTime(context, mChartData.detail.getStart(),
673 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH));
675 return getString(R.string.computing_size);
679 protected AlertDialog createDialog(int id, int errorCode) {
682 return new AlertDialog.Builder(getActivity())
683 .setMessage(getActivity().getText(R.string.app_disable_dlg_text))
684 .setPositiveButton(R.string.app_disable_dlg_positive,
685 new DialogInterface.OnClickListener() {
686 public void onClick(DialogInterface dialog, int which) {
688 new DisableChanger(InstalledAppDetails.this, mAppEntry.info,
689 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER)
690 .execute((Object)null);
693 .setNegativeButton(R.string.dlg_cancel, null)
695 case DLG_SPECIAL_DISABLE:
696 return new AlertDialog.Builder(getActivity())
697 .setMessage(getActivity().getText(R.string.app_disable_dlg_text))
698 .setPositiveButton(R.string.app_disable_dlg_positive,
699 new DialogInterface.OnClickListener() {
700 public void onClick(DialogInterface dialog, int which) {
701 // Disable the app and ask for uninstall
702 uninstallPkg(mAppEntry.info.packageName,
706 .setNegativeButton(R.string.dlg_cancel, null)
709 return new AlertDialog.Builder(getActivity())
710 .setTitle(getActivity().getText(R.string.force_stop_dlg_title))
711 .setMessage(getActivity().getText(R.string.force_stop_dlg_text))
712 .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
713 public void onClick(DialogInterface dialog, int which) {
715 forceStopPackage(mAppEntry.info.packageName);
718 .setNegativeButton(R.string.dlg_cancel, null)
724 private void uninstallPkg(String packageName, boolean allUsers, boolean andDisable) {
725 // Create new intent to launch Uninstaller activity
726 Uri packageURI = Uri.parse("package:"+packageName);
727 Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
728 uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, allUsers);
729 startActivityForResult(uninstallIntent, REQUEST_UNINSTALL);
730 mDisableAfterUninstall = andDisable;
733 private void forceStopPackage(String pkgName) {
734 ActivityManager am = (ActivityManager) getActivity().getSystemService(
735 Context.ACTIVITY_SERVICE);
736 am.forceStopPackage(pkgName);
737 int userId = UserHandle.getUserId(mAppEntry.info.uid);
738 mState.invalidatePackage(pkgName, userId);
739 ApplicationsState.AppEntry newEnt = mState.getEntry(pkgName, userId);
740 if (newEnt != null) {
746 private void updateForceStopButton(boolean enabled) {
747 if (mAppsControlDisallowedBySystem) {
748 mForceStopButton.setEnabled(false);
750 mForceStopButton.setEnabled(enabled);
751 mForceStopButton.setOnClickListener(InstalledAppDetails.this);
755 private void checkForceStop() {
756 if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
757 // User can't force stop device admin.
758 updateForceStopButton(false);
759 } else if ((mAppEntry.info.flags&ApplicationInfo.FLAG_STOPPED) == 0) {
760 // If the app isn't explicitly stopped, then always show the
761 // force stop button.
762 updateForceStopButton(true);
764 Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART,
765 Uri.fromParts("package", mAppEntry.info.packageName, null));
766 intent.putExtra(Intent.EXTRA_PACKAGES, new String[] { mAppEntry.info.packageName });
767 intent.putExtra(Intent.EXTRA_UID, mAppEntry.info.uid);
768 intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(mAppEntry.info.uid));
769 getActivity().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
770 mCheckKillProcessesReceiver, null, Activity.RESULT_CANCELED, null, null);
774 private void startManagePermissionsActivity() {
775 // start new activity to manage app permissions
776 Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS);
777 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mAppEntry.info.packageName);
778 intent.putExtra(AppHeader.EXTRA_HIDE_INFO_BUTTON, true);
780 startActivity(intent);
781 } catch (ActivityNotFoundException e) {
782 Log.w(LOG_TAG, "No app can handle android.intent.action.MANAGE_APP_PERMISSIONS");
786 private void startAppInfoFragment(Class<?> fragment, CharSequence title) {
787 startAppInfoFragment(fragment, title, this, mAppEntry);
790 public static void startAppInfoFragment(Class<?> fragment, CharSequence title,
791 SettingsPreferenceFragment caller, AppEntry appEntry) {
792 // start new fragment to display extended information
793 Bundle args = new Bundle();
794 args.putString(ARG_PACKAGE_NAME, appEntry.info.packageName);
795 args.putInt(ARG_PACKAGE_UID, appEntry.info.uid);
796 args.putBoolean(AppHeader.EXTRA_HIDE_INFO_BUTTON, true);
798 SettingsActivity sa = (SettingsActivity) caller.getActivity();
799 sa.startPreferencePanel(fragment.getName(), args, -1, title, caller, SUB_INFO_FRAGMENT);
803 * Method implementing functionality of buttons clicked
804 * @see android.view.View.OnClickListener#onClick(android.view.View)
806 public void onClick(View v) {
807 if (mAppEntry == null) {
808 setIntentAndFinish(true, true);
811 String packageName = mAppEntry.info.packageName;
812 if (v == mUninstallButton) {
813 if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
814 Activity activity = getActivity();
815 Intent uninstallDAIntent = new Intent(activity, DeviceAdminAdd.class);
816 uninstallDAIntent.putExtra(DeviceAdminAdd.EXTRA_DEVICE_ADMIN_PACKAGE_NAME,
818 activity.startActivityForResult(uninstallDAIntent, REQUEST_REMOVE_DEVICE_ADMIN);
821 EnforcedAdmin admin = RestrictedLockUtils.checkIfUninstallBlocked(getActivity(),
822 packageName, mUserId);
823 boolean uninstallBlockedBySystem = mAppsControlDisallowedBySystem ||
824 RestrictedLockUtils.hasBaseUserRestriction(getActivity(), packageName, mUserId);
825 if (admin != null && !uninstallBlockedBySystem) {
826 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getActivity(), admin);
827 } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
828 if (mAppEntry.info.enabled && !isDisabledUntilUsed()) {
829 // If the system app has an update and this is the only user on the device,
830 // then offer to downgrade the app, otherwise only offer to disable the
831 // app for this user.
832 if (mUpdatedSysApp && isSingleUser()) {
833 showDialogInner(DLG_SPECIAL_DISABLE, 0);
835 showDialogInner(DLG_DISABLE, 0);
838 new DisableChanger(this, mAppEntry.info,
839 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
840 .execute((Object) null);
842 } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
843 uninstallPkg(packageName, true, false);
845 uninstallPkg(packageName, false, false);
847 } else if (v == mForceStopButton) {
848 if (mAppsControlDisallowedAdmin != null && !mAppsControlDisallowedBySystem) {
849 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
850 getActivity(), mAppsControlDisallowedAdmin);
852 showDialogInner(DLG_FORCE_STOP, 0);
853 //forceStopPackage(mAppInfo.packageName);
858 /** Returns whether there is only one user on this device, not including the system-only user */
859 private boolean isSingleUser() {
860 final int userCount = mUserManager.getUserCount();
861 return userCount == 1
862 || (mUserManager.isSplitSystemUser() && userCount == 2);
866 public boolean onPreferenceClick(Preference preference) {
867 if (preference == mStoragePreference) {
868 startAppInfoFragment(AppStorageSettings.class, mStoragePreference.getTitle());
869 } else if (preference == mNotificationPreference) {
870 startAppInfoFragment(AppNotificationSettings.class,
871 getString(R.string.app_notifications_title));
872 } else if (preference == mPermissionsPreference) {
873 startManagePermissionsActivity();
874 } else if (preference == mLaunchPreference) {
875 startAppInfoFragment(AppLaunchSettings.class, mLaunchPreference.getTitle());
876 } else if (preference == mMemoryPreference) {
877 ProcessStatsBase.launchMemoryDetail((SettingsActivity) getActivity(),
878 mStatsManager.getMemInfo(), mStats, false);
879 } else if (preference == mDataPreference) {
880 startAppInfoFragment(AppDataUsage.class, getString(R.string.app_data_usage));
881 } else if (preference == mBatteryPreference) {
882 BatteryEntry entry = new BatteryEntry(getActivity(), null, mUserManager, mSipper);
883 PowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(),
884 mBatteryHelper, BatteryStats.STATS_SINCE_CHARGED, entry, true, false);
891 private void addDynamicPrefs() {
892 if (Utils.isManagedProfile(UserManager.get(getContext()))) {
895 final PreferenceScreen screen = getPreferenceScreen();
896 if (DefaultHomePreference.hasHomePreference(mPackageName, getContext())) {
897 screen.addPreference(new ShortcutPreference(getPrefContext(),
898 AdvancedAppSettings.class, "default_home", R.string.home_app,
899 R.string.configure_apps));
901 if (DefaultBrowserPreference.hasBrowserPreference(mPackageName, getContext())) {
902 screen.addPreference(new ShortcutPreference(getPrefContext(),
903 AdvancedAppSettings.class, "default_browser", R.string.default_browser_title,
904 R.string.configure_apps));
906 if (DefaultPhonePreference.hasPhonePreference(mPackageName, getContext())) {
907 screen.addPreference(new ShortcutPreference(getPrefContext(),
908 AdvancedAppSettings.class, "default_phone_app", R.string.default_phone_title,
909 R.string.configure_apps));
911 if (DefaultEmergencyPreference.hasEmergencyPreference(mPackageName, getContext())) {
912 screen.addPreference(new ShortcutPreference(getPrefContext(),
913 AdvancedAppSettings.class, "default_emergency_app",
914 R.string.default_emergency_app, R.string.configure_apps));
916 if (DefaultSmsPreference.hasSmsPreference(mPackageName, getContext())) {
917 screen.addPreference(new ShortcutPreference(getPrefContext(),
918 AdvancedAppSettings.class, "default_sms_app", R.string.sms_application_title,
919 R.string.configure_apps));
921 boolean hasDrawOverOtherApps = hasPermission(permission.SYSTEM_ALERT_WINDOW);
922 boolean hasWriteSettings = hasPermission(permission.WRITE_SETTINGS);
923 if (hasDrawOverOtherApps || hasWriteSettings) {
924 PreferenceCategory category = new PreferenceCategory(getPrefContext());
925 category.setTitle(R.string.advanced_apps);
926 screen.addPreference(category);
928 if (hasDrawOverOtherApps) {
929 Preference pref = new Preference(getPrefContext());
930 pref.setTitle(R.string.draw_overlay);
931 pref.setKey("system_alert_window");
932 pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
934 public boolean onPreferenceClick(Preference preference) {
935 startAppInfoFragment(DrawOverlayDetails.class,
936 getString(R.string.draw_overlay));
940 category.addPreference(pref);
942 if (hasWriteSettings) {
943 Preference pref = new Preference(getPrefContext());
944 pref.setTitle(R.string.write_settings);
945 pref.setKey("write_settings_apps");
946 pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
948 public boolean onPreferenceClick(Preference preference) {
949 startAppInfoFragment(WriteSettingsDetails.class,
950 getString(R.string.write_settings));
954 category.addPreference(pref);
958 addAppInstallerInfoPref(screen);
961 private void addAppInstallerInfoPref(PreferenceScreen screen) {
962 String installerPackageName = null;
964 installerPackageName =
965 getContext().getPackageManager().getInstallerPackageName(mPackageName);
966 } catch (IllegalArgumentException e) {
967 Log.e(TAG, "Exception while retrieving the package installer of " + mPackageName, e);
969 if (installerPackageName == null) {
972 final CharSequence installerLabel = Utils.getApplicationLabel(getContext(),
973 installerPackageName);
974 if (installerLabel == null) {
977 PreferenceCategory category = new PreferenceCategory(getPrefContext());
978 category.setTitle(R.string.app_install_details_group_title);
979 screen.addPreference(category);
980 Preference pref = new Preference(getPrefContext());
981 pref.setTitle(R.string.app_install_details_title);
982 pref.setKey("app_info_store");
983 pref.setSummary(getString(R.string.app_install_details_summary, installerLabel));
984 final Intent intent = new Intent(Intent.ACTION_SHOW_APP_INFO)
985 .setPackage(installerPackageName);
986 final Intent result = resolveIntent(intent);
987 if (result != null) {
988 result.putExtra(Intent.EXTRA_PACKAGE_NAME, mPackageName);
989 pref.setIntent(result);
991 pref.setEnabled(false);
993 category.addPreference(pref);
996 private boolean hasPermission(String permission) {
997 if (mPackageInfo == null || mPackageInfo.requestedPermissions == null) {
1000 for (int i = 0; i < mPackageInfo.requestedPermissions.length; i++) {
1001 if (mPackageInfo.requestedPermissions[i].equals(permission)) {
1008 private void updateDynamicPrefs() {
1009 Preference pref = findPreference("default_home");
1011 pref.setSummary(DefaultHomePreference.isHomeDefault(mPackageName, getContext())
1012 ? R.string.yes : R.string.no);
1014 pref = findPreference("default_browser");
1016 pref.setSummary(DefaultBrowserPreference.isBrowserDefault(mPackageName, getContext())
1017 ? R.string.yes : R.string.no);
1019 pref = findPreference("default_phone_app");
1021 pref.setSummary(DefaultPhonePreference.isPhoneDefault(mPackageName, getContext())
1022 ? R.string.yes : R.string.no);
1024 pref = findPreference("default_emergency_app");
1026 pref.setSummary(DefaultEmergencyPreference.isEmergencyDefault(mPackageName,
1027 getContext()) ? R.string.yes : R.string.no);
1029 pref = findPreference("default_sms_app");
1031 pref.setSummary(DefaultSmsPreference.isSmsDefault(mPackageName, getContext())
1032 ? R.string.yes : R.string.no);
1034 pref = findPreference("system_alert_window");
1036 pref.setSummary(DrawOverlayDetails.getSummary(getContext(), mAppEntry));
1038 pref = findPreference("write_settings_apps");
1040 pref.setSummary(WriteSettingsDetails.getSummary(getContext(), mAppEntry));
1044 public static void setupAppSnippet(View appSnippet, CharSequence label, Drawable icon,
1045 CharSequence versionName) {
1046 LayoutInflater.from(appSnippet.getContext()).inflate(R.layout.widget_text_views,
1047 (ViewGroup) appSnippet.findViewById(android.R.id.widget_frame));
1049 ImageView iconView = (ImageView) appSnippet.findViewById(android.R.id.icon);
1050 iconView.setImageDrawable(icon);
1051 // Set application name.
1052 TextView labelView = (TextView) appSnippet.findViewById(android.R.id.title);
1053 labelView.setText(label);
1054 // Version number of application
1055 TextView appVersion = (TextView) appSnippet.findViewById(R.id.widget_text1);
1057 if (!TextUtils.isEmpty(versionName)) {
1058 appVersion.setSelected(true);
1059 appVersion.setVisibility(View.VISIBLE);
1060 appVersion.setText(appSnippet.getContext().getString(R.string.version_text,
1061 String.valueOf(versionName)));
1063 appVersion.setVisibility(View.INVISIBLE);
1067 public static NetworkTemplate getTemplate(Context context) {
1068 if (DataUsageList.hasReadyMobileRadio(context)) {
1069 return NetworkTemplate.buildTemplateMobileWildcard();
1071 if (DataUsageSummary.hasWifiRadio(context)) {
1072 return NetworkTemplate.buildTemplateWifiWildcard();
1074 return NetworkTemplate.buildTemplateEthernet();
1077 public static CharSequence getNotificationSummary(AppEntry appEntry, Context context) {
1078 return getNotificationSummary(appEntry, context, new NotificationBackend());
1081 public static CharSequence getNotificationSummary(AppEntry appEntry, Context context,
1082 NotificationBackend backend) {
1083 AppRow appRow = backend.loadAppRow(context, context.getPackageManager(), appEntry.info);
1084 return getNotificationSummary(appRow, context);
1087 public static CharSequence getNotificationSummary(AppRow appRow, Context context) {
1088 boolean showSlider = Settings.Secure.getInt(
1089 context.getContentResolver(), NOTIFICATION_TUNER_SETTING, 0) == 1;
1090 List<String> summaryAttributes = new ArrayList<>();
1091 StringBuffer summary = new StringBuffer();
1093 if (appRow.appImportance != Ranking.IMPORTANCE_UNSPECIFIED) {
1094 summaryAttributes.add(context.getString(
1095 R.string.notification_summary_level, appRow.appImportance));
1098 if (appRow.banned) {
1099 summaryAttributes.add(context.getString(R.string.notifications_disabled));
1100 } else if (appRow.appImportance > Ranking.IMPORTANCE_NONE
1101 && appRow.appImportance < Ranking.IMPORTANCE_DEFAULT) {
1102 summaryAttributes.add(context.getString(R.string.notifications_silenced));
1105 final boolean lockscreenSecure = new LockPatternUtils(context).isSecure(
1106 UserHandle.myUserId());
1107 if (lockscreenSecure) {
1108 if (appRow.appVisOverride == Notification.VISIBILITY_PRIVATE) {
1109 summaryAttributes.add(context.getString(R.string.notifications_redacted));
1110 } else if (appRow.appVisOverride == Notification.VISIBILITY_SECRET) {
1111 summaryAttributes.add(context.getString(R.string.notifications_hidden));
1114 if (appRow.appBypassDnd) {
1115 summaryAttributes.add(context.getString(R.string.notifications_priority));
1117 final int N = summaryAttributes.size();
1118 for (int i = 0; i < N; i++) {
1120 summary.append(context.getString(R.string.notifications_summary_divider));
1122 summary.append(summaryAttributes.get(i));
1124 return summary.toString();
1127 private class MemoryUpdater extends AsyncTask<Void, Void, ProcStatsPackageEntry> {
1130 protected ProcStatsPackageEntry doInBackground(Void... params) {
1131 if (getActivity() == null) {
1134 if (mPackageInfo == null) {
1137 if (mStatsManager == null) {
1138 mStatsManager = new ProcStatsData(getActivity(), false);
1139 mStatsManager.setDuration(ProcessStatsBase.sDurations[0]);
1141 mStatsManager.refreshStats(true);
1142 for (ProcStatsPackageEntry pkgEntry : mStatsManager.getEntries()) {
1143 for (ProcStatsEntry entry : pkgEntry.mEntries) {
1144 if (entry.mUid == mPackageInfo.applicationInfo.uid) {
1145 pkgEntry.updateMetrics();
1154 protected void onPostExecute(ProcStatsPackageEntry entry) {
1155 if (getActivity() == null) {
1158 if (entry != null) {
1160 mMemoryPreference.setEnabled(true);
1161 double amount = Math.max(entry.mRunWeight, entry.mBgWeight)
1162 * mStatsManager.getMemInfo().weightToRam;
1163 mMemoryPreference.setSummary(getString(R.string.memory_use_summary,
1164 Formatter.formatShortFileSize(getContext(), (long) amount)));
1166 mMemoryPreference.setEnabled(false);
1167 mMemoryPreference.setSummary(getString(R.string.no_memory_use_summary));
1173 private class BatteryUpdater extends AsyncTask<Void, Void, Void> {
1175 protected Void doInBackground(Void... params) {
1176 mBatteryHelper.create((Bundle) null);
1177 mBatteryHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED,
1178 mUserManager.getUserProfiles());
1179 List<BatterySipper> usageList = mBatteryHelper.getUsageList();
1180 final int N = usageList.size();
1181 for (int i = 0; i < N; i++) {
1182 BatterySipper sipper = usageList.get(i);
1183 if (sipper.getUid() == mPackageInfo.applicationInfo.uid) {
1192 protected void onPostExecute(Void result) {
1193 if (getActivity() == null) {
1200 private static class DisableChanger extends AsyncTask<Object, Object, Object> {
1201 final PackageManager mPm;
1202 final WeakReference<InstalledAppDetails> mActivity;
1203 final ApplicationInfo mInfo;
1206 DisableChanger(InstalledAppDetails activity, ApplicationInfo info, int state) {
1208 mActivity = new WeakReference<InstalledAppDetails>(activity);
1214 protected Object doInBackground(Object... params) {
1215 mPm.setApplicationEnabledSetting(mInfo.packageName, mState, 0);
1220 private final LoaderCallbacks<ChartData> mDataCallbacks = new LoaderCallbacks<ChartData>() {
1223 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
1224 return new ChartDataLoader(getActivity(), mStatsSession, args);
1228 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1230 mDataPreference.setSummary(getDataSummary());
1234 public void onLoaderReset(Loader<ChartData> loader) {
1235 // Leave last result.
1239 private final BroadcastReceiver mCheckKillProcessesReceiver = new BroadcastReceiver() {
1241 public void onReceive(Context context, Intent intent) {
1242 updateForceStopButton(getResultCode() != Activity.RESULT_CANCELED);
1246 private final PermissionsResultCallback mPermissionCallback
1247 = new PermissionsResultCallback() {
1249 public void onPermissionSummaryResult(int standardGrantedPermissionCount,
1250 int requestedPermissionCount, int additionalGrantedPermissionCount,
1251 List<CharSequence> grantedGroupLabels) {
1252 if (getActivity() == null) {
1255 final Resources res = getResources();
1256 CharSequence summary = null;
1258 if (requestedPermissionCount == 0) {
1259 summary = res.getString(
1260 R.string.runtime_permissions_summary_no_permissions_requested);
1261 mPermissionsPreference.setOnPreferenceClickListener(null);
1262 mPermissionsPreference.setEnabled(false);
1264 final ArrayList<CharSequence> list = new ArrayList<>(grantedGroupLabels);
1265 if (additionalGrantedPermissionCount > 0) {
1266 // N additional permissions.
1267 list.add(res.getQuantityString(
1268 R.plurals.runtime_permissions_additional_count,
1269 additionalGrantedPermissionCount, additionalGrantedPermissionCount));
1271 if (list.size() == 0) {
1272 summary = res.getString(
1273 R.string.runtime_permissions_summary_no_permissions_granted);
1275 summary = ListFormatter.getInstance().format(list);
1277 mPermissionsPreference.setOnPreferenceClickListener(InstalledAppDetails.this);
1278 mPermissionsPreference.setEnabled(true);
1280 mPermissionsPreference.setSummary(summary);