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