blob: 4e6c3dcb07028c0362116b8253e6af3c6109a8cc [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Dianne Hackbornb4e12492013-06-26 15:08:19 -070019import android.R;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070020import android.os.BatteryStats;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070021import android.os.IBinder;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070022import com.android.internal.app.IUsageStats;
23import com.android.internal.os.PkgUsageStats;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070024import com.android.internal.os.TransferPipe;
Dianne Hackborn8c841092013-06-24 13:46:13 -070025import com.android.internal.util.FastPrintWriter;
Dianne Hackbornb3756322011-08-12 13:58:13 -070026import com.android.internal.util.MemInfoReader;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.ComponentName;
29import android.content.Context;
30import android.content.Intent;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -070031import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.pm.ConfigurationInfo;
33import android.content.pm.IPackageDataObserver;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070034import android.content.pm.PackageManager;
Dianne Hackborn41203752012-08-31 14:05:51 -070035import android.content.pm.UserInfo;
Kenny Root5ef44b72011-01-26 17:22:20 -080036import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.Bitmap;
Dianne Hackbornb3756322011-08-12 13:58:13 -070038import android.graphics.Point;
Craig Mautner967212c2013-04-13 21:10:58 -070039import android.graphics.Rect;
Jeff Brownbd6e1502012-08-28 03:27:37 -070040import android.hardware.display.DisplayManagerGlobal;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -070041import android.os.Bundle;
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -070042import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.Parcel;
45import android.os.Parcelable;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070046import android.os.Process;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070047import android.os.RemoteException;
Peter Visontay8d224ca2011-02-18 16:39:19 +000048import android.os.ServiceManager;
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -070049import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070050import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.text.TextUtils;
Kenny Root5ef44b72011-01-26 17:22:20 -080052import android.util.DisplayMetrics;
Peter Visontay8d224ca2011-02-18 16:39:19 +000053import android.util.Log;
Dianne Hackbornb3756322011-08-12 13:58:13 -070054import android.util.Slog;
55import android.view.Display;
Kenny Root5ef44b72011-01-26 17:22:20 -080056
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070057import java.io.FileDescriptor;
58import java.io.FileOutputStream;
59import java.io.PrintWriter;
Peter Visontay8d224ca2011-02-18 16:39:19 +000060import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import java.util.List;
Peter Visontay8d224ca2011-02-18 16:39:19 +000062import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
64/**
65 * Interact with the overall activities running in the system.
66 */
67public class ActivityManager {
68 private static String TAG = "ActivityManager";
Joe Onorato43a17652011-04-06 19:22:23 -070069 private static boolean localLOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71 private final Context mContext;
72 private final Handler mHandler;
73
Dianne Hackborna4972e92012-03-14 10:38:05 -070074 /**
75 * Result for IActivityManager.startActivity: an error where the
76 * start had to be canceled.
77 * @hide
78 */
79 public static final int START_CANCELED = -6;
80
81 /**
82 * Result for IActivityManager.startActivity: an error where the
83 * thing being started is not an activity.
84 * @hide
85 */
86 public static final int START_NOT_ACTIVITY = -5;
87
88 /**
89 * Result for IActivityManager.startActivity: an error where the
90 * caller does not have permission to start the activity.
91 * @hide
92 */
93 public static final int START_PERMISSION_DENIED = -4;
94
95 /**
96 * Result for IActivityManager.startActivity: an error where the
97 * caller has requested both to forward a result and to receive
98 * a result.
99 * @hide
100 */
101 public static final int START_FORWARD_AND_REQUEST_CONFLICT = -3;
102
103 /**
104 * Result for IActivityManager.startActivity: an error where the
105 * requested class is not found.
106 * @hide
107 */
108 public static final int START_CLASS_NOT_FOUND = -2;
109
110 /**
111 * Result for IActivityManager.startActivity: an error where the
112 * given Intent could not be resolved to an activity.
113 * @hide
114 */
115 public static final int START_INTENT_NOT_RESOLVED = -1;
116
117 /**
118 * Result for IActivityManaqer.startActivity: the activity was started
119 * successfully as normal.
120 * @hide
121 */
122 public static final int START_SUCCESS = 0;
123
124 /**
125 * Result for IActivityManaqer.startActivity: the caller asked that the Intent not
126 * be executed if it is the recipient, and that is indeed the case.
127 * @hide
128 */
129 public static final int START_RETURN_INTENT_TO_CALLER = 1;
130
131 /**
132 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
133 * a task was simply brought to the foreground.
134 * @hide
135 */
136 public static final int START_TASK_TO_FRONT = 2;
137
138 /**
139 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
140 * the given Intent was given to the existing top activity.
141 * @hide
142 */
143 public static final int START_DELIVERED_TO_TOP = 3;
144
145 /**
146 * Result for IActivityManaqer.startActivity: request was canceled because
147 * app switches are temporarily canceled to ensure the user's last request
148 * (such as pressing home) is performed.
149 * @hide
150 */
151 public static final int START_SWITCHES_CANCELED = 4;
152
153 /**
154 * Flag for IActivityManaqer.startActivity: do special start mode where
155 * a new activity is launched only if it is needed.
156 * @hide
157 */
158 public static final int START_FLAG_ONLY_IF_NEEDED = 1<<0;
159
160 /**
161 * Flag for IActivityManaqer.startActivity: launch the app for
162 * debugging.
163 * @hide
164 */
165 public static final int START_FLAG_DEBUG = 1<<1;
166
167 /**
168 * Flag for IActivityManaqer.startActivity: launch the app for
169 * OpenGL tracing.
170 * @hide
171 */
172 public static final int START_FLAG_OPENGL_TRACES = 1<<2;
173
174 /**
175 * Flag for IActivityManaqer.startActivity: if the app is being
176 * launched for profiling, automatically stop the profiler once done.
177 * @hide
178 */
179 public static final int START_FLAG_AUTO_STOP_PROFILER = 1<<3;
180
181 /**
182 * Result for IActivityManaqer.broadcastIntent: success!
183 * @hide
184 */
185 public static final int BROADCAST_SUCCESS = 0;
186
187 /**
188 * Result for IActivityManaqer.broadcastIntent: attempt to broadcast
189 * a sticky intent without appropriate permission.
190 * @hide
191 */
192 public static final int BROADCAST_STICKY_CANT_HAVE_PERMISSION = -1;
193
194 /**
195 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
196 * for a sendBroadcast operation.
197 * @hide
198 */
199 public static final int INTENT_SENDER_BROADCAST = 1;
200
201 /**
202 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
203 * for a startActivity operation.
204 * @hide
205 */
206 public static final int INTENT_SENDER_ACTIVITY = 2;
207
208 /**
209 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
210 * for an activity result operation.
211 * @hide
212 */
213 public static final int INTENT_SENDER_ACTIVITY_RESULT = 3;
214
215 /**
216 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
217 * for a startService operation.
218 * @hide
219 */
220 public static final int INTENT_SENDER_SERVICE = 4;
221
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700222 /** @hide User operation call: success! */
223 public static final int USER_OP_SUCCESS = 0;
224
225 /** @hide User operation call: given user id is not known. */
226 public static final int USER_OP_UNKNOWN_USER = -1;
227
228 /** @hide User operation call: given user id is the current user, can't be stopped. */
229 public static final int USER_OP_IS_CURRENT = -2;
230
Dianne Hackborna413dc02013-07-12 12:02:55 -0700231 /** @hide Process is a persistent system process. */
232 public static final int PROCESS_STATE_PERSISTENT = 0;
233
234 /** @hide Process is a persistent system process and is doing UI. */
235 public static final int PROCESS_STATE_PERSISTENT_UI = 1;
236
Dianne Hackbornc8230512013-07-13 21:32:12 -0700237 /** @hide Process is hosting the current top activities. Note that this covers
238 * all activities that are visible to the user. */
Dianne Hackborna413dc02013-07-12 12:02:55 -0700239 public static final int PROCESS_STATE_TOP = 2;
240
241 /** @hide Process is important to the user, and something they are aware of. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700242 public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 3;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700243
244 /** @hide Process is important to the user, but not something they are aware of. */
245 public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 4;
246
Dianne Hackborna413dc02013-07-12 12:02:55 -0700247 /** @hide Process is in the background running a backup/restore operation. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700248 public static final int PROCESS_STATE_BACKUP = 5;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700249
Dianne Hackbornc8230512013-07-13 21:32:12 -0700250 /** @hide Process is in the background, but it can't restore its state so we want
251 * to try to avoid killing it. */
252 public static final int PROCESS_STATE_HEAVY_WEIGHT = 6;
253
254 /** @hide Process is in the background running a service. Unlike oom_adj, this level
255 * is used for both the normal running in background state and the executing
256 * operations state. */
Dianne Hackborna413dc02013-07-12 12:02:55 -0700257 public static final int PROCESS_STATE_SERVICE = 7;
258
Dianne Hackbornc8230512013-07-13 21:32:12 -0700259 /** @hide Process is in the background running a receiver. Note that from the
260 * perspective of oom_adj receivers run at a higher foreground level, but for our
261 * prioritization here that is not necessary and putting them below services means
262 * many fewer changes in some process states as they receive broadcasts. */
263 public static final int PROCESS_STATE_RECEIVER = 8;
264
Dianne Hackborna413dc02013-07-12 12:02:55 -0700265 /** @hide Process is in the background but hosts the home activity. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700266 public static final int PROCESS_STATE_HOME = 9;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700267
268 /** @hide Process is in the background but hosts the last shown activity. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700269 public static final int PROCESS_STATE_LAST_ACTIVITY = 10;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700270
Dianne Hackbornc8230512013-07-13 21:32:12 -0700271 /** @hide Process is being cached for later use and contains activities. */
272 public static final int PROCESS_STATE_CACHED_ACTIVITY = 11;
273
274 /** @hide Process is being cached for later use and is a client of another cached
275 * process that contains activities. */
276 public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 12;
277
278 /** @hide Process is being cached for later use and is empty. */
279 public static final int PROCESS_STATE_CACHED_EMPTY = 13;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /*package*/ ActivityManager(Context context, Handler handler) {
282 mContext = context;
283 mHandler = handler;
284 }
285
286 /**
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700287 * Screen compatibility mode: the application most always run in
288 * compatibility mode.
289 * @hide
290 */
291 public static final int COMPAT_MODE_ALWAYS = -1;
292
293 /**
294 * Screen compatibility mode: the application can never run in
295 * compatibility mode.
296 * @hide
297 */
298 public static final int COMPAT_MODE_NEVER = -2;
299
300 /**
301 * Screen compatibility mode: unknown.
302 * @hide
303 */
304 public static final int COMPAT_MODE_UNKNOWN = -3;
305
306 /**
307 * Screen compatibility mode: the application currently has compatibility
308 * mode disabled.
309 * @hide
310 */
311 public static final int COMPAT_MODE_DISABLED = 0;
312
313 /**
314 * Screen compatibility mode: the application currently has compatibility
315 * mode enabled.
316 * @hide
317 */
318 public static final int COMPAT_MODE_ENABLED = 1;
319
320 /**
321 * Screen compatibility mode: request to toggle the application's
322 * compatibility mode.
323 * @hide
324 */
325 public static final int COMPAT_MODE_TOGGLE = 2;
326
327 /** @hide */
328 public int getFrontActivityScreenCompatMode() {
329 try {
330 return ActivityManagerNative.getDefault().getFrontActivityScreenCompatMode();
331 } catch (RemoteException e) {
332 // System dead, we will be dead too soon!
333 return 0;
334 }
335 }
336
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700337 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700338 public void setFrontActivityScreenCompatMode(int mode) {
339 try {
340 ActivityManagerNative.getDefault().setFrontActivityScreenCompatMode(mode);
341 } catch (RemoteException e) {
342 // System dead, we will be dead too soon!
343 }
344 }
345
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700346 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700347 public int getPackageScreenCompatMode(String packageName) {
348 try {
349 return ActivityManagerNative.getDefault().getPackageScreenCompatMode(packageName);
350 } catch (RemoteException e) {
351 // System dead, we will be dead too soon!
352 return 0;
353 }
354 }
355
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700356 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700357 public void setPackageScreenCompatMode(String packageName, int mode) {
358 try {
359 ActivityManagerNative.getDefault().setPackageScreenCompatMode(packageName, mode);
360 } catch (RemoteException e) {
361 // System dead, we will be dead too soon!
362 }
363 }
364
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700365 /** @hide */
366 public boolean getPackageAskScreenCompat(String packageName) {
367 try {
368 return ActivityManagerNative.getDefault().getPackageAskScreenCompat(packageName);
369 } catch (RemoteException e) {
370 // System dead, we will be dead too soon!
371 return false;
372 }
373 }
374
375 /** @hide */
376 public void setPackageAskScreenCompat(String packageName, boolean ask) {
377 try {
378 ActivityManagerNative.getDefault().setPackageAskScreenCompat(packageName, ask);
379 } catch (RemoteException e) {
380 // System dead, we will be dead too soon!
381 }
382 }
383
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700384 /**
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700385 * Return the approximate per-application memory class of the current
386 * device. This gives you an idea of how hard a memory limit you should
387 * impose on your application to let the overall system work best. The
388 * returned value is in megabytes; the baseline Android memory class is
389 * 16 (which happens to be the Java heap limit of those devices); some
390 * device with more memory may return 24 or even higher numbers.
391 */
392 public int getMemoryClass() {
393 return staticGetMemoryClass();
394 }
395
396 /** @hide */
397 static public int staticGetMemoryClass() {
398 // Really brain dead right now -- just take this from the configured
399 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn7ad33c82011-03-08 15:53:25 -0800400 String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", "");
Dianne Hackbornde398512011-01-18 18:45:21 -0800401 if (vmHeapSize != null && !"".equals(vmHeapSize)) {
402 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
403 }
404 return staticGetLargeMemoryClass();
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800405 }
406
407 /**
408 * Return the approximate per-application memory class of the current
409 * device when an application is running with a large heap. This is the
410 * space available for memory-intensive applications; most applications
411 * should not need this amount of memory, and should instead stay with the
412 * {@link #getMemoryClass()} limit. The returned value is in megabytes.
413 * This may be the same size as {@link #getMemoryClass()} on memory
414 * constrained devices, or it may be significantly larger on devices with
415 * a large amount of available RAM.
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800416 *
417 * <p>The is the size of the application's Dalvik heap if it has
418 * specified <code>android:largeHeap="true"</code> in its manifest.
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800419 */
420 public int getLargeMemoryClass() {
421 return staticGetLargeMemoryClass();
422 }
423
424 /** @hide */
425 static public int staticGetLargeMemoryClass() {
426 // Really brain dead right now -- just take this from the configured
427 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700428 String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
429 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
430 }
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700431
432 /**
433 * Returns true if this is a low-RAM device. Exactly whether a device is low-RAM
434 * is ultimately up to the device configuration, but currently it generally means
435 * something in the class of a 512MB device with about a 800x480 or less screen.
436 * This is mostly intended to be used by apps to determine whether they should turn
437 * off certain features that require more RAM.
438 */
439 public boolean isLowRamDevice() {
440 return isLowRamDeviceStatic();
441 }
442
443 /** @hide */
444 public static boolean isLowRamDeviceStatic() {
445 return Resources.getSystem().getBoolean(com.android.internal.R.bool.config_lowRamDevice);
446 }
447
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700448 /**
Dianne Hackbornb3756322011-08-12 13:58:13 -0700449 * Used by persistent processes to determine if they are running on a
450 * higher-end device so should be okay using hardware drawing acceleration
451 * (which tends to consume a lot more RAM).
452 * @hide
453 */
Jeff Brown98365d72012-08-19 20:30:52 -0700454 static public boolean isHighEndGfx() {
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700455 return !isLowRamDeviceStatic() &&
456 !Resources.getSystem().getBoolean(com.android.internal.R.bool.config_avoidGfxAccel);
Dianne Hackborn49d228b32011-08-24 17:37:31 -0700457 }
458
459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 * Information you can retrieve about tasks that the user has most recently
461 * started or visited.
462 */
463 public static class RecentTaskInfo implements Parcelable {
464 /**
465 * If this task is currently running, this is the identifier for it.
466 * If it is not running, this will be -1.
467 */
468 public int id;
469
470 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -0800471 * The true identifier of this task, valid even if it is not running.
472 */
473 public int persistentId;
474
475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 * The original Intent used to launch the task. You can use this
477 * Intent to re-launch the task (if it is no longer running) or bring
478 * the current task to the front.
479 */
480 public Intent baseIntent;
481
482 /**
483 * If this task was started from an alias, this is the actual
484 * activity component that was initially started; the component of
485 * the baseIntent in this case is the name of the actual activity
486 * implementation that the alias referred to. Otherwise, this is null.
487 */
488 public ComponentName origActivity;
Dianne Hackbornd2835932010-12-13 16:28:46 -0800489
490 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -0800491 * Description of the task's last state.
492 */
493 public CharSequence description;
Craig Mautner6d90fed2013-05-22 15:03:10 -0700494
495 /**
496 * The id of the ActivityStack this Task was on most recently.
497 */
498 public int stackId;
499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 public RecentTaskInfo() {
501 }
502
Craig Mautner6d90fed2013-05-22 15:03:10 -0700503 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 public int describeContents() {
505 return 0;
506 }
507
Craig Mautner6d90fed2013-05-22 15:03:10 -0700508 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public void writeToParcel(Parcel dest, int flags) {
510 dest.writeInt(id);
Dianne Hackbornd94df452011-02-16 18:53:31 -0800511 dest.writeInt(persistentId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 if (baseIntent != null) {
513 dest.writeInt(1);
514 baseIntent.writeToParcel(dest, 0);
515 } else {
516 dest.writeInt(0);
517 }
518 ComponentName.writeToParcel(origActivity, dest);
Dianne Hackbornd2835932010-12-13 16:28:46 -0800519 TextUtils.writeToParcel(description, dest,
520 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
Craig Mautner6d90fed2013-05-22 15:03:10 -0700521 dest.writeInt(stackId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
523
524 public void readFromParcel(Parcel source) {
525 id = source.readInt();
Dianne Hackbornd94df452011-02-16 18:53:31 -0800526 persistentId = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 if (source.readInt() != 0) {
528 baseIntent = Intent.CREATOR.createFromParcel(source);
529 } else {
530 baseIntent = null;
531 }
532 origActivity = ComponentName.readFromParcel(source);
Dianne Hackbornd2835932010-12-13 16:28:46 -0800533 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
Craig Mautner6d90fed2013-05-22 15:03:10 -0700534 stackId = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
Craig Mautner6d90fed2013-05-22 15:03:10 -0700536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public static final Creator<RecentTaskInfo> CREATOR
538 = new Creator<RecentTaskInfo>() {
539 public RecentTaskInfo createFromParcel(Parcel source) {
540 return new RecentTaskInfo(source);
541 }
542 public RecentTaskInfo[] newArray(int size) {
543 return new RecentTaskInfo[size];
544 }
545 };
546
547 private RecentTaskInfo(Parcel source) {
548 readFromParcel(source);
549 }
550 }
551
552 /**
553 * Flag for use with {@link #getRecentTasks}: return all tasks, even those
554 * that have set their
555 * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag.
556 */
557 public static final int RECENT_WITH_EXCLUDED = 0x0001;
558
559 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -0800560 * Provides a list that does not contain any
Dianne Hackborn53d9264d2010-04-13 12:49:14 -0700561 * recent tasks that currently are not available to the user.
562 */
563 public static final int RECENT_IGNORE_UNAVAILABLE = 0x0002;
Dianne Hackbornd2835932010-12-13 16:28:46 -0800564
565 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 * Return a list of the tasks that the user has recently launched, with
567 * the most recent being first and older ones after in order.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -0700568 *
569 * <p><b>Note: this method is only intended for debugging and presenting
570 * task management user interfaces</b>. This should never be used for
571 * core logic in an application, such as deciding between different
572 * behaviors based on the information found here. Such uses are
573 * <em>not</em> supported, and will likely break in the future. For
574 * example, if multiple applications can be actively running at the
575 * same time, assumptions made about the meaning of the data here for
576 * purposes of control flow will be incorrect.</p>
577 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 * @param maxNum The maximum number of entries to return in the list. The
579 * actual number returned may be smaller, depending on how many tasks the
580 * user has started and the maximum number the system can remember.
Dianne Hackbornd2835932010-12-13 16:28:46 -0800581 * @param flags Information about what to return. May be any combination
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800582 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 *
584 * @return Returns a list of RecentTaskInfo records describing each of
585 * the recent tasks.
586 *
587 * @throws SecurityException Throws SecurityException if the caller does
588 * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
589 */
590 public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags)
591 throws SecurityException {
592 try {
593 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700594 flags, UserHandle.myUserId());
Amith Yamasani82644082012-08-03 13:09:11 -0700595 } catch (RemoteException e) {
596 // System dead, we will be dead too soon!
597 return null;
598 }
599 }
600
601 /**
602 * Same as {@link #getRecentTasks(int, int)} but returns the recent tasks for a
603 * specific user. It requires holding
604 * the {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission.
605 * @param maxNum The maximum number of entries to return in the list. The
606 * actual number returned may be smaller, depending on how many tasks the
607 * user has started and the maximum number the system can remember.
608 * @param flags Information about what to return. May be any combination
609 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
610 *
611 * @return Returns a list of RecentTaskInfo records describing each of
612 * the recent tasks.
613 *
614 * @throws SecurityException Throws SecurityException if the caller does
615 * not hold the {@link android.Manifest.permission#GET_TASKS} or the
616 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permissions.
617 * @hide
618 */
619 public List<RecentTaskInfo> getRecentTasksForUser(int maxNum, int flags, int userId)
620 throws SecurityException {
621 try {
622 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
623 flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 } catch (RemoteException e) {
625 // System dead, we will be dead too soon!
626 return null;
627 }
628 }
629
630 /**
631 * Information you can retrieve about a particular task that is currently
632 * "running" in the system. Note that a running task does not mean the
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700633 * given task actually has a process it is actively running in; it simply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 * means that the user has gone to it and never closed it, but currently
635 * the system may have killed its process and is only holding on to its
636 * last state in order to restart it when the user returns.
637 */
638 public static class RunningTaskInfo implements Parcelable {
639 /**
640 * A unique identifier for this task.
641 */
642 public int id;
643
644 /**
645 * The component launched as the first activity in the task. This can
646 * be considered the "application" of this task.
647 */
648 public ComponentName baseActivity;
649
650 /**
651 * The activity component at the top of the history stack of the task.
652 * This is what the user is currently doing.
653 */
654 public ComponentName topActivity;
655
656 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800657 * Thumbnail representation of the task's current state. Currently
658 * always null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 */
660 public Bitmap thumbnail;
661
662 /**
663 * Description of the task's current state.
664 */
665 public CharSequence description;
666
667 /**
668 * Number of activities in this task.
669 */
670 public int numActivities;
671
672 /**
673 * Number of activities that are currently running (not stopped
674 * and persisted) in this task.
675 */
676 public int numRunning;
677
678 public RunningTaskInfo() {
679 }
680
681 public int describeContents() {
682 return 0;
683 }
684
685 public void writeToParcel(Parcel dest, int flags) {
686 dest.writeInt(id);
687 ComponentName.writeToParcel(baseActivity, dest);
688 ComponentName.writeToParcel(topActivity, dest);
689 if (thumbnail != null) {
690 dest.writeInt(1);
691 thumbnail.writeToParcel(dest, 0);
692 } else {
693 dest.writeInt(0);
694 }
695 TextUtils.writeToParcel(description, dest,
696 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
697 dest.writeInt(numActivities);
698 dest.writeInt(numRunning);
699 }
700
701 public void readFromParcel(Parcel source) {
702 id = source.readInt();
703 baseActivity = ComponentName.readFromParcel(source);
704 topActivity = ComponentName.readFromParcel(source);
705 if (source.readInt() != 0) {
706 thumbnail = Bitmap.CREATOR.createFromParcel(source);
707 } else {
708 thumbnail = null;
709 }
710 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
711 numActivities = source.readInt();
712 numRunning = source.readInt();
713 }
714
715 public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() {
716 public RunningTaskInfo createFromParcel(Parcel source) {
717 return new RunningTaskInfo(source);
718 }
719 public RunningTaskInfo[] newArray(int size) {
720 return new RunningTaskInfo[size];
721 }
722 };
723
724 private RunningTaskInfo(Parcel source) {
725 readFromParcel(source);
726 }
727 }
Dianne Hackbornd2835932010-12-13 16:28:46 -0800728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 /**
730 * Return a list of the tasks that are currently running, with
731 * the most recent being first and older ones after in order. Note that
732 * "running" does not mean any of the task's code is currently loaded or
733 * activity -- the task may have been frozen by the system, so that it
734 * can be restarted in its previous state when next brought to the
735 * foreground.
736 *
737 * @param maxNum The maximum number of entries to return in the list. The
738 * actual number returned may be smaller, depending on how many tasks the
739 * user has started.
Jim Miller0b2a6d02010-07-13 18:01:29 -0700740 *
741 * @param flags Optional flags
742 * @param receiver Optional receiver for delayed thumbnails
743 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 * @return Returns a list of RunningTaskInfo records describing each of
745 * the running tasks.
746 *
Jim Miller0b2a6d02010-07-13 18:01:29 -0700747 * Some thumbnails may not be available at the time of this call. The optional
748 * receiver may be used to receive those thumbnails.
749 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 * @throws SecurityException Throws SecurityException if the caller does
751 * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
Jim Miller0b2a6d02010-07-13 18:01:29 -0700752 *
753 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 */
Jim Miller0b2a6d02010-07-13 18:01:29 -0700755 public List<RunningTaskInfo> getRunningTasks(int maxNum, int flags, IThumbnailReceiver receiver)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 throws SecurityException {
757 try {
Jim Miller0b2a6d02010-07-13 18:01:29 -0700758 return ActivityManagerNative.getDefault().getTasks(maxNum, flags, receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 } catch (RemoteException e) {
760 // System dead, we will be dead too soon!
761 return null;
762 }
763 }
Jim Miller0b2a6d02010-07-13 18:01:29 -0700764
765 /**
766 * Return a list of the tasks that are currently running, with
767 * the most recent being first and older ones after in order. Note that
768 * "running" does not mean any of the task's code is currently loaded or
769 * activity -- the task may have been frozen by the system, so that it
770 * can be restarted in its previous state when next brought to the
771 * foreground.
772 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -0700773 * <p><b>Note: this method is only intended for debugging and presenting
774 * task management user interfaces</b>. This should never be used for
775 * core logic in an application, such as deciding between different
776 * behaviors based on the information found here. Such uses are
777 * <em>not</em> supported, and will likely break in the future. For
778 * example, if multiple applications can be actively running at the
779 * same time, assumptions made about the meaning of the data here for
780 * purposes of control flow will be incorrect.</p>
781 *
Jim Miller0b2a6d02010-07-13 18:01:29 -0700782 * @param maxNum The maximum number of entries to return in the list. The
783 * actual number returned may be smaller, depending on how many tasks the
784 * user has started.
785 *
786 * @return Returns a list of RunningTaskInfo records describing each of
787 * the running tasks.
788 *
789 * @throws SecurityException Throws SecurityException if the caller does
790 * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
791 */
792 public List<RunningTaskInfo> getRunningTasks(int maxNum)
793 throws SecurityException {
794 return getRunningTasks(maxNum, 0, null);
795 }
796
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700797 /**
798 * Remove some end of a task's activity stack that is not part of
799 * the main application. The selected activities will be finished, so
800 * they are no longer part of the main task.
801 *
802 * @param taskId The identifier of the task.
803 * @param subTaskIndex The number of the sub-task; this corresponds
804 * to the index of the thumbnail returned by {@link #getTaskThumbnails(int)}.
805 * @return Returns true if the sub-task was found and was removed.
806 *
807 * @hide
808 */
809 public boolean removeSubTask(int taskId, int subTaskIndex)
810 throws SecurityException {
811 try {
812 return ActivityManagerNative.getDefault().removeSubTask(taskId, subTaskIndex);
813 } catch (RemoteException e) {
814 // System dead, we will be dead too soon!
815 return false;
816 }
817 }
818
819 /**
820 * If set, the process of the root activity of the task will be killed
821 * as part of removing the task.
822 * @hide
823 */
824 public static final int REMOVE_TASK_KILL_PROCESS = 0x0001;
825
826 /**
827 * Completely remove the given task.
828 *
829 * @param taskId Identifier of the task to be removed.
830 * @param flags Additional operational flags. May be 0 or
831 * {@link #REMOVE_TASK_KILL_PROCESS}.
832 * @return Returns true if the given task was found and removed.
833 *
834 * @hide
835 */
836 public boolean removeTask(int taskId, int flags)
837 throws SecurityException {
838 try {
839 return ActivityManagerNative.getDefault().removeTask(taskId, flags);
840 } catch (RemoteException e) {
841 // System dead, we will be dead too soon!
842 return false;
843 }
844 }
845
Dianne Hackbornd94df452011-02-16 18:53:31 -0800846 /** @hide */
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700847 public static class TaskThumbnails implements Parcelable {
848 public Bitmap mainThumbnail;
849
850 public int numSubThumbbails;
851
852 /** @hide */
853 public IThumbnailRetriever retriever;
854
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700855 public TaskThumbnails() {
856 }
857
858 public Bitmap getSubThumbnail(int index) {
859 try {
860 return retriever.getThumbnail(index);
861 } catch (RemoteException e) {
862 return null;
863 }
864 }
865
866 public int describeContents() {
867 return 0;
868 }
869
870 public void writeToParcel(Parcel dest, int flags) {
871 if (mainThumbnail != null) {
872 dest.writeInt(1);
873 mainThumbnail.writeToParcel(dest, 0);
874 } else {
875 dest.writeInt(0);
876 }
877 dest.writeInt(numSubThumbbails);
878 dest.writeStrongInterface(retriever);
879 }
880
881 public void readFromParcel(Parcel source) {
882 if (source.readInt() != 0) {
883 mainThumbnail = Bitmap.CREATOR.createFromParcel(source);
884 } else {
885 mainThumbnail = null;
886 }
887 numSubThumbbails = source.readInt();
888 retriever = IThumbnailRetriever.Stub.asInterface(source.readStrongBinder());
889 }
890
891 public static final Creator<TaskThumbnails> CREATOR = new Creator<TaskThumbnails>() {
892 public TaskThumbnails createFromParcel(Parcel source) {
893 return new TaskThumbnails(source);
894 }
895 public TaskThumbnails[] newArray(int size) {
896 return new TaskThumbnails[size];
897 }
898 };
899
900 private TaskThumbnails(Parcel source) {
901 readFromParcel(source);
902 }
903 }
904
905 /** @hide */
906 public TaskThumbnails getTaskThumbnails(int id) throws SecurityException {
Dianne Hackbornd94df452011-02-16 18:53:31 -0800907 try {
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700908 return ActivityManagerNative.getDefault().getTaskThumbnails(id);
Dianne Hackbornd94df452011-02-16 18:53:31 -0800909 } catch (RemoteException e) {
910 // System dead, we will be dead too soon!
911 return null;
912 }
913 }
Dianne Hackborn15491c62012-09-19 10:59:14 -0700914
915 /** @hide */
916 public Bitmap getTaskTopThumbnail(int id) throws SecurityException {
917 try {
918 return ActivityManagerNative.getDefault().getTaskTopThumbnail(id);
919 } catch (RemoteException e) {
920 // System dead, we will be dead too soon!
921 return null;
922 }
923 }
924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800926 * Flag for {@link #moveTaskToFront(int, int)}: also move the "home"
927 * activity along with the task, so it is positioned immediately behind
928 * the task.
929 */
930 public static final int MOVE_TASK_WITH_HOME = 0x00000001;
931
932 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -0800933 * Flag for {@link #moveTaskToFront(int, int)}: don't count this as a
934 * user-instigated action, so the current activity will not receive a
935 * hint that the user is leaving.
936 */
937 public static final int MOVE_TASK_NO_USER_ACTION = 0x00000002;
938
939 /**
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700940 * Equivalent to calling {@link #moveTaskToFront(int, int, Bundle)}
941 * with a null options argument.
942 *
943 * @param taskId The identifier of the task to be moved, as found in
944 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
945 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -0700946 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700947 */
948 public void moveTaskToFront(int taskId, int flags) {
949 moveTaskToFront(taskId, flags, null);
950 }
951
952 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800953 * Ask that the task associated with a given task ID be moved to the
954 * front of the stack, so it is now visible to the user. Requires that
955 * the caller hold permission {@link android.Manifest.permission#REORDER_TASKS}
956 * or a SecurityException will be thrown.
957 *
958 * @param taskId The identifier of the task to be moved, as found in
959 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
960 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -0700961 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700962 * @param options Additional options for the operation, either null or
963 * as per {@link Context#startActivity(Intent, android.os.Bundle)
964 * Context.startActivity(Intent, Bundle)}.
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800965 */
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700966 public void moveTaskToFront(int taskId, int flags, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800967 try {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700968 ActivityManagerNative.getDefault().moveTaskToFront(taskId, flags, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800969 } catch (RemoteException e) {
970 // System dead, we will be dead too soon!
971 }
972 }
973
974 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 * Information you can retrieve about a particular Service that is
976 * currently running in the system.
977 */
978 public static class RunningServiceInfo implements Parcelable {
979 /**
980 * The service component.
981 */
982 public ComponentName service;
983
984 /**
985 * If non-zero, this is the process the service is running in.
986 */
987 public int pid;
988
989 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700990 * The UID that owns this service.
991 */
992 public int uid;
993
994 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 * The name of the process this service runs in.
996 */
997 public String process;
998
999 /**
1000 * Set to true if the service has asked to run as a foreground process.
1001 */
1002 public boolean foreground;
1003
1004 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001005 * The time when the service was first made active, either by someone
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001006 * starting or binding to it. This
1007 * is in units of {@link android.os.SystemClock#elapsedRealtime()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 */
1009 public long activeSince;
1010
1011 /**
1012 * Set to true if this service has been explicitly started.
1013 */
1014 public boolean started;
1015
1016 /**
1017 * Number of clients connected to the service.
1018 */
1019 public int clientCount;
1020
1021 /**
1022 * Number of times the service's process has crashed while the service
1023 * is running.
1024 */
1025 public int crashCount;
1026
1027 /**
1028 * The time when there was last activity in the service (either
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001029 * explicit requests to start it or clients binding to it). This
1030 * is in units of {@link android.os.SystemClock#uptimeMillis()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public long lastActivityTime;
1033
1034 /**
1035 * If non-zero, this service is not currently running, but scheduled to
1036 * restart at the given time.
1037 */
1038 public long restarting;
1039
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001040 /**
1041 * Bit for {@link #flags}: set if this service has been
1042 * explicitly started.
1043 */
1044 public static final int FLAG_STARTED = 1<<0;
1045
1046 /**
1047 * Bit for {@link #flags}: set if the service has asked to
1048 * run as a foreground process.
1049 */
1050 public static final int FLAG_FOREGROUND = 1<<1;
1051
1052 /**
1053 * Bit for {@link #flags): set if the service is running in a
1054 * core system process.
1055 */
1056 public static final int FLAG_SYSTEM_PROCESS = 1<<2;
1057
1058 /**
1059 * Bit for {@link #flags): set if the service is running in a
1060 * persistent process.
1061 */
1062 public static final int FLAG_PERSISTENT_PROCESS = 1<<3;
1063
1064 /**
1065 * Running flags.
1066 */
1067 public int flags;
1068
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001069 /**
1070 * For special services that are bound to by system code, this is
1071 * the package that holds the binding.
1072 */
1073 public String clientPackage;
1074
1075 /**
1076 * For special services that are bound to by system code, this is
1077 * a string resource providing a user-visible label for who the
1078 * client is.
1079 */
1080 public int clientLabel;
1081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 public RunningServiceInfo() {
1083 }
1084
1085 public int describeContents() {
1086 return 0;
1087 }
1088
1089 public void writeToParcel(Parcel dest, int flags) {
1090 ComponentName.writeToParcel(service, dest);
1091 dest.writeInt(pid);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001092 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 dest.writeString(process);
1094 dest.writeInt(foreground ? 1 : 0);
1095 dest.writeLong(activeSince);
1096 dest.writeInt(started ? 1 : 0);
1097 dest.writeInt(clientCount);
1098 dest.writeInt(crashCount);
1099 dest.writeLong(lastActivityTime);
1100 dest.writeLong(restarting);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001101 dest.writeInt(this.flags);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001102 dest.writeString(clientPackage);
1103 dest.writeInt(clientLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
1105
1106 public void readFromParcel(Parcel source) {
1107 service = ComponentName.readFromParcel(source);
1108 pid = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001109 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 process = source.readString();
1111 foreground = source.readInt() != 0;
1112 activeSince = source.readLong();
1113 started = source.readInt() != 0;
1114 clientCount = source.readInt();
1115 crashCount = source.readInt();
1116 lastActivityTime = source.readLong();
1117 restarting = source.readLong();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001118 flags = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001119 clientPackage = source.readString();
1120 clientLabel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 }
1122
1123 public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() {
1124 public RunningServiceInfo createFromParcel(Parcel source) {
1125 return new RunningServiceInfo(source);
1126 }
1127 public RunningServiceInfo[] newArray(int size) {
1128 return new RunningServiceInfo[size];
1129 }
1130 };
1131
1132 private RunningServiceInfo(Parcel source) {
1133 readFromParcel(source);
1134 }
1135 }
1136
1137 /**
1138 * Return a list of the services that are currently running.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001139 *
1140 * <p><b>Note: this method is only intended for debugging or implementing
1141 * service management type user interfaces.</b></p>
1142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 * @param maxNum The maximum number of entries to return in the list. The
1144 * actual number returned may be smaller, depending on how many services
1145 * are running.
1146 *
1147 * @return Returns a list of RunningServiceInfo records describing each of
1148 * the running tasks.
1149 */
1150 public List<RunningServiceInfo> getRunningServices(int maxNum)
1151 throws SecurityException {
1152 try {
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001153 return ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 .getServices(maxNum, 0);
1155 } catch (RemoteException e) {
1156 // System dead, we will be dead too soon!
1157 return null;
1158 }
1159 }
1160
1161 /**
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001162 * Returns a PendingIntent you can start to show a control panel for the
1163 * given running service. If the service does not have a control panel,
1164 * null is returned.
1165 */
1166 public PendingIntent getRunningServiceControlPanel(ComponentName service)
1167 throws SecurityException {
1168 try {
1169 return ActivityManagerNative.getDefault()
1170 .getRunningServiceControlPanel(service);
1171 } catch (RemoteException e) {
1172 // System dead, we will be dead too soon!
1173 return null;
1174 }
1175 }
1176
1177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 * Information you can retrieve about the available memory through
1179 * {@link ActivityManager#getMemoryInfo}.
1180 */
1181 public static class MemoryInfo implements Parcelable {
1182 /**
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001183 * The available memory on the system. This number should not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 * be considered absolute: due to the nature of the kernel, a significant
1185 * portion of this memory is actually in use and needed for the overall
1186 * system to run well.
1187 */
1188 public long availMem;
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001189
1190 /**
1191 * The total memory accessible by the kernel. This is basically the
1192 * RAM size of the device, not including below-kernel fixed allocations
1193 * like DMA buffers, RAM for the baseband CPU, etc.
1194 */
1195 public long totalMem;
1196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 /**
1198 * The threshold of {@link #availMem} at which we consider memory to be
1199 * low and start killing background services and other non-extraneous
1200 * processes.
1201 */
1202 public long threshold;
1203
1204 /**
1205 * Set to true if the system considers itself to currently be in a low
1206 * memory situation.
1207 */
1208 public boolean lowMemory;
1209
Dianne Hackborn7d608422011-08-07 16:24:18 -07001210 /** @hide */
1211 public long hiddenAppThreshold;
1212 /** @hide */
1213 public long secondaryServerThreshold;
1214 /** @hide */
1215 public long visibleAppThreshold;
1216 /** @hide */
1217 public long foregroundAppThreshold;
1218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 public MemoryInfo() {
1220 }
1221
1222 public int describeContents() {
1223 return 0;
1224 }
1225
1226 public void writeToParcel(Parcel dest, int flags) {
1227 dest.writeLong(availMem);
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001228 dest.writeLong(totalMem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 dest.writeLong(threshold);
1230 dest.writeInt(lowMemory ? 1 : 0);
Dianne Hackborn7d608422011-08-07 16:24:18 -07001231 dest.writeLong(hiddenAppThreshold);
1232 dest.writeLong(secondaryServerThreshold);
1233 dest.writeLong(visibleAppThreshold);
1234 dest.writeLong(foregroundAppThreshold);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236
1237 public void readFromParcel(Parcel source) {
1238 availMem = source.readLong();
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001239 totalMem = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 threshold = source.readLong();
1241 lowMemory = source.readInt() != 0;
Dianne Hackborn7d608422011-08-07 16:24:18 -07001242 hiddenAppThreshold = source.readLong();
1243 secondaryServerThreshold = source.readLong();
1244 visibleAppThreshold = source.readLong();
1245 foregroundAppThreshold = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 }
1247
1248 public static final Creator<MemoryInfo> CREATOR
1249 = new Creator<MemoryInfo>() {
1250 public MemoryInfo createFromParcel(Parcel source) {
1251 return new MemoryInfo(source);
1252 }
1253 public MemoryInfo[] newArray(int size) {
1254 return new MemoryInfo[size];
1255 }
1256 };
1257
1258 private MemoryInfo(Parcel source) {
1259 readFromParcel(source);
1260 }
1261 }
1262
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001263 /**
1264 * Return general information about the memory state of the system. This
1265 * can be used to help decide how to manage your own memory, though note
1266 * that polling is not recommended and
1267 * {@link android.content.ComponentCallbacks2#onTrimMemory(int)
1268 * ComponentCallbacks2.onTrimMemory(int)} is the preferred way to do this.
1269 * Also see {@link #getMyMemoryState} for how to retrieve the current trim
1270 * level of your process as needed, which gives a better hint for how to
1271 * manage its memory.
1272 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 public void getMemoryInfo(MemoryInfo outInfo) {
1274 try {
1275 ActivityManagerNative.getDefault().getMemoryInfo(outInfo);
1276 } catch (RemoteException e) {
1277 }
1278 }
Craig Mautner967212c2013-04-13 21:10:58 -07001279
Craig Mautner5ff12102013-05-24 12:50:15 -07001280 /**
1281 * Information you can retrieve about the WindowManager StackBox hierarchy.
1282 * @hide
1283 */
1284 public static class StackBoxInfo implements Parcelable {
1285 public int stackBoxId;
1286 public float weight;
1287 public boolean vertical;
1288 public Rect bounds;
1289 public StackBoxInfo[] children;
1290 public int stackId;
1291 public StackInfo stack;
1292
1293 @Override
1294 public int describeContents() {
1295 return 0;
1296 }
1297
1298 @Override
1299 public void writeToParcel(Parcel dest, int flags) {
1300 dest.writeInt(stackBoxId);
1301 dest.writeFloat(weight);
1302 dest.writeInt(vertical ? 1 : 0);
1303 bounds.writeToParcel(dest, flags);
1304 dest.writeInt(stackId);
1305 if (children != null) {
1306 children[0].writeToParcel(dest, flags);
1307 children[1].writeToParcel(dest, flags);
1308 } else {
1309 stack.writeToParcel(dest, flags);
1310 }
1311 }
1312
1313 public void readFromParcel(Parcel source) {
1314 stackBoxId = source.readInt();
1315 weight = source.readFloat();
1316 vertical = source.readInt() == 1;
1317 bounds = Rect.CREATOR.createFromParcel(source);
1318 stackId = source.readInt();
1319 if (stackId == -1) {
1320 children = new StackBoxInfo[2];
1321 children[0] = StackBoxInfo.CREATOR.createFromParcel(source);
1322 children[1] = StackBoxInfo.CREATOR.createFromParcel(source);
1323 } else {
1324 stack = StackInfo.CREATOR.createFromParcel(source);
1325 }
1326 }
1327
1328 public static final Creator<StackBoxInfo> CREATOR =
1329 new Creator<ActivityManager.StackBoxInfo>() {
1330
1331 @Override
1332 public StackBoxInfo createFromParcel(Parcel source) {
1333 return new StackBoxInfo(source);
1334 }
1335
1336 @Override
1337 public StackBoxInfo[] newArray(int size) {
1338 return new StackBoxInfo[size];
1339 }
1340 };
1341
1342 public StackBoxInfo() {
1343 }
1344
1345 public StackBoxInfo(Parcel source) {
1346 readFromParcel(source);
1347 }
1348
1349 public String toString(String prefix) {
1350 StringBuilder sb = new StringBuilder(256);
1351 sb.append(prefix); sb.append("Box id=" + stackBoxId); sb.append(" weight=" + weight);
1352 sb.append(" vertical=" + vertical); sb.append(" bounds=" + bounds.toShortString());
1353 sb.append("\n");
1354 if (children != null) {
1355 sb.append(prefix); sb.append("First child=\n");
1356 sb.append(children[0].toString(prefix + " "));
1357 sb.append(prefix); sb.append("Second child=\n");
1358 sb.append(children[1].toString(prefix + " "));
1359 } else {
1360 sb.append(prefix); sb.append("Stack=\n");
1361 sb.append(stack.toString(prefix + " "));
1362 }
1363 return sb.toString();
1364 }
1365
1366 @Override
1367 public String toString() {
1368 return toString("");
1369 }
1370 }
Craig Mautner967212c2013-04-13 21:10:58 -07001371
1372 /**
1373 * Information you can retrieve about an ActivityStack in the system.
1374 * @hide
1375 */
1376 public static class StackInfo implements Parcelable {
1377 public int stackId;
1378 public Rect bounds;
1379 public int[] taskIds;
1380 public String[] taskNames;
1381
Craig Mautner967212c2013-04-13 21:10:58 -07001382 @Override
1383 public int describeContents() {
1384 return 0;
1385 }
1386
1387 @Override
1388 public void writeToParcel(Parcel dest, int flags) {
1389 dest.writeInt(stackId);
1390 dest.writeInt(bounds.left);
1391 dest.writeInt(bounds.top);
1392 dest.writeInt(bounds.right);
1393 dest.writeInt(bounds.bottom);
1394 dest.writeIntArray(taskIds);
1395 dest.writeStringArray(taskNames);
1396 }
1397
1398 public void readFromParcel(Parcel source) {
1399 stackId = source.readInt();
1400 bounds = new Rect(
1401 source.readInt(), source.readInt(), source.readInt(), source.readInt());
1402 taskIds = source.createIntArray();
1403 taskNames = source.createStringArray();
1404 }
1405
1406 public static final Creator<StackInfo> CREATOR = new Creator<StackInfo>() {
1407 @Override
1408 public StackInfo createFromParcel(Parcel source) {
1409 return new StackInfo(source);
1410 }
1411 @Override
1412 public StackInfo[] newArray(int size) {
1413 return new StackInfo[size];
1414 }
1415 };
1416
Craig Mautner5ff12102013-05-24 12:50:15 -07001417 public StackInfo() {
1418 }
1419
Craig Mautner967212c2013-04-13 21:10:58 -07001420 private StackInfo(Parcel source) {
1421 readFromParcel(source);
1422 }
1423
Craig Mautner5ff12102013-05-24 12:50:15 -07001424 public String toString(String prefix) {
Craig Mautner967212c2013-04-13 21:10:58 -07001425 StringBuilder sb = new StringBuilder(256);
Craig Mautner5ff12102013-05-24 12:50:15 -07001426 sb.append(prefix); sb.append("Stack id="); sb.append(stackId);
Craig Mautner967212c2013-04-13 21:10:58 -07001427 sb.append(" bounds="); sb.append(bounds.toShortString()); sb.append("\n");
Craig Mautner5ff12102013-05-24 12:50:15 -07001428 prefix = prefix + " ";
Craig Mautner967212c2013-04-13 21:10:58 -07001429 for (int i = 0; i < taskIds.length; ++i) {
1430 sb.append(prefix); sb.append("taskId="); sb.append(taskIds[i]);
1431 sb.append(": "); sb.append(taskNames[i]); sb.append("\n");
1432 }
1433 return sb.toString();
1434 }
Craig Mautner5ff12102013-05-24 12:50:15 -07001435
1436 @Override
1437 public String toString() {
1438 return toString("");
1439 }
Craig Mautner967212c2013-04-13 21:10:58 -07001440 }
1441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 /**
1443 * @hide
1444 */
1445 public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
1446 try {
1447 return ActivityManagerNative.getDefault().clearApplicationUserData(packageName,
Dianne Hackborn41203752012-08-31 14:05:51 -07001448 observer, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 } catch (RemoteException e) {
1450 return false;
1451 }
1452 }
Christopher Tatea3664242013-06-26 15:00:18 -07001453
1454 /**
1455 * Permits an application to erase its own data from disk. This is equivalent to
1456 * the user choosing to clear the app's data from within the device settings UI.
1457 *
1458 * @return {@code true} if the application successfully requested that the application's
1459 * data be erased; {@code false} otherwise.
1460 */
1461 public boolean clearApplicationUserData() {
1462 return clearApplicationUserData(mContext.getPackageName(), null);
1463 }
1464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 /**
1466 * Information you can retrieve about any processes that are in an error condition.
1467 */
1468 public static class ProcessErrorStateInfo implements Parcelable {
1469 /**
1470 * Condition codes
1471 */
1472 public static final int NO_ERROR = 0;
1473 public static final int CRASHED = 1;
1474 public static final int NOT_RESPONDING = 2;
1475
1476 /**
1477 * The condition that the process is in.
1478 */
1479 public int condition;
1480
1481 /**
1482 * The process name in which the crash or error occurred.
1483 */
1484 public String processName;
1485
1486 /**
1487 * The pid of this process; 0 if none
1488 */
1489 public int pid;
1490
1491 /**
1492 * The kernel user-ID that has been assigned to this process;
1493 * currently this is not a unique ID (multiple applications can have
1494 * the same uid).
1495 */
1496 public int uid;
1497
1498 /**
Dan Egnor60d87622009-12-16 16:32:58 -08001499 * The activity name associated with the error, if known. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 */
1501 public String tag;
1502
1503 /**
1504 * A short message describing the error condition.
1505 */
1506 public String shortMsg;
1507
1508 /**
1509 * A long message describing the error condition.
1510 */
1511 public String longMsg;
1512
1513 /**
Dan Egnorb7f03672009-12-09 16:22:32 -08001514 * The stack trace where the error originated. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 */
Dan Egnorb7f03672009-12-09 16:22:32 -08001516 public String stackTrace;
1517
1518 /**
1519 * to be deprecated: This value will always be null.
1520 */
1521 public byte[] crashData = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
1523 public ProcessErrorStateInfo() {
1524 }
1525
Craig Mautner5ff12102013-05-24 12:50:15 -07001526 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 public int describeContents() {
1528 return 0;
1529 }
1530
Craig Mautner5ff12102013-05-24 12:50:15 -07001531 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 public void writeToParcel(Parcel dest, int flags) {
1533 dest.writeInt(condition);
1534 dest.writeString(processName);
1535 dest.writeInt(pid);
1536 dest.writeInt(uid);
1537 dest.writeString(tag);
1538 dest.writeString(shortMsg);
1539 dest.writeString(longMsg);
Dan Egnorb7f03672009-12-09 16:22:32 -08001540 dest.writeString(stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
Craig Mautner5ff12102013-05-24 12:50:15 -07001542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 public void readFromParcel(Parcel source) {
1544 condition = source.readInt();
1545 processName = source.readString();
1546 pid = source.readInt();
1547 uid = source.readInt();
1548 tag = source.readString();
1549 shortMsg = source.readString();
1550 longMsg = source.readString();
Dan Egnorb7f03672009-12-09 16:22:32 -08001551 stackTrace = source.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 }
1553
1554 public static final Creator<ProcessErrorStateInfo> CREATOR =
1555 new Creator<ProcessErrorStateInfo>() {
1556 public ProcessErrorStateInfo createFromParcel(Parcel source) {
1557 return new ProcessErrorStateInfo(source);
1558 }
1559 public ProcessErrorStateInfo[] newArray(int size) {
1560 return new ProcessErrorStateInfo[size];
1561 }
1562 };
1563
1564 private ProcessErrorStateInfo(Parcel source) {
1565 readFromParcel(source);
1566 }
1567 }
1568
1569 /**
1570 * Returns a list of any processes that are currently in an error condition. The result
1571 * will be null if all processes are running properly at this time.
1572 *
1573 * @return Returns a list of ProcessErrorStateInfo records, or null if there are no
1574 * current error conditions (it will not return an empty list). This list ordering is not
1575 * specified.
1576 */
1577 public List<ProcessErrorStateInfo> getProcessesInErrorState() {
1578 try {
1579 return ActivityManagerNative.getDefault().getProcessesInErrorState();
1580 } catch (RemoteException e) {
1581 return null;
1582 }
1583 }
1584
1585 /**
1586 * Information you can retrieve about a running process.
1587 */
1588 public static class RunningAppProcessInfo implements Parcelable {
1589 /**
1590 * The name of the process that this object is associated with
1591 */
1592 public String processName;
1593
1594 /**
1595 * The pid of this process; 0 if none
1596 */
1597 public int pid;
1598
Dianne Hackborneb034652009-09-07 00:49:58 -07001599 /**
1600 * The user id of this process.
1601 */
1602 public int uid;
1603
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001604 /**
1605 * All packages that have been loaded into the process.
1606 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 public String pkgList[];
1608
1609 /**
Dianne Hackborn482566e2010-09-03 12:51:28 -07001610 * Constant for {@link #flags}: this is an app that is unable to
1611 * correctly save its state when going to the background,
1612 * so it can not be killed while in the background.
1613 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001614 */
Dianne Hackborn482566e2010-09-03 12:51:28 -07001615 public static final int FLAG_CANT_SAVE_STATE = 1<<0;
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001616
1617 /**
Dianne Hackborn42499172010-10-15 18:45:07 -07001618 * Constant for {@link #flags}: this process is associated with a
1619 * persistent system app.
1620 * @hide
1621 */
1622 public static final int FLAG_PERSISTENT = 1<<1;
1623
1624 /**
Dianne Hackborn0c380492012-08-20 17:23:30 -07001625 * Constant for {@link #flags}: this process is associated with a
1626 * persistent system app.
1627 * @hide
1628 */
1629 public static final int FLAG_HAS_ACTIVITIES = 1<<2;
1630
1631 /**
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001632 * Flags of information. May be any of
Dianne Hackborn482566e2010-09-03 12:51:28 -07001633 * {@link #FLAG_CANT_SAVE_STATE}.
1634 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001635 */
1636 public int flags;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001637
1638 /**
1639 * Last memory trim level reported to the process: corresponds to
1640 * the values supplied to {@link android.content.ComponentCallbacks2#onTrimMemory(int)
1641 * ComponentCallbacks2.onTrimMemory(int)}.
1642 */
1643 public int lastTrimLevel;
1644
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001645 /**
Dianne Hackborna93c2c12012-05-31 15:29:36 -07001646 * Constant for {@link #importance}: this is a persistent process.
1647 * Only used when reporting to process observers.
1648 * @hide
1649 */
1650 public static final int IMPORTANCE_PERSISTENT = 50;
1651
1652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 * Constant for {@link #importance}: this process is running the
1654 * foreground UI.
1655 */
1656 public static final int IMPORTANCE_FOREGROUND = 100;
1657
1658 /**
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001659 * Constant for {@link #importance}: this process is running something
1660 * that is actively visible to the user, though not in the immediate
1661 * foreground.
Dianne Hackborn860755f2010-06-03 18:47:52 -07001662 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001663 public static final int IMPORTANCE_VISIBLE = 200;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001664
1665 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 * Constant for {@link #importance}: this process is running something
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001667 * that is considered to be actively perceptible to the user. An
1668 * example would be an application performing background music playback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001670 public static final int IMPORTANCE_PERCEPTIBLE = 130;
1671
1672 /**
Dianne Hackborn5383f502010-10-22 12:59:20 -07001673 * Constant for {@link #importance}: this process is running an
1674 * application that can not save its state, and thus can't be killed
1675 * while in the background.
1676 * @hide
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001677 */
Dianne Hackborn5383f502010-10-22 12:59:20 -07001678 public static final int IMPORTANCE_CANT_SAVE_STATE = 170;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679
1680 /**
1681 * Constant for {@link #importance}: this process is contains services
1682 * that should remain running.
1683 */
1684 public static final int IMPORTANCE_SERVICE = 300;
1685
1686 /**
1687 * Constant for {@link #importance}: this process process contains
1688 * background code that is expendable.
1689 */
1690 public static final int IMPORTANCE_BACKGROUND = 400;
1691
1692 /**
1693 * Constant for {@link #importance}: this process is empty of any
1694 * actively running code.
1695 */
1696 public static final int IMPORTANCE_EMPTY = 500;
1697
1698 /**
1699 * The relative importance level that the system places on this
1700 * process. May be one of {@link #IMPORTANCE_FOREGROUND},
1701 * {@link #IMPORTANCE_VISIBLE}, {@link #IMPORTANCE_SERVICE},
1702 * {@link #IMPORTANCE_BACKGROUND}, or {@link #IMPORTANCE_EMPTY}. These
1703 * constants are numbered so that "more important" values are always
1704 * smaller than "less important" values.
1705 */
1706 public int importance;
1707
1708 /**
1709 * An additional ordering within a particular {@link #importance}
1710 * category, providing finer-grained information about the relative
1711 * utility of processes within a category. This number means nothing
1712 * except that a smaller values are more recently used (and thus
1713 * more important). Currently an LRU value is only maintained for
1714 * the {@link #IMPORTANCE_BACKGROUND} category, though others may
1715 * be maintained in the future.
1716 */
1717 public int lru;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001718
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001719 /**
1720 * Constant for {@link #importanceReasonCode}: nothing special has
1721 * been specified for the reason for this level.
1722 */
1723 public static final int REASON_UNKNOWN = 0;
1724
1725 /**
1726 * Constant for {@link #importanceReasonCode}: one of the application's
1727 * content providers is being used by another process. The pid of
1728 * the client process is in {@link #importanceReasonPid} and the
1729 * target provider in this process is in
1730 * {@link #importanceReasonComponent}.
1731 */
1732 public static final int REASON_PROVIDER_IN_USE = 1;
1733
1734 /**
1735 * Constant for {@link #importanceReasonCode}: one of the application's
1736 * content providers is being used by another process. The pid of
1737 * the client process is in {@link #importanceReasonPid} and the
1738 * target provider in this process is in
1739 * {@link #importanceReasonComponent}.
1740 */
1741 public static final int REASON_SERVICE_IN_USE = 2;
1742
1743 /**
1744 * The reason for {@link #importance}, if any.
1745 */
1746 public int importanceReasonCode;
1747
1748 /**
1749 * For the specified values of {@link #importanceReasonCode}, this
1750 * is the process ID of the other process that is a client of this
1751 * process. This will be 0 if no other process is using this one.
1752 */
1753 public int importanceReasonPid;
1754
1755 /**
1756 * For the specified values of {@link #importanceReasonCode}, this
1757 * is the name of the component that is being used in this process.
1758 */
1759 public ComponentName importanceReasonComponent;
1760
Dianne Hackborn905577f2011-09-07 18:31:28 -07001761 /**
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07001762 * When {@link #importanceReasonPid} is non-0, this is the importance
Dianne Hackborn905577f2011-09-07 18:31:28 -07001763 * of the other pid. @hide
1764 */
1765 public int importanceReasonImportance;
1766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 public RunningAppProcessInfo() {
1768 importance = IMPORTANCE_FOREGROUND;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001769 importanceReasonCode = REASON_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
1771
1772 public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) {
1773 processName = pProcessName;
1774 pid = pPid;
1775 pkgList = pArr;
1776 }
1777
1778 public int describeContents() {
1779 return 0;
1780 }
1781
1782 public void writeToParcel(Parcel dest, int flags) {
1783 dest.writeString(processName);
1784 dest.writeInt(pid);
Dianne Hackborneb034652009-09-07 00:49:58 -07001785 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 dest.writeStringArray(pkgList);
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001787 dest.writeInt(this.flags);
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001788 dest.writeInt(lastTrimLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 dest.writeInt(importance);
1790 dest.writeInt(lru);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001791 dest.writeInt(importanceReasonCode);
1792 dest.writeInt(importanceReasonPid);
1793 ComponentName.writeToParcel(importanceReasonComponent, dest);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001794 dest.writeInt(importanceReasonImportance);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796
1797 public void readFromParcel(Parcel source) {
1798 processName = source.readString();
1799 pid = source.readInt();
Dianne Hackborneb034652009-09-07 00:49:58 -07001800 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 pkgList = source.readStringArray();
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001802 flags = source.readInt();
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001803 lastTrimLevel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 importance = source.readInt();
1805 lru = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001806 importanceReasonCode = source.readInt();
1807 importanceReasonPid = source.readInt();
1808 importanceReasonComponent = ComponentName.readFromParcel(source);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001809 importanceReasonImportance = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 }
1811
1812 public static final Creator<RunningAppProcessInfo> CREATOR =
1813 new Creator<RunningAppProcessInfo>() {
1814 public RunningAppProcessInfo createFromParcel(Parcel source) {
1815 return new RunningAppProcessInfo(source);
1816 }
1817 public RunningAppProcessInfo[] newArray(int size) {
1818 return new RunningAppProcessInfo[size];
1819 }
1820 };
1821
1822 private RunningAppProcessInfo(Parcel source) {
1823 readFromParcel(source);
1824 }
1825 }
1826
1827 /**
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07001828 * Returns a list of application processes installed on external media
1829 * that are running on the device.
1830 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001831 * <p><b>Note: this method is only intended for debugging or building
1832 * a user-facing process management UI.</b></p>
1833 *
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07001834 * @return Returns a list of ApplicationInfo records, or null if none
1835 * This list ordering is not specified.
1836 * @hide
1837 */
1838 public List<ApplicationInfo> getRunningExternalApplications() {
1839 try {
1840 return ActivityManagerNative.getDefault().getRunningExternalApplications();
1841 } catch (RemoteException e) {
1842 return null;
1843 }
1844 }
1845
1846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 * Returns a list of application processes that are running on the device.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001848 *
1849 * <p><b>Note: this method is only intended for debugging or building
1850 * a user-facing process management UI.</b></p>
1851 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 * @return Returns a list of RunningAppProcessInfo records, or null if there are no
1853 * running processes (it will not return an empty list). This list ordering is not
1854 * specified.
1855 */
1856 public List<RunningAppProcessInfo> getRunningAppProcesses() {
1857 try {
1858 return ActivityManagerNative.getDefault().getRunningAppProcesses();
1859 } catch (RemoteException e) {
1860 return null;
1861 }
1862 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001863
1864 /**
1865 * Return global memory state information for the calling process. This
1866 * does not fill in all fields of the {@link RunningAppProcessInfo}. The
1867 * only fields that will be filled in are
1868 * {@link RunningAppProcessInfo#pid},
1869 * {@link RunningAppProcessInfo#uid},
1870 * {@link RunningAppProcessInfo#lastTrimLevel},
1871 * {@link RunningAppProcessInfo#importance},
1872 * {@link RunningAppProcessInfo#lru}, and
1873 * {@link RunningAppProcessInfo#importanceReasonCode}.
1874 */
1875 static public void getMyMemoryState(RunningAppProcessInfo outState) {
1876 try {
1877 ActivityManagerNative.getDefault().getMyMemoryState(outState);
1878 } catch (RemoteException e) {
1879 }
1880 }
1881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07001883 * Return information about the memory usage of one or more processes.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001884 *
1885 * <p><b>Note: this method is only intended for debugging or building
1886 * a user-facing process management UI.</b></p>
1887 *
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07001888 * @param pids The pids of the processes whose memory usage is to be
1889 * retrieved.
1890 * @return Returns an array of memory information, one for each
1891 * requested pid.
1892 */
1893 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
1894 try {
1895 return ActivityManagerNative.getDefault().getProcessMemoryInfo(pids);
1896 } catch (RemoteException e) {
1897 return null;
1898 }
1899 }
1900
1901 /**
Dianne Hackborn03abb812010-01-04 18:43:19 -08001902 * @deprecated This is now just a wrapper for
1903 * {@link #killBackgroundProcesses(String)}; the previous behavior here
1904 * is no longer available to applications because it allows them to
1905 * break other applications by removing their alarms, stopping their
1906 * services, etc.
1907 */
1908 @Deprecated
1909 public void restartPackage(String packageName) {
1910 killBackgroundProcesses(packageName);
1911 }
1912
1913 /**
1914 * Have the system immediately kill all background processes associated
1915 * with the given package. This is the same as the kernel killing those
1916 * processes to reclaim memory; the system will take care of restarting
1917 * these processes in the future as needed.
1918 *
1919 * <p>You must hold the permission
1920 * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to
1921 * call this method.
1922 *
1923 * @param packageName The name of the package whose processes are to
1924 * be killed.
1925 */
1926 public void killBackgroundProcesses(String packageName) {
1927 try {
Dianne Hackborn1676c852012-09-10 14:52:30 -07001928 ActivityManagerNative.getDefault().killBackgroundProcesses(packageName,
1929 UserHandle.myUserId());
Dianne Hackborn03abb812010-01-04 18:43:19 -08001930 } catch (RemoteException e) {
1931 }
1932 }
1933
1934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 * Have the system perform a force stop of everything associated with
1936 * the given application package. All processes that share its uid
1937 * will be killed, all services it has running stopped, all activities
1938 * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}
1939 * broadcast will be sent, so that any of its registered alarms can
1940 * be stopped, notifications removed, etc.
1941 *
1942 * <p>You must hold the permission
Dianne Hackborn03abb812010-01-04 18:43:19 -08001943 * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 * call this method.
1945 *
1946 * @param packageName The name of the package to be stopped.
Dianne Hackborn03abb812010-01-04 18:43:19 -08001947 *
1948 * @hide This is not available to third party applications due to
1949 * it allowing them to break other applications by stopping their
1950 * services, removing their alarms, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 */
Dianne Hackborn03abb812010-01-04 18:43:19 -08001952 public void forceStopPackage(String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 try {
Dianne Hackborn1676c852012-09-10 14:52:30 -07001954 ActivityManagerNative.getDefault().forceStopPackage(packageName,
1955 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 } catch (RemoteException e) {
1957 }
1958 }
1959
1960 /**
1961 * Get the device configuration attributes.
1962 */
1963 public ConfigurationInfo getDeviceConfigurationInfo() {
1964 try {
1965 return ActivityManagerNative.getDefault().getDeviceConfigurationInfo();
1966 } catch (RemoteException e) {
1967 }
1968 return null;
1969 }
Kenny Root5ef44b72011-01-26 17:22:20 -08001970
1971 /**
1972 * Get the preferred density of icons for the launcher. This is used when
1973 * custom drawables are created (e.g., for shortcuts).
1974 *
1975 * @return density in terms of DPI
1976 */
1977 public int getLauncherLargeIconDensity() {
1978 final Resources res = mContext.getResources();
1979 final int density = res.getDisplayMetrics().densityDpi;
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08001980 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08001981
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08001982 if (sw < 600) {
1983 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08001984 return density;
1985 }
1986
1987 switch (density) {
1988 case DisplayMetrics.DENSITY_LOW:
1989 return DisplayMetrics.DENSITY_MEDIUM;
1990 case DisplayMetrics.DENSITY_MEDIUM:
1991 return DisplayMetrics.DENSITY_HIGH;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07001992 case DisplayMetrics.DENSITY_TV:
1993 return DisplayMetrics.DENSITY_XHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08001994 case DisplayMetrics.DENSITY_HIGH:
1995 return DisplayMetrics.DENSITY_XHIGH;
1996 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08001997 return DisplayMetrics.DENSITY_XXHIGH;
1998 case DisplayMetrics.DENSITY_XXHIGH:
1999 return DisplayMetrics.DENSITY_XHIGH * 2;
Kenny Root5ef44b72011-01-26 17:22:20 -08002000 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002001 // The density is some abnormal value. Return some other
2002 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002003 return (int)((density*1.5f)+.5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08002004 }
2005 }
2006
2007 /**
2008 * Get the preferred launcher icon size. This is used when custom drawables
2009 * are created (e.g., for shortcuts).
2010 *
2011 * @return dimensions of square icons in terms of pixels
2012 */
2013 public int getLauncherLargeIconSize() {
2014 final Resources res = mContext.getResources();
2015 final int size = res.getDimensionPixelSize(android.R.dimen.app_icon_size);
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002016 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08002017
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002018 if (sw < 600) {
2019 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08002020 return size;
2021 }
2022
2023 final int density = res.getDisplayMetrics().densityDpi;
2024
2025 switch (density) {
2026 case DisplayMetrics.DENSITY_LOW:
2027 return (size * DisplayMetrics.DENSITY_MEDIUM) / DisplayMetrics.DENSITY_LOW;
2028 case DisplayMetrics.DENSITY_MEDIUM:
2029 return (size * DisplayMetrics.DENSITY_HIGH) / DisplayMetrics.DENSITY_MEDIUM;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002030 case DisplayMetrics.DENSITY_TV:
2031 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08002032 case DisplayMetrics.DENSITY_HIGH:
2033 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
2034 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002035 return (size * DisplayMetrics.DENSITY_XXHIGH) / DisplayMetrics.DENSITY_XHIGH;
2036 case DisplayMetrics.DENSITY_XXHIGH:
2037 return (size * DisplayMetrics.DENSITY_XHIGH*2) / DisplayMetrics.DENSITY_XXHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08002038 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002039 // The density is some abnormal value. Return some other
2040 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002041 return (int)((size*1.5f) + .5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08002042 }
2043 }
2044
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08002045 /**
2046 * Returns "true" if the user interface is currently being messed with
2047 * by a monkey.
2048 */
2049 public static boolean isUserAMonkey() {
2050 try {
2051 return ActivityManagerNative.getDefault().isUserAMonkey();
2052 } catch (RemoteException e) {
2053 }
2054 return false;
2055 }
Brett Chabot3b4fcbc2011-01-09 13:41:02 -08002056
2057 /**
2058 * Returns "true" if device is running in a test harness.
2059 */
2060 public static boolean isRunningInTestHarness() {
2061 return SystemProperties.getBoolean("ro.test_harness", false);
2062 }
Peter Visontay8d224ca2011-02-18 16:39:19 +00002063
2064 /**
2065 * Returns the launch count of each installed package.
2066 *
2067 * @hide
2068 */
2069 public Map<String, Integer> getAllPackageLaunchCounts() {
2070 try {
2071 IUsageStats usageStatsService = IUsageStats.Stub.asInterface(
2072 ServiceManager.getService("usagestats"));
2073 if (usageStatsService == null) {
2074 return new HashMap<String, Integer>();
2075 }
2076
Peter Visontaybfcda392011-03-02 18:53:37 +00002077 PkgUsageStats[] allPkgUsageStats = usageStatsService.getAllPkgUsageStats();
2078 if (allPkgUsageStats == null) {
2079 return new HashMap<String, Integer>();
2080 }
2081
Peter Visontay8d224ca2011-02-18 16:39:19 +00002082 Map<String, Integer> launchCounts = new HashMap<String, Integer>();
Peter Visontaybfcda392011-03-02 18:53:37 +00002083 for (PkgUsageStats pkgUsageStats : allPkgUsageStats) {
Peter Visontay8d224ca2011-02-18 16:39:19 +00002084 launchCounts.put(pkgUsageStats.packageName, pkgUsageStats.launchCount);
2085 }
2086
2087 return launchCounts;
2088 } catch (RemoteException e) {
2089 Log.w(TAG, "Could not query launch counts", e);
2090 return new HashMap<String, Integer>();
2091 }
2092 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002093
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002094 /** @hide */
2095 public static int checkComponentPermission(String permission, int uid,
2096 int owningUid, boolean exported) {
2097 // Root, system server get to do everything.
2098 if (uid == 0 || uid == Process.SYSTEM_UID) {
2099 return PackageManager.PERMISSION_GRANTED;
2100 }
2101 // Isolated processes don't get any permissions.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002102 if (UserHandle.isIsolated(uid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002103 return PackageManager.PERMISSION_DENIED;
2104 }
2105 // If there is a uid that owns whatever is being accessed, it has
2106 // blanket access to it regardless of the permissions it requires.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002107 if (owningUid >= 0 && UserHandle.isSameApp(uid, owningUid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002108 return PackageManager.PERMISSION_GRANTED;
2109 }
2110 // If the target is not exported, then nobody else can get to it.
2111 if (!exported) {
2112 Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid);
2113 return PackageManager.PERMISSION_DENIED;
2114 }
2115 if (permission == null) {
2116 return PackageManager.PERMISSION_GRANTED;
2117 }
2118 try {
2119 return AppGlobals.getPackageManager()
2120 .checkUidPermission(permission, uid);
2121 } catch (RemoteException e) {
2122 // Should never happen, but if it does... deny!
2123 Slog.e(TAG, "PackageManager is dead?!?", e);
2124 }
2125 return PackageManager.PERMISSION_DENIED;
2126 }
2127
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002128 /** @hide */
2129 public static int checkUidPermission(String permission, int uid) {
2130 try {
2131 return AppGlobals.getPackageManager()
2132 .checkUidPermission(permission, uid);
2133 } catch (RemoteException e) {
2134 // Should never happen, but if it does... deny!
2135 Slog.e(TAG, "PackageManager is dead?!?", e);
2136 }
2137 return PackageManager.PERMISSION_DENIED;
2138 }
2139
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002140 /**
2141 * @hide
2142 * Helper for dealing with incoming user arguments to system service calls.
2143 * Takes care of checking permissions and converting USER_CURRENT to the
2144 * actual current user.
2145 *
2146 * @param callingPid The pid of the incoming call, as per Binder.getCallingPid().
2147 * @param callingUid The uid of the incoming call, as per Binder.getCallingUid().
2148 * @param userId The user id argument supplied by the caller -- this is the user
2149 * they want to run as.
2150 * @param allowAll If true, we will allow USER_ALL. This means you must be prepared
2151 * to get a USER_ALL returned and deal with it correctly. If false,
2152 * an exception will be thrown if USER_ALL is supplied.
2153 * @param requireFull If true, the caller must hold
2154 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} to be able to run as a
2155 * different user than their current process; otherwise they must hold
2156 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS}.
2157 * @param name Optional textual name of the incoming call; only for generating error messages.
2158 * @param callerPackage Optional package name of caller; only for error messages.
2159 *
2160 * @return Returns the user ID that the call should run as. Will always be a concrete
2161 * user number, unless <var>allowAll</var> is true in which case it could also be
2162 * USER_ALL.
2163 */
Dianne Hackborn41203752012-08-31 14:05:51 -07002164 public static int handleIncomingUser(int callingPid, int callingUid, int userId,
2165 boolean allowAll, boolean requireFull, String name, String callerPackage) {
2166 if (UserHandle.getUserId(callingUid) == userId) {
2167 return userId;
2168 }
2169 try {
2170 return ActivityManagerNative.getDefault().handleIncomingUser(callingPid,
2171 callingUid, userId, allowAll, requireFull, name, callerPackage);
2172 } catch (RemoteException e) {
2173 throw new SecurityException("Failed calling activity manager", e);
2174 }
2175 }
2176
2177 /** @hide */
2178 public static int getCurrentUser() {
2179 UserInfo ui;
2180 try {
2181 ui = ActivityManagerNative.getDefault().getCurrentUser();
2182 return ui != null ? ui.id : 0;
2183 } catch (RemoteException e) {
2184 return 0;
2185 }
2186 }
2187
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002188 /**
Mark Brophy9fc03302011-07-01 16:56:24 +01002189 * Returns the usage statistics of each installed package.
2190 *
2191 * @hide
2192 */
2193 public PkgUsageStats[] getAllPackageUsageStats() {
2194 try {
2195 IUsageStats usageStatsService = IUsageStats.Stub.asInterface(
2196 ServiceManager.getService("usagestats"));
2197 if (usageStatsService != null) {
2198 return usageStatsService.getAllPkgUsageStats();
2199 }
2200 } catch (RemoteException e) {
2201 Log.w(TAG, "Could not query usage stats", e);
2202 }
2203 return new PkgUsageStats[0];
2204 }
2205
2206 /**
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002207 * @param userid the user's id. Zero indicates the default user
2208 * @hide
2209 */
2210 public boolean switchUser(int userid) {
2211 try {
2212 return ActivityManagerNative.getDefault().switchUser(userid);
2213 } catch (RemoteException e) {
2214 return false;
2215 }
2216 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07002217
2218 /**
2219 * Return whether the given user is actively running. This means that
2220 * the user is in the "started" state, not "stopped" -- it is currently
2221 * allowed to run code through scheduled alarms, receiving broadcasts,
2222 * etc. A started user may be either the current foreground user or a
2223 * background user; the result here does not distinguish between the two.
2224 * @param userid the user's id. Zero indicates the default user.
2225 * @hide
2226 */
2227 public boolean isUserRunning(int userid) {
2228 try {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002229 return ActivityManagerNative.getDefault().isUserRunning(userid, false);
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07002230 } catch (RemoteException e) {
2231 return false;
2232 }
2233 }
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002234
2235 /**
2236 * Perform a system dump of various state associated with the given application
2237 * package name. This call blocks while the dump is being performed, so should
2238 * not be done on a UI thread. The data will be written to the given file
2239 * descriptor as text. An application must hold the
2240 * {@link android.Manifest.permission#DUMP} permission to make this call.
2241 * @param fd The file descriptor that the dump should be written to.
2242 * @param packageName The name of the package that is to be dumped.
2243 */
2244 public void dumpPackageState(FileDescriptor fd, String packageName) {
2245 dumpPackageStateStatic(fd, packageName);
2246 }
2247
2248 /**
2249 * @hide
2250 */
2251 public static void dumpPackageStateStatic(FileDescriptor fd, String packageName) {
2252 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07002253 PrintWriter pw = new FastPrintWriter(fout);
2254 dumpService(pw, fd, Context.ACTIVITY_SERVICE, new String[] { "package", packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002255 pw.println();
Dianne Hackborn8c841092013-06-24 13:46:13 -07002256 dumpService(pw, fd, "procstats", new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002257 pw.println();
Dianne Hackborn904a8572013-06-28 18:12:31 -07002258 dumpService(pw, fd, "usagestats", new String[] { "--packages", packageName });
2259 pw.println();
Dianne Hackborn8c841092013-06-24 13:46:13 -07002260 dumpService(pw, fd, "package", new String[] { packageName });
2261 pw.println();
2262 dumpService(pw, fd, BatteryStats.SERVICE_NAME, new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002263 pw.flush();
2264 }
2265
2266 private static void dumpService(PrintWriter pw, FileDescriptor fd, String name, String[] args) {
2267 pw.print("DUMP OF SERVICE "); pw.print(name); pw.println(":");
2268 IBinder service = ServiceManager.checkService(name);
2269 if (service == null) {
2270 pw.println(" (Service not found)");
2271 return;
2272 }
2273 TransferPipe tp = null;
2274 try {
2275 pw.flush();
2276 tp = new TransferPipe();
2277 tp.setBufferPrefix(" ");
2278 service.dump(tp.getWriteFd().getFileDescriptor(), args);
2279 tp.go(fd);
2280 } catch (Throwable e) {
2281 if (tp != null) {
2282 tp.kill();
2283 }
2284 pw.println("Failure dumping service:");
2285 e.printStackTrace(pw);
2286 }
2287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288}