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