blob: 005baedcdfe2c921602716f8e9398e9128bd7073 [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 Hackbornaec68bb2014-08-20 15:25:13 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070021import android.annotation.SystemApi;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -070022import android.graphics.Canvas;
23import android.graphics.Matrix;
24import android.graphics.Point;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070025import android.os.BatteryStats;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070026import android.os.IBinder;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070027import android.os.ParcelFileDescriptor;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070028
Dianne Hackborn8a0de582013-08-07 15:22:07 -070029import com.android.internal.app.ProcessStats;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070030import com.android.internal.os.TransferPipe;
Dianne Hackborn8c841092013-06-24 13:46:13 -070031import com.android.internal.util.FastPrintWriter;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.ComponentName;
34import android.content.Context;
35import android.content.Intent;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -070036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.ConfigurationInfo;
38import android.content.pm.IPackageDataObserver;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070039import android.content.pm.PackageManager;
Dianne Hackborn41203752012-08-31 14:05:51 -070040import android.content.pm.UserInfo;
Kenny Root5ef44b72011-01-26 17:22:20 -080041import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.graphics.Bitmap;
Winson Chunga449dc02014-05-16 11:15:04 -070043import android.graphics.Color;
Craig Mautner967212c2013-04-13 21:10:58 -070044import android.graphics.Rect;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -070045import android.os.Bundle;
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -070046import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Handler;
48import android.os.Parcel;
49import android.os.Parcelable;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070050import android.os.Process;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070051import android.os.RemoteException;
Peter Visontay8d224ca2011-02-18 16:39:19 +000052import android.os.ServiceManager;
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -070053import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070054import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.text.TextUtils;
Kenny Root5ef44b72011-01-26 17:22:20 -080056import android.util.DisplayMetrics;
Winson Chung48a10a52014-08-27 14:36:51 -070057import android.util.Size;
Dianne Hackbornb3756322011-08-12 13:58:13 -070058import android.util.Slog;
Craig Mautner648f69b2014-09-18 14:16:26 -070059import org.xmlpull.v1.XmlSerializer;
Kenny Root5ef44b72011-01-26 17:22:20 -080060
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070061import java.io.FileDescriptor;
62import java.io.FileOutputStream;
Craig Mautner648f69b2014-09-18 14:16:26 -070063import java.io.IOException;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070064import java.io.PrintWriter;
Winson Chung1147c402014-05-14 11:05:00 -070065import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import java.util.List;
67
68/**
69 * Interact with the overall activities running in the system.
70 */
71public class ActivityManager {
72 private static String TAG = "ActivityManager";
Joe Onorato43a17652011-04-06 19:22:23 -070073 private static boolean localLOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
Dianne Hackborn852975d2014-08-22 17:42:43 -070075 private static int gMaxRecentTasks = -1;
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 private final Context mContext;
78 private final Handler mHandler;
79
Dianne Hackborna4972e92012-03-14 10:38:05 -070080 /**
Scott Maincc2195b2013-10-16 13:57:46 -070081 * <a href="{@docRoot}guide/topics/manifest/meta-data-element.html">{@code
82 * &lt;meta-data>}</a> name for a 'home' Activity that declares a package that is to be
Christopher Tatebd413f62013-09-18 18:31:59 -070083 * uninstalled in lieu of the declaring one. The package named here must be
Scott Maincc2195b2013-10-16 13:57:46 -070084 * signed with the same certificate as the one declaring the {@code &lt;meta-data>}.
Christopher Tatebd413f62013-09-18 18:31:59 -070085 */
86 public static final String META_HOME_ALTERNATE = "android.app.home.alternate";
87
88 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -070089 * Result for IActivityManager.startActivity: trying to start an activity under voice
90 * control when that activity does not support the VOICE category.
91 * @hide
92 */
93 public static final int START_NOT_VOICE_COMPATIBLE = -7;
94
95 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -070096 * Result for IActivityManager.startActivity: an error where the
97 * start had to be canceled.
98 * @hide
99 */
100 public static final int START_CANCELED = -6;
101
102 /**
103 * Result for IActivityManager.startActivity: an error where the
104 * thing being started is not an activity.
105 * @hide
106 */
107 public static final int START_NOT_ACTIVITY = -5;
108
109 /**
110 * Result for IActivityManager.startActivity: an error where the
111 * caller does not have permission to start the activity.
112 * @hide
113 */
114 public static final int START_PERMISSION_DENIED = -4;
115
116 /**
117 * Result for IActivityManager.startActivity: an error where the
118 * caller has requested both to forward a result and to receive
119 * a result.
120 * @hide
121 */
122 public static final int START_FORWARD_AND_REQUEST_CONFLICT = -3;
123
124 /**
125 * Result for IActivityManager.startActivity: an error where the
126 * requested class is not found.
127 * @hide
128 */
129 public static final int START_CLASS_NOT_FOUND = -2;
130
131 /**
132 * Result for IActivityManager.startActivity: an error where the
133 * given Intent could not be resolved to an activity.
134 * @hide
135 */
136 public static final int START_INTENT_NOT_RESOLVED = -1;
137
138 /**
139 * Result for IActivityManaqer.startActivity: the activity was started
140 * successfully as normal.
141 * @hide
142 */
143 public static final int START_SUCCESS = 0;
144
145 /**
146 * Result for IActivityManaqer.startActivity: the caller asked that the Intent not
147 * be executed if it is the recipient, and that is indeed the case.
148 * @hide
149 */
150 public static final int START_RETURN_INTENT_TO_CALLER = 1;
151
152 /**
153 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
154 * a task was simply brought to the foreground.
155 * @hide
156 */
157 public static final int START_TASK_TO_FRONT = 2;
158
159 /**
160 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
161 * the given Intent was given to the existing top activity.
162 * @hide
163 */
164 public static final int START_DELIVERED_TO_TOP = 3;
165
166 /**
167 * Result for IActivityManaqer.startActivity: request was canceled because
168 * app switches are temporarily canceled to ensure the user's last request
169 * (such as pressing home) is performed.
170 * @hide
171 */
172 public static final int START_SWITCHES_CANCELED = 4;
173
174 /**
Craig Mautneraea74a52014-03-08 14:23:10 -0800175 * Result for IActivityManaqer.startActivity: a new activity was attempted to be started
176 * while in Lock Task Mode.
177 * @hide
178 */
179 public static final int START_RETURN_LOCK_TASK_MODE_VIOLATION = 5;
180
181 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700182 * Flag for IActivityManaqer.startActivity: do special start mode where
183 * a new activity is launched only if it is needed.
184 * @hide
185 */
186 public static final int START_FLAG_ONLY_IF_NEEDED = 1<<0;
187
188 /**
189 * Flag for IActivityManaqer.startActivity: launch the app for
190 * debugging.
191 * @hide
192 */
193 public static final int START_FLAG_DEBUG = 1<<1;
194
195 /**
196 * Flag for IActivityManaqer.startActivity: launch the app for
197 * OpenGL tracing.
198 * @hide
199 */
200 public static final int START_FLAG_OPENGL_TRACES = 1<<2;
201
202 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700203 * Result for IActivityManaqer.broadcastIntent: success!
204 * @hide
205 */
206 public static final int BROADCAST_SUCCESS = 0;
207
208 /**
209 * Result for IActivityManaqer.broadcastIntent: attempt to broadcast
210 * a sticky intent without appropriate permission.
211 * @hide
212 */
213 public static final int BROADCAST_STICKY_CANT_HAVE_PERMISSION = -1;
214
215 /**
216 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
217 * for a sendBroadcast operation.
218 * @hide
219 */
220 public static final int INTENT_SENDER_BROADCAST = 1;
221
222 /**
223 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
224 * for a startActivity operation.
225 * @hide
226 */
227 public static final int INTENT_SENDER_ACTIVITY = 2;
228
229 /**
230 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
231 * for an activity result operation.
232 * @hide
233 */
234 public static final int INTENT_SENDER_ACTIVITY_RESULT = 3;
235
236 /**
237 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
238 * for a startService operation.
239 * @hide
240 */
241 public static final int INTENT_SENDER_SERVICE = 4;
242
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700243 /** @hide User operation call: success! */
244 public static final int USER_OP_SUCCESS = 0;
245
246 /** @hide User operation call: given user id is not known. */
247 public static final int USER_OP_UNKNOWN_USER = -1;
248
249 /** @hide User operation call: given user id is the current user, can't be stopped. */
250 public static final int USER_OP_IS_CURRENT = -2;
251
Dianne Hackborna413dc02013-07-12 12:02:55 -0700252 /** @hide Process is a persistent system process. */
253 public static final int PROCESS_STATE_PERSISTENT = 0;
254
255 /** @hide Process is a persistent system process and is doing UI. */
256 public static final int PROCESS_STATE_PERSISTENT_UI = 1;
257
Dianne Hackbornc8230512013-07-13 21:32:12 -0700258 /** @hide Process is hosting the current top activities. Note that this covers
259 * all activities that are visible to the user. */
Dianne Hackborna413dc02013-07-12 12:02:55 -0700260 public static final int PROCESS_STATE_TOP = 2;
261
262 /** @hide Process is important to the user, and something they are aware of. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700263 public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 3;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700264
265 /** @hide Process is important to the user, but not something they are aware of. */
266 public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 4;
267
Dianne Hackborna413dc02013-07-12 12:02:55 -0700268 /** @hide Process is in the background running a backup/restore operation. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700269 public static final int PROCESS_STATE_BACKUP = 5;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700270
Dianne Hackbornc8230512013-07-13 21:32:12 -0700271 /** @hide Process is in the background, but it can't restore its state so we want
272 * to try to avoid killing it. */
273 public static final int PROCESS_STATE_HEAVY_WEIGHT = 6;
274
275 /** @hide Process is in the background running a service. Unlike oom_adj, this level
276 * is used for both the normal running in background state and the executing
277 * operations state. */
Dianne Hackborna413dc02013-07-12 12:02:55 -0700278 public static final int PROCESS_STATE_SERVICE = 7;
279
Dianne Hackbornc8230512013-07-13 21:32:12 -0700280 /** @hide Process is in the background running a receiver. Note that from the
281 * perspective of oom_adj receivers run at a higher foreground level, but for our
282 * prioritization here that is not necessary and putting them below services means
283 * many fewer changes in some process states as they receive broadcasts. */
284 public static final int PROCESS_STATE_RECEIVER = 8;
285
Dianne Hackborna413dc02013-07-12 12:02:55 -0700286 /** @hide Process is in the background but hosts the home activity. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700287 public static final int PROCESS_STATE_HOME = 9;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700288
289 /** @hide Process is in the background but hosts the last shown activity. */
Dianne Hackbornc8230512013-07-13 21:32:12 -0700290 public static final int PROCESS_STATE_LAST_ACTIVITY = 10;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700291
Dianne Hackbornc8230512013-07-13 21:32:12 -0700292 /** @hide Process is being cached for later use and contains activities. */
293 public static final int PROCESS_STATE_CACHED_ACTIVITY = 11;
294
295 /** @hide Process is being cached for later use and is a client of another cached
296 * process that contains activities. */
297 public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 12;
298
299 /** @hide Process is being cached for later use and is empty. */
300 public static final int PROCESS_STATE_CACHED_EMPTY = 13;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700301
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700302 Point mAppTaskThumbnailSize;
303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /*package*/ ActivityManager(Context context, Handler handler) {
305 mContext = context;
306 mHandler = handler;
307 }
308
309 /**
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700310 * Screen compatibility mode: the application most always run in
311 * compatibility mode.
312 * @hide
313 */
314 public static final int COMPAT_MODE_ALWAYS = -1;
315
316 /**
317 * Screen compatibility mode: the application can never run in
318 * compatibility mode.
319 * @hide
320 */
321 public static final int COMPAT_MODE_NEVER = -2;
322
323 /**
324 * Screen compatibility mode: unknown.
325 * @hide
326 */
327 public static final int COMPAT_MODE_UNKNOWN = -3;
328
329 /**
330 * Screen compatibility mode: the application currently has compatibility
331 * mode disabled.
332 * @hide
333 */
334 public static final int COMPAT_MODE_DISABLED = 0;
335
336 /**
337 * Screen compatibility mode: the application currently has compatibility
338 * mode enabled.
339 * @hide
340 */
341 public static final int COMPAT_MODE_ENABLED = 1;
342
343 /**
344 * Screen compatibility mode: request to toggle the application's
345 * compatibility mode.
346 * @hide
347 */
348 public static final int COMPAT_MODE_TOGGLE = 2;
349
350 /** @hide */
351 public int getFrontActivityScreenCompatMode() {
352 try {
353 return ActivityManagerNative.getDefault().getFrontActivityScreenCompatMode();
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 setFrontActivityScreenCompatMode(int mode) {
362 try {
363 ActivityManagerNative.getDefault().setFrontActivityScreenCompatMode(mode);
364 } catch (RemoteException e) {
365 // System dead, we will be dead too soon!
366 }
367 }
368
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700369 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700370 public int getPackageScreenCompatMode(String packageName) {
371 try {
372 return ActivityManagerNative.getDefault().getPackageScreenCompatMode(packageName);
373 } catch (RemoteException e) {
374 // System dead, we will be dead too soon!
375 return 0;
376 }
377 }
378
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700379 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700380 public void setPackageScreenCompatMode(String packageName, int mode) {
381 try {
382 ActivityManagerNative.getDefault().setPackageScreenCompatMode(packageName, mode);
383 } catch (RemoteException e) {
384 // System dead, we will be dead too soon!
385 }
386 }
387
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700388 /** @hide */
389 public boolean getPackageAskScreenCompat(String packageName) {
390 try {
391 return ActivityManagerNative.getDefault().getPackageAskScreenCompat(packageName);
392 } catch (RemoteException e) {
393 // System dead, we will be dead too soon!
394 return false;
395 }
396 }
397
398 /** @hide */
399 public void setPackageAskScreenCompat(String packageName, boolean ask) {
400 try {
401 ActivityManagerNative.getDefault().setPackageAskScreenCompat(packageName, ask);
402 } catch (RemoteException e) {
403 // System dead, we will be dead too soon!
404 }
405 }
406
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700407 /**
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700408 * Return the approximate per-application memory class of the current
409 * device. This gives you an idea of how hard a memory limit you should
410 * impose on your application to let the overall system work best. The
411 * returned value is in megabytes; the baseline Android memory class is
412 * 16 (which happens to be the Java heap limit of those devices); some
413 * device with more memory may return 24 or even higher numbers.
414 */
415 public int getMemoryClass() {
416 return staticGetMemoryClass();
417 }
418
419 /** @hide */
420 static public int staticGetMemoryClass() {
421 // Really brain dead right now -- just take this from the configured
422 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn7ad33c82011-03-08 15:53:25 -0800423 String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", "");
Dianne Hackbornde398512011-01-18 18:45:21 -0800424 if (vmHeapSize != null && !"".equals(vmHeapSize)) {
425 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
426 }
427 return staticGetLargeMemoryClass();
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800428 }
429
430 /**
431 * Return the approximate per-application memory class of the current
432 * device when an application is running with a large heap. This is the
433 * space available for memory-intensive applications; most applications
434 * should not need this amount of memory, and should instead stay with the
435 * {@link #getMemoryClass()} limit. The returned value is in megabytes.
436 * This may be the same size as {@link #getMemoryClass()} on memory
437 * constrained devices, or it may be significantly larger on devices with
438 * a large amount of available RAM.
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800439 *
440 * <p>The is the size of the application's Dalvik heap if it has
441 * specified <code>android:largeHeap="true"</code> in its manifest.
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800442 */
443 public int getLargeMemoryClass() {
444 return staticGetLargeMemoryClass();
445 }
446
447 /** @hide */
448 static public int staticGetLargeMemoryClass() {
449 // Really brain dead right now -- just take this from the configured
450 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700451 String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
Dianne Hackborn852975d2014-08-22 17:42:43 -0700452 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length() - 1));
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700453 }
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700454
455 /**
456 * Returns true if this is a low-RAM device. Exactly whether a device is low-RAM
457 * is ultimately up to the device configuration, but currently it generally means
458 * something in the class of a 512MB device with about a 800x480 or less screen.
459 * This is mostly intended to be used by apps to determine whether they should turn
460 * off certain features that require more RAM.
461 */
462 public boolean isLowRamDevice() {
463 return isLowRamDeviceStatic();
464 }
465
466 /** @hide */
467 public static boolean isLowRamDeviceStatic() {
Christopher Tate2f61f912013-08-20 13:55:50 -0700468 return "true".equals(SystemProperties.get("ro.config.low_ram", "false"));
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700469 }
470
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700471 /**
Dianne Hackbornb3756322011-08-12 13:58:13 -0700472 * Used by persistent processes to determine if they are running on a
473 * higher-end device so should be okay using hardware drawing acceleration
474 * (which tends to consume a lot more RAM).
475 * @hide
476 */
Jeff Brown98365d72012-08-19 20:30:52 -0700477 static public boolean isHighEndGfx() {
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700478 return !isLowRamDeviceStatic() &&
479 !Resources.getSystem().getBoolean(com.android.internal.R.bool.config_avoidGfxAccel);
Dianne Hackborn49d228b32011-08-24 17:37:31 -0700480 }
481
482 /**
Dianne Hackborn852975d2014-08-22 17:42:43 -0700483 * Return the maximum number of recents entries that we will maintain and show.
484 * @hide
485 */
486 static public int getMaxRecentTasksStatic() {
487 if (gMaxRecentTasks < 0) {
488 return gMaxRecentTasks = isLowRamDeviceStatic() ? 50 : 100;
489 }
490 return gMaxRecentTasks;
491 }
492
493 /**
494 * Return the default limit on the number of recents that an app can make.
495 * @hide
496 */
497 static public int getDefaultAppRecentsLimitStatic() {
498 return getMaxRecentTasksStatic() / 6;
499 }
500
501 /**
502 * Return the maximum limit on the number of recents that an app can make.
503 * @hide
504 */
505 static public int getMaxAppRecentsLimitStatic() {
506 return getMaxRecentTasksStatic() / 2;
507 }
508
509 /**
Winson Chung1147c402014-05-14 11:05:00 -0700510 * Information you can set and retrieve about the current activity within the recent task list.
Winson Chung03a9bae2014-05-02 09:56:12 -0700511 */
Winson Chunga449dc02014-05-16 11:15:04 -0700512 public static class TaskDescription implements Parcelable {
Craig Mautner648f69b2014-09-18 14:16:26 -0700513 /** @hide */
514 public static final String ATTR_TASKDESCRIPTION_PREFIX = "task_description_";
515 private static final String ATTR_TASKDESCRIPTIONLABEL =
516 ATTR_TASKDESCRIPTION_PREFIX + "label";
517 private static final String ATTR_TASKDESCRIPTIONCOLOR =
518 ATTR_TASKDESCRIPTION_PREFIX + "color";
519 private static final String ATTR_TASKDESCRIPTIONICONFILENAME =
520 ATTR_TASKDESCRIPTION_PREFIX + "icon_filename";
521
Winson Chunga449dc02014-05-16 11:15:04 -0700522 private String mLabel;
523 private Bitmap mIcon;
Craig Mautner648f69b2014-09-18 14:16:26 -0700524 private String mIconFilename;
Winson Chunga449dc02014-05-16 11:15:04 -0700525 private int mColorPrimary;
Winson Chung03a9bae2014-05-02 09:56:12 -0700526
527 /**
Winson Chunga449dc02014-05-16 11:15:04 -0700528 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -0700529 *
Winson Chunga449dc02014-05-16 11:15:04 -0700530 * @param label A label and description of the current state of this task.
531 * @param icon An icon that represents the current state of this task.
532 * @param colorPrimary A color to override the theme's primary color. This color must be opaque.
Winson Chung03a9bae2014-05-02 09:56:12 -0700533 */
Winson Chunga449dc02014-05-16 11:15:04 -0700534 public TaskDescription(String label, Bitmap icon, int colorPrimary) {
535 if ((colorPrimary != 0) && (Color.alpha(colorPrimary) != 255)) {
536 throw new RuntimeException("A TaskDescription's primary color should be opaque");
537 }
538
539 mLabel = label;
540 mIcon = icon;
541 mColorPrimary = colorPrimary;
Winson Chung03a9bae2014-05-02 09:56:12 -0700542 }
543
Craig Mautner648f69b2014-09-18 14:16:26 -0700544 /** @hide */
545 public TaskDescription(String label, int colorPrimary, String iconFilename) {
546 this(label, null, colorPrimary);
547 mIconFilename = iconFilename;
548 }
549
Winson Chung03a9bae2014-05-02 09:56:12 -0700550 /**
Winson Chunga449dc02014-05-16 11:15:04 -0700551 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -0700552 *
553 * @param label A label and description of the current state of this activity.
554 * @param icon An icon that represents the current state of this activity.
555 */
Winson Chunga449dc02014-05-16 11:15:04 -0700556 public TaskDescription(String label, Bitmap icon) {
Winson Chung03a9bae2014-05-02 09:56:12 -0700557 this(label, icon, 0);
558 }
559
560 /**
Winson Chunga449dc02014-05-16 11:15:04 -0700561 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -0700562 *
563 * @param label A label and description of the current state of this activity.
564 */
Winson Chunga449dc02014-05-16 11:15:04 -0700565 public TaskDescription(String label) {
Winson Chung03a9bae2014-05-02 09:56:12 -0700566 this(label, null, 0);
567 }
568
Winson Chunga449dc02014-05-16 11:15:04 -0700569 /**
570 * Creates an empty TaskDescription.
571 */
572 public TaskDescription() {
Winson Chung03a9bae2014-05-02 09:56:12 -0700573 this(null, null, 0);
574 }
575
Winson Chunga449dc02014-05-16 11:15:04 -0700576 /**
577 * Creates a copy of another TaskDescription.
578 */
579 public TaskDescription(TaskDescription td) {
Craig Mautner648f69b2014-09-18 14:16:26 -0700580 mLabel = td.mLabel;
581 mIcon = td.mIcon;
582 setPrimaryColor(td.mColorPrimary);
583 mIconFilename = td.mIconFilename;
Winson Chunga449dc02014-05-16 11:15:04 -0700584 }
585
586 private TaskDescription(Parcel source) {
Winson Chung03a9bae2014-05-02 09:56:12 -0700587 readFromParcel(source);
588 }
589
590 /**
Winson Chung2cb86c72014-06-25 12:03:30 -0700591 * Sets the label for this task description.
592 * @hide
593 */
594 public void setLabel(String label) {
595 mLabel = label;
596 }
597
598 /**
599 * Sets the primary color for this task description.
600 * @hide
601 */
602 public void setPrimaryColor(int primaryColor) {
Craig Mautner648f69b2014-09-18 14:16:26 -0700603 mColorPrimary = 0xFF000000 | primaryColor;
Winson Chung2cb86c72014-06-25 12:03:30 -0700604 }
605
606 /**
607 * Sets the icon for this task description.
608 * @hide
609 */
610 public void setIcon(Bitmap icon) {
611 mIcon = icon;
612 }
613
614 /**
Craig Mautner648f69b2014-09-18 14:16:26 -0700615 * Moves the icon bitmap reference from an actual Bitmap to a file containing the
616 * bitmap.
617 * @hide
618 */
619 public void setIconFilename(String iconFilename) {
620 mIconFilename = iconFilename;
621 mIcon = null;
622 }
623
624 /**
Winson Chunga449dc02014-05-16 11:15:04 -0700625 * @return The label and description of the current state of this task.
Winson Chung03a9bae2014-05-02 09:56:12 -0700626 */
Winson Chunga449dc02014-05-16 11:15:04 -0700627 public String getLabel() {
628 return mLabel;
629 }
630
631 /**
632 * @return The icon that represents the current state of this task.
633 */
634 public Bitmap getIcon() {
Craig Mautner648f69b2014-09-18 14:16:26 -0700635 if (mIcon != null) {
636 return mIcon;
637 }
638 if (mIconFilename != null) {
639 try {
640 return ActivityManagerNative.getDefault().
641 getTaskDescriptionIcon(mIconFilename);
642 } catch (RemoteException e) {
643 }
644 }
645 return null;
646 }
647
648 /** @hide */
649 public String getIconFilename() {
650 return mIconFilename;
Winson Chunga449dc02014-05-16 11:15:04 -0700651 }
652
653 /**
654 * @return The color override on the theme's primary color.
655 */
656 public int getPrimaryColor() {
657 return mColorPrimary;
Winson Chung03a9bae2014-05-02 09:56:12 -0700658 }
659
Craig Mautner648f69b2014-09-18 14:16:26 -0700660 /** @hide */
661 public void saveToXml(XmlSerializer out) throws IOException {
662 if (mLabel != null) {
663 out.attribute(null, ATTR_TASKDESCRIPTIONLABEL, mLabel);
664 }
665 if (mColorPrimary != 0) {
666 out.attribute(null, ATTR_TASKDESCRIPTIONCOLOR, Integer.toHexString(mColorPrimary));
667 }
668 if (mIconFilename != null) {
669 out.attribute(null, ATTR_TASKDESCRIPTIONICONFILENAME, mIconFilename);
670 }
671 }
672
673 /** @hide */
674 public void restoreFromXml(String attrName, String attrValue) {
675 if (ATTR_TASKDESCRIPTIONLABEL.equals(attrName)) {
676 setLabel(attrValue);
677 } else if (ATTR_TASKDESCRIPTIONCOLOR.equals(attrName)) {
678 setPrimaryColor((int) Long.parseLong(attrValue, 16));
679 } else if (ATTR_TASKDESCRIPTIONICONFILENAME.equals(attrName)) {
680 setIconFilename(attrValue);
681 }
682 }
683
Winson Chung03a9bae2014-05-02 09:56:12 -0700684 @Override
685 public int describeContents() {
686 return 0;
687 }
688
689 @Override
690 public void writeToParcel(Parcel dest, int flags) {
Winson Chunga449dc02014-05-16 11:15:04 -0700691 if (mLabel == null) {
Winson Chung03a9bae2014-05-02 09:56:12 -0700692 dest.writeInt(0);
693 } else {
694 dest.writeInt(1);
Winson Chunga449dc02014-05-16 11:15:04 -0700695 dest.writeString(mLabel);
Winson Chung03a9bae2014-05-02 09:56:12 -0700696 }
Winson Chunga449dc02014-05-16 11:15:04 -0700697 if (mIcon == null) {
698 dest.writeInt(0);
699 } else {
700 dest.writeInt(1);
701 mIcon.writeToParcel(dest, 0);
702 }
703 dest.writeInt(mColorPrimary);
Craig Mautner648f69b2014-09-18 14:16:26 -0700704 if (mIconFilename == null) {
705 dest.writeInt(0);
706 } else {
707 dest.writeInt(1);
708 dest.writeString(mIconFilename);
709 }
Winson Chung03a9bae2014-05-02 09:56:12 -0700710 }
711
712 public void readFromParcel(Parcel source) {
Winson Chunga449dc02014-05-16 11:15:04 -0700713 mLabel = source.readInt() > 0 ? source.readString() : null;
714 mIcon = source.readInt() > 0 ? Bitmap.CREATOR.createFromParcel(source) : null;
715 mColorPrimary = source.readInt();
Craig Mautner648f69b2014-09-18 14:16:26 -0700716 mIconFilename = source.readInt() > 0 ? source.readString() : null;
Winson Chung03a9bae2014-05-02 09:56:12 -0700717 }
718
Winson Chunga449dc02014-05-16 11:15:04 -0700719 public static final Creator<TaskDescription> CREATOR
720 = new Creator<TaskDescription>() {
721 public TaskDescription createFromParcel(Parcel source) {
722 return new TaskDescription(source);
Winson Chung03a9bae2014-05-02 09:56:12 -0700723 }
Winson Chunga449dc02014-05-16 11:15:04 -0700724 public TaskDescription[] newArray(int size) {
725 return new TaskDescription[size];
Winson Chung03a9bae2014-05-02 09:56:12 -0700726 }
727 };
728
729 @Override
730 public String toString() {
Winson Chunga449dc02014-05-16 11:15:04 -0700731 return "TaskDescription Label: " + mLabel + " Icon: " + mIcon +
732 " colorPrimary: " + mColorPrimary;
Winson Chung03a9bae2014-05-02 09:56:12 -0700733 }
734 }
735
736 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 * Information you can retrieve about tasks that the user has most recently
738 * started or visited.
739 */
740 public static class RecentTaskInfo implements Parcelable {
741 /**
742 * If this task is currently running, this is the identifier for it.
743 * If it is not running, this will be -1.
744 */
745 public int id;
746
747 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -0800748 * The true identifier of this task, valid even if it is not running.
749 */
750 public int persistentId;
751
752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 * The original Intent used to launch the task. You can use this
754 * Intent to re-launch the task (if it is no longer running) or bring
755 * the current task to the front.
756 */
757 public Intent baseIntent;
758
759 /**
760 * If this task was started from an alias, this is the actual
761 * activity component that was initially started; the component of
762 * the baseIntent in this case is the name of the actual activity
763 * implementation that the alias referred to. Otherwise, this is null.
764 */
765 public ComponentName origActivity;
Dianne Hackbornd2835932010-12-13 16:28:46 -0800766
767 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -0800768 * Description of the task's last state.
769 */
770 public CharSequence description;
Craig Mautner6d90fed2013-05-22 15:03:10 -0700771
772 /**
773 * The id of the ActivityStack this Task was on most recently.
Craig Mautner27030522013-08-26 12:25:36 -0700774 * @hide
Craig Mautner6d90fed2013-05-22 15:03:10 -0700775 */
776 public int stackId;
777
Kenny Guy82326a92014-03-17 17:16:06 +0000778 /**
Craig Mautner2fbd7542014-03-21 09:34:07 -0700779 * The id of the user the task was running as.
Kenny Guy82326a92014-03-17 17:16:06 +0000780 * @hide
781 */
782 public int userId;
783
Craig Mautner2fbd7542014-03-21 09:34:07 -0700784 /**
Winson Chungffa2ec62014-07-03 15:54:42 -0700785 * The first time this task was active.
786 * @hide
787 */
788 public long firstActiveTime;
789
790 /**
Winson Chungf1fbd772014-06-24 18:06:58 -0700791 * The last time this task was active.
792 * @hide
793 */
794 public long lastActiveTime;
795
796 /**
Winson Chung03a9bae2014-05-02 09:56:12 -0700797 * The recent activity values for the highest activity in the stack to have set the values.
Winson Chunga449dc02014-05-16 11:15:04 -0700798 * {@link Activity#setTaskDescription(android.app.ActivityManager.TaskDescription)}.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700799 */
Winson Chunga449dc02014-05-16 11:15:04 -0700800 public TaskDescription taskDescription;
Craig Mautner2fbd7542014-03-21 09:34:07 -0700801
Craig Mautnera228ae92014-07-09 05:44:55 -0700802 /**
803 * Task affiliation for grouping with other tasks.
Craig Mautnera228ae92014-07-09 05:44:55 -0700804 */
805 public int affiliatedTaskId;
806
Winson Chungec396d62014-08-06 17:08:00 -0700807 /**
808 * Task affiliation color of the source task with the affiliated task id.
809 *
810 * @hide
811 */
812 public int affiliatedTaskColor;
813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 public RecentTaskInfo() {
815 }
816
Craig Mautner6d90fed2013-05-22 15:03:10 -0700817 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 public int describeContents() {
819 return 0;
820 }
821
Craig Mautner6d90fed2013-05-22 15:03:10 -0700822 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 public void writeToParcel(Parcel dest, int flags) {
824 dest.writeInt(id);
Dianne Hackbornd94df452011-02-16 18:53:31 -0800825 dest.writeInt(persistentId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 if (baseIntent != null) {
827 dest.writeInt(1);
828 baseIntent.writeToParcel(dest, 0);
829 } else {
830 dest.writeInt(0);
831 }
832 ComponentName.writeToParcel(origActivity, dest);
Dianne Hackbornd2835932010-12-13 16:28:46 -0800833 TextUtils.writeToParcel(description, dest,
834 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
Winson Chunga449dc02014-05-16 11:15:04 -0700835 if (taskDescription != null) {
Craig Mautner2fbd7542014-03-21 09:34:07 -0700836 dest.writeInt(1);
Winson Chunga449dc02014-05-16 11:15:04 -0700837 taskDescription.writeToParcel(dest, 0);
Winson Chung03a9bae2014-05-02 09:56:12 -0700838 } else {
839 dest.writeInt(0);
Craig Mautner2fbd7542014-03-21 09:34:07 -0700840 }
Craig Mautner6d90fed2013-05-22 15:03:10 -0700841 dest.writeInt(stackId);
Kenny Guy82326a92014-03-17 17:16:06 +0000842 dest.writeInt(userId);
Winson Chungffa2ec62014-07-03 15:54:42 -0700843 dest.writeLong(firstActiveTime);
Winson Chungf1fbd772014-06-24 18:06:58 -0700844 dest.writeLong(lastActiveTime);
Craig Mautnera228ae92014-07-09 05:44:55 -0700845 dest.writeInt(affiliatedTaskId);
Winson Chungec396d62014-08-06 17:08:00 -0700846 dest.writeInt(affiliatedTaskColor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 }
848
849 public void readFromParcel(Parcel source) {
850 id = source.readInt();
Dianne Hackbornd94df452011-02-16 18:53:31 -0800851 persistentId = source.readInt();
Craig Mautner688b5102014-03-27 16:55:03 -0700852 baseIntent = source.readInt() > 0 ? Intent.CREATOR.createFromParcel(source) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 origActivity = ComponentName.readFromParcel(source);
Dianne Hackbornd2835932010-12-13 16:28:46 -0800854 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
Winson Chunga449dc02014-05-16 11:15:04 -0700855 taskDescription = source.readInt() > 0 ?
856 TaskDescription.CREATOR.createFromParcel(source) : null;
Craig Mautner6d90fed2013-05-22 15:03:10 -0700857 stackId = source.readInt();
Kenny Guy82326a92014-03-17 17:16:06 +0000858 userId = source.readInt();
Winson Chungffa2ec62014-07-03 15:54:42 -0700859 firstActiveTime = source.readLong();
Winson Chungf1fbd772014-06-24 18:06:58 -0700860 lastActiveTime = source.readLong();
Craig Mautnera228ae92014-07-09 05:44:55 -0700861 affiliatedTaskId = source.readInt();
Winson Chungec396d62014-08-06 17:08:00 -0700862 affiliatedTaskColor = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Craig Mautner6d90fed2013-05-22 15:03:10 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 public static final Creator<RecentTaskInfo> CREATOR
866 = new Creator<RecentTaskInfo>() {
867 public RecentTaskInfo createFromParcel(Parcel source) {
868 return new RecentTaskInfo(source);
869 }
870 public RecentTaskInfo[] newArray(int size) {
871 return new RecentTaskInfo[size];
872 }
873 };
874
875 private RecentTaskInfo(Parcel source) {
876 readFromParcel(source);
877 }
878 }
879
880 /**
881 * Flag for use with {@link #getRecentTasks}: return all tasks, even those
882 * that have set their
883 * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag.
884 */
885 public static final int RECENT_WITH_EXCLUDED = 0x0001;
Kenny Guy82326a92014-03-17 17:16:06 +0000886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -0800888 * Provides a list that does not contain any
Dianne Hackborn53d9264d2010-04-13 12:49:14 -0700889 * recent tasks that currently are not available to the user.
890 */
891 public static final int RECENT_IGNORE_UNAVAILABLE = 0x0002;
Dianne Hackbornd2835932010-12-13 16:28:46 -0800892
893 /**
Kenny Guy2a764942014-04-02 13:29:20 +0100894 * Provides a list that contains recent tasks for all
895 * profiles of a user.
Kenny Guy82326a92014-03-17 17:16:06 +0000896 * @hide
897 */
Kenny Guy2a764942014-04-02 13:29:20 +0100898 public static final int RECENT_INCLUDE_PROFILES = 0x0004;
Kenny Guy82326a92014-03-17 17:16:06 +0000899
900 /**
Winson Chunga4ccb862014-08-22 15:26:27 -0700901 * Ignores all tasks that are on the home stack.
902 * @hide
903 */
904 public static final int RECENT_IGNORE_HOME_STACK_TASKS = 0x0008;
905
906 /**
Dianne Hackborna25a8b22014-05-30 12:30:35 -0700907 * <p></p>Return a list of the tasks that the user has recently launched, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 * the most recent being first and older ones after in order.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -0700909 *
910 * <p><b>Note: this method is only intended for debugging and presenting
911 * task management user interfaces</b>. This should never be used for
912 * core logic in an application, such as deciding between different
913 * behaviors based on the information found here. Such uses are
914 * <em>not</em> supported, and will likely break in the future. For
915 * example, if multiple applications can be actively running at the
916 * same time, assumptions made about the meaning of the data here for
917 * purposes of control flow will be incorrect.</p>
918 *
Dianne Hackborna25a8b22014-05-30 12:30:35 -0700919 * @deprecated As of {@link android.os.Build.VERSION_CODES#L}, this method is
920 * no longer available to third party applications: as the introduction of
921 * document-centric recents means
922 * it can leak personal information to the caller. For backwards compatibility,
923 * it will still return a small subset of its data: at least the caller's
924 * own tasks (though see {@link #getAppTasks()} for the correct supported
925 * way to retrieve that information), and possibly some other tasks
926 * such as home that are known to not be sensitive.
927 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 * @param maxNum The maximum number of entries to return in the list. The
929 * actual number returned may be smaller, depending on how many tasks the
930 * user has started and the maximum number the system can remember.
Dianne Hackbornd2835932010-12-13 16:28:46 -0800931 * @param flags Information about what to return. May be any combination
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800932 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 *
934 * @return Returns a list of RecentTaskInfo records describing each of
935 * the recent tasks.
936 *
937 * @throws SecurityException Throws SecurityException if the caller does
938 * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
939 */
Dianne Hackborna25a8b22014-05-30 12:30:35 -0700940 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags)
942 throws SecurityException {
943 try {
944 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700945 flags, UserHandle.myUserId());
Amith Yamasani82644082012-08-03 13:09:11 -0700946 } catch (RemoteException e) {
947 // System dead, we will be dead too soon!
948 return null;
949 }
950 }
951
952 /**
953 * Same as {@link #getRecentTasks(int, int)} but returns the recent tasks for a
954 * specific user. It requires holding
955 * the {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission.
956 * @param maxNum The maximum number of entries to return in the list. The
957 * actual number returned may be smaller, depending on how many tasks the
958 * user has started and the maximum number the system can remember.
959 * @param flags Information about what to return. May be any combination
960 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
961 *
962 * @return Returns a list of RecentTaskInfo records describing each of
963 * the recent tasks.
964 *
965 * @throws SecurityException Throws SecurityException if the caller does
966 * not hold the {@link android.Manifest.permission#GET_TASKS} or the
967 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permissions.
968 * @hide
969 */
970 public List<RecentTaskInfo> getRecentTasksForUser(int maxNum, int flags, int userId)
971 throws SecurityException {
972 try {
973 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
974 flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 } catch (RemoteException e) {
976 // System dead, we will be dead too soon!
977 return null;
978 }
979 }
980
981 /**
982 * Information you can retrieve about a particular task that is currently
983 * "running" in the system. Note that a running task does not mean the
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700984 * given task actually has a process it is actively running in; it simply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 * means that the user has gone to it and never closed it, but currently
986 * the system may have killed its process and is only holding on to its
987 * last state in order to restart it when the user returns.
988 */
989 public static class RunningTaskInfo implements Parcelable {
990 /**
991 * A unique identifier for this task.
992 */
993 public int id;
994
995 /**
996 * The component launched as the first activity in the task. This can
997 * be considered the "application" of this task.
998 */
999 public ComponentName baseActivity;
1000
1001 /**
1002 * The activity component at the top of the history stack of the task.
1003 * This is what the user is currently doing.
1004 */
1005 public ComponentName topActivity;
1006
1007 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -08001008 * Thumbnail representation of the task's current state. Currently
1009 * always null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 */
1011 public Bitmap thumbnail;
1012
1013 /**
1014 * Description of the task's current state.
1015 */
1016 public CharSequence description;
1017
1018 /**
1019 * Number of activities in this task.
1020 */
1021 public int numActivities;
1022
1023 /**
1024 * Number of activities that are currently running (not stopped
1025 * and persisted) in this task.
1026 */
1027 public int numRunning;
1028
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001029 /**
1030 * Last time task was run. For sorting.
1031 * @hide
1032 */
1033 public long lastActiveTime;
1034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 public RunningTaskInfo() {
1036 }
1037
1038 public int describeContents() {
1039 return 0;
1040 }
1041
1042 public void writeToParcel(Parcel dest, int flags) {
1043 dest.writeInt(id);
1044 ComponentName.writeToParcel(baseActivity, dest);
1045 ComponentName.writeToParcel(topActivity, dest);
1046 if (thumbnail != null) {
1047 dest.writeInt(1);
1048 thumbnail.writeToParcel(dest, 0);
1049 } else {
1050 dest.writeInt(0);
1051 }
1052 TextUtils.writeToParcel(description, dest,
1053 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1054 dest.writeInt(numActivities);
1055 dest.writeInt(numRunning);
1056 }
1057
1058 public void readFromParcel(Parcel source) {
1059 id = source.readInt();
1060 baseActivity = ComponentName.readFromParcel(source);
1061 topActivity = ComponentName.readFromParcel(source);
1062 if (source.readInt() != 0) {
1063 thumbnail = Bitmap.CREATOR.createFromParcel(source);
1064 } else {
1065 thumbnail = null;
1066 }
1067 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
1068 numActivities = source.readInt();
1069 numRunning = source.readInt();
1070 }
1071
1072 public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() {
1073 public RunningTaskInfo createFromParcel(Parcel source) {
1074 return new RunningTaskInfo(source);
1075 }
1076 public RunningTaskInfo[] newArray(int size) {
1077 return new RunningTaskInfo[size];
1078 }
1079 };
1080
1081 private RunningTaskInfo(Parcel source) {
1082 readFromParcel(source);
1083 }
1084 }
Winson Chung1147c402014-05-14 11:05:00 -07001085
1086 /**
1087 * Get the list of tasks associated with the calling application.
1088 *
1089 * @return The list of tasks associated with the application making this call.
1090 * @throws SecurityException
1091 */
1092 public List<ActivityManager.AppTask> getAppTasks() {
1093 ArrayList<AppTask> tasks = new ArrayList<AppTask>();
1094 List<IAppTask> appTasks;
1095 try {
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001096 appTasks = ActivityManagerNative.getDefault().getAppTasks(mContext.getPackageName());
Winson Chung1147c402014-05-14 11:05:00 -07001097 } catch (RemoteException e) {
1098 // System dead, we will be dead too soon!
1099 return null;
1100 }
1101 int numAppTasks = appTasks.size();
1102 for (int i = 0; i < numAppTasks; i++) {
1103 tasks.add(new AppTask(appTasks.get(i)));
1104 }
1105 return tasks;
1106 }
1107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 /**
Winson Chung48a10a52014-08-27 14:36:51 -07001109 * Return the current design dimensions for {@link AppTask} thumbnails, for use
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001110 * with {@link #addAppTask}.
1111 */
Winson Chung48a10a52014-08-27 14:36:51 -07001112 public Size getAppTaskThumbnailSize() {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001113 synchronized (this) {
1114 ensureAppTaskThumbnailSizeLocked();
Winson Chung48a10a52014-08-27 14:36:51 -07001115 return new Size(mAppTaskThumbnailSize.x, mAppTaskThumbnailSize.y);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001116 }
1117 }
1118
1119 private void ensureAppTaskThumbnailSizeLocked() {
1120 if (mAppTaskThumbnailSize == null) {
1121 try {
1122 mAppTaskThumbnailSize = ActivityManagerNative.getDefault().getAppTaskThumbnailSize();
1123 } catch (RemoteException e) {
1124 throw new IllegalStateException("System dead?", e);
1125 }
1126 }
1127 }
1128
1129 /**
1130 * Add a new {@link AppTask} for the calling application. This will create a new
1131 * recents entry that is added to the <b>end</b> of all existing recents.
1132 *
1133 * @param activity The activity that is adding the entry. This is used to help determine
1134 * the context that the new recents entry will be in.
1135 * @param intent The Intent that describes the recents entry. This is the same Intent that
1136 * you would have used to launch the activity for it. In generally you will want to set
1137 * both {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT} and
1138 * {@link Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS}; the latter is required since this recents
1139 * entry will exist without an activity, so it doesn't make sense to not retain it when
1140 * its activity disappears. The given Intent here also must have an explicit ComponentName
1141 * set on it.
1142 * @param description Optional additional description information.
1143 * @param thumbnail Thumbnail to use for the recents entry. Should be the size given by
Winson Chung48a10a52014-08-27 14:36:51 -07001144 * {@link #getAppTaskThumbnailSize()}. If the bitmap is not that exact size, it will be
1145 * recreated in your process, probably in a way you don't like, before the recents entry
1146 * is added.
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001147 *
1148 * @return Returns the task id of the newly added app task, or -1 if the add failed. The
1149 * most likely cause of failure is that there is no more room for more tasks for your app.
1150 */
1151 public int addAppTask(@NonNull Activity activity, @NonNull Intent intent,
1152 @Nullable TaskDescription description, @NonNull Bitmap thumbnail) {
1153 Point size;
1154 synchronized (this) {
1155 ensureAppTaskThumbnailSizeLocked();
1156 size = mAppTaskThumbnailSize;
1157 }
1158 final int tw = thumbnail.getWidth();
1159 final int th = thumbnail.getHeight();
1160 if (tw != size.x || th != size.y) {
1161 Bitmap bm = Bitmap.createBitmap(size.x, size.y, thumbnail.getConfig());
1162
1163 // Use ScaleType.CENTER_CROP, except we leave the top edge at the top.
1164 float scale;
1165 float dx = 0, dy = 0;
1166 if (tw * size.x > size.y * th) {
1167 scale = (float) size.x / (float) th;
1168 dx = (size.y - tw * scale) * 0.5f;
1169 } else {
1170 scale = (float) size.y / (float) tw;
1171 dy = (size.x - th * scale) * 0.5f;
1172 }
1173 Matrix matrix = new Matrix();
1174 matrix.setScale(scale, scale);
1175 matrix.postTranslate((int) (dx + 0.5f), 0);
1176
1177 Canvas canvas = new Canvas(bm);
1178 canvas.drawBitmap(thumbnail, matrix, null);
1179 canvas.setBitmap(null);
1180
1181 thumbnail = bm;
1182 }
1183 if (description == null) {
1184 description = new TaskDescription();
1185 }
1186 try {
1187 return ActivityManagerNative.getDefault().addAppTask(activity.getActivityToken(),
1188 intent, description, thumbnail);
1189 } catch (RemoteException e) {
1190 throw new IllegalStateException("System dead?", e);
1191 }
1192 }
1193
1194 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 * Return a list of the tasks that are currently running, with
1196 * the most recent being first and older ones after in order. Note that
1197 * "running" does not mean any of the task's code is currently loaded or
1198 * activity -- the task may have been frozen by the system, so that it
1199 * can be restarted in its previous state when next brought to the
1200 * foreground.
Jim Miller0b2a6d02010-07-13 18:01:29 -07001201 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001202 * <p><b>Note: this method is only intended for debugging and presenting
1203 * task management user interfaces</b>. This should never be used for
1204 * core logic in an application, such as deciding between different
1205 * behaviors based on the information found here. Such uses are
1206 * <em>not</em> supported, and will likely break in the future. For
1207 * example, if multiple applications can be actively running at the
1208 * same time, assumptions made about the meaning of the data here for
1209 * purposes of control flow will be incorrect.</p>
1210 *
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001211 * @deprecated As of {@link android.os.Build.VERSION_CODES#L}, this method
1212 * is no longer available to third party
1213 * applications: the introduction of document-centric recents means
1214 * it can leak person information to the caller. For backwards compatibility,
1215 * it will still retu rn a small subset of its data: at least the caller's
1216 * own tasks, and possibly some other tasks
1217 * such as home that are known to not be sensitive.
1218 *
Jim Miller0b2a6d02010-07-13 18:01:29 -07001219 * @param maxNum The maximum number of entries to return in the list. The
1220 * actual number returned may be smaller, depending on how many tasks the
1221 * user has started.
1222 *
1223 * @return Returns a list of RunningTaskInfo records describing each of
1224 * the running tasks.
1225 *
1226 * @throws SecurityException Throws SecurityException if the caller does
1227 * not hold the {@link android.Manifest.permission#GET_TASKS} permission.
1228 */
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001229 @Deprecated
Jim Miller0b2a6d02010-07-13 18:01:29 -07001230 public List<RunningTaskInfo> getRunningTasks(int maxNum)
1231 throws SecurityException {
Dianne Hackborn09233282014-04-30 11:33:59 -07001232 try {
1233 return ActivityManagerNative.getDefault().getTasks(maxNum, 0);
1234 } catch (RemoteException e) {
1235 // System dead, we will be dead too soon!
1236 return null;
1237 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07001238 }
1239
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001240 /**
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001241 * If set, the process of the root activity of the task will be killed
1242 * as part of removing the task.
1243 * @hide
1244 */
1245 public static final int REMOVE_TASK_KILL_PROCESS = 0x0001;
1246
1247 /**
1248 * Completely remove the given task.
1249 *
1250 * @param taskId Identifier of the task to be removed.
1251 * @param flags Additional operational flags. May be 0 or
1252 * {@link #REMOVE_TASK_KILL_PROCESS}.
1253 * @return Returns true if the given task was found and removed.
1254 *
1255 * @hide
1256 */
1257 public boolean removeTask(int taskId, int flags)
1258 throws SecurityException {
1259 try {
1260 return ActivityManagerNative.getDefault().removeTask(taskId, flags);
1261 } catch (RemoteException e) {
1262 // System dead, we will be dead too soon!
1263 return false;
1264 }
1265 }
1266
Dianne Hackbornd94df452011-02-16 18:53:31 -08001267 /** @hide */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001268 public static class TaskThumbnail implements Parcelable {
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001269 public Bitmap mainThumbnail;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001270 public ParcelFileDescriptor thumbnailFileDescriptor;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001271
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001272 public TaskThumbnail() {
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001273 }
1274
1275 public int describeContents() {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001276 if (thumbnailFileDescriptor != null) {
1277 return thumbnailFileDescriptor.describeContents();
1278 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001279 return 0;
1280 }
1281
1282 public void writeToParcel(Parcel dest, int flags) {
1283 if (mainThumbnail != null) {
1284 dest.writeInt(1);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001285 mainThumbnail.writeToParcel(dest, flags);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001286 } else {
1287 dest.writeInt(0);
1288 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001289 if (thumbnailFileDescriptor != null) {
1290 dest.writeInt(1);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001291 thumbnailFileDescriptor.writeToParcel(dest, flags);
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001292 } else {
1293 dest.writeInt(0);
1294 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001295 }
1296
1297 public void readFromParcel(Parcel source) {
1298 if (source.readInt() != 0) {
1299 mainThumbnail = Bitmap.CREATOR.createFromParcel(source);
1300 } else {
1301 mainThumbnail = null;
1302 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001303 if (source.readInt() != 0) {
1304 thumbnailFileDescriptor = ParcelFileDescriptor.CREATOR.createFromParcel(source);
1305 } else {
1306 thumbnailFileDescriptor = null;
1307 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001308 }
1309
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001310 public static final Creator<TaskThumbnail> CREATOR = new Creator<TaskThumbnail>() {
1311 public TaskThumbnail createFromParcel(Parcel source) {
1312 return new TaskThumbnail(source);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001313 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001314 public TaskThumbnail[] newArray(int size) {
1315 return new TaskThumbnail[size];
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001316 }
1317 };
1318
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001319 private TaskThumbnail(Parcel source) {
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001320 readFromParcel(source);
1321 }
1322 }
1323
1324 /** @hide */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001325 public TaskThumbnail getTaskThumbnail(int id) throws SecurityException {
Dianne Hackbornd94df452011-02-16 18:53:31 -08001326 try {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001327 return ActivityManagerNative.getDefault().getTaskThumbnail(id);
Dianne Hackborn15491c62012-09-19 10:59:14 -07001328 } catch (RemoteException e) {
1329 // System dead, we will be dead too soon!
1330 return null;
1331 }
1332 }
1333
Winson Chung303e1ff2014-03-07 15:06:19 -08001334 /** @hide */
1335 public boolean isInHomeStack(int taskId) {
1336 try {
1337 return ActivityManagerNative.getDefault().isInHomeStack(taskId);
1338 } catch (RemoteException e) {
1339 // System dead, we will be dead too soon!
1340 return false;
1341 }
1342 }
1343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001345 * Flag for {@link #moveTaskToFront(int, int)}: also move the "home"
1346 * activity along with the task, so it is positioned immediately behind
1347 * the task.
1348 */
1349 public static final int MOVE_TASK_WITH_HOME = 0x00000001;
1350
1351 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -08001352 * Flag for {@link #moveTaskToFront(int, int)}: don't count this as a
1353 * user-instigated action, so the current activity will not receive a
1354 * hint that the user is leaving.
1355 */
1356 public static final int MOVE_TASK_NO_USER_ACTION = 0x00000002;
1357
1358 /**
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001359 * Equivalent to calling {@link #moveTaskToFront(int, int, Bundle)}
1360 * with a null options argument.
1361 *
1362 * @param taskId The identifier of the task to be moved, as found in
1363 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
1364 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -07001365 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001366 */
1367 public void moveTaskToFront(int taskId, int flags) {
1368 moveTaskToFront(taskId, flags, null);
1369 }
1370
1371 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001372 * Ask that the task associated with a given task ID be moved to the
1373 * front of the stack, so it is now visible to the user. Requires that
1374 * the caller hold permission {@link android.Manifest.permission#REORDER_TASKS}
1375 * or a SecurityException will be thrown.
1376 *
1377 * @param taskId The identifier of the task to be moved, as found in
1378 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
1379 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -07001380 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001381 * @param options Additional options for the operation, either null or
1382 * as per {@link Context#startActivity(Intent, android.os.Bundle)
1383 * Context.startActivity(Intent, Bundle)}.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001384 */
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001385 public void moveTaskToFront(int taskId, int flags, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001386 try {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001387 ActivityManagerNative.getDefault().moveTaskToFront(taskId, flags, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001388 } catch (RemoteException e) {
1389 // System dead, we will be dead too soon!
1390 }
1391 }
1392
1393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 * Information you can retrieve about a particular Service that is
1395 * currently running in the system.
1396 */
1397 public static class RunningServiceInfo implements Parcelable {
1398 /**
1399 * The service component.
1400 */
1401 public ComponentName service;
1402
1403 /**
1404 * If non-zero, this is the process the service is running in.
1405 */
1406 public int pid;
1407
1408 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001409 * The UID that owns this service.
1410 */
1411 public int uid;
1412
1413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 * The name of the process this service runs in.
1415 */
1416 public String process;
1417
1418 /**
1419 * Set to true if the service has asked to run as a foreground process.
1420 */
1421 public boolean foreground;
1422
1423 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001424 * The time when the service was first made active, either by someone
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001425 * starting or binding to it. This
1426 * is in units of {@link android.os.SystemClock#elapsedRealtime()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 */
1428 public long activeSince;
1429
1430 /**
1431 * Set to true if this service has been explicitly started.
1432 */
1433 public boolean started;
1434
1435 /**
1436 * Number of clients connected to the service.
1437 */
1438 public int clientCount;
1439
1440 /**
1441 * Number of times the service's process has crashed while the service
1442 * is running.
1443 */
1444 public int crashCount;
1445
1446 /**
1447 * The time when there was last activity in the service (either
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001448 * explicit requests to start it or clients binding to it). This
1449 * is in units of {@link android.os.SystemClock#uptimeMillis()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 */
1451 public long lastActivityTime;
1452
1453 /**
1454 * If non-zero, this service is not currently running, but scheduled to
1455 * restart at the given time.
1456 */
1457 public long restarting;
1458
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001459 /**
1460 * Bit for {@link #flags}: set if this service has been
1461 * explicitly started.
1462 */
1463 public static final int FLAG_STARTED = 1<<0;
1464
1465 /**
1466 * Bit for {@link #flags}: set if the service has asked to
1467 * run as a foreground process.
1468 */
1469 public static final int FLAG_FOREGROUND = 1<<1;
1470
1471 /**
1472 * Bit for {@link #flags): set if the service is running in a
1473 * core system process.
1474 */
1475 public static final int FLAG_SYSTEM_PROCESS = 1<<2;
1476
1477 /**
1478 * Bit for {@link #flags): set if the service is running in a
1479 * persistent process.
1480 */
1481 public static final int FLAG_PERSISTENT_PROCESS = 1<<3;
1482
1483 /**
1484 * Running flags.
1485 */
1486 public int flags;
1487
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001488 /**
1489 * For special services that are bound to by system code, this is
1490 * the package that holds the binding.
1491 */
1492 public String clientPackage;
1493
1494 /**
1495 * For special services that are bound to by system code, this is
1496 * a string resource providing a user-visible label for who the
1497 * client is.
1498 */
1499 public int clientLabel;
1500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 public RunningServiceInfo() {
1502 }
1503
1504 public int describeContents() {
1505 return 0;
1506 }
1507
1508 public void writeToParcel(Parcel dest, int flags) {
1509 ComponentName.writeToParcel(service, dest);
1510 dest.writeInt(pid);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001511 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 dest.writeString(process);
1513 dest.writeInt(foreground ? 1 : 0);
1514 dest.writeLong(activeSince);
1515 dest.writeInt(started ? 1 : 0);
1516 dest.writeInt(clientCount);
1517 dest.writeInt(crashCount);
1518 dest.writeLong(lastActivityTime);
1519 dest.writeLong(restarting);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001520 dest.writeInt(this.flags);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001521 dest.writeString(clientPackage);
1522 dest.writeInt(clientLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
1524
1525 public void readFromParcel(Parcel source) {
1526 service = ComponentName.readFromParcel(source);
1527 pid = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001528 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 process = source.readString();
1530 foreground = source.readInt() != 0;
1531 activeSince = source.readLong();
1532 started = source.readInt() != 0;
1533 clientCount = source.readInt();
1534 crashCount = source.readInt();
1535 lastActivityTime = source.readLong();
1536 restarting = source.readLong();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001537 flags = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001538 clientPackage = source.readString();
1539 clientLabel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
1541
1542 public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() {
1543 public RunningServiceInfo createFromParcel(Parcel source) {
1544 return new RunningServiceInfo(source);
1545 }
1546 public RunningServiceInfo[] newArray(int size) {
1547 return new RunningServiceInfo[size];
1548 }
1549 };
1550
1551 private RunningServiceInfo(Parcel source) {
1552 readFromParcel(source);
1553 }
1554 }
1555
1556 /**
1557 * Return a list of the services that are currently running.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001558 *
1559 * <p><b>Note: this method is only intended for debugging or implementing
1560 * service management type user interfaces.</b></p>
1561 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 * @param maxNum The maximum number of entries to return in the list. The
1563 * actual number returned may be smaller, depending on how many services
1564 * are running.
1565 *
1566 * @return Returns a list of RunningServiceInfo records describing each of
1567 * the running tasks.
1568 */
1569 public List<RunningServiceInfo> getRunningServices(int maxNum)
1570 throws SecurityException {
1571 try {
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001572 return ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 .getServices(maxNum, 0);
1574 } catch (RemoteException e) {
1575 // System dead, we will be dead too soon!
1576 return null;
1577 }
1578 }
1579
1580 /**
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001581 * Returns a PendingIntent you can start to show a control panel for the
1582 * given running service. If the service does not have a control panel,
1583 * null is returned.
1584 */
1585 public PendingIntent getRunningServiceControlPanel(ComponentName service)
1586 throws SecurityException {
1587 try {
1588 return ActivityManagerNative.getDefault()
1589 .getRunningServiceControlPanel(service);
1590 } catch (RemoteException e) {
1591 // System dead, we will be dead too soon!
1592 return null;
1593 }
1594 }
1595
1596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 * Information you can retrieve about the available memory through
1598 * {@link ActivityManager#getMemoryInfo}.
1599 */
1600 public static class MemoryInfo implements Parcelable {
1601 /**
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001602 * The available memory on the system. This number should not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 * be considered absolute: due to the nature of the kernel, a significant
1604 * portion of this memory is actually in use and needed for the overall
1605 * system to run well.
1606 */
1607 public long availMem;
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001608
1609 /**
1610 * The total memory accessible by the kernel. This is basically the
1611 * RAM size of the device, not including below-kernel fixed allocations
1612 * like DMA buffers, RAM for the baseband CPU, etc.
1613 */
1614 public long totalMem;
1615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 /**
1617 * The threshold of {@link #availMem} at which we consider memory to be
1618 * low and start killing background services and other non-extraneous
1619 * processes.
1620 */
1621 public long threshold;
1622
1623 /**
1624 * Set to true if the system considers itself to currently be in a low
1625 * memory situation.
1626 */
1627 public boolean lowMemory;
1628
Dianne Hackborn7d608422011-08-07 16:24:18 -07001629 /** @hide */
1630 public long hiddenAppThreshold;
1631 /** @hide */
1632 public long secondaryServerThreshold;
1633 /** @hide */
1634 public long visibleAppThreshold;
1635 /** @hide */
1636 public long foregroundAppThreshold;
1637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 public MemoryInfo() {
1639 }
1640
1641 public int describeContents() {
1642 return 0;
1643 }
1644
1645 public void writeToParcel(Parcel dest, int flags) {
1646 dest.writeLong(availMem);
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001647 dest.writeLong(totalMem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 dest.writeLong(threshold);
1649 dest.writeInt(lowMemory ? 1 : 0);
Dianne Hackborn7d608422011-08-07 16:24:18 -07001650 dest.writeLong(hiddenAppThreshold);
1651 dest.writeLong(secondaryServerThreshold);
1652 dest.writeLong(visibleAppThreshold);
1653 dest.writeLong(foregroundAppThreshold);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
1655
1656 public void readFromParcel(Parcel source) {
1657 availMem = source.readLong();
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001658 totalMem = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 threshold = source.readLong();
1660 lowMemory = source.readInt() != 0;
Dianne Hackborn7d608422011-08-07 16:24:18 -07001661 hiddenAppThreshold = source.readLong();
1662 secondaryServerThreshold = source.readLong();
1663 visibleAppThreshold = source.readLong();
1664 foregroundAppThreshold = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
1666
1667 public static final Creator<MemoryInfo> CREATOR
1668 = new Creator<MemoryInfo>() {
1669 public MemoryInfo createFromParcel(Parcel source) {
1670 return new MemoryInfo(source);
1671 }
1672 public MemoryInfo[] newArray(int size) {
1673 return new MemoryInfo[size];
1674 }
1675 };
1676
1677 private MemoryInfo(Parcel source) {
1678 readFromParcel(source);
1679 }
1680 }
1681
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001682 /**
1683 * Return general information about the memory state of the system. This
1684 * can be used to help decide how to manage your own memory, though note
1685 * that polling is not recommended and
1686 * {@link android.content.ComponentCallbacks2#onTrimMemory(int)
1687 * ComponentCallbacks2.onTrimMemory(int)} is the preferred way to do this.
1688 * Also see {@link #getMyMemoryState} for how to retrieve the current trim
1689 * level of your process as needed, which gives a better hint for how to
1690 * manage its memory.
1691 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 public void getMemoryInfo(MemoryInfo outInfo) {
1693 try {
1694 ActivityManagerNative.getDefault().getMemoryInfo(outInfo);
1695 } catch (RemoteException e) {
1696 }
1697 }
Craig Mautner967212c2013-04-13 21:10:58 -07001698
Craig Mautner5ff12102013-05-24 12:50:15 -07001699 /**
Craig Mautner967212c2013-04-13 21:10:58 -07001700 * Information you can retrieve about an ActivityStack in the system.
1701 * @hide
1702 */
1703 public static class StackInfo implements Parcelable {
1704 public int stackId;
Craig Mautnered6649f2013-12-02 14:08:25 -08001705 public Rect bounds = new Rect();
Craig Mautner967212c2013-04-13 21:10:58 -07001706 public int[] taskIds;
1707 public String[] taskNames;
Craig Mautnered6649f2013-12-02 14:08:25 -08001708 public int displayId;
Craig Mautner967212c2013-04-13 21:10:58 -07001709
Craig Mautner967212c2013-04-13 21:10:58 -07001710 @Override
1711 public int describeContents() {
1712 return 0;
1713 }
1714
1715 @Override
1716 public void writeToParcel(Parcel dest, int flags) {
1717 dest.writeInt(stackId);
1718 dest.writeInt(bounds.left);
1719 dest.writeInt(bounds.top);
1720 dest.writeInt(bounds.right);
1721 dest.writeInt(bounds.bottom);
1722 dest.writeIntArray(taskIds);
1723 dest.writeStringArray(taskNames);
Craig Mautnered6649f2013-12-02 14:08:25 -08001724 dest.writeInt(displayId);
Craig Mautner967212c2013-04-13 21:10:58 -07001725 }
1726
1727 public void readFromParcel(Parcel source) {
1728 stackId = source.readInt();
1729 bounds = new Rect(
1730 source.readInt(), source.readInt(), source.readInt(), source.readInt());
1731 taskIds = source.createIntArray();
1732 taskNames = source.createStringArray();
Craig Mautnered6649f2013-12-02 14:08:25 -08001733 displayId = source.readInt();
Craig Mautner967212c2013-04-13 21:10:58 -07001734 }
1735
1736 public static final Creator<StackInfo> CREATOR = new Creator<StackInfo>() {
1737 @Override
1738 public StackInfo createFromParcel(Parcel source) {
1739 return new StackInfo(source);
1740 }
1741 @Override
1742 public StackInfo[] newArray(int size) {
1743 return new StackInfo[size];
1744 }
1745 };
1746
Craig Mautner5ff12102013-05-24 12:50:15 -07001747 public StackInfo() {
1748 }
1749
Craig Mautner967212c2013-04-13 21:10:58 -07001750 private StackInfo(Parcel source) {
1751 readFromParcel(source);
1752 }
1753
Craig Mautner5ff12102013-05-24 12:50:15 -07001754 public String toString(String prefix) {
Craig Mautner967212c2013-04-13 21:10:58 -07001755 StringBuilder sb = new StringBuilder(256);
Craig Mautner5ff12102013-05-24 12:50:15 -07001756 sb.append(prefix); sb.append("Stack id="); sb.append(stackId);
Craig Mautnered6649f2013-12-02 14:08:25 -08001757 sb.append(" bounds="); sb.append(bounds.toShortString());
1758 sb.append(" displayId="); sb.append(displayId);
1759 sb.append("\n");
Craig Mautner5ff12102013-05-24 12:50:15 -07001760 prefix = prefix + " ";
Craig Mautner967212c2013-04-13 21:10:58 -07001761 for (int i = 0; i < taskIds.length; ++i) {
1762 sb.append(prefix); sb.append("taskId="); sb.append(taskIds[i]);
1763 sb.append(": "); sb.append(taskNames[i]); sb.append("\n");
1764 }
1765 return sb.toString();
1766 }
Craig Mautner5ff12102013-05-24 12:50:15 -07001767
1768 @Override
1769 public String toString() {
1770 return toString("");
1771 }
Craig Mautner967212c2013-04-13 21:10:58 -07001772 }
1773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 /**
1775 * @hide
1776 */
1777 public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
1778 try {
1779 return ActivityManagerNative.getDefault().clearApplicationUserData(packageName,
Dianne Hackborn41203752012-08-31 14:05:51 -07001780 observer, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 } catch (RemoteException e) {
1782 return false;
1783 }
1784 }
Christopher Tatea3664242013-06-26 15:00:18 -07001785
1786 /**
1787 * Permits an application to erase its own data from disk. This is equivalent to
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07001788 * the user choosing to clear the app's data from within the device settings UI. It
1789 * erases all dynamic data associated with the app -- its private data and data in its
1790 * private area on external storage -- but does not remove the installed application
1791 * itself, nor any OBB files.
Christopher Tatea3664242013-06-26 15:00:18 -07001792 *
1793 * @return {@code true} if the application successfully requested that the application's
1794 * data be erased; {@code false} otherwise.
1795 */
1796 public boolean clearApplicationUserData() {
1797 return clearApplicationUserData(mContext.getPackageName(), null);
1798 }
1799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 /**
1801 * Information you can retrieve about any processes that are in an error condition.
1802 */
1803 public static class ProcessErrorStateInfo implements Parcelable {
1804 /**
1805 * Condition codes
1806 */
1807 public static final int NO_ERROR = 0;
1808 public static final int CRASHED = 1;
1809 public static final int NOT_RESPONDING = 2;
1810
1811 /**
1812 * The condition that the process is in.
1813 */
1814 public int condition;
1815
1816 /**
1817 * The process name in which the crash or error occurred.
1818 */
1819 public String processName;
1820
1821 /**
1822 * The pid of this process; 0 if none
1823 */
1824 public int pid;
1825
1826 /**
1827 * The kernel user-ID that has been assigned to this process;
1828 * currently this is not a unique ID (multiple applications can have
1829 * the same uid).
1830 */
1831 public int uid;
1832
1833 /**
Dan Egnor60d87622009-12-16 16:32:58 -08001834 * The activity name associated with the error, if known. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 */
1836 public String tag;
1837
1838 /**
1839 * A short message describing the error condition.
1840 */
1841 public String shortMsg;
1842
1843 /**
1844 * A long message describing the error condition.
1845 */
1846 public String longMsg;
1847
1848 /**
Dan Egnorb7f03672009-12-09 16:22:32 -08001849 * The stack trace where the error originated. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 */
Dan Egnorb7f03672009-12-09 16:22:32 -08001851 public String stackTrace;
1852
1853 /**
1854 * to be deprecated: This value will always be null.
1855 */
1856 public byte[] crashData = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857
1858 public ProcessErrorStateInfo() {
1859 }
1860
Craig Mautner5ff12102013-05-24 12:50:15 -07001861 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 public int describeContents() {
1863 return 0;
1864 }
1865
Craig Mautner5ff12102013-05-24 12:50:15 -07001866 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 public void writeToParcel(Parcel dest, int flags) {
1868 dest.writeInt(condition);
1869 dest.writeString(processName);
1870 dest.writeInt(pid);
1871 dest.writeInt(uid);
1872 dest.writeString(tag);
1873 dest.writeString(shortMsg);
1874 dest.writeString(longMsg);
Dan Egnorb7f03672009-12-09 16:22:32 -08001875 dest.writeString(stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 }
Craig Mautner5ff12102013-05-24 12:50:15 -07001877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 public void readFromParcel(Parcel source) {
1879 condition = source.readInt();
1880 processName = source.readString();
1881 pid = source.readInt();
1882 uid = source.readInt();
1883 tag = source.readString();
1884 shortMsg = source.readString();
1885 longMsg = source.readString();
Dan Egnorb7f03672009-12-09 16:22:32 -08001886 stackTrace = source.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 }
1888
1889 public static final Creator<ProcessErrorStateInfo> CREATOR =
1890 new Creator<ProcessErrorStateInfo>() {
1891 public ProcessErrorStateInfo createFromParcel(Parcel source) {
1892 return new ProcessErrorStateInfo(source);
1893 }
1894 public ProcessErrorStateInfo[] newArray(int size) {
1895 return new ProcessErrorStateInfo[size];
1896 }
1897 };
1898
1899 private ProcessErrorStateInfo(Parcel source) {
1900 readFromParcel(source);
1901 }
1902 }
1903
1904 /**
1905 * Returns a list of any processes that are currently in an error condition. The result
1906 * will be null if all processes are running properly at this time.
1907 *
1908 * @return Returns a list of ProcessErrorStateInfo records, or null if there are no
1909 * current error conditions (it will not return an empty list). This list ordering is not
1910 * specified.
1911 */
1912 public List<ProcessErrorStateInfo> getProcessesInErrorState() {
1913 try {
1914 return ActivityManagerNative.getDefault().getProcessesInErrorState();
1915 } catch (RemoteException e) {
1916 return null;
1917 }
1918 }
1919
1920 /**
1921 * Information you can retrieve about a running process.
1922 */
1923 public static class RunningAppProcessInfo implements Parcelable {
1924 /**
1925 * The name of the process that this object is associated with
1926 */
1927 public String processName;
1928
1929 /**
1930 * The pid of this process; 0 if none
1931 */
1932 public int pid;
1933
Dianne Hackborneb034652009-09-07 00:49:58 -07001934 /**
1935 * The user id of this process.
1936 */
1937 public int uid;
1938
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001939 /**
1940 * All packages that have been loaded into the process.
1941 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 public String pkgList[];
1943
1944 /**
Dianne Hackborn482566e2010-09-03 12:51:28 -07001945 * Constant for {@link #flags}: this is an app that is unable to
1946 * correctly save its state when going to the background,
1947 * so it can not be killed while in the background.
1948 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001949 */
Dianne Hackborn482566e2010-09-03 12:51:28 -07001950 public static final int FLAG_CANT_SAVE_STATE = 1<<0;
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001951
1952 /**
Dianne Hackborn42499172010-10-15 18:45:07 -07001953 * Constant for {@link #flags}: this process is associated with a
1954 * persistent system app.
1955 * @hide
1956 */
1957 public static final int FLAG_PERSISTENT = 1<<1;
1958
1959 /**
Dianne Hackborn0c380492012-08-20 17:23:30 -07001960 * Constant for {@link #flags}: this process is associated with a
1961 * persistent system app.
1962 * @hide
1963 */
1964 public static final int FLAG_HAS_ACTIVITIES = 1<<2;
1965
1966 /**
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001967 * Flags of information. May be any of
Dianne Hackborn482566e2010-09-03 12:51:28 -07001968 * {@link #FLAG_CANT_SAVE_STATE}.
1969 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001970 */
1971 public int flags;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08001972
1973 /**
1974 * Last memory trim level reported to the process: corresponds to
1975 * the values supplied to {@link android.content.ComponentCallbacks2#onTrimMemory(int)
1976 * ComponentCallbacks2.onTrimMemory(int)}.
1977 */
1978 public int lastTrimLevel;
1979
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07001980 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 * Constant for {@link #importance}: this process is running the
1982 * foreground UI.
1983 */
1984 public static final int IMPORTANCE_FOREGROUND = 100;
1985
1986 /**
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001987 * Constant for {@link #importance}: this process is running something
1988 * that is actively visible to the user, though not in the immediate
1989 * foreground.
Dianne Hackborn860755f2010-06-03 18:47:52 -07001990 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001991 public static final int IMPORTANCE_VISIBLE = 200;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001992
1993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 * Constant for {@link #importance}: this process is running something
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001995 * that is considered to be actively perceptible to the user. An
1996 * example would be an application performing background music playback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001998 public static final int IMPORTANCE_PERCEPTIBLE = 130;
1999
2000 /**
Dianne Hackborn5383f502010-10-22 12:59:20 -07002001 * Constant for {@link #importance}: this process is running an
2002 * application that can not save its state, and thus can't be killed
2003 * while in the background.
2004 * @hide
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002005 */
Dianne Hackborn5383f502010-10-22 12:59:20 -07002006 public static final int IMPORTANCE_CANT_SAVE_STATE = 170;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007
2008 /**
2009 * Constant for {@link #importance}: this process is contains services
2010 * that should remain running.
2011 */
2012 public static final int IMPORTANCE_SERVICE = 300;
2013
2014 /**
2015 * Constant for {@link #importance}: this process process contains
2016 * background code that is expendable.
2017 */
2018 public static final int IMPORTANCE_BACKGROUND = 400;
2019
2020 /**
2021 * Constant for {@link #importance}: this process is empty of any
2022 * actively running code.
2023 */
2024 public static final int IMPORTANCE_EMPTY = 500;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002025
2026 /**
2027 * Constant for {@link #importance}: this process does not exist.
2028 */
2029 public static final int IMPORTANCE_GONE = 1000;
2030
2031 /** @hide */
2032 public static int procStateToImportance(int procState) {
2033 if (procState >= ActivityManager.PROCESS_STATE_HOME) {
2034 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
2035 } else if (procState >= ActivityManager.PROCESS_STATE_SERVICE) {
2036 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
2037 } else if (procState > ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
2038 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
2039 } else if (procState >= ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND) {
2040 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
2041 } else if (procState >= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
2042 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
2043 } else {
2044 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
2045 }
2046 }
2047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 /**
2049 * The relative importance level that the system places on this
2050 * process. May be one of {@link #IMPORTANCE_FOREGROUND},
2051 * {@link #IMPORTANCE_VISIBLE}, {@link #IMPORTANCE_SERVICE},
2052 * {@link #IMPORTANCE_BACKGROUND}, or {@link #IMPORTANCE_EMPTY}. These
2053 * constants are numbered so that "more important" values are always
2054 * smaller than "less important" values.
2055 */
2056 public int importance;
2057
2058 /**
2059 * An additional ordering within a particular {@link #importance}
2060 * category, providing finer-grained information about the relative
2061 * utility of processes within a category. This number means nothing
2062 * except that a smaller values are more recently used (and thus
2063 * more important). Currently an LRU value is only maintained for
2064 * the {@link #IMPORTANCE_BACKGROUND} category, though others may
2065 * be maintained in the future.
2066 */
2067 public int lru;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002068
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002069 /**
2070 * Constant for {@link #importanceReasonCode}: nothing special has
2071 * been specified for the reason for this level.
2072 */
2073 public static final int REASON_UNKNOWN = 0;
2074
2075 /**
2076 * Constant for {@link #importanceReasonCode}: one of the application's
2077 * content providers is being used by another process. The pid of
2078 * the client process is in {@link #importanceReasonPid} and the
2079 * target provider in this process is in
2080 * {@link #importanceReasonComponent}.
2081 */
2082 public static final int REASON_PROVIDER_IN_USE = 1;
2083
2084 /**
2085 * Constant for {@link #importanceReasonCode}: one of the application's
2086 * content providers is being used by another process. The pid of
2087 * the client process is in {@link #importanceReasonPid} and the
2088 * target provider in this process is in
2089 * {@link #importanceReasonComponent}.
2090 */
2091 public static final int REASON_SERVICE_IN_USE = 2;
2092
2093 /**
2094 * The reason for {@link #importance}, if any.
2095 */
2096 public int importanceReasonCode;
2097
2098 /**
2099 * For the specified values of {@link #importanceReasonCode}, this
2100 * is the process ID of the other process that is a client of this
2101 * process. This will be 0 if no other process is using this one.
2102 */
2103 public int importanceReasonPid;
2104
2105 /**
2106 * For the specified values of {@link #importanceReasonCode}, this
2107 * is the name of the component that is being used in this process.
2108 */
2109 public ComponentName importanceReasonComponent;
2110
Dianne Hackborn905577f2011-09-07 18:31:28 -07002111 /**
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002112 * When {@link #importanceReasonPid} is non-0, this is the importance
Dianne Hackborn905577f2011-09-07 18:31:28 -07002113 * of the other pid. @hide
2114 */
2115 public int importanceReasonImportance;
2116
Dianne Hackborn684bf342014-04-29 17:56:57 -07002117 /**
2118 * Current process state, as per PROCESS_STATE_* constants.
2119 * @hide
2120 */
2121 public int processState;
2122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 public RunningAppProcessInfo() {
2124 importance = IMPORTANCE_FOREGROUND;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002125 importanceReasonCode = REASON_UNKNOWN;
Dianne Hackborn684bf342014-04-29 17:56:57 -07002126 processState = PROCESS_STATE_IMPORTANT_FOREGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 }
2128
2129 public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) {
2130 processName = pProcessName;
2131 pid = pPid;
2132 pkgList = pArr;
2133 }
2134
2135 public int describeContents() {
2136 return 0;
2137 }
2138
2139 public void writeToParcel(Parcel dest, int flags) {
2140 dest.writeString(processName);
2141 dest.writeInt(pid);
Dianne Hackborneb034652009-09-07 00:49:58 -07002142 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 dest.writeStringArray(pkgList);
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002144 dest.writeInt(this.flags);
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002145 dest.writeInt(lastTrimLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 dest.writeInt(importance);
2147 dest.writeInt(lru);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002148 dest.writeInt(importanceReasonCode);
2149 dest.writeInt(importanceReasonPid);
2150 ComponentName.writeToParcel(importanceReasonComponent, dest);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002151 dest.writeInt(importanceReasonImportance);
Dianne Hackborn684bf342014-04-29 17:56:57 -07002152 dest.writeInt(processState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
2154
2155 public void readFromParcel(Parcel source) {
2156 processName = source.readString();
2157 pid = source.readInt();
Dianne Hackborneb034652009-09-07 00:49:58 -07002158 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 pkgList = source.readStringArray();
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002160 flags = source.readInt();
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002161 lastTrimLevel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 importance = source.readInt();
2163 lru = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002164 importanceReasonCode = source.readInt();
2165 importanceReasonPid = source.readInt();
2166 importanceReasonComponent = ComponentName.readFromParcel(source);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002167 importanceReasonImportance = source.readInt();
Dianne Hackborn684bf342014-04-29 17:56:57 -07002168 processState = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170
2171 public static final Creator<RunningAppProcessInfo> CREATOR =
2172 new Creator<RunningAppProcessInfo>() {
2173 public RunningAppProcessInfo createFromParcel(Parcel source) {
2174 return new RunningAppProcessInfo(source);
2175 }
2176 public RunningAppProcessInfo[] newArray(int size) {
2177 return new RunningAppProcessInfo[size];
2178 }
2179 };
2180
2181 private RunningAppProcessInfo(Parcel source) {
2182 readFromParcel(source);
2183 }
2184 }
2185
2186 /**
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07002187 * Returns a list of application processes installed on external media
2188 * that are running on the device.
2189 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002190 * <p><b>Note: this method is only intended for debugging or building
2191 * a user-facing process management UI.</b></p>
2192 *
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07002193 * @return Returns a list of ApplicationInfo records, or null if none
2194 * This list ordering is not specified.
2195 * @hide
2196 */
2197 public List<ApplicationInfo> getRunningExternalApplications() {
2198 try {
2199 return ActivityManagerNative.getDefault().getRunningExternalApplications();
2200 } catch (RemoteException e) {
2201 return null;
2202 }
2203 }
2204
2205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 * Returns a list of application processes that are running on the device.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002207 *
2208 * <p><b>Note: this method is only intended for debugging or building
2209 * a user-facing process management UI.</b></p>
2210 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 * @return Returns a list of RunningAppProcessInfo records, or null if there are no
2212 * running processes (it will not return an empty list). This list ordering is not
2213 * specified.
2214 */
2215 public List<RunningAppProcessInfo> getRunningAppProcesses() {
2216 try {
2217 return ActivityManagerNative.getDefault().getRunningAppProcesses();
2218 } catch (RemoteException e) {
2219 return null;
2220 }
2221 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002222
2223 /**
2224 * Return global memory state information for the calling process. This
2225 * does not fill in all fields of the {@link RunningAppProcessInfo}. The
2226 * only fields that will be filled in are
2227 * {@link RunningAppProcessInfo#pid},
2228 * {@link RunningAppProcessInfo#uid},
2229 * {@link RunningAppProcessInfo#lastTrimLevel},
2230 * {@link RunningAppProcessInfo#importance},
2231 * {@link RunningAppProcessInfo#lru}, and
2232 * {@link RunningAppProcessInfo#importanceReasonCode}.
2233 */
2234 static public void getMyMemoryState(RunningAppProcessInfo outState) {
2235 try {
2236 ActivityManagerNative.getDefault().getMyMemoryState(outState);
2237 } catch (RemoteException e) {
2238 }
2239 }
2240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07002242 * Return information about the memory usage of one or more processes.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002243 *
2244 * <p><b>Note: this method is only intended for debugging or building
2245 * a user-facing process management UI.</b></p>
2246 *
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07002247 * @param pids The pids of the processes whose memory usage is to be
2248 * retrieved.
2249 * @return Returns an array of memory information, one for each
2250 * requested pid.
2251 */
2252 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
2253 try {
2254 return ActivityManagerNative.getDefault().getProcessMemoryInfo(pids);
2255 } catch (RemoteException e) {
2256 return null;
2257 }
2258 }
2259
2260 /**
Dianne Hackborn03abb812010-01-04 18:43:19 -08002261 * @deprecated This is now just a wrapper for
2262 * {@link #killBackgroundProcesses(String)}; the previous behavior here
2263 * is no longer available to applications because it allows them to
2264 * break other applications by removing their alarms, stopping their
2265 * services, etc.
2266 */
2267 @Deprecated
2268 public void restartPackage(String packageName) {
2269 killBackgroundProcesses(packageName);
2270 }
2271
2272 /**
2273 * Have the system immediately kill all background processes associated
2274 * with the given package. This is the same as the kernel killing those
2275 * processes to reclaim memory; the system will take care of restarting
2276 * these processes in the future as needed.
2277 *
2278 * <p>You must hold the permission
2279 * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to
2280 * call this method.
2281 *
2282 * @param packageName The name of the package whose processes are to
2283 * be killed.
2284 */
2285 public void killBackgroundProcesses(String packageName) {
2286 try {
Dianne Hackborn1676c852012-09-10 14:52:30 -07002287 ActivityManagerNative.getDefault().killBackgroundProcesses(packageName,
2288 UserHandle.myUserId());
Dianne Hackborn03abb812010-01-04 18:43:19 -08002289 } catch (RemoteException e) {
2290 }
2291 }
2292
2293 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 * Have the system perform a force stop of everything associated with
2295 * the given application package. All processes that share its uid
2296 * will be killed, all services it has running stopped, all activities
2297 * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}
2298 * broadcast will be sent, so that any of its registered alarms can
2299 * be stopped, notifications removed, etc.
2300 *
2301 * <p>You must hold the permission
Dianne Hackborn03abb812010-01-04 18:43:19 -08002302 * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 * call this method.
2304 *
2305 * @param packageName The name of the package to be stopped.
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01002306 * @param userId The user for which the running package is to be stopped.
Dianne Hackborn03abb812010-01-04 18:43:19 -08002307 *
2308 * @hide This is not available to third party applications due to
2309 * it allowing them to break other applications by stopping their
2310 * services, removing their alarms, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 */
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01002312 public void forceStopPackageAsUser(String packageName, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 try {
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01002314 ActivityManagerNative.getDefault().forceStopPackage(packageName, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 } catch (RemoteException e) {
2316 }
2317 }
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01002318
2319 /**
2320 * @see #forceStopPackageAsUser(String, int)
2321 * @hide
2322 */
2323 public void forceStopPackage(String packageName) {
2324 forceStopPackageAsUser(packageName, UserHandle.myUserId());
2325 }
2326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 /**
2328 * Get the device configuration attributes.
2329 */
2330 public ConfigurationInfo getDeviceConfigurationInfo() {
2331 try {
2332 return ActivityManagerNative.getDefault().getDeviceConfigurationInfo();
2333 } catch (RemoteException e) {
2334 }
2335 return null;
2336 }
Kenny Root5ef44b72011-01-26 17:22:20 -08002337
2338 /**
2339 * Get the preferred density of icons for the launcher. This is used when
2340 * custom drawables are created (e.g., for shortcuts).
2341 *
2342 * @return density in terms of DPI
2343 */
2344 public int getLauncherLargeIconDensity() {
2345 final Resources res = mContext.getResources();
2346 final int density = res.getDisplayMetrics().densityDpi;
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002347 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08002348
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002349 if (sw < 600) {
2350 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08002351 return density;
2352 }
2353
2354 switch (density) {
2355 case DisplayMetrics.DENSITY_LOW:
2356 return DisplayMetrics.DENSITY_MEDIUM;
2357 case DisplayMetrics.DENSITY_MEDIUM:
2358 return DisplayMetrics.DENSITY_HIGH;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002359 case DisplayMetrics.DENSITY_TV:
2360 return DisplayMetrics.DENSITY_XHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08002361 case DisplayMetrics.DENSITY_HIGH:
2362 return DisplayMetrics.DENSITY_XHIGH;
2363 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002364 return DisplayMetrics.DENSITY_XXHIGH;
2365 case DisplayMetrics.DENSITY_XXHIGH:
2366 return DisplayMetrics.DENSITY_XHIGH * 2;
Kenny Root5ef44b72011-01-26 17:22:20 -08002367 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002368 // The density is some abnormal value. Return some other
2369 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002370 return (int)((density*1.5f)+.5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08002371 }
2372 }
2373
2374 /**
2375 * Get the preferred launcher icon size. This is used when custom drawables
2376 * are created (e.g., for shortcuts).
2377 *
2378 * @return dimensions of square icons in terms of pixels
2379 */
2380 public int getLauncherLargeIconSize() {
Craig Mautner2fbd7542014-03-21 09:34:07 -07002381 return getLauncherLargeIconSizeInner(mContext);
2382 }
2383
2384 static int getLauncherLargeIconSizeInner(Context context) {
2385 final Resources res = context.getResources();
Kenny Root5ef44b72011-01-26 17:22:20 -08002386 final int size = res.getDimensionPixelSize(android.R.dimen.app_icon_size);
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002387 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08002388
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002389 if (sw < 600) {
2390 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08002391 return size;
2392 }
2393
2394 final int density = res.getDisplayMetrics().densityDpi;
2395
2396 switch (density) {
2397 case DisplayMetrics.DENSITY_LOW:
2398 return (size * DisplayMetrics.DENSITY_MEDIUM) / DisplayMetrics.DENSITY_LOW;
2399 case DisplayMetrics.DENSITY_MEDIUM:
2400 return (size * DisplayMetrics.DENSITY_HIGH) / DisplayMetrics.DENSITY_MEDIUM;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002401 case DisplayMetrics.DENSITY_TV:
2402 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08002403 case DisplayMetrics.DENSITY_HIGH:
2404 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
2405 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002406 return (size * DisplayMetrics.DENSITY_XXHIGH) / DisplayMetrics.DENSITY_XHIGH;
2407 case DisplayMetrics.DENSITY_XXHIGH:
2408 return (size * DisplayMetrics.DENSITY_XHIGH*2) / DisplayMetrics.DENSITY_XXHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08002409 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08002410 // The density is some abnormal value. Return some other
2411 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07002412 return (int)((size*1.5f) + .5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08002413 }
2414 }
2415
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08002416 /**
2417 * Returns "true" if the user interface is currently being messed with
2418 * by a monkey.
2419 */
2420 public static boolean isUserAMonkey() {
2421 try {
2422 return ActivityManagerNative.getDefault().isUserAMonkey();
2423 } catch (RemoteException e) {
2424 }
2425 return false;
2426 }
Brett Chabot3b4fcbc2011-01-09 13:41:02 -08002427
2428 /**
2429 * Returns "true" if device is running in a test harness.
2430 */
2431 public static boolean isRunningInTestHarness() {
2432 return SystemProperties.getBoolean("ro.test_harness", false);
2433 }
Peter Visontay8d224ca2011-02-18 16:39:19 +00002434
2435 /**
2436 * Returns the launch count of each installed package.
2437 *
2438 * @hide
2439 */
Adam Lesinski0debc9a2014-07-16 19:09:13 -07002440 /*public Map<String, Integer> getAllPackageLaunchCounts() {
Peter Visontay8d224ca2011-02-18 16:39:19 +00002441 try {
2442 IUsageStats usageStatsService = IUsageStats.Stub.asInterface(
2443 ServiceManager.getService("usagestats"));
2444 if (usageStatsService == null) {
2445 return new HashMap<String, Integer>();
2446 }
2447
Dianne Hackborne22b3b12014-05-07 18:06:44 -07002448 UsageStats.PackageStats[] allPkgUsageStats = usageStatsService.getAllPkgUsageStats(
2449 ActivityThread.currentPackageName());
Peter Visontaybfcda392011-03-02 18:53:37 +00002450 if (allPkgUsageStats == null) {
2451 return new HashMap<String, Integer>();
2452 }
2453
Peter Visontay8d224ca2011-02-18 16:39:19 +00002454 Map<String, Integer> launchCounts = new HashMap<String, Integer>();
Dianne Hackborne22b3b12014-05-07 18:06:44 -07002455 for (UsageStats.PackageStats pkgUsageStats : allPkgUsageStats) {
2456 launchCounts.put(pkgUsageStats.getPackageName(), pkgUsageStats.getLaunchCount());
Peter Visontay8d224ca2011-02-18 16:39:19 +00002457 }
2458
2459 return launchCounts;
2460 } catch (RemoteException e) {
2461 Log.w(TAG, "Could not query launch counts", e);
2462 return new HashMap<String, Integer>();
2463 }
Adam Lesinski0debc9a2014-07-16 19:09:13 -07002464 }*/
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002465
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002466 /** @hide */
2467 public static int checkComponentPermission(String permission, int uid,
2468 int owningUid, boolean exported) {
2469 // Root, system server get to do everything.
2470 if (uid == 0 || uid == Process.SYSTEM_UID) {
2471 return PackageManager.PERMISSION_GRANTED;
2472 }
2473 // Isolated processes don't get any permissions.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002474 if (UserHandle.isIsolated(uid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002475 return PackageManager.PERMISSION_DENIED;
2476 }
2477 // If there is a uid that owns whatever is being accessed, it has
2478 // blanket access to it regardless of the permissions it requires.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002479 if (owningUid >= 0 && UserHandle.isSameApp(uid, owningUid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002480 return PackageManager.PERMISSION_GRANTED;
2481 }
2482 // If the target is not exported, then nobody else can get to it.
2483 if (!exported) {
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -07002484 /*
2485 RuntimeException here = new RuntimeException("here");
2486 here.fillInStackTrace();
2487 Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid,
2488 here);
2489 */
Dianne Hackborn5320eb82012-05-18 12:05:04 -07002490 return PackageManager.PERMISSION_DENIED;
2491 }
2492 if (permission == null) {
2493 return PackageManager.PERMISSION_GRANTED;
2494 }
2495 try {
2496 return AppGlobals.getPackageManager()
2497 .checkUidPermission(permission, uid);
2498 } catch (RemoteException e) {
2499 // Should never happen, but if it does... deny!
2500 Slog.e(TAG, "PackageManager is dead?!?", e);
2501 }
2502 return PackageManager.PERMISSION_DENIED;
2503 }
2504
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002505 /** @hide */
2506 public static int checkUidPermission(String permission, int uid) {
2507 try {
2508 return AppGlobals.getPackageManager()
2509 .checkUidPermission(permission, uid);
2510 } catch (RemoteException e) {
2511 // Should never happen, but if it does... deny!
2512 Slog.e(TAG, "PackageManager is dead?!?", e);
2513 }
2514 return PackageManager.PERMISSION_DENIED;
2515 }
2516
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002517 /**
2518 * @hide
2519 * Helper for dealing with incoming user arguments to system service calls.
2520 * Takes care of checking permissions and converting USER_CURRENT to the
2521 * actual current user.
2522 *
2523 * @param callingPid The pid of the incoming call, as per Binder.getCallingPid().
2524 * @param callingUid The uid of the incoming call, as per Binder.getCallingUid().
2525 * @param userId The user id argument supplied by the caller -- this is the user
2526 * they want to run as.
2527 * @param allowAll If true, we will allow USER_ALL. This means you must be prepared
2528 * to get a USER_ALL returned and deal with it correctly. If false,
2529 * an exception will be thrown if USER_ALL is supplied.
2530 * @param requireFull If true, the caller must hold
2531 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} to be able to run as a
2532 * different user than their current process; otherwise they must hold
2533 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS}.
2534 * @param name Optional textual name of the incoming call; only for generating error messages.
2535 * @param callerPackage Optional package name of caller; only for error messages.
2536 *
2537 * @return Returns the user ID that the call should run as. Will always be a concrete
2538 * user number, unless <var>allowAll</var> is true in which case it could also be
2539 * USER_ALL.
2540 */
Dianne Hackborn41203752012-08-31 14:05:51 -07002541 public static int handleIncomingUser(int callingPid, int callingUid, int userId,
2542 boolean allowAll, boolean requireFull, String name, String callerPackage) {
2543 if (UserHandle.getUserId(callingUid) == userId) {
2544 return userId;
2545 }
2546 try {
2547 return ActivityManagerNative.getDefault().handleIncomingUser(callingPid,
2548 callingUid, userId, allowAll, requireFull, name, callerPackage);
2549 } catch (RemoteException e) {
2550 throw new SecurityException("Failed calling activity manager", e);
2551 }
2552 }
2553
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07002554 /**
2555 * Gets the userId of the current foreground user. Requires system permissions.
2556 * @hide
2557 */
2558 @SystemApi
Dianne Hackborn41203752012-08-31 14:05:51 -07002559 public static int getCurrentUser() {
2560 UserInfo ui;
2561 try {
2562 ui = ActivityManagerNative.getDefault().getCurrentUser();
2563 return ui != null ? ui.id : 0;
2564 } catch (RemoteException e) {
2565 return 0;
2566 }
2567 }
2568
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002569 /**
2570 * @param userid the user's id. Zero indicates the default user
2571 * @hide
2572 */
2573 public boolean switchUser(int userid) {
2574 try {
2575 return ActivityManagerNative.getDefault().switchUser(userid);
2576 } catch (RemoteException e) {
2577 return false;
2578 }
2579 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07002580
2581 /**
2582 * Return whether the given user is actively running. This means that
2583 * the user is in the "started" state, not "stopped" -- it is currently
2584 * allowed to run code through scheduled alarms, receiving broadcasts,
2585 * etc. A started user may be either the current foreground user or a
2586 * background user; the result here does not distinguish between the two.
2587 * @param userid the user's id. Zero indicates the default user.
2588 * @hide
2589 */
2590 public boolean isUserRunning(int userid) {
2591 try {
Dianne Hackborna8a9bd62012-10-09 15:36:59 -07002592 return ActivityManagerNative.getDefault().isUserRunning(userid, false);
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07002593 } catch (RemoteException e) {
2594 return false;
2595 }
2596 }
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002597
2598 /**
2599 * Perform a system dump of various state associated with the given application
2600 * package name. This call blocks while the dump is being performed, so should
2601 * not be done on a UI thread. The data will be written to the given file
2602 * descriptor as text. An application must hold the
2603 * {@link android.Manifest.permission#DUMP} permission to make this call.
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002604 * @param fd The file descriptor that the dump should be written to. The file
2605 * descriptor is <em>not</em> closed by this function; the caller continues to
2606 * own it.
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002607 * @param packageName The name of the package that is to be dumped.
2608 */
2609 public void dumpPackageState(FileDescriptor fd, String packageName) {
2610 dumpPackageStateStatic(fd, packageName);
2611 }
2612
2613 /**
2614 * @hide
2615 */
2616 public static void dumpPackageStateStatic(FileDescriptor fd, String packageName) {
2617 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07002618 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08002619 dumpService(pw, fd, Context.ACTIVITY_SERVICE, new String[] {
2620 "-a", "package", packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002621 pw.println();
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08002622 dumpService(pw, fd, "meminfo", new String[] { "--local", packageName });
2623 pw.println();
2624 dumpService(pw, fd, ProcessStats.SERVICE_NAME, new String[] { "-a", packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002625 pw.println();
Dianne Hackborn904a8572013-06-28 18:12:31 -07002626 dumpService(pw, fd, "usagestats", new String[] { "--packages", packageName });
2627 pw.println();
Dianne Hackborn8c841092013-06-24 13:46:13 -07002628 dumpService(pw, fd, "package", new String[] { packageName });
2629 pw.println();
2630 dumpService(pw, fd, BatteryStats.SERVICE_NAME, new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002631 pw.flush();
2632 }
2633
2634 private static void dumpService(PrintWriter pw, FileDescriptor fd, String name, String[] args) {
2635 pw.print("DUMP OF SERVICE "); pw.print(name); pw.println(":");
2636 IBinder service = ServiceManager.checkService(name);
2637 if (service == null) {
2638 pw.println(" (Service not found)");
2639 return;
2640 }
2641 TransferPipe tp = null;
2642 try {
2643 pw.flush();
2644 tp = new TransferPipe();
2645 tp.setBufferPrefix(" ");
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08002646 service.dumpAsync(tp.getWriteFd().getFileDescriptor(), args);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002647 tp.go(fd);
2648 } catch (Throwable e) {
2649 if (tp != null) {
2650 tp.kill();
2651 }
2652 pw.println("Failure dumping service:");
2653 e.printStackTrace(pw);
2654 }
2655 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002656
2657 /**
2658 * @hide
2659 */
2660 public void startLockTaskMode(int taskId) {
2661 try {
2662 ActivityManagerNative.getDefault().startLockTaskMode(taskId);
2663 } catch (RemoteException e) {
2664 }
2665 }
2666
2667 /**
2668 * @hide
2669 */
2670 public void stopLockTaskMode() {
2671 try {
2672 ActivityManagerNative.getDefault().stopLockTaskMode();
2673 } catch (RemoteException e) {
2674 }
2675 }
2676
2677 /**
Jason Monk386c94f2014-07-14 16:42:24 -04002678 * Return whether currently in lock task mode. When in this mode
2679 * no new tasks can be created or switched to.
2680 *
2681 * @see Activity#startLockTask()
Craig Mautneraea74a52014-03-08 14:23:10 -08002682 */
2683 public boolean isInLockTaskMode() {
2684 try {
2685 return ActivityManagerNative.getDefault().isInLockTaskMode();
2686 } catch (RemoteException e) {
2687 return false;
2688 }
2689 }
Winson Chung1147c402014-05-14 11:05:00 -07002690
2691 /**
2692 * The AppTask allows you to manage your own application's tasks.
2693 * See {@link android.app.ActivityManager#getAppTasks()}
2694 */
2695 public static class AppTask {
2696 private IAppTask mAppTaskImpl;
2697
2698 /** @hide */
2699 public AppTask(IAppTask task) {
2700 mAppTaskImpl = task;
2701 }
2702
2703 /**
2704 * Finishes all activities in this task and removes it from the recent tasks list.
2705 */
2706 public void finishAndRemoveTask() {
2707 try {
2708 mAppTaskImpl.finishAndRemoveTask();
2709 } catch (RemoteException e) {
2710 Slog.e(TAG, "Invalid AppTask", e);
2711 }
2712 }
2713
2714 /**
2715 * Get the RecentTaskInfo associated with this task.
2716 *
2717 * @return The RecentTaskInfo for this task, or null if the task no longer exists.
2718 */
2719 public RecentTaskInfo getTaskInfo() {
2720 try {
2721 return mAppTaskImpl.getTaskInfo();
2722 } catch (RemoteException e) {
2723 Slog.e(TAG, "Invalid AppTask", e);
2724 return null;
2725 }
2726 }
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002727
2728 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002729 * Bring this task to the foreground. If it contains activities, they will be
2730 * brought to the foreground with it and their instances re-created if needed.
2731 * If it doesn't contain activities, the root activity of the task will be
2732 * re-launched.
2733 */
2734 public void moveToFront() {
2735 try {
2736 mAppTaskImpl.moveToFront();
2737 } catch (RemoteException e) {
2738 Slog.e(TAG, "Invalid AppTask", e);
2739 }
2740 }
2741
2742 /**
2743 * Start an activity in this task. Brings the task to the foreground. If this task
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002744 * is not currently active (that is, its id < 0), then a new activity for the given
2745 * Intent will be launched as the root of the task and the task brought to the
2746 * foreground. Otherwise, if this task is currently active and the Intent does not specify
2747 * an activity to launch in a new task, then a new activity for the given Intent will
2748 * be launched on top of the task and the task brought to the foreground. If this
2749 * task is currently active and the Intent specifies {@link Intent#FLAG_ACTIVITY_NEW_TASK}
2750 * or would otherwise be launched in to a new task, then the activity not launched but
2751 * this task be brought to the foreground and a new intent delivered to the top
2752 * activity if appropriate.
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002753 *
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002754 * <p>In other words, you generally want to use an Intent here that does not specify
2755 * {@link Intent#FLAG_ACTIVITY_NEW_TASK} or {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT},
2756 * and let the system do the right thing.</p>
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002757 *
2758 * @param intent The Intent describing the new activity to be launched on the task.
2759 * @param options Optional launch options.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002760 *
2761 * @see Activity#startActivity(android.content.Intent, android.os.Bundle)
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002762 */
2763 public void startActivity(Context context, Intent intent, Bundle options) {
2764 ActivityThread thread = ActivityThread.currentActivityThread();
2765 thread.getInstrumentation().execStartActivityFromAppTask(context,
2766 thread.getApplicationThread(), mAppTaskImpl, intent, options);
2767 }
2768
2769 /**
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002770 * Modify the {@link Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag in the root
2771 * Intent of this AppTask.
2772 *
2773 * @param exclude If true, {@link Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} will
2774 * be set; otherwise, it will be cleared.
2775 */
2776 public void setExcludeFromRecents(boolean exclude) {
2777 try {
2778 mAppTaskImpl.setExcludeFromRecents(exclude);
2779 } catch (RemoteException e) {
2780 Slog.e(TAG, "Invalid AppTask", e);
2781 }
2782 }
Winson Chung1147c402014-05-14 11:05:00 -07002783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784}