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