blob: 7ca345993c232cff64d7457b6bc9a18df181b320 [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 Hackborn91268cf2013-06-13 19:06:50 -070019import android.os.BatteryStats;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070020import android.os.IBinder;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070021import com.android.internal.app.IUsageStats;
Dianne Hackborn8a0de582013-08-07 15:22:07 -070022import com.android.internal.app.ProcessStats;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070023import 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;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.ComponentName;
28import android.content.Context;
29import android.content.Intent;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -070030import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.pm.ConfigurationInfo;
32import android.content.pm.IPackageDataObserver;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070033import android.content.pm.PackageManager;
Dianne Hackborn41203752012-08-31 14:05:51 -070034import android.content.pm.UserInfo;
Kenny Root5ef44b72011-01-26 17:22:20 -080035import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.graphics.Bitmap;
Craig Mautner967212c2013-04-13 21:10:58 -070037import android.graphics.Rect;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -070038import android.os.Bundle;
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -070039import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.Handler;
41import android.os.Parcel;
42import android.os.Parcelable;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070043import android.os.Process;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070044import android.os.RemoteException;
Peter Visontay8d224ca2011-02-18 16:39:19 +000045import android.os.ServiceManager;
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -070046import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070047import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.text.TextUtils;
Kenny Root5ef44b72011-01-26 17:22:20 -080049import android.util.DisplayMetrics;
Peter Visontay8d224ca2011-02-18 16:39:19 +000050import android.util.Log;
Dianne Hackbornb3756322011-08-12 13:58:13 -070051import android.util.Slog;
Kenny Root5ef44b72011-01-26 17:22:20 -080052
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070053import java.io.FileDescriptor;
54import java.io.FileOutputStream;
55import java.io.PrintWriter;
Peter Visontay8d224ca2011-02-18 16:39:19 +000056import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import java.util.List;
Peter Visontay8d224ca2011-02-18 16:39:19 +000058import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60/**
61 * Interact with the overall activities running in the system.
62 */
63public class ActivityManager {
64 private static String TAG = "ActivityManager";
Joe Onorato43a17652011-04-06 19:22:23 -070065 private static boolean localLOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67 private final Context mContext;
68 private final Handler mHandler;
69
Dianne Hackborna4972e92012-03-14 10:38:05 -070070 /**
Scott Maincc2195b2013-10-16 13:57:46 -070071 * <a href="{@docRoot}guide/topics/manifest/meta-data-element.html">{@code
72 * &lt;meta-data>}</a> name for a 'home' Activity that declares a package that is to be
Christopher Tatebd413f62013-09-18 18:31:59 -070073 * uninstalled in lieu of the declaring one. The package named here must be
Scott Maincc2195b2013-10-16 13:57:46 -070074 * signed with the same certificate as the one declaring the {@code &lt;meta-data>}.
Christopher Tatebd413f62013-09-18 18:31:59 -070075 */
76 public static final String META_HOME_ALTERNATE = "android.app.home.alternate";
77
78 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -070079 * Result for IActivityManager.startActivity: an error where the
80 * start had to be canceled.
81 * @hide
82 */
83 public static final int START_CANCELED = -6;
84
85 /**
86 * Result for IActivityManager.startActivity: an error where the
87 * thing being started is not an activity.
88 * @hide
89 */
90 public static final int START_NOT_ACTIVITY = -5;
91
92 /**
93 * Result for IActivityManager.startActivity: an error where the
94 * caller does not have permission to start the activity.
95 * @hide
96 */
97 public static final int START_PERMISSION_DENIED = -4;
98
99 /**
100 * Result for IActivityManager.startActivity: an error where the
101 * caller has requested both to forward a result and to receive
102 * a result.
103 * @hide
104 */
105 public static final int START_FORWARD_AND_REQUEST_CONFLICT = -3;
106
107 /**
108 * Result for IActivityManager.startActivity: an error where the
109 * requested class is not found.
110 * @hide
111 */
112 public static final int START_CLASS_NOT_FOUND = -2;
113
114 /**
115 * Result for IActivityManager.startActivity: an error where the
116 * given Intent could not be resolved to an activity.
117 * @hide
118 */
119 public static final int START_INTENT_NOT_RESOLVED = -1;
120
121 /**
122 * Result for IActivityManaqer.startActivity: the activity was started
123 * successfully as normal.
124 * @hide
125 */
126 public static final int START_SUCCESS = 0;
127
128 /**
129 * Result for IActivityManaqer.startActivity: the caller asked that the Intent not
130 * be executed if it is the recipient, and that is indeed the case.
131 * @hide
132 */
133 public static final int START_RETURN_INTENT_TO_CALLER = 1;
134
135 /**
136 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
137 * a task was simply brought to the foreground.
138 * @hide
139 */
140 public static final int START_TASK_TO_FRONT = 2;
141
142 /**
143 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
144 * the given Intent was given to the existing top activity.
145 * @hide
146 */
147 public static final int START_DELIVERED_TO_TOP = 3;
148
149 /**
150 * Result for IActivityManaqer.startActivity: request was canceled because
151 * app switches are temporarily canceled to ensure the user's last request
152 * (such as pressing home) is performed.
153 * @hide
154 */
155 public static final int START_SWITCHES_CANCELED = 4;
156
157 /**
158 * Flag for IActivityManaqer.startActivity: do special start mode where
159 * a new activity is launched only if it is needed.
160 * @hide
161 */
162 public static final int START_FLAG_ONLY_IF_NEEDED = 1<<0;
163
164 /**
165 * Flag for IActivityManaqer.startActivity: launch the app for
166 * debugging.
167 * @hide
168 */
169 public static final int START_FLAG_DEBUG = 1<<1;
170
171 /**
172 * Flag for IActivityManaqer.startActivity: launch the app for
173 * OpenGL tracing.
174 * @hide
175 */
176 public static final int START_FLAG_OPENGL_TRACES = 1<<2;
177
178 /**
179 * Flag for IActivityManaqer.startActivity: if the app is being
180 * launched for profiling, automatically stop the profiler once done.
181 * @hide
182 */
183 public static final int START_FLAG_AUTO_STOP_PROFILER = 1<<3;
184
185 /**
186 * Result for IActivityManaqer.broadcastIntent: success!
187 * @hide
188 */
189 public static final int BROADCAST_SUCCESS = 0;
190
191 /**
192 * Result for IActivityManaqer.broadcastIntent: attempt to broadcast
193 * a sticky intent without appropriate permission.
194 * @hide
195 */
196 public static final int BROADCAST_STICKY_CANT_HAVE_PERMISSION = -1;
197
198 /**
199 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
200 * for a sendBroadcast operation.
201 * @hide
202 */
203 public static final int INTENT_SENDER_BROADCAST = 1;
204
205 /**
206 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
207 * for a startActivity operation.
208 * @hide
209 */
210 public static final int INTENT_SENDER_ACTIVITY = 2;
211
212 /**
213 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
214 * for an activity result operation.
215 * @hide
216 */
217 public static final int INTENT_SENDER_ACTIVITY_RESULT = 3;
218
219 /**
220 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
221 * for a startService operation.
222 * @hide
223 */
224 public static final int INTENT_SENDER_SERVICE = 4;
225
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700226 /** @hide User operation call: success! */
227 public static final int USER_OP_SUCCESS = 0;
228
229 /** @hide User operation call: given user id is not known. */
230 public static final int USER_OP_UNKNOWN_USER = -1;
231
232 /** @hide User operation call: given user id is the current user, can't be stopped. */
233 public static final int USER_OP_IS_CURRENT = -2;
234
Dianne Hackborna413dc02013-07-12 12:02:55 -0700235 /** @hide Process is a persistent system process. */
236 public static final int PROCESS_STATE_PERSISTENT = 0;
237
238 /** @hide Process is a persistent system process and is doing UI. */
239 public static final int PROCESS_STATE_PERSISTENT_UI = 1;
240
Dianne Hackbornc8230512013-07-13 21:32:12 -0700241 /** @hide Process is hosting the current top activities. Note that this covers
242 * all activities that are visible to the user. */
Dianne Hackborna413dc02013-07-12 12:02:55 -0700243 public static final int PROCESS_STATE_TOP = 2;
244
245 /** @hide Process is important to the user, and something they are aware of. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700246 public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 3;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700247
248 /** @hide Process is important to the user, but not something they are aware of. */
249 public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 4;
250
Dianne Hackborna413dc02013-07-12 12:02:55 -0700251 /** @hide Process is in the background running a backup/restore operation. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700252 public static final int PROCESS_STATE_BACKUP = 5;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700253
Dianne Hackbornc8230512013-07-13 21:32:12 -0700254 /** @hide Process is in the background, but it can't restore its state so we want
255 * to try to avoid killing it. */
256 public static final int PROCESS_STATE_HEAVY_WEIGHT = 6;
257
258 /** @hide Process is in the background running a service. Unlike oom_adj, this level
259 * is used for both the normal running in background state and the executing
260 * operations state. */
Dianne Hackborna413dc02013-07-12 12:02:55 -0700261 public static final int PROCESS_STATE_SERVICE = 7;
262
Dianne Hackbornc8230512013-07-13 21:32:12 -0700263 /** @hide Process is in the background running a receiver. Note that from the
264 * perspective of oom_adj receivers run at a higher foreground level, but for our
265 * prioritization here that is not necessary and putting them below services means
266 * many fewer changes in some process states as they receive broadcasts. */
267 public static final int PROCESS_STATE_RECEIVER = 8;
268
Dianne Hackborna413dc02013-07-12 12:02:55 -0700269 /** @hide Process is in the background but hosts the home activity. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700270 public static final int PROCESS_STATE_HOME = 9;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700271
272 /** @hide Process is in the background but hosts the last shown activity. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700273 public static final int PROCESS_STATE_LAST_ACTIVITY = 10;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700274
Dianne Hackbornc8230512013-07-13 21:32:12 -0700275 /** @hide Process is being cached for later use and contains activities. */
276 public static final int PROCESS_STATE_CACHED_ACTIVITY = 11;
277
278 /** @hide Process is being cached for later use and is a client of another cached
279 * process that contains activities. */
280 public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 12;
281
282 /** @hide Process is being cached for later use and is empty. */
283 public static final int PROCESS_STATE_CACHED_EMPTY = 13;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /*package*/ ActivityManager(Context context, Handler handler) {
286 mContext = context;
287 mHandler = handler;
288 }
289
290 /**
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700291 * Screen compatibility mode: the application most always run in
292 * compatibility mode.
293 * @hide
294 */
295 public static final int COMPAT_MODE_ALWAYS = -1;
296
297 /**
298 * Screen compatibility mode: the application can never run in
299 * compatibility mode.
300 * @hide
301 */
302 public static final int COMPAT_MODE_NEVER = -2;
303
304 /**
305 * Screen compatibility mode: unknown.
306 * @hide
307 */
308 public static final int COMPAT_MODE_UNKNOWN = -3;
309
310 /**
311 * Screen compatibility mode: the application currently has compatibility
312 * mode disabled.
313 * @hide
314 */
315 public static final int COMPAT_MODE_DISABLED = 0;
316
317 /**
318 * Screen compatibility mode: the application currently has compatibility
319 * mode enabled.
320 * @hide
321 */
322 public static final int COMPAT_MODE_ENABLED = 1;
323
324 /**
325 * Screen compatibility mode: request to toggle the application's
326 * compatibility mode.
327 * @hide
328 */
329 public static final int COMPAT_MODE_TOGGLE = 2;
330
331 /** @hide */
332 public int getFrontActivityScreenCompatMode() {
333 try {
334 return ActivityManagerNative.getDefault().getFrontActivityScreenCompatMode();
335 } catch (RemoteException e) {
336 // System dead, we will be dead too soon!
337 return 0;
338 }
339 }
340
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700341 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700342 public void setFrontActivityScreenCompatMode(int mode) {
343 try {
344 ActivityManagerNative.getDefault().setFrontActivityScreenCompatMode(mode);
345 } catch (RemoteException e) {
346 // System dead, we will be dead too soon!
347 }
348 }
349
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700350 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700351 public int getPackageScreenCompatMode(String packageName) {
352 try {
353 return ActivityManagerNative.getDefault().getPackageScreenCompatMode(packageName);
354 } catch (RemoteException e) {
355 // System dead, we will be dead too soon!
356 return 0;
357 }
358 }
359
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700360 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700361 public void setPackageScreenCompatMode(String packageName, int mode) {
362 try {
363 ActivityManagerNative.getDefault().setPackageScreenCompatMode(packageName, mode);
364 } catch (RemoteException e) {
365 // System dead, we will be dead too soon!
366 }
367 }
368
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700369 /** @hide */
370 public boolean getPackageAskScreenCompat(String packageName) {
371 try {
372 return ActivityManagerNative.getDefault().getPackageAskScreenCompat(packageName);
373 } catch (RemoteException e) {
374 // System dead, we will be dead too soon!
375 return false;
376 }
377 }
378
379 /** @hide */
380 public void setPackageAskScreenCompat(String packageName, boolean ask) {
381 try {
382 ActivityManagerNative.getDefault().setPackageAskScreenCompat(packageName, ask);
383 } catch (RemoteException e) {
384 // System dead, we will be dead too soon!
385 }
386 }
387
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700388 /**
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700389 * Return the approximate per-application memory class of the current
390 * device. This gives you an idea of how hard a memory limit you should
391 * impose on your application to let the overall system work best. The
392 * returned value is in megabytes; the baseline Android memory class is
393 * 16 (which happens to be the Java heap limit of those devices); some
394 * device with more memory may return 24 or even higher numbers.
395 */
396 public int getMemoryClass() {
397 return staticGetMemoryClass();
398 }
399
400 /** @hide */
401 static public int staticGetMemoryClass() {
402 // Really brain dead right now -- just take this from the configured
403 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn7ad33c82011-03-08 15:53:25 -0800404 String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", "");
Dianne Hackbornde398512011-01-18 18:45:21 -0800405 if (vmHeapSize != null && !"".equals(vmHeapSize)) {
406 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
407 }
408 return staticGetLargeMemoryClass();
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800409 }
410
411 /**
412 * Return the approximate per-application memory class of the current
413 * device when an application is running with a large heap. This is the
414 * space available for memory-intensive applications; most applications
415 * should not need this amount of memory, and should instead stay with the
416 * {@link #getMemoryClass()} limit. The returned value is in megabytes.
417 * This may be the same size as {@link #getMemoryClass()} on memory
418 * constrained devices, or it may be significantly larger on devices with
419 * a large amount of available RAM.
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800420 *
421 * <p>The is the size of the application's Dalvik heap if it has
422 * specified <code>android:largeHeap="true"</code> in its manifest.
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800423 */
424 public int getLargeMemoryClass() {
425 return staticGetLargeMemoryClass();
426 }
427
428 /** @hide */
429 static public int staticGetLargeMemoryClass() {
430 // Really brain dead right now -- just take this from the configured
431 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700432 String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
433 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
434 }
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700435
436 /**
437 * Returns true if this is a low-RAM device. Exactly whether a device is low-RAM
438 * is ultimately up to the device configuration, but currently it generally means
439 * something in the class of a 512MB device with about a 800x480 or less screen.
440 * This is mostly intended to be used by apps to determine whether they should turn
441 * off certain features that require more RAM.
442 */
443 public boolean isLowRamDevice() {
444 return isLowRamDeviceStatic();
445 }
446
447 /** @hide */
448 public static boolean isLowRamDeviceStatic() {
Christopher Tate2f61f912013-08-20 13:55:50 -0700449 return "true".equals(SystemProperties.get("ro.config.low_ram", "false"));
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700450 }
451
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700452 /**
Dianne Hackbornb3756322011-08-12 13:58:13 -0700453 * Used by persistent processes to determine if they are running on a
454 * higher-end device so should be okay using hardware drawing acceleration
455 * (which tends to consume a lot more RAM).
456 * @hide
457 */
Jeff Brown98365d72012-08-19 20:30:52 -0700458 static public boolean isHighEndGfx() {
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700459 return !isLowRamDeviceStatic() &&
460 !Resources.getSystem().getBoolean(com.android.internal.R.bool.config_avoidGfxAccel);
Dianne Hackborn49d228b32011-08-24 17:37:31 -0700461 }
462
463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 * Information you can retrieve about tasks that the user has most recently
465 * started or visited.
466 */
467 public static class RecentTaskInfo implements Parcelable {
468 /**
469 * If this task is currently running, this is the identifier for it.
470 * If it is not running, this will be -1.
471 */
472 public int id;
473
474 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -0800475 * The true identifier of this task, valid even if it is not running.
476 */
477 public int persistentId;
478
479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 * The original Intent used to launch the task. You can use this
481 * Intent to re-launch the task (if it is no longer running) or bring
482 * the current task to the front.
483 */
484 public Intent baseIntent;
485
486 /**
487 * If this task was started from an alias, this is the actual
488 * activity component that was initially started; the component of
489 * the baseIntent in this case is the name of the actual activity
490 * implementation that the alias referred to. Otherwise, this is null.
491 */
492 public ComponentName origActivity;
Dianne Hackbornd2835932010-12-13 16:28:46 -0800493
494 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -0800495 * Description of the task's last state.
496 */
497 public CharSequence description;
Craig Mautner6d90fed2013-05-22 15:03:10 -0700498
499 /**
500 * The id of the ActivityStack this Task was on most recently.
Craig Mautner27030522013-08-26 12:25:36 -0700501 * @hide
Craig Mautner6d90fed2013-05-22 15:03:10 -0700502 */
503 public int stackId;
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 public RecentTaskInfo() {
506 }
507
Craig Mautner6d90fed2013-05-22 15:03:10 -0700508 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public int describeContents() {
510 return 0;
511 }
512
Craig Mautner6d90fed2013-05-22 15:03:10 -0700513 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 public void writeToParcel(Parcel dest, int flags) {
515 dest.writeInt(id);
Dianne Hackbornd94df452011-02-16 18:53:31 -0800516 dest.writeInt(persistentId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 if (baseIntent != null) {
518 dest.writeInt(1);
519 baseIntent.writeToParcel(dest, 0);
520 } else {
521 dest.writeInt(0);
522 }
523 ComponentName.writeToParcel(origActivity, dest);
Dianne Hackbornd2835932010-12-13 16:28:46 -0800524 TextUtils.writeToParcel(description, dest,
525 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
Craig Mautner6d90fed2013-05-22 15:03:10 -0700526 dest.writeInt(stackId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528
529 public void readFromParcel(Parcel source) {
530 id = source.readInt();
Dianne Hackbornd94df452011-02-16 18:53:31 -0800531 persistentId = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 if (source.readInt() != 0) {
533 baseIntent = Intent.CREATOR.createFromParcel(source);
534 } else {
535 baseIntent = null;
536 }
537 origActivity = ComponentName.readFromParcel(source);
Dianne Hackbornd2835932010-12-13 16:28:46 -0800538 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
Craig Mautner6d90fed2013-05-22 15:03:10 -0700539 stackId = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
Craig Mautner6d90fed2013-05-22 15:03:10 -0700541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 public static final Creator<RecentTaskInfo> CREATOR
543 = new Creator<RecentTaskInfo>() {
544 public RecentTaskInfo createFromParcel(Parcel source) {
545 return new RecentTaskInfo(source);
546 }
547 public RecentTaskInfo[] newArray(int size) {
548 return new RecentTaskInfo[size];
549 }
550 };
551
552 private RecentTaskInfo(Parcel source) {
553 readFromParcel(source);
554 }
555 }
556
557 /**
558 * Flag for use with {@link #getRecentTasks}: return all tasks, even those
559 * that have set their
560 * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag.
561 */
562 public static final int RECENT_WITH_EXCLUDED = 0x0001;
563
564 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -0800565 * Provides a list that does not contain any
Dianne Hackborn53d9264d2010-04-13 12:49:14 -0700566 * recent tasks that currently are not available to the user.
567 */
568 public static final int RECENT_IGNORE_UNAVAILABLE = 0x0002;
Dianne Hackbornd2835932010-12-13 16:28:46 -0800569
570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 * Return a list of the tasks that the user has recently launched, with
572 * the most recent being first and older ones after in order.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -0700573 *
574 * <p><b>Note: this method is only intended for debugging and presenting
575 * task management user interfaces</b>. This should never be used for
576 * core logic in an application, such as deciding between different
577 * behaviors based on the information found here. Such uses are
578 * <em>not</em> supported, and will likely break in the future. For
579 * example, if multiple applications can be actively running at the
580 * same time, assumptions made about the meaning of the data here for
581 * purposes of control flow will be incorrect.</p>
582 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 * @param maxNum The maximum number of entries to return in the list. The
584 * actual number returned may be smaller, depending on how many tasks the
585 * user has started and the maximum number the system can remember.
Dianne Hackbornd2835932010-12-13 16:28:46 -0800586 * @param flags Information about what to return. May be any combination
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800587 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 *
589 * @return Returns a list of RecentTaskInfo records describing each of
590 * the recent tasks.
591 *
592 * @throws SecurityException Throws SecurityException if the caller does
593 * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
594 */
595 public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags)
596 throws SecurityException {
597 try {
598 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700599 flags, UserHandle.myUserId());
Amith Yamasani82644082012-08-03 13:09:11 -0700600 } catch (RemoteException e) {
601 // System dead, we will be dead too soon!
602 return null;
603 }
604 }
605
606 /**
607 * Same as {@link #getRecentTasks(int, int)} but returns the recent tasks for a
608 * specific user. It requires holding
609 * the {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission.
610 * @param maxNum The maximum number of entries to return in the list. The
611 * actual number returned may be smaller, depending on how many tasks the
612 * user has started and the maximum number the system can remember.
613 * @param flags Information about what to return. May be any combination
614 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
615 *
616 * @return Returns a list of RecentTaskInfo records describing each of
617 * the recent tasks.
618 *
619 * @throws SecurityException Throws SecurityException if the caller does
620 * not hold the {@link android.Manifest.permission#GET_TASKS} or the
621 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permissions.
622 * @hide
623 */
624 public List<RecentTaskInfo> getRecentTasksForUser(int maxNum, int flags, int userId)
625 throws SecurityException {
626 try {
627 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
628 flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 } catch (RemoteException e) {
630 // System dead, we will be dead too soon!
631 return null;
632 }
633 }
634
635 /**
636 * Information you can retrieve about a particular task that is currently
637 * "running" in the system. Note that a running task does not mean the
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700638 * given task actually has a process it is actively running in; it simply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 * means that the user has gone to it and never closed it, but currently
640 * the system may have killed its process and is only holding on to its
641 * last state in order to restart it when the user returns.
642 */
643 public static class RunningTaskInfo implements Parcelable {
644 /**
645 * A unique identifier for this task.
646 */
647 public int id;
648
649 /**
650 * The component launched as the first activity in the task. This can
651 * be considered the "application" of this task.
652 */
653 public ComponentName baseActivity;
654
655 /**
656 * The activity component at the top of the history stack of the task.
657 * This is what the user is currently doing.
658 */
659 public ComponentName topActivity;
660
661 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800662 * Thumbnail representation of the task's current state. Currently
663 * always null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 */
665 public Bitmap thumbnail;
666
667 /**
668 * Description of the task's current state.
669 */
670 public CharSequence description;
671
672 /**
673 * Number of activities in this task.
674 */
675 public int numActivities;
676
677 /**
678 * Number of activities that are currently running (not stopped
679 * and persisted) in this task.
680 */
681 public int numRunning;
682
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700683 /**
684 * Last time task was run. For sorting.
685 * @hide
686 */
687 public long lastActiveTime;
688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 public RunningTaskInfo() {
690 }
691
692 public int describeContents() {
693 return 0;
694 }
695
696 public void writeToParcel(Parcel dest, int flags) {
697 dest.writeInt(id);
698 ComponentName.writeToParcel(baseActivity, dest);
699 ComponentName.writeToParcel(topActivity, dest);
700 if (thumbnail != null) {
701 dest.writeInt(1);
702 thumbnail.writeToParcel(dest, 0);
703 } else {
704 dest.writeInt(0);
705 }
706 TextUtils.writeToParcel(description, dest,
707 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
708 dest.writeInt(numActivities);
709 dest.writeInt(numRunning);
710 }
711
712 public void readFromParcel(Parcel source) {
713 id = source.readInt();
714 baseActivity = ComponentName.readFromParcel(source);
715 topActivity = ComponentName.readFromParcel(source);
716 if (source.readInt() != 0) {
717 thumbnail = Bitmap.CREATOR.createFromParcel(source);
718 } else {
719 thumbnail = null;
720 }
721 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
722 numActivities = source.readInt();
723 numRunning = source.readInt();
724 }
725
726 public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() {
727 public RunningTaskInfo createFromParcel(Parcel source) {
728 return new RunningTaskInfo(source);
729 }
730 public RunningTaskInfo[] newArray(int size) {
731 return new RunningTaskInfo[size];
732 }
733 };
734
735 private RunningTaskInfo(Parcel source) {
736 readFromParcel(source);
737 }
738 }
Dianne Hackbornd2835932010-12-13 16:28:46 -0800739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 /**
741 * Return a list of the tasks that are currently running, with
742 * the most recent being first and older ones after in order. Note that
743 * "running" does not mean any of the task's code is currently loaded or
744 * activity -- the task may have been frozen by the system, so that it
745 * can be restarted in its previous state when next brought to the
746 * foreground.
747 *
748 * @param maxNum The maximum number of entries to return in the list. The
749 * actual number returned may be smaller, depending on how many tasks the
750 * user has started.
Jim Miller0b2a6d02010-07-13 18:01:29 -0700751 *
752 * @param flags Optional flags
753 * @param receiver Optional receiver for delayed thumbnails
754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 * @return Returns a list of RunningTaskInfo records describing each of
756 * the running tasks.
757 *
Jim Miller0b2a6d02010-07-13 18:01:29 -0700758 * Some thumbnails may not be available at the time of this call. The optional
759 * receiver may be used to receive those thumbnails.
760 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 * @throws SecurityException Throws SecurityException if the caller does
762 * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
Jim Miller0b2a6d02010-07-13 18:01:29 -0700763 *
764 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 */
Jim Miller0b2a6d02010-07-13 18:01:29 -0700766 public List<RunningTaskInfo> getRunningTasks(int maxNum, int flags, IThumbnailReceiver receiver)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 throws SecurityException {
768 try {
Jim Miller0b2a6d02010-07-13 18:01:29 -0700769 return ActivityManagerNative.getDefault().getTasks(maxNum, flags, receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 } catch (RemoteException e) {
771 // System dead, we will be dead too soon!
772 return null;
773 }
774 }
Jim Miller0b2a6d02010-07-13 18:01:29 -0700775
776 /**
777 * Return a list of the tasks that are currently running, with
778 * the most recent being first and older ones after in order. Note that
779 * "running" does not mean any of the task's code is currently loaded or
780 * activity -- the task may have been frozen by the system, so that it
781 * can be restarted in its previous state when next brought to the
782 * foreground.
783 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -0700784 * <p><b>Note: this method is only intended for debugging and presenting
785 * task management user interfaces</b>. This should never be used for
786 * core logic in an application, such as deciding between different
787 * behaviors based on the information found here. Such uses are
788 * <em>not</em> supported, and will likely break in the future. For
789 * example, if multiple applications can be actively running at the
790 * same time, assumptions made about the meaning of the data here for
791 * purposes of control flow will be incorrect.</p>
792 *
Jim Miller0b2a6d02010-07-13 18:01:29 -0700793 * @param maxNum The maximum number of entries to return in the list. The
794 * actual number returned may be smaller, depending on how many tasks the
795 * user has started.
796 *
797 * @return Returns a list of RunningTaskInfo records describing each of
798 * the running tasks.
799 *
800 * @throws SecurityException Throws SecurityException if the caller does
801 * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
802 */
803 public List<RunningTaskInfo> getRunningTasks(int maxNum)
804 throws SecurityException {
805 return getRunningTasks(maxNum, 0, null);
806 }
807
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700808 /**
809 * Remove some end of a task's activity stack that is not part of
810 * the main application. The selected activities will be finished, so
811 * they are no longer part of the main task.
812 *
813 * @param taskId The identifier of the task.
814 * @param subTaskIndex The number of the sub-task; this corresponds
815 * to the index of the thumbnail returned by {@link #getTaskThumbnails(int)}.
816 * @return Returns true if the sub-task was found and was removed.
817 *
818 * @hide
819 */
820 public boolean removeSubTask(int taskId, int subTaskIndex)
821 throws SecurityException {
822 try {
823 return ActivityManagerNative.getDefault().removeSubTask(taskId, subTaskIndex);
824 } catch (RemoteException e) {
825 // System dead, we will be dead too soon!
826 return false;
827 }
828 }
829
830 /**
831 * If set, the process of the root activity of the task will be killed
832 * as part of removing the task.
833 * @hide
834 */
835 public static final int REMOVE_TASK_KILL_PROCESS = 0x0001;
836
837 /**
838 * Completely remove the given task.
839 *
840 * @param taskId Identifier of the task to be removed.
841 * @param flags Additional operational flags. May be 0 or
842 * {@link #REMOVE_TASK_KILL_PROCESS}.
843 * @return Returns true if the given task was found and removed.
844 *
845 * @hide
846 */
847 public boolean removeTask(int taskId, int flags)
848 throws SecurityException {
849 try {
850 return ActivityManagerNative.getDefault().removeTask(taskId, flags);
851 } catch (RemoteException e) {
852 // System dead, we will be dead too soon!
853 return false;
854 }
855 }
856
Dianne Hackbornd94df452011-02-16 18:53:31 -0800857 /** @hide */
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700858 public static class TaskThumbnails implements Parcelable {
859 public Bitmap mainThumbnail;
860
861 public int numSubThumbbails;
862
863 /** @hide */
864 public IThumbnailRetriever retriever;
865
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700866 public TaskThumbnails() {
867 }
868
869 public Bitmap getSubThumbnail(int index) {
870 try {
871 return retriever.getThumbnail(index);
872 } catch (RemoteException e) {
873 return null;
874 }
875 }
876
877 public int describeContents() {
878 return 0;
879 }
880
881 public void writeToParcel(Parcel dest, int flags) {
882 if (mainThumbnail != null) {
883 dest.writeInt(1);
884 mainThumbnail.writeToParcel(dest, 0);
885 } else {
886 dest.writeInt(0);
887 }
888 dest.writeInt(numSubThumbbails);
889 dest.writeStrongInterface(retriever);
890 }
891
892 public void readFromParcel(Parcel source) {
893 if (source.readInt() != 0) {
894 mainThumbnail = Bitmap.CREATOR.createFromParcel(source);
895 } else {
896 mainThumbnail = null;
897 }
898 numSubThumbbails = source.readInt();
899 retriever = IThumbnailRetriever.Stub.asInterface(source.readStrongBinder());
900 }
901
902 public static final Creator<TaskThumbnails> CREATOR = new Creator<TaskThumbnails>() {
903 public TaskThumbnails createFromParcel(Parcel source) {
904 return new TaskThumbnails(source);
905 }
906 public TaskThumbnails[] newArray(int size) {
907 return new TaskThumbnails[size];
908 }
909 };
910
911 private TaskThumbnails(Parcel source) {
912 readFromParcel(source);
913 }
914 }
915
916 /** @hide */
917 public TaskThumbnails getTaskThumbnails(int id) throws SecurityException {
Dianne Hackbornd94df452011-02-16 18:53:31 -0800918 try {
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700919 return ActivityManagerNative.getDefault().getTaskThumbnails(id);
Dianne Hackbornd94df452011-02-16 18:53:31 -0800920 } catch (RemoteException e) {
921 // System dead, we will be dead too soon!
922 return null;
923 }
924 }
Dianne Hackborn15491c62012-09-19 10:59:14 -0700925
926 /** @hide */
927 public Bitmap getTaskTopThumbnail(int id) throws SecurityException {
928 try {
929 return ActivityManagerNative.getDefault().getTaskTopThumbnail(id);
930 } catch (RemoteException e) {
931 // System dead, we will be dead too soon!
932 return null;
933 }
934 }
935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800937 * Flag for {@link #moveTaskToFront(int, int)}: also move the "home"
938 * activity along with the task, so it is positioned immediately behind
939 * the task.
940 */
941 public static final int MOVE_TASK_WITH_HOME = 0x00000001;
942
943 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -0800944 * Flag for {@link #moveTaskToFront(int, int)}: don't count this as a
945 * user-instigated action, so the current activity will not receive a
946 * hint that the user is leaving.
947 */
948 public static final int MOVE_TASK_NO_USER_ACTION = 0x00000002;
949
950 /**
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700951 * Equivalent to calling {@link #moveTaskToFront(int, int, Bundle)}
952 * with a null options argument.
953 *
954 * @param taskId The identifier of the task to be moved, as found in
955 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
956 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -0700957 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700958 */
959 public void moveTaskToFront(int taskId, int flags) {
960 moveTaskToFront(taskId, flags, null);
961 }
962
963 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800964 * Ask that the task associated with a given task ID be moved to the
965 * front of the stack, so it is now visible to the user. Requires that
966 * the caller hold permission {@link android.Manifest.permission#REORDER_TASKS}
967 * or a SecurityException will be thrown.
968 *
969 * @param taskId The identifier of the task to be moved, as found in
970 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
971 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -0700972 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700973 * @param options Additional options for the operation, either null or
974 * as per {@link Context#startActivity(Intent, android.os.Bundle)
975 * Context.startActivity(Intent, Bundle)}.
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800976 */
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700977 public void moveTaskToFront(int taskId, int flags, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800978 try {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700979 ActivityManagerNative.getDefault().moveTaskToFront(taskId, flags, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800980 } catch (RemoteException e) {
981 // System dead, we will be dead too soon!
982 }
983 }
984
985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 * Information you can retrieve about a particular Service that is
987 * currently running in the system.
988 */
989 public static class RunningServiceInfo implements Parcelable {
990 /**
991 * The service component.
992 */
993 public ComponentName service;
994
995 /**
996 * If non-zero, this is the process the service is running in.
997 */
998 public int pid;
999
1000 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001001 * The UID that owns this service.
1002 */
1003 public int uid;
1004
1005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 * The name of the process this service runs in.
1007 */
1008 public String process;
1009
1010 /**
1011 * Set to true if the service has asked to run as a foreground process.
1012 */
1013 public boolean foreground;
1014
1015 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001016 * The time when the service was first made active, either by someone
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001017 * starting or binding to it. This
1018 * is in units of {@link android.os.SystemClock#elapsedRealtime()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 */
1020 public long activeSince;
1021
1022 /**
1023 * Set to true if this service has been explicitly started.
1024 */
1025 public boolean started;
1026
1027 /**
1028 * Number of clients connected to the service.
1029 */
1030 public int clientCount;
1031
1032 /**
1033 * Number of times the service's process has crashed while the service
1034 * is running.
1035 */
1036 public int crashCount;
1037
1038 /**
1039 * The time when there was last activity in the service (either
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001040 * explicit requests to start it or clients binding to it). This
1041 * is in units of {@link android.os.SystemClock#uptimeMillis()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public long lastActivityTime;
1044
1045 /**
1046 * If non-zero, this service is not currently running, but scheduled to
1047 * restart at the given time.
1048 */
1049 public long restarting;
1050
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001051 /**
1052 * Bit for {@link #flags}: set if this service has been
1053 * explicitly started.
1054 */
1055 public static final int FLAG_STARTED = 1<<0;
1056
1057 /**
1058 * Bit for {@link #flags}: set if the service has asked to
1059 * run as a foreground process.
1060 */
1061 public static final int FLAG_FOREGROUND = 1<<1;
1062
1063 /**
1064 * Bit for {@link #flags): set if the service is running in a
1065 * core system process.
1066 */
1067 public static final int FLAG_SYSTEM_PROCESS = 1<<2;
1068
1069 /**
1070 * Bit for {@link #flags): set if the service is running in a
1071 * persistent process.
1072 */
1073 public static final int FLAG_PERSISTENT_PROCESS = 1<<3;
1074
1075 /**
1076 * Running flags.
1077 */
1078 public int flags;
1079
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001080 /**
1081 * For special services that are bound to by system code, this is
1082 * the package that holds the binding.
1083 */
1084 public String clientPackage;
1085
1086 /**
1087 * For special services that are bound to by system code, this is
1088 * a string resource providing a user-visible label for who the
1089 * client is.
1090 */
1091 public int clientLabel;
1092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 public RunningServiceInfo() {
1094 }
1095
1096 public int describeContents() {
1097 return 0;
1098 }
1099
1100 public void writeToParcel(Parcel dest, int flags) {
1101 ComponentName.writeToParcel(service, dest);
1102 dest.writeInt(pid);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001103 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 dest.writeString(process);
1105 dest.writeInt(foreground ? 1 : 0);
1106 dest.writeLong(activeSince);
1107 dest.writeInt(started ? 1 : 0);
1108 dest.writeInt(clientCount);
1109 dest.writeInt(crashCount);
1110 dest.writeLong(lastActivityTime);
1111 dest.writeLong(restarting);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001112 dest.writeInt(this.flags);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001113 dest.writeString(clientPackage);
1114 dest.writeInt(clientLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116
1117 public void readFromParcel(Parcel source) {
1118 service = ComponentName.readFromParcel(source);
1119 pid = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001120 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 process = source.readString();
1122 foreground = source.readInt() != 0;
1123 activeSince = source.readLong();
1124 started = source.readInt() != 0;
1125 clientCount = source.readInt();
1126 crashCount = source.readInt();
1127 lastActivityTime = source.readLong();
1128 restarting = source.readLong();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001129 flags = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001130 clientPackage = source.readString();
1131 clientLabel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 }
1133
1134 public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() {
1135 public RunningServiceInfo createFromParcel(Parcel source) {
1136 return new RunningServiceInfo(source);
1137 }
1138 public RunningServiceInfo[] newArray(int size) {
1139 return new RunningServiceInfo[size];
1140 }
1141 };
1142
1143 private RunningServiceInfo(Parcel source) {
1144 readFromParcel(source);
1145 }
1146 }
1147
1148 /**
1149 * Return a list of the services that are currently running.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001150 *
1151 * <p><b>Note: this method is only intended for debugging or implementing
1152 * service management type user interfaces.</b></p>
1153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 * @param maxNum The maximum number of entries to return in the list. The
1155 * actual number returned may be smaller, depending on how many services
1156 * are running.
1157 *
1158 * @return Returns a list of RunningServiceInfo records describing each of
1159 * the running tasks.
1160 */
1161 public List<RunningServiceInfo> getRunningServices(int maxNum)
1162 throws SecurityException {
1163 try {
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001164 return ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 .getServices(maxNum, 0);
1166 } catch (RemoteException e) {
1167 // System dead, we will be dead too soon!
1168 return null;
1169 }
1170 }
1171
1172 /**
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001173 * Returns a PendingIntent you can start to show a control panel for the
1174 * given running service. If the service does not have a control panel,
1175 * null is returned.
1176 */
1177 public PendingIntent getRunningServiceControlPanel(ComponentName service)
1178 throws SecurityException {
1179 try {
1180 return ActivityManagerNative.getDefault()
1181 .getRunningServiceControlPanel(service);
1182 } catch (RemoteException e) {
1183 // System dead, we will be dead too soon!
1184 return null;
1185 }
1186 }
1187
1188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 * Information you can retrieve about the available memory through
1190 * {@link ActivityManager#getMemoryInfo}.
1191 */
1192 public static class MemoryInfo implements Parcelable {
1193 /**
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001194 * The available memory on the system. This number should not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 * be considered absolute: due to the nature of the kernel, a significant
1196 * portion of this memory is actually in use and needed for the overall
1197 * system to run well.
1198 */
1199 public long availMem;
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001200
1201 /**
1202 * The total memory accessible by the kernel. This is basically the
1203 * RAM size of the device, not including below-kernel fixed allocations
1204 * like DMA buffers, RAM for the baseband CPU, etc.
1205 */
1206 public long totalMem;
1207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 /**
1209 * The threshold of {@link #availMem} at which we consider memory to be
1210 * low and start killing background services and other non-extraneous
1211 * processes.
1212 */
1213 public long threshold;
1214
1215 /**
1216 * Set to true if the system considers itself to currently be in a low
1217 * memory situation.
1218 */
1219 public boolean lowMemory;
1220
Dianne Hackborn7d608422011-08-07 16:24:18 -07001221 /** @hide */
1222 public long hiddenAppThreshold;
1223 /** @hide */
1224 public long secondaryServerThreshold;
1225 /** @hide */
1226 public long visibleAppThreshold;
1227 /** @hide */
1228 public long foregroundAppThreshold;
1229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 public MemoryInfo() {
1231 }
1232
1233 public int describeContents() {
1234 return 0;
1235 }
1236
1237 public void writeToParcel(Parcel dest, int flags) {
1238 dest.writeLong(availMem);
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001239 dest.writeLong(totalMem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 dest.writeLong(threshold);
1241 dest.writeInt(lowMemory ? 1 : 0);
Dianne Hackborn7d608422011-08-07 16:24:18 -07001242 dest.writeLong(hiddenAppThreshold);
1243 dest.writeLong(secondaryServerThreshold);
1244 dest.writeLong(visibleAppThreshold);
1245 dest.writeLong(foregroundAppThreshold);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 }
1247
1248 public void readFromParcel(Parcel source) {
1249 availMem = source.readLong();
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001250 totalMem = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 threshold = source.readLong();
1252 lowMemory = source.readInt() != 0;
Dianne Hackborn7d608422011-08-07 16:24:18 -07001253 hiddenAppThreshold = source.readLong();
1254 secondaryServerThreshold = source.readLong();
1255 visibleAppThreshold = source.readLong();
1256 foregroundAppThreshold = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 }
1258
1259 public static final Creator<MemoryInfo> CREATOR
1260 = new Creator<MemoryInfo>() {
1261 public MemoryInfo createFromParcel(Parcel source) {
1262 return new MemoryInfo(source);
1263 }
1264 public MemoryInfo[] newArray(int size) {
1265 return new MemoryInfo[size];
1266 }
1267 };
1268
1269 private MemoryInfo(Parcel source) {
1270 readFromParcel(source);
1271 }
1272 }
1273
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001274 /**
1275 * Return general information about the memory state of the system. This
1276 * can be used to help decide how to manage your own memory, though note
1277 * that polling is not recommended and
1278 * {@link android.content.ComponentCallbacks2#onTrimMemory(int)
1279 * ComponentCallbacks2.onTrimMemory(int)} is the preferred way to do this.
1280 * Also see {@link #getMyMemoryState} for how to retrieve the current trim
1281 * level of your process as needed, which gives a better hint for how to
1282 * manage its memory.
1283 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 public void getMemoryInfo(MemoryInfo outInfo) {
1285 try {
1286 ActivityManagerNative.getDefault().getMemoryInfo(outInfo);
1287 } catch (RemoteException e) {
1288 }
1289 }
Craig Mautner967212c2013-04-13 21:10:58 -07001290
Craig Mautner5ff12102013-05-24 12:50:15 -07001291 /**
1292 * Information you can retrieve about the WindowManager StackBox hierarchy.
1293 * @hide
1294 */
1295 public static class StackBoxInfo implements Parcelable {
1296 public int stackBoxId;
1297 public float weight;
1298 public boolean vertical;
1299 public Rect bounds;
1300 public StackBoxInfo[] children;
1301 public int stackId;
1302 public StackInfo stack;
1303
1304 @Override
1305 public int describeContents() {
1306 return 0;
1307 }
1308
1309 @Override
1310 public void writeToParcel(Parcel dest, int flags) {
1311 dest.writeInt(stackBoxId);
1312 dest.writeFloat(weight);
1313 dest.writeInt(vertical ? 1 : 0);
1314 bounds.writeToParcel(dest, flags);
1315 dest.writeInt(stackId);
1316 if (children != null) {
1317 children[0].writeToParcel(dest, flags);
1318 children[1].writeToParcel(dest, flags);
1319 } else {
1320 stack.writeToParcel(dest, flags);
1321 }
1322 }
1323
1324 public void readFromParcel(Parcel source) {
1325 stackBoxId = source.readInt();
1326 weight = source.readFloat();
1327 vertical = source.readInt() == 1;
1328 bounds = Rect.CREATOR.createFromParcel(source);
1329 stackId = source.readInt();
1330 if (stackId == -1) {
1331 children = new StackBoxInfo[2];
1332 children[0] = StackBoxInfo.CREATOR.createFromParcel(source);
1333 children[1] = StackBoxInfo.CREATOR.createFromParcel(source);
1334 } else {
1335 stack = StackInfo.CREATOR.createFromParcel(source);
1336 }
1337 }
1338
1339 public static final Creator<StackBoxInfo> CREATOR =
1340 new Creator<ActivityManager.StackBoxInfo>() {
1341
1342 @Override
1343 public StackBoxInfo createFromParcel(Parcel source) {
1344 return new StackBoxInfo(source);
1345 }
1346
1347 @Override
1348 public StackBoxInfo[] newArray(int size) {
1349 return new StackBoxInfo[size];
1350 }
1351 };
1352
1353 public StackBoxInfo() {
1354 }
1355
1356 public StackBoxInfo(Parcel source) {
1357 readFromParcel(source);
1358 }
1359
1360 public String toString(String prefix) {
1361 StringBuilder sb = new StringBuilder(256);
1362 sb.append(prefix); sb.append("Box id=" + stackBoxId); sb.append(" weight=" + weight);
1363 sb.append(" vertical=" + vertical); sb.append(" bounds=" + bounds.toShortString());
1364 sb.append("\n");
1365 if (children != null) {
1366 sb.append(prefix); sb.append("First child=\n");
1367 sb.append(children[0].toString(prefix + " "));
1368 sb.append(prefix); sb.append("Second child=\n");
1369 sb.append(children[1].toString(prefix + " "));
1370 } else {
1371 sb.append(prefix); sb.append("Stack=\n");
1372 sb.append(stack.toString(prefix + " "));
1373 }
1374 return sb.toString();
1375 }
1376
1377 @Override
1378 public String toString() {
1379 return toString("");
1380 }
1381 }
Craig Mautner967212c2013-04-13 21:10:58 -07001382
1383 /**
1384 * Information you can retrieve about an ActivityStack in the system.
1385 * @hide
1386 */
1387 public static class StackInfo implements Parcelable {
1388 public int stackId;
1389 public Rect bounds;
1390 public int[] taskIds;
1391 public String[] taskNames;
1392
Craig Mautner967212c2013-04-13 21:10:58 -07001393 @Override
1394 public int describeContents() {
1395 return 0;
1396 }
1397
1398 @Override
1399 public void writeToParcel(Parcel dest, int flags) {
1400 dest.writeInt(stackId);
1401 dest.writeInt(bounds.left);
1402 dest.writeInt(bounds.top);
1403 dest.writeInt(bounds.right);
1404 dest.writeInt(bounds.bottom);
1405 dest.writeIntArray(taskIds);
1406 dest.writeStringArray(taskNames);
1407 }
1408
1409 public void readFromParcel(Parcel source) {
1410 stackId = source.readInt();
1411 bounds = new Rect(
1412 source.readInt(), source.readInt(), source.readInt(), source.readInt());
1413 taskIds = source.createIntArray();
1414 taskNames = source.createStringArray();
1415 }
1416
1417 public static final Creator<StackInfo> CREATOR = new Creator<StackInfo>() {
1418 @Override
1419 public StackInfo createFromParcel(Parcel source) {
1420 return new StackInfo(source);
1421 }
1422 @Override
1423 public StackInfo[] newArray(int size) {
1424 return new StackInfo[size];
1425 }
1426 };
1427
Craig Mautner5ff12102013-05-24 12:50:15 -07001428 public StackInfo() {
1429 }
1430
Craig Mautner967212c2013-04-13 21:10:58 -07001431 private StackInfo(Parcel source) {
1432 readFromParcel(source);
1433 }
1434
Craig Mautner5ff12102013-05-24 12:50:15 -07001435 public String toString(String prefix) {
Craig Mautner967212c2013-04-13 21:10:58 -07001436 StringBuilder sb = new StringBuilder(256);
Craig Mautner5ff12102013-05-24 12:50:15 -07001437 sb.append(prefix); sb.append("Stack id="); sb.append(stackId);
Craig Mautner967212c2013-04-13 21:10:58 -07001438 sb.append(" bounds="); sb.append(bounds.toShortString()); sb.append("\n");
Craig Mautner5ff12102013-05-24 12:50:15 -07001439 prefix = prefix + " ";
Craig Mautner967212c2013-04-13 21:10:58 -07001440 for (int i = 0; i < taskIds.length; ++i) {
1441 sb.append(prefix); sb.append("taskId="); sb.append(taskIds[i]);
1442 sb.append(": "); sb.append(taskNames[i]); sb.append("\n");
1443 }
1444 return sb.toString();
1445 }
Craig Mautner5ff12102013-05-24 12:50:15 -07001446
1447 @Override
1448 public String toString() {
1449 return toString("");
1450 }
Craig Mautner967212c2013-04-13 21:10:58 -07001451 }
1452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 /**
1454 * @hide
1455 */
1456 public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
1457 try {
1458 return ActivityManagerNative.getDefault().clearApplicationUserData(packageName,
Dianne Hackborn41203752012-08-31 14:05:51 -07001459 observer, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 } catch (RemoteException e) {
1461 return false;
1462 }
1463 }
Christopher Tatea3664242013-06-26 15:00:18 -07001464
1465 /**
1466 * Permits an application to erase its own data from disk. This is equivalent to
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001467 * the user choosing to clear the app's data from within the device settings UI. It
1468 * erases all dynamic data associated with the app -- its private data and data in its
1469 * private area on external storage -- but does not remove the installed application
1470 * itself, nor any OBB files.
Christopher Tatea3664242013-06-26 15:00:18 -07001471 *
1472 * @return {@code true} if the application successfully requested that the application's
1473 * data be erased; {@code false} otherwise.
1474 */
1475 public boolean clearApplicationUserData() {
1476 return clearApplicationUserData(mContext.getPackageName(), null);
1477 }
1478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 /**
1480 * Information you can retrieve about any processes that are in an error condition.
1481 */
1482 public static class ProcessErrorStateInfo implements Parcelable {
1483 /**
1484 * Condition codes
1485 */
1486 public static final int NO_ERROR = 0;
1487 public static final int CRASHED = 1;
1488 public static final int NOT_RESPONDING = 2;
1489
1490 /**
1491 * The condition that the process is in.
1492 */
1493 public int condition;
1494
1495 /**
1496 * The process name in which the crash or error occurred.
1497 */
1498 public String processName;
1499
1500 /**
1501 * The pid of this process; 0 if none
1502 */
1503 public int pid;
1504
1505 /**
1506 * The kernel user-ID that has been assigned to this process;
1507 * currently this is not a unique ID (multiple applications can have
1508 * the same uid).
1509 */
1510 public int uid;
1511
1512 /**
Dan Egnor60d87622009-12-16 16:32:58 -08001513 * The activity name associated with the error, if known. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 */
1515 public String tag;
1516
1517 /**
1518 * A short message describing the error condition.
1519 */
1520 public String shortMsg;
1521
1522 /**
1523 * A long message describing the error condition.
1524 */
1525 public String longMsg;
1526
1527 /**
Dan Egnorb7f03672009-12-09 16:22:32 -08001528 * The stack trace where the error originated. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 */
Dan Egnorb7f03672009-12-09 16:22:32 -08001530 public String stackTrace;
1531
1532 /**
1533 * to be deprecated: This value will always be null.
1534 */
1535 public byte[] crashData = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536
1537 public ProcessErrorStateInfo() {
1538 }
1539
Craig Mautner5ff12102013-05-24 12:50:15 -07001540 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 public int describeContents() {
1542 return 0;
1543 }
1544
Craig Mautner5ff12102013-05-24 12:50:15 -07001545 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 public void writeToParcel(Parcel dest, int flags) {
1547 dest.writeInt(condition);
1548 dest.writeString(processName);
1549 dest.writeInt(pid);
1550 dest.writeInt(uid);
1551 dest.writeString(tag);
1552 dest.writeString(shortMsg);
1553 dest.writeString(longMsg);
Dan Egnorb7f03672009-12-09 16:22:32 -08001554 dest.writeString(stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
Craig Mautner5ff12102013-05-24 12:50:15 -07001556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 public void readFromParcel(Parcel source) {
1558 condition = source.readInt();
1559 processName = source.readString();
1560 pid = source.readInt();
1561 uid = source.readInt();
1562 tag = source.readString();
1563 shortMsg = source.readString();
1564 longMsg = source.readString();
Dan Egnorb7f03672009-12-09 16:22:32 -08001565 stackTrace = source.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
1567
1568 public static final Creator<ProcessErrorStateInfo> CREATOR =
1569 new Creator<ProcessErrorStateInfo>() {
1570 public ProcessErrorStateInfo createFromParcel(Parcel source) {
1571 return new ProcessErrorStateInfo(source);
1572 }
1573 public ProcessErrorStateInfo[] newArray(int size) {
1574 return new ProcessErrorStateInfo[size];
1575 }
1576 };
1577
1578 private ProcessErrorStateInfo(Parcel source) {
1579 readFromParcel(source);
1580 }
1581 }
1582
1583 /**
1584 * Returns a list of any processes that are currently in an error condition. The result
1585 * will be null if all processes are running properly at this time.
1586 *
1587 * @return Returns a list of ProcessErrorStateInfo records, or null if there are no
1588 * current error conditions (it will not return an empty list). This list ordering is not
1589 * specified.
1590 */
1591 public List<ProcessErrorStateInfo> getProcessesInErrorState() {
1592 try {
1593 return ActivityManagerNative.getDefault().getProcessesInErrorState();
1594 } catch (RemoteException e) {
1595 return null;
1596 }
1597 }
1598
1599 /**
1600 * Information you can retrieve about a running process.
1601 */
1602 public static class RunningAppProcessInfo implements Parcelable {
1603 /**
1604 * The name of the process that this object is associated with
1605 */
1606 public String processName;
1607
1608 /**
1609 * The pid of this process; 0 if none
1610 */
1611 public int pid;
1612
Dianne Hackborneb034652009-09-07 00:49:58 -07001613 /**
1614 * The user id of this process.
1615 */
1616 public int uid;
1617
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001618 /**
1619 * All packages that have been loaded into the process.
1620 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 public String pkgList[];
1622
1623 /**
Dianne Hackborn482566e2010-09-03 12:51:28 -07001624 * Constant for {@link #flags}: this is an app that is unable to
1625 * correctly save its state when going to the background,
1626 * so it can not be killed while in the background.
1627 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001628 */
Dianne Hackborn482566e2010-09-03 12:51:28 -07001629 public static final int FLAG_CANT_SAVE_STATE = 1<<0;
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001630
1631 /**
Dianne Hackborn42499172010-10-15 18:45:07 -07001632 * Constant for {@link #flags}: this process is associated with a
1633 * persistent system app.
1634 * @hide
1635 */
1636 public static final int FLAG_PERSISTENT = 1<<1;
1637
1638 /**
Dianne Hackborn0c380492012-08-20 17:23:30 -07001639 * Constant for {@link #flags}: this process is associated with a
1640 * persistent system app.
1641 * @hide
1642 */
1643 public static final int FLAG_HAS_ACTIVITIES = 1<<2;
1644
1645 /**
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001646 * Flags of information. May be any of
Dianne Hackborn482566e2010-09-03 12:51:28 -07001647 * {@link #FLAG_CANT_SAVE_STATE}.
1648 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001649 */
1650 public int flags;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001651
1652 /**
1653 * Last memory trim level reported to the process: corresponds to
1654 * the values supplied to {@link android.content.ComponentCallbacks2#onTrimMemory(int)
1655 * ComponentCallbacks2.onTrimMemory(int)}.
1656 */
1657 public int lastTrimLevel;
1658
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001659 /**
Dianne Hackborna93c2c12012-05-31 15:29:36 -07001660 * Constant for {@link #importance}: this is a persistent process.
1661 * Only used when reporting to process observers.
1662 * @hide
1663 */
1664 public static final int IMPORTANCE_PERSISTENT = 50;
1665
1666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 * Constant for {@link #importance}: this process is running the
1668 * foreground UI.
1669 */
1670 public static final int IMPORTANCE_FOREGROUND = 100;
1671
1672 /**
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001673 * Constant for {@link #importance}: this process is running something
1674 * that is actively visible to the user, though not in the immediate
1675 * foreground.
Dianne Hackborn860755f2010-06-03 18:47:52 -07001676 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001677 public static final int IMPORTANCE_VISIBLE = 200;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001678
1679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 * Constant for {@link #importance}: this process is running something
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001681 * that is considered to be actively perceptible to the user. An
1682 * example would be an application performing background music playback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001684 public static final int IMPORTANCE_PERCEPTIBLE = 130;
1685
1686 /**
Dianne Hackborn5383f502010-10-22 12:59:20 -07001687 * Constant for {@link #importance}: this process is running an
1688 * application that can not save its state, and thus can't be killed
1689 * while in the background.
1690 * @hide
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001691 */
Dianne Hackborn5383f502010-10-22 12:59:20 -07001692 public static final int IMPORTANCE_CANT_SAVE_STATE = 170;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693
1694 /**
1695 * Constant for {@link #importance}: this process is contains services
1696 * that should remain running.
1697 */
1698 public static final int IMPORTANCE_SERVICE = 300;
1699
1700 /**
1701 * Constant for {@link #importance}: this process process contains
1702 * background code that is expendable.
1703 */
1704 public static final int IMPORTANCE_BACKGROUND = 400;
1705
1706 /**
1707 * Constant for {@link #importance}: this process is empty of any
1708 * actively running code.
1709 */
1710 public static final int IMPORTANCE_EMPTY = 500;
1711
1712 /**
1713 * The relative importance level that the system places on this
1714 * process. May be one of {@link #IMPORTANCE_FOREGROUND},
1715 * {@link #IMPORTANCE_VISIBLE}, {@link #IMPORTANCE_SERVICE},
1716 * {@link #IMPORTANCE_BACKGROUND}, or {@link #IMPORTANCE_EMPTY}. These
1717 * constants are numbered so that "more important" values are always
1718 * smaller than "less important" values.
1719 */
1720 public int importance;
1721
1722 /**
1723 * An additional ordering within a particular {@link #importance}
1724 * category, providing finer-grained information about the relative
1725 * utility of processes within a category. This number means nothing
1726 * except that a smaller values are more recently used (and thus
1727 * more important). Currently an LRU value is only maintained for
1728 * the {@link #IMPORTANCE_BACKGROUND} category, though others may
1729 * be maintained in the future.
1730 */
1731 public int lru;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001732
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001733 /**
1734 * Constant for {@link #importanceReasonCode}: nothing special has
1735 * been specified for the reason for this level.
1736 */
1737 public static final int REASON_UNKNOWN = 0;
1738
1739 /**
1740 * Constant for {@link #importanceReasonCode}: one of the application's
1741 * content providers is being used by another process. The pid of
1742 * the client process is in {@link #importanceReasonPid} and the
1743 * target provider in this process is in
1744 * {@link #importanceReasonComponent}.
1745 */
1746 public static final int REASON_PROVIDER_IN_USE = 1;
1747
1748 /**
1749 * Constant for {@link #importanceReasonCode}: one of the application's
1750 * content providers is being used by another process. The pid of
1751 * the client process is in {@link #importanceReasonPid} and the
1752 * target provider in this process is in
1753 * {@link #importanceReasonComponent}.
1754 */
1755 public static final int REASON_SERVICE_IN_USE = 2;
1756
1757 /**
1758 * The reason for {@link #importance}, if any.
1759 */
1760 public int importanceReasonCode;
1761
1762 /**
1763 * For the specified values of {@link #importanceReasonCode}, this
1764 * is the process ID of the other process that is a client of this
1765 * process. This will be 0 if no other process is using this one.
1766 */
1767 public int importanceReasonPid;
1768
1769 /**
1770 * For the specified values of {@link #importanceReasonCode}, this
1771 * is the name of the component that is being used in this process.
1772 */
1773 public ComponentName importanceReasonComponent;
1774
Dianne Hackborn905577f2011-09-07 18:31:28 -07001775 /**
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07001776 * When {@link #importanceReasonPid} is non-0, this is the importance
Dianne Hackborn905577f2011-09-07 18:31:28 -07001777 * of the other pid. @hide
1778 */
1779 public int importanceReasonImportance;
1780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 public RunningAppProcessInfo() {
1782 importance = IMPORTANCE_FOREGROUND;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001783 importanceReasonCode = REASON_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
1785
1786 public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) {
1787 processName = pProcessName;
1788 pid = pPid;
1789 pkgList = pArr;
1790 }
1791
1792 public int describeContents() {
1793 return 0;
1794 }
1795
1796 public void writeToParcel(Parcel dest, int flags) {
1797 dest.writeString(processName);
1798 dest.writeInt(pid);
Dianne Hackborneb034652009-09-07 00:49:58 -07001799 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 dest.writeStringArray(pkgList);
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001801 dest.writeInt(this.flags);
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001802 dest.writeInt(lastTrimLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 dest.writeInt(importance);
1804 dest.writeInt(lru);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001805 dest.writeInt(importanceReasonCode);
1806 dest.writeInt(importanceReasonPid);
1807 ComponentName.writeToParcel(importanceReasonComponent, dest);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001808 dest.writeInt(importanceReasonImportance);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
1810
1811 public void readFromParcel(Parcel source) {
1812 processName = source.readString();
1813 pid = source.readInt();
Dianne Hackborneb034652009-09-07 00:49:58 -07001814 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 pkgList = source.readStringArray();
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001816 flags = source.readInt();
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001817 lastTrimLevel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 importance = source.readInt();
1819 lru = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001820 importanceReasonCode = source.readInt();
1821 importanceReasonPid = source.readInt();
1822 importanceReasonComponent = ComponentName.readFromParcel(source);
Dianne Hackborn905577f2011-09-07 18:31:28 -07001823 importanceReasonImportance = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
1825
1826 public static final Creator<RunningAppProcessInfo> CREATOR =
1827 new Creator<RunningAppProcessInfo>() {
1828 public RunningAppProcessInfo createFromParcel(Parcel source) {
1829 return new RunningAppProcessInfo(source);
1830 }
1831 public RunningAppProcessInfo[] newArray(int size) {
1832 return new RunningAppProcessInfo[size];
1833 }
1834 };
1835
1836 private RunningAppProcessInfo(Parcel source) {
1837 readFromParcel(source);
1838 }
1839 }
1840
1841 /**
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07001842 * Returns a list of application processes installed on external media
1843 * that are running on the device.
1844 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001845 * <p><b>Note: this method is only intended for debugging or building
1846 * a user-facing process management UI.</b></p>
1847 *
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07001848 * @return Returns a list of ApplicationInfo records, or null if none
1849 * This list ordering is not specified.
1850 * @hide
1851 */
1852 public List<ApplicationInfo> getRunningExternalApplications() {
1853 try {
1854 return ActivityManagerNative.getDefault().getRunningExternalApplications();
1855 } catch (RemoteException e) {
1856 return null;
1857 }
1858 }
1859
1860 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 * Returns a list of application processes that are running on the device.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001862 *
1863 * <p><b>Note: this method is only intended for debugging or building
1864 * a user-facing process management UI.</b></p>
1865 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 * @return Returns a list of RunningAppProcessInfo records, or null if there are no
1867 * running processes (it will not return an empty list). This list ordering is not
1868 * specified.
1869 */
1870 public List<RunningAppProcessInfo> getRunningAppProcesses() {
1871 try {
1872 return ActivityManagerNative.getDefault().getRunningAppProcesses();
1873 } catch (RemoteException e) {
1874 return null;
1875 }
1876 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001877
1878 /**
1879 * Return global memory state information for the calling process. This
1880 * does not fill in all fields of the {@link RunningAppProcessInfo}. The
1881 * only fields that will be filled in are
1882 * {@link RunningAppProcessInfo#pid},
1883 * {@link RunningAppProcessInfo#uid},
1884 * {@link RunningAppProcessInfo#lastTrimLevel},
1885 * {@link RunningAppProcessInfo#importance},
1886 * {@link RunningAppProcessInfo#lru}, and
1887 * {@link RunningAppProcessInfo#importanceReasonCode}.
1888 */
1889 static public void getMyMemoryState(RunningAppProcessInfo outState) {
1890 try {
1891 ActivityManagerNative.getDefault().getMyMemoryState(outState);
1892 } catch (RemoteException e) {
1893 }
1894 }
1895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07001897 * Return information about the memory usage of one or more processes.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001898 *
1899 * <p><b>Note: this method is only intended for debugging or building
1900 * a user-facing process management UI.</b></p>
1901 *
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07001902 * @param pids The pids of the processes whose memory usage is to be
1903 * retrieved.
1904 * @return Returns an array of memory information, one for each
1905 * requested pid.
1906 */
1907 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
1908 try {
1909 return ActivityManagerNative.getDefault().getProcessMemoryInfo(pids);
1910 } catch (RemoteException e) {
1911 return null;
1912 }
1913 }
1914
1915 /**
Dianne Hackborn03abb812010-01-04 18:43:19 -08001916 * @deprecated This is now just a wrapper for
1917 * {@link #killBackgroundProcesses(String)}; the previous behavior here
1918 * is no longer available to applications because it allows them to
1919 * break other applications by removing their alarms, stopping their
1920 * services, etc.
1921 */
1922 @Deprecated
1923 public void restartPackage(String packageName) {
1924 killBackgroundProcesses(packageName);
1925 }
1926
1927 /**
1928 * Have the system immediately kill all background processes associated
1929 * with the given package. This is the same as the kernel killing those
1930 * processes to reclaim memory; the system will take care of restarting
1931 * these processes in the future as needed.
1932 *
1933 * <p>You must hold the permission
1934 * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to
1935 * call this method.
1936 *
1937 * @param packageName The name of the package whose processes are to
1938 * be killed.
1939 */
1940 public void killBackgroundProcesses(String packageName) {
1941 try {
Dianne Hackborn1676c852012-09-10 14:52:30 -07001942 ActivityManagerNative.getDefault().killBackgroundProcesses(packageName,
1943 UserHandle.myUserId());
Dianne Hackborn03abb812010-01-04 18:43:19 -08001944 } catch (RemoteException e) {
1945 }
1946 }
1947
1948 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 * Have the system perform a force stop of everything associated with
1950 * the given application package. All processes that share its uid
1951 * will be killed, all services it has running stopped, all activities
1952 * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}
1953 * broadcast will be sent, so that any of its registered alarms can
1954 * be stopped, notifications removed, etc.
1955 *
1956 * <p>You must hold the permission
Dianne Hackborn03abb812010-01-04 18:43:19 -08001957 * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 * call this method.
1959 *
1960 * @param packageName The name of the package to be stopped.
Dianne Hackborn03abb812010-01-04 18:43:19 -08001961 *
1962 * @hide This is not available to third party applications due to
1963 * it allowing them to break other applications by stopping their
1964 * services, removing their alarms, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 */
Dianne Hackborn03abb812010-01-04 18:43:19 -08001966 public void forceStopPackage(String packageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 try {
Dianne Hackborn1676c852012-09-10 14:52:30 -07001968 ActivityManagerNative.getDefault().forceStopPackage(packageName,
1969 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 } catch (RemoteException e) {
1971 }
1972 }
1973
1974 /**
1975 * Get the device configuration attributes.
1976 */
1977 public ConfigurationInfo getDeviceConfigurationInfo() {
1978 try {
1979 return ActivityManagerNative.getDefault().getDeviceConfigurationInfo();
1980 } catch (RemoteException e) {
1981 }
1982 return null;
1983 }
Kenny Root5ef44b72011-01-26 17:22:20 -08001984
1985 /**
1986 * Get the preferred density of icons for the launcher. This is used when
1987 * custom drawables are created (e.g., for shortcuts).
1988 *
1989 * @return density in terms of DPI
1990 */
1991 public int getLauncherLargeIconDensity() {
1992 final Resources res = mContext.getResources();
1993 final int density = res.getDisplayMetrics().densityDpi;
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08001994 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08001995
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08001996 if (sw < 600) {
1997 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08001998 return density;
1999 }
2000
2001 switch (density) {
2002 case DisplayMetrics.DENSITY_LOW:
2003 return DisplayMetrics.DENSITY_MEDIUM;
2004 case DisplayMetrics.DENSITY_MEDIUM:
2005 return DisplayMetrics.DENSITY_HIGH;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002006 case DisplayMetrics.DENSITY_TV:
2007 return DisplayMetrics.DENSITY_XHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08002008 case DisplayMetrics.DENSITY_HIGH:
2009 return DisplayMetrics.DENSITY_XHIGH;
2010 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002011 return DisplayMetrics.DENSITY_XXHIGH;
2012 case DisplayMetrics.DENSITY_XXHIGH:
2013 return DisplayMetrics.DENSITY_XHIGH * 2;
Kenny Root5ef44b72011-01-26 17:22:20 -08002014 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002015 // The density is some abnormal value. Return some other
2016 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002017 return (int)((density*1.5f)+.5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08002018 }
2019 }
2020
2021 /**
2022 * Get the preferred launcher icon size. This is used when custom drawables
2023 * are created (e.g., for shortcuts).
2024 *
2025 * @return dimensions of square icons in terms of pixels
2026 */
2027 public int getLauncherLargeIconSize() {
2028 final Resources res = mContext.getResources();
2029 final int size = res.getDimensionPixelSize(android.R.dimen.app_icon_size);
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002030 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08002031
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002032 if (sw < 600) {
2033 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08002034 return size;
2035 }
2036
2037 final int density = res.getDisplayMetrics().densityDpi;
2038
2039 switch (density) {
2040 case DisplayMetrics.DENSITY_LOW:
2041 return (size * DisplayMetrics.DENSITY_MEDIUM) / DisplayMetrics.DENSITY_LOW;
2042 case DisplayMetrics.DENSITY_MEDIUM:
2043 return (size * DisplayMetrics.DENSITY_HIGH) / DisplayMetrics.DENSITY_MEDIUM;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002044 case DisplayMetrics.DENSITY_TV:
2045 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08002046 case DisplayMetrics.DENSITY_HIGH:
2047 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
2048 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002049 return (size * DisplayMetrics.DENSITY_XXHIGH) / DisplayMetrics.DENSITY_XHIGH;
2050 case DisplayMetrics.DENSITY_XXHIGH:
2051 return (size * DisplayMetrics.DENSITY_XHIGH*2) / DisplayMetrics.DENSITY_XXHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08002052 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002053 // The density is some abnormal value. Return some other
2054 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002055 return (int)((size*1.5f) + .5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08002056 }
2057 }
2058
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08002059 /**
2060 * Returns "true" if the user interface is currently being messed with
2061 * by a monkey.
2062 */
2063 public static boolean isUserAMonkey() {
2064 try {
2065 return ActivityManagerNative.getDefault().isUserAMonkey();
2066 } catch (RemoteException e) {
2067 }
2068 return false;
2069 }
Brett Chabot3b4fcbc2011-01-09 13:41:02 -08002070
2071 /**
2072 * Returns "true" if device is running in a test harness.
2073 */
2074 public static boolean isRunningInTestHarness() {
2075 return SystemProperties.getBoolean("ro.test_harness", false);
2076 }
Peter Visontay8d224ca2011-02-18 16:39:19 +00002077
2078 /**
2079 * Returns the launch count of each installed package.
2080 *
2081 * @hide
2082 */
2083 public Map<String, Integer> getAllPackageLaunchCounts() {
2084 try {
2085 IUsageStats usageStatsService = IUsageStats.Stub.asInterface(
2086 ServiceManager.getService("usagestats"));
2087 if (usageStatsService == null) {
2088 return new HashMap<String, Integer>();
2089 }
2090
Peter Visontaybfcda392011-03-02 18:53:37 +00002091 PkgUsageStats[] allPkgUsageStats = usageStatsService.getAllPkgUsageStats();
2092 if (allPkgUsageStats == null) {
2093 return new HashMap<String, Integer>();
2094 }
2095
Peter Visontay8d224ca2011-02-18 16:39:19 +00002096 Map<String, Integer> launchCounts = new HashMap<String, Integer>();
Peter Visontaybfcda392011-03-02 18:53:37 +00002097 for (PkgUsageStats pkgUsageStats : allPkgUsageStats) {
Peter Visontay8d224ca2011-02-18 16:39:19 +00002098 launchCounts.put(pkgUsageStats.packageName, pkgUsageStats.launchCount);
2099 }
2100
2101 return launchCounts;
2102 } catch (RemoteException e) {
2103 Log.w(TAG, "Could not query launch counts", e);
2104 return new HashMap<String, Integer>();
2105 }
2106 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002107
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002108 /** @hide */
2109 public static int checkComponentPermission(String permission, int uid,
2110 int owningUid, boolean exported) {
2111 // Root, system server get to do everything.
2112 if (uid == 0 || uid == Process.SYSTEM_UID) {
2113 return PackageManager.PERMISSION_GRANTED;
2114 }
2115 // Isolated processes don't get any permissions.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002116 if (UserHandle.isIsolated(uid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002117 return PackageManager.PERMISSION_DENIED;
2118 }
2119 // If there is a uid that owns whatever is being accessed, it has
2120 // blanket access to it regardless of the permissions it requires.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002121 if (owningUid >= 0 && UserHandle.isSameApp(uid, owningUid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002122 return PackageManager.PERMISSION_GRANTED;
2123 }
2124 // If the target is not exported, then nobody else can get to it.
2125 if (!exported) {
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -07002126 /*
2127 RuntimeException here = new RuntimeException("here");
2128 here.fillInStackTrace();
2129 Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid,
2130 here);
2131 */
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002132 return PackageManager.PERMISSION_DENIED;
2133 }
2134 if (permission == null) {
2135 return PackageManager.PERMISSION_GRANTED;
2136 }
2137 try {
2138 return AppGlobals.getPackageManager()
2139 .checkUidPermission(permission, uid);
2140 } catch (RemoteException e) {
2141 // Should never happen, but if it does... deny!
2142 Slog.e(TAG, "PackageManager is dead?!?", e);
2143 }
2144 return PackageManager.PERMISSION_DENIED;
2145 }
2146
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002147 /** @hide */
2148 public static int checkUidPermission(String permission, int uid) {
2149 try {
2150 return AppGlobals.getPackageManager()
2151 .checkUidPermission(permission, uid);
2152 } catch (RemoteException e) {
2153 // Should never happen, but if it does... deny!
2154 Slog.e(TAG, "PackageManager is dead?!?", e);
2155 }
2156 return PackageManager.PERMISSION_DENIED;
2157 }
2158
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002159 /**
2160 * @hide
2161 * Helper for dealing with incoming user arguments to system service calls.
2162 * Takes care of checking permissions and converting USER_CURRENT to the
2163 * actual current user.
2164 *
2165 * @param callingPid The pid of the incoming call, as per Binder.getCallingPid().
2166 * @param callingUid The uid of the incoming call, as per Binder.getCallingUid().
2167 * @param userId The user id argument supplied by the caller -- this is the user
2168 * they want to run as.
2169 * @param allowAll If true, we will allow USER_ALL. This means you must be prepared
2170 * to get a USER_ALL returned and deal with it correctly. If false,
2171 * an exception will be thrown if USER_ALL is supplied.
2172 * @param requireFull If true, the caller must hold
2173 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} to be able to run as a
2174 * different user than their current process; otherwise they must hold
2175 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS}.
2176 * @param name Optional textual name of the incoming call; only for generating error messages.
2177 * @param callerPackage Optional package name of caller; only for error messages.
2178 *
2179 * @return Returns the user ID that the call should run as. Will always be a concrete
2180 * user number, unless <var>allowAll</var> is true in which case it could also be
2181 * USER_ALL.
2182 */
Dianne Hackborn41203752012-08-31 14:05:51 -07002183 public static int handleIncomingUser(int callingPid, int callingUid, int userId,
2184 boolean allowAll, boolean requireFull, String name, String callerPackage) {
2185 if (UserHandle.getUserId(callingUid) == userId) {
2186 return userId;
2187 }
2188 try {
2189 return ActivityManagerNative.getDefault().handleIncomingUser(callingPid,
2190 callingUid, userId, allowAll, requireFull, name, callerPackage);
2191 } catch (RemoteException e) {
2192 throw new SecurityException("Failed calling activity manager", e);
2193 }
2194 }
2195
2196 /** @hide */
2197 public static int getCurrentUser() {
2198 UserInfo ui;
2199 try {
2200 ui = ActivityManagerNative.getDefault().getCurrentUser();
2201 return ui != null ? ui.id : 0;
2202 } catch (RemoteException e) {
2203 return 0;
2204 }
2205 }
2206
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002207 /**
Mark Brophy9fc03302011-07-01 16:56:24 +01002208 * Returns the usage statistics of each installed package.
2209 *
2210 * @hide
2211 */
2212 public PkgUsageStats[] getAllPackageUsageStats() {
2213 try {
2214 IUsageStats usageStatsService = IUsageStats.Stub.asInterface(
2215 ServiceManager.getService("usagestats"));
2216 if (usageStatsService != null) {
2217 return usageStatsService.getAllPkgUsageStats();
2218 }
2219 } catch (RemoteException e) {
2220 Log.w(TAG, "Could not query usage stats", e);
2221 }
2222 return new PkgUsageStats[0];
2223 }
2224
2225 /**
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002226 * @param userid the user's id. Zero indicates the default user
2227 * @hide
2228 */
2229 public boolean switchUser(int userid) {
2230 try {
2231 return ActivityManagerNative.getDefault().switchUser(userid);
2232 } catch (RemoteException e) {
2233 return false;
2234 }
2235 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07002236
2237 /**
2238 * Return whether the given user is actively running. This means that
2239 * the user is in the "started" state, not "stopped" -- it is currently
2240 * allowed to run code through scheduled alarms, receiving broadcasts,
2241 * etc. A started user may be either the current foreground user or a
2242 * background user; the result here does not distinguish between the two.
2243 * @param userid the user's id. Zero indicates the default user.
2244 * @hide
2245 */
2246 public boolean isUserRunning(int userid) {
2247 try {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002248 return ActivityManagerNative.getDefault().isUserRunning(userid, false);
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07002249 } catch (RemoteException e) {
2250 return false;
2251 }
2252 }
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002253
2254 /**
2255 * Perform a system dump of various state associated with the given application
2256 * package name. This call blocks while the dump is being performed, so should
2257 * not be done on a UI thread. The data will be written to the given file
2258 * descriptor as text. An application must hold the
2259 * {@link android.Manifest.permission#DUMP} permission to make this call.
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002260 * @param fd The file descriptor that the dump should be written to. The file
2261 * descriptor is <em>not</em> closed by this function; the caller continues to
2262 * own it.
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002263 * @param packageName The name of the package that is to be dumped.
2264 */
2265 public void dumpPackageState(FileDescriptor fd, String packageName) {
2266 dumpPackageStateStatic(fd, packageName);
2267 }
2268
2269 /**
2270 * @hide
2271 */
2272 public static void dumpPackageStateStatic(FileDescriptor fd, String packageName) {
2273 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07002274 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08002275 dumpService(pw, fd, Context.ACTIVITY_SERVICE, new String[] {
2276 "-a", "package", packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002277 pw.println();
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08002278 dumpService(pw, fd, "meminfo", new String[] { "--local", packageName });
2279 pw.println();
2280 dumpService(pw, fd, ProcessStats.SERVICE_NAME, new String[] { "-a", packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002281 pw.println();
Dianne Hackborn904a8572013-06-28 18:12:31 -07002282 dumpService(pw, fd, "usagestats", new String[] { "--packages", packageName });
2283 pw.println();
Dianne Hackborn8c841092013-06-24 13:46:13 -07002284 dumpService(pw, fd, "package", new String[] { packageName });
2285 pw.println();
2286 dumpService(pw, fd, BatteryStats.SERVICE_NAME, new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002287 pw.flush();
2288 }
2289
2290 private static void dumpService(PrintWriter pw, FileDescriptor fd, String name, String[] args) {
2291 pw.print("DUMP OF SERVICE "); pw.print(name); pw.println(":");
2292 IBinder service = ServiceManager.checkService(name);
2293 if (service == null) {
2294 pw.println(" (Service not found)");
2295 return;
2296 }
2297 TransferPipe tp = null;
2298 try {
2299 pw.flush();
2300 tp = new TransferPipe();
2301 tp.setBufferPrefix(" ");
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08002302 service.dumpAsync(tp.getWriteFd().getFileDescriptor(), args);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002303 tp.go(fd);
2304 } catch (Throwable e) {
2305 if (tp != null) {
2306 tp.kill();
2307 }
2308 pw.println("Failure dumping service:");
2309 e.printStackTrace(pw);
2310 }
2311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312}