ActivityManager.java revision 904a857d5a319e32d1df065b38e3191324b35b0f
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.app; 18 19import android.R; 20import android.os.BatteryStats; 21import android.os.IBinder; 22import com.android.internal.app.IUsageStats; 23import com.android.internal.os.PkgUsageStats; 24import com.android.internal.os.TransferPipe; 25import com.android.internal.util.FastPrintWriter; 26import com.android.internal.util.MemInfoReader; 27 28import android.content.ComponentName; 29import android.content.Context; 30import android.content.Intent; 31import android.content.pm.ApplicationInfo; 32import android.content.pm.ConfigurationInfo; 33import android.content.pm.IPackageDataObserver; 34import android.content.pm.PackageManager; 35import android.content.pm.UserInfo; 36import android.content.res.Resources; 37import android.graphics.Bitmap; 38import android.graphics.Point; 39import android.graphics.Rect; 40import android.hardware.display.DisplayManagerGlobal; 41import android.os.Bundle; 42import android.os.Debug; 43import android.os.Handler; 44import android.os.Parcel; 45import android.os.Parcelable; 46import android.os.Process; 47import android.os.RemoteException; 48import android.os.ServiceManager; 49import android.os.SystemProperties; 50import android.os.UserHandle; 51import android.text.TextUtils; 52import android.util.DisplayMetrics; 53import android.util.Log; 54import android.util.Slog; 55import android.view.Display; 56 57import java.io.FileDescriptor; 58import java.io.FileOutputStream; 59import java.io.PrintWriter; 60import java.util.HashMap; 61import java.util.List; 62import java.util.Map; 63 64/** 65 * Interact with the overall activities running in the system. 66 */ 67public class ActivityManager { 68 private static String TAG = "ActivityManager"; 69 private static boolean localLOGV = false; 70 71 private final Context mContext; 72 private final Handler mHandler; 73 74 /** 75 * Result for IActivityManager.startActivity: an error where the 76 * start had to be canceled. 77 * @hide 78 */ 79 public static final int START_CANCELED = -6; 80 81 /** 82 * Result for IActivityManager.startActivity: an error where the 83 * thing being started is not an activity. 84 * @hide 85 */ 86 public static final int START_NOT_ACTIVITY = -5; 87 88 /** 89 * Result for IActivityManager.startActivity: an error where the 90 * caller does not have permission to start the activity. 91 * @hide 92 */ 93 public static final int START_PERMISSION_DENIED = -4; 94 95 /** 96 * Result for IActivityManager.startActivity: an error where the 97 * caller has requested both to forward a result and to receive 98 * a result. 99 * @hide 100 */ 101 public static final int START_FORWARD_AND_REQUEST_CONFLICT = -3; 102 103 /** 104 * Result for IActivityManager.startActivity: an error where the 105 * requested class is not found. 106 * @hide 107 */ 108 public static final int START_CLASS_NOT_FOUND = -2; 109 110 /** 111 * Result for IActivityManager.startActivity: an error where the 112 * given Intent could not be resolved to an activity. 113 * @hide 114 */ 115 public static final int START_INTENT_NOT_RESOLVED = -1; 116 117 /** 118 * Result for IActivityManaqer.startActivity: the activity was started 119 * successfully as normal. 120 * @hide 121 */ 122 public static final int START_SUCCESS = 0; 123 124 /** 125 * Result for IActivityManaqer.startActivity: the caller asked that the Intent not 126 * be executed if it is the recipient, and that is indeed the case. 127 * @hide 128 */ 129 public static final int START_RETURN_INTENT_TO_CALLER = 1; 130 131 /** 132 * Result for IActivityManaqer.startActivity: activity wasn't really started, but 133 * a task was simply brought to the foreground. 134 * @hide 135 */ 136 public static final int START_TASK_TO_FRONT = 2; 137 138 /** 139 * Result for IActivityManaqer.startActivity: activity wasn't really started, but 140 * the given Intent was given to the existing top activity. 141 * @hide 142 */ 143 public static final int START_DELIVERED_TO_TOP = 3; 144 145 /** 146 * Result for IActivityManaqer.startActivity: request was canceled because 147 * app switches are temporarily canceled to ensure the user's last request 148 * (such as pressing home) is performed. 149 * @hide 150 */ 151 public static final int START_SWITCHES_CANCELED = 4; 152 153 /** 154 * Flag for IActivityManaqer.startActivity: do special start mode where 155 * a new activity is launched only if it is needed. 156 * @hide 157 */ 158 public static final int START_FLAG_ONLY_IF_NEEDED = 1<<0; 159 160 /** 161 * Flag for IActivityManaqer.startActivity: launch the app for 162 * debugging. 163 * @hide 164 */ 165 public static final int START_FLAG_DEBUG = 1<<1; 166 167 /** 168 * Flag for IActivityManaqer.startActivity: launch the app for 169 * OpenGL tracing. 170 * @hide 171 */ 172 public static final int START_FLAG_OPENGL_TRACES = 1<<2; 173 174 /** 175 * Flag for IActivityManaqer.startActivity: if the app is being 176 * launched for profiling, automatically stop the profiler once done. 177 * @hide 178 */ 179 public static final int START_FLAG_AUTO_STOP_PROFILER = 1<<3; 180 181 /** 182 * Result for IActivityManaqer.broadcastIntent: success! 183 * @hide 184 */ 185 public static final int BROADCAST_SUCCESS = 0; 186 187 /** 188 * Result for IActivityManaqer.broadcastIntent: attempt to broadcast 189 * a sticky intent without appropriate permission. 190 * @hide 191 */ 192 public static final int BROADCAST_STICKY_CANT_HAVE_PERMISSION = -1; 193 194 /** 195 * Type for IActivityManaqer.getIntentSender: this PendingIntent is 196 * for a sendBroadcast operation. 197 * @hide 198 */ 199 public static final int INTENT_SENDER_BROADCAST = 1; 200 201 /** 202 * Type for IActivityManaqer.getIntentSender: this PendingIntent is 203 * for a startActivity operation. 204 * @hide 205 */ 206 public static final int INTENT_SENDER_ACTIVITY = 2; 207 208 /** 209 * Type for IActivityManaqer.getIntentSender: this PendingIntent is 210 * for an activity result operation. 211 * @hide 212 */ 213 public static final int INTENT_SENDER_ACTIVITY_RESULT = 3; 214 215 /** 216 * Type for IActivityManaqer.getIntentSender: this PendingIntent is 217 * for a startService operation. 218 * @hide 219 */ 220 public static final int INTENT_SENDER_SERVICE = 4; 221 222 /** @hide User operation call: success! */ 223 public static final int USER_OP_SUCCESS = 0; 224 225 /** @hide User operation call: given user id is not known. */ 226 public static final int USER_OP_UNKNOWN_USER = -1; 227 228 /** @hide User operation call: given user id is the current user, can't be stopped. */ 229 public static final int USER_OP_IS_CURRENT = -2; 230 231 /*package*/ ActivityManager(Context context, Handler handler) { 232 mContext = context; 233 mHandler = handler; 234 } 235 236 /** 237 * Screen compatibility mode: the application most always run in 238 * compatibility mode. 239 * @hide 240 */ 241 public static final int COMPAT_MODE_ALWAYS = -1; 242 243 /** 244 * Screen compatibility mode: the application can never run in 245 * compatibility mode. 246 * @hide 247 */ 248 public static final int COMPAT_MODE_NEVER = -2; 249 250 /** 251 * Screen compatibility mode: unknown. 252 * @hide 253 */ 254 public static final int COMPAT_MODE_UNKNOWN = -3; 255 256 /** 257 * Screen compatibility mode: the application currently has compatibility 258 * mode disabled. 259 * @hide 260 */ 261 public static final int COMPAT_MODE_DISABLED = 0; 262 263 /** 264 * Screen compatibility mode: the application currently has compatibility 265 * mode enabled. 266 * @hide 267 */ 268 public static final int COMPAT_MODE_ENABLED = 1; 269 270 /** 271 * Screen compatibility mode: request to toggle the application's 272 * compatibility mode. 273 * @hide 274 */ 275 public static final int COMPAT_MODE_TOGGLE = 2; 276 277 /** @hide */ 278 public int getFrontActivityScreenCompatMode() { 279 try { 280 return ActivityManagerNative.getDefault().getFrontActivityScreenCompatMode(); 281 } catch (RemoteException e) { 282 // System dead, we will be dead too soon! 283 return 0; 284 } 285 } 286 287 /** @hide */ 288 public void setFrontActivityScreenCompatMode(int mode) { 289 try { 290 ActivityManagerNative.getDefault().setFrontActivityScreenCompatMode(mode); 291 } catch (RemoteException e) { 292 // System dead, we will be dead too soon! 293 } 294 } 295 296 /** @hide */ 297 public int getPackageScreenCompatMode(String packageName) { 298 try { 299 return ActivityManagerNative.getDefault().getPackageScreenCompatMode(packageName); 300 } catch (RemoteException e) { 301 // System dead, we will be dead too soon! 302 return 0; 303 } 304 } 305 306 /** @hide */ 307 public void setPackageScreenCompatMode(String packageName, int mode) { 308 try { 309 ActivityManagerNative.getDefault().setPackageScreenCompatMode(packageName, mode); 310 } catch (RemoteException e) { 311 // System dead, we will be dead too soon! 312 } 313 } 314 315 /** @hide */ 316 public boolean getPackageAskScreenCompat(String packageName) { 317 try { 318 return ActivityManagerNative.getDefault().getPackageAskScreenCompat(packageName); 319 } catch (RemoteException e) { 320 // System dead, we will be dead too soon! 321 return false; 322 } 323 } 324 325 /** @hide */ 326 public void setPackageAskScreenCompat(String packageName, boolean ask) { 327 try { 328 ActivityManagerNative.getDefault().setPackageAskScreenCompat(packageName, ask); 329 } catch (RemoteException e) { 330 // System dead, we will be dead too soon! 331 } 332 } 333 334 /** 335 * Return the approximate per-application memory class of the current 336 * device. This gives you an idea of how hard a memory limit you should 337 * impose on your application to let the overall system work best. The 338 * returned value is in megabytes; the baseline Android memory class is 339 * 16 (which happens to be the Java heap limit of those devices); some 340 * device with more memory may return 24 or even higher numbers. 341 */ 342 public int getMemoryClass() { 343 return staticGetMemoryClass(); 344 } 345 346 /** @hide */ 347 static public int staticGetMemoryClass() { 348 // Really brain dead right now -- just take this from the configured 349 // vm heap size, and assume it is in megabytes and thus ends with "m". 350 String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", ""); 351 if (vmHeapSize != null && !"".equals(vmHeapSize)) { 352 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1)); 353 } 354 return staticGetLargeMemoryClass(); 355 } 356 357 /** 358 * Return the approximate per-application memory class of the current 359 * device when an application is running with a large heap. This is the 360 * space available for memory-intensive applications; most applications 361 * should not need this amount of memory, and should instead stay with the 362 * {@link #getMemoryClass()} limit. The returned value is in megabytes. 363 * This may be the same size as {@link #getMemoryClass()} on memory 364 * constrained devices, or it may be significantly larger on devices with 365 * a large amount of available RAM. 366 * 367 * <p>The is the size of the application's Dalvik heap if it has 368 * specified <code>android:largeHeap="true"</code> in its manifest. 369 */ 370 public int getLargeMemoryClass() { 371 return staticGetLargeMemoryClass(); 372 } 373 374 /** @hide */ 375 static public int staticGetLargeMemoryClass() { 376 // Really brain dead right now -- just take this from the configured 377 // vm heap size, and assume it is in megabytes and thus ends with "m". 378 String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m"); 379 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1)); 380 } 381 382 /** 383 * Returns true if this is a low-RAM device. Exactly whether a device is low-RAM 384 * is ultimately up to the device configuration, but currently it generally means 385 * something in the class of a 512MB device with about a 800x480 or less screen. 386 * This is mostly intended to be used by apps to determine whether they should turn 387 * off certain features that require more RAM. 388 */ 389 public boolean isLowRamDevice() { 390 return isLowRamDeviceStatic(); 391 } 392 393 /** @hide */ 394 public static boolean isLowRamDeviceStatic() { 395 return Resources.getSystem().getBoolean(com.android.internal.R.bool.config_lowRamDevice); 396 } 397 398 /** 399 * Used by persistent processes to determine if they are running on a 400 * higher-end device so should be okay using hardware drawing acceleration 401 * (which tends to consume a lot more RAM). 402 * @hide 403 */ 404 static public boolean isHighEndGfx() { 405 return !isLowRamDeviceStatic() && 406 !Resources.getSystem().getBoolean(com.android.internal.R.bool.config_avoidGfxAccel); 407 } 408 409 /** 410 * Information you can retrieve about tasks that the user has most recently 411 * started or visited. 412 */ 413 public static class RecentTaskInfo implements Parcelable { 414 /** 415 * If this task is currently running, this is the identifier for it. 416 * If it is not running, this will be -1. 417 */ 418 public int id; 419 420 /** 421 * The true identifier of this task, valid even if it is not running. 422 */ 423 public int persistentId; 424 425 /** 426 * The original Intent used to launch the task. You can use this 427 * Intent to re-launch the task (if it is no longer running) or bring 428 * the current task to the front. 429 */ 430 public Intent baseIntent; 431 432 /** 433 * If this task was started from an alias, this is the actual 434 * activity component that was initially started; the component of 435 * the baseIntent in this case is the name of the actual activity 436 * implementation that the alias referred to. Otherwise, this is null. 437 */ 438 public ComponentName origActivity; 439 440 /** 441 * Description of the task's last state. 442 */ 443 public CharSequence description; 444 445 /** 446 * The id of the ActivityStack this Task was on most recently. 447 */ 448 public int stackId; 449 450 public RecentTaskInfo() { 451 } 452 453 @Override 454 public int describeContents() { 455 return 0; 456 } 457 458 @Override 459 public void writeToParcel(Parcel dest, int flags) { 460 dest.writeInt(id); 461 dest.writeInt(persistentId); 462 if (baseIntent != null) { 463 dest.writeInt(1); 464 baseIntent.writeToParcel(dest, 0); 465 } else { 466 dest.writeInt(0); 467 } 468 ComponentName.writeToParcel(origActivity, dest); 469 TextUtils.writeToParcel(description, dest, 470 Parcelable.PARCELABLE_WRITE_RETURN_VALUE); 471 dest.writeInt(stackId); 472 } 473 474 public void readFromParcel(Parcel source) { 475 id = source.readInt(); 476 persistentId = source.readInt(); 477 if (source.readInt() != 0) { 478 baseIntent = Intent.CREATOR.createFromParcel(source); 479 } else { 480 baseIntent = null; 481 } 482 origActivity = ComponentName.readFromParcel(source); 483 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); 484 stackId = source.readInt(); 485 } 486 487 public static final Creator<RecentTaskInfo> CREATOR 488 = new Creator<RecentTaskInfo>() { 489 public RecentTaskInfo createFromParcel(Parcel source) { 490 return new RecentTaskInfo(source); 491 } 492 public RecentTaskInfo[] newArray(int size) { 493 return new RecentTaskInfo[size]; 494 } 495 }; 496 497 private RecentTaskInfo(Parcel source) { 498 readFromParcel(source); 499 } 500 } 501 502 /** 503 * Flag for use with {@link #getRecentTasks}: return all tasks, even those 504 * that have set their 505 * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag. 506 */ 507 public static final int RECENT_WITH_EXCLUDED = 0x0001; 508 509 /** 510 * Provides a list that does not contain any 511 * recent tasks that currently are not available to the user. 512 */ 513 public static final int RECENT_IGNORE_UNAVAILABLE = 0x0002; 514 515 /** 516 * Return a list of the tasks that the user has recently launched, with 517 * the most recent being first and older ones after in order. 518 * 519 * <p><b>Note: this method is only intended for debugging and presenting 520 * task management user interfaces</b>. This should never be used for 521 * core logic in an application, such as deciding between different 522 * behaviors based on the information found here. Such uses are 523 * <em>not</em> supported, and will likely break in the future. For 524 * example, if multiple applications can be actively running at the 525 * same time, assumptions made about the meaning of the data here for 526 * purposes of control flow will be incorrect.</p> 527 * 528 * @param maxNum The maximum number of entries to return in the list. The 529 * actual number returned may be smaller, depending on how many tasks the 530 * user has started and the maximum number the system can remember. 531 * @param flags Information about what to return. May be any combination 532 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}. 533 * 534 * @return Returns a list of RecentTaskInfo records describing each of 535 * the recent tasks. 536 * 537 * @throws SecurityException Throws SecurityException if the caller does 538 * not hold the {@link android.Manifest.permission#GET_TASKS} permission. 539 */ 540 public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags) 541 throws SecurityException { 542 try { 543 return ActivityManagerNative.getDefault().getRecentTasks(maxNum, 544 flags, UserHandle.myUserId()); 545 } catch (RemoteException e) { 546 // System dead, we will be dead too soon! 547 return null; 548 } 549 } 550 551 /** 552 * Same as {@link #getRecentTasks(int, int)} but returns the recent tasks for a 553 * specific user. It requires holding 554 * the {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission. 555 * @param maxNum The maximum number of entries to return in the list. The 556 * actual number returned may be smaller, depending on how many tasks the 557 * user has started and the maximum number the system can remember. 558 * @param flags Information about what to return. May be any combination 559 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}. 560 * 561 * @return Returns a list of RecentTaskInfo records describing each of 562 * the recent tasks. 563 * 564 * @throws SecurityException Throws SecurityException if the caller does 565 * not hold the {@link android.Manifest.permission#GET_TASKS} or the 566 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permissions. 567 * @hide 568 */ 569 public List<RecentTaskInfo> getRecentTasksForUser(int maxNum, int flags, int userId) 570 throws SecurityException { 571 try { 572 return ActivityManagerNative.getDefault().getRecentTasks(maxNum, 573 flags, userId); 574 } catch (RemoteException e) { 575 // System dead, we will be dead too soon! 576 return null; 577 } 578 } 579 580 /** 581 * Information you can retrieve about a particular task that is currently 582 * "running" in the system. Note that a running task does not mean the 583 * given task actually has a process it is actively running in; it simply 584 * means that the user has gone to it and never closed it, but currently 585 * the system may have killed its process and is only holding on to its 586 * last state in order to restart it when the user returns. 587 */ 588 public static class RunningTaskInfo implements Parcelable { 589 /** 590 * A unique identifier for this task. 591 */ 592 public int id; 593 594 /** 595 * The component launched as the first activity in the task. This can 596 * be considered the "application" of this task. 597 */ 598 public ComponentName baseActivity; 599 600 /** 601 * The activity component at the top of the history stack of the task. 602 * This is what the user is currently doing. 603 */ 604 public ComponentName topActivity; 605 606 /** 607 * Thumbnail representation of the task's current state. Currently 608 * always null. 609 */ 610 public Bitmap thumbnail; 611 612 /** 613 * Description of the task's current state. 614 */ 615 public CharSequence description; 616 617 /** 618 * Number of activities in this task. 619 */ 620 public int numActivities; 621 622 /** 623 * Number of activities that are currently running (not stopped 624 * and persisted) in this task. 625 */ 626 public int numRunning; 627 628 public RunningTaskInfo() { 629 } 630 631 public int describeContents() { 632 return 0; 633 } 634 635 public void writeToParcel(Parcel dest, int flags) { 636 dest.writeInt(id); 637 ComponentName.writeToParcel(baseActivity, dest); 638 ComponentName.writeToParcel(topActivity, dest); 639 if (thumbnail != null) { 640 dest.writeInt(1); 641 thumbnail.writeToParcel(dest, 0); 642 } else { 643 dest.writeInt(0); 644 } 645 TextUtils.writeToParcel(description, dest, 646 Parcelable.PARCELABLE_WRITE_RETURN_VALUE); 647 dest.writeInt(numActivities); 648 dest.writeInt(numRunning); 649 } 650 651 public void readFromParcel(Parcel source) { 652 id = source.readInt(); 653 baseActivity = ComponentName.readFromParcel(source); 654 topActivity = ComponentName.readFromParcel(source); 655 if (source.readInt() != 0) { 656 thumbnail = Bitmap.CREATOR.createFromParcel(source); 657 } else { 658 thumbnail = null; 659 } 660 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); 661 numActivities = source.readInt(); 662 numRunning = source.readInt(); 663 } 664 665 public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() { 666 public RunningTaskInfo createFromParcel(Parcel source) { 667 return new RunningTaskInfo(source); 668 } 669 public RunningTaskInfo[] newArray(int size) { 670 return new RunningTaskInfo[size]; 671 } 672 }; 673 674 private RunningTaskInfo(Parcel source) { 675 readFromParcel(source); 676 } 677 } 678 679 /** 680 * Return a list of the tasks that are currently running, with 681 * the most recent being first and older ones after in order. Note that 682 * "running" does not mean any of the task's code is currently loaded or 683 * activity -- the task may have been frozen by the system, so that it 684 * can be restarted in its previous state when next brought to the 685 * foreground. 686 * 687 * @param maxNum The maximum number of entries to return in the list. The 688 * actual number returned may be smaller, depending on how many tasks the 689 * user has started. 690 * 691 * @param flags Optional flags 692 * @param receiver Optional receiver for delayed thumbnails 693 * 694 * @return Returns a list of RunningTaskInfo records describing each of 695 * the running tasks. 696 * 697 * Some thumbnails may not be available at the time of this call. The optional 698 * receiver may be used to receive those thumbnails. 699 * 700 * @throws SecurityException Throws SecurityException if the caller does 701 * not hold the {@link android.Manifest.permission#GET_TASKS} permission. 702 * 703 * @hide 704 */ 705 public List<RunningTaskInfo> getRunningTasks(int maxNum, int flags, IThumbnailReceiver receiver) 706 throws SecurityException { 707 try { 708 return ActivityManagerNative.getDefault().getTasks(maxNum, flags, receiver); 709 } catch (RemoteException e) { 710 // System dead, we will be dead too soon! 711 return null; 712 } 713 } 714 715 /** 716 * Return a list of the tasks that are currently running, with 717 * the most recent being first and older ones after in order. Note that 718 * "running" does not mean any of the task's code is currently loaded or 719 * activity -- the task may have been frozen by the system, so that it 720 * can be restarted in its previous state when next brought to the 721 * foreground. 722 * 723 * <p><b>Note: this method is only intended for debugging and presenting 724 * task management user interfaces</b>. This should never be used for 725 * core logic in an application, such as deciding between different 726 * behaviors based on the information found here. Such uses are 727 * <em>not</em> supported, and will likely break in the future. For 728 * example, if multiple applications can be actively running at the 729 * same time, assumptions made about the meaning of the data here for 730 * purposes of control flow will be incorrect.</p> 731 * 732 * @param maxNum The maximum number of entries to return in the list. The 733 * actual number returned may be smaller, depending on how many tasks the 734 * user has started. 735 * 736 * @return Returns a list of RunningTaskInfo records describing each of 737 * the running tasks. 738 * 739 * @throws SecurityException Throws SecurityException if the caller does 740 * not hold the {@link android.Manifest.permission#GET_TASKS} permission. 741 */ 742 public List<RunningTaskInfo> getRunningTasks(int maxNum) 743 throws SecurityException { 744 return getRunningTasks(maxNum, 0, null); 745 } 746 747 /** 748 * Remove some end of a task's activity stack that is not part of 749 * the main application. The selected activities will be finished, so 750 * they are no longer part of the main task. 751 * 752 * @param taskId The identifier of the task. 753 * @param subTaskIndex The number of the sub-task; this corresponds 754 * to the index of the thumbnail returned by {@link #getTaskThumbnails(int)}. 755 * @return Returns true if the sub-task was found and was removed. 756 * 757 * @hide 758 */ 759 public boolean removeSubTask(int taskId, int subTaskIndex) 760 throws SecurityException { 761 try { 762 return ActivityManagerNative.getDefault().removeSubTask(taskId, subTaskIndex); 763 } catch (RemoteException e) { 764 // System dead, we will be dead too soon! 765 return false; 766 } 767 } 768 769 /** 770 * If set, the process of the root activity of the task will be killed 771 * as part of removing the task. 772 * @hide 773 */ 774 public static final int REMOVE_TASK_KILL_PROCESS = 0x0001; 775 776 /** 777 * Completely remove the given task. 778 * 779 * @param taskId Identifier of the task to be removed. 780 * @param flags Additional operational flags. May be 0 or 781 * {@link #REMOVE_TASK_KILL_PROCESS}. 782 * @return Returns true if the given task was found and removed. 783 * 784 * @hide 785 */ 786 public boolean removeTask(int taskId, int flags) 787 throws SecurityException { 788 try { 789 return ActivityManagerNative.getDefault().removeTask(taskId, flags); 790 } catch (RemoteException e) { 791 // System dead, we will be dead too soon! 792 return false; 793 } 794 } 795 796 /** @hide */ 797 public static class TaskThumbnails implements Parcelable { 798 public Bitmap mainThumbnail; 799 800 public int numSubThumbbails; 801 802 /** @hide */ 803 public IThumbnailRetriever retriever; 804 805 public TaskThumbnails() { 806 } 807 808 public Bitmap getSubThumbnail(int index) { 809 try { 810 return retriever.getThumbnail(index); 811 } catch (RemoteException e) { 812 return null; 813 } 814 } 815 816 public int describeContents() { 817 return 0; 818 } 819 820 public void writeToParcel(Parcel dest, int flags) { 821 if (mainThumbnail != null) { 822 dest.writeInt(1); 823 mainThumbnail.writeToParcel(dest, 0); 824 } else { 825 dest.writeInt(0); 826 } 827 dest.writeInt(numSubThumbbails); 828 dest.writeStrongInterface(retriever); 829 } 830 831 public void readFromParcel(Parcel source) { 832 if (source.readInt() != 0) { 833 mainThumbnail = Bitmap.CREATOR.createFromParcel(source); 834 } else { 835 mainThumbnail = null; 836 } 837 numSubThumbbails = source.readInt(); 838 retriever = IThumbnailRetriever.Stub.asInterface(source.readStrongBinder()); 839 } 840 841 public static final Creator<TaskThumbnails> CREATOR = new Creator<TaskThumbnails>() { 842 public TaskThumbnails createFromParcel(Parcel source) { 843 return new TaskThumbnails(source); 844 } 845 public TaskThumbnails[] newArray(int size) { 846 return new TaskThumbnails[size]; 847 } 848 }; 849 850 private TaskThumbnails(Parcel source) { 851 readFromParcel(source); 852 } 853 } 854 855 /** @hide */ 856 public TaskThumbnails getTaskThumbnails(int id) throws SecurityException { 857 try { 858 return ActivityManagerNative.getDefault().getTaskThumbnails(id); 859 } catch (RemoteException e) { 860 // System dead, we will be dead too soon! 861 return null; 862 } 863 } 864 865 /** @hide */ 866 public Bitmap getTaskTopThumbnail(int id) throws SecurityException { 867 try { 868 return ActivityManagerNative.getDefault().getTaskTopThumbnail(id); 869 } catch (RemoteException e) { 870 // System dead, we will be dead too soon! 871 return null; 872 } 873 } 874 875 /** 876 * Flag for {@link #moveTaskToFront(int, int)}: also move the "home" 877 * activity along with the task, so it is positioned immediately behind 878 * the task. 879 */ 880 public static final int MOVE_TASK_WITH_HOME = 0x00000001; 881 882 /** 883 * Flag for {@link #moveTaskToFront(int, int)}: don't count this as a 884 * user-instigated action, so the current activity will not receive a 885 * hint that the user is leaving. 886 */ 887 public static final int MOVE_TASK_NO_USER_ACTION = 0x00000002; 888 889 /** 890 * Equivalent to calling {@link #moveTaskToFront(int, int, Bundle)} 891 * with a null options argument. 892 * 893 * @param taskId The identifier of the task to be moved, as found in 894 * {@link RunningTaskInfo} or {@link RecentTaskInfo}. 895 * @param flags Additional operational flags, 0 or more of 896 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}. 897 */ 898 public void moveTaskToFront(int taskId, int flags) { 899 moveTaskToFront(taskId, flags, null); 900 } 901 902 /** 903 * Ask that the task associated with a given task ID be moved to the 904 * front of the stack, so it is now visible to the user. Requires that 905 * the caller hold permission {@link android.Manifest.permission#REORDER_TASKS} 906 * or a SecurityException will be thrown. 907 * 908 * @param taskId The identifier of the task to be moved, as found in 909 * {@link RunningTaskInfo} or {@link RecentTaskInfo}. 910 * @param flags Additional operational flags, 0 or more of 911 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}. 912 * @param options Additional options for the operation, either null or 913 * as per {@link Context#startActivity(Intent, android.os.Bundle) 914 * Context.startActivity(Intent, Bundle)}. 915 */ 916 public void moveTaskToFront(int taskId, int flags, Bundle options) { 917 try { 918 ActivityManagerNative.getDefault().moveTaskToFront(taskId, flags, options); 919 } catch (RemoteException e) { 920 // System dead, we will be dead too soon! 921 } 922 } 923 924 /** 925 * Information you can retrieve about a particular Service that is 926 * currently running in the system. 927 */ 928 public static class RunningServiceInfo implements Parcelable { 929 /** 930 * The service component. 931 */ 932 public ComponentName service; 933 934 /** 935 * If non-zero, this is the process the service is running in. 936 */ 937 public int pid; 938 939 /** 940 * The UID that owns this service. 941 */ 942 public int uid; 943 944 /** 945 * The name of the process this service runs in. 946 */ 947 public String process; 948 949 /** 950 * Set to true if the service has asked to run as a foreground process. 951 */ 952 public boolean foreground; 953 954 /** 955 * The time when the service was first made active, either by someone 956 * starting or binding to it. This 957 * is in units of {@link android.os.SystemClock#elapsedRealtime()}. 958 */ 959 public long activeSince; 960 961 /** 962 * Set to true if this service has been explicitly started. 963 */ 964 public boolean started; 965 966 /** 967 * Number of clients connected to the service. 968 */ 969 public int clientCount; 970 971 /** 972 * Number of times the service's process has crashed while the service 973 * is running. 974 */ 975 public int crashCount; 976 977 /** 978 * The time when there was last activity in the service (either 979 * explicit requests to start it or clients binding to it). This 980 * is in units of {@link android.os.SystemClock#uptimeMillis()}. 981 */ 982 public long lastActivityTime; 983 984 /** 985 * If non-zero, this service is not currently running, but scheduled to 986 * restart at the given time. 987 */ 988 public long restarting; 989 990 /** 991 * Bit for {@link #flags}: set if this service has been 992 * explicitly started. 993 */ 994 public static final int FLAG_STARTED = 1<<0; 995 996 /** 997 * Bit for {@link #flags}: set if the service has asked to 998 * run as a foreground process. 999 */ 1000 public static final int FLAG_FOREGROUND = 1<<1; 1001 1002 /** 1003 * Bit for {@link #flags): set if the service is running in a 1004 * core system process. 1005 */ 1006 public static final int FLAG_SYSTEM_PROCESS = 1<<2; 1007 1008 /** 1009 * Bit for {@link #flags): set if the service is running in a 1010 * persistent process. 1011 */ 1012 public static final int FLAG_PERSISTENT_PROCESS = 1<<3; 1013 1014 /** 1015 * Running flags. 1016 */ 1017 public int flags; 1018 1019 /** 1020 * For special services that are bound to by system code, this is 1021 * the package that holds the binding. 1022 */ 1023 public String clientPackage; 1024 1025 /** 1026 * For special services that are bound to by system code, this is 1027 * a string resource providing a user-visible label for who the 1028 * client is. 1029 */ 1030 public int clientLabel; 1031 1032 public RunningServiceInfo() { 1033 } 1034 1035 public int describeContents() { 1036 return 0; 1037 } 1038 1039 public void writeToParcel(Parcel dest, int flags) { 1040 ComponentName.writeToParcel(service, dest); 1041 dest.writeInt(pid); 1042 dest.writeInt(uid); 1043 dest.writeString(process); 1044 dest.writeInt(foreground ? 1 : 0); 1045 dest.writeLong(activeSince); 1046 dest.writeInt(started ? 1 : 0); 1047 dest.writeInt(clientCount); 1048 dest.writeInt(crashCount); 1049 dest.writeLong(lastActivityTime); 1050 dest.writeLong(restarting); 1051 dest.writeInt(this.flags); 1052 dest.writeString(clientPackage); 1053 dest.writeInt(clientLabel); 1054 } 1055 1056 public void readFromParcel(Parcel source) { 1057 service = ComponentName.readFromParcel(source); 1058 pid = source.readInt(); 1059 uid = source.readInt(); 1060 process = source.readString(); 1061 foreground = source.readInt() != 0; 1062 activeSince = source.readLong(); 1063 started = source.readInt() != 0; 1064 clientCount = source.readInt(); 1065 crashCount = source.readInt(); 1066 lastActivityTime = source.readLong(); 1067 restarting = source.readLong(); 1068 flags = source.readInt(); 1069 clientPackage = source.readString(); 1070 clientLabel = source.readInt(); 1071 } 1072 1073 public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() { 1074 public RunningServiceInfo createFromParcel(Parcel source) { 1075 return new RunningServiceInfo(source); 1076 } 1077 public RunningServiceInfo[] newArray(int size) { 1078 return new RunningServiceInfo[size]; 1079 } 1080 }; 1081 1082 private RunningServiceInfo(Parcel source) { 1083 readFromParcel(source); 1084 } 1085 } 1086 1087 /** 1088 * Return a list of the services that are currently running. 1089 * 1090 * <p><b>Note: this method is only intended for debugging or implementing 1091 * service management type user interfaces.</b></p> 1092 * 1093 * @param maxNum The maximum number of entries to return in the list. The 1094 * actual number returned may be smaller, depending on how many services 1095 * are running. 1096 * 1097 * @return Returns a list of RunningServiceInfo records describing each of 1098 * the running tasks. 1099 */ 1100 public List<RunningServiceInfo> getRunningServices(int maxNum) 1101 throws SecurityException { 1102 try { 1103 return ActivityManagerNative.getDefault() 1104 .getServices(maxNum, 0); 1105 } catch (RemoteException e) { 1106 // System dead, we will be dead too soon! 1107 return null; 1108 } 1109 } 1110 1111 /** 1112 * Returns a PendingIntent you can start to show a control panel for the 1113 * given running service. If the service does not have a control panel, 1114 * null is returned. 1115 */ 1116 public PendingIntent getRunningServiceControlPanel(ComponentName service) 1117 throws SecurityException { 1118 try { 1119 return ActivityManagerNative.getDefault() 1120 .getRunningServiceControlPanel(service); 1121 } catch (RemoteException e) { 1122 // System dead, we will be dead too soon! 1123 return null; 1124 } 1125 } 1126 1127 /** 1128 * Information you can retrieve about the available memory through 1129 * {@link ActivityManager#getMemoryInfo}. 1130 */ 1131 public static class MemoryInfo implements Parcelable { 1132 /** 1133 * The available memory on the system. This number should not 1134 * be considered absolute: due to the nature of the kernel, a significant 1135 * portion of this memory is actually in use and needed for the overall 1136 * system to run well. 1137 */ 1138 public long availMem; 1139 1140 /** 1141 * The total memory accessible by the kernel. This is basically the 1142 * RAM size of the device, not including below-kernel fixed allocations 1143 * like DMA buffers, RAM for the baseband CPU, etc. 1144 */ 1145 public long totalMem; 1146 1147 /** 1148 * The threshold of {@link #availMem} at which we consider memory to be 1149 * low and start killing background services and other non-extraneous 1150 * processes. 1151 */ 1152 public long threshold; 1153 1154 /** 1155 * Set to true if the system considers itself to currently be in a low 1156 * memory situation. 1157 */ 1158 public boolean lowMemory; 1159 1160 /** @hide */ 1161 public long hiddenAppThreshold; 1162 /** @hide */ 1163 public long secondaryServerThreshold; 1164 /** @hide */ 1165 public long visibleAppThreshold; 1166 /** @hide */ 1167 public long foregroundAppThreshold; 1168 1169 public MemoryInfo() { 1170 } 1171 1172 public int describeContents() { 1173 return 0; 1174 } 1175 1176 public void writeToParcel(Parcel dest, int flags) { 1177 dest.writeLong(availMem); 1178 dest.writeLong(totalMem); 1179 dest.writeLong(threshold); 1180 dest.writeInt(lowMemory ? 1 : 0); 1181 dest.writeLong(hiddenAppThreshold); 1182 dest.writeLong(secondaryServerThreshold); 1183 dest.writeLong(visibleAppThreshold); 1184 dest.writeLong(foregroundAppThreshold); 1185 } 1186 1187 public void readFromParcel(Parcel source) { 1188 availMem = source.readLong(); 1189 totalMem = source.readLong(); 1190 threshold = source.readLong(); 1191 lowMemory = source.readInt() != 0; 1192 hiddenAppThreshold = source.readLong(); 1193 secondaryServerThreshold = source.readLong(); 1194 visibleAppThreshold = source.readLong(); 1195 foregroundAppThreshold = source.readLong(); 1196 } 1197 1198 public static final Creator<MemoryInfo> CREATOR 1199 = new Creator<MemoryInfo>() { 1200 public MemoryInfo createFromParcel(Parcel source) { 1201 return new MemoryInfo(source); 1202 } 1203 public MemoryInfo[] newArray(int size) { 1204 return new MemoryInfo[size]; 1205 } 1206 }; 1207 1208 private MemoryInfo(Parcel source) { 1209 readFromParcel(source); 1210 } 1211 } 1212 1213 /** 1214 * Return general information about the memory state of the system. This 1215 * can be used to help decide how to manage your own memory, though note 1216 * that polling is not recommended and 1217 * {@link android.content.ComponentCallbacks2#onTrimMemory(int) 1218 * ComponentCallbacks2.onTrimMemory(int)} is the preferred way to do this. 1219 * Also see {@link #getMyMemoryState} for how to retrieve the current trim 1220 * level of your process as needed, which gives a better hint for how to 1221 * manage its memory. 1222 */ 1223 public void getMemoryInfo(MemoryInfo outInfo) { 1224 try { 1225 ActivityManagerNative.getDefault().getMemoryInfo(outInfo); 1226 } catch (RemoteException e) { 1227 } 1228 } 1229 1230 /** 1231 * Information you can retrieve about the WindowManager StackBox hierarchy. 1232 * @hide 1233 */ 1234 public static class StackBoxInfo implements Parcelable { 1235 public int stackBoxId; 1236 public float weight; 1237 public boolean vertical; 1238 public Rect bounds; 1239 public StackBoxInfo[] children; 1240 public int stackId; 1241 public StackInfo stack; 1242 1243 @Override 1244 public int describeContents() { 1245 return 0; 1246 } 1247 1248 @Override 1249 public void writeToParcel(Parcel dest, int flags) { 1250 dest.writeInt(stackBoxId); 1251 dest.writeFloat(weight); 1252 dest.writeInt(vertical ? 1 : 0); 1253 bounds.writeToParcel(dest, flags); 1254 dest.writeInt(stackId); 1255 if (children != null) { 1256 children[0].writeToParcel(dest, flags); 1257 children[1].writeToParcel(dest, flags); 1258 } else { 1259 stack.writeToParcel(dest, flags); 1260 } 1261 } 1262 1263 public void readFromParcel(Parcel source) { 1264 stackBoxId = source.readInt(); 1265 weight = source.readFloat(); 1266 vertical = source.readInt() == 1; 1267 bounds = Rect.CREATOR.createFromParcel(source); 1268 stackId = source.readInt(); 1269 if (stackId == -1) { 1270 children = new StackBoxInfo[2]; 1271 children[0] = StackBoxInfo.CREATOR.createFromParcel(source); 1272 children[1] = StackBoxInfo.CREATOR.createFromParcel(source); 1273 } else { 1274 stack = StackInfo.CREATOR.createFromParcel(source); 1275 } 1276 } 1277 1278 public static final Creator<StackBoxInfo> CREATOR = 1279 new Creator<ActivityManager.StackBoxInfo>() { 1280 1281 @Override 1282 public StackBoxInfo createFromParcel(Parcel source) { 1283 return new StackBoxInfo(source); 1284 } 1285 1286 @Override 1287 public StackBoxInfo[] newArray(int size) { 1288 return new StackBoxInfo[size]; 1289 } 1290 }; 1291 1292 public StackBoxInfo() { 1293 } 1294 1295 public StackBoxInfo(Parcel source) { 1296 readFromParcel(source); 1297 } 1298 1299 public String toString(String prefix) { 1300 StringBuilder sb = new StringBuilder(256); 1301 sb.append(prefix); sb.append("Box id=" + stackBoxId); sb.append(" weight=" + weight); 1302 sb.append(" vertical=" + vertical); sb.append(" bounds=" + bounds.toShortString()); 1303 sb.append("\n"); 1304 if (children != null) { 1305 sb.append(prefix); sb.append("First child=\n"); 1306 sb.append(children[0].toString(prefix + " ")); 1307 sb.append(prefix); sb.append("Second child=\n"); 1308 sb.append(children[1].toString(prefix + " ")); 1309 } else { 1310 sb.append(prefix); sb.append("Stack=\n"); 1311 sb.append(stack.toString(prefix + " ")); 1312 } 1313 return sb.toString(); 1314 } 1315 1316 @Override 1317 public String toString() { 1318 return toString(""); 1319 } 1320 } 1321 1322 /** 1323 * Information you can retrieve about an ActivityStack in the system. 1324 * @hide 1325 */ 1326 public static class StackInfo implements Parcelable { 1327 public int stackId; 1328 public Rect bounds; 1329 public int[] taskIds; 1330 public String[] taskNames; 1331 1332 @Override 1333 public int describeContents() { 1334 return 0; 1335 } 1336 1337 @Override 1338 public void writeToParcel(Parcel dest, int flags) { 1339 dest.writeInt(stackId); 1340 dest.writeInt(bounds.left); 1341 dest.writeInt(bounds.top); 1342 dest.writeInt(bounds.right); 1343 dest.writeInt(bounds.bottom); 1344 dest.writeIntArray(taskIds); 1345 dest.writeStringArray(taskNames); 1346 } 1347 1348 public void readFromParcel(Parcel source) { 1349 stackId = source.readInt(); 1350 bounds = new Rect( 1351 source.readInt(), source.readInt(), source.readInt(), source.readInt()); 1352 taskIds = source.createIntArray(); 1353 taskNames = source.createStringArray(); 1354 } 1355 1356 public static final Creator<StackInfo> CREATOR = new Creator<StackInfo>() { 1357 @Override 1358 public StackInfo createFromParcel(Parcel source) { 1359 return new StackInfo(source); 1360 } 1361 @Override 1362 public StackInfo[] newArray(int size) { 1363 return new StackInfo[size]; 1364 } 1365 }; 1366 1367 public StackInfo() { 1368 } 1369 1370 private StackInfo(Parcel source) { 1371 readFromParcel(source); 1372 } 1373 1374 public String toString(String prefix) { 1375 StringBuilder sb = new StringBuilder(256); 1376 sb.append(prefix); sb.append("Stack id="); sb.append(stackId); 1377 sb.append(" bounds="); sb.append(bounds.toShortString()); sb.append("\n"); 1378 prefix = prefix + " "; 1379 for (int i = 0; i < taskIds.length; ++i) { 1380 sb.append(prefix); sb.append("taskId="); sb.append(taskIds[i]); 1381 sb.append(": "); sb.append(taskNames[i]); sb.append("\n"); 1382 } 1383 return sb.toString(); 1384 } 1385 1386 @Override 1387 public String toString() { 1388 return toString(""); 1389 } 1390 } 1391 1392 /** 1393 * @hide 1394 */ 1395 public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) { 1396 try { 1397 return ActivityManagerNative.getDefault().clearApplicationUserData(packageName, 1398 observer, UserHandle.myUserId()); 1399 } catch (RemoteException e) { 1400 return false; 1401 } 1402 } 1403 1404 /** 1405 * Permits an application to erase its own data from disk. This is equivalent to 1406 * the user choosing to clear the app's data from within the device settings UI. 1407 * 1408 * @return {@code true} if the application successfully requested that the application's 1409 * data be erased; {@code false} otherwise. 1410 */ 1411 public boolean clearApplicationUserData() { 1412 return clearApplicationUserData(mContext.getPackageName(), null); 1413 } 1414 1415 /** 1416 * Information you can retrieve about any processes that are in an error condition. 1417 */ 1418 public static class ProcessErrorStateInfo implements Parcelable { 1419 /** 1420 * Condition codes 1421 */ 1422 public static final int NO_ERROR = 0; 1423 public static final int CRASHED = 1; 1424 public static final int NOT_RESPONDING = 2; 1425 1426 /** 1427 * The condition that the process is in. 1428 */ 1429 public int condition; 1430 1431 /** 1432 * The process name in which the crash or error occurred. 1433 */ 1434 public String processName; 1435 1436 /** 1437 * The pid of this process; 0 if none 1438 */ 1439 public int pid; 1440 1441 /** 1442 * The kernel user-ID that has been assigned to this process; 1443 * currently this is not a unique ID (multiple applications can have 1444 * the same uid). 1445 */ 1446 public int uid; 1447 1448 /** 1449 * The activity name associated with the error, if known. May be null. 1450 */ 1451 public String tag; 1452 1453 /** 1454 * A short message describing the error condition. 1455 */ 1456 public String shortMsg; 1457 1458 /** 1459 * A long message describing the error condition. 1460 */ 1461 public String longMsg; 1462 1463 /** 1464 * The stack trace where the error originated. May be null. 1465 */ 1466 public String stackTrace; 1467 1468 /** 1469 * to be deprecated: This value will always be null. 1470 */ 1471 public byte[] crashData = null; 1472 1473 public ProcessErrorStateInfo() { 1474 } 1475 1476 @Override 1477 public int describeContents() { 1478 return 0; 1479 } 1480 1481 @Override 1482 public void writeToParcel(Parcel dest, int flags) { 1483 dest.writeInt(condition); 1484 dest.writeString(processName); 1485 dest.writeInt(pid); 1486 dest.writeInt(uid); 1487 dest.writeString(tag); 1488 dest.writeString(shortMsg); 1489 dest.writeString(longMsg); 1490 dest.writeString(stackTrace); 1491 } 1492 1493 public void readFromParcel(Parcel source) { 1494 condition = source.readInt(); 1495 processName = source.readString(); 1496 pid = source.readInt(); 1497 uid = source.readInt(); 1498 tag = source.readString(); 1499 shortMsg = source.readString(); 1500 longMsg = source.readString(); 1501 stackTrace = source.readString(); 1502 } 1503 1504 public static final Creator<ProcessErrorStateInfo> CREATOR = 1505 new Creator<ProcessErrorStateInfo>() { 1506 public ProcessErrorStateInfo createFromParcel(Parcel source) { 1507 return new ProcessErrorStateInfo(source); 1508 } 1509 public ProcessErrorStateInfo[] newArray(int size) { 1510 return new ProcessErrorStateInfo[size]; 1511 } 1512 }; 1513 1514 private ProcessErrorStateInfo(Parcel source) { 1515 readFromParcel(source); 1516 } 1517 } 1518 1519 /** 1520 * Returns a list of any processes that are currently in an error condition. The result 1521 * will be null if all processes are running properly at this time. 1522 * 1523 * @return Returns a list of ProcessErrorStateInfo records, or null if there are no 1524 * current error conditions (it will not return an empty list). This list ordering is not 1525 * specified. 1526 */ 1527 public List<ProcessErrorStateInfo> getProcessesInErrorState() { 1528 try { 1529 return ActivityManagerNative.getDefault().getProcessesInErrorState(); 1530 } catch (RemoteException e) { 1531 return null; 1532 } 1533 } 1534 1535 /** 1536 * Information you can retrieve about a running process. 1537 */ 1538 public static class RunningAppProcessInfo implements Parcelable { 1539 /** 1540 * The name of the process that this object is associated with 1541 */ 1542 public String processName; 1543 1544 /** 1545 * The pid of this process; 0 if none 1546 */ 1547 public int pid; 1548 1549 /** 1550 * The user id of this process. 1551 */ 1552 public int uid; 1553 1554 /** 1555 * All packages that have been loaded into the process. 1556 */ 1557 public String pkgList[]; 1558 1559 /** 1560 * Constant for {@link #flags}: this is an app that is unable to 1561 * correctly save its state when going to the background, 1562 * so it can not be killed while in the background. 1563 * @hide 1564 */ 1565 public static final int FLAG_CANT_SAVE_STATE = 1<<0; 1566 1567 /** 1568 * Constant for {@link #flags}: this process is associated with a 1569 * persistent system app. 1570 * @hide 1571 */ 1572 public static final int FLAG_PERSISTENT = 1<<1; 1573 1574 /** 1575 * Constant for {@link #flags}: this process is associated with a 1576 * persistent system app. 1577 * @hide 1578 */ 1579 public static final int FLAG_HAS_ACTIVITIES = 1<<2; 1580 1581 /** 1582 * Flags of information. May be any of 1583 * {@link #FLAG_CANT_SAVE_STATE}. 1584 * @hide 1585 */ 1586 public int flags; 1587 1588 /** 1589 * Last memory trim level reported to the process: corresponds to 1590 * the values supplied to {@link android.content.ComponentCallbacks2#onTrimMemory(int) 1591 * ComponentCallbacks2.onTrimMemory(int)}. 1592 */ 1593 public int lastTrimLevel; 1594 1595 /** 1596 * Constant for {@link #importance}: this is a persistent process. 1597 * Only used when reporting to process observers. 1598 * @hide 1599 */ 1600 public static final int IMPORTANCE_PERSISTENT = 50; 1601 1602 /** 1603 * Constant for {@link #importance}: this process is running the 1604 * foreground UI. 1605 */ 1606 public static final int IMPORTANCE_FOREGROUND = 100; 1607 1608 /** 1609 * Constant for {@link #importance}: this process is running something 1610 * that is actively visible to the user, though not in the immediate 1611 * foreground. 1612 */ 1613 public static final int IMPORTANCE_VISIBLE = 200; 1614 1615 /** 1616 * Constant for {@link #importance}: this process is running something 1617 * that is considered to be actively perceptible to the user. An 1618 * example would be an application performing background music playback. 1619 */ 1620 public static final int IMPORTANCE_PERCEPTIBLE = 130; 1621 1622 /** 1623 * Constant for {@link #importance}: this process is running an 1624 * application that can not save its state, and thus can't be killed 1625 * while in the background. 1626 * @hide 1627 */ 1628 public static final int IMPORTANCE_CANT_SAVE_STATE = 170; 1629 1630 /** 1631 * Constant for {@link #importance}: this process is contains services 1632 * that should remain running. 1633 */ 1634 public static final int IMPORTANCE_SERVICE = 300; 1635 1636 /** 1637 * Constant for {@link #importance}: this process process contains 1638 * background code that is expendable. 1639 */ 1640 public static final int IMPORTANCE_BACKGROUND = 400; 1641 1642 /** 1643 * Constant for {@link #importance}: this process is empty of any 1644 * actively running code. 1645 */ 1646 public static final int IMPORTANCE_EMPTY = 500; 1647 1648 /** 1649 * The relative importance level that the system places on this 1650 * process. May be one of {@link #IMPORTANCE_FOREGROUND}, 1651 * {@link #IMPORTANCE_VISIBLE}, {@link #IMPORTANCE_SERVICE}, 1652 * {@link #IMPORTANCE_BACKGROUND}, or {@link #IMPORTANCE_EMPTY}. These 1653 * constants are numbered so that "more important" values are always 1654 * smaller than "less important" values. 1655 */ 1656 public int importance; 1657 1658 /** 1659 * An additional ordering within a particular {@link #importance} 1660 * category, providing finer-grained information about the relative 1661 * utility of processes within a category. This number means nothing 1662 * except that a smaller values are more recently used (and thus 1663 * more important). Currently an LRU value is only maintained for 1664 * the {@link #IMPORTANCE_BACKGROUND} category, though others may 1665 * be maintained in the future. 1666 */ 1667 public int lru; 1668 1669 /** 1670 * Constant for {@link #importanceReasonCode}: nothing special has 1671 * been specified for the reason for this level. 1672 */ 1673 public static final int REASON_UNKNOWN = 0; 1674 1675 /** 1676 * Constant for {@link #importanceReasonCode}: one of the application's 1677 * content providers is being used by another process. The pid of 1678 * the client process is in {@link #importanceReasonPid} and the 1679 * target provider in this process is in 1680 * {@link #importanceReasonComponent}. 1681 */ 1682 public static final int REASON_PROVIDER_IN_USE = 1; 1683 1684 /** 1685 * Constant for {@link #importanceReasonCode}: one of the application's 1686 * content providers is being used by another process. The pid of 1687 * the client process is in {@link #importanceReasonPid} and the 1688 * target provider in this process is in 1689 * {@link #importanceReasonComponent}. 1690 */ 1691 public static final int REASON_SERVICE_IN_USE = 2; 1692 1693 /** 1694 * The reason for {@link #importance}, if any. 1695 */ 1696 public int importanceReasonCode; 1697 1698 /** 1699 * For the specified values of {@link #importanceReasonCode}, this 1700 * is the process ID of the other process that is a client of this 1701 * process. This will be 0 if no other process is using this one. 1702 */ 1703 public int importanceReasonPid; 1704 1705 /** 1706 * For the specified values of {@link #importanceReasonCode}, this 1707 * is the name of the component that is being used in this process. 1708 */ 1709 public ComponentName importanceReasonComponent; 1710 1711 /** 1712 * When {@link #importanceReasonPid} is non-0, this is the importance 1713 * of the other pid. @hide 1714 */ 1715 public int importanceReasonImportance; 1716 1717 public RunningAppProcessInfo() { 1718 importance = IMPORTANCE_FOREGROUND; 1719 importanceReasonCode = REASON_UNKNOWN; 1720 } 1721 1722 public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) { 1723 processName = pProcessName; 1724 pid = pPid; 1725 pkgList = pArr; 1726 } 1727 1728 public int describeContents() { 1729 return 0; 1730 } 1731 1732 public void writeToParcel(Parcel dest, int flags) { 1733 dest.writeString(processName); 1734 dest.writeInt(pid); 1735 dest.writeInt(uid); 1736 dest.writeStringArray(pkgList); 1737 dest.writeInt(this.flags); 1738 dest.writeInt(lastTrimLevel); 1739 dest.writeInt(importance); 1740 dest.writeInt(lru); 1741 dest.writeInt(importanceReasonCode); 1742 dest.writeInt(importanceReasonPid); 1743 ComponentName.writeToParcel(importanceReasonComponent, dest); 1744 dest.writeInt(importanceReasonImportance); 1745 } 1746 1747 public void readFromParcel(Parcel source) { 1748 processName = source.readString(); 1749 pid = source.readInt(); 1750 uid = source.readInt(); 1751 pkgList = source.readStringArray(); 1752 flags = source.readInt(); 1753 lastTrimLevel = source.readInt(); 1754 importance = source.readInt(); 1755 lru = source.readInt(); 1756 importanceReasonCode = source.readInt(); 1757 importanceReasonPid = source.readInt(); 1758 importanceReasonComponent = ComponentName.readFromParcel(source); 1759 importanceReasonImportance = source.readInt(); 1760 } 1761 1762 public static final Creator<RunningAppProcessInfo> CREATOR = 1763 new Creator<RunningAppProcessInfo>() { 1764 public RunningAppProcessInfo createFromParcel(Parcel source) { 1765 return new RunningAppProcessInfo(source); 1766 } 1767 public RunningAppProcessInfo[] newArray(int size) { 1768 return new RunningAppProcessInfo[size]; 1769 } 1770 }; 1771 1772 private RunningAppProcessInfo(Parcel source) { 1773 readFromParcel(source); 1774 } 1775 } 1776 1777 /** 1778 * Returns a list of application processes installed on external media 1779 * that are running on the device. 1780 * 1781 * <p><b>Note: this method is only intended for debugging or building 1782 * a user-facing process management UI.</b></p> 1783 * 1784 * @return Returns a list of ApplicationInfo records, or null if none 1785 * This list ordering is not specified. 1786 * @hide 1787 */ 1788 public List<ApplicationInfo> getRunningExternalApplications() { 1789 try { 1790 return ActivityManagerNative.getDefault().getRunningExternalApplications(); 1791 } catch (RemoteException e) { 1792 return null; 1793 } 1794 } 1795 1796 /** 1797 * Returns a list of application processes that are running on the device. 1798 * 1799 * <p><b>Note: this method is only intended for debugging or building 1800 * a user-facing process management UI.</b></p> 1801 * 1802 * @return Returns a list of RunningAppProcessInfo records, or null if there are no 1803 * running processes (it will not return an empty list). This list ordering is not 1804 * specified. 1805 */ 1806 public List<RunningAppProcessInfo> getRunningAppProcesses() { 1807 try { 1808 return ActivityManagerNative.getDefault().getRunningAppProcesses(); 1809 } catch (RemoteException e) { 1810 return null; 1811 } 1812 } 1813 1814 /** 1815 * Return global memory state information for the calling process. This 1816 * does not fill in all fields of the {@link RunningAppProcessInfo}. The 1817 * only fields that will be filled in are 1818 * {@link RunningAppProcessInfo#pid}, 1819 * {@link RunningAppProcessInfo#uid}, 1820 * {@link RunningAppProcessInfo#lastTrimLevel}, 1821 * {@link RunningAppProcessInfo#importance}, 1822 * {@link RunningAppProcessInfo#lru}, and 1823 * {@link RunningAppProcessInfo#importanceReasonCode}. 1824 */ 1825 static public void getMyMemoryState(RunningAppProcessInfo outState) { 1826 try { 1827 ActivityManagerNative.getDefault().getMyMemoryState(outState); 1828 } catch (RemoteException e) { 1829 } 1830 } 1831 1832 /** 1833 * Return information about the memory usage of one or more processes. 1834 * 1835 * <p><b>Note: this method is only intended for debugging or building 1836 * a user-facing process management UI.</b></p> 1837 * 1838 * @param pids The pids of the processes whose memory usage is to be 1839 * retrieved. 1840 * @return Returns an array of memory information, one for each 1841 * requested pid. 1842 */ 1843 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) { 1844 try { 1845 return ActivityManagerNative.getDefault().getProcessMemoryInfo(pids); 1846 } catch (RemoteException e) { 1847 return null; 1848 } 1849 } 1850 1851 /** 1852 * @deprecated This is now just a wrapper for 1853 * {@link #killBackgroundProcesses(String)}; the previous behavior here 1854 * is no longer available to applications because it allows them to 1855 * break other applications by removing their alarms, stopping their 1856 * services, etc. 1857 */ 1858 @Deprecated 1859 public void restartPackage(String packageName) { 1860 killBackgroundProcesses(packageName); 1861 } 1862 1863 /** 1864 * Have the system immediately kill all background processes associated 1865 * with the given package. This is the same as the kernel killing those 1866 * processes to reclaim memory; the system will take care of restarting 1867 * these processes in the future as needed. 1868 * 1869 * <p>You must hold the permission 1870 * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to 1871 * call this method. 1872 * 1873 * @param packageName The name of the package whose processes are to 1874 * be killed. 1875 */ 1876 public void killBackgroundProcesses(String packageName) { 1877 try { 1878 ActivityManagerNative.getDefault().killBackgroundProcesses(packageName, 1879 UserHandle.myUserId()); 1880 } catch (RemoteException e) { 1881 } 1882 } 1883 1884 /** 1885 * Have the system perform a force stop of everything associated with 1886 * the given application package. All processes that share its uid 1887 * will be killed, all services it has running stopped, all activities 1888 * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED} 1889 * broadcast will be sent, so that any of its registered alarms can 1890 * be stopped, notifications removed, etc. 1891 * 1892 * <p>You must hold the permission 1893 * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to 1894 * call this method. 1895 * 1896 * @param packageName The name of the package to be stopped. 1897 * 1898 * @hide This is not available to third party applications due to 1899 * it allowing them to break other applications by stopping their 1900 * services, removing their alarms, etc. 1901 */ 1902 public void forceStopPackage(String packageName) { 1903 try { 1904 ActivityManagerNative.getDefault().forceStopPackage(packageName, 1905 UserHandle.myUserId()); 1906 } catch (RemoteException e) { 1907 } 1908 } 1909 1910 /** 1911 * Get the device configuration attributes. 1912 */ 1913 public ConfigurationInfo getDeviceConfigurationInfo() { 1914 try { 1915 return ActivityManagerNative.getDefault().getDeviceConfigurationInfo(); 1916 } catch (RemoteException e) { 1917 } 1918 return null; 1919 } 1920 1921 /** 1922 * Get the preferred density of icons for the launcher. This is used when 1923 * custom drawables are created (e.g., for shortcuts). 1924 * 1925 * @return density in terms of DPI 1926 */ 1927 public int getLauncherLargeIconDensity() { 1928 final Resources res = mContext.getResources(); 1929 final int density = res.getDisplayMetrics().densityDpi; 1930 final int sw = res.getConfiguration().smallestScreenWidthDp; 1931 1932 if (sw < 600) { 1933 // Smaller than approx 7" tablets, use the regular icon size. 1934 return density; 1935 } 1936 1937 switch (density) { 1938 case DisplayMetrics.DENSITY_LOW: 1939 return DisplayMetrics.DENSITY_MEDIUM; 1940 case DisplayMetrics.DENSITY_MEDIUM: 1941 return DisplayMetrics.DENSITY_HIGH; 1942 case DisplayMetrics.DENSITY_TV: 1943 return DisplayMetrics.DENSITY_XHIGH; 1944 case DisplayMetrics.DENSITY_HIGH: 1945 return DisplayMetrics.DENSITY_XHIGH; 1946 case DisplayMetrics.DENSITY_XHIGH: 1947 return DisplayMetrics.DENSITY_XXHIGH; 1948 case DisplayMetrics.DENSITY_XXHIGH: 1949 return DisplayMetrics.DENSITY_XHIGH * 2; 1950 default: 1951 // The density is some abnormal value. Return some other 1952 // abnormal value that is a reasonable scaling of it. 1953 return (int)((density*1.5f)+.5f); 1954 } 1955 } 1956 1957 /** 1958 * Get the preferred launcher icon size. This is used when custom drawables 1959 * are created (e.g., for shortcuts). 1960 * 1961 * @return dimensions of square icons in terms of pixels 1962 */ 1963 public int getLauncherLargeIconSize() { 1964 final Resources res = mContext.getResources(); 1965 final int size = res.getDimensionPixelSize(android.R.dimen.app_icon_size); 1966 final int sw = res.getConfiguration().smallestScreenWidthDp; 1967 1968 if (sw < 600) { 1969 // Smaller than approx 7" tablets, use the regular icon size. 1970 return size; 1971 } 1972 1973 final int density = res.getDisplayMetrics().densityDpi; 1974 1975 switch (density) { 1976 case DisplayMetrics.DENSITY_LOW: 1977 return (size * DisplayMetrics.DENSITY_MEDIUM) / DisplayMetrics.DENSITY_LOW; 1978 case DisplayMetrics.DENSITY_MEDIUM: 1979 return (size * DisplayMetrics.DENSITY_HIGH) / DisplayMetrics.DENSITY_MEDIUM; 1980 case DisplayMetrics.DENSITY_TV: 1981 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH; 1982 case DisplayMetrics.DENSITY_HIGH: 1983 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH; 1984 case DisplayMetrics.DENSITY_XHIGH: 1985 return (size * DisplayMetrics.DENSITY_XXHIGH) / DisplayMetrics.DENSITY_XHIGH; 1986 case DisplayMetrics.DENSITY_XXHIGH: 1987 return (size * DisplayMetrics.DENSITY_XHIGH*2) / DisplayMetrics.DENSITY_XXHIGH; 1988 default: 1989 // The density is some abnormal value. Return some other 1990 // abnormal value that is a reasonable scaling of it. 1991 return (int)((size*1.5f) + .5f); 1992 } 1993 } 1994 1995 /** 1996 * Returns "true" if the user interface is currently being messed with 1997 * by a monkey. 1998 */ 1999 public static boolean isUserAMonkey() { 2000 try { 2001 return ActivityManagerNative.getDefault().isUserAMonkey(); 2002 } catch (RemoteException e) { 2003 } 2004 return false; 2005 } 2006 2007 /** 2008 * Returns "true" if device is running in a test harness. 2009 */ 2010 public static boolean isRunningInTestHarness() { 2011 return SystemProperties.getBoolean("ro.test_harness", false); 2012 } 2013 2014 /** 2015 * Returns the launch count of each installed package. 2016 * 2017 * @hide 2018 */ 2019 public Map<String, Integer> getAllPackageLaunchCounts() { 2020 try { 2021 IUsageStats usageStatsService = IUsageStats.Stub.asInterface( 2022 ServiceManager.getService("usagestats")); 2023 if (usageStatsService == null) { 2024 return new HashMap<String, Integer>(); 2025 } 2026 2027 PkgUsageStats[] allPkgUsageStats = usageStatsService.getAllPkgUsageStats(); 2028 if (allPkgUsageStats == null) { 2029 return new HashMap<String, Integer>(); 2030 } 2031 2032 Map<String, Integer> launchCounts = new HashMap<String, Integer>(); 2033 for (PkgUsageStats pkgUsageStats : allPkgUsageStats) { 2034 launchCounts.put(pkgUsageStats.packageName, pkgUsageStats.launchCount); 2035 } 2036 2037 return launchCounts; 2038 } catch (RemoteException e) { 2039 Log.w(TAG, "Could not query launch counts", e); 2040 return new HashMap<String, Integer>(); 2041 } 2042 } 2043 2044 /** @hide */ 2045 public static int checkComponentPermission(String permission, int uid, 2046 int owningUid, boolean exported) { 2047 // Root, system server get to do everything. 2048 if (uid == 0 || uid == Process.SYSTEM_UID) { 2049 return PackageManager.PERMISSION_GRANTED; 2050 } 2051 // Isolated processes don't get any permissions. 2052 if (UserHandle.isIsolated(uid)) { 2053 return PackageManager.PERMISSION_DENIED; 2054 } 2055 // If there is a uid that owns whatever is being accessed, it has 2056 // blanket access to it regardless of the permissions it requires. 2057 if (owningUid >= 0 && UserHandle.isSameApp(uid, owningUid)) { 2058 return PackageManager.PERMISSION_GRANTED; 2059 } 2060 // If the target is not exported, then nobody else can get to it. 2061 if (!exported) { 2062 Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid); 2063 return PackageManager.PERMISSION_DENIED; 2064 } 2065 if (permission == null) { 2066 return PackageManager.PERMISSION_GRANTED; 2067 } 2068 try { 2069 return AppGlobals.getPackageManager() 2070 .checkUidPermission(permission, uid); 2071 } catch (RemoteException e) { 2072 // Should never happen, but if it does... deny! 2073 Slog.e(TAG, "PackageManager is dead?!?", e); 2074 } 2075 return PackageManager.PERMISSION_DENIED; 2076 } 2077 2078 /** @hide */ 2079 public static int checkUidPermission(String permission, int uid) { 2080 try { 2081 return AppGlobals.getPackageManager() 2082 .checkUidPermission(permission, uid); 2083 } catch (RemoteException e) { 2084 // Should never happen, but if it does... deny! 2085 Slog.e(TAG, "PackageManager is dead?!?", e); 2086 } 2087 return PackageManager.PERMISSION_DENIED; 2088 } 2089 2090 /** 2091 * @hide 2092 * Helper for dealing with incoming user arguments to system service calls. 2093 * Takes care of checking permissions and converting USER_CURRENT to the 2094 * actual current user. 2095 * 2096 * @param callingPid The pid of the incoming call, as per Binder.getCallingPid(). 2097 * @param callingUid The uid of the incoming call, as per Binder.getCallingUid(). 2098 * @param userId The user id argument supplied by the caller -- this is the user 2099 * they want to run as. 2100 * @param allowAll If true, we will allow USER_ALL. This means you must be prepared 2101 * to get a USER_ALL returned and deal with it correctly. If false, 2102 * an exception will be thrown if USER_ALL is supplied. 2103 * @param requireFull If true, the caller must hold 2104 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} to be able to run as a 2105 * different user than their current process; otherwise they must hold 2106 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS}. 2107 * @param name Optional textual name of the incoming call; only for generating error messages. 2108 * @param callerPackage Optional package name of caller; only for error messages. 2109 * 2110 * @return Returns the user ID that the call should run as. Will always be a concrete 2111 * user number, unless <var>allowAll</var> is true in which case it could also be 2112 * USER_ALL. 2113 */ 2114 public static int handleIncomingUser(int callingPid, int callingUid, int userId, 2115 boolean allowAll, boolean requireFull, String name, String callerPackage) { 2116 if (UserHandle.getUserId(callingUid) == userId) { 2117 return userId; 2118 } 2119 try { 2120 return ActivityManagerNative.getDefault().handleIncomingUser(callingPid, 2121 callingUid, userId, allowAll, requireFull, name, callerPackage); 2122 } catch (RemoteException e) { 2123 throw new SecurityException("Failed calling activity manager", e); 2124 } 2125 } 2126 2127 /** @hide */ 2128 public static int getCurrentUser() { 2129 UserInfo ui; 2130 try { 2131 ui = ActivityManagerNative.getDefault().getCurrentUser(); 2132 return ui != null ? ui.id : 0; 2133 } catch (RemoteException e) { 2134 return 0; 2135 } 2136 } 2137 2138 /** 2139 * Returns the usage statistics of each installed package. 2140 * 2141 * @hide 2142 */ 2143 public PkgUsageStats[] getAllPackageUsageStats() { 2144 try { 2145 IUsageStats usageStatsService = IUsageStats.Stub.asInterface( 2146 ServiceManager.getService("usagestats")); 2147 if (usageStatsService != null) { 2148 return usageStatsService.getAllPkgUsageStats(); 2149 } 2150 } catch (RemoteException e) { 2151 Log.w(TAG, "Could not query usage stats", e); 2152 } 2153 return new PkgUsageStats[0]; 2154 } 2155 2156 /** 2157 * @param userid the user's id. Zero indicates the default user 2158 * @hide 2159 */ 2160 public boolean switchUser(int userid) { 2161 try { 2162 return ActivityManagerNative.getDefault().switchUser(userid); 2163 } catch (RemoteException e) { 2164 return false; 2165 } 2166 } 2167 2168 /** 2169 * Return whether the given user is actively running. This means that 2170 * the user is in the "started" state, not "stopped" -- it is currently 2171 * allowed to run code through scheduled alarms, receiving broadcasts, 2172 * etc. A started user may be either the current foreground user or a 2173 * background user; the result here does not distinguish between the two. 2174 * @param userid the user's id. Zero indicates the default user. 2175 * @hide 2176 */ 2177 public boolean isUserRunning(int userid) { 2178 try { 2179 return ActivityManagerNative.getDefault().isUserRunning(userid, false); 2180 } catch (RemoteException e) { 2181 return false; 2182 } 2183 } 2184 2185 /** 2186 * Perform a system dump of various state associated with the given application 2187 * package name. This call blocks while the dump is being performed, so should 2188 * not be done on a UI thread. The data will be written to the given file 2189 * descriptor as text. An application must hold the 2190 * {@link android.Manifest.permission#DUMP} permission to make this call. 2191 * @param fd The file descriptor that the dump should be written to. 2192 * @param packageName The name of the package that is to be dumped. 2193 */ 2194 public void dumpPackageState(FileDescriptor fd, String packageName) { 2195 dumpPackageStateStatic(fd, packageName); 2196 } 2197 2198 /** 2199 * @hide 2200 */ 2201 public static void dumpPackageStateStatic(FileDescriptor fd, String packageName) { 2202 FileOutputStream fout = new FileOutputStream(fd); 2203 PrintWriter pw = new FastPrintWriter(fout); 2204 dumpService(pw, fd, Context.ACTIVITY_SERVICE, new String[] { "package", packageName }); 2205 pw.println(); 2206 dumpService(pw, fd, "procstats", new String[] { packageName }); 2207 pw.println(); 2208 dumpService(pw, fd, "usagestats", new String[] { "--packages", packageName }); 2209 pw.println(); 2210 dumpService(pw, fd, "package", new String[] { packageName }); 2211 pw.println(); 2212 dumpService(pw, fd, BatteryStats.SERVICE_NAME, new String[] { packageName }); 2213 pw.flush(); 2214 } 2215 2216 private static void dumpService(PrintWriter pw, FileDescriptor fd, String name, String[] args) { 2217 pw.print("DUMP OF SERVICE "); pw.print(name); pw.println(":"); 2218 IBinder service = ServiceManager.checkService(name); 2219 if (service == null) { 2220 pw.println(" (Service not found)"); 2221 return; 2222 } 2223 TransferPipe tp = null; 2224 try { 2225 pw.flush(); 2226 tp = new TransferPipe(); 2227 tp.setBufferPrefix(" "); 2228 service.dump(tp.getWriteFd().getFileDescriptor(), args); 2229 tp.go(fd); 2230 } catch (Throwable e) { 2231 if (tp != null) { 2232 tp.kill(); 2233 } 2234 pw.println("Failure dumping service:"); 2235 e.printStackTrace(pw); 2236 } 2237 } 2238} 2239