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