ActivityManager.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.app; 18 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.content.pm.IPackageDataObserver; 23import android.graphics.Bitmap; 24import android.os.RemoteException; 25import android.os.Handler; 26import android.os.IBinder; 27import android.os.Parcel; 28import android.os.Parcelable; 29import android.os.Parcelable.Creator; 30import android.text.TextUtils; 31import android.util.Log; 32 33import java.util.List; 34 35/** 36 * Interact with the overall activities running in the system. 37 */ 38public class ActivityManager { 39 private static String TAG = "ActivityManager"; 40 private static boolean DEBUG = false; 41 private static boolean localLOGV = DEBUG || android.util.Config.LOGV; 42 43 private final Context mContext; 44 private final Handler mHandler; 45 46 /*package*/ ActivityManager(Context context, Handler handler) { 47 mContext = context; 48 mHandler = handler; 49 } 50 51 /** 52 * Information you can retrieve about tasks that the user has most recently 53 * started or visited. 54 */ 55 public static class RecentTaskInfo implements Parcelable { 56 /** 57 * If this task is currently running, this is the identifier for it. 58 * If it is not running, this will be -1. 59 */ 60 public int id; 61 62 /** 63 * The original Intent used to launch the task. You can use this 64 * Intent to re-launch the task (if it is no longer running) or bring 65 * the current task to the front. 66 */ 67 public Intent baseIntent; 68 69 /** 70 * If this task was started from an alias, this is the actual 71 * activity component that was initially started; the component of 72 * the baseIntent in this case is the name of the actual activity 73 * implementation that the alias referred to. Otherwise, this is null. 74 */ 75 public ComponentName origActivity; 76 77 public RecentTaskInfo() { 78 } 79 80 public int describeContents() { 81 return 0; 82 } 83 84 public void writeToParcel(Parcel dest, int flags) { 85 dest.writeInt(id); 86 if (baseIntent != null) { 87 dest.writeInt(1); 88 baseIntent.writeToParcel(dest, 0); 89 } else { 90 dest.writeInt(0); 91 } 92 ComponentName.writeToParcel(origActivity, dest); 93 } 94 95 public void readFromParcel(Parcel source) { 96 id = source.readInt(); 97 if (source.readInt() != 0) { 98 baseIntent = Intent.CREATOR.createFromParcel(source); 99 } else { 100 baseIntent = null; 101 } 102 origActivity = ComponentName.readFromParcel(source); 103 } 104 105 public static final Creator<RecentTaskInfo> CREATOR 106 = new Creator<RecentTaskInfo>() { 107 public RecentTaskInfo createFromParcel(Parcel source) { 108 return new RecentTaskInfo(source); 109 } 110 public RecentTaskInfo[] newArray(int size) { 111 return new RecentTaskInfo[size]; 112 } 113 }; 114 115 private RecentTaskInfo(Parcel source) { 116 readFromParcel(source); 117 } 118 } 119 120 /** 121 * Flag for use with {@link #getRecentTasks}: return all tasks, even those 122 * that have set their 123 * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag. 124 */ 125 public static final int RECENT_WITH_EXCLUDED = 0x0001; 126 127 /** 128 * Return a list of the tasks that the user has recently launched, with 129 * the most recent being first and older ones after in order. 130 * 131 * @param maxNum The maximum number of entries to return in the list. The 132 * actual number returned may be smaller, depending on how many tasks the 133 * user has started and the maximum number the system can remember. 134 * 135 * @return Returns a list of RecentTaskInfo records describing each of 136 * the recent tasks. 137 * 138 * @throws SecurityException Throws SecurityException if the caller does 139 * not hold the {@link android.Manifest.permission#GET_TASKS} permission. 140 */ 141 public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags) 142 throws SecurityException { 143 try { 144 return ActivityManagerNative.getDefault().getRecentTasks(maxNum, 145 flags); 146 } catch (RemoteException e) { 147 // System dead, we will be dead too soon! 148 return null; 149 } 150 } 151 152 /** 153 * Information you can retrieve about a particular task that is currently 154 * "running" in the system. Note that a running task does not mean the 155 * given task actual has a process it is actively running in; it simply 156 * means that the user has gone to it and never closed it, but currently 157 * the system may have killed its process and is only holding on to its 158 * last state in order to restart it when the user returns. 159 */ 160 public static class RunningTaskInfo implements Parcelable { 161 /** 162 * A unique identifier for this task. 163 */ 164 public int id; 165 166 /** 167 * The component launched as the first activity in the task. This can 168 * be considered the "application" of this task. 169 */ 170 public ComponentName baseActivity; 171 172 /** 173 * The activity component at the top of the history stack of the task. 174 * This is what the user is currently doing. 175 */ 176 public ComponentName topActivity; 177 178 /** 179 * Thumbnail representation of the task's current state. 180 */ 181 public Bitmap thumbnail; 182 183 /** 184 * Description of the task's current state. 185 */ 186 public CharSequence description; 187 188 /** 189 * Number of activities in this task. 190 */ 191 public int numActivities; 192 193 /** 194 * Number of activities that are currently running (not stopped 195 * and persisted) in this task. 196 */ 197 public int numRunning; 198 199 public RunningTaskInfo() { 200 } 201 202 public int describeContents() { 203 return 0; 204 } 205 206 public void writeToParcel(Parcel dest, int flags) { 207 dest.writeInt(id); 208 ComponentName.writeToParcel(baseActivity, dest); 209 ComponentName.writeToParcel(topActivity, dest); 210 if (thumbnail != null) { 211 dest.writeInt(1); 212 thumbnail.writeToParcel(dest, 0); 213 } else { 214 dest.writeInt(0); 215 } 216 TextUtils.writeToParcel(description, dest, 217 Parcelable.PARCELABLE_WRITE_RETURN_VALUE); 218 dest.writeInt(numActivities); 219 dest.writeInt(numRunning); 220 } 221 222 public void readFromParcel(Parcel source) { 223 id = source.readInt(); 224 baseActivity = ComponentName.readFromParcel(source); 225 topActivity = ComponentName.readFromParcel(source); 226 if (source.readInt() != 0) { 227 thumbnail = Bitmap.CREATOR.createFromParcel(source); 228 } else { 229 thumbnail = null; 230 } 231 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); 232 numActivities = source.readInt(); 233 numRunning = source.readInt(); 234 } 235 236 public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() { 237 public RunningTaskInfo createFromParcel(Parcel source) { 238 return new RunningTaskInfo(source); 239 } 240 public RunningTaskInfo[] newArray(int size) { 241 return new RunningTaskInfo[size]; 242 } 243 }; 244 245 private RunningTaskInfo(Parcel source) { 246 readFromParcel(source); 247 } 248 } 249 250 /** 251 * Return a list of the tasks that are currently running, with 252 * the most recent being first and older ones after in order. Note that 253 * "running" does not mean any of the task's code is currently loaded or 254 * activity -- the task may have been frozen by the system, so that it 255 * can be restarted in its previous state when next brought to the 256 * foreground. 257 * 258 * @param maxNum The maximum number of entries to return in the list. The 259 * actual number returned may be smaller, depending on how many tasks the 260 * user has started. 261 * 262 * @return Returns a list of RunningTaskInfo records describing each of 263 * the running tasks. 264 * 265 * @throws SecurityException Throws SecurityException if the caller does 266 * not hold the {@link android.Manifest.permission#GET_TASKS} permission. 267 */ 268 public List<RunningTaskInfo> getRunningTasks(int maxNum) 269 throws SecurityException { 270 try { 271 return (List<RunningTaskInfo>)ActivityManagerNative.getDefault() 272 .getTasks(maxNum, 0, null); 273 } catch (RemoteException e) { 274 // System dead, we will be dead too soon! 275 return null; 276 } 277 } 278 279 /** 280 * Information you can retrieve about a particular Service that is 281 * currently running in the system. 282 */ 283 public static class RunningServiceInfo implements Parcelable { 284 /** 285 * The service component. 286 */ 287 public ComponentName service; 288 289 /** 290 * If non-zero, this is the process the service is running in. 291 */ 292 public int pid; 293 294 /** 295 * The name of the process this service runs in. 296 */ 297 public String process; 298 299 /** 300 * Set to true if the service has asked to run as a foreground process. 301 */ 302 public boolean foreground; 303 304 /** 305 * The time when the service was first made activity, either by someone 306 * starting or binding to it. 307 */ 308 public long activeSince; 309 310 /** 311 * Set to true if this service has been explicitly started. 312 */ 313 public boolean started; 314 315 /** 316 * Number of clients connected to the service. 317 */ 318 public int clientCount; 319 320 /** 321 * Number of times the service's process has crashed while the service 322 * is running. 323 */ 324 public int crashCount; 325 326 /** 327 * The time when there was last activity in the service (either 328 * explicit requests to start it or clients binding to it). 329 */ 330 public long lastActivityTime; 331 332 /** 333 * If non-zero, this service is not currently running, but scheduled to 334 * restart at the given time. 335 */ 336 public long restarting; 337 338 public RunningServiceInfo() { 339 } 340 341 public int describeContents() { 342 return 0; 343 } 344 345 public void writeToParcel(Parcel dest, int flags) { 346 ComponentName.writeToParcel(service, dest); 347 dest.writeInt(pid); 348 dest.writeString(process); 349 dest.writeInt(foreground ? 1 : 0); 350 dest.writeLong(activeSince); 351 dest.writeInt(started ? 1 : 0); 352 dest.writeInt(clientCount); 353 dest.writeInt(crashCount); 354 dest.writeLong(lastActivityTime); 355 dest.writeLong(restarting); 356 } 357 358 public void readFromParcel(Parcel source) { 359 service = ComponentName.readFromParcel(source); 360 pid = source.readInt(); 361 process = source.readString(); 362 foreground = source.readInt() != 0; 363 activeSince = source.readLong(); 364 started = source.readInt() != 0; 365 clientCount = source.readInt(); 366 crashCount = source.readInt(); 367 lastActivityTime = source.readLong(); 368 restarting = source.readLong(); 369 } 370 371 public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() { 372 public RunningServiceInfo createFromParcel(Parcel source) { 373 return new RunningServiceInfo(source); 374 } 375 public RunningServiceInfo[] newArray(int size) { 376 return new RunningServiceInfo[size]; 377 } 378 }; 379 380 private RunningServiceInfo(Parcel source) { 381 readFromParcel(source); 382 } 383 } 384 385 /** 386 * Return a list of the services that are currently running. 387 * 388 * @param maxNum The maximum number of entries to return in the list. The 389 * actual number returned may be smaller, depending on how many services 390 * are running. 391 * 392 * @return Returns a list of RunningServiceInfo records describing each of 393 * the running tasks. 394 */ 395 public List<RunningServiceInfo> getRunningServices(int maxNum) 396 throws SecurityException { 397 try { 398 return (List<RunningServiceInfo>)ActivityManagerNative.getDefault() 399 .getServices(maxNum, 0); 400 } catch (RemoteException e) { 401 // System dead, we will be dead too soon! 402 return null; 403 } 404 } 405 406 /** 407 * Information you can retrieve about the available memory through 408 * {@link ActivityManager#getMemoryInfo}. 409 */ 410 public static class MemoryInfo implements Parcelable { 411 /** 412 * The total available memory on the system. This number should not 413 * be considered absolute: due to the nature of the kernel, a significant 414 * portion of this memory is actually in use and needed for the overall 415 * system to run well. 416 */ 417 public long availMem; 418 419 /** 420 * The threshold of {@link #availMem} at which we consider memory to be 421 * low and start killing background services and other non-extraneous 422 * processes. 423 */ 424 public long threshold; 425 426 /** 427 * Set to true if the system considers itself to currently be in a low 428 * memory situation. 429 */ 430 public boolean lowMemory; 431 432 public MemoryInfo() { 433 } 434 435 public int describeContents() { 436 return 0; 437 } 438 439 public void writeToParcel(Parcel dest, int flags) { 440 dest.writeLong(availMem); 441 dest.writeLong(threshold); 442 dest.writeInt(lowMemory ? 1 : 0); 443 } 444 445 public void readFromParcel(Parcel source) { 446 availMem = source.readLong(); 447 threshold = source.readLong(); 448 lowMemory = source.readInt() != 0; 449 } 450 451 public static final Creator<MemoryInfo> CREATOR 452 = new Creator<MemoryInfo>() { 453 public MemoryInfo createFromParcel(Parcel source) { 454 return new MemoryInfo(source); 455 } 456 public MemoryInfo[] newArray(int size) { 457 return new MemoryInfo[size]; 458 } 459 }; 460 461 private MemoryInfo(Parcel source) { 462 readFromParcel(source); 463 } 464 } 465 466 public void getMemoryInfo(MemoryInfo outInfo) { 467 try { 468 ActivityManagerNative.getDefault().getMemoryInfo(outInfo); 469 } catch (RemoteException e) { 470 } 471 } 472 473 /** 474 * @hide 475 */ 476 public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) { 477 try { 478 return ActivityManagerNative.getDefault().clearApplicationUserData(packageName, 479 observer); 480 } catch (RemoteException e) { 481 return false; 482 } 483 } 484 485 /** 486 * Information you can retrieve about any processes that are in an error condition. 487 */ 488 public static class ProcessErrorStateInfo implements Parcelable { 489 /** 490 * Condition codes 491 */ 492 public static final int NO_ERROR = 0; 493 public static final int CRASHED = 1; 494 public static final int NOT_RESPONDING = 2; 495 496 /** 497 * The condition that the process is in. 498 */ 499 public int condition; 500 501 /** 502 * The process name in which the crash or error occurred. 503 */ 504 public String processName; 505 506 /** 507 * The pid of this process; 0 if none 508 */ 509 public int pid; 510 511 /** 512 * The kernel user-ID that has been assigned to this process; 513 * currently this is not a unique ID (multiple applications can have 514 * the same uid). 515 */ 516 public int uid; 517 518 /** 519 * The tag that was provided when the process crashed. 520 */ 521 public String tag; 522 523 /** 524 * A short message describing the error condition. 525 */ 526 public String shortMsg; 527 528 /** 529 * A long message describing the error condition. 530 */ 531 public String longMsg; 532 533 /** 534 * Raw data about the crash (typically a stack trace). 535 */ 536 public byte[] crashData; 537 538 public ProcessErrorStateInfo() { 539 } 540 541 public int describeContents() { 542 return 0; 543 } 544 545 public void writeToParcel(Parcel dest, int flags) { 546 dest.writeInt(condition); 547 dest.writeString(processName); 548 dest.writeInt(pid); 549 dest.writeInt(uid); 550 dest.writeString(tag); 551 dest.writeString(shortMsg); 552 dest.writeString(longMsg); 553 dest.writeInt(crashData == null ? -1 : crashData.length); 554 dest.writeByteArray(crashData); 555 } 556 557 public void readFromParcel(Parcel source) { 558 condition = source.readInt(); 559 processName = source.readString(); 560 pid = source.readInt(); 561 uid = source.readInt(); 562 tag = source.readString(); 563 shortMsg = source.readString(); 564 longMsg = source.readString(); 565 int cdLen = source.readInt(); 566 if (cdLen == -1) { 567 crashData = null; 568 } else { 569 crashData = new byte[cdLen]; 570 source.readByteArray(crashData); 571 } 572 } 573 574 public static final Creator<ProcessErrorStateInfo> CREATOR = 575 new Creator<ProcessErrorStateInfo>() { 576 public ProcessErrorStateInfo createFromParcel(Parcel source) { 577 return new ProcessErrorStateInfo(source); 578 } 579 public ProcessErrorStateInfo[] newArray(int size) { 580 return new ProcessErrorStateInfo[size]; 581 } 582 }; 583 584 private ProcessErrorStateInfo(Parcel source) { 585 readFromParcel(source); 586 } 587 } 588 589 /** 590 * Returns a list of any processes that are currently in an error condition. The result 591 * will be null if all processes are running properly at this time. 592 * 593 * @return Returns a list of ProcessErrorStateInfo records, or null if there are no 594 * current error conditions (it will not return an empty list). This list ordering is not 595 * specified. 596 */ 597 public List<ProcessErrorStateInfo> getProcessesInErrorState() { 598 try { 599 return ActivityManagerNative.getDefault().getProcessesInErrorState(); 600 } catch (RemoteException e) { 601 return null; 602 } 603 } 604} 605