OSDN Git Service

efeade10b39ed02f252e138596d78726b51f2d39
[android-x86/packages-apps-Settings.git] / src / com / android / settings / applications / InstalledAppDetails.java
1 /**
2  * Copyright (C) 2007 The Android Open Source Project
3  *
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
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16
17 package com.android.settings.applications;
18
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;
77
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;
104
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;
110
111 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
112
113 /**
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.
121  */
122 public class InstalledAppDetails extends AppInfoBase
123         implements View.OnClickListener, OnPreferenceClickListener {
124
125     private static final String LOG_TAG = "InstalledAppDetails";
126
127     // Menu identifiers
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;
131
132     // Result code identifiers
133     public static final int REQUEST_UNINSTALL = 0;
134     private static final int REQUEST_REMOVE_DEVICE_ADMIN = 1;
135
136     private static final int SUB_INFO_FRAGMENT = 1;
137     public static final int REQUEST_TOGGLE_PROTECTION = 3;
138
139     private static final int LOADER_CHART_DATA = 2;
140
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;
144
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";
153
154     private static final String NOTIFICATION_TUNER_SETTING = "show_importance_slider";
155
156     private final HashSet<String> mHomePackages = new HashSet<String>();
157
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;
170
171     private boolean mDisableAfterUninstall;
172     // Used for updating notification preference.
173     private final NotificationBackend mBackend = new NotificationBackend();
174
175     private ChartData mChartData;
176     private INetworkStatsSession mStatsSession;
177
178     private Preference mBatteryPreference;
179
180     private BatteryStatsHelper mBatteryHelper;
181     private BatterySipper mSipper;
182
183     protected ProcStatsData mStatsManager;
184     protected ProcStatsPackageEntry mStats;
185
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);
197             disableable = true;
198         } else {
199             button.setText(R.string.enable_text);
200             disableable = true;
201         }
202
203         return disableable;
204     }
205
206     private boolean isDisabledUntilUsed() {
207         return mAppEntry.info.enabledSetting
208                 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
209     }
210
211     private void initUninstallButtons() {
212         final boolean isBundled = (mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
213         boolean enabled = true;
214         if (isBundled) {
215             enabled = handleDisableable(mUninstallButton);
216         } else {
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.
221                 enabled = false;
222             }
223             mUninstallButton.setText(R.string.uninstall_text);
224         }
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)) {
228             enabled = false;
229         }
230
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)) {
235             enabled = false;
236         }
237
238         // If the uninstall intent is already queued, disable the uninstall button
239         if (mDpm.isUninstallInQueue(mPackageName)) {
240             enabled = false;
241         }
242
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)) {
250             if (isBundled) {
251                 enabled = false;
252             } else {
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);
259                 } else {
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());
263                 }
264             }
265         }
266
267         if (mAppsControlDisallowedBySystem) {
268             enabled = false;
269         }
270
271         try {
272             IWebViewUpdateService webviewUpdateService =
273                 IWebViewUpdateService.Stub.asInterface(ServiceManager.getService("webviewupdate"));
274             if (webviewUpdateService.isFallbackPackage(mAppEntry.info.packageName)) {
275                 enabled = false;
276             }
277         } catch (RemoteException e) {
278             throw new RuntimeException(e);
279         }
280
281         // This is a protected app component.
282         // You cannot a uninstall a protected component
283         if (mPackageInfo.applicationInfo.protect) {
284             enabled = false;
285         }
286
287         mUninstallButton.setEnabled(enabled);
288         if (enabled) {
289             // Register listener
290             mUninstallButton.setOnClickListener(this);
291         }
292     }
293
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)) {
300             return true;
301         }
302         for (UserInfo userInfo : userInfos) {
303             ComponentName cn = dpm.getProfileOwnerAsUser(userInfo.id);
304             if (cn != null && cn.getPackageName().equals(packageName)) {
305                 return true;
306             }
307         }
308         return false;
309     }
310
311     /** Called when the activity is first created. */
312     @Override
313     public void onCreate(Bundle icicle) {
314         super.onCreate(icicle);
315
316         setHasOptionsMenu(true);
317         addPreferencesFromResource(R.xml.installed_app_details);
318         addDynamicPrefs();
319
320         if (Utils.isBandwidthControlEnabled()) {
321             INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
322                     ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
323             try {
324                 mStatsSession = statsService.openSession();
325             } catch (RemoteException e) {
326                 throw new RuntimeException(e);
327             }
328         } else {
329             removePreference(KEY_DATA);
330         }
331         mBatteryHelper = new BatteryStatsHelper(getActivity(), true);
332     }
333
334     @Override
335     protected int getMetricsCategory() {
336         return MetricsEvent.APPLICATIONS_INSTALLED_APP_DETAILS;
337     }
338
339     @Override
340     public void onResume() {
341         super.onResume();
342         if (mFinishing) {
343             return;
344         }
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),
351                     mDataCallbacks);
352         }
353         new BatteryUpdater().execute();
354         new MemoryUpdater().execute();
355         updateDynamicPrefs();
356     }
357
358     @Override
359     public void onPause() {
360         getLoaderManager().destroyLoader(LOADER_CHART_DATA);
361         super.onPause();
362     }
363
364     @Override
365     public void onDestroy() {
366         TrafficStats.closeQuietly(mStatsSession);
367         super.onDestroy();
368     }
369
370     public void onActivityCreated(Bundle savedInstanceState) {
371         super.onActivityCreated(savedInstanceState);
372         if (mFinishing) {
373             return;
374         }
375         handleHeader();
376
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);
386         }
387         mBatteryPreference = findPreference(KEY_BATTERY);
388         mBatteryPreference.setEnabled(false);
389         mBatteryPreference.setOnPreferenceClickListener(this);
390         mMemoryPreference = findPreference(KEY_MEMORY);
391         mMemoryPreference.setOnPreferenceClickListener(this);
392
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);
398             } else {
399                 mLaunchPreference.setOnPreferenceClickListener(this);
400             }
401         } else {
402             mLaunchPreference.setEnabled(false);
403         }
404     }
405
406     private void handleHeader() {
407         mHeader = (LayoutPreference) findPreference(KEY_HEADER);
408
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);
415
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() {
423                 @Override
424                 public void onClick(View v) {
425                     startActivity(intent);
426                 }
427             });
428         } else {
429             gear.setVisibility(View.GONE);
430         }
431     }
432
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;
437     }
438
439     @Override
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);
448     }
449
450     @Override
451     public void onPrepareOptionsMenu(Menu menu) {
452         if (mFinishing) {
453             return;
454         }
455         boolean showIt = true;
456         if (mUpdatedSysApp) {
457             showIt = false;
458         } else if (mAppEntry == null) {
459             showIt = false;
460         } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
461             showIt = false;
462         } else if (mPackageInfo == null || mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
463             showIt = false;
464         } else if (UserHandle.myUserId() != 0) {
465             showIt = false;
466         } else if (mUserManager.getUsers().size() < 2) {
467             showIt = false;
468         }
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);
476         }
477
478         menu.findItem(OPEN_PROTECTED_APPS).setVisible(mPackageInfo.applicationInfo.protect);
479     }
480
481     @Override
482     public boolean onOptionsItemSelected(MenuItem item) {
483         switch (item.getItemId()) {
484             case UNINSTALL_ALL_USERS_MENU:
485                 uninstallPkg(mAppEntry.info.packageName, true, false);
486                 return true;
487             case UNINSTALL_UPDATES:
488                 uninstallPkg(mAppEntry.info.packageName, false, false);
489                 return true;
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);
494         }
495         return false;
496     }
497
498     @Override
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);
507             }
508             if (!refreshUi()) {
509                 setIntentAndFinish(true, true);
510             }
511         }
512         if (requestCode == REQUEST_REMOVE_DEVICE_ADMIN) {
513             if (!refreshUi()) {
514                 setIntentAndFinish(true, true);
515             }
516         }
517         if (requestCode == REQUEST_TOGGLE_PROTECTION) {
518             switch (resultCode) {
519                 case Activity.RESULT_OK:
520                     new ToggleProtectedAppComponents().execute();
521                     break;
522                 case Activity.RESULT_CANCELED:
523                     // User failed to enter/confirm a lock pattern, do nothing
524                     break;
525             }
526         }
527     }
528
529     private class ToggleProtectedAppComponents extends AsyncTask<Void, Void, Void> {
530         @Override
531         protected void onPostExecute(Void aVoid) {
532             getActivity().invalidateOptionsMenu();
533             if (!refreshUi()) {
534                 setIntentAndFinish(true, true);
535             }
536         }
537
538         @Override
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));
543             }
544
545             ProtectedAppsReceiver.updateProtectedAppComponentsAndNotify(getActivity(),
546                     components, PackageManager.COMPONENT_VISIBLE_STATUS);
547             return null;
548         }
549     }
550
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);
557     }
558
559     private boolean signaturesMatch(String pkg1, String pkg2) {
560         if (pkg1 != null && pkg2 != null) {
561             try {
562                 final int match = mPm.checkSignatures(pkg1, pkg2);
563                 if (match >= PackageManager.SIGNATURE_MATCH) {
564                     return true;
565                 }
566             } catch (Exception e) {
567                 // e.g. named alternate package not found during lookup;
568                 // this is an expected case sometimes
569             }
570         }
571         return false;
572     }
573
574     @Override
575     protected boolean refreshUi() {
576         retrieveAppEntry();
577         if (mAppEntry == null) {
578             return false; // onCreate must have failed, make sure to exit
579         }
580
581         if (mPackageInfo == null) {
582             return false; // onCreate must have failed, make sure to exit
583         }
584
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);
593
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);
600                 }
601             }
602         }
603
604         checkForceStop();
605         setAppLabelAndIcon(mPackageInfo);
606         initUninstallButtons();
607
608         // Update the preference summaries.
609         Activity context = getActivity();
610         mStoragePreference.setSummary(AppStorageSettings.getSummary(mAppEntry, context));
611
612         PermissionsSummaryHelper.getPermissionSummary(getContext(),
613                 mPackageName, mPermissionCallback);
614         mLaunchPreference.setSummary(AppUtils.getLaunchByDefaultSummary(mAppEntry, mUsbManager,
615                 mPm, context));
616         mNotificationPreference.setSummary(getNotificationSummary(mAppEntry, context,
617                 mBackend));
618         if (mDataPreference != null) {
619             mDataPreference.setSummary(getDataSummary());
620         }
621
622         updateBattery();
623
624         if (!mInitialized) {
625             // First time init: are we displaying an uninstalled app?
626             mInitialized = true;
627             mShowUninstalled = (mAppEntry.info.flags&ApplicationInfo.FLAG_INSTALLED) == 0;
628         } else {
629             // All other times: if the app no longer exists then we want
630             // to go away.
631             try {
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;
640                 }
641             } catch (NameNotFoundException e) {
642                 return false;
643             }
644         }
645
646         return true;
647     }
648
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));
657         } else {
658             mBatteryPreference.setEnabled(false);
659             mBatteryPreference.setSummary(getString(R.string.no_battery_summary));
660         }
661     }
662
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);
668             }
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));
674         }
675         return getString(R.string.computing_size);
676     }
677
678     @Override
679     protected AlertDialog createDialog(int id, int errorCode) {
680         switch (id) {
681             case DLG_DISABLE:
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) {
687                                 // Disable the app
688                                 new DisableChanger(InstalledAppDetails.this, mAppEntry.info,
689                                         PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER)
690                                 .execute((Object)null);
691                             }
692                         })
693                         .setNegativeButton(R.string.dlg_cancel, null)
694                         .create();
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,
703                                         false, true);
704                             }
705                         })
706                         .setNegativeButton(R.string.dlg_cancel, null)
707                         .create();
708             case DLG_FORCE_STOP:
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) {
714                                 // Force stop
715                                 forceStopPackage(mAppEntry.info.packageName);
716                             }
717                         })
718                         .setNegativeButton(R.string.dlg_cancel, null)
719                         .create();
720         }
721         return null;
722     }
723
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;
731     }
732
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) {
741             mAppEntry = newEnt;
742         }
743         checkForceStop();
744     }
745
746     private void updateForceStopButton(boolean enabled) {
747         if (mAppsControlDisallowedBySystem) {
748             mForceStopButton.setEnabled(false);
749         } else {
750             mForceStopButton.setEnabled(enabled);
751             mForceStopButton.setOnClickListener(InstalledAppDetails.this);
752         }
753     }
754
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);
763         } else {
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);
771         }
772     }
773
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);
779         try {
780             startActivity(intent);
781         } catch (ActivityNotFoundException e) {
782             Log.w(LOG_TAG, "No app can handle android.intent.action.MANAGE_APP_PERMISSIONS");
783         }
784     }
785
786     private void startAppInfoFragment(Class<?> fragment, CharSequence title) {
787         startAppInfoFragment(fragment, title, this, mAppEntry);
788     }
789
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);
797
798         SettingsActivity sa = (SettingsActivity) caller.getActivity();
799         sa.startPreferencePanel(fragment.getName(), args, -1, title, caller, SUB_INFO_FRAGMENT);
800     }
801
802     /*
803      * Method implementing functionality of buttons clicked
804      * @see android.view.View.OnClickListener#onClick(android.view.View)
805      */
806     public void onClick(View v) {
807         if (mAppEntry == null) {
808             setIntentAndFinish(true, true);
809             return;
810         }
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,
817                         mPackageName);
818                 activity.startActivityForResult(uninstallDAIntent, REQUEST_REMOVE_DEVICE_ADMIN);
819                 return;
820             }
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);
834                     } else {
835                         showDialogInner(DLG_DISABLE, 0);
836                     }
837                 } else {
838                     new DisableChanger(this, mAppEntry.info,
839                             PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
840                                     .execute((Object) null);
841                 }
842             } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
843                 uninstallPkg(packageName, true, false);
844             } else {
845                 uninstallPkg(packageName, false, false);
846             }
847         } else if (v == mForceStopButton) {
848             if (mAppsControlDisallowedAdmin != null && !mAppsControlDisallowedBySystem) {
849                 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
850                         getActivity(), mAppsControlDisallowedAdmin);
851             } else {
852                 showDialogInner(DLG_FORCE_STOP, 0);
853                 //forceStopPackage(mAppInfo.packageName);
854             }
855         }
856     }
857
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);
863     }
864
865     @Override
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);
885         } else {
886             return false;
887         }
888         return true;
889     }
890
891     private void addDynamicPrefs() {
892         if (Utils.isManagedProfile(UserManager.get(getContext()))) {
893             return;
894         }
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));
900         }
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));
905         }
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));
910         }
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));
915         }
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));
920         }
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);
927
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() {
933                     @Override
934                     public boolean onPreferenceClick(Preference preference) {
935                         startAppInfoFragment(DrawOverlayDetails.class,
936                                 getString(R.string.draw_overlay));
937                         return true;
938                     }
939                 });
940                 category.addPreference(pref);
941             }
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() {
947                     @Override
948                     public boolean onPreferenceClick(Preference preference) {
949                         startAppInfoFragment(WriteSettingsDetails.class,
950                                 getString(R.string.write_settings));
951                         return true;
952                     }
953                 });
954                 category.addPreference(pref);
955             }
956         }
957
958         addAppInstallerInfoPref(screen);
959     }
960
961     private void addAppInstallerInfoPref(PreferenceScreen screen) {
962         String installerPackageName = null;
963         try {
964             installerPackageName =
965                     getContext().getPackageManager().getInstallerPackageName(mPackageName);
966         } catch (IllegalArgumentException e) {
967             Log.e(TAG, "Exception while retrieving the package installer of " + mPackageName, e);
968         }
969         if (installerPackageName == null) {
970             return;
971         }
972         final CharSequence installerLabel = Utils.getApplicationLabel(getContext(),
973                 installerPackageName);
974         if (installerLabel == null) {
975             return;
976         }
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);
990         } else {
991             pref.setEnabled(false);
992         }
993         category.addPreference(pref);
994     }
995
996     private boolean hasPermission(String permission) {
997         if (mPackageInfo == null || mPackageInfo.requestedPermissions == null) {
998             return false;
999         }
1000         for (int i = 0; i < mPackageInfo.requestedPermissions.length; i++) {
1001             if (mPackageInfo.requestedPermissions[i].equals(permission)) {
1002                 return true;
1003             }
1004         }
1005         return false;
1006     }
1007
1008     private void updateDynamicPrefs() {
1009         Preference pref = findPreference("default_home");
1010         if (pref != null) {
1011             pref.setSummary(DefaultHomePreference.isHomeDefault(mPackageName, getContext())
1012                     ? R.string.yes : R.string.no);
1013         }
1014         pref = findPreference("default_browser");
1015         if (pref != null) {
1016             pref.setSummary(DefaultBrowserPreference.isBrowserDefault(mPackageName, getContext())
1017                     ? R.string.yes : R.string.no);
1018         }
1019         pref = findPreference("default_phone_app");
1020         if (pref != null) {
1021             pref.setSummary(DefaultPhonePreference.isPhoneDefault(mPackageName, getContext())
1022                     ? R.string.yes : R.string.no);
1023         }
1024         pref = findPreference("default_emergency_app");
1025         if (pref != null) {
1026             pref.setSummary(DefaultEmergencyPreference.isEmergencyDefault(mPackageName,
1027                     getContext()) ? R.string.yes : R.string.no);
1028         }
1029         pref = findPreference("default_sms_app");
1030         if (pref != null) {
1031             pref.setSummary(DefaultSmsPreference.isSmsDefault(mPackageName, getContext())
1032                     ? R.string.yes : R.string.no);
1033         }
1034         pref = findPreference("system_alert_window");
1035         if (pref != null) {
1036             pref.setSummary(DrawOverlayDetails.getSummary(getContext(), mAppEntry));
1037         }
1038         pref = findPreference("write_settings_apps");
1039         if (pref != null) {
1040             pref.setSummary(WriteSettingsDetails.getSummary(getContext(), mAppEntry));
1041         }
1042     }
1043
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));
1048
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);
1056
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)));
1062         } else {
1063             appVersion.setVisibility(View.INVISIBLE);
1064         }
1065     }
1066
1067     public static NetworkTemplate getTemplate(Context context) {
1068         if (DataUsageList.hasReadyMobileRadio(context)) {
1069             return NetworkTemplate.buildTemplateMobileWildcard();
1070         }
1071         if (DataUsageSummary.hasWifiRadio(context)) {
1072             return NetworkTemplate.buildTemplateWifiWildcard();
1073         }
1074         return NetworkTemplate.buildTemplateEthernet();
1075     }
1076
1077     public static CharSequence getNotificationSummary(AppEntry appEntry, Context context) {
1078         return getNotificationSummary(appEntry, context, new NotificationBackend());
1079     }
1080
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);
1085     }
1086
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();
1092         if (showSlider) {
1093             if (appRow.appImportance != Ranking.IMPORTANCE_UNSPECIFIED) {
1094                 summaryAttributes.add(context.getString(
1095                         R.string.notification_summary_level, appRow.appImportance));
1096             }
1097         } else {
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));
1103             }
1104         }
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));
1112             }
1113         }
1114         if (appRow.appBypassDnd) {
1115             summaryAttributes.add(context.getString(R.string.notifications_priority));
1116         }
1117         final int N = summaryAttributes.size();
1118         for (int i = 0; i < N; i++) {
1119             if (i > 0) {
1120                 summary.append(context.getString(R.string.notifications_summary_divider));
1121             }
1122             summary.append(summaryAttributes.get(i));
1123         }
1124         return summary.toString();
1125     }
1126
1127     private class MemoryUpdater extends AsyncTask<Void, Void, ProcStatsPackageEntry> {
1128
1129         @Override
1130         protected ProcStatsPackageEntry doInBackground(Void... params) {
1131             if (getActivity() == null) {
1132                 return null;
1133             }
1134             if (mPackageInfo == null) {
1135                 return null;
1136             }
1137             if (mStatsManager == null) {
1138                 mStatsManager = new ProcStatsData(getActivity(), false);
1139                 mStatsManager.setDuration(ProcessStatsBase.sDurations[0]);
1140             }
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();
1146                         return pkgEntry;
1147                     }
1148                 }
1149             }
1150             return null;
1151         }
1152
1153         @Override
1154         protected void onPostExecute(ProcStatsPackageEntry entry) {
1155             if (getActivity() == null) {
1156                 return;
1157             }
1158             if (entry != null) {
1159                 mStats = entry;
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)));
1165             } else {
1166                 mMemoryPreference.setEnabled(false);
1167                 mMemoryPreference.setSummary(getString(R.string.no_memory_use_summary));
1168             }
1169         }
1170
1171     }
1172
1173     private class BatteryUpdater extends AsyncTask<Void, Void, Void> {
1174         @Override
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) {
1184                     mSipper = sipper;
1185                     break;
1186                 }
1187             }
1188             return null;
1189         }
1190
1191         @Override
1192         protected void onPostExecute(Void result) {
1193             if (getActivity() == null) {
1194                 return;
1195             }
1196             refreshUi();
1197         }
1198     }
1199
1200     private static class DisableChanger extends AsyncTask<Object, Object, Object> {
1201         final PackageManager mPm;
1202         final WeakReference<InstalledAppDetails> mActivity;
1203         final ApplicationInfo mInfo;
1204         final int mState;
1205
1206         DisableChanger(InstalledAppDetails activity, ApplicationInfo info, int state) {
1207             mPm = activity.mPm;
1208             mActivity = new WeakReference<InstalledAppDetails>(activity);
1209             mInfo = info;
1210             mState = state;
1211         }
1212
1213         @Override
1214         protected Object doInBackground(Object... params) {
1215             mPm.setApplicationEnabledSetting(mInfo.packageName, mState, 0);
1216             return null;
1217         }
1218     }
1219
1220     private final LoaderCallbacks<ChartData> mDataCallbacks = new LoaderCallbacks<ChartData>() {
1221
1222         @Override
1223         public Loader<ChartData> onCreateLoader(int id, Bundle args) {
1224             return new ChartDataLoader(getActivity(), mStatsSession, args);
1225         }
1226
1227         @Override
1228         public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1229             mChartData = data;
1230             mDataPreference.setSummary(getDataSummary());
1231         }
1232
1233         @Override
1234         public void onLoaderReset(Loader<ChartData> loader) {
1235             // Leave last result.
1236         }
1237     };
1238
1239     private final BroadcastReceiver mCheckKillProcessesReceiver = new BroadcastReceiver() {
1240         @Override
1241         public void onReceive(Context context, Intent intent) {
1242             updateForceStopButton(getResultCode() != Activity.RESULT_CANCELED);
1243         }
1244     };
1245
1246     private final PermissionsResultCallback mPermissionCallback
1247             = new PermissionsResultCallback() {
1248         @Override
1249         public void onPermissionSummaryResult(int standardGrantedPermissionCount,
1250                 int requestedPermissionCount, int additionalGrantedPermissionCount,
1251                 List<CharSequence> grantedGroupLabels) {
1252             if (getActivity() == null) {
1253                 return;
1254             }
1255             final Resources res = getResources();
1256             CharSequence summary = null;
1257
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);
1263             } else {
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));
1270                 }
1271                 if (list.size() == 0) {
1272                     summary = res.getString(
1273                             R.string.runtime_permissions_summary_no_permissions_granted);
1274                 } else {
1275                     summary = ListFormatter.getInstance().format(list);
1276                 }
1277                 mPermissionsPreference.setOnPreferenceClickListener(InstalledAppDetails.this);
1278                 mPermissionsPreference.setEnabled(true);
1279             }
1280             mPermissionsPreference.setSummary(summary);
1281         }
1282     };
1283 }