blob: b1927d043cce38b10ab8ef536793656ca28d8b5d [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
Svet Ganov019d2302015-05-04 11:07:38 -070019import android.Manifest;
Michal Karpinski3da5c972015-12-11 18:16:30 +000020import android.annotation.IntDef;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -070021import android.annotation.NonNull;
22import android.annotation.Nullable;
Svet Ganov019d2302015-05-04 11:07:38 -070023import android.annotation.RequiresPermission;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070024import android.annotation.SystemApi;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -070025import android.graphics.Canvas;
26import android.graphics.Matrix;
27import android.graphics.Point;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070028import android.os.BatteryStats;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070029import android.os.IBinder;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070030import android.os.ParcelFileDescriptor;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070031
Svet Ganov019d2302015-05-04 11:07:38 -070032import android.util.Log;
Dianne Hackborn8a0de582013-08-07 15:22:07 -070033import com.android.internal.app.ProcessStats;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070034import com.android.internal.os.TransferPipe;
Dianne Hackborn8c841092013-06-24 13:46:13 -070035import com.android.internal.util.FastPrintWriter;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.ComponentName;
38import android.content.Context;
39import android.content.Intent;
Felipe Lemef3fa0f82016-01-07 12:08:19 -080040import android.content.UriPermission;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -070041import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.pm.ConfigurationInfo;
43import android.content.pm.IPackageDataObserver;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070044import android.content.pm.PackageManager;
Felipe Lemef3fa0f82016-01-07 12:08:19 -080045import android.content.pm.ParceledListSlice;
Dianne Hackborn41203752012-08-31 14:05:51 -070046import android.content.pm.UserInfo;
Kenny Root5ef44b72011-01-26 17:22:20 -080047import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.graphics.Bitmap;
Winson Chunga449dc02014-05-16 11:15:04 -070049import android.graphics.Color;
Craig Mautner967212c2013-04-13 21:10:58 -070050import android.graphics.Rect;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -070051import android.os.Bundle;
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -070052import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Handler;
54import android.os.Parcel;
55import android.os.Parcelable;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070056import android.os.Process;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070057import android.os.RemoteException;
Peter Visontay8d224ca2011-02-18 16:39:19 +000058import android.os.ServiceManager;
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -070059import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070060import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.text.TextUtils;
Kenny Root5ef44b72011-01-26 17:22:20 -080062import android.util.DisplayMetrics;
Winson Chung48a10a52014-08-27 14:36:51 -070063import android.util.Size;
Dianne Hackbornb3756322011-08-12 13:58:13 -070064import android.util.Slog;
Felipe Lemef3fa0f82016-01-07 12:08:19 -080065
Craig Mautner648f69b2014-09-18 14:16:26 -070066import org.xmlpull.v1.XmlSerializer;
Kenny Root5ef44b72011-01-26 17:22:20 -080067
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070068import java.io.FileDescriptor;
69import java.io.FileOutputStream;
Craig Mautner648f69b2014-09-18 14:16:26 -070070import java.io.IOException;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070071import java.io.PrintWriter;
Michal Karpinski3da5c972015-12-11 18:16:30 +000072import java.lang.annotation.Retention;
73import java.lang.annotation.RetentionPolicy;
Winson Chung1147c402014-05-14 11:05:00 -070074import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.List;
76
77/**
78 * Interact with the overall activities running in the system.
79 */
80public class ActivityManager {
81 private static String TAG = "ActivityManager";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Dianne Hackborn852975d2014-08-22 17:42:43 -070083 private static int gMaxRecentTasks = -1;
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private final Context mContext;
86 private final Handler mHandler;
87
Felipe Lemedc7af962016-01-22 18:27:03 -080088 /**
89 * Defines acceptable types of bugreports.
90 * @hide
91 */
Michal Karpinski3da5c972015-12-11 18:16:30 +000092 @Retention(RetentionPolicy.SOURCE)
93 @IntDef({
94 BUGREPORT_OPTION_FULL,
95 BUGREPORT_OPTION_INTERACTIVE,
96 BUGREPORT_OPTION_REMOTE
97 })
Michal Karpinski3da5c972015-12-11 18:16:30 +000098 public @interface BugreportMode {}
99 /**
100 * Takes a bugreport without user interference (and hence causing less
101 * interference to the system), but includes all sections.
102 * @hide
103 */
104 public static final int BUGREPORT_OPTION_FULL = 0;
105 /**
106 * Allows user to monitor progress and enter additional data; might not include all
107 * sections.
108 * @hide
109 */
110 public static final int BUGREPORT_OPTION_INTERACTIVE = 1;
111 /**
112 * Takes a bugreport requested remotely by administrator of the Device Owner app,
113 * not the device's user.
114 * @hide
115 */
116 public static final int BUGREPORT_OPTION_REMOTE = 2;
117
Dianne Hackborna4972e92012-03-14 10:38:05 -0700118 /**
Scott Maincc2195b2013-10-16 13:57:46 -0700119 * <a href="{@docRoot}guide/topics/manifest/meta-data-element.html">{@code
Neil Fuller71fbb812015-11-30 09:51:33 +0000120 * <meta-data>}</a> name for a 'home' Activity that declares a package that is to be
Christopher Tatebd413f62013-09-18 18:31:59 -0700121 * uninstalled in lieu of the declaring one. The package named here must be
Neil Fuller71fbb812015-11-30 09:51:33 +0000122 * signed with the same certificate as the one declaring the {@code <meta-data>}.
Christopher Tatebd413f62013-09-18 18:31:59 -0700123 */
124 public static final String META_HOME_ALTERNATE = "android.app.home.alternate";
125
126 /**
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700127 * Result for IActivityManager.startActivity: trying to start a background user
128 * activity that shouldn't be displayed for all users.
129 * @hide
130 */
131 public static final int START_NOT_CURRENT_USER_ACTIVITY = -8;
132
133 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700134 * Result for IActivityManager.startActivity: trying to start an activity under voice
135 * control when that activity does not support the VOICE category.
136 * @hide
137 */
138 public static final int START_NOT_VOICE_COMPATIBLE = -7;
139
140 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700141 * Result for IActivityManager.startActivity: an error where the
142 * start had to be canceled.
143 * @hide
144 */
145 public static final int START_CANCELED = -6;
146
147 /**
148 * Result for IActivityManager.startActivity: an error where the
149 * thing being started is not an activity.
150 * @hide
151 */
152 public static final int START_NOT_ACTIVITY = -5;
153
154 /**
155 * Result for IActivityManager.startActivity: an error where the
156 * caller does not have permission to start the activity.
157 * @hide
158 */
159 public static final int START_PERMISSION_DENIED = -4;
160
161 /**
162 * Result for IActivityManager.startActivity: an error where the
163 * caller has requested both to forward a result and to receive
164 * a result.
165 * @hide
166 */
167 public static final int START_FORWARD_AND_REQUEST_CONFLICT = -3;
168
169 /**
170 * Result for IActivityManager.startActivity: an error where the
171 * requested class is not found.
172 * @hide
173 */
174 public static final int START_CLASS_NOT_FOUND = -2;
175
176 /**
177 * Result for IActivityManager.startActivity: an error where the
178 * given Intent could not be resolved to an activity.
179 * @hide
180 */
181 public static final int START_INTENT_NOT_RESOLVED = -1;
182
183 /**
184 * Result for IActivityManaqer.startActivity: the activity was started
185 * successfully as normal.
186 * @hide
187 */
188 public static final int START_SUCCESS = 0;
189
190 /**
191 * Result for IActivityManaqer.startActivity: the caller asked that the Intent not
192 * be executed if it is the recipient, and that is indeed the case.
193 * @hide
194 */
195 public static final int START_RETURN_INTENT_TO_CALLER = 1;
196
197 /**
198 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
199 * a task was simply brought to the foreground.
200 * @hide
201 */
202 public static final int START_TASK_TO_FRONT = 2;
203
204 /**
205 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
206 * the given Intent was given to the existing top activity.
207 * @hide
208 */
209 public static final int START_DELIVERED_TO_TOP = 3;
210
211 /**
212 * Result for IActivityManaqer.startActivity: request was canceled because
213 * app switches are temporarily canceled to ensure the user's last request
214 * (such as pressing home) is performed.
215 * @hide
216 */
217 public static final int START_SWITCHES_CANCELED = 4;
218
219 /**
Craig Mautneraea74a52014-03-08 14:23:10 -0800220 * Result for IActivityManaqer.startActivity: a new activity was attempted to be started
221 * while in Lock Task Mode.
222 * @hide
223 */
224 public static final int START_RETURN_LOCK_TASK_MODE_VIOLATION = 5;
225
226 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700227 * Flag for IActivityManaqer.startActivity: do special start mode where
228 * a new activity is launched only if it is needed.
229 * @hide
230 */
231 public static final int START_FLAG_ONLY_IF_NEEDED = 1<<0;
232
233 /**
234 * Flag for IActivityManaqer.startActivity: launch the app for
235 * debugging.
236 * @hide
237 */
238 public static final int START_FLAG_DEBUG = 1<<1;
239
240 /**
241 * Flag for IActivityManaqer.startActivity: launch the app for
Man Caocfa78b22015-06-11 20:14:34 -0700242 * allocation tracking.
243 * @hide
244 */
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700245 public static final int START_FLAG_TRACK_ALLOCATION = 1<<2;
Man Caocfa78b22015-06-11 20:14:34 -0700246
247 /**
Tamas Berghammerdf6cb282016-01-29 12:07:00 +0000248 * Flag for IActivityManaqer.startActivity: launch the app with
249 * native debugging support.
250 * @hide
251 */
252 public static final int START_FLAG_NATIVE_DEBUGGING = 1<<3;
253
254 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700255 * Result for IActivityManaqer.broadcastIntent: success!
256 * @hide
257 */
258 public static final int BROADCAST_SUCCESS = 0;
259
260 /**
261 * Result for IActivityManaqer.broadcastIntent: attempt to broadcast
262 * a sticky intent without appropriate permission.
263 * @hide
264 */
265 public static final int BROADCAST_STICKY_CANT_HAVE_PERMISSION = -1;
266
267 /**
Amith Yamasani83b6ef02014-11-07 15:34:04 -0800268 * Result for IActivityManager.broadcastIntent: trying to send a broadcast
269 * to a stopped user. Fail.
270 * @hide
271 */
272 public static final int BROADCAST_FAILED_USER_STOPPED = -2;
273
274 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700275 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
276 * for a sendBroadcast operation.
277 * @hide
278 */
279 public static final int INTENT_SENDER_BROADCAST = 1;
280
281 /**
282 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
283 * for a startActivity operation.
284 * @hide
285 */
286 public static final int INTENT_SENDER_ACTIVITY = 2;
287
288 /**
289 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
290 * for an activity result operation.
291 * @hide
292 */
293 public static final int INTENT_SENDER_ACTIVITY_RESULT = 3;
294
295 /**
296 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
297 * for a startService operation.
298 * @hide
299 */
300 public static final int INTENT_SENDER_SERVICE = 4;
301
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700302 /** @hide User operation call: success! */
303 public static final int USER_OP_SUCCESS = 0;
304
305 /** @hide User operation call: given user id is not known. */
306 public static final int USER_OP_UNKNOWN_USER = -1;
307
308 /** @hide User operation call: given user id is the current user, can't be stopped. */
309 public static final int USER_OP_IS_CURRENT = -2;
310
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700311 /** @hide User operation call: system user can't be stopped. */
312 public static final int USER_OP_ERROR_IS_SYSTEM = -3;
313
314 /** @hide User operation call: one of related users cannot be stopped. */
315 public static final int USER_OP_ERROR_RELATED_USERS_CANNOT_STOP = -4;
316
Ruben Brunka27eef42015-01-28 15:04:16 -0800317 /** @hide Process does not exist. */
318 public static final int PROCESS_STATE_NONEXISTENT = -1;
319
Dianne Hackborna413dc02013-07-12 12:02:55 -0700320 /** @hide Process is a persistent system process. */
321 public static final int PROCESS_STATE_PERSISTENT = 0;
322
323 /** @hide Process is a persistent system process and is doing UI. */
324 public static final int PROCESS_STATE_PERSISTENT_UI = 1;
325
Dianne Hackbornc8230512013-07-13 21:32:12 -0700326 /** @hide Process is hosting the current top activities. Note that this covers
327 * all activities that are visible to the user. */
Dianne Hackborna413dc02013-07-12 12:02:55 -0700328 public static final int PROCESS_STATE_TOP = 2;
329
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700330 /** @hide Process is hosting a foreground service due to a system binding. */
331 public static final int PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 3;
332
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700333 /** @hide Process is hosting a foreground service. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700334 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 4;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700335
336 /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700337 public static final int PROCESS_STATE_TOP_SLEEPING = 5;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700338
Dianne Hackborna413dc02013-07-12 12:02:55 -0700339 /** @hide Process is important to the user, and something they are aware of. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700340 public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 6;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700341
342 /** @hide Process is important to the user, but not something they are aware of. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700343 public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 7;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700344
Dianne Hackborna413dc02013-07-12 12:02:55 -0700345 /** @hide Process is in the background running a backup/restore operation. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700346 public static final int PROCESS_STATE_BACKUP = 8;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700347
Dianne Hackbornc8230512013-07-13 21:32:12 -0700348 /** @hide Process is in the background, but it can't restore its state so we want
349 * to try to avoid killing it. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700350 public static final int PROCESS_STATE_HEAVY_WEIGHT = 9;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700351
352 /** @hide Process is in the background running a service. Unlike oom_adj, this level
353 * is used for both the normal running in background state and the executing
354 * operations state. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700355 public static final int PROCESS_STATE_SERVICE = 10;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700356
Dianne Hackbornc8230512013-07-13 21:32:12 -0700357 /** @hide Process is in the background running a receiver. Note that from the
358 * perspective of oom_adj receivers run at a higher foreground level, but for our
359 * prioritization here that is not necessary and putting them below services means
360 * many fewer changes in some process states as they receive broadcasts. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700361 public static final int PROCESS_STATE_RECEIVER = 11;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700362
Dianne Hackborna413dc02013-07-12 12:02:55 -0700363 /** @hide Process is in the background but hosts the home activity. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700364 public static final int PROCESS_STATE_HOME = 12;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700365
366 /** @hide Process is in the background but hosts the last shown activity. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700367 public static final int PROCESS_STATE_LAST_ACTIVITY = 13;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700368
Dianne Hackbornc8230512013-07-13 21:32:12 -0700369 /** @hide Process is being cached for later use and contains activities. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700370 public static final int PROCESS_STATE_CACHED_ACTIVITY = 14;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700371
372 /** @hide Process is being cached for later use and is a client of another cached
373 * process that contains activities. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700374 public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 15;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700375
376 /** @hide Process is being cached for later use and is empty. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700377 public static final int PROCESS_STATE_CACHED_EMPTY = 16;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700378
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700379 /** @hide Should this process state be considered a background state? */
380 public static final boolean isProcStateBackground(int procState) {
381 return procState >= PROCESS_STATE_BACKUP;
382 }
383
Dianne Hackborna83ce1d2015-03-11 15:16:13 -0700384 /** @hide requestType for assist context: only basic information. */
385 public static final int ASSIST_CONTEXT_BASIC = 0;
386
387 /** @hide requestType for assist context: generate full AssistStructure. */
388 public static final int ASSIST_CONTEXT_FULL = 1;
389
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700390 /** @hide Flag for registerUidObserver: report changes in process state. */
391 public static final int UID_OBSERVER_PROCSTATE = 1<<0;
392
393 /** @hide Flag for registerUidObserver: report uid gone. */
394 public static final int UID_OBSERVER_GONE = 1<<1;
395
396 /** @hide Flag for registerUidObserver: report uid has become idle. */
397 public static final int UID_OBSERVER_IDLE = 1<<2;
398
399 /** @hide Flag for registerUidObserver: report uid has become active. */
400 public static final int UID_OBSERVER_ACTIVE = 1<<3;
401
402 /** @hide Mode for {@link IActivityManager#getAppStartMode}: normal free-to-run operation. */
403 public static final int APP_START_MODE_NORMAL = 0;
404
405 /** @hide Mode for {@link IActivityManager#getAppStartMode}: delay running until later. */
406 public static final int APP_START_MODE_DELAYED = 1;
407
408 /** @hide Mode for {@link IActivityManager#getAppStartMode}: disable/cancel pending
409 * launches. */
410 public static final int APP_START_MODE_DISABLED = 2;
411
Benjamin Franz43261142015-02-11 15:59:44 +0000412 /**
413 * Lock task mode is not active.
414 */
415 public static final int LOCK_TASK_MODE_NONE = 0;
416
417 /**
418 * Full lock task mode is active.
419 */
420 public static final int LOCK_TASK_MODE_LOCKED = 1;
421
422 /**
423 * App pinning mode is active.
424 */
425 public static final int LOCK_TASK_MODE_PINNED = 2;
426
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700427 Point mAppTaskThumbnailSize;
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /*package*/ ActivityManager(Context context, Handler handler) {
430 mContext = context;
431 mHandler = handler;
432 }
433
434 /**
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700435 * Screen compatibility mode: the application most always run in
436 * compatibility mode.
437 * @hide
438 */
439 public static final int COMPAT_MODE_ALWAYS = -1;
440
441 /**
442 * Screen compatibility mode: the application can never run in
443 * compatibility mode.
444 * @hide
445 */
446 public static final int COMPAT_MODE_NEVER = -2;
447
448 /**
449 * Screen compatibility mode: unknown.
450 * @hide
451 */
452 public static final int COMPAT_MODE_UNKNOWN = -3;
453
454 /**
455 * Screen compatibility mode: the application currently has compatibility
456 * mode disabled.
457 * @hide
458 */
459 public static final int COMPAT_MODE_DISABLED = 0;
460
461 /**
462 * Screen compatibility mode: the application currently has compatibility
463 * mode enabled.
464 * @hide
465 */
466 public static final int COMPAT_MODE_ENABLED = 1;
467
468 /**
469 * Screen compatibility mode: request to toggle the application's
470 * compatibility mode.
471 * @hide
472 */
473 public static final int COMPAT_MODE_TOGGLE = 2;
474
Wale Ogunwale3797c222015-10-27 14:21:58 -0700475 /** @hide */
476 public static class StackId {
477 /** Invalid stack ID. */
478 public static final int INVALID_STACK_ID = -1;
Chong Zhang5dcb2752015-08-18 13:50:26 -0700479
Wale Ogunwale3797c222015-10-27 14:21:58 -0700480 /** First static stack ID. */
481 public static final int FIRST_STATIC_STACK_ID = 0;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700482
Wale Ogunwale3797c222015-10-27 14:21:58 -0700483 /** Home activity stack ID. */
484 public static final int HOME_STACK_ID = FIRST_STATIC_STACK_ID;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700485
Wale Ogunwale3797c222015-10-27 14:21:58 -0700486 /** ID of stack where fullscreen activities are normally launched into. */
487 public static final int FULLSCREEN_WORKSPACE_STACK_ID = 1;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700488
Wale Ogunwale3797c222015-10-27 14:21:58 -0700489 /** ID of stack where freeform/resized activities are normally launched into. */
490 public static final int FREEFORM_WORKSPACE_STACK_ID = FULLSCREEN_WORKSPACE_STACK_ID + 1;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700491
Wale Ogunwale3797c222015-10-27 14:21:58 -0700492 /** ID of stack that occupies a dedicated region of the screen. */
493 public static final int DOCKED_STACK_ID = FREEFORM_WORKSPACE_STACK_ID + 1;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700494
Wale Ogunwale3797c222015-10-27 14:21:58 -0700495 /** ID of stack that always on top (always visible) when it exist. */
496 public static final int PINNED_STACK_ID = DOCKED_STACK_ID + 1;
Wale Ogunwale99db1862015-10-23 20:08:22 -0700497
Wale Ogunwale3797c222015-10-27 14:21:58 -0700498 /** Last static stack stack ID. */
499 public static final int LAST_STATIC_STACK_ID = PINNED_STACK_ID;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700500
Wale Ogunwale3797c222015-10-27 14:21:58 -0700501 /** Start of ID range used by stacks that are created dynamically. */
502 public static final int FIRST_DYNAMIC_STACK_ID = LAST_STATIC_STACK_ID + 1;
503
504 public static boolean isStaticStack(int stackId) {
505 return stackId >= FIRST_STATIC_STACK_ID && stackId <= LAST_STATIC_STACK_ID;
506 }
507
508 /**
509 * Returns true if the activities contained in the input stack display a shadow around
510 * their border.
511 */
512 public static boolean hasWindowShadow(int stackId) {
513 return stackId == FREEFORM_WORKSPACE_STACK_ID || stackId == PINNED_STACK_ID;
514 }
515
516 /**
517 * Returns true if the activities contained in the input stack display a decor view.
518 */
519 public static boolean hasWindowDecor(int stackId) {
520 return stackId == FREEFORM_WORKSPACE_STACK_ID;
521 }
522
523 /**
524 * Returns true if the tasks contained in the stack can be resized independently of the
525 * stack.
526 */
527 public static boolean isTaskResizeAllowed(int stackId) {
528 return stackId == FREEFORM_WORKSPACE_STACK_ID;
529 }
530
Wale Ogunwale807d8822015-12-15 17:05:09 -0800531 /** Returns true if the task bounds should persist across power cycles. */
Wale Ogunwale3797c222015-10-27 14:21:58 -0700532 public static boolean persistTaskBounds(int stackId) {
Wale Ogunwale807d8822015-12-15 17:05:09 -0800533 return stackId == FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700534 }
535
536 /**
537 * Returns true if dynamic stacks are allowed to be visible behind the input stack.
538 */
539 public static boolean isDynamicStacksVisibleBehindAllowed(int stackId) {
540 return stackId == PINNED_STACK_ID;
541 }
542
543 /**
544 * Returns true if we try to maintain focus in the current stack when the top activity
545 * finishes.
546 */
547 public static boolean keepFocusInStackIfPossible(int stackId) {
548 return stackId == FREEFORM_WORKSPACE_STACK_ID
549 || stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID;
550 }
551
552 /**
553 * Returns true if Stack size is affected by the docked stack changing size.
554 */
555 public static boolean isResizeableByDockedStack(int stackId) {
556 return isStaticStack(stackId) &&
557 stackId != DOCKED_STACK_ID && stackId != PINNED_STACK_ID;
558 }
559
560 /**
561 * Returns true if the size of tasks in the input stack are affected by the docked stack
562 * changing size.
563 */
564 public static boolean isTaskResizeableByDockedStack(int stackId) {
565 return isStaticStack(stackId) && stackId != FREEFORM_WORKSPACE_STACK_ID
566 && stackId != DOCKED_STACK_ID && stackId != PINNED_STACK_ID;
567 }
568
569 /**
Jorim Jaggie9098022016-01-27 19:29:40 -0800570 * Returns true if the windows of tasks being moved to the target stack from the source
571 * stack should be replaced, meaning that window manager will keep the old window around
572 * until the new is ready.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700573 */
Jorim Jaggie9098022016-01-27 19:29:40 -0800574 public static boolean replaceWindowsOnTaskMove(int sourceStackId, int targetStackId) {
575 return sourceStackId == FREEFORM_WORKSPACE_STACK_ID
576 || targetStackId == FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700577 }
Filip Gruszczynski96daf322015-11-18 18:01:27 -0800578
579 /**
580 * Returns true if animation specs should be constructed for app transition that moves
581 * the task to the specified stack.
582 */
583 public static boolean useAnimationSpecForAppTransition(int stackId) {
Jorim Jaggi8a5c6402016-02-12 16:55:18 -0800584
585 // TODO: INVALID_STACK_ID is also animated because we don't persist stack id's across
586 // reboots.
Filip Gruszczynski96daf322015-11-18 18:01:27 -0800587 return stackId == FREEFORM_WORKSPACE_STACK_ID
Jorim Jaggi8a5c6402016-02-12 16:55:18 -0800588 || stackId == FULLSCREEN_WORKSPACE_STACK_ID || stackId == DOCKED_STACK_ID
589 || stackId == INVALID_STACK_ID;
Filip Gruszczynski96daf322015-11-18 18:01:27 -0800590 }
Wale Ogunwaled045c822015-12-02 09:14:28 -0800591
592 /** Returns true if the windows in the stack can receive input keys. */
593 public static boolean canReceiveKeys(int stackId) {
594 return stackId != PINNED_STACK_ID;
595 }
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800596
Chong Zhang75b37202015-12-04 14:16:36 -0800597 /**
598 * Returns true if the stack can be visible above lockscreen.
599 */
600 public static boolean isAllowedOverLockscreen(int stackId) {
601 return stackId == HOME_STACK_ID || stackId == FULLSCREEN_WORKSPACE_STACK_ID;
602 }
603
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800604 public static boolean isAlwaysOnTop(int stackId) {
605 return stackId == PINNED_STACK_ID;
606 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800607
608 /**
609 * Returns true if the top task in the task is allowed to return home when finished and
610 * there are other tasks in the stack.
611 */
612 public static boolean allowTopTaskToReturnHome(int stackId) {
613 return stackId != PINNED_STACK_ID;
614 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -0800615
616 /**
617 * Returns true if the stack should be resized to match the bounds specified by
618 * {@link ActivityOptions#setLaunchBounds} when launching an activity into the stack.
619 */
620 public static boolean resizeStackWithLaunchBounds(int stackId) {
621 return stackId == PINNED_STACK_ID;
622 }
Wale Ogunwale3fd20fe2016-01-23 17:41:28 -0800623
624 /**
625 * Returns true if any visible windows belonging to apps in this stack should be kept on
626 * screen when the app is killed due to something like the low memory killer.
627 */
628 public static boolean keepVisibleDeadAppWindowOnScreen(int stackId) {
629 return stackId != PINNED_STACK_ID;
630 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800631
632 /**
633 * Returns true if the backdrop on the client side should match the frame of the window.
634 * Returns false, if the backdrop should be fullscreen.
635 */
636 public static boolean useWindowFrameForBackdrop(int stackId) {
637 return stackId == FREEFORM_WORKSPACE_STACK_ID || stackId == PINNED_STACK_ID;
638 }
Jorim Jaggi86905582016-02-09 21:36:09 -0800639
640 /**
641 * Returns true if a window from the specified stack with {@param stackId} are normally
642 * fullscreen, i. e. they can become the top opaque fullscreen window, meaning that it
643 * controls system bars, lockscreen occluded/dismissing state, screen rotation animation,
644 * etc.
645 */
646 public static boolean normallyFullscreenWindows(int stackId) {
647 return stackId != PINNED_STACK_ID && stackId != FREEFORM_WORKSPACE_STACK_ID
648 && stackId != DOCKED_STACK_ID;
649 }
Wale Ogunwale3797c222015-10-27 14:21:58 -0700650 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700651
Wale Ogunwale59a73ca2015-09-14 12:54:50 -0700652 /**
653 * Input parameter to {@link android.app.IActivityManager#moveTaskToDockedStack} which
654 * specifies the position of the created docked stack at the top half of the screen if
655 * in portrait mode or at the left half of the screen if in landscape mode.
656 * @hide
657 */
658 public static final int DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT = 0;
659
660 /**
661 * Input parameter to {@link android.app.IActivityManager#moveTaskToDockedStack} which
662 * specifies the position of the created docked stack at the bottom half of the screen if
663 * in portrait mode or at the right half of the screen if in landscape mode.
664 * @hide
665 */
666 public static final int DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT = 1;
667
Chong Zhang87b21722015-09-21 15:39:51 -0700668 /**
669 * Input parameter to {@link android.app.IActivityManager#resizeTask} which indicates
Chong Zhang6de2ae82015-09-30 18:25:21 -0700670 * that the resize doesn't need to preserve the window, and can be skipped if bounds
671 * is unchanged. This mode is used by window manager in most cases.
Chong Zhang87b21722015-09-21 15:39:51 -0700672 * @hide
673 */
674 public static final int RESIZE_MODE_SYSTEM = 0;
675
676 /**
677 * Input parameter to {@link android.app.IActivityManager#resizeTask} which indicates
Chong Zhang6de2ae82015-09-30 18:25:21 -0700678 * that the resize should preserve the window if possible.
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700679 * @hide
680 */
Chong Zhang6de2ae82015-09-30 18:25:21 -0700681 public static final int RESIZE_MODE_PRESERVE_WINDOW = (0x1 << 0);
Chong Zhang87b21722015-09-21 15:39:51 -0700682
683 /**
684 * Input parameter to {@link android.app.IActivityManager#resizeTask} which indicates
685 * that the resize should be performed even if the bounds appears unchanged.
686 * @hide
687 */
Chong Zhang6de2ae82015-09-30 18:25:21 -0700688 public static final int RESIZE_MODE_FORCED = (0x1 << 1);
689
690 /**
691 * Input parameter to {@link android.app.IActivityManager#resizeTask} used by window
692 * manager during a screen rotation.
693 * @hide
694 */
695 public static final int RESIZE_MODE_SYSTEM_SCREEN_ROTATION = RESIZE_MODE_PRESERVE_WINDOW;
696
697 /**
698 * Input parameter to {@link android.app.IActivityManager#resizeTask} used when the
699 * resize is due to a drag action.
700 * @hide
701 */
702 public static final int RESIZE_MODE_USER = RESIZE_MODE_PRESERVE_WINDOW;
703
704 /**
705 * Input parameter to {@link android.app.IActivityManager#resizeTask} which indicates
706 * that the resize should preserve the window if possible, and should not be skipped
707 * even if the bounds is unchanged. Usually used to force a resizing when a drag action
708 * is ending.
709 * @hide
710 */
711 public static final int RESIZE_MODE_USER_FORCED =
712 RESIZE_MODE_PRESERVE_WINDOW | RESIZE_MODE_FORCED;
Chong Zhang87b21722015-09-21 15:39:51 -0700713
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700714 /** @hide */
715 public int getFrontActivityScreenCompatMode() {
716 try {
717 return ActivityManagerNative.getDefault().getFrontActivityScreenCompatMode();
718 } catch (RemoteException e) {
719 // System dead, we will be dead too soon!
720 return 0;
721 }
722 }
723
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700724 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700725 public void setFrontActivityScreenCompatMode(int mode) {
726 try {
727 ActivityManagerNative.getDefault().setFrontActivityScreenCompatMode(mode);
728 } catch (RemoteException e) {
729 // System dead, we will be dead too soon!
730 }
731 }
732
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700733 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700734 public int getPackageScreenCompatMode(String packageName) {
735 try {
736 return ActivityManagerNative.getDefault().getPackageScreenCompatMode(packageName);
737 } catch (RemoteException e) {
738 // System dead, we will be dead too soon!
739 return 0;
740 }
741 }
742
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700743 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700744 public void setPackageScreenCompatMode(String packageName, int mode) {
745 try {
746 ActivityManagerNative.getDefault().setPackageScreenCompatMode(packageName, mode);
747 } catch (RemoteException e) {
748 // System dead, we will be dead too soon!
749 }
750 }
751
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700752 /** @hide */
753 public boolean getPackageAskScreenCompat(String packageName) {
754 try {
755 return ActivityManagerNative.getDefault().getPackageAskScreenCompat(packageName);
756 } catch (RemoteException e) {
757 // System dead, we will be dead too soon!
758 return false;
759 }
760 }
761
762 /** @hide */
763 public void setPackageAskScreenCompat(String packageName, boolean ask) {
764 try {
765 ActivityManagerNative.getDefault().setPackageAskScreenCompat(packageName, ask);
766 } catch (RemoteException e) {
767 // System dead, we will be dead too soon!
768 }
769 }
770
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700771 /**
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700772 * Return the approximate per-application memory class of the current
773 * device. This gives you an idea of how hard a memory limit you should
774 * impose on your application to let the overall system work best. The
775 * returned value is in megabytes; the baseline Android memory class is
776 * 16 (which happens to be the Java heap limit of those devices); some
777 * device with more memory may return 24 or even higher numbers.
778 */
779 public int getMemoryClass() {
780 return staticGetMemoryClass();
781 }
Stefan Kuhne16045c22015-06-05 07:18:06 -0700782
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700783 /** @hide */
784 static public int staticGetMemoryClass() {
785 // Really brain dead right now -- just take this from the configured
786 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn7ad33c82011-03-08 15:53:25 -0800787 String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", "");
Dianne Hackbornde398512011-01-18 18:45:21 -0800788 if (vmHeapSize != null && !"".equals(vmHeapSize)) {
789 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
790 }
791 return staticGetLargeMemoryClass();
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800792 }
Stefan Kuhne16045c22015-06-05 07:18:06 -0700793
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800794 /**
795 * Return the approximate per-application memory class of the current
796 * device when an application is running with a large heap. This is the
797 * space available for memory-intensive applications; most applications
798 * should not need this amount of memory, and should instead stay with the
799 * {@link #getMemoryClass()} limit. The returned value is in megabytes.
800 * This may be the same size as {@link #getMemoryClass()} on memory
801 * constrained devices, or it may be significantly larger on devices with
802 * a large amount of available RAM.
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800803 *
804 * <p>The is the size of the application's Dalvik heap if it has
805 * specified <code>android:largeHeap="true"</code> in its manifest.
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800806 */
807 public int getLargeMemoryClass() {
808 return staticGetLargeMemoryClass();
809 }
Stefan Kuhne16045c22015-06-05 07:18:06 -0700810
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800811 /** @hide */
812 static public int staticGetLargeMemoryClass() {
813 // Really brain dead right now -- just take this from the configured
814 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700815 String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
Dianne Hackborn852975d2014-08-22 17:42:43 -0700816 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length() - 1));
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700817 }
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700818
819 /**
820 * Returns true if this is a low-RAM device. Exactly whether a device is low-RAM
821 * is ultimately up to the device configuration, but currently it generally means
822 * something in the class of a 512MB device with about a 800x480 or less screen.
823 * This is mostly intended to be used by apps to determine whether they should turn
824 * off certain features that require more RAM.
825 */
826 public boolean isLowRamDevice() {
827 return isLowRamDeviceStatic();
828 }
829
830 /** @hide */
831 public static boolean isLowRamDeviceStatic() {
Christopher Tate2f61f912013-08-20 13:55:50 -0700832 return "true".equals(SystemProperties.get("ro.config.low_ram", "false"));
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700833 }
834
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700835 /**
Dianne Hackbornb3756322011-08-12 13:58:13 -0700836 * Used by persistent processes to determine if they are running on a
837 * higher-end device so should be okay using hardware drawing acceleration
838 * (which tends to consume a lot more RAM).
839 * @hide
840 */
Jeff Brown98365d72012-08-19 20:30:52 -0700841 static public boolean isHighEndGfx() {
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700842 return !isLowRamDeviceStatic() &&
843 !Resources.getSystem().getBoolean(com.android.internal.R.bool.config_avoidGfxAccel);
Dianne Hackborn49d228b32011-08-24 17:37:31 -0700844 }
845
846 /**
Dianne Hackborn852975d2014-08-22 17:42:43 -0700847 * Return the maximum number of recents entries that we will maintain and show.
848 * @hide
849 */
850 static public int getMaxRecentTasksStatic() {
851 if (gMaxRecentTasks < 0) {
852 return gMaxRecentTasks = isLowRamDeviceStatic() ? 50 : 100;
853 }
854 return gMaxRecentTasks;
855 }
856
857 /**
858 * Return the default limit on the number of recents that an app can make.
859 * @hide
860 */
861 static public int getDefaultAppRecentsLimitStatic() {
862 return getMaxRecentTasksStatic() / 6;
863 }
864
865 /**
866 * Return the maximum limit on the number of recents that an app can make.
867 * @hide
868 */
869 static public int getMaxAppRecentsLimitStatic() {
870 return getMaxRecentTasksStatic() / 2;
871 }
872
873 /**
Winson Chung1147c402014-05-14 11:05:00 -0700874 * Information you can set and retrieve about the current activity within the recent task list.
Winson Chung03a9bae2014-05-02 09:56:12 -0700875 */
Winson Chunga449dc02014-05-16 11:15:04 -0700876 public static class TaskDescription implements Parcelable {
Craig Mautner648f69b2014-09-18 14:16:26 -0700877 /** @hide */
878 public static final String ATTR_TASKDESCRIPTION_PREFIX = "task_description_";
879 private static final String ATTR_TASKDESCRIPTIONLABEL =
880 ATTR_TASKDESCRIPTION_PREFIX + "label";
Winson Chung1af8eda2016-02-05 17:55:56 +0000881 private static final String ATTR_TASKDESCRIPTIONCOLOR_PRIMARY =
Craig Mautner648f69b2014-09-18 14:16:26 -0700882 ATTR_TASKDESCRIPTION_PREFIX + "color";
Winson Chung1af8eda2016-02-05 17:55:56 +0000883 private static final String ATTR_TASKDESCRIPTIONCOLOR_BACKGROUND =
884 ATTR_TASKDESCRIPTION_PREFIX + "colorBackground";
Craig Mautner648f69b2014-09-18 14:16:26 -0700885 private static final String ATTR_TASKDESCRIPTIONICONFILENAME =
886 ATTR_TASKDESCRIPTION_PREFIX + "icon_filename";
887
Winson Chunga449dc02014-05-16 11:15:04 -0700888 private String mLabel;
889 private Bitmap mIcon;
Craig Mautner648f69b2014-09-18 14:16:26 -0700890 private String mIconFilename;
Winson Chunga449dc02014-05-16 11:15:04 -0700891 private int mColorPrimary;
Winson Chung1af8eda2016-02-05 17:55:56 +0000892 private int mColorBackground;
Winson Chung03a9bae2014-05-02 09:56:12 -0700893
894 /**
Winson Chunga449dc02014-05-16 11:15:04 -0700895 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -0700896 *
Winson Chunga449dc02014-05-16 11:15:04 -0700897 * @param label A label and description of the current state of this task.
898 * @param icon An icon that represents the current state of this task.
Winson Chung1af8eda2016-02-05 17:55:56 +0000899 * @param colorPrimary A color to override the theme's primary color. This color must be
900 * opaque.
Winson Chung03a9bae2014-05-02 09:56:12 -0700901 */
Winson Chunga449dc02014-05-16 11:15:04 -0700902 public TaskDescription(String label, Bitmap icon, int colorPrimary) {
Winson Chung1af8eda2016-02-05 17:55:56 +0000903 this(label, icon, null, colorPrimary, 0);
Winson Chunga449dc02014-05-16 11:15:04 -0700904 if ((colorPrimary != 0) && (Color.alpha(colorPrimary) != 255)) {
905 throw new RuntimeException("A TaskDescription's primary color should be opaque");
906 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700907 }
908
Winson Chung03a9bae2014-05-02 09:56:12 -0700909 /**
Winson Chunga449dc02014-05-16 11:15:04 -0700910 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -0700911 *
912 * @param label A label and description of the current state of this activity.
913 * @param icon An icon that represents the current state of this activity.
914 */
Winson Chunga449dc02014-05-16 11:15:04 -0700915 public TaskDescription(String label, Bitmap icon) {
Winson Chung1af8eda2016-02-05 17:55:56 +0000916 this(label, icon, null, 0, 0);
Winson Chung03a9bae2014-05-02 09:56:12 -0700917 }
918
919 /**
Winson Chunga449dc02014-05-16 11:15:04 -0700920 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -0700921 *
922 * @param label A label and description of the current state of this activity.
923 */
Winson Chunga449dc02014-05-16 11:15:04 -0700924 public TaskDescription(String label) {
Winson Chung1af8eda2016-02-05 17:55:56 +0000925 this(label, null, null, 0, 0);
Winson Chung03a9bae2014-05-02 09:56:12 -0700926 }
927
Winson Chunga449dc02014-05-16 11:15:04 -0700928 /**
929 * Creates an empty TaskDescription.
930 */
931 public TaskDescription() {
Winson Chung1af8eda2016-02-05 17:55:56 +0000932 this(null, null, null, 0, 0);
933 }
934
935 /** @hide */
936 public TaskDescription(String label, Bitmap icon, String iconFilename, int colorPrimary,
937 int colorBackground) {
938 mLabel = label;
939 mIcon = icon;
940 mIconFilename = iconFilename;
941 mColorPrimary = colorPrimary;
942 mColorBackground = colorBackground;
Winson Chung03a9bae2014-05-02 09:56:12 -0700943 }
944
Winson Chunga449dc02014-05-16 11:15:04 -0700945 /**
946 * Creates a copy of another TaskDescription.
947 */
948 public TaskDescription(TaskDescription td) {
Winsonb6403152016-02-23 13:32:09 -0800949 copyFrom(td);
950 }
951
952 /**
953 * Copies this the values from another TaskDescription.
954 * @hide
955 */
956 public void copyFrom(TaskDescription other) {
957 mLabel = other.mLabel;
958 mIcon = other.mIcon;
959 mIconFilename = other.mIconFilename;
960 mColorPrimary = other.mColorPrimary;
961 mColorBackground = other.mColorBackground;
Winson Chunga449dc02014-05-16 11:15:04 -0700962 }
963
964 private TaskDescription(Parcel source) {
Winson Chung03a9bae2014-05-02 09:56:12 -0700965 readFromParcel(source);
966 }
967
968 /**
Winson Chung2cb86c72014-06-25 12:03:30 -0700969 * Sets the label for this task description.
970 * @hide
971 */
972 public void setLabel(String label) {
973 mLabel = label;
974 }
975
976 /**
977 * Sets the primary color for this task description.
978 * @hide
979 */
980 public void setPrimaryColor(int primaryColor) {
Winson Chungae12dae2014-09-29 14:21:26 -0700981 // Ensure that the given color is valid
982 if ((primaryColor != 0) && (Color.alpha(primaryColor) != 255)) {
983 throw new RuntimeException("A TaskDescription's primary color should be opaque");
984 }
985 mColorPrimary = primaryColor;
Winson Chung2cb86c72014-06-25 12:03:30 -0700986 }
987
988 /**
Winson Chung1af8eda2016-02-05 17:55:56 +0000989 * Sets the background color for this task description.
990 * @hide
991 */
992 public void setBackgroundColor(int backgroundColor) {
993 // Ensure that the given color is valid
994 if ((backgroundColor != 0) && (Color.alpha(backgroundColor) != 255)) {
995 throw new RuntimeException("A TaskDescription's background color should be opaque");
996 }
997 mColorBackground = backgroundColor;
998 }
999
1000 /**
Winson Chung2cb86c72014-06-25 12:03:30 -07001001 * Sets the icon for this task description.
1002 * @hide
1003 */
1004 public void setIcon(Bitmap icon) {
1005 mIcon = icon;
1006 }
1007
1008 /**
Craig Mautner648f69b2014-09-18 14:16:26 -07001009 * Moves the icon bitmap reference from an actual Bitmap to a file containing the
1010 * bitmap.
1011 * @hide
1012 */
1013 public void setIconFilename(String iconFilename) {
1014 mIconFilename = iconFilename;
1015 mIcon = null;
1016 }
1017
1018 /**
Winson Chunga449dc02014-05-16 11:15:04 -07001019 * @return The label and description of the current state of this task.
Winson Chung03a9bae2014-05-02 09:56:12 -07001020 */
Winson Chunga449dc02014-05-16 11:15:04 -07001021 public String getLabel() {
1022 return mLabel;
1023 }
1024
1025 /**
1026 * @return The icon that represents the current state of this task.
1027 */
1028 public Bitmap getIcon() {
Craig Mautner648f69b2014-09-18 14:16:26 -07001029 if (mIcon != null) {
1030 return mIcon;
1031 }
Suprabh Shukla23593142015-11-03 17:31:15 -08001032 return loadTaskDescriptionIcon(mIconFilename, UserHandle.myUserId());
Craig Mautner648f69b2014-09-18 14:16:26 -07001033 }
1034
1035 /** @hide */
1036 public String getIconFilename() {
1037 return mIconFilename;
Winson Chunga449dc02014-05-16 11:15:04 -07001038 }
1039
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001040 /** @hide */
1041 public Bitmap getInMemoryIcon() {
1042 return mIcon;
1043 }
1044
1045 /** @hide */
Suprabh Shukla23593142015-11-03 17:31:15 -08001046 public static Bitmap loadTaskDescriptionIcon(String iconFilename, int userId) {
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001047 if (iconFilename != null) {
1048 try {
Winson Chung1af8eda2016-02-05 17:55:56 +00001049 return ActivityManagerNative.getDefault().getTaskDescriptionIcon(iconFilename,
1050 userId);
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001051 } catch (RemoteException e) {
1052 }
1053 }
1054 return null;
1055 }
1056
Winson Chunga449dc02014-05-16 11:15:04 -07001057 /**
1058 * @return The color override on the theme's primary color.
1059 */
1060 public int getPrimaryColor() {
1061 return mColorPrimary;
Winson Chung03a9bae2014-05-02 09:56:12 -07001062 }
1063
Winson Chung1af8eda2016-02-05 17:55:56 +00001064 /**
1065 * @return The background color.
1066 * @hide
1067 */
1068 public int getBackgroundColor() {
1069 return mColorBackground;
1070 }
1071
Craig Mautner648f69b2014-09-18 14:16:26 -07001072 /** @hide */
1073 public void saveToXml(XmlSerializer out) throws IOException {
1074 if (mLabel != null) {
1075 out.attribute(null, ATTR_TASKDESCRIPTIONLABEL, mLabel);
1076 }
1077 if (mColorPrimary != 0) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001078 out.attribute(null, ATTR_TASKDESCRIPTIONCOLOR_PRIMARY,
1079 Integer.toHexString(mColorPrimary));
1080 }
1081 if (mColorBackground != 0) {
1082 out.attribute(null, ATTR_TASKDESCRIPTIONCOLOR_BACKGROUND,
1083 Integer.toHexString(mColorBackground));
Craig Mautner648f69b2014-09-18 14:16:26 -07001084 }
1085 if (mIconFilename != null) {
1086 out.attribute(null, ATTR_TASKDESCRIPTIONICONFILENAME, mIconFilename);
1087 }
1088 }
1089
1090 /** @hide */
1091 public void restoreFromXml(String attrName, String attrValue) {
1092 if (ATTR_TASKDESCRIPTIONLABEL.equals(attrName)) {
1093 setLabel(attrValue);
Winson Chung1af8eda2016-02-05 17:55:56 +00001094 } else if (ATTR_TASKDESCRIPTIONCOLOR_PRIMARY.equals(attrName)) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001095 setPrimaryColor((int) Long.parseLong(attrValue, 16));
Winson Chung1af8eda2016-02-05 17:55:56 +00001096 } else if (ATTR_TASKDESCRIPTIONCOLOR_BACKGROUND.equals(attrName)) {
1097 setBackgroundColor((int) Long.parseLong(attrValue, 16));
Craig Mautner648f69b2014-09-18 14:16:26 -07001098 } else if (ATTR_TASKDESCRIPTIONICONFILENAME.equals(attrName)) {
1099 setIconFilename(attrValue);
1100 }
1101 }
1102
Winson Chung03a9bae2014-05-02 09:56:12 -07001103 @Override
1104 public int describeContents() {
1105 return 0;
1106 }
1107
1108 @Override
1109 public void writeToParcel(Parcel dest, int flags) {
Winson Chunga449dc02014-05-16 11:15:04 -07001110 if (mLabel == null) {
Winson Chung03a9bae2014-05-02 09:56:12 -07001111 dest.writeInt(0);
1112 } else {
1113 dest.writeInt(1);
Winson Chunga449dc02014-05-16 11:15:04 -07001114 dest.writeString(mLabel);
Winson Chung03a9bae2014-05-02 09:56:12 -07001115 }
Winson Chunga449dc02014-05-16 11:15:04 -07001116 if (mIcon == null) {
1117 dest.writeInt(0);
1118 } else {
1119 dest.writeInt(1);
1120 mIcon.writeToParcel(dest, 0);
1121 }
1122 dest.writeInt(mColorPrimary);
Winson Chung1af8eda2016-02-05 17:55:56 +00001123 dest.writeInt(mColorBackground);
Craig Mautner648f69b2014-09-18 14:16:26 -07001124 if (mIconFilename == null) {
1125 dest.writeInt(0);
1126 } else {
1127 dest.writeInt(1);
1128 dest.writeString(mIconFilename);
1129 }
Winson Chung03a9bae2014-05-02 09:56:12 -07001130 }
1131
1132 public void readFromParcel(Parcel source) {
Winson Chunga449dc02014-05-16 11:15:04 -07001133 mLabel = source.readInt() > 0 ? source.readString() : null;
1134 mIcon = source.readInt() > 0 ? Bitmap.CREATOR.createFromParcel(source) : null;
1135 mColorPrimary = source.readInt();
Winson Chung1af8eda2016-02-05 17:55:56 +00001136 mColorBackground = source.readInt();
Craig Mautner648f69b2014-09-18 14:16:26 -07001137 mIconFilename = source.readInt() > 0 ? source.readString() : null;
Winson Chung03a9bae2014-05-02 09:56:12 -07001138 }
1139
Winson Chunga449dc02014-05-16 11:15:04 -07001140 public static final Creator<TaskDescription> CREATOR
1141 = new Creator<TaskDescription>() {
1142 public TaskDescription createFromParcel(Parcel source) {
1143 return new TaskDescription(source);
Winson Chung03a9bae2014-05-02 09:56:12 -07001144 }
Winson Chunga449dc02014-05-16 11:15:04 -07001145 public TaskDescription[] newArray(int size) {
1146 return new TaskDescription[size];
Winson Chung03a9bae2014-05-02 09:56:12 -07001147 }
1148 };
1149
1150 @Override
1151 public String toString() {
Winson Chunga449dc02014-05-16 11:15:04 -07001152 return "TaskDescription Label: " + mLabel + " Icon: " + mIcon +
Winson Chung1af8eda2016-02-05 17:55:56 +00001153 " IconFilename: " + mIconFilename + " colorPrimary: " + mColorPrimary +
1154 " colorBackground: " + mColorBackground;
Winson Chung03a9bae2014-05-02 09:56:12 -07001155 }
1156 }
1157
1158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 * Information you can retrieve about tasks that the user has most recently
1160 * started or visited.
1161 */
1162 public static class RecentTaskInfo implements Parcelable {
1163 /**
1164 * If this task is currently running, this is the identifier for it.
1165 * If it is not running, this will be -1.
1166 */
1167 public int id;
1168
1169 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -08001170 * The true identifier of this task, valid even if it is not running.
1171 */
1172 public int persistentId;
Stefan Kuhne16045c22015-06-05 07:18:06 -07001173
Dianne Hackbornd94df452011-02-16 18:53:31 -08001174 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 * The original Intent used to launch the task. You can use this
1176 * Intent to re-launch the task (if it is no longer running) or bring
1177 * the current task to the front.
1178 */
1179 public Intent baseIntent;
1180
1181 /**
1182 * If this task was started from an alias, this is the actual
1183 * activity component that was initially started; the component of
1184 * the baseIntent in this case is the name of the actual activity
1185 * implementation that the alias referred to. Otherwise, this is null.
1186 */
1187 public ComponentName origActivity;
Dianne Hackbornd2835932010-12-13 16:28:46 -08001188
1189 /**
James Cook4d0ef042015-07-09 14:28:23 -07001190 * The actual activity component that started the task.
1191 * @hide
1192 */
1193 @Nullable
1194 public ComponentName realActivity;
1195
1196 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -08001197 * Description of the task's last state.
1198 */
1199 public CharSequence description;
Craig Mautner6d90fed2013-05-22 15:03:10 -07001200
1201 /**
1202 * The id of the ActivityStack this Task was on most recently.
Craig Mautner27030522013-08-26 12:25:36 -07001203 * @hide
Craig Mautner6d90fed2013-05-22 15:03:10 -07001204 */
1205 public int stackId;
1206
Kenny Guy82326a92014-03-17 17:16:06 +00001207 /**
Craig Mautner2fbd7542014-03-21 09:34:07 -07001208 * The id of the user the task was running as.
Kenny Guy82326a92014-03-17 17:16:06 +00001209 * @hide
1210 */
1211 public int userId;
1212
Craig Mautner2fbd7542014-03-21 09:34:07 -07001213 /**
Winson Chungffa2ec62014-07-03 15:54:42 -07001214 * The first time this task was active.
1215 * @hide
1216 */
1217 public long firstActiveTime;
1218
1219 /**
Winson Chungf1fbd772014-06-24 18:06:58 -07001220 * The last time this task was active.
1221 * @hide
1222 */
1223 public long lastActiveTime;
1224
1225 /**
Winson Chung03a9bae2014-05-02 09:56:12 -07001226 * The recent activity values for the highest activity in the stack to have set the values.
Winson Chunga449dc02014-05-16 11:15:04 -07001227 * {@link Activity#setTaskDescription(android.app.ActivityManager.TaskDescription)}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07001228 */
Winson Chunga449dc02014-05-16 11:15:04 -07001229 public TaskDescription taskDescription;
Craig Mautner2fbd7542014-03-21 09:34:07 -07001230
Craig Mautnera228ae92014-07-09 05:44:55 -07001231 /**
1232 * Task affiliation for grouping with other tasks.
Craig Mautnera228ae92014-07-09 05:44:55 -07001233 */
1234 public int affiliatedTaskId;
1235
Winson Chungec396d62014-08-06 17:08:00 -07001236 /**
1237 * Task affiliation color of the source task with the affiliated task id.
1238 *
1239 * @hide
1240 */
1241 public int affiliatedTaskColor;
1242
Wale Ogunwale6035e012015-04-14 15:54:10 -07001243 /**
1244 * The component launched as the first activity in the task.
1245 * This can be considered the "application" of this task.
1246 */
1247 public ComponentName baseActivity;
1248
1249 /**
1250 * The activity component at the top of the history stack of the task.
1251 * This is what the user is currently doing.
1252 */
1253 public ComponentName topActivity;
1254
1255 /**
1256 * Number of activities in this task.
1257 */
1258 public int numActivities;
1259
Winson2dd76942015-11-02 09:20:01 -08001260 /**
1261 * The bounds of the task.
1262 * @hide
1263 */
1264 public Rect bounds;
1265
Wale Ogunwale21b60582016-01-27 12:34:16 -08001266 /**
1267 * True if the task can go in the docked stack.
1268 * @hide
1269 */
1270 public boolean isDockable;
1271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 public RecentTaskInfo() {
1273 }
1274
Craig Mautner6d90fed2013-05-22 15:03:10 -07001275 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 public int describeContents() {
1277 return 0;
1278 }
1279
Craig Mautner6d90fed2013-05-22 15:03:10 -07001280 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 public void writeToParcel(Parcel dest, int flags) {
1282 dest.writeInt(id);
Dianne Hackbornd94df452011-02-16 18:53:31 -08001283 dest.writeInt(persistentId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 if (baseIntent != null) {
1285 dest.writeInt(1);
1286 baseIntent.writeToParcel(dest, 0);
1287 } else {
1288 dest.writeInt(0);
1289 }
1290 ComponentName.writeToParcel(origActivity, dest);
James Cook4d0ef042015-07-09 14:28:23 -07001291 ComponentName.writeToParcel(realActivity, dest);
Dianne Hackbornd2835932010-12-13 16:28:46 -08001292 TextUtils.writeToParcel(description, dest,
1293 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
Winson Chunga449dc02014-05-16 11:15:04 -07001294 if (taskDescription != null) {
Craig Mautner2fbd7542014-03-21 09:34:07 -07001295 dest.writeInt(1);
Winson Chunga449dc02014-05-16 11:15:04 -07001296 taskDescription.writeToParcel(dest, 0);
Winson Chung03a9bae2014-05-02 09:56:12 -07001297 } else {
1298 dest.writeInt(0);
Craig Mautner2fbd7542014-03-21 09:34:07 -07001299 }
Craig Mautner6d90fed2013-05-22 15:03:10 -07001300 dest.writeInt(stackId);
Kenny Guy82326a92014-03-17 17:16:06 +00001301 dest.writeInt(userId);
Winson Chungffa2ec62014-07-03 15:54:42 -07001302 dest.writeLong(firstActiveTime);
Winson Chungf1fbd772014-06-24 18:06:58 -07001303 dest.writeLong(lastActiveTime);
Craig Mautnera228ae92014-07-09 05:44:55 -07001304 dest.writeInt(affiliatedTaskId);
Winson Chungec396d62014-08-06 17:08:00 -07001305 dest.writeInt(affiliatedTaskColor);
Wale Ogunwale6035e012015-04-14 15:54:10 -07001306 ComponentName.writeToParcel(baseActivity, dest);
1307 ComponentName.writeToParcel(topActivity, dest);
1308 dest.writeInt(numActivities);
Winson2dd76942015-11-02 09:20:01 -08001309 if (bounds != null) {
1310 dest.writeInt(1);
1311 bounds.writeToParcel(dest, 0);
1312 } else {
1313 dest.writeInt(0);
1314 }
Wale Ogunwale21b60582016-01-27 12:34:16 -08001315 dest.writeInt(isDockable ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
1317
1318 public void readFromParcel(Parcel source) {
1319 id = source.readInt();
Dianne Hackbornd94df452011-02-16 18:53:31 -08001320 persistentId = source.readInt();
Craig Mautner688b5102014-03-27 16:55:03 -07001321 baseIntent = source.readInt() > 0 ? Intent.CREATOR.createFromParcel(source) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 origActivity = ComponentName.readFromParcel(source);
James Cook4d0ef042015-07-09 14:28:23 -07001323 realActivity = ComponentName.readFromParcel(source);
Dianne Hackbornd2835932010-12-13 16:28:46 -08001324 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
Winson Chunga449dc02014-05-16 11:15:04 -07001325 taskDescription = source.readInt() > 0 ?
1326 TaskDescription.CREATOR.createFromParcel(source) : null;
Craig Mautner6d90fed2013-05-22 15:03:10 -07001327 stackId = source.readInt();
Kenny Guy82326a92014-03-17 17:16:06 +00001328 userId = source.readInt();
Winson Chungffa2ec62014-07-03 15:54:42 -07001329 firstActiveTime = source.readLong();
Winson Chungf1fbd772014-06-24 18:06:58 -07001330 lastActiveTime = source.readLong();
Craig Mautnera228ae92014-07-09 05:44:55 -07001331 affiliatedTaskId = source.readInt();
Winson Chungec396d62014-08-06 17:08:00 -07001332 affiliatedTaskColor = source.readInt();
Wale Ogunwale6035e012015-04-14 15:54:10 -07001333 baseActivity = ComponentName.readFromParcel(source);
1334 topActivity = ComponentName.readFromParcel(source);
1335 numActivities = source.readInt();
Winson2dd76942015-11-02 09:20:01 -08001336 bounds = source.readInt() > 0 ?
1337 Rect.CREATOR.createFromParcel(source) : null;
Wale Ogunwale21b60582016-01-27 12:34:16 -08001338 isDockable = source.readInt() == 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
Craig Mautner6d90fed2013-05-22 15:03:10 -07001340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 public static final Creator<RecentTaskInfo> CREATOR
1342 = new Creator<RecentTaskInfo>() {
1343 public RecentTaskInfo createFromParcel(Parcel source) {
1344 return new RecentTaskInfo(source);
1345 }
1346 public RecentTaskInfo[] newArray(int size) {
1347 return new RecentTaskInfo[size];
1348 }
1349 };
1350
1351 private RecentTaskInfo(Parcel source) {
1352 readFromParcel(source);
1353 }
1354 }
1355
1356 /**
1357 * Flag for use with {@link #getRecentTasks}: return all tasks, even those
1358 * that have set their
1359 * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag.
1360 */
1361 public static final int RECENT_WITH_EXCLUDED = 0x0001;
Kenny Guy82326a92014-03-17 17:16:06 +00001362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -08001364 * Provides a list that does not contain any
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07001365 * recent tasks that currently are not available to the user.
1366 */
1367 public static final int RECENT_IGNORE_UNAVAILABLE = 0x0002;
Dianne Hackbornd2835932010-12-13 16:28:46 -08001368
1369 /**
Kenny Guy2a764942014-04-02 13:29:20 +01001370 * Provides a list that contains recent tasks for all
1371 * profiles of a user.
Kenny Guy82326a92014-03-17 17:16:06 +00001372 * @hide
1373 */
Kenny Guy2a764942014-04-02 13:29:20 +01001374 public static final int RECENT_INCLUDE_PROFILES = 0x0004;
Kenny Guy82326a92014-03-17 17:16:06 +00001375
1376 /**
Winson Chunga4ccb862014-08-22 15:26:27 -07001377 * Ignores all tasks that are on the home stack.
1378 * @hide
1379 */
1380 public static final int RECENT_IGNORE_HOME_STACK_TASKS = 0x0008;
1381
1382 /**
Winson Chung0583d3d2015-12-18 10:03:12 -05001383 * Ignores all tasks that are on the docked stack.
1384 * @hide
1385 */
1386 public static final int RECENT_INGORE_DOCKED_STACK_TASKS = 0x0010;
1387
1388 /**
1389 * Ignores all tasks that are on the pinned stack.
1390 * @hide
1391 */
1392 public static final int RECENT_INGORE_PINNED_STACK_TASKS = 0x0020;
1393
1394 /**
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001395 * <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 -08001396 * the most recent being first and older ones after in order.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001397 *
1398 * <p><b>Note: this method is only intended for debugging and presenting
1399 * task management user interfaces</b>. This should never be used for
1400 * core logic in an application, such as deciding between different
1401 * behaviors based on the information found here. Such uses are
1402 * <em>not</em> supported, and will likely break in the future. For
1403 * example, if multiple applications can be actively running at the
1404 * same time, assumptions made about the meaning of the data here for
1405 * purposes of control flow will be incorrect.</p>
1406 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001407 * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method is
Dianne Hackborn2d7576b2014-10-03 16:26:04 -07001408 * no longer available to third party applications: the introduction of
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001409 * document-centric recents means
1410 * it can leak personal information to the caller. For backwards compatibility,
1411 * it will still return a small subset of its data: at least the caller's
1412 * own tasks (though see {@link #getAppTasks()} for the correct supported
1413 * way to retrieve that information), and possibly some other tasks
1414 * such as home that are known to not be sensitive.
1415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 * @param maxNum The maximum number of entries to return in the list. The
1417 * actual number returned may be smaller, depending on how many tasks the
1418 * user has started and the maximum number the system can remember.
Dianne Hackbornd2835932010-12-13 16:28:46 -08001419 * @param flags Information about what to return. May be any combination
Dianne Hackborn805fd7e2011-01-16 18:30:29 -08001420 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
Stefan Kuhne16045c22015-06-05 07:18:06 -07001421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 * @return Returns a list of RecentTaskInfo records describing each of
1423 * the recent tasks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 */
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001425 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags)
1427 throws SecurityException {
1428 try {
1429 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001430 flags, UserHandle.myUserId());
Amith Yamasani82644082012-08-03 13:09:11 -07001431 } catch (RemoteException e) {
1432 // System dead, we will be dead too soon!
1433 return null;
1434 }
1435 }
1436
1437 /**
1438 * Same as {@link #getRecentTasks(int, int)} but returns the recent tasks for a
1439 * specific user. It requires holding
1440 * the {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission.
1441 * @param maxNum The maximum number of entries to return in the list. The
1442 * actual number returned may be smaller, depending on how many tasks the
1443 * user has started and the maximum number the system can remember.
1444 * @param flags Information about what to return. May be any combination
1445 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
1446 *
1447 * @return Returns a list of RecentTaskInfo records describing each of
Vadim Tryshev35605752015-08-31 13:28:08 -07001448 * the recent tasks. Most recently activated tasks go first.
Amith Yamasani82644082012-08-03 13:09:11 -07001449 *
Amith Yamasani82644082012-08-03 13:09:11 -07001450 * @hide
1451 */
1452 public List<RecentTaskInfo> getRecentTasksForUser(int maxNum, int flags, int userId)
1453 throws SecurityException {
1454 try {
1455 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
1456 flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 } catch (RemoteException e) {
1458 // System dead, we will be dead too soon!
1459 return null;
1460 }
1461 }
1462
1463 /**
1464 * Information you can retrieve about a particular task that is currently
1465 * "running" in the system. Note that a running task does not mean the
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001466 * given task actually has a process it is actively running in; it simply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 * means that the user has gone to it and never closed it, but currently
1468 * the system may have killed its process and is only holding on to its
1469 * last state in order to restart it when the user returns.
1470 */
1471 public static class RunningTaskInfo implements Parcelable {
1472 /**
1473 * A unique identifier for this task.
1474 */
1475 public int id;
1476
1477 /**
Winson5510f6c2015-10-27 12:11:26 -07001478 * The stack that currently contains this task.
1479 * @hide
1480 */
1481 public int stackId;
1482
1483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 * The component launched as the first activity in the task. This can
1485 * be considered the "application" of this task.
1486 */
1487 public ComponentName baseActivity;
1488
1489 /**
1490 * The activity component at the top of the history stack of the task.
1491 * This is what the user is currently doing.
1492 */
1493 public ComponentName topActivity;
1494
1495 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -08001496 * Thumbnail representation of the task's current state. Currently
1497 * always null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 */
1499 public Bitmap thumbnail;
1500
1501 /**
1502 * Description of the task's current state.
1503 */
1504 public CharSequence description;
1505
1506 /**
1507 * Number of activities in this task.
1508 */
1509 public int numActivities;
1510
1511 /**
1512 * Number of activities that are currently running (not stopped
1513 * and persisted) in this task.
1514 */
1515 public int numRunning;
1516
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001517 /**
1518 * Last time task was run. For sorting.
1519 * @hide
1520 */
1521 public long lastActiveTime;
1522
Wale Ogunwale21b60582016-01-27 12:34:16 -08001523 /**
1524 * True if the task can go in the docked stack.
1525 * @hide
1526 */
1527 public boolean isDockable;
1528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 public RunningTaskInfo() {
1530 }
1531
1532 public int describeContents() {
1533 return 0;
1534 }
1535
1536 public void writeToParcel(Parcel dest, int flags) {
1537 dest.writeInt(id);
Winson5510f6c2015-10-27 12:11:26 -07001538 dest.writeInt(stackId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 ComponentName.writeToParcel(baseActivity, dest);
1540 ComponentName.writeToParcel(topActivity, dest);
1541 if (thumbnail != null) {
1542 dest.writeInt(1);
1543 thumbnail.writeToParcel(dest, 0);
1544 } else {
1545 dest.writeInt(0);
1546 }
1547 TextUtils.writeToParcel(description, dest,
1548 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1549 dest.writeInt(numActivities);
1550 dest.writeInt(numRunning);
1551 }
1552
1553 public void readFromParcel(Parcel source) {
1554 id = source.readInt();
Winson5510f6c2015-10-27 12:11:26 -07001555 stackId = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 baseActivity = ComponentName.readFromParcel(source);
1557 topActivity = ComponentName.readFromParcel(source);
1558 if (source.readInt() != 0) {
1559 thumbnail = Bitmap.CREATOR.createFromParcel(source);
1560 } else {
1561 thumbnail = null;
1562 }
1563 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
1564 numActivities = source.readInt();
1565 numRunning = source.readInt();
1566 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07001567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() {
1569 public RunningTaskInfo createFromParcel(Parcel source) {
1570 return new RunningTaskInfo(source);
1571 }
1572 public RunningTaskInfo[] newArray(int size) {
1573 return new RunningTaskInfo[size];
1574 }
1575 };
1576
1577 private RunningTaskInfo(Parcel source) {
1578 readFromParcel(source);
1579 }
1580 }
Winson Chung1147c402014-05-14 11:05:00 -07001581
1582 /**
1583 * Get the list of tasks associated with the calling application.
1584 *
1585 * @return The list of tasks associated with the application making this call.
1586 * @throws SecurityException
1587 */
1588 public List<ActivityManager.AppTask> getAppTasks() {
1589 ArrayList<AppTask> tasks = new ArrayList<AppTask>();
1590 List<IAppTask> appTasks;
1591 try {
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001592 appTasks = ActivityManagerNative.getDefault().getAppTasks(mContext.getPackageName());
Winson Chung1147c402014-05-14 11:05:00 -07001593 } catch (RemoteException e) {
1594 // System dead, we will be dead too soon!
1595 return null;
1596 }
1597 int numAppTasks = appTasks.size();
1598 for (int i = 0; i < numAppTasks; i++) {
1599 tasks.add(new AppTask(appTasks.get(i)));
1600 }
1601 return tasks;
1602 }
1603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 /**
Winson Chung48a10a52014-08-27 14:36:51 -07001605 * Return the current design dimensions for {@link AppTask} thumbnails, for use
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001606 * with {@link #addAppTask}.
1607 */
Winson Chung48a10a52014-08-27 14:36:51 -07001608 public Size getAppTaskThumbnailSize() {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001609 synchronized (this) {
1610 ensureAppTaskThumbnailSizeLocked();
Winson Chung48a10a52014-08-27 14:36:51 -07001611 return new Size(mAppTaskThumbnailSize.x, mAppTaskThumbnailSize.y);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001612 }
1613 }
1614
1615 private void ensureAppTaskThumbnailSizeLocked() {
1616 if (mAppTaskThumbnailSize == null) {
1617 try {
1618 mAppTaskThumbnailSize = ActivityManagerNative.getDefault().getAppTaskThumbnailSize();
1619 } catch (RemoteException e) {
1620 throw new IllegalStateException("System dead?", e);
1621 }
1622 }
1623 }
1624
1625 /**
1626 * Add a new {@link AppTask} for the calling application. This will create a new
1627 * recents entry that is added to the <b>end</b> of all existing recents.
1628 *
1629 * @param activity The activity that is adding the entry. This is used to help determine
1630 * the context that the new recents entry will be in.
1631 * @param intent The Intent that describes the recents entry. This is the same Intent that
1632 * you would have used to launch the activity for it. In generally you will want to set
1633 * both {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT} and
1634 * {@link Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS}; the latter is required since this recents
1635 * entry will exist without an activity, so it doesn't make sense to not retain it when
1636 * its activity disappears. The given Intent here also must have an explicit ComponentName
1637 * set on it.
1638 * @param description Optional additional description information.
1639 * @param thumbnail Thumbnail to use for the recents entry. Should be the size given by
Winson Chung48a10a52014-08-27 14:36:51 -07001640 * {@link #getAppTaskThumbnailSize()}. If the bitmap is not that exact size, it will be
1641 * recreated in your process, probably in a way you don't like, before the recents entry
1642 * is added.
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001643 *
1644 * @return Returns the task id of the newly added app task, or -1 if the add failed. The
1645 * most likely cause of failure is that there is no more room for more tasks for your app.
1646 */
1647 public int addAppTask(@NonNull Activity activity, @NonNull Intent intent,
1648 @Nullable TaskDescription description, @NonNull Bitmap thumbnail) {
1649 Point size;
1650 synchronized (this) {
1651 ensureAppTaskThumbnailSizeLocked();
1652 size = mAppTaskThumbnailSize;
1653 }
1654 final int tw = thumbnail.getWidth();
1655 final int th = thumbnail.getHeight();
1656 if (tw != size.x || th != size.y) {
1657 Bitmap bm = Bitmap.createBitmap(size.x, size.y, thumbnail.getConfig());
1658
1659 // Use ScaleType.CENTER_CROP, except we leave the top edge at the top.
1660 float scale;
1661 float dx = 0, dy = 0;
1662 if (tw * size.x > size.y * th) {
1663 scale = (float) size.x / (float) th;
1664 dx = (size.y - tw * scale) * 0.5f;
1665 } else {
1666 scale = (float) size.y / (float) tw;
1667 dy = (size.x - th * scale) * 0.5f;
1668 }
1669 Matrix matrix = new Matrix();
1670 matrix.setScale(scale, scale);
1671 matrix.postTranslate((int) (dx + 0.5f), 0);
1672
1673 Canvas canvas = new Canvas(bm);
1674 canvas.drawBitmap(thumbnail, matrix, null);
1675 canvas.setBitmap(null);
1676
1677 thumbnail = bm;
1678 }
1679 if (description == null) {
1680 description = new TaskDescription();
1681 }
1682 try {
1683 return ActivityManagerNative.getDefault().addAppTask(activity.getActivityToken(),
1684 intent, description, thumbnail);
1685 } catch (RemoteException e) {
1686 throw new IllegalStateException("System dead?", e);
1687 }
1688 }
1689
1690 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 * Return a list of the tasks that are currently running, with
1692 * the most recent being first and older ones after in order. Note that
1693 * "running" does not mean any of the task's code is currently loaded or
1694 * activity -- the task may have been frozen by the system, so that it
1695 * can be restarted in its previous state when next brought to the
1696 * foreground.
Jim Miller0b2a6d02010-07-13 18:01:29 -07001697 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001698 * <p><b>Note: this method is only intended for debugging and presenting
1699 * task management user interfaces</b>. This should never be used for
1700 * core logic in an application, such as deciding between different
1701 * behaviors based on the information found here. Such uses are
1702 * <em>not</em> supported, and will likely break in the future. For
1703 * example, if multiple applications can be actively running at the
1704 * same time, assumptions made about the meaning of the data here for
1705 * purposes of control flow will be incorrect.</p>
1706 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001707 * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001708 * is no longer available to third party
1709 * applications: the introduction of document-centric recents means
1710 * it can leak person information to the caller. For backwards compatibility,
1711 * it will still retu rn a small subset of its data: at least the caller's
1712 * own tasks, and possibly some other tasks
1713 * such as home that are known to not be sensitive.
1714 *
Jim Miller0b2a6d02010-07-13 18:01:29 -07001715 * @param maxNum The maximum number of entries to return in the list. The
1716 * actual number returned may be smaller, depending on how many tasks the
1717 * user has started.
1718 *
1719 * @return Returns a list of RunningTaskInfo records describing each of
1720 * the running tasks.
Jim Miller0b2a6d02010-07-13 18:01:29 -07001721 */
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001722 @Deprecated
Jim Miller0b2a6d02010-07-13 18:01:29 -07001723 public List<RunningTaskInfo> getRunningTasks(int maxNum)
1724 throws SecurityException {
Dianne Hackborn09233282014-04-30 11:33:59 -07001725 try {
1726 return ActivityManagerNative.getDefault().getTasks(maxNum, 0);
1727 } catch (RemoteException e) {
1728 // System dead, we will be dead too soon!
1729 return null;
1730 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07001731 }
1732
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001733 /**
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001734 * Completely remove the given task.
1735 *
1736 * @param taskId Identifier of the task to be removed.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001737 * @return Returns true if the given task was found and removed.
1738 *
1739 * @hide
1740 */
Wale Ogunwaled54b5782014-10-23 15:55:23 -07001741 public boolean removeTask(int taskId) throws SecurityException {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001742 try {
Wale Ogunwaled54b5782014-10-23 15:55:23 -07001743 return ActivityManagerNative.getDefault().removeTask(taskId);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001744 } catch (RemoteException e) {
1745 // System dead, we will be dead too soon!
1746 return false;
1747 }
1748 }
1749
Winsonc809cbb2015-11-02 12:06:15 -08001750 /**
1751 * Metadata related to the {@link TaskThumbnail}.
1752 *
1753 * @hide
1754 */
1755 public static class TaskThumbnailInfo implements Parcelable {
1756 /** @hide */
1757 public static final String ATTR_TASK_THUMBNAILINFO_PREFIX = "task_thumbnailinfo_";
1758 private static final String ATTR_TASK_WIDTH =
1759 ATTR_TASK_THUMBNAILINFO_PREFIX + "task_width";
1760 private static final String ATTR_TASK_HEIGHT =
1761 ATTR_TASK_THUMBNAILINFO_PREFIX + "task_height";
1762 private static final String ATTR_SCREEN_ORIENTATION =
1763 ATTR_TASK_THUMBNAILINFO_PREFIX + "screen_orientation";
1764
1765 public int taskWidth;
1766 public int taskHeight;
1767 public int screenOrientation;
1768
1769 public TaskThumbnailInfo() {
1770 // Do nothing
1771 }
1772
1773 private TaskThumbnailInfo(Parcel source) {
1774 readFromParcel(source);
1775 }
1776
Winsonc6a2da02015-11-11 18:11:59 -08001777 /**
1778 * Resets this info state to the initial state.
1779 * @hide
1780 */
1781 public void reset() {
1782 taskWidth = 0;
1783 taskHeight = 0;
1784 screenOrientation = 0;
1785 }
1786
Winsonc809cbb2015-11-02 12:06:15 -08001787 /** @hide */
1788 public void saveToXml(XmlSerializer out) throws IOException {
1789 out.attribute(null, ATTR_TASK_WIDTH, Integer.toString(taskWidth));
1790 out.attribute(null, ATTR_TASK_HEIGHT, Integer.toString(taskHeight));
1791 out.attribute(null, ATTR_SCREEN_ORIENTATION, Integer.toString(screenOrientation));
1792 }
1793
1794 /** @hide */
1795 public void restoreFromXml(String attrName, String attrValue) {
1796 if (ATTR_TASK_WIDTH.equals(attrName)) {
1797 taskWidth = Integer.parseInt(attrValue);
1798 } else if (ATTR_TASK_HEIGHT.equals(attrName)) {
1799 taskHeight = Integer.parseInt(attrValue);
1800 } else if (ATTR_SCREEN_ORIENTATION.equals(attrName)) {
1801 screenOrientation = Integer.parseInt(attrValue);
1802 }
1803 }
1804
1805 public int describeContents() {
1806 return 0;
1807 }
1808
1809 public void writeToParcel(Parcel dest, int flags) {
1810 dest.writeInt(taskWidth);
1811 dest.writeInt(taskHeight);
1812 dest.writeInt(screenOrientation);
1813 }
1814
1815 public void readFromParcel(Parcel source) {
1816 taskWidth = source.readInt();
1817 taskHeight = source.readInt();
1818 screenOrientation = source.readInt();
1819 }
1820
1821 public static final Creator<TaskThumbnailInfo> CREATOR = new Creator<TaskThumbnailInfo>() {
1822 public TaskThumbnailInfo createFromParcel(Parcel source) {
1823 return new TaskThumbnailInfo(source);
1824 }
1825 public TaskThumbnailInfo[] newArray(int size) {
1826 return new TaskThumbnailInfo[size];
1827 }
1828 };
1829 }
1830
Dianne Hackbornd94df452011-02-16 18:53:31 -08001831 /** @hide */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001832 public static class TaskThumbnail implements Parcelable {
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001833 public Bitmap mainThumbnail;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001834 public ParcelFileDescriptor thumbnailFileDescriptor;
Winsonc809cbb2015-11-02 12:06:15 -08001835 public TaskThumbnailInfo thumbnailInfo;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001836
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001837 public TaskThumbnail() {
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001838 }
1839
Winsonc809cbb2015-11-02 12:06:15 -08001840 private TaskThumbnail(Parcel source) {
1841 readFromParcel(source);
1842 }
1843
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001844 public int describeContents() {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001845 if (thumbnailFileDescriptor != null) {
1846 return thumbnailFileDescriptor.describeContents();
1847 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001848 return 0;
1849 }
1850
1851 public void writeToParcel(Parcel dest, int flags) {
1852 if (mainThumbnail != null) {
1853 dest.writeInt(1);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001854 mainThumbnail.writeToParcel(dest, flags);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001855 } else {
1856 dest.writeInt(0);
1857 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001858 if (thumbnailFileDescriptor != null) {
1859 dest.writeInt(1);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001860 thumbnailFileDescriptor.writeToParcel(dest, flags);
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001861 } else {
1862 dest.writeInt(0);
1863 }
Winsonc809cbb2015-11-02 12:06:15 -08001864 if (thumbnailInfo != null) {
1865 dest.writeInt(1);
1866 thumbnailInfo.writeToParcel(dest, flags);
1867 } else {
1868 dest.writeInt(0);
1869 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001870 }
1871
1872 public void readFromParcel(Parcel source) {
1873 if (source.readInt() != 0) {
1874 mainThumbnail = Bitmap.CREATOR.createFromParcel(source);
1875 } else {
1876 mainThumbnail = null;
1877 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001878 if (source.readInt() != 0) {
1879 thumbnailFileDescriptor = ParcelFileDescriptor.CREATOR.createFromParcel(source);
1880 } else {
1881 thumbnailFileDescriptor = null;
1882 }
Winsonc809cbb2015-11-02 12:06:15 -08001883 if (source.readInt() != 0) {
1884 thumbnailInfo = TaskThumbnailInfo.CREATOR.createFromParcel(source);
1885 } else {
1886 thumbnailInfo = null;
1887 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001888 }
1889
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001890 public static final Creator<TaskThumbnail> CREATOR = new Creator<TaskThumbnail>() {
1891 public TaskThumbnail createFromParcel(Parcel source) {
1892 return new TaskThumbnail(source);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001893 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001894 public TaskThumbnail[] newArray(int size) {
1895 return new TaskThumbnail[size];
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001896 }
1897 };
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001898 }
1899
1900 /** @hide */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001901 public TaskThumbnail getTaskThumbnail(int id) throws SecurityException {
Dianne Hackbornd94df452011-02-16 18:53:31 -08001902 try {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001903 return ActivityManagerNative.getDefault().getTaskThumbnail(id);
Dianne Hackborn15491c62012-09-19 10:59:14 -07001904 } catch (RemoteException e) {
1905 // System dead, we will be dead too soon!
1906 return null;
1907 }
1908 }
1909
Winson Chung303e1ff2014-03-07 15:06:19 -08001910 /** @hide */
1911 public boolean isInHomeStack(int taskId) {
1912 try {
1913 return ActivityManagerNative.getDefault().isInHomeStack(taskId);
1914 } catch (RemoteException e) {
1915 // System dead, we will be dead too soon!
1916 return false;
1917 }
1918 }
1919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001921 * Flag for {@link #moveTaskToFront(int, int)}: also move the "home"
1922 * activity along with the task, so it is positioned immediately behind
1923 * the task.
1924 */
1925 public static final int MOVE_TASK_WITH_HOME = 0x00000001;
1926
1927 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -08001928 * Flag for {@link #moveTaskToFront(int, int)}: don't count this as a
1929 * user-instigated action, so the current activity will not receive a
1930 * hint that the user is leaving.
1931 */
1932 public static final int MOVE_TASK_NO_USER_ACTION = 0x00000002;
1933
1934 /**
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001935 * Equivalent to calling {@link #moveTaskToFront(int, int, Bundle)}
1936 * with a null options argument.
1937 *
1938 * @param taskId The identifier of the task to be moved, as found in
1939 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
1940 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -07001941 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001942 */
1943 public void moveTaskToFront(int taskId, int flags) {
1944 moveTaskToFront(taskId, flags, null);
1945 }
1946
1947 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001948 * Ask that the task associated with a given task ID be moved to the
1949 * front of the stack, so it is now visible to the user. Requires that
1950 * the caller hold permission {@link android.Manifest.permission#REORDER_TASKS}
1951 * or a SecurityException will be thrown.
1952 *
1953 * @param taskId The identifier of the task to be moved, as found in
1954 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
1955 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -07001956 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001957 * @param options Additional options for the operation, either null or
1958 * as per {@link Context#startActivity(Intent, android.os.Bundle)
1959 * Context.startActivity(Intent, Bundle)}.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001960 */
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001961 public void moveTaskToFront(int taskId, int flags, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001962 try {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001963 ActivityManagerNative.getDefault().moveTaskToFront(taskId, flags, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001964 } catch (RemoteException e) {
1965 // System dead, we will be dead too soon!
1966 }
1967 }
1968
1969 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 * Information you can retrieve about a particular Service that is
1971 * currently running in the system.
1972 */
1973 public static class RunningServiceInfo implements Parcelable {
1974 /**
1975 * The service component.
1976 */
1977 public ComponentName service;
1978
1979 /**
1980 * If non-zero, this is the process the service is running in.
1981 */
1982 public int pid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07001983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001985 * The UID that owns this service.
1986 */
1987 public int uid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07001988
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001989 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 * The name of the process this service runs in.
1991 */
1992 public String process;
Stefan Kuhne16045c22015-06-05 07:18:06 -07001993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 /**
1995 * Set to true if the service has asked to run as a foreground process.
1996 */
1997 public boolean foreground;
Stefan Kuhne16045c22015-06-05 07:18:06 -07001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002000 * The time when the service was first made active, either by someone
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002001 * starting or binding to it. This
2002 * is in units of {@link android.os.SystemClock#elapsedRealtime()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 */
2004 public long activeSince;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 /**
2007 * Set to true if this service has been explicitly started.
2008 */
2009 public boolean started;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 /**
2012 * Number of clients connected to the service.
2013 */
2014 public int clientCount;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 /**
2017 * Number of times the service's process has crashed while the service
2018 * is running.
2019 */
2020 public int crashCount;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 /**
2023 * The time when there was last activity in the service (either
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002024 * explicit requests to start it or clients binding to it). This
2025 * is in units of {@link android.os.SystemClock#uptimeMillis()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 */
2027 public long lastActivityTime;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 /**
2030 * If non-zero, this service is not currently running, but scheduled to
2031 * restart at the given time.
2032 */
2033 public long restarting;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002034
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002035 /**
2036 * Bit for {@link #flags}: set if this service has been
2037 * explicitly started.
2038 */
2039 public static final int FLAG_STARTED = 1<<0;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002040
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002041 /**
2042 * Bit for {@link #flags}: set if the service has asked to
2043 * run as a foreground process.
2044 */
2045 public static final int FLAG_FOREGROUND = 1<<1;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002046
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002047 /**
2048 * Bit for {@link #flags): set if the service is running in a
2049 * core system process.
2050 */
2051 public static final int FLAG_SYSTEM_PROCESS = 1<<2;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002052
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002053 /**
2054 * Bit for {@link #flags): set if the service is running in a
2055 * persistent process.
2056 */
2057 public static final int FLAG_PERSISTENT_PROCESS = 1<<3;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002058
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002059 /**
2060 * Running flags.
2061 */
2062 public int flags;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002063
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002064 /**
2065 * For special services that are bound to by system code, this is
2066 * the package that holds the binding.
2067 */
2068 public String clientPackage;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002069
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002070 /**
2071 * For special services that are bound to by system code, this is
2072 * a string resource providing a user-visible label for who the
2073 * client is.
2074 */
2075 public int clientLabel;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 public RunningServiceInfo() {
2078 }
2079
2080 public int describeContents() {
2081 return 0;
2082 }
2083
2084 public void writeToParcel(Parcel dest, int flags) {
2085 ComponentName.writeToParcel(service, dest);
2086 dest.writeInt(pid);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002087 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 dest.writeString(process);
2089 dest.writeInt(foreground ? 1 : 0);
2090 dest.writeLong(activeSince);
2091 dest.writeInt(started ? 1 : 0);
2092 dest.writeInt(clientCount);
2093 dest.writeInt(crashCount);
2094 dest.writeLong(lastActivityTime);
2095 dest.writeLong(restarting);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002096 dest.writeInt(this.flags);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002097 dest.writeString(clientPackage);
2098 dest.writeInt(clientLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
2100
2101 public void readFromParcel(Parcel source) {
2102 service = ComponentName.readFromParcel(source);
2103 pid = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002104 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 process = source.readString();
2106 foreground = source.readInt() != 0;
2107 activeSince = source.readLong();
2108 started = source.readInt() != 0;
2109 clientCount = source.readInt();
2110 crashCount = source.readInt();
2111 lastActivityTime = source.readLong();
2112 restarting = source.readLong();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002113 flags = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002114 clientPackage = source.readString();
2115 clientLabel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() {
2119 public RunningServiceInfo createFromParcel(Parcel source) {
2120 return new RunningServiceInfo(source);
2121 }
2122 public RunningServiceInfo[] newArray(int size) {
2123 return new RunningServiceInfo[size];
2124 }
2125 };
2126
2127 private RunningServiceInfo(Parcel source) {
2128 readFromParcel(source);
2129 }
2130 }
2131
2132 /**
2133 * Return a list of the services that are currently running.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002134 *
2135 * <p><b>Note: this method is only intended for debugging or implementing
2136 * service management type user interfaces.</b></p>
2137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 * @param maxNum The maximum number of entries to return in the list. The
2139 * actual number returned may be smaller, depending on how many services
2140 * are running.
Stefan Kuhne16045c22015-06-05 07:18:06 -07002141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 * @return Returns a list of RunningServiceInfo records describing each of
2143 * the running tasks.
2144 */
2145 public List<RunningServiceInfo> getRunningServices(int maxNum)
2146 throws SecurityException {
2147 try {
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002148 return ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 .getServices(maxNum, 0);
2150 } catch (RemoteException e) {
2151 // System dead, we will be dead too soon!
2152 return null;
2153 }
2154 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 /**
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002157 * Returns a PendingIntent you can start to show a control panel for the
2158 * given running service. If the service does not have a control panel,
2159 * null is returned.
2160 */
2161 public PendingIntent getRunningServiceControlPanel(ComponentName service)
2162 throws SecurityException {
2163 try {
2164 return ActivityManagerNative.getDefault()
2165 .getRunningServiceControlPanel(service);
2166 } catch (RemoteException e) {
2167 // System dead, we will be dead too soon!
2168 return null;
2169 }
2170 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002171
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 * Information you can retrieve about the available memory through
2174 * {@link ActivityManager#getMemoryInfo}.
2175 */
2176 public static class MemoryInfo implements Parcelable {
2177 /**
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002178 * The available memory on the system. This number should not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 * be considered absolute: due to the nature of the kernel, a significant
2180 * portion of this memory is actually in use and needed for the overall
2181 * system to run well.
2182 */
2183 public long availMem;
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002184
2185 /**
2186 * The total memory accessible by the kernel. This is basically the
2187 * RAM size of the device, not including below-kernel fixed allocations
2188 * like DMA buffers, RAM for the baseband CPU, etc.
2189 */
2190 public long totalMem;
2191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 /**
2193 * The threshold of {@link #availMem} at which we consider memory to be
2194 * low and start killing background services and other non-extraneous
2195 * processes.
2196 */
2197 public long threshold;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 /**
2200 * Set to true if the system considers itself to currently be in a low
2201 * memory situation.
2202 */
2203 public boolean lowMemory;
2204
Dianne Hackborn7d608422011-08-07 16:24:18 -07002205 /** @hide */
2206 public long hiddenAppThreshold;
2207 /** @hide */
2208 public long secondaryServerThreshold;
2209 /** @hide */
2210 public long visibleAppThreshold;
2211 /** @hide */
2212 public long foregroundAppThreshold;
2213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 public MemoryInfo() {
2215 }
2216
2217 public int describeContents() {
2218 return 0;
2219 }
2220
2221 public void writeToParcel(Parcel dest, int flags) {
2222 dest.writeLong(availMem);
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002223 dest.writeLong(totalMem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 dest.writeLong(threshold);
2225 dest.writeInt(lowMemory ? 1 : 0);
Dianne Hackborn7d608422011-08-07 16:24:18 -07002226 dest.writeLong(hiddenAppThreshold);
2227 dest.writeLong(secondaryServerThreshold);
2228 dest.writeLong(visibleAppThreshold);
2229 dest.writeLong(foregroundAppThreshold);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 public void readFromParcel(Parcel source) {
2233 availMem = source.readLong();
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002234 totalMem = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 threshold = source.readLong();
2236 lowMemory = source.readInt() != 0;
Dianne Hackborn7d608422011-08-07 16:24:18 -07002237 hiddenAppThreshold = source.readLong();
2238 secondaryServerThreshold = source.readLong();
2239 visibleAppThreshold = source.readLong();
2240 foregroundAppThreshold = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 }
2242
2243 public static final Creator<MemoryInfo> CREATOR
2244 = new Creator<MemoryInfo>() {
2245 public MemoryInfo createFromParcel(Parcel source) {
2246 return new MemoryInfo(source);
2247 }
2248 public MemoryInfo[] newArray(int size) {
2249 return new MemoryInfo[size];
2250 }
2251 };
2252
2253 private MemoryInfo(Parcel source) {
2254 readFromParcel(source);
2255 }
2256 }
2257
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002258 /**
2259 * Return general information about the memory state of the system. This
2260 * can be used to help decide how to manage your own memory, though note
2261 * that polling is not recommended and
2262 * {@link android.content.ComponentCallbacks2#onTrimMemory(int)
2263 * ComponentCallbacks2.onTrimMemory(int)} is the preferred way to do this.
2264 * Also see {@link #getMyMemoryState} for how to retrieve the current trim
2265 * level of your process as needed, which gives a better hint for how to
2266 * manage its memory.
2267 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 public void getMemoryInfo(MemoryInfo outInfo) {
2269 try {
2270 ActivityManagerNative.getDefault().getMemoryInfo(outInfo);
2271 } catch (RemoteException e) {
2272 }
2273 }
Craig Mautner967212c2013-04-13 21:10:58 -07002274
Craig Mautner5ff12102013-05-24 12:50:15 -07002275 /**
Craig Mautner967212c2013-04-13 21:10:58 -07002276 * Information you can retrieve about an ActivityStack in the system.
2277 * @hide
2278 */
2279 public static class StackInfo implements Parcelable {
2280 public int stackId;
Craig Mautnered6649f2013-12-02 14:08:25 -08002281 public Rect bounds = new Rect();
Craig Mautner967212c2013-04-13 21:10:58 -07002282 public int[] taskIds;
2283 public String[] taskNames;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002284 public Rect[] taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002285 public int[] taskUserIds;
Craig Mautnered6649f2013-12-02 14:08:25 -08002286 public int displayId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002287 public int userId;
Craig Mautner967212c2013-04-13 21:10:58 -07002288
Craig Mautner967212c2013-04-13 21:10:58 -07002289 @Override
2290 public int describeContents() {
2291 return 0;
2292 }
2293
2294 @Override
2295 public void writeToParcel(Parcel dest, int flags) {
2296 dest.writeInt(stackId);
2297 dest.writeInt(bounds.left);
2298 dest.writeInt(bounds.top);
2299 dest.writeInt(bounds.right);
2300 dest.writeInt(bounds.bottom);
2301 dest.writeIntArray(taskIds);
2302 dest.writeStringArray(taskNames);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002303 final int boundsCount = taskBounds == null ? 0 : taskBounds.length;
2304 dest.writeInt(boundsCount);
2305 for (int i = 0; i < boundsCount; i++) {
2306 dest.writeInt(taskBounds[i].left);
2307 dest.writeInt(taskBounds[i].top);
2308 dest.writeInt(taskBounds[i].right);
2309 dest.writeInt(taskBounds[i].bottom);
2310 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002311 dest.writeIntArray(taskUserIds);
Craig Mautnered6649f2013-12-02 14:08:25 -08002312 dest.writeInt(displayId);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002313 dest.writeInt(userId);
Craig Mautner967212c2013-04-13 21:10:58 -07002314 }
2315
2316 public void readFromParcel(Parcel source) {
2317 stackId = source.readInt();
2318 bounds = new Rect(
2319 source.readInt(), source.readInt(), source.readInt(), source.readInt());
2320 taskIds = source.createIntArray();
2321 taskNames = source.createStringArray();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002322 final int boundsCount = source.readInt();
2323 if (boundsCount > 0) {
2324 taskBounds = new Rect[boundsCount];
2325 for (int i = 0; i < boundsCount; i++) {
2326 taskBounds[i] = new Rect();
2327 taskBounds[i].set(
2328 source.readInt(), source.readInt(), source.readInt(), source.readInt());
2329 }
2330 } else {
2331 taskBounds = null;
2332 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002333 taskUserIds = source.createIntArray();
Craig Mautnered6649f2013-12-02 14:08:25 -08002334 displayId = source.readInt();
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002335 userId = source.readInt();
Craig Mautner967212c2013-04-13 21:10:58 -07002336 }
2337
2338 public static final Creator<StackInfo> CREATOR = new Creator<StackInfo>() {
2339 @Override
2340 public StackInfo createFromParcel(Parcel source) {
2341 return new StackInfo(source);
2342 }
2343 @Override
2344 public StackInfo[] newArray(int size) {
2345 return new StackInfo[size];
2346 }
2347 };
2348
Craig Mautner5ff12102013-05-24 12:50:15 -07002349 public StackInfo() {
2350 }
2351
Craig Mautner967212c2013-04-13 21:10:58 -07002352 private StackInfo(Parcel source) {
2353 readFromParcel(source);
2354 }
2355
Craig Mautner5ff12102013-05-24 12:50:15 -07002356 public String toString(String prefix) {
Craig Mautner967212c2013-04-13 21:10:58 -07002357 StringBuilder sb = new StringBuilder(256);
Craig Mautner5ff12102013-05-24 12:50:15 -07002358 sb.append(prefix); sb.append("Stack id="); sb.append(stackId);
Craig Mautnered6649f2013-12-02 14:08:25 -08002359 sb.append(" bounds="); sb.append(bounds.toShortString());
2360 sb.append(" displayId="); sb.append(displayId);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002361 sb.append(" userId="); sb.append(userId);
Craig Mautnered6649f2013-12-02 14:08:25 -08002362 sb.append("\n");
Craig Mautner5ff12102013-05-24 12:50:15 -07002363 prefix = prefix + " ";
Craig Mautner967212c2013-04-13 21:10:58 -07002364 for (int i = 0; i < taskIds.length; ++i) {
2365 sb.append(prefix); sb.append("taskId="); sb.append(taskIds[i]);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002366 sb.append(": "); sb.append(taskNames[i]);
2367 if (taskBounds != null) {
2368 sb.append(" bounds="); sb.append(taskBounds[i].toShortString());
2369 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002370 sb.append(" userId=").append(taskUserIds[i]);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002371 sb.append("\n");
Craig Mautner967212c2013-04-13 21:10:58 -07002372 }
2373 return sb.toString();
2374 }
Craig Mautner5ff12102013-05-24 12:50:15 -07002375
2376 @Override
2377 public String toString() {
2378 return toString("");
2379 }
Craig Mautner967212c2013-04-13 21:10:58 -07002380 }
2381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 /**
2383 * @hide
2384 */
2385 public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
2386 try {
Stefan Kuhne16045c22015-06-05 07:18:06 -07002387 return ActivityManagerNative.getDefault().clearApplicationUserData(packageName,
Dianne Hackborn41203752012-08-31 14:05:51 -07002388 observer, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 } catch (RemoteException e) {
2390 return false;
2391 }
2392 }
Christopher Tatea3664242013-06-26 15:00:18 -07002393
2394 /**
2395 * Permits an application to erase its own data from disk. This is equivalent to
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002396 * the user choosing to clear the app's data from within the device settings UI. It
2397 * erases all dynamic data associated with the app -- its private data and data in its
2398 * private area on external storage -- but does not remove the installed application
2399 * itself, nor any OBB files.
Christopher Tatea3664242013-06-26 15:00:18 -07002400 *
2401 * @return {@code true} if the application successfully requested that the application's
2402 * data be erased; {@code false} otherwise.
2403 */
2404 public boolean clearApplicationUserData() {
2405 return clearApplicationUserData(mContext.getPackageName(), null);
2406 }
2407
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002408
2409 /**
2410 * Permits an application to get the persistent URI permissions granted to another.
2411 *
2412 * <p>Typically called by Settings.
2413 *
2414 * @param packageName application to look for the granted permissions
2415 * @return list of granted URI permissions
2416 *
2417 * @hide
2418 */
2419 public ParceledListSlice<UriPermission> getGrantedUriPermissions(String packageName) {
2420 try {
2421 return ActivityManagerNative.getDefault().getGrantedUriPermissions(packageName,
2422 UserHandle.myUserId());
2423 } catch (RemoteException e) {
2424 Log.e(TAG, "Couldn't get granted URI permissions for :" + packageName, e);
2425 return ParceledListSlice.emptyList();
2426 }
2427 }
2428
2429 /**
2430 * Permits an application to clear the persistent URI permissions granted to another.
2431 *
2432 * <p>Typically called by Settings.
2433 *
2434 * @param packageName application to clear its granted permissions
2435 *
2436 * @hide
2437 */
2438 public void clearGrantedUriPermissions(String packageName) {
2439 try {
2440 ActivityManagerNative.getDefault().clearGrantedUriPermissions(packageName,
2441 UserHandle.myUserId());
2442 } catch (RemoteException e) {
2443 Log.e(TAG, "Couldn't clear granted URI permissions for :" + packageName, e);
2444 }
2445 }
2446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 /**
2448 * Information you can retrieve about any processes that are in an error condition.
2449 */
2450 public static class ProcessErrorStateInfo implements Parcelable {
2451 /**
2452 * Condition codes
2453 */
2454 public static final int NO_ERROR = 0;
2455 public static final int CRASHED = 1;
2456 public static final int NOT_RESPONDING = 2;
2457
2458 /**
2459 * The condition that the process is in.
2460 */
2461 public int condition;
2462
2463 /**
2464 * The process name in which the crash or error occurred.
2465 */
2466 public String processName;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 /**
2469 * The pid of this process; 0 if none
2470 */
2471 public int pid;
2472
2473 /**
2474 * The kernel user-ID that has been assigned to this process;
2475 * currently this is not a unique ID (multiple applications can have
2476 * the same uid).
2477 */
2478 public int uid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 /**
Dan Egnor60d87622009-12-16 16:32:58 -08002481 * The activity name associated with the error, if known. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 */
2483 public String tag;
2484
2485 /**
2486 * A short message describing the error condition.
2487 */
2488 public String shortMsg;
2489
2490 /**
2491 * A long message describing the error condition.
2492 */
2493 public String longMsg;
2494
2495 /**
Dan Egnorb7f03672009-12-09 16:22:32 -08002496 * The stack trace where the error originated. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 */
Dan Egnorb7f03672009-12-09 16:22:32 -08002498 public String stackTrace;
2499
2500 /**
2501 * to be deprecated: This value will always be null.
2502 */
2503 public byte[] crashData = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504
2505 public ProcessErrorStateInfo() {
2506 }
2507
Craig Mautner5ff12102013-05-24 12:50:15 -07002508 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 public int describeContents() {
2510 return 0;
2511 }
2512
Craig Mautner5ff12102013-05-24 12:50:15 -07002513 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 public void writeToParcel(Parcel dest, int flags) {
2515 dest.writeInt(condition);
2516 dest.writeString(processName);
2517 dest.writeInt(pid);
2518 dest.writeInt(uid);
2519 dest.writeString(tag);
2520 dest.writeString(shortMsg);
2521 dest.writeString(longMsg);
Dan Egnorb7f03672009-12-09 16:22:32 -08002522 dest.writeString(stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 }
Craig Mautner5ff12102013-05-24 12:50:15 -07002524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 public void readFromParcel(Parcel source) {
2526 condition = source.readInt();
2527 processName = source.readString();
2528 pid = source.readInt();
2529 uid = source.readInt();
2530 tag = source.readString();
2531 shortMsg = source.readString();
2532 longMsg = source.readString();
Dan Egnorb7f03672009-12-09 16:22:32 -08002533 stackTrace = source.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002535
2536 public static final Creator<ProcessErrorStateInfo> CREATOR =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 new Creator<ProcessErrorStateInfo>() {
2538 public ProcessErrorStateInfo createFromParcel(Parcel source) {
2539 return new ProcessErrorStateInfo(source);
2540 }
2541 public ProcessErrorStateInfo[] newArray(int size) {
2542 return new ProcessErrorStateInfo[size];
2543 }
2544 };
2545
2546 private ProcessErrorStateInfo(Parcel source) {
2547 readFromParcel(source);
2548 }
2549 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 /**
Stefan Kuhne16045c22015-06-05 07:18:06 -07002552 * Returns a list of any processes that are currently in an error condition. The result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 * will be null if all processes are running properly at this time.
Stefan Kuhne16045c22015-06-05 07:18:06 -07002554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 * @return Returns a list of ProcessErrorStateInfo records, or null if there are no
2556 * current error conditions (it will not return an empty list). This list ordering is not
2557 * specified.
2558 */
2559 public List<ProcessErrorStateInfo> getProcessesInErrorState() {
2560 try {
2561 return ActivityManagerNative.getDefault().getProcessesInErrorState();
2562 } catch (RemoteException e) {
2563 return null;
2564 }
2565 }
2566
2567 /**
2568 * Information you can retrieve about a running process.
2569 */
Stefan Kuhne16045c22015-06-05 07:18:06 -07002570 public static class RunningAppProcessInfo implements Parcelable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 /**
2572 * The name of the process that this object is associated with
2573 */
2574 public String processName;
2575
2576 /**
2577 * The pid of this process; 0 if none
2578 */
2579 public int pid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002580
Dianne Hackborneb034652009-09-07 00:49:58 -07002581 /**
2582 * The user id of this process.
2583 */
2584 public int uid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002585
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002586 /**
2587 * All packages that have been loaded into the process.
2588 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 public String pkgList[];
Stefan Kuhne16045c22015-06-05 07:18:06 -07002590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 /**
Dianne Hackborn482566e2010-09-03 12:51:28 -07002592 * Constant for {@link #flags}: this is an app that is unable to
2593 * correctly save its state when going to the background,
2594 * so it can not be killed while in the background.
2595 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002596 */
Dianne Hackborn482566e2010-09-03 12:51:28 -07002597 public static final int FLAG_CANT_SAVE_STATE = 1<<0;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002598
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002599 /**
Dianne Hackborn42499172010-10-15 18:45:07 -07002600 * Constant for {@link #flags}: this process is associated with a
2601 * persistent system app.
2602 * @hide
2603 */
2604 public static final int FLAG_PERSISTENT = 1<<1;
2605
2606 /**
Dianne Hackborn0c380492012-08-20 17:23:30 -07002607 * Constant for {@link #flags}: this process is associated with a
2608 * persistent system app.
2609 * @hide
2610 */
2611 public static final int FLAG_HAS_ACTIVITIES = 1<<2;
2612
2613 /**
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002614 * Flags of information. May be any of
Dianne Hackborn482566e2010-09-03 12:51:28 -07002615 * {@link #FLAG_CANT_SAVE_STATE}.
2616 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002617 */
2618 public int flags;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002619
2620 /**
2621 * Last memory trim level reported to the process: corresponds to
2622 * the values supplied to {@link android.content.ComponentCallbacks2#onTrimMemory(int)
2623 * ComponentCallbacks2.onTrimMemory(int)}.
2624 */
2625 public int lastTrimLevel;
2626
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002627 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002628 * Constant for {@link #importance}: This process is running the
2629 * foreground UI; that is, it is the thing currently at the top of the screen
2630 * that the user is interacting with.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 */
2632 public static final int IMPORTANCE_FOREGROUND = 100;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002635 * Constant for {@link #importance}: This process is running a foreground
2636 * service, for example to perform music playback even while the user is
2637 * not immediately in the app. This generally indicates that the process
2638 * is doing something the user actively cares about.
2639 */
2640 public static final int IMPORTANCE_FOREGROUND_SERVICE = 125;
2641
2642 /**
2643 * Constant for {@link #importance}: This process is running the foreground
2644 * UI, but the device is asleep so it is not visible to the user. This means
2645 * the user is not really aware of the process, because they can not see or
2646 * interact with it, but it is quite important because it what they expect to
2647 * return to once unlocking the device.
2648 */
2649 public static final int IMPORTANCE_TOP_SLEEPING = 150;
2650
2651 /**
2652 * Constant for {@link #importance}: This process is running something
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002653 * that is actively visible to the user, though not in the immediate
Dianne Hackborn1e383822015-04-10 14:02:33 -07002654 * foreground. This may be running a window that is behind the current
2655 * foreground (so paused and with its state saved, not interacting with
2656 * the user, but visible to them to some degree); it may also be running
2657 * other services under the system's control that it inconsiders important.
Dianne Hackborn860755f2010-06-03 18:47:52 -07002658 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002659 public static final int IMPORTANCE_VISIBLE = 200;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002660
Dianne Hackborn860755f2010-06-03 18:47:52 -07002661 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002662 * Constant for {@link #importance}: This process is not something the user
2663 * is directly aware of, but is otherwise perceptable to them to some degree.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002665 public static final int IMPORTANCE_PERCEPTIBLE = 130;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002666
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002667 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002668 * Constant for {@link #importance}: This process is running an
Dianne Hackborn5383f502010-10-22 12:59:20 -07002669 * application that can not save its state, and thus can't be killed
2670 * while in the background.
2671 * @hide
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002672 */
Dianne Hackborn5383f502010-10-22 12:59:20 -07002673 public static final int IMPORTANCE_CANT_SAVE_STATE = 170;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002676 * Constant for {@link #importance}: This process is contains services
2677 * that should remain running. These are background services apps have
2678 * started, not something the user is aware of, so they may be killed by
2679 * the system relatively freely (though it is generally desired that they
2680 * stay running as long as they want to).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 */
2682 public static final int IMPORTANCE_SERVICE = 300;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002685 * Constant for {@link #importance}: This process process contains
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 * background code that is expendable.
2687 */
2688 public static final int IMPORTANCE_BACKGROUND = 400;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002691 * Constant for {@link #importance}: This process is empty of any
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 * actively running code.
2693 */
2694 public static final int IMPORTANCE_EMPTY = 500;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002695
2696 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002697 * Constant for {@link #importance}: This process does not exist.
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002698 */
2699 public static final int IMPORTANCE_GONE = 1000;
2700
2701 /** @hide */
2702 public static int procStateToImportance(int procState) {
Dianne Hackborn1e383822015-04-10 14:02:33 -07002703 if (procState == PROCESS_STATE_NONEXISTENT) {
2704 return IMPORTANCE_GONE;
2705 } else if (procState >= PROCESS_STATE_HOME) {
2706 return IMPORTANCE_BACKGROUND;
2707 } else if (procState >= PROCESS_STATE_SERVICE) {
2708 return IMPORTANCE_SERVICE;
2709 } else if (procState > PROCESS_STATE_HEAVY_WEIGHT) {
2710 return IMPORTANCE_CANT_SAVE_STATE;
2711 } else if (procState >= PROCESS_STATE_IMPORTANT_BACKGROUND) {
2712 return IMPORTANCE_PERCEPTIBLE;
2713 } else if (procState >= PROCESS_STATE_IMPORTANT_FOREGROUND) {
2714 return IMPORTANCE_VISIBLE;
2715 } else if (procState >= PROCESS_STATE_TOP_SLEEPING) {
2716 return IMPORTANCE_TOP_SLEEPING;
2717 } else if (procState >= PROCESS_STATE_FOREGROUND_SERVICE) {
2718 return IMPORTANCE_FOREGROUND_SERVICE;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002719 } else {
Dianne Hackborn1e383822015-04-10 14:02:33 -07002720 return IMPORTANCE_FOREGROUND;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002721 }
2722 }
2723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 /**
2725 * The relative importance level that the system places on this
2726 * process. May be one of {@link #IMPORTANCE_FOREGROUND},
2727 * {@link #IMPORTANCE_VISIBLE}, {@link #IMPORTANCE_SERVICE},
2728 * {@link #IMPORTANCE_BACKGROUND}, or {@link #IMPORTANCE_EMPTY}. These
2729 * constants are numbered so that "more important" values are always
2730 * smaller than "less important" values.
2731 */
2732 public int importance;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 /**
2735 * An additional ordering within a particular {@link #importance}
2736 * category, providing finer-grained information about the relative
2737 * utility of processes within a category. This number means nothing
2738 * except that a smaller values are more recently used (and thus
2739 * more important). Currently an LRU value is only maintained for
2740 * the {@link #IMPORTANCE_BACKGROUND} category, though others may
2741 * be maintained in the future.
2742 */
2743 public int lru;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002744
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002745 /**
2746 * Constant for {@link #importanceReasonCode}: nothing special has
2747 * been specified for the reason for this level.
2748 */
2749 public static final int REASON_UNKNOWN = 0;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002750
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002751 /**
2752 * Constant for {@link #importanceReasonCode}: one of the application's
2753 * content providers is being used by another process. The pid of
2754 * the client process is in {@link #importanceReasonPid} and the
2755 * target provider in this process is in
2756 * {@link #importanceReasonComponent}.
2757 */
2758 public static final int REASON_PROVIDER_IN_USE = 1;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002759
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002760 /**
2761 * Constant for {@link #importanceReasonCode}: one of the application's
2762 * content providers is being used by another process. The pid of
2763 * the client process is in {@link #importanceReasonPid} and the
2764 * target provider in this process is in
2765 * {@link #importanceReasonComponent}.
2766 */
2767 public static final int REASON_SERVICE_IN_USE = 2;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002768
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002769 /**
2770 * The reason for {@link #importance}, if any.
2771 */
2772 public int importanceReasonCode;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002773
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002774 /**
2775 * For the specified values of {@link #importanceReasonCode}, this
2776 * is the process ID of the other process that is a client of this
2777 * process. This will be 0 if no other process is using this one.
2778 */
2779 public int importanceReasonPid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002780
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002781 /**
2782 * For the specified values of {@link #importanceReasonCode}, this
2783 * is the name of the component that is being used in this process.
2784 */
2785 public ComponentName importanceReasonComponent;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002786
Dianne Hackborn905577f2011-09-07 18:31:28 -07002787 /**
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002788 * When {@link #importanceReasonPid} is non-0, this is the importance
Dianne Hackborn905577f2011-09-07 18:31:28 -07002789 * of the other pid. @hide
2790 */
2791 public int importanceReasonImportance;
2792
Dianne Hackborn684bf342014-04-29 17:56:57 -07002793 /**
2794 * Current process state, as per PROCESS_STATE_* constants.
2795 * @hide
2796 */
2797 public int processState;
2798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 public RunningAppProcessInfo() {
2800 importance = IMPORTANCE_FOREGROUND;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002801 importanceReasonCode = REASON_UNKNOWN;
Dianne Hackborn684bf342014-04-29 17:56:57 -07002802 processState = PROCESS_STATE_IMPORTANT_FOREGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) {
2806 processName = pProcessName;
2807 pid = pPid;
2808 pkgList = pArr;
2809 }
2810
2811 public int describeContents() {
2812 return 0;
2813 }
2814
2815 public void writeToParcel(Parcel dest, int flags) {
2816 dest.writeString(processName);
2817 dest.writeInt(pid);
Dianne Hackborneb034652009-09-07 00:49:58 -07002818 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 dest.writeStringArray(pkgList);
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002820 dest.writeInt(this.flags);
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002821 dest.writeInt(lastTrimLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 dest.writeInt(importance);
2823 dest.writeInt(lru);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002824 dest.writeInt(importanceReasonCode);
2825 dest.writeInt(importanceReasonPid);
2826 ComponentName.writeToParcel(importanceReasonComponent, dest);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002827 dest.writeInt(importanceReasonImportance);
Dianne Hackborn684bf342014-04-29 17:56:57 -07002828 dest.writeInt(processState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 }
2830
2831 public void readFromParcel(Parcel source) {
2832 processName = source.readString();
2833 pid = source.readInt();
Dianne Hackborneb034652009-09-07 00:49:58 -07002834 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 pkgList = source.readStringArray();
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002836 flags = source.readInt();
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002837 lastTrimLevel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 importance = source.readInt();
2839 lru = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002840 importanceReasonCode = source.readInt();
2841 importanceReasonPid = source.readInt();
2842 importanceReasonComponent = ComponentName.readFromParcel(source);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002843 importanceReasonImportance = source.readInt();
Dianne Hackborn684bf342014-04-29 17:56:57 -07002844 processState = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 }
2846
Stefan Kuhne16045c22015-06-05 07:18:06 -07002847 public static final Creator<RunningAppProcessInfo> CREATOR =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 new Creator<RunningAppProcessInfo>() {
2849 public RunningAppProcessInfo createFromParcel(Parcel source) {
2850 return new RunningAppProcessInfo(source);
2851 }
2852 public RunningAppProcessInfo[] newArray(int size) {
2853 return new RunningAppProcessInfo[size];
2854 }
2855 };
2856
2857 private RunningAppProcessInfo(Parcel source) {
2858 readFromParcel(source);
2859 }
2860 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 /**
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07002863 * Returns a list of application processes installed on external media
2864 * that are running on the device.
2865 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002866 * <p><b>Note: this method is only intended for debugging or building
2867 * a user-facing process management UI.</b></p>
2868 *
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07002869 * @return Returns a list of ApplicationInfo records, or null if none
2870 * This list ordering is not specified.
2871 * @hide
2872 */
2873 public List<ApplicationInfo> getRunningExternalApplications() {
2874 try {
2875 return ActivityManagerNative.getDefault().getRunningExternalApplications();
2876 } catch (RemoteException e) {
2877 return null;
2878 }
2879 }
2880
2881 /**
Stefan Kuhne16045c22015-06-05 07:18:06 -07002882 * Sets the memory trim mode for a process and schedules a memory trim operation.
2883 *
2884 * <p><b>Note: this method is only intended for testing framework.</b></p>
2885 *
2886 * @return Returns true if successful.
2887 * @hide
2888 */
2889 public boolean setProcessMemoryTrimLevel(String process, int userId, int level) {
2890 try {
2891 return ActivityManagerNative.getDefault().setProcessMemoryTrimLevel(process, userId,
2892 level);
2893 } catch (RemoteException e) {
2894 return false;
2895 }
2896 }
2897
2898 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 * Returns a list of application processes that are running on the device.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002900 *
2901 * <p><b>Note: this method is only intended for debugging or building
2902 * a user-facing process management UI.</b></p>
2903 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 * @return Returns a list of RunningAppProcessInfo records, or null if there are no
2905 * running processes (it will not return an empty list). This list ordering is not
2906 * specified.
2907 */
2908 public List<RunningAppProcessInfo> getRunningAppProcesses() {
2909 try {
2910 return ActivityManagerNative.getDefault().getRunningAppProcesses();
2911 } catch (RemoteException e) {
2912 return null;
2913 }
2914 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002915
2916 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002917 * Return the importance of a given package name, based on the processes that are
2918 * currently running. The return value is one of the importance constants defined
2919 * in {@link RunningAppProcessInfo}, giving you the highest importance of all the
2920 * processes that this package has code running inside of. If there are no processes
2921 * running its code, {@link RunningAppProcessInfo#IMPORTANCE_GONE} is returned.
Jeff Sharkeyf85ac772015-05-13 12:52:06 -07002922 * @hide
Dianne Hackborn1e383822015-04-10 14:02:33 -07002923 */
Dianne Hackbornb7258942015-05-13 15:10:24 -07002924 @SystemApi
Dianne Hackborn1e383822015-04-10 14:02:33 -07002925 public int getPackageImportance(String packageName) {
2926 try {
Adam Lesinskic30454c2015-06-24 13:24:35 -07002927 int procState = ActivityManagerNative.getDefault().getPackageProcessState(packageName,
2928 mContext.getOpPackageName());
Dianne Hackborn1e383822015-04-10 14:02:33 -07002929 return RunningAppProcessInfo.procStateToImportance(procState);
2930 } catch (RemoteException e) {
2931 return RunningAppProcessInfo.IMPORTANCE_GONE;
2932 }
2933 }
2934
2935 /**
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002936 * Return global memory state information for the calling process. This
2937 * does not fill in all fields of the {@link RunningAppProcessInfo}. The
2938 * only fields that will be filled in are
2939 * {@link RunningAppProcessInfo#pid},
2940 * {@link RunningAppProcessInfo#uid},
2941 * {@link RunningAppProcessInfo#lastTrimLevel},
2942 * {@link RunningAppProcessInfo#importance},
2943 * {@link RunningAppProcessInfo#lru}, and
2944 * {@link RunningAppProcessInfo#importanceReasonCode}.
2945 */
2946 static public void getMyMemoryState(RunningAppProcessInfo outState) {
2947 try {
2948 ActivityManagerNative.getDefault().getMyMemoryState(outState);
2949 } catch (RemoteException e) {
2950 }
2951 }
2952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07002954 * Return information about the memory usage of one or more processes.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002955 *
2956 * <p><b>Note: this method is only intended for debugging or building
2957 * a user-facing process management UI.</b></p>
2958 *
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07002959 * @param pids The pids of the processes whose memory usage is to be
2960 * retrieved.
2961 * @return Returns an array of memory information, one for each
2962 * requested pid.
2963 */
2964 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
2965 try {
2966 return ActivityManagerNative.getDefault().getProcessMemoryInfo(pids);
2967 } catch (RemoteException e) {
2968 return null;
2969 }
2970 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002971
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07002972 /**
Dianne Hackborn03abb812010-01-04 18:43:19 -08002973 * @deprecated This is now just a wrapper for
2974 * {@link #killBackgroundProcesses(String)}; the previous behavior here
2975 * is no longer available to applications because it allows them to
2976 * break other applications by removing their alarms, stopping their
2977 * services, etc.
2978 */
2979 @Deprecated
2980 public void restartPackage(String packageName) {
2981 killBackgroundProcesses(packageName);
2982 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002983
Dianne Hackborn03abb812010-01-04 18:43:19 -08002984 /**
2985 * Have the system immediately kill all background processes associated
2986 * with the given package. This is the same as the kernel killing those
2987 * processes to reclaim memory; the system will take care of restarting
2988 * these processes in the future as needed.
Stefan Kuhne16045c22015-06-05 07:18:06 -07002989 *
Dianne Hackborn03abb812010-01-04 18:43:19 -08002990 * <p>You must hold the permission
2991 * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to
2992 * call this method.
Stefan Kuhne16045c22015-06-05 07:18:06 -07002993 *
Dianne Hackborn03abb812010-01-04 18:43:19 -08002994 * @param packageName The name of the package whose processes are to
2995 * be killed.
2996 */
2997 public void killBackgroundProcesses(String packageName) {
2998 try {
Dianne Hackborn1676c852012-09-10 14:52:30 -07002999 ActivityManagerNative.getDefault().killBackgroundProcesses(packageName,
3000 UserHandle.myUserId());
Dianne Hackborn03abb812010-01-04 18:43:19 -08003001 } catch (RemoteException e) {
3002 }
3003 }
Svet Ganov019d2302015-05-04 11:07:38 -07003004
3005 /**
3006 * Kills the specified UID.
3007 * @param uid The UID to kill.
3008 * @param reason The reason for the kill.
3009 *
3010 * @hide
3011 */
Svet Ganov019d2302015-05-04 11:07:38 -07003012 @RequiresPermission(Manifest.permission.KILL_UID)
3013 public void killUid(int uid, String reason) {
3014 try {
Svetoslavaa41add2015-08-06 15:03:55 -07003015 ActivityManagerNative.getDefault().killUid(UserHandle.getAppId(uid),
3016 UserHandle.getUserId(uid), reason);
Svet Ganov019d2302015-05-04 11:07:38 -07003017 } catch (RemoteException e) {
3018 Log.e(TAG, "Couldn't kill uid:" + uid, e);
3019 }
3020 }
3021
Dianne Hackborn03abb812010-01-04 18:43:19 -08003022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 * Have the system perform a force stop of everything associated with
3024 * the given application package. All processes that share its uid
3025 * will be killed, all services it has running stopped, all activities
3026 * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}
3027 * broadcast will be sent, so that any of its registered alarms can
3028 * be stopped, notifications removed, etc.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003029 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 * <p>You must hold the permission
Dianne Hackborn03abb812010-01-04 18:43:19 -08003031 * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 * call this method.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003033 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 * @param packageName The name of the package to be stopped.
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003035 * @param userId The user for which the running package is to be stopped.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003036 *
Dianne Hackborn03abb812010-01-04 18:43:19 -08003037 * @hide This is not available to third party applications due to
3038 * it allowing them to break other applications by stopping their
3039 * services, removing their alarms, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 */
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003041 public void forceStopPackageAsUser(String packageName, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 try {
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003043 ActivityManagerNative.getDefault().forceStopPackage(packageName, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 } catch (RemoteException e) {
3045 }
3046 }
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003047
3048 /**
3049 * @see #forceStopPackageAsUser(String, int)
3050 * @hide
3051 */
3052 public void forceStopPackage(String packageName) {
3053 forceStopPackageAsUser(packageName, UserHandle.myUserId());
3054 }
3055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 /**
3057 * Get the device configuration attributes.
3058 */
3059 public ConfigurationInfo getDeviceConfigurationInfo() {
3060 try {
3061 return ActivityManagerNative.getDefault().getDeviceConfigurationInfo();
3062 } catch (RemoteException e) {
3063 }
3064 return null;
3065 }
Kenny Root5ef44b72011-01-26 17:22:20 -08003066
3067 /**
3068 * Get the preferred density of icons for the launcher. This is used when
3069 * custom drawables are created (e.g., for shortcuts).
3070 *
3071 * @return density in terms of DPI
3072 */
3073 public int getLauncherLargeIconDensity() {
3074 final Resources res = mContext.getResources();
3075 final int density = res.getDisplayMetrics().densityDpi;
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003076 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08003077
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003078 if (sw < 600) {
3079 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08003080 return density;
3081 }
3082
3083 switch (density) {
3084 case DisplayMetrics.DENSITY_LOW:
3085 return DisplayMetrics.DENSITY_MEDIUM;
3086 case DisplayMetrics.DENSITY_MEDIUM:
3087 return DisplayMetrics.DENSITY_HIGH;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07003088 case DisplayMetrics.DENSITY_TV:
3089 return DisplayMetrics.DENSITY_XHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08003090 case DisplayMetrics.DENSITY_HIGH:
3091 return DisplayMetrics.DENSITY_XHIGH;
3092 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003093 return DisplayMetrics.DENSITY_XXHIGH;
3094 case DisplayMetrics.DENSITY_XXHIGH:
3095 return DisplayMetrics.DENSITY_XHIGH * 2;
Kenny Root5ef44b72011-01-26 17:22:20 -08003096 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003097 // The density is some abnormal value. Return some other
3098 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07003099 return (int)((density*1.5f)+.5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08003100 }
3101 }
3102
3103 /**
3104 * Get the preferred launcher icon size. This is used when custom drawables
3105 * are created (e.g., for shortcuts).
3106 *
3107 * @return dimensions of square icons in terms of pixels
3108 */
3109 public int getLauncherLargeIconSize() {
Craig Mautner2fbd7542014-03-21 09:34:07 -07003110 return getLauncherLargeIconSizeInner(mContext);
3111 }
3112
3113 static int getLauncherLargeIconSizeInner(Context context) {
3114 final Resources res = context.getResources();
Kenny Root5ef44b72011-01-26 17:22:20 -08003115 final int size = res.getDimensionPixelSize(android.R.dimen.app_icon_size);
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003116 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08003117
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003118 if (sw < 600) {
3119 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08003120 return size;
3121 }
3122
3123 final int density = res.getDisplayMetrics().densityDpi;
3124
3125 switch (density) {
3126 case DisplayMetrics.DENSITY_LOW:
3127 return (size * DisplayMetrics.DENSITY_MEDIUM) / DisplayMetrics.DENSITY_LOW;
3128 case DisplayMetrics.DENSITY_MEDIUM:
3129 return (size * DisplayMetrics.DENSITY_HIGH) / DisplayMetrics.DENSITY_MEDIUM;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07003130 case DisplayMetrics.DENSITY_TV:
3131 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08003132 case DisplayMetrics.DENSITY_HIGH:
3133 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
3134 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003135 return (size * DisplayMetrics.DENSITY_XXHIGH) / DisplayMetrics.DENSITY_XHIGH;
3136 case DisplayMetrics.DENSITY_XXHIGH:
3137 return (size * DisplayMetrics.DENSITY_XHIGH*2) / DisplayMetrics.DENSITY_XXHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08003138 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003139 // The density is some abnormal value. Return some other
3140 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07003141 return (int)((size*1.5f) + .5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08003142 }
3143 }
3144
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08003145 /**
3146 * Returns "true" if the user interface is currently being messed with
3147 * by a monkey.
3148 */
3149 public static boolean isUserAMonkey() {
3150 try {
3151 return ActivityManagerNative.getDefault().isUserAMonkey();
3152 } catch (RemoteException e) {
3153 }
3154 return false;
3155 }
Brett Chabot3b4fcbc2011-01-09 13:41:02 -08003156
3157 /**
3158 * Returns "true" if device is running in a test harness.
3159 */
3160 public static boolean isRunningInTestHarness() {
3161 return SystemProperties.getBoolean("ro.test_harness", false);
3162 }
Peter Visontay8d224ca2011-02-18 16:39:19 +00003163
3164 /**
3165 * Returns the launch count of each installed package.
3166 *
3167 * @hide
3168 */
Adam Lesinski0debc9a2014-07-16 19:09:13 -07003169 /*public Map<String, Integer> getAllPackageLaunchCounts() {
Peter Visontay8d224ca2011-02-18 16:39:19 +00003170 try {
3171 IUsageStats usageStatsService = IUsageStats.Stub.asInterface(
3172 ServiceManager.getService("usagestats"));
3173 if (usageStatsService == null) {
3174 return new HashMap<String, Integer>();
3175 }
3176
Dianne Hackborne22b3b12014-05-07 18:06:44 -07003177 UsageStats.PackageStats[] allPkgUsageStats = usageStatsService.getAllPkgUsageStats(
3178 ActivityThread.currentPackageName());
Peter Visontaybfcda392011-03-02 18:53:37 +00003179 if (allPkgUsageStats == null) {
3180 return new HashMap<String, Integer>();
3181 }
3182
Peter Visontay8d224ca2011-02-18 16:39:19 +00003183 Map<String, Integer> launchCounts = new HashMap<String, Integer>();
Dianne Hackborne22b3b12014-05-07 18:06:44 -07003184 for (UsageStats.PackageStats pkgUsageStats : allPkgUsageStats) {
3185 launchCounts.put(pkgUsageStats.getPackageName(), pkgUsageStats.getLaunchCount());
Peter Visontay8d224ca2011-02-18 16:39:19 +00003186 }
3187
3188 return launchCounts;
3189 } catch (RemoteException e) {
3190 Log.w(TAG, "Could not query launch counts", e);
3191 return new HashMap<String, Integer>();
3192 }
Adam Lesinski0debc9a2014-07-16 19:09:13 -07003193 }*/
Amith Yamasani4b2e9342011-03-31 12:38:53 -07003194
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003195 /** @hide */
3196 public static int checkComponentPermission(String permission, int uid,
3197 int owningUid, boolean exported) {
3198 // Root, system server get to do everything.
Svetoslavc6d1c342015-02-26 14:44:43 -08003199 final int appId = UserHandle.getAppId(uid);
3200 if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003201 return PackageManager.PERMISSION_GRANTED;
3202 }
3203 // Isolated processes don't get any permissions.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003204 if (UserHandle.isIsolated(uid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003205 return PackageManager.PERMISSION_DENIED;
3206 }
3207 // If there is a uid that owns whatever is being accessed, it has
3208 // blanket access to it regardless of the permissions it requires.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003209 if (owningUid >= 0 && UserHandle.isSameApp(uid, owningUid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003210 return PackageManager.PERMISSION_GRANTED;
3211 }
3212 // If the target is not exported, then nobody else can get to it.
3213 if (!exported) {
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -07003214 /*
3215 RuntimeException here = new RuntimeException("here");
3216 here.fillInStackTrace();
3217 Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid,
3218 here);
3219 */
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003220 return PackageManager.PERMISSION_DENIED;
3221 }
3222 if (permission == null) {
3223 return PackageManager.PERMISSION_GRANTED;
3224 }
3225 try {
3226 return AppGlobals.getPackageManager()
3227 .checkUidPermission(permission, uid);
3228 } catch (RemoteException e) {
3229 // Should never happen, but if it does... deny!
3230 Slog.e(TAG, "PackageManager is dead?!?", e);
3231 }
3232 return PackageManager.PERMISSION_DENIED;
3233 }
3234
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003235 /** @hide */
3236 public static int checkUidPermission(String permission, int uid) {
3237 try {
3238 return AppGlobals.getPackageManager()
3239 .checkUidPermission(permission, uid);
3240 } catch (RemoteException e) {
3241 // Should never happen, but if it does... deny!
3242 Slog.e(TAG, "PackageManager is dead?!?", e);
3243 }
3244 return PackageManager.PERMISSION_DENIED;
3245 }
3246
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08003247 /**
3248 * @hide
3249 * Helper for dealing with incoming user arguments to system service calls.
3250 * Takes care of checking permissions and converting USER_CURRENT to the
3251 * actual current user.
3252 *
3253 * @param callingPid The pid of the incoming call, as per Binder.getCallingPid().
3254 * @param callingUid The uid of the incoming call, as per Binder.getCallingUid().
3255 * @param userId The user id argument supplied by the caller -- this is the user
3256 * they want to run as.
3257 * @param allowAll If true, we will allow USER_ALL. This means you must be prepared
3258 * to get a USER_ALL returned and deal with it correctly. If false,
3259 * an exception will be thrown if USER_ALL is supplied.
3260 * @param requireFull If true, the caller must hold
3261 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} to be able to run as a
3262 * different user than their current process; otherwise they must hold
3263 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS}.
3264 * @param name Optional textual name of the incoming call; only for generating error messages.
3265 * @param callerPackage Optional package name of caller; only for error messages.
3266 *
3267 * @return Returns the user ID that the call should run as. Will always be a concrete
3268 * user number, unless <var>allowAll</var> is true in which case it could also be
3269 * USER_ALL.
3270 */
Dianne Hackborn41203752012-08-31 14:05:51 -07003271 public static int handleIncomingUser(int callingPid, int callingUid, int userId,
3272 boolean allowAll, boolean requireFull, String name, String callerPackage) {
3273 if (UserHandle.getUserId(callingUid) == userId) {
3274 return userId;
3275 }
3276 try {
3277 return ActivityManagerNative.getDefault().handleIncomingUser(callingPid,
3278 callingUid, userId, allowAll, requireFull, name, callerPackage);
3279 } catch (RemoteException e) {
3280 throw new SecurityException("Failed calling activity manager", e);
3281 }
3282 }
3283
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07003284 /**
3285 * Gets the userId of the current foreground user. Requires system permissions.
3286 * @hide
3287 */
3288 @SystemApi
Dianne Hackborn41203752012-08-31 14:05:51 -07003289 public static int getCurrentUser() {
3290 UserInfo ui;
3291 try {
3292 ui = ActivityManagerNative.getDefault().getCurrentUser();
3293 return ui != null ? ui.id : 0;
3294 } catch (RemoteException e) {
3295 return 0;
3296 }
3297 }
3298
Amith Yamasani4b2e9342011-03-31 12:38:53 -07003299 /**
Stefan Kuhne16045c22015-06-05 07:18:06 -07003300 * @param userid the user's id. Zero indicates the default user.
Amith Yamasani4b2e9342011-03-31 12:38:53 -07003301 * @hide
3302 */
3303 public boolean switchUser(int userid) {
3304 try {
3305 return ActivityManagerNative.getDefault().switchUser(userid);
3306 } catch (RemoteException e) {
3307 return false;
3308 }
3309 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003310
Jeff Sharkeye17ac152015-11-06 22:40:29 -08003311 /** {@hide} */
3312 public static final int FLAG_OR_STOPPED = 1 << 0;
3313 /** {@hide} */
Jeff Sharkey8924e872015-11-30 12:52:10 -07003314 public static final int FLAG_AND_LOCKED = 1 << 1;
Jeff Sharkey0825ab22015-12-02 13:04:49 -07003315 /** {@hide} */
3316 public static final int FLAG_AND_UNLOCKED = 1 << 2;
Jeff Sharkeye17ac152015-11-06 22:40:29 -08003317
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003318 /**
3319 * Return whether the given user is actively running. This means that
3320 * the user is in the "started" state, not "stopped" -- it is currently
3321 * allowed to run code through scheduled alarms, receiving broadcasts,
3322 * etc. A started user may be either the current foreground user or a
3323 * background user; the result here does not distinguish between the two.
3324 * @param userid the user's id. Zero indicates the default user.
3325 * @hide
3326 */
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07003327 public boolean isUserRunning(int userId) {
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003328 try {
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07003329 return ActivityManagerNative.getDefault().isUserRunning(userId, 0);
3330 } catch (RemoteException e) {
3331 return false;
3332 }
3333 }
3334
3335 /** {@hide} */
3336 public boolean isUserRunningAndLocked(int userId) {
3337 try {
3338 return ActivityManagerNative.getDefault().isUserRunning(userId,
3339 ActivityManager.FLAG_AND_LOCKED);
3340 } catch (RemoteException e) {
3341 return false;
3342 }
3343 }
3344
3345 /** {@hide} */
3346 public boolean isUserRunningAndUnlocked(int userId) {
3347 try {
3348 return ActivityManagerNative.getDefault().isUserRunning(userId,
3349 ActivityManager.FLAG_AND_UNLOCKED);
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003350 } catch (RemoteException e) {
3351 return false;
3352 }
3353 }
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003354
3355 /**
3356 * Perform a system dump of various state associated with the given application
3357 * package name. This call blocks while the dump is being performed, so should
3358 * not be done on a UI thread. The data will be written to the given file
3359 * descriptor as text. An application must hold the
3360 * {@link android.Manifest.permission#DUMP} permission to make this call.
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07003361 * @param fd The file descriptor that the dump should be written to. The file
3362 * descriptor is <em>not</em> closed by this function; the caller continues to
3363 * own it.
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003364 * @param packageName The name of the package that is to be dumped.
3365 */
3366 public void dumpPackageState(FileDescriptor fd, String packageName) {
3367 dumpPackageStateStatic(fd, packageName);
3368 }
3369
3370 /**
3371 * @hide
3372 */
3373 public static void dumpPackageStateStatic(FileDescriptor fd, String packageName) {
3374 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07003375 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07003376 dumpService(pw, fd, "package", new String[] { packageName });
3377 pw.println();
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08003378 dumpService(pw, fd, Context.ACTIVITY_SERVICE, new String[] {
3379 "-a", "package", packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003380 pw.println();
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07003381 dumpService(pw, fd, "meminfo", new String[] { "--local", "--package", packageName });
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08003382 pw.println();
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07003383 dumpService(pw, fd, ProcessStats.SERVICE_NAME, new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003384 pw.println();
Dianne Hackborn904a8572013-06-28 18:12:31 -07003385 dumpService(pw, fd, "usagestats", new String[] { "--packages", packageName });
3386 pw.println();
Dianne Hackborn8c841092013-06-24 13:46:13 -07003387 dumpService(pw, fd, BatteryStats.SERVICE_NAME, new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003388 pw.flush();
3389 }
3390
3391 private static void dumpService(PrintWriter pw, FileDescriptor fd, String name, String[] args) {
3392 pw.print("DUMP OF SERVICE "); pw.print(name); pw.println(":");
3393 IBinder service = ServiceManager.checkService(name);
3394 if (service == null) {
3395 pw.println(" (Service not found)");
3396 return;
3397 }
3398 TransferPipe tp = null;
3399 try {
3400 pw.flush();
3401 tp = new TransferPipe();
3402 tp.setBufferPrefix(" ");
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08003403 service.dumpAsync(tp.getWriteFd().getFileDescriptor(), args);
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07003404 tp.go(fd, 10000);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003405 } catch (Throwable e) {
3406 if (tp != null) {
3407 tp.kill();
3408 }
3409 pw.println("Failure dumping service:");
3410 e.printStackTrace(pw);
3411 }
3412 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003413
3414 /**
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07003415 * Request that the system start watching for the calling process to exceed a pss
Dianne Hackborn17f69352015-07-17 18:04:14 -07003416 * size as given here. Once called, the system will look for any occasions where it
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07003417 * sees the associated process with a larger pss size and, when this happens, automatically
3418 * pull a heap dump from it and allow the user to share the data. Note that this request
3419 * continues running even if the process is killed and restarted. To remove the watch,
3420 * use {@link #clearWatchHeapLimit()}.
3421 *
Dianne Hackbornece0f4f2015-06-11 13:29:01 -07003422 * <p>This API only work if the calling process has been marked as
3423 * {@link ApplicationInfo#FLAG_DEBUGGABLE} or this is running on a debuggable
3424 * (userdebug or eng) build.</p>
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07003425 *
3426 * <p>Callers can optionally implement {@link #ACTION_REPORT_HEAP_LIMIT} to directly
3427 * handle heap limit reports themselves.</p>
3428 *
3429 * @param pssSize The size in bytes to set the limit at.
3430 */
3431 public void setWatchHeapLimit(long pssSize) {
3432 try {
3433 ActivityManagerNative.getDefault().setDumpHeapDebugLimit(null, 0, pssSize,
3434 mContext.getPackageName());
3435 } catch (RemoteException e) {
3436 }
3437 }
3438
3439 /**
3440 * Action an app can implement to handle reports from {@link #setWatchHeapLimit(long)}.
3441 * If your package has an activity handling this action, it will be launched with the
3442 * heap data provided to it the same way as {@link Intent#ACTION_SEND}. Note that to
3443 * match the activty must support this action and a MIME type of "*&#47;*".
3444 */
3445 public static final String ACTION_REPORT_HEAP_LIMIT = "android.app.action.REPORT_HEAP_LIMIT";
3446
3447 /**
3448 * Clear a heap watch limit previously set by {@link #setWatchHeapLimit(long)}.
3449 */
3450 public void clearWatchHeapLimit() {
3451 try {
3452 ActivityManagerNative.getDefault().setDumpHeapDebugLimit(null, 0, 0, null);
3453 } catch (RemoteException e) {
3454 }
3455 }
3456
3457 /**
Craig Mautneraea74a52014-03-08 14:23:10 -08003458 * @hide
3459 */
3460 public void startLockTaskMode(int taskId) {
3461 try {
3462 ActivityManagerNative.getDefault().startLockTaskMode(taskId);
3463 } catch (RemoteException e) {
3464 }
3465 }
3466
3467 /**
3468 * @hide
3469 */
3470 public void stopLockTaskMode() {
3471 try {
3472 ActivityManagerNative.getDefault().stopLockTaskMode();
3473 } catch (RemoteException e) {
3474 }
3475 }
3476
3477 /**
Jason Monk386c94f2014-07-14 16:42:24 -04003478 * Return whether currently in lock task mode. When in this mode
3479 * no new tasks can be created or switched to.
3480 *
3481 * @see Activity#startLockTask()
Benjamin Franz43261142015-02-11 15:59:44 +00003482 *
3483 * @deprecated Use {@link #getLockTaskModeState} instead.
Craig Mautneraea74a52014-03-08 14:23:10 -08003484 */
3485 public boolean isInLockTaskMode() {
Benjamin Franz43261142015-02-11 15:59:44 +00003486 return getLockTaskModeState() != LOCK_TASK_MODE_NONE;
3487 }
3488
3489 /**
3490 * Return the current state of task locking. The three possible outcomes
3491 * are {@link #LOCK_TASK_MODE_NONE}, {@link #LOCK_TASK_MODE_LOCKED}
3492 * and {@link #LOCK_TASK_MODE_PINNED}.
3493 *
3494 * @see Activity#startLockTask()
3495 */
3496 public int getLockTaskModeState() {
Craig Mautneraea74a52014-03-08 14:23:10 -08003497 try {
Benjamin Franz43261142015-02-11 15:59:44 +00003498 return ActivityManagerNative.getDefault().getLockTaskModeState();
Craig Mautneraea74a52014-03-08 14:23:10 -08003499 } catch (RemoteException e) {
Benjamin Franz43261142015-02-11 15:59:44 +00003500 return ActivityManager.LOCK_TASK_MODE_NONE;
Craig Mautneraea74a52014-03-08 14:23:10 -08003501 }
3502 }
Winson Chung1147c402014-05-14 11:05:00 -07003503
3504 /**
3505 * The AppTask allows you to manage your own application's tasks.
3506 * See {@link android.app.ActivityManager#getAppTasks()}
3507 */
3508 public static class AppTask {
3509 private IAppTask mAppTaskImpl;
3510
3511 /** @hide */
3512 public AppTask(IAppTask task) {
3513 mAppTaskImpl = task;
3514 }
3515
3516 /**
3517 * Finishes all activities in this task and removes it from the recent tasks list.
3518 */
3519 public void finishAndRemoveTask() {
3520 try {
3521 mAppTaskImpl.finishAndRemoveTask();
3522 } catch (RemoteException e) {
3523 Slog.e(TAG, "Invalid AppTask", e);
3524 }
3525 }
3526
3527 /**
3528 * Get the RecentTaskInfo associated with this task.
3529 *
3530 * @return The RecentTaskInfo for this task, or null if the task no longer exists.
3531 */
3532 public RecentTaskInfo getTaskInfo() {
3533 try {
3534 return mAppTaskImpl.getTaskInfo();
3535 } catch (RemoteException e) {
3536 Slog.e(TAG, "Invalid AppTask", e);
3537 return null;
3538 }
3539 }
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07003540
3541 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003542 * Bring this task to the foreground. If it contains activities, they will be
3543 * brought to the foreground with it and their instances re-created if needed.
3544 * If it doesn't contain activities, the root activity of the task will be
3545 * re-launched.
3546 */
3547 public void moveToFront() {
3548 try {
3549 mAppTaskImpl.moveToFront();
3550 } catch (RemoteException e) {
3551 Slog.e(TAG, "Invalid AppTask", e);
3552 }
3553 }
3554
3555 /**
3556 * Start an activity in this task. Brings the task to the foreground. If this task
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003557 * is not currently active (that is, its id < 0), then a new activity for the given
3558 * Intent will be launched as the root of the task and the task brought to the
3559 * foreground. Otherwise, if this task is currently active and the Intent does not specify
3560 * an activity to launch in a new task, then a new activity for the given Intent will
3561 * be launched on top of the task and the task brought to the foreground. If this
3562 * task is currently active and the Intent specifies {@link Intent#FLAG_ACTIVITY_NEW_TASK}
3563 * or would otherwise be launched in to a new task, then the activity not launched but
3564 * this task be brought to the foreground and a new intent delivered to the top
3565 * activity if appropriate.
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003566 *
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003567 * <p>In other words, you generally want to use an Intent here that does not specify
3568 * {@link Intent#FLAG_ACTIVITY_NEW_TASK} or {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT},
3569 * and let the system do the right thing.</p>
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003570 *
3571 * @param intent The Intent describing the new activity to be launched on the task.
3572 * @param options Optional launch options.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003573 *
3574 * @see Activity#startActivity(android.content.Intent, android.os.Bundle)
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003575 */
3576 public void startActivity(Context context, Intent intent, Bundle options) {
3577 ActivityThread thread = ActivityThread.currentActivityThread();
3578 thread.getInstrumentation().execStartActivityFromAppTask(context,
3579 thread.getApplicationThread(), mAppTaskImpl, intent, options);
3580 }
3581
3582 /**
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07003583 * Modify the {@link Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag in the root
3584 * Intent of this AppTask.
3585 *
3586 * @param exclude If true, {@link Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} will
3587 * be set; otherwise, it will be cleared.
3588 */
3589 public void setExcludeFromRecents(boolean exclude) {
3590 try {
3591 mAppTaskImpl.setExcludeFromRecents(exclude);
3592 } catch (RemoteException e) {
3593 Slog.e(TAG, "Invalid AppTask", e);
3594 }
3595 }
Winson Chung1147c402014-05-14 11:05:00 -07003596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597}