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