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