OSDN Git Service

Merge "Clear only keystore credential entires" into mnc-dev
[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.app.Activity;
20 import android.app.ActivityManager;
21 import android.app.AlertDialog;
22 import android.app.LoaderManager.LoaderCallbacks;
23 import android.content.ActivityNotFoundException;
24 import android.content.BroadcastReceiver;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.DialogInterface;
28 import android.content.Intent;
29 import android.content.Loader;
30 import android.content.pm.ApplicationInfo;
31 import android.content.pm.PackageInfo;
32 import android.content.pm.PackageManager;
33 import android.content.pm.PackageManager.NameNotFoundException;
34 import android.content.pm.ResolveInfo;
35 import android.net.INetworkStatsService;
36 import android.net.INetworkStatsSession;
37 import android.net.NetworkTemplate;
38 import android.net.TrafficStats;
39 import android.net.Uri;
40 import android.os.AsyncTask;
41 import android.os.BatteryStats;
42 import android.os.Bundle;
43 import android.os.RemoteException;
44 import android.os.ServiceManager;
45 import android.os.UserHandle;
46 import android.preference.Preference;
47 import android.preference.Preference.OnPreferenceClickListener;
48 import android.text.format.DateUtils;
49 import android.text.format.Formatter;
50 import android.util.Log;
51 import android.view.Menu;
52 import android.view.MenuInflater;
53 import android.view.MenuItem;
54 import android.view.View;
55 import android.widget.Button;
56 import android.widget.ImageView;
57 import android.widget.TextView;
58
59 import com.android.internal.logging.MetricsLogger;
60 import com.android.internal.os.BatterySipper;
61 import com.android.internal.os.BatteryStatsHelper;
62 import com.android.settings.DataUsageSummary;
63 import com.android.settings.DataUsageSummary.AppItem;
64 import com.android.settings.R;
65 import com.android.settings.SettingsActivity;
66 import com.android.settings.Utils;
67 import com.android.settings.applications.ApplicationsState.AppEntry;
68 import com.android.settings.applications.PermissionsSummaryHelper.PermissionsResultCallback;
69 import com.android.settings.fuelgauge.BatteryEntry;
70 import com.android.settings.fuelgauge.PowerUsageDetail;
71 import com.android.settings.net.ChartData;
72 import com.android.settings.net.ChartDataLoader;
73 import com.android.settings.notification.AppNotificationSettings;
74 import com.android.settings.notification.NotificationBackend;
75 import com.android.settings.notification.NotificationBackend.AppRow;
76
77 import java.lang.ref.WeakReference;
78 import java.util.ArrayList;
79 import java.util.HashSet;
80 import java.util.List;
81
82 /**
83  * Activity to display application information from Settings. This activity presents
84  * extended information associated with a package like code, data, total size, permissions
85  * used by the application and also the set of default launchable activities.
86  * For system applications, an option to clear user data is displayed only if data size is > 0.
87  * System applications that do not want clear user data do not have this option.
88  * For non-system applications, there is no option to clear data. Instead there is an option to
89  * uninstall the application.
90  */
91 public class InstalledAppDetails extends AppInfoBase
92         implements View.OnClickListener, OnPreferenceClickListener {
93
94     private static final String LOG_TAG = "InstalledAppDetails";
95
96     // Menu identifiers
97     public static final int UNINSTALL_ALL_USERS_MENU = 1;
98     public static final int UNINSTALL_UPDATES = 2;
99
100     // Result code identifiers
101     public static final int REQUEST_UNINSTALL = 0;
102     private static final int SUB_INFO_FRAGMENT = 1;
103
104     private static final int LOADER_CHART_DATA = 2;
105
106     private static final int DLG_FORCE_STOP = DLG_BASE + 1;
107     private static final int DLG_DISABLE = DLG_BASE + 2;
108     private static final int DLG_SPECIAL_DISABLE = DLG_BASE + 3;
109     private static final int DLG_FACTORY_RESET = DLG_BASE + 4;
110
111     private static final String KEY_HEADER = "header_view";
112     private static final String KEY_NOTIFICATION = "notification_settings";
113     private static final String KEY_STORAGE = "storage_settings";
114     private static final String KEY_PERMISSION = "permission_settings";
115     private static final String KEY_DATA = "data_settings";
116     private static final String KEY_LAUNCH = "preferred_settings";
117     private static final String KEY_BATTERY = "battery";
118
119     private final HashSet<String> mHomePackages = new HashSet<String>();
120
121     private boolean mInitialized;
122     private boolean mShowUninstalled;
123     private LayoutPreference mHeader;
124     private Button mUninstallButton;
125     private boolean mUpdatedSysApp = false;
126     private TextView mAppVersion;
127     private Button mForceStopButton;
128     private Preference mNotificationPreference;
129     private Preference mStoragePreference;
130     private Preference mPermissionsPreference;
131     private Preference mLaunchPreference;
132     private Preference mDataPreference;
133
134     private boolean mDisableAfterUninstall;
135     // Used for updating notification preference.
136     private final NotificationBackend mBackend = new NotificationBackend();
137
138     private ChartData mChartData;
139     private INetworkStatsSession mStatsSession;
140
141     private Preference mBatteryPreference;
142
143     private BatteryStatsHelper mBatteryHelper;
144     private BatterySipper mSipper;
145
146     private boolean handleDisableable(Button button) {
147         boolean disableable = false;
148         // Try to prevent the user from bricking their phone
149         // by not allowing disabling of apps signed with the
150         // system cert and any launcher app in the system.
151         if (mHomePackages.contains(mAppEntry.info.packageName)
152                 || Utils.isSystemPackage(mPm, mPackageInfo)) {
153             // Disable button for core system applications.
154             button.setText(R.string.disable_text);
155         } else if (mAppEntry.info.enabled) {
156             button.setText(R.string.disable_text);
157             disableable = true;
158         } else {
159             button.setText(R.string.enable_text);
160             disableable = true;
161         }
162
163         return disableable;
164     }
165
166     private void initUninstallButtons() {
167         final boolean isBundled = (mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
168         boolean enabled = true;
169         if (isBundled) {
170             enabled = handleDisableable(mUninstallButton);
171         } else {
172             if ((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) == 0
173                     && mUserManager.getUsers().size() >= 2) {
174                 // When we have multiple users, there is a separate menu
175                 // to uninstall for all users.
176                 enabled = false;
177             }
178             mUninstallButton.setText(R.string.uninstall_text);
179         }
180         // If this is a device admin, it can't be uninstalled or disabled.
181         // We do this here so the text of the button is still set correctly.
182         if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
183             enabled = false;
184         }
185
186         // Home apps need special handling.  Bundled ones we don't risk downgrading
187         // because that can interfere with home-key resolution.  Furthermore, we
188         // can't allow uninstallation of the only home app, and we don't want to
189         // allow uninstallation of an explicitly preferred one -- the user can go
190         // to Home settings and pick a different one, after which we'll permit
191         // uninstallation of the now-not-default one.
192         if (enabled && mHomePackages.contains(mPackageInfo.packageName)) {
193             if (isBundled) {
194                 enabled = false;
195             } else {
196                 ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
197                 ComponentName currentDefaultHome  = mPm.getHomeActivities(homeActivities);
198                 if (currentDefaultHome == null) {
199                     // No preferred default, so permit uninstall only when
200                     // there is more than one candidate
201                     enabled = (mHomePackages.size() > 1);
202                 } else {
203                     // There is an explicit default home app -- forbid uninstall of
204                     // that one, but permit it for installed-but-inactive ones.
205                     enabled = !mPackageInfo.packageName.equals(currentDefaultHome.getPackageName());
206                 }
207             }
208         }
209
210         if (mAppControlRestricted) {
211             enabled = false;
212         }
213
214         mUninstallButton.setEnabled(enabled);
215         if (enabled) {
216             // Register listener
217             mUninstallButton.setOnClickListener(this);
218         }
219     }
220
221     /** Called when the activity is first created. */
222     @Override
223     public void onCreate(Bundle icicle) {
224         super.onCreate(icicle);
225
226         setHasOptionsMenu(true);
227         addPreferencesFromResource(R.xml.installed_app_details);
228
229         if (Utils.isBandwidthControlEnabled()) {
230             INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
231                     ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
232             try {
233                 mStatsSession = statsService.openSession();
234             } catch (RemoteException e) {
235                 throw new RuntimeException(e);
236             }
237         } else {
238             removePreference(KEY_DATA);
239         }
240         mBatteryHelper = new BatteryStatsHelper(getActivity(), true);
241     }
242
243     @Override
244     protected int getMetricsCategory() {
245         return MetricsLogger.APPLICATIONS_INSTALLED_APP_DETAILS;
246     }
247
248     @Override
249     public void onResume() {
250         super.onResume();
251         if (mFinishing) {
252             return;
253         }
254         mState.requestSize(mPackageName, mUserId);
255         AppItem app = new AppItem(mAppEntry.info.uid);
256         app.addUid(mAppEntry.info.uid);
257         if (mStatsSession != null) {
258             getLoaderManager().restartLoader(LOADER_CHART_DATA,
259                     ChartDataLoader.buildArgs(getTemplate(getContext()), app),
260                     mDataCallbacks);
261         }
262         new BatteryUpdater().execute();
263     }
264
265     @Override
266     public void onPause() {
267         getLoaderManager().destroyLoader(LOADER_CHART_DATA);
268         super.onPause();
269     }
270
271     @Override
272     public void onDestroy() {
273         TrafficStats.closeQuietly(mStatsSession);
274
275         super.onDestroy();
276     }
277
278     public void onActivityCreated(Bundle savedInstanceState) {
279         super.onActivityCreated(savedInstanceState);
280         if (mFinishing) {
281             return;
282         }
283         handleHeader();
284
285         mNotificationPreference = findPreference(KEY_NOTIFICATION);
286         mNotificationPreference.setOnPreferenceClickListener(this);
287         mStoragePreference = findPreference(KEY_STORAGE);
288         mStoragePreference.setOnPreferenceClickListener(this);
289         mPermissionsPreference = findPreference(KEY_PERMISSION);
290         mPermissionsPreference.setOnPreferenceClickListener(this);
291         mDataPreference = findPreference(KEY_DATA);
292         if (mDataPreference != null) {
293             mDataPreference.setOnPreferenceClickListener(this);
294         }
295         mBatteryPreference = findPreference(KEY_BATTERY);
296         mBatteryPreference.setEnabled(false);
297         mBatteryPreference.setOnPreferenceClickListener(this);
298
299         mLaunchPreference = findPreference(KEY_LAUNCH);
300         if (mAppEntry.info != null) {
301             if ((mAppEntry.info.flags&ApplicationInfo.FLAG_INSTALLED) == 0 ||
302                     !mAppEntry.info.enabled) {
303                 mLaunchPreference.setEnabled(false);
304             } else {
305                 mLaunchPreference.setOnPreferenceClickListener(this);
306             }
307         } else {
308             mLaunchPreference.setEnabled(false);
309         }
310     }
311
312     private void handleHeader() {
313         mHeader = (LayoutPreference) findPreference(KEY_HEADER);
314
315         // Get Control button panel
316         View btnPanel = mHeader.findViewById(R.id.control_buttons_panel);
317         mForceStopButton = (Button) btnPanel.findViewById(R.id.right_button);
318         mForceStopButton.setText(R.string.force_stop);
319         mUninstallButton = (Button) btnPanel.findViewById(R.id.left_button);
320         mForceStopButton.setEnabled(false);
321     }
322
323     @Override
324     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
325         menu.add(0, UNINSTALL_UPDATES, 0, R.string.app_factory_reset)
326                 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
327         menu.add(0, UNINSTALL_ALL_USERS_MENU, 1, R.string.uninstall_all_users_text)
328                 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
329     }
330
331     @Override
332     public void onPrepareOptionsMenu(Menu menu) {
333         boolean showIt = true;
334         if (mUpdatedSysApp) {
335             showIt = false;
336         } else if (mAppEntry == null) {
337             showIt = false;
338         } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
339             showIt = false;
340         } else if (mPackageInfo == null || mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
341             showIt = false;
342         } else if (UserHandle.myUserId() != 0) {
343             showIt = false;
344         } else if (mUserManager.getUsers().size() < 2) {
345             showIt = false;
346         }
347         menu.findItem(UNINSTALL_ALL_USERS_MENU).setVisible(showIt);
348         mUpdatedSysApp = (mAppEntry.info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
349         menu.findItem(UNINSTALL_UPDATES).setVisible(mUpdatedSysApp && !mAppControlRestricted);
350     }
351
352     @Override
353     public boolean onOptionsItemSelected(MenuItem item) {
354         switch (item.getItemId()) {
355             case UNINSTALL_ALL_USERS_MENU:
356                 uninstallPkg(mAppEntry.info.packageName, true, false);
357                 return true;
358             case UNINSTALL_UPDATES:
359                 showDialogInner(DLG_FACTORY_RESET, 0);
360                 return true;
361         }
362         return false;
363     }
364
365     @Override
366     public void onActivityResult(int requestCode, int resultCode, Intent data) {
367         super.onActivityResult(requestCode, resultCode, data);
368         if (requestCode == REQUEST_UNINSTALL) {
369             if (mDisableAfterUninstall) {
370                 mDisableAfterUninstall = false;
371                 try {
372                     ApplicationInfo ainfo = getActivity().getPackageManager().getApplicationInfo(
373                             mAppEntry.info.packageName, PackageManager.GET_UNINSTALLED_PACKAGES
374                             | PackageManager.GET_DISABLED_COMPONENTS);
375                     if ((ainfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0) {
376                         new DisableChanger(this, mAppEntry.info,
377                                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER)
378                                 .execute((Object)null);
379                     }
380                 } catch (NameNotFoundException e) {
381                 }
382             }
383             if (!refreshUi()) {
384                 setIntentAndFinish(true, true);
385             }
386         }
387     }
388
389     // Utility method to set application label and icon.
390     private void setAppLabelAndIcon(PackageInfo pkgInfo) {
391         final View appSnippet = mHeader.findViewById(R.id.app_snippet);
392         appSnippet.setPaddingRelative(0, appSnippet.getPaddingTop(), 0,
393                 appSnippet.getPaddingBottom());
394
395         ImageView icon = (ImageView) appSnippet.findViewById(R.id.app_icon);
396         mState.ensureIcon(mAppEntry);
397         icon.setImageDrawable(mAppEntry.icon);
398         // Set application name.
399         TextView label = (TextView) appSnippet.findViewById(R.id.app_name);
400         label.setText(mAppEntry.label);
401         // Version number of application
402         mAppVersion = (TextView) appSnippet.findViewById(R.id.app_summary);
403
404         if (pkgInfo != null && pkgInfo.versionName != null) {
405             mAppVersion.setVisibility(View.VISIBLE);
406             mAppVersion.setText(getActivity().getString(R.string.version_text,
407                     String.valueOf(pkgInfo.versionName)));
408         } else {
409             mAppVersion.setVisibility(View.INVISIBLE);
410         }
411     }
412
413     private boolean signaturesMatch(String pkg1, String pkg2) {
414         if (pkg1 != null && pkg2 != null) {
415             try {
416                 final int match = mPm.checkSignatures(pkg1, pkg2);
417                 if (match >= PackageManager.SIGNATURE_MATCH) {
418                     return true;
419                 }
420             } catch (Exception e) {
421                 // e.g. named alternate package not found during lookup;
422                 // this is an expected case sometimes
423             }
424         }
425         return false;
426     }
427
428     @Override
429     protected boolean refreshUi() {
430         retrieveAppEntry();
431         if (mAppEntry == null) {
432             return false; // onCreate must have failed, make sure to exit
433         }
434
435         if (mPackageInfo == null) {
436             return false; // onCreate must have failed, make sure to exit
437         }
438
439         // Get list of "home" apps and trace through any meta-data references
440         List<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
441         mPm.getHomeActivities(homeActivities);
442         mHomePackages.clear();
443         for (int i = 0; i< homeActivities.size(); i++) {
444             ResolveInfo ri = homeActivities.get(i);
445             final String activityPkg = ri.activityInfo.packageName;
446             mHomePackages.add(activityPkg);
447
448             // Also make sure to include anything proxying for the home app
449             final Bundle metadata = ri.activityInfo.metaData;
450             if (metadata != null) {
451                 final String metaPkg = metadata.getString(ActivityManager.META_HOME_ALTERNATE);
452                 if (signaturesMatch(metaPkg, activityPkg)) {
453                     mHomePackages.add(metaPkg);
454                 }
455             }
456         }
457
458         checkForceStop();
459         setAppLabelAndIcon(mPackageInfo);
460         initUninstallButtons();
461
462         // Update the preference summaries.
463         Activity context = getActivity();
464         mStoragePreference.setSummary(AppStorageSettings.getSummary(mAppEntry, context));
465         PermissionsSummaryHelper.getPermissionCounts(getContext(), mPackageName,
466                 mPermissionCallback);
467         mLaunchPreference.setSummary(Utils.getLaunchByDeafaultSummary(mAppEntry, mUsbManager,
468                 mPm, context));
469         mNotificationPreference.setSummary(getNotificationSummary(mAppEntry, context,
470                 mBackend));
471         if (mDataPreference != null) {
472             mDataPreference.setSummary(getDataSummary());
473         }
474
475         updateBattery();
476
477         if (!mInitialized) {
478             // First time init: are we displaying an uninstalled app?
479             mInitialized = true;
480             mShowUninstalled = (mAppEntry.info.flags&ApplicationInfo.FLAG_INSTALLED) == 0;
481         } else {
482             // All other times: if the app no longer exists then we want
483             // to go away.
484             try {
485                 ApplicationInfo ainfo = context.getPackageManager().getApplicationInfo(
486                         mAppEntry.info.packageName, PackageManager.GET_UNINSTALLED_PACKAGES
487                         | PackageManager.GET_DISABLED_COMPONENTS);
488                 if (!mShowUninstalled) {
489                     // If we did not start out with the app uninstalled, then
490                     // it transitioning to the uninstalled state for the current
491                     // user means we should go away as well.
492                     return (ainfo.flags&ApplicationInfo.FLAG_INSTALLED) != 0;
493                 }
494             } catch (NameNotFoundException e) {
495                 return false;
496             }
497         }
498
499         return true;
500     }
501
502     private void updateBattery() {
503         if (mSipper != null) {
504             mBatteryPreference.setEnabled(true);
505             int dischargeAmount = mBatteryHelper.getStats().getDischargeAmount(
506                     BatteryStats.STATS_SINCE_CHARGED);
507             final int percentOfMax = (int) ((mSipper.totalPowerMah)
508                     / mBatteryHelper.getTotalPower() * dischargeAmount + .5f);
509             mBatteryPreference.setSummary(getString(R.string.battery_summary, percentOfMax));
510         } else {
511             mBatteryPreference.setEnabled(false);
512             mBatteryPreference.setSummary(getString(R.string.no_battery_summary));
513         }
514     }
515
516     private CharSequence getDataSummary() {
517         if (mChartData != null) {
518             long totalBytes = mChartData.detail.getTotalBytes();
519             if (totalBytes == 0) {
520                 return getString(R.string.no_data_usage);
521             }
522             Context context = getActivity();
523             return getString(R.string.data_summary_format,
524                     Formatter.formatFileSize(context, totalBytes),
525                     DateUtils.formatDateTime(context, mChartData.detail.getStart(),
526                             DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH));
527         }
528         return getString(R.string.computing_size);
529     }
530
531     @Override
532     protected AlertDialog createDialog(int id, int errorCode) {
533         switch (id) {
534             case DLG_DISABLE:
535                 return new AlertDialog.Builder(getActivity())
536                         .setTitle(getActivity().getText(R.string.app_disable_dlg_title))
537                         .setMessage(getActivity().getText(R.string.app_disable_dlg_text))
538                         .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
539                             public void onClick(DialogInterface dialog, int which) {
540                                 // Disable the app
541                                 new DisableChanger(InstalledAppDetails.this, mAppEntry.info,
542                                         PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER)
543                                 .execute((Object)null);
544                             }
545                         })
546                         .setNegativeButton(R.string.dlg_cancel, null)
547                         .create();
548             case DLG_SPECIAL_DISABLE:
549                 return new AlertDialog.Builder(getActivity())
550                         .setTitle(getActivity().getText(R.string.app_special_disable_dlg_title))
551                         .setMessage(getActivity().getText(R.string.app_special_disable_dlg_text))
552                         .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
553                             public void onClick(DialogInterface dialog, int which) {
554                                 // Clear user data here
555                                 uninstallPkg(mAppEntry.info.packageName,
556                                         false, true);
557                             }
558                         })
559                         .setNegativeButton(R.string.dlg_cancel, null)
560                         .create();
561             case DLG_FORCE_STOP:
562                 return new AlertDialog.Builder(getActivity())
563                         .setTitle(getActivity().getText(R.string.force_stop_dlg_title))
564                         .setMessage(getActivity().getText(R.string.force_stop_dlg_text))
565                         .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
566                             public void onClick(DialogInterface dialog, int which) {
567                                 // Force stop
568                                 forceStopPackage(mAppEntry.info.packageName);
569                             }
570                         })
571                         .setNegativeButton(R.string.dlg_cancel, null)
572                         .create();
573             case DLG_FACTORY_RESET:
574                 return new AlertDialog.Builder(getActivity())
575                         .setTitle(getActivity().getText(R.string.app_factory_reset_dlg_title))
576                         .setMessage(getActivity().getText(R.string.app_factory_reset_dlg_text))
577                         .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
578                             public void onClick(DialogInterface dialog, int which) {
579                                 // Clear user data here
580                                 uninstallPkg(mAppEntry.info.packageName,
581                                         false, false);
582                             }
583                         })
584                         .setNegativeButton(R.string.dlg_cancel, null)
585                         .create();
586         }
587         return null;
588     }
589
590     private void uninstallPkg(String packageName, boolean allUsers, boolean andDisable) {
591          // Create new intent to launch Uninstaller activity
592         Uri packageURI = Uri.parse("package:"+packageName);
593         Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
594         uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, allUsers);
595         startActivityForResult(uninstallIntent, REQUEST_UNINSTALL);
596         mDisableAfterUninstall = andDisable;
597     }
598
599     private void forceStopPackage(String pkgName) {
600         ActivityManager am = (ActivityManager)getActivity().getSystemService(
601                 Context.ACTIVITY_SERVICE);
602         am.forceStopPackage(pkgName);
603         int userId = UserHandle.getUserId(mAppEntry.info.uid);
604         mState.invalidatePackage(pkgName, userId);
605         ApplicationsState.AppEntry newEnt = mState.getEntry(pkgName, userId);
606         if (newEnt != null) {
607             mAppEntry = newEnt;
608         }
609         checkForceStop();
610     }
611
612     private void updateForceStopButton(boolean enabled) {
613         if (mAppControlRestricted) {
614             mForceStopButton.setEnabled(false);
615         } else {
616             mForceStopButton.setEnabled(enabled);
617             mForceStopButton.setOnClickListener(InstalledAppDetails.this);
618         }
619     }
620
621     private void checkForceStop() {
622         if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
623             // User can't force stop device admin.
624             updateForceStopButton(false);
625         } else if ((mAppEntry.info.flags&ApplicationInfo.FLAG_STOPPED) == 0) {
626             // If the app isn't explicitly stopped, then always show the
627             // force stop button.
628             updateForceStopButton(true);
629         } else {
630             Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART,
631                     Uri.fromParts("package", mAppEntry.info.packageName, null));
632             intent.putExtra(Intent.EXTRA_PACKAGES, new String[] { mAppEntry.info.packageName });
633             intent.putExtra(Intent.EXTRA_UID, mAppEntry.info.uid);
634             intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(mAppEntry.info.uid));
635             getActivity().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null,
636                     mCheckKillProcessesReceiver, null, Activity.RESULT_CANCELED, null, null);
637         }
638     }
639
640     private void startManagePermissionsActivity() {
641         // start new activity to manage app permissions
642         Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS);
643         intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mAppEntry.info.packageName);
644         intent.putExtra(AppInfoWithHeader.EXTRA_HIDE_INFO_BUTTON, true);
645         try {
646             startActivity(intent);
647         } catch (ActivityNotFoundException e) {
648             Log.w(LOG_TAG, "No app can handle android.intent.action.MANAGE_APP_PERMISSIONS");
649         }
650     }
651
652     private void startAppInfoFragment(Class<?> fragment, CharSequence title) {
653         // start new fragment to display extended information
654         Bundle args = new Bundle();
655         args.putString(ARG_PACKAGE_NAME, mAppEntry.info.packageName);
656         args.putInt(ARG_PACKAGE_UID, mAppEntry.info.uid);
657         args.putBoolean(AppInfoWithHeader.EXTRA_HIDE_INFO_BUTTON, true);
658
659         SettingsActivity sa = (SettingsActivity) getActivity();
660         sa.startPreferencePanel(fragment.getName(), args, -1, title, this, SUB_INFO_FRAGMENT);
661     }
662
663     /*
664      * Method implementing functionality of buttons clicked
665      * @see android.view.View.OnClickListener#onClick(android.view.View)
666      */
667     public void onClick(View v) {
668         String packageName = mAppEntry.info.packageName;
669         if(v == mUninstallButton) {
670             if ((mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
671                 if (mAppEntry.info.enabled) {
672                     if (mUpdatedSysApp) {
673                         showDialogInner(DLG_SPECIAL_DISABLE, 0);
674                     } else {
675                         showDialogInner(DLG_DISABLE, 0);
676                     }
677                 } else {
678                     new DisableChanger(this, mAppEntry.info,
679                             PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
680                                     .execute((Object) null);
681                 }
682             } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
683                 uninstallPkg(packageName, true, false);
684             } else {
685                 uninstallPkg(packageName, false, false);
686             }
687         } else if (v == mForceStopButton) {
688             showDialogInner(DLG_FORCE_STOP, 0);
689             //forceStopPackage(mAppInfo.packageName);
690         }
691     }
692
693     @Override
694     public boolean onPreferenceClick(Preference preference) {
695         if (preference == mStoragePreference) {
696             startAppInfoFragment(AppStorageSettings.class, mStoragePreference.getTitle());
697         } else if (preference == mNotificationPreference) {
698             startAppInfoFragment(AppNotificationSettings.class,
699                     getString(R.string.app_notifications_title));
700         } else if (preference == mPermissionsPreference) {
701             startManagePermissionsActivity();
702         } else if (preference == mLaunchPreference) {
703             startAppInfoFragment(AppLaunchSettings.class, mLaunchPreference.getTitle());
704         } else if (preference == mDataPreference) {
705             Bundle args = new Bundle();
706             args.putString(DataUsageSummary.EXTRA_SHOW_APP_IMMEDIATE_PKG,
707                     mAppEntry.info.packageName);
708
709             SettingsActivity sa = (SettingsActivity) getActivity();
710             sa.startPreferencePanel(DataUsageSummary.class.getName(), args, -1,
711                     getString(R.string.app_data_usage), this, SUB_INFO_FRAGMENT);
712         } else if (preference == mBatteryPreference) {
713             BatteryEntry entry = new BatteryEntry(getActivity(), null, mUserManager, mSipper);
714             PowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(),
715                     mBatteryHelper, BatteryStats.STATS_SINCE_CHARGED, entry, true);
716         } else {
717             return false;
718         }
719         return true;
720     }
721
722     private static NetworkTemplate getTemplate(Context context) {
723         if (DataUsageSummary.hasReadyMobileRadio(context)) {
724             return NetworkTemplate.buildTemplateMobileWildcard();
725         }
726         if (DataUsageSummary.hasWifiRadio(context)) {
727             return NetworkTemplate.buildTemplateWifiWildcard();
728         }
729         return NetworkTemplate.buildTemplateEthernet();
730     }
731
732     public static CharSequence getNotificationSummary(AppEntry appEntry, Context context) {
733         return getNotificationSummary(appEntry, context, new NotificationBackend());
734     }
735
736     public static CharSequence getNotificationSummary(AppEntry appEntry, Context context,
737             NotificationBackend backend) {
738         AppRow appRow = backend.loadAppRow(context.getPackageManager(), appEntry.info);
739         return getNotificationSummary(appRow, context);
740     }
741
742     public static CharSequence getNotificationSummary(AppRow appRow, Context context) {
743         if (appRow.banned) {
744             return context.getString(R.string.notifications_disabled);
745         }
746         ArrayList<CharSequence> notifSummary = new ArrayList<>();
747         if (appRow.priority) {
748             notifSummary.add(context.getString(R.string.notifications_priority));
749         }
750         if (appRow.sensitive) {
751             notifSummary.add(context.getString(R.string.notifications_sensitive));
752         }
753         if (!appRow.peekable) {
754             notifSummary.add(context.getString(R.string.notifications_no_peeking));
755         }
756         switch (notifSummary.size()) {
757             case 3:
758                 return context.getString(R.string.notifications_three_items,
759                         notifSummary.get(0), notifSummary.get(1), notifSummary.get(2));
760             case 2:
761                 return context.getString(R.string.notifications_two_items,
762                         notifSummary.get(0), notifSummary.get(1));
763             case 1:
764                 return notifSummary.get(0);
765             default:
766                 return context.getString(R.string.notifications_enabled);
767         }
768     }
769
770     private class BatteryUpdater extends AsyncTask<Void, Void, Void> {
771         @Override
772         protected Void doInBackground(Void... params) {
773             mBatteryHelper.create((Bundle) null);
774             mBatteryHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED,
775                     mUserManager.getUserProfiles());
776             List<BatterySipper> usageList = mBatteryHelper.getUsageList();
777             final int N = usageList.size();
778             for (int i = 0; i < N; i++) {
779                 BatterySipper sipper = usageList.get(i);
780                 if (sipper.getUid() == mPackageInfo.applicationInfo.uid) {
781                     mSipper = sipper;
782                     break;
783                 }
784             }
785             return null;
786         }
787
788         @Override
789         protected void onPostExecute(Void result) {
790             refreshUi();
791         }
792     }
793
794     private static class DisableChanger extends AsyncTask<Object, Object, Object> {
795         final PackageManager mPm;
796         final WeakReference<InstalledAppDetails> mActivity;
797         final ApplicationInfo mInfo;
798         final int mState;
799
800         DisableChanger(InstalledAppDetails activity, ApplicationInfo info, int state) {
801             mPm = activity.mPm;
802             mActivity = new WeakReference<InstalledAppDetails>(activity);
803             mInfo = info;
804             mState = state;
805         }
806
807         @Override
808         protected Object doInBackground(Object... params) {
809             mPm.setApplicationEnabledSetting(mInfo.packageName, mState, 0);
810             return null;
811         }
812     }
813
814     private final LoaderCallbacks<ChartData> mDataCallbacks = new LoaderCallbacks<ChartData>() {
815
816         @Override
817         public Loader<ChartData> onCreateLoader(int id, Bundle args) {
818             return new ChartDataLoader(getActivity(), mStatsSession, args);
819         }
820
821         @Override
822         public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
823             mChartData = data;
824             mDataPreference.setSummary(getDataSummary());
825         }
826
827         @Override
828         public void onLoaderReset(Loader<ChartData> loader) {
829             mChartData = null;
830             mDataPreference.setSummary(getDataSummary());
831         }
832     };
833
834     private final BroadcastReceiver mCheckKillProcessesReceiver = new BroadcastReceiver() {
835         @Override
836         public void onReceive(Context context, Intent intent) {
837             updateForceStopButton(getResultCode() != Activity.RESULT_CANCELED);
838         }
839     };
840
841     private final PermissionsResultCallback mPermissionCallback = new PermissionsResultCallback() {
842         @Override
843         public void onPermissionCountResult(int[] result) {
844             if (getActivity() == null) {
845                 return;
846             }
847             if (result != null) {
848                 mPermissionsPreference.setSummary(getResources().getQuantityString(
849                         R.plurals.runtime_permissions_summary, result[1], result[0], result[1]));
850             } else {
851                 mPermissionsPreference.setSummary(null);
852             }
853         }
854     };
855 }
856
857