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