InstalledAppDetails.java revision bcd1a8f73c2f5e409d3c7888b016e5bc9afb204f
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 if (mAppEntry == null) { 709 setIntentAndFinish(true, true); 710 return; 711 } 712 String packageName = mAppEntry.info.packageName; 713 if(v == mUninstallButton) { 714 if ((mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 715 if (mAppEntry.info.enabled && !isDisabledUntilUsed()) { 716 if (mUpdatedSysApp) { 717 showDialogInner(DLG_SPECIAL_DISABLE, 0); 718 } else { 719 showDialogInner(DLG_DISABLE, 0); 720 } 721 } else { 722 new DisableChanger(this, mAppEntry.info, 723 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) 724 .execute((Object) null); 725 } 726 } else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) { 727 uninstallPkg(packageName, true, false); 728 } else { 729 uninstallPkg(packageName, false, false); 730 } 731 } else if (v == mForceStopButton) { 732 showDialogInner(DLG_FORCE_STOP, 0); 733 //forceStopPackage(mAppInfo.packageName); 734 } 735 } 736 737 @Override 738 public boolean onPreferenceClick(Preference preference) { 739 if (preference == mStoragePreference) { 740 startAppInfoFragment(AppStorageSettings.class, mStoragePreference.getTitle()); 741 } else if (preference == mNotificationPreference) { 742 startAppInfoFragment(AppNotificationSettings.class, 743 getString(R.string.app_notifications_title)); 744 } else if (preference == mPermissionsPreference) { 745 startManagePermissionsActivity(); 746 } else if (preference == mLaunchPreference) { 747 startAppInfoFragment(AppLaunchSettings.class, mLaunchPreference.getTitle()); 748 } else if (preference == mMemoryPreference) { 749 ProcessStatsBase.launchMemoryDetail((SettingsActivity) getActivity(), 750 mStatsManager.getMemInfo(), mStats, false); 751 } else if (preference == mDataPreference) { 752 Bundle args = new Bundle(); 753 args.putString(DataUsageSummary.EXTRA_SHOW_APP_IMMEDIATE_PKG, 754 mAppEntry.info.packageName); 755 756 SettingsActivity sa = (SettingsActivity) getActivity(); 757 sa.startPreferencePanel(DataUsageSummary.class.getName(), args, -1, 758 getString(R.string.app_data_usage), this, SUB_INFO_FRAGMENT); 759 } else if (preference == mBatteryPreference) { 760 BatteryEntry entry = new BatteryEntry(getActivity(), null, mUserManager, mSipper); 761 PowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(), 762 mBatteryHelper, BatteryStats.STATS_SINCE_CHARGED, entry, true, false); 763 } else { 764 return false; 765 } 766 return true; 767 } 768 769 public static void setupAppSnippet(View appSnippet, CharSequence label, Drawable icon, 770 CharSequence versionName) { 771 LayoutInflater.from(appSnippet.getContext()).inflate(R.layout.widget_text_views, 772 (ViewGroup) appSnippet.findViewById(android.R.id.widget_frame)); 773 774 ImageView iconView = (ImageView) appSnippet.findViewById(android.R.id.icon); 775 iconView.setImageDrawable(icon); 776 // Set application name. 777 TextView labelView = (TextView) appSnippet.findViewById(android.R.id.title); 778 labelView.setText(label); 779 // Version number of application 780 TextView appVersion = (TextView) appSnippet.findViewById(R.id.widget_text1); 781 782 if (!TextUtils.isEmpty(versionName)) { 783 appVersion.setSelected(true); 784 appVersion.setVisibility(View.VISIBLE); 785 appVersion.setText(appSnippet.getContext().getString(R.string.version_text, 786 String.valueOf(versionName))); 787 } else { 788 appVersion.setVisibility(View.INVISIBLE); 789 } 790 } 791 792 private static NetworkTemplate getTemplate(Context context) { 793 if (DataUsageSummary.hasReadyMobileRadio(context)) { 794 return NetworkTemplate.buildTemplateMobileWildcard(); 795 } 796 if (DataUsageSummary.hasWifiRadio(context)) { 797 return NetworkTemplate.buildTemplateWifiWildcard(); 798 } 799 return NetworkTemplate.buildTemplateEthernet(); 800 } 801 802 public static CharSequence getNotificationSummary(AppEntry appEntry, Context context) { 803 return getNotificationSummary(appEntry, context, new NotificationBackend()); 804 } 805 806 public static CharSequence getNotificationSummary(AppEntry appEntry, Context context, 807 NotificationBackend backend) { 808 AppRow appRow = backend.loadAppRow(context.getPackageManager(), appEntry.info); 809 return getNotificationSummary(appRow, context); 810 } 811 812 public static CharSequence getNotificationSummary(AppRow appRow, Context context) { 813 if (appRow.banned) { 814 return context.getString(R.string.notifications_disabled); 815 } 816 ArrayList<CharSequence> notifSummary = new ArrayList<>(); 817 if (appRow.priority) { 818 notifSummary.add(context.getString(R.string.notifications_priority)); 819 } 820 if (appRow.sensitive) { 821 notifSummary.add(context.getString(R.string.notifications_sensitive)); 822 } 823 if (!appRow.peekable) { 824 notifSummary.add(context.getString(R.string.notifications_no_peeking)); 825 } 826 switch (notifSummary.size()) { 827 case 3: 828 return context.getString(R.string.notifications_three_items, 829 notifSummary.get(0), notifSummary.get(1), notifSummary.get(2)); 830 case 2: 831 return context.getString(R.string.notifications_two_items, 832 notifSummary.get(0), notifSummary.get(1)); 833 case 1: 834 return notifSummary.get(0); 835 default: 836 return context.getString(R.string.notifications_enabled); 837 } 838 } 839 840 private class MemoryUpdater extends AsyncTask<Void, Void, ProcStatsPackageEntry> { 841 842 @Override 843 protected ProcStatsPackageEntry doInBackground(Void... params) { 844 if (getActivity() == null) { 845 return null; 846 } 847 if (mPackageInfo == null) { 848 return null; 849 } 850 if (mStatsManager == null) { 851 mStatsManager = new ProcStatsData(getActivity(), false); 852 mStatsManager.setDuration(ProcessStatsBase.sDurations[0]); 853 } 854 mStatsManager.refreshStats(true); 855 for (ProcStatsPackageEntry pkgEntry : mStatsManager.getEntries()) { 856 for (ProcStatsEntry entry : pkgEntry.mEntries) { 857 if (entry.mUid == mPackageInfo.applicationInfo.uid) { 858 pkgEntry.updateMetrics(); 859 return pkgEntry; 860 } 861 } 862 } 863 return null; 864 } 865 866 @Override 867 protected void onPostExecute(ProcStatsPackageEntry entry) { 868 if (getActivity() == null) { 869 return; 870 } 871 if (entry != null) { 872 mStats = entry; 873 mMemoryPreference.setEnabled(true); 874 double amount = Math.max(entry.mRunWeight, entry.mBgWeight) 875 * mStatsManager.getMemInfo().weightToRam; 876 mMemoryPreference.setSummary(getString(R.string.memory_use_summary, 877 Formatter.formatShortFileSize(getContext(), (long) amount))); 878 } else { 879 mMemoryPreference.setEnabled(false); 880 mMemoryPreference.setSummary(getString(R.string.no_memory_use_summary)); 881 } 882 } 883 884 } 885 886 private class BatteryUpdater extends AsyncTask<Void, Void, Void> { 887 @Override 888 protected Void doInBackground(Void... params) { 889 mBatteryHelper.create((Bundle) null); 890 mBatteryHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, 891 mUserManager.getUserProfiles()); 892 List<BatterySipper> usageList = mBatteryHelper.getUsageList(); 893 final int N = usageList.size(); 894 for (int i = 0; i < N; i++) { 895 BatterySipper sipper = usageList.get(i); 896 if (sipper.getUid() == mPackageInfo.applicationInfo.uid) { 897 mSipper = sipper; 898 break; 899 } 900 } 901 return null; 902 } 903 904 @Override 905 protected void onPostExecute(Void result) { 906 if (getActivity() == null) { 907 return; 908 } 909 refreshUi(); 910 } 911 } 912 913 private static class DisableChanger extends AsyncTask<Object, Object, Object> { 914 final PackageManager mPm; 915 final WeakReference<InstalledAppDetails> mActivity; 916 final ApplicationInfo mInfo; 917 final int mState; 918 919 DisableChanger(InstalledAppDetails activity, ApplicationInfo info, int state) { 920 mPm = activity.mPm; 921 mActivity = new WeakReference<InstalledAppDetails>(activity); 922 mInfo = info; 923 mState = state; 924 } 925 926 @Override 927 protected Object doInBackground(Object... params) { 928 mPm.setApplicationEnabledSetting(mInfo.packageName, mState, 0); 929 return null; 930 } 931 } 932 933 private final LoaderCallbacks<ChartData> mDataCallbacks = new LoaderCallbacks<ChartData>() { 934 935 @Override 936 public Loader<ChartData> onCreateLoader(int id, Bundle args) { 937 return new ChartDataLoader(getActivity(), mStatsSession, args); 938 } 939 940 @Override 941 public void onLoadFinished(Loader<ChartData> loader, ChartData data) { 942 mChartData = data; 943 mDataPreference.setSummary(getDataSummary()); 944 } 945 946 @Override 947 public void onLoaderReset(Loader<ChartData> loader) { 948 // Leave last result. 949 } 950 }; 951 952 private final BroadcastReceiver mCheckKillProcessesReceiver = new BroadcastReceiver() { 953 @Override 954 public void onReceive(Context context, Intent intent) { 955 updateForceStopButton(getResultCode() != Activity.RESULT_CANCELED); 956 } 957 }; 958 959 private final PermissionsResultCallback mPermissionCallback 960 = new PermissionsResultCallback() { 961 @Override 962 public void onPermissionSummaryResult(int[] counts, CharSequence[] groupLabels) { 963 if (getActivity() == null) { 964 return; 965 } 966 mPermissionReceiver = null; 967 final Resources res = getResources(); 968 CharSequence summary = null; 969 if (counts != null) { 970 int totalCount = counts[1]; 971 int additionalCounts = counts[2]; 972 973 if (totalCount == 0) { 974 summary = res.getString( 975 R.string.runtime_permissions_summary_no_permissions_requested); 976 } else { 977 final ArrayList<CharSequence> list = new ArrayList(Arrays.asList(groupLabels)); 978 if (additionalCounts > 0) { 979 // N additional permissions. 980 list.add(res.getQuantityString( 981 R.plurals.runtime_permissions_additional_count, 982 additionalCounts, additionalCounts)); 983 } 984 if (list.size() == 0) { 985 summary = res.getString( 986 R.string.runtime_permissions_summary_no_permissions_granted); 987 } else { 988 summary = ListFormatter.getInstance().format(list); 989 } 990 } 991 } 992 mPermissionsPreference.setSummary(summary); 993 } 994 }; 995} 996