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