ActivityManager.java revision 43a17654cf4bfe7f1ec22bd8b7b32daccdf27c09
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.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.content.pm.ApplicationInfo; 23import android.content.pm.ConfigurationInfo; 24import android.content.pm.IPackageDataObserver; 25import android.content.res.Configuration; 26import android.content.res.Resources; 27import android.graphics.Bitmap; 28import android.os.Debug; 29import android.os.RemoteException; 30import android.os.Handler; 31import android.os.Parcel; 32import android.os.Parcelable; 33import android.os.ServiceManager; 34import android.os.SystemProperties; 35import android.text.TextUtils; 36import android.util.DisplayMetrics; 37import android.util.Log; 38import com.android.internal.app.IUsageStats; 39import com.android.internal.os.PkgUsageStats; 40 41import java.util.HashMap; 42import java.util.List; 43import java.util.Map; 44 45/** 46 * Interact with the overall activities running in the system. 47 */ 48public class ActivityManager { 49 private static String TAG = "ActivityManager"; 50 private static boolean localLOGV = false; 51 52 private final Context mContext; 53 private final Handler mHandler; 54 55 /*package*/ ActivityManager(Context context, Handler handler) { 56 mContext = context; 57 mHandler = handler; 58 } 59 60 /** 61 * Return the approximate per-application memory class of the current 62 * device. This gives you an idea of how hard a memory limit you should 63 * impose on your application to let the overall system work best. The 64 * returned value is in megabytes; the baseline Android memory class is 65 * 16 (which happens to be the Java heap limit of those devices); some 66 * device with more memory may return 24 or even higher numbers. 67 */ 68 public int getMemoryClass() { 69 return staticGetMemoryClass(); 70 } 71 72 /** @hide */ 73 static public int staticGetMemoryClass() { 74 // Really brain dead right now -- just take this from the configured 75 // vm heap size, and assume it is in megabytes and thus ends with "m". 76 String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", ""); 77 if (vmHeapSize != null && !"".equals(vmHeapSize)) { 78 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1)); 79 } 80 return staticGetLargeMemoryClass(); 81 } 82 83 /** 84 * Return the approximate per-application memory class of the current 85 * device when an application is running with a large heap. This is the 86 * space available for memory-intensive applications; most applications 87 * should not need this amount of memory, and should instead stay with the 88 * {@link #getMemoryClass()} limit. The returned value is in megabytes. 89 * This may be the same size as {@link #getMemoryClass()} on memory 90 * constrained devices, or it may be significantly larger on devices with 91 * a large amount of available RAM. 92 * 93 * <p>The is the size of the application's Dalvik heap if it has 94 * specified <code>android:largeHeap="true"</code> in its manifest. 95 */ 96 public int getLargeMemoryClass() { 97 return staticGetLargeMemoryClass(); 98 } 99 100 /** @hide */ 101 static public int staticGetLargeMemoryClass() { 102 // Really brain dead right now -- just take this from the configured 103 // vm heap size, and assume it is in megabytes and thus ends with "m". 104 String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m"); 105 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1)); 106 } 107 108 /** 109 * Information you can retrieve about tasks that the user has most recently 110 * started or visited. 111 */ 112 public static class RecentTaskInfo implements Parcelable { 113 /** 114 * If this task is currently running, this is the identifier for it. 115 * If it is not running, this will be -1. 116 */ 117 public int id; 118 119 /** 120 * The true identifier of this task, valid even if it is not running. 121 */ 122 public int persistentId; 123 124 /** 125 * The original Intent used to launch the task. You can use this 126 * Intent to re-launch the task (if it is no longer running) or bring 127 * the current task to the front. 128 */ 129 public Intent baseIntent; 130 131 /** 132 * If this task was started from an alias, this is the actual 133 * activity component that was initially started; the component of 134 * the baseIntent in this case is the name of the actual activity 135 * implementation that the alias referred to. Otherwise, this is null. 136 */ 137 public ComponentName origActivity; 138 139 /** 140 * Description of the task's last state. 141 */ 142 public CharSequence description; 143 144 public RecentTaskInfo() { 145 } 146 147 public int describeContents() { 148 return 0; 149 } 150 151 public void writeToParcel(Parcel dest, int flags) { 152 dest.writeInt(id); 153 dest.writeInt(persistentId); 154 if (baseIntent != null) { 155 dest.writeInt(1); 156 baseIntent.writeToParcel(dest, 0); 157 } else { 158 dest.writeInt(0); 159 } 160 ComponentName.writeToParcel(origActivity, dest); 161 TextUtils.writeToParcel(description, dest, 162 Parcelable.PARCELABLE_WRITE_RETURN_VALUE); 163 } 164 165 public void readFromParcel(Parcel source) { 166 id = source.readInt(); 167 persistentId = source.readInt(); 168 if (source.readInt() != 0) { 169 baseIntent = Intent.CREATOR.createFromParcel(source); 170 } else { 171 baseIntent = null; 172 } 173 origActivity = ComponentName.readFromParcel(source); 174 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); 175 } 176 177 public static final Creator<RecentTaskInfo> CREATOR 178 = new Creator<RecentTaskInfo>() { 179 public RecentTaskInfo createFromParcel(Parcel source) { 180 return new RecentTaskInfo(source); 181 } 182 public RecentTaskInfo[] newArray(int size) { 183 return new RecentTaskInfo[size]; 184 } 185 }; 186 187 private RecentTaskInfo(Parcel source) { 188 readFromParcel(source); 189 } 190 } 191 192 /** 193 * Flag for use with {@link #getRecentTasks}: return all tasks, even those 194 * that have set their 195 * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag. 196 */ 197 public static final int RECENT_WITH_EXCLUDED = 0x0001; 198 199 /** 200 * Provides a list that does not contain any 201 * recent tasks that currently are not available to the user. 202 */ 203 public static final int RECENT_IGNORE_UNAVAILABLE = 0x0002; 204 205 /** 206 * Flag for use with {@link #getRecentTasks}: also return the thumbnail 207 * bitmap (if available) for each recent task. 208 * @hide 209 */ 210 public static final int TASKS_GET_THUMBNAILS = 0x0001000; 211 212 /** 213 * Return a list of the tasks that the user has recently launched, with 214 * the most recent being first and older ones after in order. 215 * 216 * @param maxNum The maximum number of entries to return in the list. The 217 * actual number returned may be smaller, depending on how many tasks the 218 * user has started and the maximum number the system can remember. 219 * @param flags Information about what to return. May be any combination 220 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}. 221 * 222 * @return Returns a list of RecentTaskInfo records describing each of 223 * the recent tasks. 224 * 225 * @throws SecurityException Throws SecurityException if the caller does 226 * not hold the {@link android.Manifest.permission#GET_TASKS} permission. 227 */ 228 public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags) 229 throws SecurityException { 230 try { 231 return ActivityManagerNative.getDefault().getRecentTasks(maxNum, 232 flags); 233 } catch (RemoteException e) { 234 // System dead, we will be dead too soon! 235 return null; 236 } 237 } 238 239 /** 240 * Information you can retrieve about a particular task that is currently 241 * "running" in the system. Note that a running task does not mean the 242 * given task actual has a process it is actively running in; it simply 243 * means that the user has gone to it and never closed it, but currently 244 * the system may have killed its process and is only holding on to its 245 * last state in order to restart it when the user returns. 246 */ 247 public static class RunningTaskInfo implements Parcelable { 248 /** 249 * A unique identifier for this task. 250 */ 251 public int id; 252 253 /** 254 * The component launched as the first activity in the task. This can 255 * be considered the "application" of this task. 256 */ 257 public ComponentName baseActivity; 258 259 /** 260 * The activity component at the top of the history stack of the task. 261 * This is what the user is currently doing. 262 */ 263 public ComponentName topActivity; 264 265 /** 266 * Thumbnail representation of the task's current state. Currently 267 * always null. 268 */ 269 public Bitmap thumbnail; 270 271 /** 272 * Description of the task's current state. 273 */ 274 public CharSequence description; 275 276 /** 277 * Number of activities in this task. 278 */ 279 public int numActivities; 280 281 /** 282 * Number of activities that are currently running (not stopped 283 * and persisted) in this task. 284 */ 285 public int numRunning; 286 287 public RunningTaskInfo() { 288 } 289 290 public int describeContents() { 291 return 0; 292 } 293 294 public void writeToParcel(Parcel dest, int flags) { 295 dest.writeInt(id); 296 ComponentName.writeToParcel(baseActivity, dest); 297 ComponentName.writeToParcel(topActivity, dest); 298 if (thumbnail != null) { 299 dest.writeInt(1); 300 thumbnail.writeToParcel(dest, 0); 301 } else { 302 dest.writeInt(0); 303 } 304 TextUtils.writeToParcel(description, dest, 305 Parcelable.PARCELABLE_WRITE_RETURN_VALUE); 306 dest.writeInt(numActivities); 307 dest.writeInt(numRunning); 308 } 309 310 public void readFromParcel(Parcel source) { 311 id = source.readInt(); 312 baseActivity = ComponentName.readFromParcel(source); 313 topActivity = ComponentName.readFromParcel(source); 314 if (source.readInt() != 0) { 315 thumbnail = Bitmap.CREATOR.createFromParcel(source); 316 } else { 317 thumbnail = null; 318 } 319 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); 320 numActivities = source.readInt(); 321 numRunning = source.readInt(); 322 } 323 324 public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() { 325 public RunningTaskInfo createFromParcel(Parcel source) { 326 return new RunningTaskInfo(source); 327 } 328 public RunningTaskInfo[] newArray(int size) { 329 return new RunningTaskInfo[size]; 330 } 331 }; 332 333 private RunningTaskInfo(Parcel source) { 334 readFromParcel(source); 335 } 336 } 337 338 /** 339 * Return a list of the tasks that are currently running, with 340 * the most recent being first and older ones after in order. Note that 341 * "running" does not mean any of the task's code is currently loaded or 342 * activity -- the task may have been frozen by the system, so that it 343 * can be restarted in its previous state when next brought to the 344 * foreground. 345 * 346 * @param maxNum The maximum number of entries to return in the list. The 347 * actual number returned may be smaller, depending on how many tasks the 348 * user has started. 349 * 350 * @param flags Optional flags 351 * @param receiver Optional receiver for delayed thumbnails 352 * 353 * @return Returns a list of RunningTaskInfo records describing each of 354 * the running tasks. 355 * 356 * Some thumbnails may not be available at the time of this call. The optional 357 * receiver may be used to receive those thumbnails. 358 * 359 * @throws SecurityException Throws SecurityException if the caller does 360 * not hold the {@link android.Manifest.permission#GET_TASKS} permission. 361 * 362 * @hide 363 */ 364 public List<RunningTaskInfo> getRunningTasks(int maxNum, int flags, IThumbnailReceiver receiver) 365 throws SecurityException { 366 try { 367 return ActivityManagerNative.getDefault().getTasks(maxNum, flags, receiver); 368 } catch (RemoteException e) { 369 // System dead, we will be dead too soon! 370 return null; 371 } 372 } 373 374 /** 375 * Return a list of the tasks that are currently running, with 376 * the most recent being first and older ones after in order. Note that 377 * "running" does not mean any of the task's code is currently loaded or 378 * activity -- the task may have been frozen by the system, so that it 379 * can be restarted in its previous state when next brought to the 380 * foreground. 381 * 382 * @param maxNum The maximum number of entries to return in the list. The 383 * actual number returned may be smaller, depending on how many tasks the 384 * user has started. 385 * 386 * @return Returns a list of RunningTaskInfo records describing each of 387 * the running tasks. 388 * 389 * @throws SecurityException Throws SecurityException if the caller does 390 * not hold the {@link android.Manifest.permission#GET_TASKS} permission. 391 */ 392 public List<RunningTaskInfo> getRunningTasks(int maxNum) 393 throws SecurityException { 394 return getRunningTasks(maxNum, 0, null); 395 } 396 397 /** @hide */ 398 public Bitmap getTaskThumbnail(int id) throws SecurityException { 399 try { 400 return ActivityManagerNative.getDefault().getTaskThumbnail(id); 401 } catch (RemoteException e) { 402 // System dead, we will be dead too soon! 403 return null; 404 } 405 } 406 407 /** 408 * Flag for {@link #moveTaskToFront(int, int)}: also move the "home" 409 * activity along with the task, so it is positioned immediately behind 410 * the task. 411 */ 412 public static final int MOVE_TASK_WITH_HOME = 0x00000001; 413 414 /** 415 * Flag for {@link #moveTaskToFront(int, int)}: don't count this as a 416 * user-instigated action, so the current activity will not receive a 417 * hint that the user is leaving. 418 */ 419 public static final int MOVE_TASK_NO_USER_ACTION = 0x00000002; 420 421 /** 422 * Ask that the task associated with a given task ID be moved to the 423 * front of the stack, so it is now visible to the user. Requires that 424 * the caller hold permission {@link android.Manifest.permission#REORDER_TASKS} 425 * or a SecurityException will be thrown. 426 * 427 * @param taskId The identifier of the task to be moved, as found in 428 * {@link RunningTaskInfo} or {@link RecentTaskInfo}. 429 * @param flags Additional operational flags, 0 or more of 430 * {@link #MOVE_TASK_WITH_HOME}. 431 */ 432 public void moveTaskToFront(int taskId, int flags) { 433 try { 434 ActivityManagerNative.getDefault().moveTaskToFront(taskId, flags); 435 } catch (RemoteException e) { 436 // System dead, we will be dead too soon! 437 } 438 } 439 440 /** 441 * Information you can retrieve about a particular Service that is 442 * currently running in the system. 443 */ 444 public static class RunningServiceInfo implements Parcelable { 445 /** 446 * The service component. 447 */ 448 public ComponentName service; 449 450 /** 451 * If non-zero, this is the process the service is running in. 452 */ 453 public int pid; 454 455 /** 456 * The UID that owns this service. 457 */ 458 public int uid; 459 460 /** 461 * The name of the process this service runs in. 462 */ 463 public String process; 464 465 /** 466 * Set to true if the service has asked to run as a foreground process. 467 */ 468 public boolean foreground; 469 470 /** 471 * The time when the service was first made active, either by someone 472 * starting or binding to it. This 473 * is in units of {@link android.os.SystemClock#elapsedRealtime()}. 474 */ 475 public long activeSince; 476 477 /** 478 * Set to true if this service has been explicitly started. 479 */ 480 public boolean started; 481 482 /** 483 * Number of clients connected to the service. 484 */ 485 public int clientCount; 486 487 /** 488 * Number of times the service's process has crashed while the service 489 * is running. 490 */ 491 public int crashCount; 492 493 /** 494 * The time when there was last activity in the service (either 495 * explicit requests to start it or clients binding to it). This 496 * is in units of {@link android.os.SystemClock#uptimeMillis()}. 497 */ 498 public long lastActivityTime; 499 500 /** 501 * If non-zero, this service is not currently running, but scheduled to 502 * restart at the given time. 503 */ 504 public long restarting; 505 506 /** 507 * Bit for {@link #flags}: set if this service has been 508 * explicitly started. 509 */ 510 public static final int FLAG_STARTED = 1<<0; 511 512 /** 513 * Bit for {@link #flags}: set if the service has asked to 514 * run as a foreground process. 515 */ 516 public static final int FLAG_FOREGROUND = 1<<1; 517 518 /** 519 * Bit for {@link #flags): set if the service is running in a 520 * core system process. 521 */ 522 public static final int FLAG_SYSTEM_PROCESS = 1<<2; 523 524 /** 525 * Bit for {@link #flags): set if the service is running in a 526 * persistent process. 527 */ 528 public static final int FLAG_PERSISTENT_PROCESS = 1<<3; 529 530 /** 531 * Running flags. 532 */ 533 public int flags; 534 535 /** 536 * For special services that are bound to by system code, this is 537 * the package that holds the binding. 538 */ 539 public String clientPackage; 540 541 /** 542 * For special services that are bound to by system code, this is 543 * a string resource providing a user-visible label for who the 544 * client is. 545 */ 546 public int clientLabel; 547 548 public RunningServiceInfo() { 549 } 550 551 public int describeContents() { 552 return 0; 553 } 554 555 public void writeToParcel(Parcel dest, int flags) { 556 ComponentName.writeToParcel(service, dest); 557 dest.writeInt(pid); 558 dest.writeInt(uid); 559 dest.writeString(process); 560 dest.writeInt(foreground ? 1 : 0); 561 dest.writeLong(activeSince); 562 dest.writeInt(started ? 1 : 0); 563 dest.writeInt(clientCount); 564 dest.writeInt(crashCount); 565 dest.writeLong(lastActivityTime); 566 dest.writeLong(restarting); 567 dest.writeInt(this.flags); 568 dest.writeString(clientPackage); 569 dest.writeInt(clientLabel); 570 } 571 572 public void readFromParcel(Parcel source) { 573 service = ComponentName.readFromParcel(source); 574 pid = source.readInt(); 575 uid = source.readInt(); 576 process = source.readString(); 577 foreground = source.readInt() != 0; 578 activeSince = source.readLong(); 579 started = source.readInt() != 0; 580 clientCount = source.readInt(); 581 crashCount = source.readInt(); 582 lastActivityTime = source.readLong(); 583 restarting = source.readLong(); 584 flags = source.readInt(); 585 clientPackage = source.readString(); 586 clientLabel = source.readInt(); 587 } 588 589 public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() { 590 public RunningServiceInfo createFromParcel(Parcel source) { 591 return new RunningServiceInfo(source); 592 } 593 public RunningServiceInfo[] newArray(int size) { 594 return new RunningServiceInfo[size]; 595 } 596 }; 597 598 private RunningServiceInfo(Parcel source) { 599 readFromParcel(source); 600 } 601 } 602 603 /** 604 * Return a list of the services that are currently running. 605 * 606 * @param maxNum The maximum number of entries to return in the list. The 607 * actual number returned may be smaller, depending on how many services 608 * are running. 609 * 610 * @return Returns a list of RunningServiceInfo records describing each of 611 * the running tasks. 612 */ 613 public List<RunningServiceInfo> getRunningServices(int maxNum) 614 throws SecurityException { 615 try { 616 return (List<RunningServiceInfo>)ActivityManagerNative.getDefault() 617 .getServices(maxNum, 0); 618 } catch (RemoteException e) { 619 // System dead, we will be dead too soon! 620 return null; 621 } 622 } 623 624 /** 625 * Returns a PendingIntent you can start to show a control panel for the 626 * given running service. If the service does not have a control panel, 627 * null is returned. 628 */ 629 public PendingIntent getRunningServiceControlPanel(ComponentName service) 630 throws SecurityException { 631 try { 632 return ActivityManagerNative.getDefault() 633 .getRunningServiceControlPanel(service); 634 } catch (RemoteException e) { 635 // System dead, we will be dead too soon! 636 return null; 637 } 638 } 639 640 /** 641 * Information you can retrieve about the available memory through 642 * {@link ActivityManager#getMemoryInfo}. 643 */ 644 public static class MemoryInfo implements Parcelable { 645 /** 646 * The total available memory on the system. This number should not 647 * be considered absolute: due to the nature of the kernel, a significant 648 * portion of this memory is actually in use and needed for the overall 649 * system to run well. 650 */ 651 public long availMem; 652 653 /** 654 * The threshold of {@link #availMem} at which we consider memory to be 655 * low and start killing background services and other non-extraneous 656 * processes. 657 */ 658 public long threshold; 659 660 /** 661 * Set to true if the system considers itself to currently be in a low 662 * memory situation. 663 */ 664 public boolean lowMemory; 665 666 public MemoryInfo() { 667 } 668 669 public int describeContents() { 670 return 0; 671 } 672 673 public void writeToParcel(Parcel dest, int flags) { 674 dest.writeLong(availMem); 675 dest.writeLong(threshold); 676 dest.writeInt(lowMemory ? 1 : 0); 677 } 678 679 public void readFromParcel(Parcel source) { 680 availMem = source.readLong(); 681 threshold = source.readLong(); 682 lowMemory = source.readInt() != 0; 683 } 684 685 public static final Creator<MemoryInfo> CREATOR 686 = new Creator<MemoryInfo>() { 687 public MemoryInfo createFromParcel(Parcel source) { 688 return new MemoryInfo(source); 689 } 690 public MemoryInfo[] newArray(int size) { 691 return new MemoryInfo[size]; 692 } 693 }; 694 695 private MemoryInfo(Parcel source) { 696 readFromParcel(source); 697 } 698 } 699 700 public void getMemoryInfo(MemoryInfo outInfo) { 701 try { 702 ActivityManagerNative.getDefault().getMemoryInfo(outInfo); 703 } catch (RemoteException e) { 704 } 705 } 706 707 /** 708 * @hide 709 */ 710 public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) { 711 try { 712 return ActivityManagerNative.getDefault().clearApplicationUserData(packageName, 713 observer); 714 } catch (RemoteException e) { 715 return false; 716 } 717 } 718 719 /** 720 * Information you can retrieve about any processes that are in an error condition. 721 */ 722 public static class ProcessErrorStateInfo implements Parcelable { 723 /** 724 * Condition codes 725 */ 726 public static final int NO_ERROR = 0; 727 public static final int CRASHED = 1; 728 public static final int NOT_RESPONDING = 2; 729 730 /** 731 * The condition that the process is in. 732 */ 733 public int condition; 734 735 /** 736 * The process name in which the crash or error occurred. 737 */ 738 public String processName; 739 740 /** 741 * The pid of this process; 0 if none 742 */ 743 public int pid; 744 745 /** 746 * The kernel user-ID that has been assigned to this process; 747 * currently this is not a unique ID (multiple applications can have 748 * the same uid). 749 */ 750 public int uid; 751 752 /** 753 * The activity name associated with the error, if known. May be null. 754 */ 755 public String tag; 756 757 /** 758 * A short message describing the error condition. 759 */ 760 public String shortMsg; 761 762 /** 763 * A long message describing the error condition. 764 */ 765 public String longMsg; 766 767 /** 768 * The stack trace where the error originated. May be null. 769 */ 770 public String stackTrace; 771 772 /** 773 * to be deprecated: This value will always be null. 774 */ 775 public byte[] crashData = null; 776 777 public ProcessErrorStateInfo() { 778 } 779 780 public int describeContents() { 781 return 0; 782 } 783 784 public void writeToParcel(Parcel dest, int flags) { 785 dest.writeInt(condition); 786 dest.writeString(processName); 787 dest.writeInt(pid); 788 dest.writeInt(uid); 789 dest.writeString(tag); 790 dest.writeString(shortMsg); 791 dest.writeString(longMsg); 792 dest.writeString(stackTrace); 793 } 794 795 public void readFromParcel(Parcel source) { 796 condition = source.readInt(); 797 processName = source.readString(); 798 pid = source.readInt(); 799 uid = source.readInt(); 800 tag = source.readString(); 801 shortMsg = source.readString(); 802 longMsg = source.readString(); 803 stackTrace = source.readString(); 804 } 805 806 public static final Creator<ProcessErrorStateInfo> CREATOR = 807 new Creator<ProcessErrorStateInfo>() { 808 public ProcessErrorStateInfo createFromParcel(Parcel source) { 809 return new ProcessErrorStateInfo(source); 810 } 811 public ProcessErrorStateInfo[] newArray(int size) { 812 return new ProcessErrorStateInfo[size]; 813 } 814 }; 815 816 private ProcessErrorStateInfo(Parcel source) { 817 readFromParcel(source); 818 } 819 } 820 821 /** 822 * Returns a list of any processes that are currently in an error condition. The result 823 * will be null if all processes are running properly at this time. 824 * 825 * @return Returns a list of ProcessErrorStateInfo records, or null if there are no 826 * current error conditions (it will not return an empty list). This list ordering is not 827 * specified. 828 */ 829 public List<ProcessErrorStateInfo> getProcessesInErrorState() { 830 try { 831 return ActivityManagerNative.getDefault().getProcessesInErrorState(); 832 } catch (RemoteException e) { 833 return null; 834 } 835 } 836 837 /** 838 * Information you can retrieve about a running process. 839 */ 840 public static class RunningAppProcessInfo implements Parcelable { 841 /** 842 * The name of the process that this object is associated with 843 */ 844 public String processName; 845 846 /** 847 * The pid of this process; 0 if none 848 */ 849 public int pid; 850 851 /** 852 * The user id of this process. 853 */ 854 public int uid; 855 856 /** 857 * All packages that have been loaded into the process. 858 */ 859 public String pkgList[]; 860 861 /** 862 * Constant for {@link #flags}: this is an app that is unable to 863 * correctly save its state when going to the background, 864 * so it can not be killed while in the background. 865 * @hide 866 */ 867 public static final int FLAG_CANT_SAVE_STATE = 1<<0; 868 869 /** 870 * Constant for {@link #flags}: this process is associated with a 871 * persistent system app. 872 * @hide 873 */ 874 public static final int FLAG_PERSISTENT = 1<<1; 875 876 /** 877 * Flags of information. May be any of 878 * {@link #FLAG_CANT_SAVE_STATE}. 879 * @hide 880 */ 881 public int flags; 882 883 /** 884 * Constant for {@link #importance}: this process is running the 885 * foreground UI. 886 */ 887 public static final int IMPORTANCE_FOREGROUND = 100; 888 889 /** 890 * Constant for {@link #importance}: this process is running something 891 * that is actively visible to the user, though not in the immediate 892 * foreground. 893 */ 894 public static final int IMPORTANCE_VISIBLE = 200; 895 896 /** 897 * Constant for {@link #importance}: this process is running something 898 * that is considered to be actively perceptible to the user. An 899 * example would be an application performing background music playback. 900 */ 901 public static final int IMPORTANCE_PERCEPTIBLE = 130; 902 903 /** 904 * Constant for {@link #importance}: this process is running an 905 * application that can not save its state, and thus can't be killed 906 * while in the background. 907 * @hide 908 */ 909 public static final int IMPORTANCE_CANT_SAVE_STATE = 170; 910 911 /** 912 * Constant for {@link #importance}: this process is contains services 913 * that should remain running. 914 */ 915 public static final int IMPORTANCE_SERVICE = 300; 916 917 /** 918 * Constant for {@link #importance}: this process process contains 919 * background code that is expendable. 920 */ 921 public static final int IMPORTANCE_BACKGROUND = 400; 922 923 /** 924 * Constant for {@link #importance}: this process is empty of any 925 * actively running code. 926 */ 927 public static final int IMPORTANCE_EMPTY = 500; 928 929 /** 930 * The relative importance level that the system places on this 931 * process. May be one of {@link #IMPORTANCE_FOREGROUND}, 932 * {@link #IMPORTANCE_VISIBLE}, {@link #IMPORTANCE_SERVICE}, 933 * {@link #IMPORTANCE_BACKGROUND}, or {@link #IMPORTANCE_EMPTY}. These 934 * constants are numbered so that "more important" values are always 935 * smaller than "less important" values. 936 */ 937 public int importance; 938 939 /** 940 * An additional ordering within a particular {@link #importance} 941 * category, providing finer-grained information about the relative 942 * utility of processes within a category. This number means nothing 943 * except that a smaller values are more recently used (and thus 944 * more important). Currently an LRU value is only maintained for 945 * the {@link #IMPORTANCE_BACKGROUND} category, though others may 946 * be maintained in the future. 947 */ 948 public int lru; 949 950 /** 951 * Constant for {@link #importanceReasonCode}: nothing special has 952 * been specified for the reason for this level. 953 */ 954 public static final int REASON_UNKNOWN = 0; 955 956 /** 957 * Constant for {@link #importanceReasonCode}: one of the application's 958 * content providers is being used by another process. The pid of 959 * the client process is in {@link #importanceReasonPid} and the 960 * target provider in this process is in 961 * {@link #importanceReasonComponent}. 962 */ 963 public static final int REASON_PROVIDER_IN_USE = 1; 964 965 /** 966 * Constant for {@link #importanceReasonCode}: one of the application's 967 * content providers is being used by another process. The pid of 968 * the client process is in {@link #importanceReasonPid} and the 969 * target provider in this process is in 970 * {@link #importanceReasonComponent}. 971 */ 972 public static final int REASON_SERVICE_IN_USE = 2; 973 974 /** 975 * The reason for {@link #importance}, if any. 976 */ 977 public int importanceReasonCode; 978 979 /** 980 * For the specified values of {@link #importanceReasonCode}, this 981 * is the process ID of the other process that is a client of this 982 * process. This will be 0 if no other process is using this one. 983 */ 984 public int importanceReasonPid; 985 986 /** 987 * For the specified values of {@link #importanceReasonCode}, this 988 * is the name of the component that is being used in this process. 989 */ 990 public ComponentName importanceReasonComponent; 991 992 public RunningAppProcessInfo() { 993 importance = IMPORTANCE_FOREGROUND; 994 importanceReasonCode = REASON_UNKNOWN; 995 } 996 997 public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) { 998 processName = pProcessName; 999 pid = pPid; 1000 pkgList = pArr; 1001 } 1002 1003 public int describeContents() { 1004 return 0; 1005 } 1006 1007 public void writeToParcel(Parcel dest, int flags) { 1008 dest.writeString(processName); 1009 dest.writeInt(pid); 1010 dest.writeInt(uid); 1011 dest.writeStringArray(pkgList); 1012 dest.writeInt(this.flags); 1013 dest.writeInt(importance); 1014 dest.writeInt(lru); 1015 dest.writeInt(importanceReasonCode); 1016 dest.writeInt(importanceReasonPid); 1017 ComponentName.writeToParcel(importanceReasonComponent, dest); 1018 } 1019 1020 public void readFromParcel(Parcel source) { 1021 processName = source.readString(); 1022 pid = source.readInt(); 1023 uid = source.readInt(); 1024 pkgList = source.readStringArray(); 1025 flags = source.readInt(); 1026 importance = source.readInt(); 1027 lru = source.readInt(); 1028 importanceReasonCode = source.readInt(); 1029 importanceReasonPid = source.readInt(); 1030 importanceReasonComponent = ComponentName.readFromParcel(source); 1031 } 1032 1033 public static final Creator<RunningAppProcessInfo> CREATOR = 1034 new Creator<RunningAppProcessInfo>() { 1035 public RunningAppProcessInfo createFromParcel(Parcel source) { 1036 return new RunningAppProcessInfo(source); 1037 } 1038 public RunningAppProcessInfo[] newArray(int size) { 1039 return new RunningAppProcessInfo[size]; 1040 } 1041 }; 1042 1043 private RunningAppProcessInfo(Parcel source) { 1044 readFromParcel(source); 1045 } 1046 } 1047 1048 /** 1049 * Returns a list of application processes installed on external media 1050 * that are running on the device. 1051 * 1052 * @return Returns a list of ApplicationInfo records, or null if none 1053 * This list ordering is not specified. 1054 * @hide 1055 */ 1056 public List<ApplicationInfo> getRunningExternalApplications() { 1057 try { 1058 return ActivityManagerNative.getDefault().getRunningExternalApplications(); 1059 } catch (RemoteException e) { 1060 return null; 1061 } 1062 } 1063 1064 /** 1065 * Returns a list of application processes that are running on the device. 1066 * 1067 * @return Returns a list of RunningAppProcessInfo records, or null if there are no 1068 * running processes (it will not return an empty list). This list ordering is not 1069 * specified. 1070 */ 1071 public List<RunningAppProcessInfo> getRunningAppProcesses() { 1072 try { 1073 return ActivityManagerNative.getDefault().getRunningAppProcesses(); 1074 } catch (RemoteException e) { 1075 return null; 1076 } 1077 } 1078 1079 /** 1080 * Return information about the memory usage of one or more processes. 1081 * 1082 * @param pids The pids of the processes whose memory usage is to be 1083 * retrieved. 1084 * @return Returns an array of memory information, one for each 1085 * requested pid. 1086 */ 1087 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) { 1088 try { 1089 return ActivityManagerNative.getDefault().getProcessMemoryInfo(pids); 1090 } catch (RemoteException e) { 1091 return null; 1092 } 1093 } 1094 1095 /** 1096 * @deprecated This is now just a wrapper for 1097 * {@link #killBackgroundProcesses(String)}; the previous behavior here 1098 * is no longer available to applications because it allows them to 1099 * break other applications by removing their alarms, stopping their 1100 * services, etc. 1101 */ 1102 @Deprecated 1103 public void restartPackage(String packageName) { 1104 killBackgroundProcesses(packageName); 1105 } 1106 1107 /** 1108 * Have the system immediately kill all background processes associated 1109 * with the given package. This is the same as the kernel killing those 1110 * processes to reclaim memory; the system will take care of restarting 1111 * these processes in the future as needed. 1112 * 1113 * <p>You must hold the permission 1114 * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to 1115 * call this method. 1116 * 1117 * @param packageName The name of the package whose processes are to 1118 * be killed. 1119 */ 1120 public void killBackgroundProcesses(String packageName) { 1121 try { 1122 ActivityManagerNative.getDefault().killBackgroundProcesses(packageName); 1123 } catch (RemoteException e) { 1124 } 1125 } 1126 1127 /** 1128 * Have the system perform a force stop of everything associated with 1129 * the given application package. All processes that share its uid 1130 * will be killed, all services it has running stopped, all activities 1131 * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED} 1132 * broadcast will be sent, so that any of its registered alarms can 1133 * be stopped, notifications removed, etc. 1134 * 1135 * <p>You must hold the permission 1136 * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to 1137 * call this method. 1138 * 1139 * @param packageName The name of the package to be stopped. 1140 * 1141 * @hide This is not available to third party applications due to 1142 * it allowing them to break other applications by stopping their 1143 * services, removing their alarms, etc. 1144 */ 1145 public void forceStopPackage(String packageName) { 1146 try { 1147 ActivityManagerNative.getDefault().forceStopPackage(packageName); 1148 } catch (RemoteException e) { 1149 } 1150 } 1151 1152 /** 1153 * Get the device configuration attributes. 1154 */ 1155 public ConfigurationInfo getDeviceConfigurationInfo() { 1156 try { 1157 return ActivityManagerNative.getDefault().getDeviceConfigurationInfo(); 1158 } catch (RemoteException e) { 1159 } 1160 return null; 1161 } 1162 1163 /** 1164 * Get the preferred density of icons for the launcher. This is used when 1165 * custom drawables are created (e.g., for shortcuts). 1166 * 1167 * @return density in terms of DPI 1168 */ 1169 public int getLauncherLargeIconDensity() { 1170 final Resources res = mContext.getResources(); 1171 final int density = res.getDisplayMetrics().densityDpi; 1172 1173 if ((res.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) 1174 != Configuration.SCREENLAYOUT_SIZE_XLARGE) { 1175 return density; 1176 } 1177 1178 switch (density) { 1179 case DisplayMetrics.DENSITY_LOW: 1180 return DisplayMetrics.DENSITY_MEDIUM; 1181 case DisplayMetrics.DENSITY_MEDIUM: 1182 return DisplayMetrics.DENSITY_HIGH; 1183 case DisplayMetrics.DENSITY_HIGH: 1184 return DisplayMetrics.DENSITY_XHIGH; 1185 case DisplayMetrics.DENSITY_XHIGH: 1186 return DisplayMetrics.DENSITY_MEDIUM * 2; 1187 default: 1188 return density; 1189 } 1190 } 1191 1192 /** 1193 * Get the preferred launcher icon size. This is used when custom drawables 1194 * are created (e.g., for shortcuts). 1195 * 1196 * @return dimensions of square icons in terms of pixels 1197 */ 1198 public int getLauncherLargeIconSize() { 1199 final Resources res = mContext.getResources(); 1200 final int size = res.getDimensionPixelSize(android.R.dimen.app_icon_size); 1201 1202 if ((res.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) 1203 != Configuration.SCREENLAYOUT_SIZE_XLARGE) { 1204 return size; 1205 } 1206 1207 final int density = res.getDisplayMetrics().densityDpi; 1208 1209 switch (density) { 1210 case DisplayMetrics.DENSITY_LOW: 1211 return (size * DisplayMetrics.DENSITY_MEDIUM) / DisplayMetrics.DENSITY_LOW; 1212 case DisplayMetrics.DENSITY_MEDIUM: 1213 return (size * DisplayMetrics.DENSITY_HIGH) / DisplayMetrics.DENSITY_MEDIUM; 1214 case DisplayMetrics.DENSITY_HIGH: 1215 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH; 1216 case DisplayMetrics.DENSITY_XHIGH: 1217 return (size * DisplayMetrics.DENSITY_MEDIUM * 2) / DisplayMetrics.DENSITY_XHIGH; 1218 default: 1219 return size; 1220 } 1221 } 1222 1223 /** 1224 * Returns "true" if the user interface is currently being messed with 1225 * by a monkey. 1226 */ 1227 public static boolean isUserAMonkey() { 1228 try { 1229 return ActivityManagerNative.getDefault().isUserAMonkey(); 1230 } catch (RemoteException e) { 1231 } 1232 return false; 1233 } 1234 1235 /** 1236 * Returns "true" if device is running in a test harness. 1237 */ 1238 public static boolean isRunningInTestHarness() { 1239 return SystemProperties.getBoolean("ro.test_harness", false); 1240 } 1241 1242 /** 1243 * Returns the launch count of each installed package. 1244 * 1245 * @hide 1246 */ 1247 public Map<String, Integer> getAllPackageLaunchCounts() { 1248 try { 1249 IUsageStats usageStatsService = IUsageStats.Stub.asInterface( 1250 ServiceManager.getService("usagestats")); 1251 if (usageStatsService == null) { 1252 return new HashMap<String, Integer>(); 1253 } 1254 1255 PkgUsageStats[] allPkgUsageStats = usageStatsService.getAllPkgUsageStats(); 1256 if (allPkgUsageStats == null) { 1257 return new HashMap<String, Integer>(); 1258 } 1259 1260 Map<String, Integer> launchCounts = new HashMap<String, Integer>(); 1261 for (PkgUsageStats pkgUsageStats : allPkgUsageStats) { 1262 launchCounts.put(pkgUsageStats.packageName, pkgUsageStats.launchCount); 1263 } 1264 1265 return launchCounts; 1266 } catch (RemoteException e) { 1267 Log.w(TAG, "Could not query launch counts", e); 1268 return new HashMap<String, Integer>(); 1269 } 1270 } 1271} 1272