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.app.Activity;
20 import android.app.ActivityManager;
21 import android.app.AlertDialog;
22 import android.app.LoaderManager.LoaderCallbacks;
23 import android.app.admin.DevicePolicyManager;
24 import android.content.ActivityNotFoundException;
25 import android.content.BroadcastReceiver;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.Loader;
31 import android.content.pm.ApplicationInfo;
32 import android.content.pm.PackageInfo;
33 import android.content.pm.PackageManager;
34 import android.content.pm.PackageManager.NameNotFoundException;
35 import android.content.pm.ResolveInfo;
36 import android.content.pm.UserInfo;
37 import android.content.res.Resources;
38 import android.icu.text.ListFormatter;
39 import android.graphics.drawable.Drawable;
40 import android.net.INetworkStatsService;
41 import android.net.INetworkStatsSession;
42 import android.net.NetworkTemplate;
43 import android.net.TrafficStats;
44 import android.net.Uri;
45 import android.os.AsyncTask;
46 import android.os.BatteryStats;
47 import android.os.Bundle;
48 import android.os.RemoteException;
49 import android.os.ServiceManager;
50 import android.os.UserHandle;
51 import android.preference.Preference;
52 import android.preference.Preference.OnPreferenceClickListener;
53 import android.text.TextUtils;
54 import android.text.format.DateUtils;
55 import android.text.format.Formatter;
56 import android.util.Log;
57 import android.view.LayoutInflater;
58 import android.view.Menu;
59 import android.view.MenuInflater;
60 import android.view.MenuItem;
61 import android.view.View;
62 import android.view.ViewGroup;
63 import android.widget.Button;
64 import android.widget.ImageView;
65 import android.widget.TextView;
67 import com.android.internal.logging.MetricsLogger;
68 import com.android.internal.os.BatterySipper;
69 import com.android.internal.os.BatteryStatsHelper;
70 import com.android.settings.DataUsageSummary;
71 import com.android.settings.DataUsageSummary.AppItem;
72 import com.android.settings.R;
73 import com.android.settings.SettingsActivity;
74 import com.android.settings.Utils;
75 import com.android.settings.applications.PermissionsSummaryHelper.PermissionsResultCallback;
76 import com.android.settings.fuelgauge.BatteryEntry;
77 import com.android.settings.fuelgauge.PowerUsageDetail;
78 import com.android.settings.net.ChartData;
79 import com.android.settings.net.ChartDataLoader;
80 import com.android.settings.notification.AppNotificationSettings;
81 import com.android.settings.notification.NotificationBackend;
82 import com.android.settings.notification.NotificationBackend.AppRow;
83 import com.android.settingslib.applications.ApplicationsState;
84 import com.android.settingslib.applications.ApplicationsState.AppEntry;
86 import java.lang.ref.WeakReference;
87 import java.util.ArrayList;
88 import java.util.Arrays;
89 import java.util.HashSet;
90 import java.util.List;
93 * Activity to display application information from Settings. This activity presents
94 * extended information associated with a package like code, data, total size, permissions
95 * used by the application and also the set of default launchable activities.
96 * For system applications, an option to clear user data is displayed only if data size is > 0.
97 * System applications that do not want clear user data do not have this option.
98 * For non-system applications, there is no option to clear data. Instead there is an option to
99 * uninstall the application.
101 public class InstalledAppDetails extends AppInfoBase
102 implements View.OnClickListener, OnPreferenceClickListener {
104 private static final String LOG_TAG = "InstalledAppDetails";
107 public static final int UNINSTALL_ALL_USERS_MENU = 1;
108 public static final int UNINSTALL_UPDATES = 2;
110 // Result code identifiers
111 public static final int REQUEST_UNINSTALL = 0;
112 private static final int SUB_INFO_FRAGMENT = 1;
114 private static final int LOADER_CHART_DATA = 2;
116 private static final int DLG_FORCE_STOP = DLG_BASE + 1;
117 private static final int DLG_DISABLE = DLG_BASE + 2;
118 private static final int DLG_SPECIAL_DISABLE = DLG_BASE + 3;
119 private static final int DLG_FACTORY_RESET = DLG_BASE + 4;
121 private static final String KEY_HEADER = "header_view";
122 private static final String KEY_NOTIFICATION = "notification_settings";
123 private static final String KEY_STORAGE = "storage_settings";
124 private static final String KEY_PERMISSION = "permission_settings";
125 private static final String KEY_DATA = "data_settings";
126 private static final String KEY_LAUNCH = "preferred_settings";
127 private static final String KEY_BATTERY = "battery";
128 private static final String KEY_MEMORY = "memory";
130 private final HashSet<String> mHomePackages = new HashSet<String>();
132 private boolean mInitialized;
133 private boolean mShowUninstalled;
134 private LayoutPreference mHeader;
135 private Button mUninstallButton;
136 private boolean mUpdatedSysApp = false;
137 private Button mForceStopButton;
138 private Preference mNotificationPreference;
139 private Preference mStoragePreference;
140 private Preference mPermissionsPreference;
141 private Preference mLaunchPreference;
142 private Preference mDataPreference;
143 private Preference mMemoryPreference;
145 private boolean mDisableAfterUninstall;
146 // Used for updating notification preference.
147 private final NotificationBackend mBackend = new NotificationBackend();
149 private ChartData mChartData;
150 private INetworkStatsSession mStatsSession;
152 private Preference mBatteryPreference;
154 private BatteryStatsHelper mBatteryHelper;
155 private BatterySipper mSipper;
157 protected ProcStatsData mStatsManager;
158 protected ProcStatsPackageEntry mStats;
160 private BroadcastReceiver mPermissionReceiver;
162 private boolean handleDisableable(Button button) {
163 boolean disableable = false;
164 // Try to prevent the user from bricking their phone
165 // by not allowing disabling of apps signed with the
166 // system cert and any launcher app in the system.
167 if (mHomePackages.contains(mAppEntry.info.packageName)
168 || Utils.isSystemPackage(mPm, mPackageInfo)) {
169 // Disable button for core system applications.
170 button.setText(R.string.disable_text);
171 } else if (mAppEntry.info.enabled && !isDisabledUntilUsed()) {
172 button.setText(R.string.disable_text);
175 button.setText(R.string.enable_text);
182 private boolean isDisabledUntilUsed() {
183 return mAppEntry.info.enabledSetting
184 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
187 private void initUninstallButtons() {
188 final boolean isBundled = (mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
189 boolean enabled = true;
191 enabled = handleDisableable(mUninstallButton);
193 if ((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) == 0
194 && mUserManager.getUsers().size() >= 2) {
195 // When we have multiple users, there is a separate menu
196 // to uninstall for all users.
199 mUninstallButton.setText(R.string.uninstall_text);
201 // If this is a device admin, it can't be uninstalled or disabled.
202 // We do this here so the text of the button is still set correctly.
203 if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
207 if (isProfileOrDeviceOwner(mPackageInfo.packageName)) {
211 // Home apps need special handling. Bundled ones we don't risk downgrading
212 // because that can interfere with home-key resolution. Furthermore, we
213 // can't allow uninstallation of the only home app, and we don't want to
214 // allow uninstallation of an explicitly preferred one -- the user can go
215 // to Home settings and pick a different one, after which we'll permit
216 // uninstallation of the now-not-default one.
217 if (enabled && mHomePackages.contains(mPackageInfo.packageName)) {
221 ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
222 ComponentName currentDefaultHome = mPm.getHomeActivities(homeActivities);
223 if (currentDefaultHome == null) {
224 // No preferred default, so permit uninstall only when
225 // there is more than one candidate
226 enabled = (mHomePackages.size() > 1);
228 // There is an explicit default home app -- forbid uninstall of
229 // that one, but permit it for installed-but-inactive ones.
230 enabled = !mPackageInfo.packageName.equals(currentDefaultHome.getPackageName());
235 if (mAppControlRestricted) {
239 mUninstallButton.setEnabled(enabled);
242 mUninstallButton.setOnClickListener(this);
246 /** Returns if the supplied package is device owner or profile owner of at least one user */
247 private boolean isProfileOrDeviceOwner(String packageName) {
248 List<UserInfo> userInfos = mUserManager.getUsers();
249 DevicePolicyManager dpm = (DevicePolicyManager)
250 getContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
251 if (packageName.equals(dpm.getDeviceOwner())) {
254 for (UserInfo userInfo : userInfos) {
255 ComponentName cn = dpm.getProfileOwnerAsUser(userInfo.id);
256 if (cn != null && cn.getPackageName().equals(packageName)) {
263 /** Called when the activity is first created. */
265 public void onCreate(Bundle icicle) {
266 super.onCreate(icicle);
268 setHasOptionsMenu(true);
269 addPreferencesFromResource(R.xml.installed_app_details);
271 if (Utils.isBandwidthControlEnabled()) {
272 INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
273 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
275 mStatsSession = statsService.openSession();
276 } catch (RemoteException e) {
277 throw new RuntimeException(e);
280 removePreference(KEY_DATA);
282 mBatteryHelper = new BatteryStatsHelper(getActivity(), true);
286 protected int getMetricsCategory() {
287 return MetricsLogger.APPLICATIONS_INSTALLED_APP_DETAILS;
291 public void onResume() {
296 mState.requestSize(mPackageName, mUserId);
297 AppItem app = new AppItem(mAppEntry.info.uid);
298 app.addUid(mAppEntry.info.uid);
299 if (mStatsSession != null) {
300 getLoaderManager().restartLoader(LOADER_CHART_DATA,
301 ChartDataLoader.buildArgs(getTemplate(getContext()), app),
304 new BatteryUpdater().execute();
305 new MemoryUpdater().execute();
309 public void onPause() {
310 getLoaderManager().destroyLoader(LOADER_CHART_DATA);
315 public void onDestroy() {
316 TrafficStats.closeQuietly(mStatsSession);
317 if (mPermissionReceiver != null) {
318 getContext().unregisterReceiver(mPermissionReceiver);
319 mPermissionReceiver = null;
325 public void onActivityCreated(Bundle savedInstanceState) {
326 super.onActivityCreated(savedInstanceState);
332 mNotificationPreference = findPreference(KEY_NOTIFICATION);
333 mNotificationPreference.setOnPreferenceClickListener(this);
334 mStoragePreference = findPreference(KEY_STORAGE);
335 mStoragePreference.setOnPreferenceClickListener(this);
336 mPermissionsPreference = findPreference(KEY_PERMISSION);
337 mPermissionsPreference.setOnPreferenceClickListener(this);
338 mDataPreference = findPreference(KEY_DATA);
339 if (mDataPreference != null) {
340 mDataPreference.setOnPreferenceClickListener(this);
342 mBatteryPreference = findPreference(KEY_BATTERY);
343 mBatteryPreference.setEnabled(false);
344 mBatteryPreference.setOnPreferenceClickListener(this);
345 mMemoryPreference = findPreference(KEY_MEMORY);
346 mMemoryPreference.setOnPreferenceClickListener(this);
348 mLaunchPreference = findPreference(KEY_LAUNCH);
349 if (mAppEntry != null && mAppEntry.info != null) {
350 if ((mAppEntry.info.flags&ApplicationInfo.FLAG_INSTALLED) == 0 ||
351 !mAppEntry.info.enabled) {
352 mLaunchPreference.setEnabled(false);
354 mLaunchPreference.setOnPreferenceClickListener(this);
357 mLaunchPreference.setEnabled(false);
361 private void handleHeader() {
362 mHeader = (LayoutPreference) findPreference(KEY_HEADER);
364 // Get Control button panel
365 View btnPanel = mHeader.findViewById(R.id.control_buttons_panel);
366 mForceStopButton = (Button) btnPanel.findViewById(R.id.right_button);
367 mForceStopButton.setText(R.string.force_stop);
368 mUninstallButton = (Button) btnPanel.findViewById(R.id.left_button);
369 mForceStopButton.setEnabled(false);
373 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
374 menu.add(0, UNINSTALL_UPDATES, 0, R.string.app_factory_reset)
375 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
376 menu.add(0, UNINSTALL_ALL_USERS_MENU, 1, R.string.uninstall_all_users_text)
377 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
381 public void onPrepareOptionsMenu(Menu menu) {
385 boolean showIt = true;
386 if (mUpdatedSysApp) {
388 } else if (mAppEntry == null) {
390 } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
392 } else if (mPackageInfo == null || mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
394 } else if (UserHandle.myUserId() != 0) {
396 } else if (mUserManager.getUsers().size() < 2) {
399 menu.findItem(UNINSTALL_ALL_USERS_MENU).setVisible(showIt);
400 mUpdatedSysApp = (mAppEntry.info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
401 menu.findItem(UNINSTALL_UPDATES).setVisible(mUpdatedSysApp && !mAppControlRestricted);
405 public boolean onOptionsItemSelected(MenuItem item) {
406 switch (item.getItemId()) {
407 case UNINSTALL_ALL_USERS_MENU:
408 uninstallPkg(mAppEntry.info.packageName, true, false);
410 case UNINSTALL_UPDATES:
411 showDialogInner(DLG_FACTORY_RESET, 0);
418 public void onActivityResult(int requestCode, int resultCode, Intent data) {
419 super.onActivityResult(requestCode, resultCode, data);
420 if (requestCode == REQUEST_UNINSTALL) {
421 if (mDisableAfterUninstall) {
422 mDisableAfterUninstall = false;
424 ApplicationInfo ainfo = getActivity().getPackageManager().getApplicationInfo(
425 mAppEntry.info.packageName, PackageManager.GET_UNINSTALLED_PACKAGES
426 | PackageManager.GET_DISABLED_COMPONENTS);
427 if ((ainfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0) {
428 new DisableChanger(this, mAppEntry.info,
429 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER)
430 .execute((Object)null);
432 } catch (NameNotFoundException e) {
436 setIntentAndFinish(true, true);
441 // Utility method to set application label and icon.
442 private void setAppLabelAndIcon(PackageInfo pkgInfo) {
443 final View appSnippet = mHeader.findViewById(R.id.app_snippet);
444 mState.ensureIcon(mAppEntry);
445 setupAppSnippet(appSnippet, mAppEntry.label, mAppEntry.icon,
446 pkgInfo != null ? pkgInfo.versionName : null);
449 private boolean signaturesMatch(String pkg1, String pkg2) {
450 if (pkg1 != null && pkg2 != null) {
452 final int match = mPm.checkSignatures(pkg1, pkg2);
453 if (match >= PackageManager.SIGNATURE_MATCH) {
456 } catch (Exception e) {
457 // e.g. named alternate package not found during lookup;
458 // this is an expected case sometimes
465 protected boolean refreshUi() {
467 if (mAppEntry == null) {
468 return false; // onCreate must have failed, make sure to exit
471 if (mPackageInfo == null) {
472 return false; // onCreate must have failed, make sure to exit
475 // Get list of "home" apps and trace through any meta-data references
476 List<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
477 mPm.getHomeActivities(homeActivities);
478 mHomePackages.clear();
479 for (int i = 0; i< homeActivities.size(); i++) {
480 ResolveInfo ri = homeActivities.get(i);
481 final String activityPkg = ri.activityInfo.packageName;
482 mHomePackages.add(activityPkg);
484 // Also make sure to include anything proxying for the home app
485 final Bundle metadata = ri.activityInfo.metaData;
486 if (metadata != null) {
487 final String metaPkg = metadata.getString(ActivityManager.META_HOME_ALTERNATE);
488 if (signaturesMatch(metaPkg, activityPkg)) {
489 mHomePackages.add(metaPkg);
495 setAppLabelAndIcon(mPackageInfo);
496 initUninstallButtons();
498 // Update the preference summaries.
499 Activity context = getActivity();
500 mStoragePreference.setSummary(AppStorageSettings.getSummary(mAppEntry, context));
501 if (mPermissionReceiver != null) {
502 getContext().unregisterReceiver(mPermissionReceiver);
504 mPermissionReceiver = PermissionsSummaryHelper.getPermissionSummary(getContext(),
505 mPackageName, mPermissionCallback);
506 mLaunchPreference.setSummary(Utils.getLaunchByDeafaultSummary(mAppEntry, mUsbManager,
508 mNotificationPreference.setSummary(getNotificationSummary(mAppEntry, context,
510 if (mDataPreference != null) {
511 mDataPreference.setSummary(getDataSummary());
517 // First time init: are we displaying an uninstalled app?
519 mShowUninstalled = (mAppEntry.info.flags&ApplicationInfo.FLAG_INSTALLED) == 0;
521 // All other times: if the app no longer exists then we want
524 ApplicationInfo ainfo = context.getPackageManager().getApplicationInfo(
525 mAppEntry.info.packageName, PackageManager.GET_UNINSTALLED_PACKAGES
526 | PackageManager.GET_DISABLED_COMPONENTS);
527 if (!mShowUninstalled) {
528 // If we did not start out with the app uninstalled, then
529 // it transitioning to the uninstalled state for the current
530 // user means we should go away as well.
531 return (ainfo.flags&ApplicationInfo.FLAG_INSTALLED) != 0;
533 } catch (NameNotFoundException e) {
541 private void updateBattery() {
542 if (mSipper != null) {
543 mBatteryPreference.setEnabled(true);
544 int dischargeAmount = mBatteryHelper.getStats().getDischargeAmount(
545 BatteryStats.STATS_SINCE_CHARGED);
546 final int percentOfMax = (int) ((mSipper.totalPowerMah)
547 / mBatteryHelper.getTotalPower() * dischargeAmount + .5f);
548 mBatteryPreference.setSummary(getString(R.string.battery_summary, percentOfMax));
550 mBatteryPreference.setEnabled(false);
551 mBatteryPreference.setSummary(getString(R.string.no_battery_summary));
555 private CharSequence getDataSummary() {
556 if (mChartData != null) {
557 long totalBytes = mChartData.detail.getTotalBytes();
558 if (totalBytes == 0) {
559 return getString(R.string.no_data_usage);
561 Context context = getActivity();
562 return getString(R.string.data_summary_format,
563 Formatter.formatFileSize(context, totalBytes),
564 DateUtils.formatDateTime(context, mChartData.detail.getStart(),
565 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH));
567 return getString(R.string.computing_size);
571 protected AlertDialog createDialog(int id, int errorCode) {
574 return new AlertDialog.Builder(getActivity())
575 .setMessage(getActivity().getText(R.string.app_disable_dlg_text))
576 .setPositiveButton(R.string.app_disable_dlg_positive,
577 new DialogInterface.OnClickListener() {
578 public void onClick(DialogInterface dialog, int which) {
580 new DisableChanger(InstalledAppDetails.this, mAppEntry.info,
581 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER)
582 .execute((Object)null);
585 .setNegativeButton(R.string.dlg_cancel, null)
587 case DLG_SPECIAL_DISABLE:
588 return new AlertDialog.Builder(getActivity())
589 .setMessage(getActivity().getText(R.string.app_special_disable_dlg_text))
590 .setPositiveButton(R.string.app_disable_dlg_positive,
591 new DialogInterface.OnClickListener() {
592 public void onClick(DialogInterface dialog, int which) {
593 // Clear user data here
594 uninstallPkg(mAppEntry.info.packageName,
598 .setNegativeButton(R.string.dlg_cancel, null)
601 return new AlertDialog.Builder(getActivity())
602 .setTitle(getActivity().getText(R.string.force_stop_dlg_title))
603 .setMessage(getActivity().getText(R.string.force_stop_dlg_text))
604 .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
605 public void onClick(DialogInterface dialog, int which) {
607 forceStopPackage(mAppEntry.info.packageName);
610 .setNegativeButton(R.string.dlg_cancel, null)
612 case DLG_FACTORY_RESET:
613 return new AlertDialog.Builder(getActivity())
614 .setTitle(getActivity().getText(R.string.app_factory_reset_dlg_title))
615 .setMessage(getActivity().getText(R.string.app_factory_reset_dlg_text))
616 .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
617 public void onClick(DialogInterface dialog, int which) {
618 // Clear user data here
619 uninstallPkg(mAppEntry.info.packageName,
623 .setNegativeButton(R.string.dlg_cancel, null)
629 private void uninstallPkg(String packageName, boolean allUsers, boolean andDisable) {
630 // Create new intent to launch Uninstaller activity
631 Uri packageURI = Uri.parse("package:"+packageName);
632 Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
633 uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, allUsers);
634 startActivityForResult(uninstallIntent, REQUEST_UNINSTALL);
635 mDisableAfterUninstall = andDisable;
638 private void forceStopPackage(String pkgName) {
639 ActivityManager am = (ActivityManager)getActivity().getSystemService(
640 Context.ACTIVITY_SERVICE);
641 am.forceStopPackage(pkgName);
642 int userId = UserHandle.getUserId(mAppEntry.info.uid);
643 mState.invalidatePackage(pkgName, userId);
644 ApplicationsState.AppEntry newEnt = mState.getEntry(pkgName, userId);
645 if (newEnt != null) {
651 private void updateForceStopButton(boolean enabled) {
652 if (mAppControlRestricted) {
653 mForceStopButton.setEnabled(false);
655 mForceStopButton.setEnabled(enabled);
656 mForceStopButton.setOnClickListener(InstalledAppDetails.this);
660 private void checkForceStop() {
661 if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
662 // User can't force stop device admin.
663 updateForceStopButton(false);
664 } else if ((mAppEntry.info.flags&ApplicationInfo.FLAG_STOPPED) == 0) {
665 // If the app isn't explicitly stopped, then always show the
666 // force stop button.
667 updateForceStopButton(true);
669 Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART,
670 Uri.fromParts("package", mAppEntry.info.packageName, null));
671 intent.putExtra(Intent.EXTRA_PACKAGES, new String[] { mAppEntry.info.packageName });
672 intent.putExtra(Intent.EXTRA_UID, mAppEntry.info.uid);
673 intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(mAppEntry.info.uid));
674 getActivity().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
675 mCheckKillProcessesReceiver, null, Activity.RESULT_CANCELED, null, null);
679 private void startManagePermissionsActivity() {
680 // start new activity to manage app permissions
681 Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS);
682 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mAppEntry.info.packageName);
683 intent.putExtra(AppInfoWithHeader.EXTRA_HIDE_INFO_BUTTON, true);
685 startActivity(intent);
686 } catch (ActivityNotFoundException e) {
687 Log.w(LOG_TAG, "No app can handle android.intent.action.MANAGE_APP_PERMISSIONS");
691 private void startAppInfoFragment(Class<?> fragment, CharSequence title) {
692 // start new fragment to display extended information
693 Bundle args = new Bundle();
694 args.putString(ARG_PACKAGE_NAME, mAppEntry.info.packageName);
695 args.putInt(ARG_PACKAGE_UID, mAppEntry.info.uid);
696 args.putBoolean(AppInfoWithHeader.EXTRA_HIDE_INFO_BUTTON, true);
698 SettingsActivity sa = (SettingsActivity) getActivity();
699 sa.startPreferencePanel(fragment.getName(), args, -1, title, this, SUB_INFO_FRAGMENT);
703 * Method implementing functionality of buttons clicked
704 * @see android.view.View.OnClickListener#onClick(android.view.View)
706 public void onClick(View v) {
707 if (mAppEntry == null) {
708 setIntentAndFinish(true, true);
711 String packageName = mAppEntry.info.packageName;
712 if(v == mUninstallButton) {
713 if ((mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
714 if (mAppEntry.info.enabled && !isDisabledUntilUsed()) {
715 if (mUpdatedSysApp) {
716 showDialogInner(DLG_SPECIAL_DISABLE, 0);
718 showDialogInner(DLG_DISABLE, 0);
721 new DisableChanger(this, mAppEntry.info,
722 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
723 .execute((Object) null);
725 } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
726 uninstallPkg(packageName, true, false);
728 uninstallPkg(packageName, false, false);
730 } else if (v == mForceStopButton) {
731 showDialogInner(DLG_FORCE_STOP, 0);
732 //forceStopPackage(mAppInfo.packageName);
737 public boolean onPreferenceClick(Preference preference) {
738 if (preference == mStoragePreference) {
739 startAppInfoFragment(AppStorageSettings.class, mStoragePreference.getTitle());
740 } else if (preference == mNotificationPreference) {
741 startAppInfoFragment(AppNotificationSettings.class,
742 getString(R.string.app_notifications_title));
743 } else if (preference == mPermissionsPreference) {
744 startManagePermissionsActivity();
745 } else if (preference == mLaunchPreference) {
746 startAppInfoFragment(AppLaunchSettings.class, mLaunchPreference.getTitle());
747 } else if (preference == mMemoryPreference) {
748 ProcessStatsBase.launchMemoryDetail((SettingsActivity) getActivity(),
749 mStatsManager.getMemInfo(), mStats);
750 } else if (preference == mDataPreference) {
751 Bundle args = new Bundle();
752 args.putString(DataUsageSummary.EXTRA_SHOW_APP_IMMEDIATE_PKG,
753 mAppEntry.info.packageName);
755 SettingsActivity sa = (SettingsActivity) getActivity();
756 sa.startPreferencePanel(DataUsageSummary.class.getName(), args, -1,
757 getString(R.string.app_data_usage), this, SUB_INFO_FRAGMENT);
758 } else if (preference == mBatteryPreference) {
759 BatteryEntry entry = new BatteryEntry(getActivity(), null, mUserManager, mSipper);
760 PowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(),
761 mBatteryHelper, BatteryStats.STATS_SINCE_CHARGED, entry, true);
768 public static void setupAppSnippet(View appSnippet, CharSequence label, Drawable icon,
769 CharSequence versionName) {
770 LayoutInflater.from(appSnippet.getContext()).inflate(R.layout.widget_text_views,
771 (ViewGroup) appSnippet.findViewById(android.R.id.widget_frame));
773 ImageView iconView = (ImageView) appSnippet.findViewById(android.R.id.icon);
774 iconView.setImageDrawable(icon);
775 // Set application name.
776 TextView labelView = (TextView) appSnippet.findViewById(android.R.id.title);
777 labelView.setText(label);
778 // Version number of application
779 TextView appVersion = (TextView) appSnippet.findViewById(R.id.widget_text1);
781 if (!TextUtils.isEmpty(versionName)) {
782 appVersion.setSelected(true);
783 appVersion.setVisibility(View.VISIBLE);
784 appVersion.setText(appSnippet.getContext().getString(R.string.version_text,
785 String.valueOf(versionName)));
787 appVersion.setVisibility(View.INVISIBLE);
791 private static NetworkTemplate getTemplate(Context context) {
792 if (DataUsageSummary.hasReadyMobileRadio(context)) {
793 return NetworkTemplate.buildTemplateMobileWildcard();
795 if (DataUsageSummary.hasWifiRadio(context)) {
796 return NetworkTemplate.buildTemplateWifiWildcard();
798 return NetworkTemplate.buildTemplateEthernet();
801 public static CharSequence getNotificationSummary(AppEntry appEntry, Context context) {
802 return getNotificationSummary(appEntry, context, new NotificationBackend());
805 public static CharSequence getNotificationSummary(AppEntry appEntry, Context context,
806 NotificationBackend backend) {
807 AppRow appRow = backend.loadAppRow(context.getPackageManager(), appEntry.info);
808 return getNotificationSummary(appRow, context);
811 public static CharSequence getNotificationSummary(AppRow appRow, Context context) {
813 return context.getString(R.string.notifications_disabled);
815 ArrayList<CharSequence> notifSummary = new ArrayList<>();
816 if (appRow.priority) {
817 notifSummary.add(context.getString(R.string.notifications_priority));
819 if (appRow.sensitive) {
820 notifSummary.add(context.getString(R.string.notifications_sensitive));
822 if (!appRow.peekable) {
823 notifSummary.add(context.getString(R.string.notifications_no_peeking));
825 switch (notifSummary.size()) {
827 return context.getString(R.string.notifications_three_items,
828 notifSummary.get(0), notifSummary.get(1), notifSummary.get(2));
830 return context.getString(R.string.notifications_two_items,
831 notifSummary.get(0), notifSummary.get(1));
833 return notifSummary.get(0);
835 return context.getString(R.string.notifications_enabled);
839 private class MemoryUpdater extends AsyncTask<Void, Void, ProcStatsPackageEntry> {
842 protected ProcStatsPackageEntry doInBackground(Void... params) {
843 if (getActivity() == null) {
846 if (mPackageInfo == null) {
849 if (mStatsManager == null) {
850 mStatsManager = new ProcStatsData(getActivity(), false);
851 mStatsManager.setDuration(ProcessStatsBase.sDurations[0]);
853 mStatsManager.refreshStats(true);
854 for (ProcStatsPackageEntry pkgEntry : mStatsManager.getEntries()) {
855 for (ProcStatsEntry entry : pkgEntry.mEntries) {
856 if (entry.mUid == mPackageInfo.applicationInfo.uid) {
857 pkgEntry.updateMetrics();
866 protected void onPostExecute(ProcStatsPackageEntry entry) {
867 if (getActivity() == null) {
872 mMemoryPreference.setEnabled(true);
873 double amount = Math.max(entry.mRunWeight, entry.mBgWeight)
874 * mStatsManager.getMemInfo().weightToRam;
875 mMemoryPreference.setSummary(getString(R.string.memory_use_summary,
876 Formatter.formatShortFileSize(getContext(), (long) amount)));
878 mMemoryPreference.setEnabled(false);
879 mMemoryPreference.setSummary(getString(R.string.no_memory_use_summary));
885 private class BatteryUpdater extends AsyncTask<Void, Void, Void> {
887 protected Void doInBackground(Void... params) {
888 mBatteryHelper.create((Bundle) null);
889 mBatteryHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED,
890 mUserManager.getUserProfiles());
891 List<BatterySipper> usageList = mBatteryHelper.getUsageList();
892 final int N = usageList.size();
893 for (int i = 0; i < N; i++) {
894 BatterySipper sipper = usageList.get(i);
895 if (sipper.getUid() == mPackageInfo.applicationInfo.uid) {
904 protected void onPostExecute(Void result) {
905 if (getActivity() == null) {
912 private static class DisableChanger extends AsyncTask<Object, Object, Object> {
913 final PackageManager mPm;
914 final WeakReference<InstalledAppDetails> mActivity;
915 final ApplicationInfo mInfo;
918 DisableChanger(InstalledAppDetails activity, ApplicationInfo info, int state) {
920 mActivity = new WeakReference<InstalledAppDetails>(activity);
926 protected Object doInBackground(Object... params) {
927 mPm.setApplicationEnabledSetting(mInfo.packageName, mState, 0);
932 private final LoaderCallbacks<ChartData> mDataCallbacks = new LoaderCallbacks<ChartData>() {
935 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
936 return new ChartDataLoader(getActivity(), mStatsSession, args);
940 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
942 mDataPreference.setSummary(getDataSummary());
946 public void onLoaderReset(Loader<ChartData> loader) {
947 // Leave last result.
951 private final BroadcastReceiver mCheckKillProcessesReceiver = new BroadcastReceiver() {
953 public void onReceive(Context context, Intent intent) {
954 updateForceStopButton(getResultCode() != Activity.RESULT_CANCELED);
958 private final PermissionsResultCallback mPermissionCallback
959 = new PermissionsResultCallback() {
961 public void onPermissionSummaryResult(int[] counts, CharSequence[] groupLabels) {
962 if (getActivity() == null) {
965 mPermissionReceiver = null;
966 final Resources res = getResources();
967 CharSequence summary = null;
968 if (counts != null) {
969 int totalCount = counts[1];
970 int additionalCounts = counts[2];
972 if (totalCount == 0) {
973 summary = res.getString(
974 R.string.runtime_permissions_summary_no_permissions_requested);
976 final ArrayList<CharSequence> list = new ArrayList(Arrays.asList(groupLabels));
977 if (additionalCounts > 0) {
978 // N additional permissions.
979 list.add(res.getQuantityString(
980 R.plurals.runtime_permissions_additional_count,
981 additionalCounts, additionalCounts));
983 if (list.size() == 0) {
984 summary = res.getString(
985 R.string.runtime_permissions_summary_no_permissions_granted);
987 summary = ListFormatter.getInstance().format(list);
991 mPermissionsPreference.setSummary(summary);