blob: c55d3b8208d7a68bfe6714028da9e43722de7ded [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 Hackborn058f1e42016-11-02 17:18:35 -070025import android.annotation.TestApi;
Jorim Jaggi29379ec2016-04-11 23:43:42 -070026import android.content.pm.ActivityInfo;
Winson21700932016-03-24 17:26:23 -070027import android.content.res.Configuration;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -070028import android.graphics.Canvas;
29import android.graphics.Matrix;
30import android.graphics.Point;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070031import android.os.BatteryStats;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070032import android.os.IBinder;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070033import android.os.ParcelFileDescriptor;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070034
Joe Onorato4eb64fd2016-03-21 15:30:09 -070035import com.android.internal.app.procstats.ProcessStats;
John Reckaa67f682016-09-20 14:24:21 -070036import com.android.internal.os.RoSystemProperties;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070037import com.android.internal.os.TransferPipe;
Dianne Hackborn8c841092013-06-24 13:46:13 -070038import com.android.internal.util.FastPrintWriter;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.ComponentName;
41import android.content.Context;
42import android.content.Intent;
Felipe Lemef3fa0f82016-01-07 12:08:19 -080043import android.content.UriPermission;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -070044import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.pm.ConfigurationInfo;
46import android.content.pm.IPackageDataObserver;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070047import android.content.pm.PackageManager;
Felipe Lemef3fa0f82016-01-07 12:08:19 -080048import android.content.pm.ParceledListSlice;
Dianne Hackborn41203752012-08-31 14:05:51 -070049import android.content.pm.UserInfo;
Kenny Root5ef44b72011-01-26 17:22:20 -080050import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.graphics.Bitmap;
Winson Chunga449dc02014-05-16 11:15:04 -070052import android.graphics.Color;
Craig Mautner967212c2013-04-13 21:10:58 -070053import android.graphics.Rect;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -070054import android.os.Bundle;
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -070055import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Handler;
57import android.os.Parcel;
58import android.os.Parcelable;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070059import android.os.Process;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070060import android.os.RemoteException;
Peter Visontay8d224ca2011-02-18 16:39:19 +000061import android.os.ServiceManager;
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -070062import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070063import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.text.TextUtils;
Dianne Hackborn058f1e42016-11-02 17:18:35 -070065import android.util.ArrayMap;
Kenny Root5ef44b72011-01-26 17:22:20 -080066import android.util.DisplayMetrics;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070067import android.util.Singleton;
Winson Chung48a10a52014-08-27 14:36:51 -070068import android.util.Size;
Felipe Lemef3fa0f82016-01-07 12:08:19 -080069
Craig Mautner648f69b2014-09-18 14:16:26 -070070import org.xmlpull.v1.XmlSerializer;
Kenny Root5ef44b72011-01-26 17:22:20 -080071
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070072import java.io.FileDescriptor;
73import java.io.FileOutputStream;
Craig Mautner648f69b2014-09-18 14:16:26 -070074import java.io.IOException;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070075import java.io.PrintWriter;
Michal Karpinski3da5c972015-12-11 18:16:30 +000076import java.lang.annotation.Retention;
77import java.lang.annotation.RetentionPolicy;
Winson Chung1147c402014-05-14 11:05:00 -070078import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import java.util.List;
80
81/**
82 * Interact with the overall activities running in the system.
83 */
84public class ActivityManager {
85 private static String TAG = "ActivityManager";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Dianne Hackborn852975d2014-08-22 17:42:43 -070087 private static int gMaxRecentTasks = -1;
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private final Context mContext;
90 private final Handler mHandler;
91
Dianne Hackborn058f1e42016-11-02 17:18:35 -070092 static final class UidObserver extends IUidObserver.Stub {
93 final OnUidImportanceListener mListener;
Dianne Hackborn058f1e42016-11-02 17:18:35 -070094
Dianne Hackborn5614bf52016-11-07 17:26:41 -080095 UidObserver(OnUidImportanceListener listener) {
Dianne Hackborn058f1e42016-11-02 17:18:35 -070096 mListener = listener;
Dianne Hackborn058f1e42016-11-02 17:18:35 -070097 }
98
99 @Override
100 public void onUidStateChanged(int uid, int procState) {
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800101 mListener.onUidImportance(uid, RunningAppProcessInfo.procStateToImportance(procState));
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700102 }
103
104 @Override
Dianne Hackborne07641d2016-11-09 15:07:23 -0800105 public void onUidGone(int uid, boolean disabled) {
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700106 mListener.onUidImportance(uid, RunningAppProcessInfo.IMPORTANCE_GONE);
107 }
108
109 @Override
110 public void onUidActive(int uid) {
111 }
112
113 @Override
Dianne Hackborne07641d2016-11-09 15:07:23 -0800114 public void onUidIdle(int uid, boolean disabled) {
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700115 }
116 }
117
118 final ArrayMap<OnUidImportanceListener, UidObserver> mImportanceListeners = new ArrayMap<>();
119
Felipe Lemedc7af962016-01-22 18:27:03 -0800120 /**
121 * Defines acceptable types of bugreports.
122 * @hide
123 */
Michal Karpinski3da5c972015-12-11 18:16:30 +0000124 @Retention(RetentionPolicy.SOURCE)
125 @IntDef({
126 BUGREPORT_OPTION_FULL,
127 BUGREPORT_OPTION_INTERACTIVE,
Wei Liu967fc8d2016-07-08 11:44:20 -0700128 BUGREPORT_OPTION_REMOTE,
129 BUGREPORT_OPTION_WEAR
Michal Karpinski3da5c972015-12-11 18:16:30 +0000130 })
Michal Karpinski3da5c972015-12-11 18:16:30 +0000131 public @interface BugreportMode {}
132 /**
133 * Takes a bugreport without user interference (and hence causing less
134 * interference to the system), but includes all sections.
135 * @hide
136 */
137 public static final int BUGREPORT_OPTION_FULL = 0;
138 /**
139 * Allows user to monitor progress and enter additional data; might not include all
140 * sections.
141 * @hide
142 */
143 public static final int BUGREPORT_OPTION_INTERACTIVE = 1;
144 /**
145 * Takes a bugreport requested remotely by administrator of the Device Owner app,
146 * not the device's user.
147 * @hide
148 */
149 public static final int BUGREPORT_OPTION_REMOTE = 2;
Wei Liu967fc8d2016-07-08 11:44:20 -0700150 /**
151 * Takes a bugreport on a wearable device.
152 * @hide
153 */
154 public static final int BUGREPORT_OPTION_WEAR = 3;
Michal Karpinski3da5c972015-12-11 18:16:30 +0000155
Dianne Hackborna4972e92012-03-14 10:38:05 -0700156 /**
Scott Maincc2195b2013-10-16 13:57:46 -0700157 * <a href="{@docRoot}guide/topics/manifest/meta-data-element.html">{@code
Neil Fuller71fbb812015-11-30 09:51:33 +0000158 * <meta-data>}</a> name for a 'home' Activity that declares a package that is to be
Christopher Tatebd413f62013-09-18 18:31:59 -0700159 * uninstalled in lieu of the declaring one. The package named here must be
Neil Fuller71fbb812015-11-30 09:51:33 +0000160 * signed with the same certificate as the one declaring the {@code <meta-data>}.
Christopher Tatebd413f62013-09-18 18:31:59 -0700161 */
162 public static final String META_HOME_ALTERNATE = "android.app.home.alternate";
163
164 /**
Amith Yamasani42449782016-04-19 11:45:51 -0700165 * Result for IActivityManager.startVoiceActivity: active session is currently hidden.
166 * @hide
167 */
168 public static final int START_VOICE_HIDDEN_SESSION = -10;
169
170 /**
171 * Result for IActivityManager.startVoiceActivity: active session does not match
172 * the requesting token.
173 * @hide
174 */
175 public static final int START_VOICE_NOT_ACTIVE_SESSION = -9;
176
177 /**
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700178 * Result for IActivityManager.startActivity: trying to start a background user
179 * activity that shouldn't be displayed for all users.
180 * @hide
181 */
182 public static final int START_NOT_CURRENT_USER_ACTIVITY = -8;
183
184 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700185 * Result for IActivityManager.startActivity: trying to start an activity under voice
186 * control when that activity does not support the VOICE category.
187 * @hide
188 */
189 public static final int START_NOT_VOICE_COMPATIBLE = -7;
190
191 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700192 * Result for IActivityManager.startActivity: an error where the
193 * start had to be canceled.
194 * @hide
195 */
196 public static final int START_CANCELED = -6;
197
198 /**
199 * Result for IActivityManager.startActivity: an error where the
200 * thing being started is not an activity.
201 * @hide
202 */
203 public static final int START_NOT_ACTIVITY = -5;
204
205 /**
206 * Result for IActivityManager.startActivity: an error where the
207 * caller does not have permission to start the activity.
208 * @hide
209 */
210 public static final int START_PERMISSION_DENIED = -4;
211
212 /**
213 * Result for IActivityManager.startActivity: an error where the
214 * caller has requested both to forward a result and to receive
215 * a result.
216 * @hide
217 */
218 public static final int START_FORWARD_AND_REQUEST_CONFLICT = -3;
219
220 /**
221 * Result for IActivityManager.startActivity: an error where the
222 * requested class is not found.
223 * @hide
224 */
225 public static final int START_CLASS_NOT_FOUND = -2;
226
227 /**
228 * Result for IActivityManager.startActivity: an error where the
229 * given Intent could not be resolved to an activity.
230 * @hide
231 */
232 public static final int START_INTENT_NOT_RESOLVED = -1;
233
234 /**
235 * Result for IActivityManaqer.startActivity: the activity was started
236 * successfully as normal.
237 * @hide
238 */
239 public static final int START_SUCCESS = 0;
240
241 /**
242 * Result for IActivityManaqer.startActivity: the caller asked that the Intent not
243 * be executed if it is the recipient, and that is indeed the case.
244 * @hide
245 */
246 public static final int START_RETURN_INTENT_TO_CALLER = 1;
247
248 /**
249 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
250 * a task was simply brought to the foreground.
251 * @hide
252 */
253 public static final int START_TASK_TO_FRONT = 2;
254
255 /**
256 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
257 * the given Intent was given to the existing top activity.
258 * @hide
259 */
260 public static final int START_DELIVERED_TO_TOP = 3;
261
262 /**
263 * Result for IActivityManaqer.startActivity: request was canceled because
264 * app switches are temporarily canceled to ensure the user's last request
265 * (such as pressing home) is performed.
266 * @hide
267 */
268 public static final int START_SWITCHES_CANCELED = 4;
269
270 /**
Craig Mautneraea74a52014-03-08 14:23:10 -0800271 * Result for IActivityManaqer.startActivity: a new activity was attempted to be started
272 * while in Lock Task Mode.
273 * @hide
274 */
275 public static final int START_RETURN_LOCK_TASK_MODE_VIOLATION = 5;
276
277 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700278 * Flag for IActivityManaqer.startActivity: do special start mode where
279 * a new activity is launched only if it is needed.
280 * @hide
281 */
282 public static final int START_FLAG_ONLY_IF_NEEDED = 1<<0;
283
284 /**
285 * Flag for IActivityManaqer.startActivity: launch the app for
286 * debugging.
287 * @hide
288 */
289 public static final int START_FLAG_DEBUG = 1<<1;
290
291 /**
292 * Flag for IActivityManaqer.startActivity: launch the app for
Man Caocfa78b22015-06-11 20:14:34 -0700293 * allocation tracking.
294 * @hide
295 */
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700296 public static final int START_FLAG_TRACK_ALLOCATION = 1<<2;
Man Caocfa78b22015-06-11 20:14:34 -0700297
298 /**
Tamas Berghammerdf6cb282016-01-29 12:07:00 +0000299 * Flag for IActivityManaqer.startActivity: launch the app with
300 * native debugging support.
301 * @hide
302 */
303 public static final int START_FLAG_NATIVE_DEBUGGING = 1<<3;
304
305 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700306 * Result for IActivityManaqer.broadcastIntent: success!
307 * @hide
308 */
309 public static final int BROADCAST_SUCCESS = 0;
310
311 /**
312 * Result for IActivityManaqer.broadcastIntent: attempt to broadcast
313 * a sticky intent without appropriate permission.
314 * @hide
315 */
316 public static final int BROADCAST_STICKY_CANT_HAVE_PERMISSION = -1;
317
318 /**
Amith Yamasani83b6ef02014-11-07 15:34:04 -0800319 * Result for IActivityManager.broadcastIntent: trying to send a broadcast
320 * to a stopped user. Fail.
321 * @hide
322 */
323 public static final int BROADCAST_FAILED_USER_STOPPED = -2;
324
325 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700326 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
327 * for a sendBroadcast operation.
328 * @hide
329 */
330 public static final int INTENT_SENDER_BROADCAST = 1;
331
332 /**
333 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
334 * for a startActivity operation.
335 * @hide
336 */
337 public static final int INTENT_SENDER_ACTIVITY = 2;
338
339 /**
340 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
341 * for an activity result operation.
342 * @hide
343 */
344 public static final int INTENT_SENDER_ACTIVITY_RESULT = 3;
345
346 /**
347 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
348 * for a startService operation.
349 * @hide
350 */
351 public static final int INTENT_SENDER_SERVICE = 4;
352
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700353 /** @hide User operation call: success! */
354 public static final int USER_OP_SUCCESS = 0;
355
356 /** @hide User operation call: given user id is not known. */
357 public static final int USER_OP_UNKNOWN_USER = -1;
358
359 /** @hide User operation call: given user id is the current user, can't be stopped. */
360 public static final int USER_OP_IS_CURRENT = -2;
361
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700362 /** @hide User operation call: system user can't be stopped. */
363 public static final int USER_OP_ERROR_IS_SYSTEM = -3;
364
365 /** @hide User operation call: one of related users cannot be stopped. */
366 public static final int USER_OP_ERROR_RELATED_USERS_CANNOT_STOP = -4;
367
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800368 /** @hide Not a real process state. */
369 public static final int PROCESS_STATE_UNKNOWN = -1;
Ruben Brunka27eef42015-01-28 15:04:16 -0800370
Dianne Hackborna413dc02013-07-12 12:02:55 -0700371 /** @hide Process is a persistent system process. */
372 public static final int PROCESS_STATE_PERSISTENT = 0;
373
374 /** @hide Process is a persistent system process and is doing UI. */
375 public static final int PROCESS_STATE_PERSISTENT_UI = 1;
376
Dianne Hackbornc8230512013-07-13 21:32:12 -0700377 /** @hide Process is hosting the current top activities. Note that this covers
378 * all activities that are visible to the user. */
Dianne Hackborna413dc02013-07-12 12:02:55 -0700379 public static final int PROCESS_STATE_TOP = 2;
380
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700381 /** @hide Process is hosting a foreground service due to a system binding. */
382 public static final int PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 3;
383
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700384 /** @hide Process is hosting a foreground service. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700385 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 4;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700386
387 /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700388 public static final int PROCESS_STATE_TOP_SLEEPING = 5;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700389
Dianne Hackborna413dc02013-07-12 12:02:55 -0700390 /** @hide Process is important to the user, and something they are aware of. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700391 public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 6;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700392
393 /** @hide Process is important to the user, but not something they are aware of. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700394 public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 7;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700395
Dianne Hackborna413dc02013-07-12 12:02:55 -0700396 /** @hide Process is in the background running a backup/restore operation. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700397 public static final int PROCESS_STATE_BACKUP = 8;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700398
Dianne Hackbornc8230512013-07-13 21:32:12 -0700399 /** @hide Process is in the background, but it can't restore its state so we want
400 * to try to avoid killing it. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700401 public static final int PROCESS_STATE_HEAVY_WEIGHT = 9;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700402
403 /** @hide Process is in the background running a service. Unlike oom_adj, this level
404 * is used for both the normal running in background state and the executing
405 * operations state. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700406 public static final int PROCESS_STATE_SERVICE = 10;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700407
Dianne Hackbornc8230512013-07-13 21:32:12 -0700408 /** @hide Process is in the background running a receiver. Note that from the
409 * perspective of oom_adj receivers run at a higher foreground level, but for our
410 * prioritization here that is not necessary and putting them below services means
411 * many fewer changes in some process states as they receive broadcasts. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700412 public static final int PROCESS_STATE_RECEIVER = 11;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700413
Dianne Hackborna413dc02013-07-12 12:02:55 -0700414 /** @hide Process is in the background but hosts the home activity. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700415 public static final int PROCESS_STATE_HOME = 12;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700416
417 /** @hide Process is in the background but hosts the last shown activity. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700418 public static final int PROCESS_STATE_LAST_ACTIVITY = 13;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700419
Dianne Hackbornc8230512013-07-13 21:32:12 -0700420 /** @hide Process is being cached for later use and contains activities. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700421 public static final int PROCESS_STATE_CACHED_ACTIVITY = 14;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700422
423 /** @hide Process is being cached for later use and is a client of another cached
424 * process that contains activities. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700425 public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 15;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700426
427 /** @hide Process is being cached for later use and is empty. */
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700428 public static final int PROCESS_STATE_CACHED_EMPTY = 16;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700429
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800430 /** @hide Process does not exist. */
431 public static final int PROCESS_STATE_NONEXISTENT = 17;
432
Joe Onorato05c9ecc2016-03-30 15:13:46 -0700433 /** @hide The lowest process state number */
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800434 public static final int MIN_PROCESS_STATE = PROCESS_STATE_PERSISTENT;
Joe Onorato05c9ecc2016-03-30 15:13:46 -0700435
436 /** @hide The highest process state number */
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800437 public static final int MAX_PROCESS_STATE = PROCESS_STATE_NONEXISTENT;
Joe Onorato05c9ecc2016-03-30 15:13:46 -0700438
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700439 /** @hide Should this process state be considered a background state? */
440 public static final boolean isProcStateBackground(int procState) {
441 return procState >= PROCESS_STATE_BACKUP;
442 }
443
Dianne Hackborna83ce1d2015-03-11 15:16:13 -0700444 /** @hide requestType for assist context: only basic information. */
445 public static final int ASSIST_CONTEXT_BASIC = 0;
446
447 /** @hide requestType for assist context: generate full AssistStructure. */
448 public static final int ASSIST_CONTEXT_FULL = 1;
449
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700450 /** @hide requestType for assist context: generate full AssistStructure for auto-fill. */
451 public static final int ASSIST_CONTEXT_AUTOFILL = 2;
452
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700453 /** @hide Flag for registerUidObserver: report changes in process state. */
454 public static final int UID_OBSERVER_PROCSTATE = 1<<0;
455
456 /** @hide Flag for registerUidObserver: report uid gone. */
457 public static final int UID_OBSERVER_GONE = 1<<1;
458
459 /** @hide Flag for registerUidObserver: report uid has become idle. */
460 public static final int UID_OBSERVER_IDLE = 1<<2;
461
462 /** @hide Flag for registerUidObserver: report uid has become active. */
463 public static final int UID_OBSERVER_ACTIVE = 1<<3;
464
465 /** @hide Mode for {@link IActivityManager#getAppStartMode}: normal free-to-run operation. */
466 public static final int APP_START_MODE_NORMAL = 0;
467
468 /** @hide Mode for {@link IActivityManager#getAppStartMode}: delay running until later. */
469 public static final int APP_START_MODE_DELAYED = 1;
470
471 /** @hide Mode for {@link IActivityManager#getAppStartMode}: disable/cancel pending
472 * launches. */
473 public static final int APP_START_MODE_DISABLED = 2;
474
Benjamin Franz43261142015-02-11 15:59:44 +0000475 /**
476 * Lock task mode is not active.
477 */
478 public static final int LOCK_TASK_MODE_NONE = 0;
479
480 /**
481 * Full lock task mode is active.
482 */
483 public static final int LOCK_TASK_MODE_LOCKED = 1;
484
485 /**
486 * App pinning mode is active.
487 */
488 public static final int LOCK_TASK_MODE_PINNED = 2;
489
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700490 Point mAppTaskThumbnailSize;
491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 /*package*/ ActivityManager(Context context, Handler handler) {
493 mContext = context;
494 mHandler = handler;
495 }
496
497 /**
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700498 * Screen compatibility mode: the application most always run in
499 * compatibility mode.
500 * @hide
501 */
502 public static final int COMPAT_MODE_ALWAYS = -1;
503
504 /**
505 * Screen compatibility mode: the application can never run in
506 * compatibility mode.
507 * @hide
508 */
509 public static final int COMPAT_MODE_NEVER = -2;
510
511 /**
512 * Screen compatibility mode: unknown.
513 * @hide
514 */
515 public static final int COMPAT_MODE_UNKNOWN = -3;
516
517 /**
518 * Screen compatibility mode: the application currently has compatibility
519 * mode disabled.
520 * @hide
521 */
522 public static final int COMPAT_MODE_DISABLED = 0;
523
524 /**
525 * Screen compatibility mode: the application currently has compatibility
526 * mode enabled.
527 * @hide
528 */
529 public static final int COMPAT_MODE_ENABLED = 1;
530
531 /**
532 * Screen compatibility mode: request to toggle the application's
533 * compatibility mode.
534 * @hide
535 */
536 public static final int COMPAT_MODE_TOGGLE = 2;
537
Wale Ogunwale3797c222015-10-27 14:21:58 -0700538 /** @hide */
539 public static class StackId {
540 /** Invalid stack ID. */
541 public static final int INVALID_STACK_ID = -1;
Chong Zhang5dcb2752015-08-18 13:50:26 -0700542
Wale Ogunwale3797c222015-10-27 14:21:58 -0700543 /** First static stack ID. */
544 public static final int FIRST_STATIC_STACK_ID = 0;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700545
Wale Ogunwale3797c222015-10-27 14:21:58 -0700546 /** Home activity stack ID. */
547 public static final int HOME_STACK_ID = FIRST_STATIC_STACK_ID;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700548
Wale Ogunwale3797c222015-10-27 14:21:58 -0700549 /** ID of stack where fullscreen activities are normally launched into. */
550 public static final int FULLSCREEN_WORKSPACE_STACK_ID = 1;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700551
Wale Ogunwale3797c222015-10-27 14:21:58 -0700552 /** ID of stack where freeform/resized activities are normally launched into. */
553 public static final int FREEFORM_WORKSPACE_STACK_ID = FULLSCREEN_WORKSPACE_STACK_ID + 1;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700554
Wale Ogunwale3797c222015-10-27 14:21:58 -0700555 /** ID of stack that occupies a dedicated region of the screen. */
556 public static final int DOCKED_STACK_ID = FREEFORM_WORKSPACE_STACK_ID + 1;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700557
Wale Ogunwale3797c222015-10-27 14:21:58 -0700558 /** ID of stack that always on top (always visible) when it exist. */
559 public static final int PINNED_STACK_ID = DOCKED_STACK_ID + 1;
Wale Ogunwale99db1862015-10-23 20:08:22 -0700560
Wale Ogunwale3797c222015-10-27 14:21:58 -0700561 /** Last static stack stack ID. */
562 public static final int LAST_STATIC_STACK_ID = PINNED_STACK_ID;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700563
Wale Ogunwale3797c222015-10-27 14:21:58 -0700564 /** Start of ID range used by stacks that are created dynamically. */
565 public static final int FIRST_DYNAMIC_STACK_ID = LAST_STATIC_STACK_ID + 1;
566
567 public static boolean isStaticStack(int stackId) {
568 return stackId >= FIRST_STATIC_STACK_ID && stackId <= LAST_STATIC_STACK_ID;
569 }
570
571 /**
572 * Returns true if the activities contained in the input stack display a shadow around
573 * their border.
574 */
575 public static boolean hasWindowShadow(int stackId) {
576 return stackId == FREEFORM_WORKSPACE_STACK_ID || stackId == PINNED_STACK_ID;
577 }
578
579 /**
580 * Returns true if the activities contained in the input stack display a decor view.
581 */
582 public static boolean hasWindowDecor(int stackId) {
583 return stackId == FREEFORM_WORKSPACE_STACK_ID;
584 }
585
586 /**
587 * Returns true if the tasks contained in the stack can be resized independently of the
588 * stack.
589 */
590 public static boolean isTaskResizeAllowed(int stackId) {
591 return stackId == FREEFORM_WORKSPACE_STACK_ID;
592 }
593
Wale Ogunwale807d8822015-12-15 17:05:09 -0800594 /** Returns true if the task bounds should persist across power cycles. */
Wale Ogunwale3797c222015-10-27 14:21:58 -0700595 public static boolean persistTaskBounds(int stackId) {
Wale Ogunwale807d8822015-12-15 17:05:09 -0800596 return stackId == FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700597 }
598
599 /**
600 * Returns true if dynamic stacks are allowed to be visible behind the input stack.
601 */
602 public static boolean isDynamicStacksVisibleBehindAllowed(int stackId) {
603 return stackId == PINNED_STACK_ID;
604 }
605
606 /**
607 * Returns true if we try to maintain focus in the current stack when the top activity
608 * finishes.
609 */
610 public static boolean keepFocusInStackIfPossible(int stackId) {
611 return stackId == FREEFORM_WORKSPACE_STACK_ID
612 || stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID;
613 }
614
615 /**
616 * Returns true if Stack size is affected by the docked stack changing size.
617 */
618 public static boolean isResizeableByDockedStack(int stackId) {
619 return isStaticStack(stackId) &&
620 stackId != DOCKED_STACK_ID && stackId != PINNED_STACK_ID;
621 }
622
623 /**
624 * Returns true if the size of tasks in the input stack are affected by the docked stack
625 * changing size.
626 */
627 public static boolean isTaskResizeableByDockedStack(int stackId) {
628 return isStaticStack(stackId) && stackId != FREEFORM_WORKSPACE_STACK_ID
629 && stackId != DOCKED_STACK_ID && stackId != PINNED_STACK_ID;
630 }
631
632 /**
Jorim Jaggie9098022016-01-27 19:29:40 -0800633 * Returns true if the windows of tasks being moved to the target stack from the source
634 * stack should be replaced, meaning that window manager will keep the old window around
635 * until the new is ready.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700636 */
Jorim Jaggie9098022016-01-27 19:29:40 -0800637 public static boolean replaceWindowsOnTaskMove(int sourceStackId, int targetStackId) {
638 return sourceStackId == FREEFORM_WORKSPACE_STACK_ID
639 || targetStackId == FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700640 }
Filip Gruszczynski96daf322015-11-18 18:01:27 -0800641
642 /**
Robert Carre6275582016-02-29 15:45:45 -0800643 * Return whether a stackId is a stack containing floating windows. Floating windows
644 * are laid out differently as they are allowed to extend past the display bounds
645 * without overscan insets.
646 */
647 public static boolean tasksAreFloating(int stackId) {
648 return stackId == FREEFORM_WORKSPACE_STACK_ID
649 || stackId == PINNED_STACK_ID;
650 }
651
652 /**
Filip Gruszczynski96daf322015-11-18 18:01:27 -0800653 * Returns true if animation specs should be constructed for app transition that moves
654 * the task to the specified stack.
655 */
656 public static boolean useAnimationSpecForAppTransition(int stackId) {
Jorim Jaggi8a5c6402016-02-12 16:55:18 -0800657
658 // TODO: INVALID_STACK_ID is also animated because we don't persist stack id's across
659 // reboots.
Filip Gruszczynski96daf322015-11-18 18:01:27 -0800660 return stackId == FREEFORM_WORKSPACE_STACK_ID
Jorim Jaggi8a5c6402016-02-12 16:55:18 -0800661 || stackId == FULLSCREEN_WORKSPACE_STACK_ID || stackId == DOCKED_STACK_ID
662 || stackId == INVALID_STACK_ID;
Filip Gruszczynski96daf322015-11-18 18:01:27 -0800663 }
Wale Ogunwaled045c822015-12-02 09:14:28 -0800664
665 /** Returns true if the windows in the stack can receive input keys. */
666 public static boolean canReceiveKeys(int stackId) {
667 return stackId != PINNED_STACK_ID;
668 }
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800669
Chong Zhang75b37202015-12-04 14:16:36 -0800670 /**
671 * Returns true if the stack can be visible above lockscreen.
672 */
673 public static boolean isAllowedOverLockscreen(int stackId) {
674 return stackId == HOME_STACK_ID || stackId == FULLSCREEN_WORKSPACE_STACK_ID;
675 }
676
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800677 public static boolean isAlwaysOnTop(int stackId) {
678 return stackId == PINNED_STACK_ID;
679 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800680
681 /**
682 * Returns true if the top task in the task is allowed to return home when finished and
683 * there are other tasks in the stack.
684 */
685 public static boolean allowTopTaskToReturnHome(int stackId) {
686 return stackId != PINNED_STACK_ID;
687 }
Wale Ogunwalecacfaa22016-01-15 11:26:08 -0800688
689 /**
690 * Returns true if the stack should be resized to match the bounds specified by
691 * {@link ActivityOptions#setLaunchBounds} when launching an activity into the stack.
692 */
693 public static boolean resizeStackWithLaunchBounds(int stackId) {
694 return stackId == PINNED_STACK_ID;
695 }
Wale Ogunwale3fd20fe2016-01-23 17:41:28 -0800696
697 /**
698 * Returns true if any visible windows belonging to apps in this stack should be kept on
699 * screen when the app is killed due to something like the low memory killer.
700 */
701 public static boolean keepVisibleDeadAppWindowOnScreen(int stackId) {
702 return stackId != PINNED_STACK_ID;
703 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800704
705 /**
706 * Returns true if the backdrop on the client side should match the frame of the window.
707 * Returns false, if the backdrop should be fullscreen.
708 */
709 public static boolean useWindowFrameForBackdrop(int stackId) {
710 return stackId == FREEFORM_WORKSPACE_STACK_ID || stackId == PINNED_STACK_ID;
711 }
Jorim Jaggi86905582016-02-09 21:36:09 -0800712
713 /**
714 * Returns true if a window from the specified stack with {@param stackId} are normally
715 * fullscreen, i. e. they can become the top opaque fullscreen window, meaning that it
716 * controls system bars, lockscreen occluded/dismissing state, screen rotation animation,
717 * etc.
718 */
719 public static boolean normallyFullscreenWindows(int stackId) {
720 return stackId != PINNED_STACK_ID && stackId != FREEFORM_WORKSPACE_STACK_ID
721 && stackId != DOCKED_STACK_ID;
722 }
Wale Ogunwalefb1c8642016-03-02 08:28:08 -0800723
724 /**
725 * Returns true if the input stack id should only be present on a device that supports
726 * multi-window mode.
727 * @see android.app.ActivityManager#supportsMultiWindow
728 */
729 public static boolean isMultiWindowStack(int stackId) {
730 return isStaticStack(stackId) || stackId == PINNED_STACK_ID
731 || stackId == FREEFORM_WORKSPACE_STACK_ID || stackId == DOCKED_STACK_ID;
732 }
Wale Ogunwale43896cf2016-04-16 10:54:30 -0700733
734 /**
735 * Returns true if activities contained in this stack can request visible behind by
736 * calling {@link Activity#requestVisibleBehind}.
737 */
738 public static boolean activitiesCanRequestVisibleBehind(int stackId) {
739 return stackId == FULLSCREEN_WORKSPACE_STACK_ID;
740 }
Robert Carr1b5ea722016-04-20 13:23:28 -0700741
742 /**
743 * Returns true if this stack may be scaled without resizing,
744 * and windows within may need to be configured as such.
745 */
746 public static boolean windowsAreScaleable(int stackId) {
747 return stackId == PINNED_STACK_ID;
748 }
Robert Carrc7294602016-05-13 11:32:05 -0700749
750 /**
751 * Returns true if windows in this stack should be given move animations
752 * by default.
753 */
754 public static boolean hasMovementAnimations(int stackId) {
755 return stackId != PINNED_STACK_ID;
756 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700757
758 /** Returns true if the input stack and its content can affect the device orientation. */
759 public static boolean canSpecifyOrientation(int stackId) {
760 return stackId == HOME_STACK_ID || stackId == FULLSCREEN_WORKSPACE_STACK_ID;
761 }
Wale Ogunwale3797c222015-10-27 14:21:58 -0700762 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700763
Wale Ogunwale59a73ca2015-09-14 12:54:50 -0700764 /**
765 * Input parameter to {@link android.app.IActivityManager#moveTaskToDockedStack} which
766 * specifies the position of the created docked stack at the top half of the screen if
767 * in portrait mode or at the left half of the screen if in landscape mode.
768 * @hide
769 */
770 public static final int DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT = 0;
771
772 /**
773 * Input parameter to {@link android.app.IActivityManager#moveTaskToDockedStack} which
774 * specifies the position of the created docked stack at the bottom half of the screen if
775 * in portrait mode or at the right half of the screen if in landscape mode.
776 * @hide
777 */
778 public static final int DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT = 1;
779
Chong Zhang87b21722015-09-21 15:39:51 -0700780 /**
781 * Input parameter to {@link android.app.IActivityManager#resizeTask} which indicates
Chong Zhang6de2ae82015-09-30 18:25:21 -0700782 * that the resize doesn't need to preserve the window, and can be skipped if bounds
783 * is unchanged. This mode is used by window manager in most cases.
Chong Zhang87b21722015-09-21 15:39:51 -0700784 * @hide
785 */
786 public static final int RESIZE_MODE_SYSTEM = 0;
787
788 /**
789 * Input parameter to {@link android.app.IActivityManager#resizeTask} which indicates
Chong Zhang6de2ae82015-09-30 18:25:21 -0700790 * that the resize should preserve the window if possible.
Wale Ogunwale1ed0d892015-09-28 13:27:44 -0700791 * @hide
792 */
Chong Zhang6de2ae82015-09-30 18:25:21 -0700793 public static final int RESIZE_MODE_PRESERVE_WINDOW = (0x1 << 0);
Chong Zhang87b21722015-09-21 15:39:51 -0700794
795 /**
796 * Input parameter to {@link android.app.IActivityManager#resizeTask} which indicates
797 * that the resize should be performed even if the bounds appears unchanged.
798 * @hide
799 */
Chong Zhang6de2ae82015-09-30 18:25:21 -0700800 public static final int RESIZE_MODE_FORCED = (0x1 << 1);
801
802 /**
803 * Input parameter to {@link android.app.IActivityManager#resizeTask} used by window
804 * manager during a screen rotation.
805 * @hide
806 */
807 public static final int RESIZE_MODE_SYSTEM_SCREEN_ROTATION = RESIZE_MODE_PRESERVE_WINDOW;
808
809 /**
810 * Input parameter to {@link android.app.IActivityManager#resizeTask} used when the
811 * resize is due to a drag action.
812 * @hide
813 */
814 public static final int RESIZE_MODE_USER = RESIZE_MODE_PRESERVE_WINDOW;
815
816 /**
817 * Input parameter to {@link android.app.IActivityManager#resizeTask} which indicates
818 * that the resize should preserve the window if possible, and should not be skipped
819 * even if the bounds is unchanged. Usually used to force a resizing when a drag action
820 * is ending.
821 * @hide
822 */
823 public static final int RESIZE_MODE_USER_FORCED =
824 RESIZE_MODE_PRESERVE_WINDOW | RESIZE_MODE_FORCED;
Chong Zhang87b21722015-09-21 15:39:51 -0700825
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700826 /** @hide */
827 public int getFrontActivityScreenCompatMode() {
828 try {
829 return ActivityManagerNative.getDefault().getFrontActivityScreenCompatMode();
830 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700831 throw e.rethrowFromSystemServer();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700832 }
833 }
834
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700835 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700836 public void setFrontActivityScreenCompatMode(int mode) {
837 try {
838 ActivityManagerNative.getDefault().setFrontActivityScreenCompatMode(mode);
839 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700840 throw e.rethrowFromSystemServer();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700841 }
842 }
843
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700844 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700845 public int getPackageScreenCompatMode(String packageName) {
846 try {
847 return ActivityManagerNative.getDefault().getPackageScreenCompatMode(packageName);
848 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700849 throw e.rethrowFromSystemServer();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700850 }
851 }
852
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700853 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700854 public void setPackageScreenCompatMode(String packageName, int mode) {
855 try {
856 ActivityManagerNative.getDefault().setPackageScreenCompatMode(packageName, mode);
857 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700858 throw e.rethrowFromSystemServer();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700859 }
860 }
861
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700862 /** @hide */
863 public boolean getPackageAskScreenCompat(String packageName) {
864 try {
865 return ActivityManagerNative.getDefault().getPackageAskScreenCompat(packageName);
866 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700867 throw e.rethrowFromSystemServer();
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700868 }
869 }
870
871 /** @hide */
872 public void setPackageAskScreenCompat(String packageName, boolean ask) {
873 try {
874 ActivityManagerNative.getDefault().setPackageAskScreenCompat(packageName, ask);
875 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700876 throw e.rethrowFromSystemServer();
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700877 }
878 }
879
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700880 /**
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700881 * Return the approximate per-application memory class of the current
882 * device. This gives you an idea of how hard a memory limit you should
883 * impose on your application to let the overall system work best. The
884 * returned value is in megabytes; the baseline Android memory class is
885 * 16 (which happens to be the Java heap limit of those devices); some
886 * device with more memory may return 24 or even higher numbers.
887 */
888 public int getMemoryClass() {
889 return staticGetMemoryClass();
890 }
Stefan Kuhne16045c22015-06-05 07:18:06 -0700891
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700892 /** @hide */
893 static public int staticGetMemoryClass() {
894 // Really brain dead right now -- just take this from the configured
895 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn7ad33c82011-03-08 15:53:25 -0800896 String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", "");
Dianne Hackbornde398512011-01-18 18:45:21 -0800897 if (vmHeapSize != null && !"".equals(vmHeapSize)) {
898 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
899 }
900 return staticGetLargeMemoryClass();
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800901 }
Stefan Kuhne16045c22015-06-05 07:18:06 -0700902
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800903 /**
904 * Return the approximate per-application memory class of the current
905 * device when an application is running with a large heap. This is the
906 * space available for memory-intensive applications; most applications
907 * should not need this amount of memory, and should instead stay with the
908 * {@link #getMemoryClass()} limit. The returned value is in megabytes.
909 * This may be the same size as {@link #getMemoryClass()} on memory
910 * constrained devices, or it may be significantly larger on devices with
911 * a large amount of available RAM.
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800912 *
913 * <p>The is the size of the application's Dalvik heap if it has
914 * specified <code>android:largeHeap="true"</code> in its manifest.
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800915 */
916 public int getLargeMemoryClass() {
917 return staticGetLargeMemoryClass();
918 }
Stefan Kuhne16045c22015-06-05 07:18:06 -0700919
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800920 /** @hide */
921 static public int staticGetLargeMemoryClass() {
922 // Really brain dead right now -- just take this from the configured
923 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700924 String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
Dianne Hackborn852975d2014-08-22 17:42:43 -0700925 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length() - 1));
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700926 }
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700927
928 /**
929 * Returns true if this is a low-RAM device. Exactly whether a device is low-RAM
930 * is ultimately up to the device configuration, but currently it generally means
931 * something in the class of a 512MB device with about a 800x480 or less screen.
932 * This is mostly intended to be used by apps to determine whether they should turn
933 * off certain features that require more RAM.
934 */
935 public boolean isLowRamDevice() {
936 return isLowRamDeviceStatic();
937 }
938
939 /** @hide */
940 public static boolean isLowRamDeviceStatic() {
John Reckaa67f682016-09-20 14:24:21 -0700941 return RoSystemProperties.CONFIG_LOW_RAM;
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700942 }
943
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700944 /**
Dianne Hackbornb3756322011-08-12 13:58:13 -0700945 * Used by persistent processes to determine if they are running on a
946 * higher-end device so should be okay using hardware drawing acceleration
947 * (which tends to consume a lot more RAM).
948 * @hide
949 */
Jeff Brown98365d72012-08-19 20:30:52 -0700950 static public boolean isHighEndGfx() {
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700951 return !isLowRamDeviceStatic() &&
952 !Resources.getSystem().getBoolean(com.android.internal.R.bool.config_avoidGfxAccel);
Dianne Hackborn49d228b32011-08-24 17:37:31 -0700953 }
954
955 /**
Dianne Hackborn852975d2014-08-22 17:42:43 -0700956 * Return the maximum number of recents entries that we will maintain and show.
957 * @hide
958 */
959 static public int getMaxRecentTasksStatic() {
960 if (gMaxRecentTasks < 0) {
Winson7c402912016-05-10 13:59:36 -0700961 return gMaxRecentTasks = isLowRamDeviceStatic() ? 36 : 48;
Dianne Hackborn852975d2014-08-22 17:42:43 -0700962 }
963 return gMaxRecentTasks;
964 }
965
966 /**
967 * Return the default limit on the number of recents that an app can make.
968 * @hide
969 */
970 static public int getDefaultAppRecentsLimitStatic() {
971 return getMaxRecentTasksStatic() / 6;
972 }
973
974 /**
975 * Return the maximum limit on the number of recents that an app can make.
976 * @hide
977 */
978 static public int getMaxAppRecentsLimitStatic() {
979 return getMaxRecentTasksStatic() / 2;
980 }
981
982 /**
Wale Ogunwalefb1c8642016-03-02 08:28:08 -0800983 * Returns true if the system supports at least one form of multi-window.
984 * E.g. freeform, split-screen, picture-in-picture.
985 * @hide
986 */
987 static public boolean supportsMultiWindow() {
988 return !isLowRamDeviceStatic()
989 && Resources.getSystem().getBoolean(
990 com.android.internal.R.bool.config_supportsMultiWindow);
991 }
992
993 /**
Winson Chung1147c402014-05-14 11:05:00 -0700994 * Information you can set and retrieve about the current activity within the recent task list.
Winson Chung03a9bae2014-05-02 09:56:12 -0700995 */
Winson Chunga449dc02014-05-16 11:15:04 -0700996 public static class TaskDescription implements Parcelable {
Craig Mautner648f69b2014-09-18 14:16:26 -0700997 /** @hide */
998 public static final String ATTR_TASKDESCRIPTION_PREFIX = "task_description_";
999 private static final String ATTR_TASKDESCRIPTIONLABEL =
1000 ATTR_TASKDESCRIPTION_PREFIX + "label";
Winson Chung1af8eda2016-02-05 17:55:56 +00001001 private static final String ATTR_TASKDESCRIPTIONCOLOR_PRIMARY =
Craig Mautner648f69b2014-09-18 14:16:26 -07001002 ATTR_TASKDESCRIPTION_PREFIX + "color";
Winson Chung1af8eda2016-02-05 17:55:56 +00001003 private static final String ATTR_TASKDESCRIPTIONCOLOR_BACKGROUND =
1004 ATTR_TASKDESCRIPTION_PREFIX + "colorBackground";
Craig Mautner648f69b2014-09-18 14:16:26 -07001005 private static final String ATTR_TASKDESCRIPTIONICONFILENAME =
1006 ATTR_TASKDESCRIPTION_PREFIX + "icon_filename";
1007
Winson Chunga449dc02014-05-16 11:15:04 -07001008 private String mLabel;
1009 private Bitmap mIcon;
Craig Mautner648f69b2014-09-18 14:16:26 -07001010 private String mIconFilename;
Winson Chunga449dc02014-05-16 11:15:04 -07001011 private int mColorPrimary;
Winson Chung1af8eda2016-02-05 17:55:56 +00001012 private int mColorBackground;
Winson Chung03a9bae2014-05-02 09:56:12 -07001013
1014 /**
Winson Chunga449dc02014-05-16 11:15:04 -07001015 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -07001016 *
Winson Chunga449dc02014-05-16 11:15:04 -07001017 * @param label A label and description of the current state of this task.
1018 * @param icon An icon that represents the current state of this task.
Winson Chung1af8eda2016-02-05 17:55:56 +00001019 * @param colorPrimary A color to override the theme's primary color. This color must be
1020 * opaque.
Winson Chung03a9bae2014-05-02 09:56:12 -07001021 */
Winson Chunga449dc02014-05-16 11:15:04 -07001022 public TaskDescription(String label, Bitmap icon, int colorPrimary) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001023 this(label, icon, null, colorPrimary, 0);
Winson Chunga449dc02014-05-16 11:15:04 -07001024 if ((colorPrimary != 0) && (Color.alpha(colorPrimary) != 255)) {
1025 throw new RuntimeException("A TaskDescription's primary color should be opaque");
1026 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001027 }
1028
Winson Chung03a9bae2014-05-02 09:56:12 -07001029 /**
Winson Chunga449dc02014-05-16 11:15:04 -07001030 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -07001031 *
1032 * @param label A label and description of the current state of this activity.
1033 * @param icon An icon that represents the current state of this activity.
1034 */
Winson Chunga449dc02014-05-16 11:15:04 -07001035 public TaskDescription(String label, Bitmap icon) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001036 this(label, icon, null, 0, 0);
Winson Chung03a9bae2014-05-02 09:56:12 -07001037 }
1038
1039 /**
Winson Chunga449dc02014-05-16 11:15:04 -07001040 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -07001041 *
1042 * @param label A label and description of the current state of this activity.
1043 */
Winson Chunga449dc02014-05-16 11:15:04 -07001044 public TaskDescription(String label) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001045 this(label, null, null, 0, 0);
Winson Chung03a9bae2014-05-02 09:56:12 -07001046 }
1047
Winson Chunga449dc02014-05-16 11:15:04 -07001048 /**
1049 * Creates an empty TaskDescription.
1050 */
1051 public TaskDescription() {
Winson Chung1af8eda2016-02-05 17:55:56 +00001052 this(null, null, null, 0, 0);
1053 }
1054
1055 /** @hide */
1056 public TaskDescription(String label, Bitmap icon, String iconFilename, int colorPrimary,
1057 int colorBackground) {
1058 mLabel = label;
1059 mIcon = icon;
1060 mIconFilename = iconFilename;
1061 mColorPrimary = colorPrimary;
1062 mColorBackground = colorBackground;
Winson Chung03a9bae2014-05-02 09:56:12 -07001063 }
1064
Winson Chunga449dc02014-05-16 11:15:04 -07001065 /**
1066 * Creates a copy of another TaskDescription.
1067 */
1068 public TaskDescription(TaskDescription td) {
Winsonb6403152016-02-23 13:32:09 -08001069 copyFrom(td);
1070 }
1071
1072 /**
1073 * Copies this the values from another TaskDescription.
1074 * @hide
1075 */
1076 public void copyFrom(TaskDescription other) {
1077 mLabel = other.mLabel;
1078 mIcon = other.mIcon;
1079 mIconFilename = other.mIconFilename;
1080 mColorPrimary = other.mColorPrimary;
1081 mColorBackground = other.mColorBackground;
Winson Chunga449dc02014-05-16 11:15:04 -07001082 }
1083
1084 private TaskDescription(Parcel source) {
Winson Chung03a9bae2014-05-02 09:56:12 -07001085 readFromParcel(source);
1086 }
1087
1088 /**
Winson Chung2cb86c72014-06-25 12:03:30 -07001089 * Sets the label for this task description.
1090 * @hide
1091 */
1092 public void setLabel(String label) {
1093 mLabel = label;
1094 }
1095
1096 /**
1097 * Sets the primary color for this task description.
1098 * @hide
1099 */
1100 public void setPrimaryColor(int primaryColor) {
Winson Chungae12dae2014-09-29 14:21:26 -07001101 // Ensure that the given color is valid
1102 if ((primaryColor != 0) && (Color.alpha(primaryColor) != 255)) {
1103 throw new RuntimeException("A TaskDescription's primary color should be opaque");
1104 }
1105 mColorPrimary = primaryColor;
Winson Chung2cb86c72014-06-25 12:03:30 -07001106 }
1107
1108 /**
Winson Chung1af8eda2016-02-05 17:55:56 +00001109 * Sets the background color for this task description.
1110 * @hide
1111 */
1112 public void setBackgroundColor(int backgroundColor) {
1113 // Ensure that the given color is valid
1114 if ((backgroundColor != 0) && (Color.alpha(backgroundColor) != 255)) {
1115 throw new RuntimeException("A TaskDescription's background color should be opaque");
1116 }
1117 mColorBackground = backgroundColor;
1118 }
1119
1120 /**
Winson Chung2cb86c72014-06-25 12:03:30 -07001121 * Sets the icon for this task description.
1122 * @hide
1123 */
1124 public void setIcon(Bitmap icon) {
1125 mIcon = icon;
1126 }
1127
1128 /**
Craig Mautner648f69b2014-09-18 14:16:26 -07001129 * Moves the icon bitmap reference from an actual Bitmap to a file containing the
1130 * bitmap.
1131 * @hide
1132 */
1133 public void setIconFilename(String iconFilename) {
1134 mIconFilename = iconFilename;
1135 mIcon = null;
1136 }
1137
1138 /**
Winson Chunga449dc02014-05-16 11:15:04 -07001139 * @return The label and description of the current state of this task.
Winson Chung03a9bae2014-05-02 09:56:12 -07001140 */
Winson Chunga449dc02014-05-16 11:15:04 -07001141 public String getLabel() {
1142 return mLabel;
1143 }
1144
1145 /**
1146 * @return The icon that represents the current state of this task.
1147 */
1148 public Bitmap getIcon() {
Craig Mautner648f69b2014-09-18 14:16:26 -07001149 if (mIcon != null) {
1150 return mIcon;
1151 }
Suprabh Shukla23593142015-11-03 17:31:15 -08001152 return loadTaskDescriptionIcon(mIconFilename, UserHandle.myUserId());
Craig Mautner648f69b2014-09-18 14:16:26 -07001153 }
1154
1155 /** @hide */
1156 public String getIconFilename() {
1157 return mIconFilename;
Winson Chunga449dc02014-05-16 11:15:04 -07001158 }
1159
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001160 /** @hide */
1161 public Bitmap getInMemoryIcon() {
1162 return mIcon;
1163 }
1164
1165 /** @hide */
Suprabh Shukla23593142015-11-03 17:31:15 -08001166 public static Bitmap loadTaskDescriptionIcon(String iconFilename, int userId) {
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001167 if (iconFilename != null) {
1168 try {
Winson Chung1af8eda2016-02-05 17:55:56 +00001169 return ActivityManagerNative.getDefault().getTaskDescriptionIcon(iconFilename,
1170 userId);
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001171 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001172 throw e.rethrowFromSystemServer();
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001173 }
1174 }
1175 return null;
1176 }
1177
Winson Chunga449dc02014-05-16 11:15:04 -07001178 /**
1179 * @return The color override on the theme's primary color.
1180 */
1181 public int getPrimaryColor() {
1182 return mColorPrimary;
Winson Chung03a9bae2014-05-02 09:56:12 -07001183 }
1184
Winson Chung1af8eda2016-02-05 17:55:56 +00001185 /**
1186 * @return The background color.
1187 * @hide
1188 */
1189 public int getBackgroundColor() {
1190 return mColorBackground;
1191 }
1192
Craig Mautner648f69b2014-09-18 14:16:26 -07001193 /** @hide */
1194 public void saveToXml(XmlSerializer out) throws IOException {
1195 if (mLabel != null) {
1196 out.attribute(null, ATTR_TASKDESCRIPTIONLABEL, mLabel);
1197 }
1198 if (mColorPrimary != 0) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001199 out.attribute(null, ATTR_TASKDESCRIPTIONCOLOR_PRIMARY,
1200 Integer.toHexString(mColorPrimary));
1201 }
1202 if (mColorBackground != 0) {
1203 out.attribute(null, ATTR_TASKDESCRIPTIONCOLOR_BACKGROUND,
1204 Integer.toHexString(mColorBackground));
Craig Mautner648f69b2014-09-18 14:16:26 -07001205 }
1206 if (mIconFilename != null) {
1207 out.attribute(null, ATTR_TASKDESCRIPTIONICONFILENAME, mIconFilename);
1208 }
1209 }
1210
1211 /** @hide */
1212 public void restoreFromXml(String attrName, String attrValue) {
1213 if (ATTR_TASKDESCRIPTIONLABEL.equals(attrName)) {
1214 setLabel(attrValue);
Winson Chung1af8eda2016-02-05 17:55:56 +00001215 } else if (ATTR_TASKDESCRIPTIONCOLOR_PRIMARY.equals(attrName)) {
Craig Mautner648f69b2014-09-18 14:16:26 -07001216 setPrimaryColor((int) Long.parseLong(attrValue, 16));
Winson Chung1af8eda2016-02-05 17:55:56 +00001217 } else if (ATTR_TASKDESCRIPTIONCOLOR_BACKGROUND.equals(attrName)) {
1218 setBackgroundColor((int) Long.parseLong(attrValue, 16));
Craig Mautner648f69b2014-09-18 14:16:26 -07001219 } else if (ATTR_TASKDESCRIPTIONICONFILENAME.equals(attrName)) {
1220 setIconFilename(attrValue);
1221 }
1222 }
1223
Winson Chung03a9bae2014-05-02 09:56:12 -07001224 @Override
1225 public int describeContents() {
1226 return 0;
1227 }
1228
1229 @Override
1230 public void writeToParcel(Parcel dest, int flags) {
Winson Chunga449dc02014-05-16 11:15:04 -07001231 if (mLabel == null) {
Winson Chung03a9bae2014-05-02 09:56:12 -07001232 dest.writeInt(0);
1233 } else {
1234 dest.writeInt(1);
Winson Chunga449dc02014-05-16 11:15:04 -07001235 dest.writeString(mLabel);
Winson Chung03a9bae2014-05-02 09:56:12 -07001236 }
Winson Chunga449dc02014-05-16 11:15:04 -07001237 if (mIcon == null) {
1238 dest.writeInt(0);
1239 } else {
1240 dest.writeInt(1);
1241 mIcon.writeToParcel(dest, 0);
1242 }
1243 dest.writeInt(mColorPrimary);
Winson Chung1af8eda2016-02-05 17:55:56 +00001244 dest.writeInt(mColorBackground);
Craig Mautner648f69b2014-09-18 14:16:26 -07001245 if (mIconFilename == null) {
1246 dest.writeInt(0);
1247 } else {
1248 dest.writeInt(1);
1249 dest.writeString(mIconFilename);
1250 }
Winson Chung03a9bae2014-05-02 09:56:12 -07001251 }
1252
1253 public void readFromParcel(Parcel source) {
Winson Chunga449dc02014-05-16 11:15:04 -07001254 mLabel = source.readInt() > 0 ? source.readString() : null;
1255 mIcon = source.readInt() > 0 ? Bitmap.CREATOR.createFromParcel(source) : null;
1256 mColorPrimary = source.readInt();
Winson Chung1af8eda2016-02-05 17:55:56 +00001257 mColorBackground = source.readInt();
Craig Mautner648f69b2014-09-18 14:16:26 -07001258 mIconFilename = source.readInt() > 0 ? source.readString() : null;
Winson Chung03a9bae2014-05-02 09:56:12 -07001259 }
1260
Winson Chunga449dc02014-05-16 11:15:04 -07001261 public static final Creator<TaskDescription> CREATOR
1262 = new Creator<TaskDescription>() {
1263 public TaskDescription createFromParcel(Parcel source) {
1264 return new TaskDescription(source);
Winson Chung03a9bae2014-05-02 09:56:12 -07001265 }
Winson Chunga449dc02014-05-16 11:15:04 -07001266 public TaskDescription[] newArray(int size) {
1267 return new TaskDescription[size];
Winson Chung03a9bae2014-05-02 09:56:12 -07001268 }
1269 };
1270
1271 @Override
1272 public String toString() {
Winson Chunga449dc02014-05-16 11:15:04 -07001273 return "TaskDescription Label: " + mLabel + " Icon: " + mIcon +
Winson Chung1af8eda2016-02-05 17:55:56 +00001274 " IconFilename: " + mIconFilename + " colorPrimary: " + mColorPrimary +
1275 " colorBackground: " + mColorBackground;
Winson Chung03a9bae2014-05-02 09:56:12 -07001276 }
1277 }
1278
1279 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 * Information you can retrieve about tasks that the user has most recently
1281 * started or visited.
1282 */
1283 public static class RecentTaskInfo implements Parcelable {
1284 /**
1285 * If this task is currently running, this is the identifier for it.
1286 * If it is not running, this will be -1.
1287 */
1288 public int id;
1289
1290 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -08001291 * The true identifier of this task, valid even if it is not running.
1292 */
1293 public int persistentId;
Stefan Kuhne16045c22015-06-05 07:18:06 -07001294
Dianne Hackbornd94df452011-02-16 18:53:31 -08001295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 * The original Intent used to launch the task. You can use this
1297 * Intent to re-launch the task (if it is no longer running) or bring
1298 * the current task to the front.
1299 */
1300 public Intent baseIntent;
1301
1302 /**
1303 * If this task was started from an alias, this is the actual
1304 * activity component that was initially started; the component of
1305 * the baseIntent in this case is the name of the actual activity
1306 * implementation that the alias referred to. Otherwise, this is null.
1307 */
1308 public ComponentName origActivity;
Dianne Hackbornd2835932010-12-13 16:28:46 -08001309
1310 /**
James Cook4d0ef042015-07-09 14:28:23 -07001311 * The actual activity component that started the task.
1312 * @hide
1313 */
1314 @Nullable
1315 public ComponentName realActivity;
1316
1317 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -08001318 * Description of the task's last state.
1319 */
1320 public CharSequence description;
Craig Mautner6d90fed2013-05-22 15:03:10 -07001321
1322 /**
1323 * The id of the ActivityStack this Task was on most recently.
Craig Mautner27030522013-08-26 12:25:36 -07001324 * @hide
Craig Mautner6d90fed2013-05-22 15:03:10 -07001325 */
1326 public int stackId;
1327
Kenny Guy82326a92014-03-17 17:16:06 +00001328 /**
Craig Mautner2fbd7542014-03-21 09:34:07 -07001329 * The id of the user the task was running as.
Kenny Guy82326a92014-03-17 17:16:06 +00001330 * @hide
1331 */
1332 public int userId;
1333
Craig Mautner2fbd7542014-03-21 09:34:07 -07001334 /**
Winson Chungffa2ec62014-07-03 15:54:42 -07001335 * The first time this task was active.
1336 * @hide
1337 */
1338 public long firstActiveTime;
1339
1340 /**
Winson Chungf1fbd772014-06-24 18:06:58 -07001341 * The last time this task was active.
1342 * @hide
1343 */
1344 public long lastActiveTime;
1345
1346 /**
Winson Chung03a9bae2014-05-02 09:56:12 -07001347 * The recent activity values for the highest activity in the stack to have set the values.
Winson Chunga449dc02014-05-16 11:15:04 -07001348 * {@link Activity#setTaskDescription(android.app.ActivityManager.TaskDescription)}.
Craig Mautner2fbd7542014-03-21 09:34:07 -07001349 */
Winson Chunga449dc02014-05-16 11:15:04 -07001350 public TaskDescription taskDescription;
Craig Mautner2fbd7542014-03-21 09:34:07 -07001351
Craig Mautnera228ae92014-07-09 05:44:55 -07001352 /**
1353 * Task affiliation for grouping with other tasks.
Craig Mautnera228ae92014-07-09 05:44:55 -07001354 */
1355 public int affiliatedTaskId;
1356
Winson Chungec396d62014-08-06 17:08:00 -07001357 /**
1358 * Task affiliation color of the source task with the affiliated task id.
1359 *
1360 * @hide
1361 */
1362 public int affiliatedTaskColor;
1363
Wale Ogunwale6035e012015-04-14 15:54:10 -07001364 /**
1365 * The component launched as the first activity in the task.
1366 * This can be considered the "application" of this task.
1367 */
1368 public ComponentName baseActivity;
1369
1370 /**
1371 * The activity component at the top of the history stack of the task.
1372 * This is what the user is currently doing.
1373 */
1374 public ComponentName topActivity;
1375
1376 /**
1377 * Number of activities in this task.
1378 */
1379 public int numActivities;
1380
Winson2dd76942015-11-02 09:20:01 -08001381 /**
1382 * The bounds of the task.
1383 * @hide
1384 */
1385 public Rect bounds;
1386
Wale Ogunwale21b60582016-01-27 12:34:16 -08001387 /**
1388 * True if the task can go in the docked stack.
1389 * @hide
1390 */
1391 public boolean isDockable;
1392
Jorim Jaggi29379ec2016-04-11 23:43:42 -07001393 /**
1394 * The resize mode of the task. See {@link ActivityInfo#resizeMode}.
1395 * @hide
1396 */
1397 public int resizeMode;
1398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 public RecentTaskInfo() {
1400 }
1401
Craig Mautner6d90fed2013-05-22 15:03:10 -07001402 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 public int describeContents() {
1404 return 0;
1405 }
1406
Craig Mautner6d90fed2013-05-22 15:03:10 -07001407 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 public void writeToParcel(Parcel dest, int flags) {
1409 dest.writeInt(id);
Dianne Hackbornd94df452011-02-16 18:53:31 -08001410 dest.writeInt(persistentId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 if (baseIntent != null) {
1412 dest.writeInt(1);
1413 baseIntent.writeToParcel(dest, 0);
1414 } else {
1415 dest.writeInt(0);
1416 }
1417 ComponentName.writeToParcel(origActivity, dest);
James Cook4d0ef042015-07-09 14:28:23 -07001418 ComponentName.writeToParcel(realActivity, dest);
Dianne Hackbornd2835932010-12-13 16:28:46 -08001419 TextUtils.writeToParcel(description, dest,
1420 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
Winson Chunga449dc02014-05-16 11:15:04 -07001421 if (taskDescription != null) {
Craig Mautner2fbd7542014-03-21 09:34:07 -07001422 dest.writeInt(1);
Winson Chunga449dc02014-05-16 11:15:04 -07001423 taskDescription.writeToParcel(dest, 0);
Winson Chung03a9bae2014-05-02 09:56:12 -07001424 } else {
1425 dest.writeInt(0);
Craig Mautner2fbd7542014-03-21 09:34:07 -07001426 }
Craig Mautner6d90fed2013-05-22 15:03:10 -07001427 dest.writeInt(stackId);
Kenny Guy82326a92014-03-17 17:16:06 +00001428 dest.writeInt(userId);
Winson Chungffa2ec62014-07-03 15:54:42 -07001429 dest.writeLong(firstActiveTime);
Winson Chungf1fbd772014-06-24 18:06:58 -07001430 dest.writeLong(lastActiveTime);
Craig Mautnera228ae92014-07-09 05:44:55 -07001431 dest.writeInt(affiliatedTaskId);
Winson Chungec396d62014-08-06 17:08:00 -07001432 dest.writeInt(affiliatedTaskColor);
Wale Ogunwale6035e012015-04-14 15:54:10 -07001433 ComponentName.writeToParcel(baseActivity, dest);
1434 ComponentName.writeToParcel(topActivity, dest);
1435 dest.writeInt(numActivities);
Winson2dd76942015-11-02 09:20:01 -08001436 if (bounds != null) {
1437 dest.writeInt(1);
1438 bounds.writeToParcel(dest, 0);
1439 } else {
1440 dest.writeInt(0);
1441 }
Wale Ogunwale21b60582016-01-27 12:34:16 -08001442 dest.writeInt(isDockable ? 1 : 0);
Jorim Jaggi29379ec2016-04-11 23:43:42 -07001443 dest.writeInt(resizeMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 }
1445
1446 public void readFromParcel(Parcel source) {
1447 id = source.readInt();
Dianne Hackbornd94df452011-02-16 18:53:31 -08001448 persistentId = source.readInt();
Craig Mautner688b5102014-03-27 16:55:03 -07001449 baseIntent = source.readInt() > 0 ? Intent.CREATOR.createFromParcel(source) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 origActivity = ComponentName.readFromParcel(source);
James Cook4d0ef042015-07-09 14:28:23 -07001451 realActivity = ComponentName.readFromParcel(source);
Dianne Hackbornd2835932010-12-13 16:28:46 -08001452 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
Winson Chunga449dc02014-05-16 11:15:04 -07001453 taskDescription = source.readInt() > 0 ?
1454 TaskDescription.CREATOR.createFromParcel(source) : null;
Craig Mautner6d90fed2013-05-22 15:03:10 -07001455 stackId = source.readInt();
Kenny Guy82326a92014-03-17 17:16:06 +00001456 userId = source.readInt();
Winson Chungffa2ec62014-07-03 15:54:42 -07001457 firstActiveTime = source.readLong();
Winson Chungf1fbd772014-06-24 18:06:58 -07001458 lastActiveTime = source.readLong();
Craig Mautnera228ae92014-07-09 05:44:55 -07001459 affiliatedTaskId = source.readInt();
Winson Chungec396d62014-08-06 17:08:00 -07001460 affiliatedTaskColor = source.readInt();
Wale Ogunwale6035e012015-04-14 15:54:10 -07001461 baseActivity = ComponentName.readFromParcel(source);
1462 topActivity = ComponentName.readFromParcel(source);
1463 numActivities = source.readInt();
Winson2dd76942015-11-02 09:20:01 -08001464 bounds = source.readInt() > 0 ?
1465 Rect.CREATOR.createFromParcel(source) : null;
Wale Ogunwale21b60582016-01-27 12:34:16 -08001466 isDockable = source.readInt() == 1;
Jorim Jaggi29379ec2016-04-11 23:43:42 -07001467 resizeMode = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
Craig Mautner6d90fed2013-05-22 15:03:10 -07001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 public static final Creator<RecentTaskInfo> CREATOR
1471 = new Creator<RecentTaskInfo>() {
1472 public RecentTaskInfo createFromParcel(Parcel source) {
1473 return new RecentTaskInfo(source);
1474 }
1475 public RecentTaskInfo[] newArray(int size) {
1476 return new RecentTaskInfo[size];
1477 }
1478 };
1479
1480 private RecentTaskInfo(Parcel source) {
1481 readFromParcel(source);
1482 }
1483 }
1484
1485 /**
1486 * Flag for use with {@link #getRecentTasks}: return all tasks, even those
1487 * that have set their
1488 * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag.
1489 */
1490 public static final int RECENT_WITH_EXCLUDED = 0x0001;
Kenny Guy82326a92014-03-17 17:16:06 +00001491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -08001493 * Provides a list that does not contain any
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07001494 * recent tasks that currently are not available to the user.
1495 */
1496 public static final int RECENT_IGNORE_UNAVAILABLE = 0x0002;
Dianne Hackbornd2835932010-12-13 16:28:46 -08001497
1498 /**
Kenny Guy2a764942014-04-02 13:29:20 +01001499 * Provides a list that contains recent tasks for all
1500 * profiles of a user.
Kenny Guy82326a92014-03-17 17:16:06 +00001501 * @hide
1502 */
Kenny Guy2a764942014-04-02 13:29:20 +01001503 public static final int RECENT_INCLUDE_PROFILES = 0x0004;
Kenny Guy82326a92014-03-17 17:16:06 +00001504
1505 /**
Winson Chunga4ccb862014-08-22 15:26:27 -07001506 * Ignores all tasks that are on the home stack.
1507 * @hide
1508 */
1509 public static final int RECENT_IGNORE_HOME_STACK_TASKS = 0x0008;
1510
1511 /**
Wale Ogunwale64ae08a2016-03-28 08:15:27 -07001512 * Ignores the top task in the docked stack.
Winson Chung0583d3d2015-12-18 10:03:12 -05001513 * @hide
1514 */
Wale Ogunwale64ae08a2016-03-28 08:15:27 -07001515 public static final int RECENT_INGORE_DOCKED_STACK_TOP_TASK = 0x0010;
Winson Chung0583d3d2015-12-18 10:03:12 -05001516
1517 /**
1518 * Ignores all tasks that are on the pinned stack.
1519 * @hide
1520 */
1521 public static final int RECENT_INGORE_PINNED_STACK_TASKS = 0x0020;
1522
1523 /**
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001524 * <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 -08001525 * the most recent being first and older ones after in order.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001526 *
1527 * <p><b>Note: this method is only intended for debugging and presenting
1528 * task management user interfaces</b>. This should never be used for
1529 * core logic in an application, such as deciding between different
1530 * behaviors based on the information found here. Such uses are
1531 * <em>not</em> supported, and will likely break in the future. For
1532 * example, if multiple applications can be actively running at the
1533 * same time, assumptions made about the meaning of the data here for
1534 * purposes of control flow will be incorrect.</p>
1535 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001536 * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method is
Dianne Hackborn2d7576b2014-10-03 16:26:04 -07001537 * no longer available to third party applications: the introduction of
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001538 * document-centric recents means
1539 * it can leak personal information to the caller. For backwards compatibility,
1540 * it will still return a small subset of its data: at least the caller's
1541 * own tasks (though see {@link #getAppTasks()} for the correct supported
1542 * way to retrieve that information), and possibly some other tasks
1543 * such as home that are known to not be sensitive.
1544 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 * @param maxNum The maximum number of entries to return in the list. The
1546 * actual number returned may be smaller, depending on how many tasks the
1547 * user has started and the maximum number the system can remember.
Dianne Hackbornd2835932010-12-13 16:28:46 -08001548 * @param flags Information about what to return. May be any combination
Dianne Hackborn805fd7e2011-01-16 18:30:29 -08001549 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
Stefan Kuhne16045c22015-06-05 07:18:06 -07001550 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * @return Returns a list of RecentTaskInfo records describing each of
1552 * the recent tasks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 */
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001554 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags)
1556 throws SecurityException {
1557 try {
1558 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
Jeff Sharkey479212c2016-06-29 16:00:55 -06001559 flags, UserHandle.myUserId()).getList();
Amith Yamasani82644082012-08-03 13:09:11 -07001560 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001561 throw e.rethrowFromSystemServer();
Amith Yamasani82644082012-08-03 13:09:11 -07001562 }
1563 }
1564
1565 /**
1566 * Same as {@link #getRecentTasks(int, int)} but returns the recent tasks for a
1567 * specific user. It requires holding
1568 * the {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission.
1569 * @param maxNum The maximum number of entries to return in the list. The
1570 * actual number returned may be smaller, depending on how many tasks the
1571 * user has started and the maximum number the system can remember.
1572 * @param flags Information about what to return. May be any combination
1573 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
1574 *
1575 * @return Returns a list of RecentTaskInfo records describing each of
Vadim Tryshev35605752015-08-31 13:28:08 -07001576 * the recent tasks. Most recently activated tasks go first.
Amith Yamasani82644082012-08-03 13:09:11 -07001577 *
Amith Yamasani82644082012-08-03 13:09:11 -07001578 * @hide
1579 */
1580 public List<RecentTaskInfo> getRecentTasksForUser(int maxNum, int flags, int userId)
1581 throws SecurityException {
1582 try {
1583 return ActivityManagerNative.getDefault().getRecentTasks(maxNum,
Jeff Sharkey479212c2016-06-29 16:00:55 -06001584 flags, userId).getList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001586 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
1588 }
1589
1590 /**
1591 * Information you can retrieve about a particular task that is currently
1592 * "running" in the system. Note that a running task does not mean the
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001593 * given task actually has a process it is actively running in; it simply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 * means that the user has gone to it and never closed it, but currently
1595 * the system may have killed its process and is only holding on to its
1596 * last state in order to restart it when the user returns.
1597 */
1598 public static class RunningTaskInfo implements Parcelable {
1599 /**
1600 * A unique identifier for this task.
1601 */
1602 public int id;
1603
1604 /**
Winson5510f6c2015-10-27 12:11:26 -07001605 * The stack that currently contains this task.
1606 * @hide
1607 */
1608 public int stackId;
1609
1610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 * The component launched as the first activity in the task. This can
1612 * be considered the "application" of this task.
1613 */
1614 public ComponentName baseActivity;
1615
1616 /**
1617 * The activity component at the top of the history stack of the task.
1618 * This is what the user is currently doing.
1619 */
1620 public ComponentName topActivity;
1621
1622 /**
Dianne Hackborn805fd7e2011-01-16 18:30:29 -08001623 * Thumbnail representation of the task's current state. Currently
1624 * always null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 */
1626 public Bitmap thumbnail;
1627
1628 /**
1629 * Description of the task's current state.
1630 */
1631 public CharSequence description;
1632
1633 /**
1634 * Number of activities in this task.
1635 */
1636 public int numActivities;
1637
1638 /**
1639 * Number of activities that are currently running (not stopped
1640 * and persisted) in this task.
1641 */
1642 public int numRunning;
1643
Craig Mautnerc0fd8052013-09-19 11:20:17 -07001644 /**
1645 * Last time task was run. For sorting.
1646 * @hide
1647 */
1648 public long lastActiveTime;
1649
Wale Ogunwale21b60582016-01-27 12:34:16 -08001650 /**
1651 * True if the task can go in the docked stack.
1652 * @hide
1653 */
1654 public boolean isDockable;
1655
Jorim Jaggi29379ec2016-04-11 23:43:42 -07001656 /**
1657 * The resize mode of the task. See {@link ActivityInfo#resizeMode}.
1658 * @hide
1659 */
1660 public int resizeMode;
1661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 public RunningTaskInfo() {
1663 }
1664
1665 public int describeContents() {
1666 return 0;
1667 }
1668
1669 public void writeToParcel(Parcel dest, int flags) {
1670 dest.writeInt(id);
Winson5510f6c2015-10-27 12:11:26 -07001671 dest.writeInt(stackId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 ComponentName.writeToParcel(baseActivity, dest);
1673 ComponentName.writeToParcel(topActivity, dest);
1674 if (thumbnail != null) {
1675 dest.writeInt(1);
1676 thumbnail.writeToParcel(dest, 0);
1677 } else {
1678 dest.writeInt(0);
1679 }
1680 TextUtils.writeToParcel(description, dest,
1681 Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1682 dest.writeInt(numActivities);
1683 dest.writeInt(numRunning);
Jorim Jaggiaa6c5742016-03-01 14:10:14 +01001684 dest.writeInt(isDockable ? 1 : 0);
Jorim Jaggi29379ec2016-04-11 23:43:42 -07001685 dest.writeInt(resizeMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
1687
1688 public void readFromParcel(Parcel source) {
1689 id = source.readInt();
Winson5510f6c2015-10-27 12:11:26 -07001690 stackId = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 baseActivity = ComponentName.readFromParcel(source);
1692 topActivity = ComponentName.readFromParcel(source);
1693 if (source.readInt() != 0) {
1694 thumbnail = Bitmap.CREATOR.createFromParcel(source);
1695 } else {
1696 thumbnail = null;
1697 }
1698 description = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
1699 numActivities = source.readInt();
1700 numRunning = source.readInt();
Jorim Jaggiaa6c5742016-03-01 14:10:14 +01001701 isDockable = source.readInt() != 0;
Jorim Jaggi29379ec2016-04-11 23:43:42 -07001702 resizeMode = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07001704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 public static final Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() {
1706 public RunningTaskInfo createFromParcel(Parcel source) {
1707 return new RunningTaskInfo(source);
1708 }
1709 public RunningTaskInfo[] newArray(int size) {
1710 return new RunningTaskInfo[size];
1711 }
1712 };
1713
1714 private RunningTaskInfo(Parcel source) {
1715 readFromParcel(source);
1716 }
1717 }
Winson Chung1147c402014-05-14 11:05:00 -07001718
1719 /**
1720 * Get the list of tasks associated with the calling application.
1721 *
1722 * @return The list of tasks associated with the application making this call.
1723 * @throws SecurityException
1724 */
1725 public List<ActivityManager.AppTask> getAppTasks() {
1726 ArrayList<AppTask> tasks = new ArrayList<AppTask>();
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07001727 List<IBinder> appTasks;
Winson Chung1147c402014-05-14 11:05:00 -07001728 try {
Dianne Hackborn885fbe52014-08-23 15:23:58 -07001729 appTasks = ActivityManagerNative.getDefault().getAppTasks(mContext.getPackageName());
Winson Chung1147c402014-05-14 11:05:00 -07001730 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001731 throw e.rethrowFromSystemServer();
Winson Chung1147c402014-05-14 11:05:00 -07001732 }
1733 int numAppTasks = appTasks.size();
1734 for (int i = 0; i < numAppTasks; i++) {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07001735 tasks.add(new AppTask(IAppTask.Stub.asInterface(appTasks.get(i))));
Winson Chung1147c402014-05-14 11:05:00 -07001736 }
1737 return tasks;
1738 }
1739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 /**
Winson Chung48a10a52014-08-27 14:36:51 -07001741 * Return the current design dimensions for {@link AppTask} thumbnails, for use
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001742 * with {@link #addAppTask}.
1743 */
Winson Chung48a10a52014-08-27 14:36:51 -07001744 public Size getAppTaskThumbnailSize() {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001745 synchronized (this) {
1746 ensureAppTaskThumbnailSizeLocked();
Winson Chung48a10a52014-08-27 14:36:51 -07001747 return new Size(mAppTaskThumbnailSize.x, mAppTaskThumbnailSize.y);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001748 }
1749 }
1750
1751 private void ensureAppTaskThumbnailSizeLocked() {
1752 if (mAppTaskThumbnailSize == null) {
1753 try {
1754 mAppTaskThumbnailSize = ActivityManagerNative.getDefault().getAppTaskThumbnailSize();
1755 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001756 throw e.rethrowFromSystemServer();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001757 }
1758 }
1759 }
1760
1761 /**
1762 * Add a new {@link AppTask} for the calling application. This will create a new
1763 * recents entry that is added to the <b>end</b> of all existing recents.
1764 *
1765 * @param activity The activity that is adding the entry. This is used to help determine
1766 * the context that the new recents entry will be in.
1767 * @param intent The Intent that describes the recents entry. This is the same Intent that
1768 * you would have used to launch the activity for it. In generally you will want to set
1769 * both {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT} and
1770 * {@link Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS}; the latter is required since this recents
1771 * entry will exist without an activity, so it doesn't make sense to not retain it when
1772 * its activity disappears. The given Intent here also must have an explicit ComponentName
1773 * set on it.
1774 * @param description Optional additional description information.
1775 * @param thumbnail Thumbnail to use for the recents entry. Should be the size given by
Winson Chung48a10a52014-08-27 14:36:51 -07001776 * {@link #getAppTaskThumbnailSize()}. If the bitmap is not that exact size, it will be
1777 * recreated in your process, probably in a way you don't like, before the recents entry
1778 * is added.
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001779 *
1780 * @return Returns the task id of the newly added app task, or -1 if the add failed. The
1781 * most likely cause of failure is that there is no more room for more tasks for your app.
1782 */
1783 public int addAppTask(@NonNull Activity activity, @NonNull Intent intent,
1784 @Nullable TaskDescription description, @NonNull Bitmap thumbnail) {
1785 Point size;
1786 synchronized (this) {
1787 ensureAppTaskThumbnailSizeLocked();
1788 size = mAppTaskThumbnailSize;
1789 }
1790 final int tw = thumbnail.getWidth();
1791 final int th = thumbnail.getHeight();
1792 if (tw != size.x || th != size.y) {
1793 Bitmap bm = Bitmap.createBitmap(size.x, size.y, thumbnail.getConfig());
1794
1795 // Use ScaleType.CENTER_CROP, except we leave the top edge at the top.
1796 float scale;
1797 float dx = 0, dy = 0;
1798 if (tw * size.x > size.y * th) {
1799 scale = (float) size.x / (float) th;
1800 dx = (size.y - tw * scale) * 0.5f;
1801 } else {
1802 scale = (float) size.y / (float) tw;
1803 dy = (size.x - th * scale) * 0.5f;
1804 }
1805 Matrix matrix = new Matrix();
1806 matrix.setScale(scale, scale);
1807 matrix.postTranslate((int) (dx + 0.5f), 0);
1808
1809 Canvas canvas = new Canvas(bm);
1810 canvas.drawBitmap(thumbnail, matrix, null);
1811 canvas.setBitmap(null);
1812
1813 thumbnail = bm;
1814 }
1815 if (description == null) {
1816 description = new TaskDescription();
1817 }
1818 try {
1819 return ActivityManagerNative.getDefault().addAppTask(activity.getActivityToken(),
1820 intent, description, thumbnail);
1821 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001822 throw e.rethrowFromSystemServer();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001823 }
1824 }
1825
1826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 * Return a list of the tasks that are currently running, with
1828 * the most recent being first and older ones after in order. Note that
1829 * "running" does not mean any of the task's code is currently loaded or
1830 * activity -- the task may have been frozen by the system, so that it
1831 * can be restarted in its previous state when next brought to the
1832 * foreground.
Jim Miller0b2a6d02010-07-13 18:01:29 -07001833 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001834 * <p><b>Note: this method is only intended for debugging and presenting
1835 * task management user interfaces</b>. This should never be used for
1836 * core logic in an application, such as deciding between different
1837 * behaviors based on the information found here. Such uses are
1838 * <em>not</em> supported, and will likely break in the future. For
1839 * example, if multiple applications can be actively running at the
1840 * same time, assumptions made about the meaning of the data here for
1841 * purposes of control flow will be incorrect.</p>
1842 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001843 * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001844 * is no longer available to third party
1845 * applications: the introduction of document-centric recents means
1846 * it can leak person information to the caller. For backwards compatibility,
1847 * it will still retu rn a small subset of its data: at least the caller's
1848 * own tasks, and possibly some other tasks
1849 * such as home that are known to not be sensitive.
1850 *
Jim Miller0b2a6d02010-07-13 18:01:29 -07001851 * @param maxNum The maximum number of entries to return in the list. The
1852 * actual number returned may be smaller, depending on how many tasks the
1853 * user has started.
1854 *
1855 * @return Returns a list of RunningTaskInfo records describing each of
1856 * the running tasks.
Jim Miller0b2a6d02010-07-13 18:01:29 -07001857 */
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001858 @Deprecated
Jim Miller0b2a6d02010-07-13 18:01:29 -07001859 public List<RunningTaskInfo> getRunningTasks(int maxNum)
1860 throws SecurityException {
Dianne Hackborn09233282014-04-30 11:33:59 -07001861 try {
1862 return ActivityManagerNative.getDefault().getTasks(maxNum, 0);
1863 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001864 throw e.rethrowFromSystemServer();
Dianne Hackborn09233282014-04-30 11:33:59 -07001865 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07001866 }
1867
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001868 /**
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001869 * Completely remove the given task.
1870 *
1871 * @param taskId Identifier of the task to be removed.
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001872 * @return Returns true if the given task was found and removed.
1873 *
1874 * @hide
1875 */
Wale Ogunwaled54b5782014-10-23 15:55:23 -07001876 public boolean removeTask(int taskId) throws SecurityException {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001877 try {
Wale Ogunwaled54b5782014-10-23 15:55:23 -07001878 return ActivityManagerNative.getDefault().removeTask(taskId);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001879 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001880 throw e.rethrowFromSystemServer();
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001881 }
1882 }
1883
Winsonc809cbb2015-11-02 12:06:15 -08001884 /**
1885 * Metadata related to the {@link TaskThumbnail}.
1886 *
1887 * @hide
1888 */
1889 public static class TaskThumbnailInfo implements Parcelable {
1890 /** @hide */
1891 public static final String ATTR_TASK_THUMBNAILINFO_PREFIX = "task_thumbnailinfo_";
1892 private static final String ATTR_TASK_WIDTH =
1893 ATTR_TASK_THUMBNAILINFO_PREFIX + "task_width";
1894 private static final String ATTR_TASK_HEIGHT =
1895 ATTR_TASK_THUMBNAILINFO_PREFIX + "task_height";
1896 private static final String ATTR_SCREEN_ORIENTATION =
1897 ATTR_TASK_THUMBNAILINFO_PREFIX + "screen_orientation";
1898
1899 public int taskWidth;
1900 public int taskHeight;
Winson21700932016-03-24 17:26:23 -07001901 public int screenOrientation = Configuration.ORIENTATION_UNDEFINED;
Winsonc809cbb2015-11-02 12:06:15 -08001902
1903 public TaskThumbnailInfo() {
1904 // Do nothing
1905 }
1906
1907 private TaskThumbnailInfo(Parcel source) {
1908 readFromParcel(source);
1909 }
1910
Winsonc6a2da02015-11-11 18:11:59 -08001911 /**
1912 * Resets this info state to the initial state.
1913 * @hide
1914 */
1915 public void reset() {
1916 taskWidth = 0;
1917 taskHeight = 0;
Winson21700932016-03-24 17:26:23 -07001918 screenOrientation = Configuration.ORIENTATION_UNDEFINED;
1919 }
1920
1921 /**
1922 * Copies from another ThumbnailInfo.
1923 */
1924 public void copyFrom(TaskThumbnailInfo o) {
1925 taskWidth = o.taskWidth;
1926 taskHeight = o.taskHeight;
1927 screenOrientation = o.screenOrientation;
Winsonc6a2da02015-11-11 18:11:59 -08001928 }
1929
Winsonc809cbb2015-11-02 12:06:15 -08001930 /** @hide */
1931 public void saveToXml(XmlSerializer out) throws IOException {
1932 out.attribute(null, ATTR_TASK_WIDTH, Integer.toString(taskWidth));
1933 out.attribute(null, ATTR_TASK_HEIGHT, Integer.toString(taskHeight));
1934 out.attribute(null, ATTR_SCREEN_ORIENTATION, Integer.toString(screenOrientation));
1935 }
1936
1937 /** @hide */
1938 public void restoreFromXml(String attrName, String attrValue) {
1939 if (ATTR_TASK_WIDTH.equals(attrName)) {
1940 taskWidth = Integer.parseInt(attrValue);
1941 } else if (ATTR_TASK_HEIGHT.equals(attrName)) {
1942 taskHeight = Integer.parseInt(attrValue);
1943 } else if (ATTR_SCREEN_ORIENTATION.equals(attrName)) {
1944 screenOrientation = Integer.parseInt(attrValue);
1945 }
1946 }
1947
1948 public int describeContents() {
1949 return 0;
1950 }
1951
1952 public void writeToParcel(Parcel dest, int flags) {
1953 dest.writeInt(taskWidth);
1954 dest.writeInt(taskHeight);
1955 dest.writeInt(screenOrientation);
1956 }
1957
1958 public void readFromParcel(Parcel source) {
1959 taskWidth = source.readInt();
1960 taskHeight = source.readInt();
1961 screenOrientation = source.readInt();
1962 }
1963
1964 public static final Creator<TaskThumbnailInfo> CREATOR = new Creator<TaskThumbnailInfo>() {
1965 public TaskThumbnailInfo createFromParcel(Parcel source) {
1966 return new TaskThumbnailInfo(source);
1967 }
1968 public TaskThumbnailInfo[] newArray(int size) {
1969 return new TaskThumbnailInfo[size];
1970 }
1971 };
1972 }
1973
Dianne Hackbornd94df452011-02-16 18:53:31 -08001974 /** @hide */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001975 public static class TaskThumbnail implements Parcelable {
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001976 public Bitmap mainThumbnail;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001977 public ParcelFileDescriptor thumbnailFileDescriptor;
Winsonc809cbb2015-11-02 12:06:15 -08001978 public TaskThumbnailInfo thumbnailInfo;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001979
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001980 public TaskThumbnail() {
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001981 }
1982
Winsonc809cbb2015-11-02 12:06:15 -08001983 private TaskThumbnail(Parcel source) {
1984 readFromParcel(source);
1985 }
1986
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001987 public int describeContents() {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001988 if (thumbnailFileDescriptor != null) {
1989 return thumbnailFileDescriptor.describeContents();
1990 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001991 return 0;
1992 }
1993
1994 public void writeToParcel(Parcel dest, int flags) {
1995 if (mainThumbnail != null) {
1996 dest.writeInt(1);
Dianne Hackborn852975d2014-08-22 17:42:43 -07001997 mainThumbnail.writeToParcel(dest, flags);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001998 } else {
1999 dest.writeInt(0);
2000 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002001 if (thumbnailFileDescriptor != null) {
2002 dest.writeInt(1);
Dianne Hackborn852975d2014-08-22 17:42:43 -07002003 thumbnailFileDescriptor.writeToParcel(dest, flags);
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002004 } else {
2005 dest.writeInt(0);
2006 }
Winsonc809cbb2015-11-02 12:06:15 -08002007 if (thumbnailInfo != null) {
2008 dest.writeInt(1);
2009 thumbnailInfo.writeToParcel(dest, flags);
2010 } else {
2011 dest.writeInt(0);
2012 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002013 }
2014
2015 public void readFromParcel(Parcel source) {
2016 if (source.readInt() != 0) {
2017 mainThumbnail = Bitmap.CREATOR.createFromParcel(source);
2018 } else {
2019 mainThumbnail = null;
2020 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002021 if (source.readInt() != 0) {
2022 thumbnailFileDescriptor = ParcelFileDescriptor.CREATOR.createFromParcel(source);
2023 } else {
2024 thumbnailFileDescriptor = null;
2025 }
Winsonc809cbb2015-11-02 12:06:15 -08002026 if (source.readInt() != 0) {
2027 thumbnailInfo = TaskThumbnailInfo.CREATOR.createFromParcel(source);
2028 } else {
2029 thumbnailInfo = null;
2030 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002031 }
2032
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002033 public static final Creator<TaskThumbnail> CREATOR = new Creator<TaskThumbnail>() {
2034 public TaskThumbnail createFromParcel(Parcel source) {
2035 return new TaskThumbnail(source);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002036 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002037 public TaskThumbnail[] newArray(int size) {
2038 return new TaskThumbnail[size];
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002039 }
2040 };
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002041 }
2042
2043 /** @hide */
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002044 public TaskThumbnail getTaskThumbnail(int id) throws SecurityException {
Dianne Hackbornd94df452011-02-16 18:53:31 -08002045 try {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002046 return ActivityManagerNative.getDefault().getTaskThumbnail(id);
Dianne Hackborn15491c62012-09-19 10:59:14 -07002047 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002048 throw e.rethrowFromSystemServer();
Dianne Hackborn15491c62012-09-19 10:59:14 -07002049 }
2050 }
2051
Winson Chung303e1ff2014-03-07 15:06:19 -08002052 /** @hide */
2053 public boolean isInHomeStack(int taskId) {
2054 try {
2055 return ActivityManagerNative.getDefault().isInHomeStack(taskId);
2056 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002057 throw e.rethrowFromSystemServer();
Winson Chung303e1ff2014-03-07 15:06:19 -08002058 }
2059 }
2060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002062 * Flag for {@link #moveTaskToFront(int, int)}: also move the "home"
2063 * activity along with the task, so it is positioned immediately behind
2064 * the task.
2065 */
2066 public static final int MOVE_TASK_WITH_HOME = 0x00000001;
2067
2068 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -08002069 * Flag for {@link #moveTaskToFront(int, int)}: don't count this as a
2070 * user-instigated action, so the current activity will not receive a
2071 * hint that the user is leaving.
2072 */
2073 public static final int MOVE_TASK_NO_USER_ACTION = 0x00000002;
2074
2075 /**
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002076 * Equivalent to calling {@link #moveTaskToFront(int, int, Bundle)}
2077 * with a null options argument.
2078 *
2079 * @param taskId The identifier of the task to be moved, as found in
2080 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
2081 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -07002082 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002083 */
2084 public void moveTaskToFront(int taskId, int flags) {
2085 moveTaskToFront(taskId, flags, null);
2086 }
2087
2088 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002089 * Ask that the task associated with a given task ID be moved to the
2090 * front of the stack, so it is now visible to the user. Requires that
2091 * the caller hold permission {@link android.Manifest.permission#REORDER_TASKS}
2092 * or a SecurityException will be thrown.
2093 *
2094 * @param taskId The identifier of the task to be moved, as found in
2095 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
2096 * @param flags Additional operational flags, 0 or more of
Craig Mautnerecbfe252013-03-28 11:58:25 -07002097 * {@link #MOVE_TASK_WITH_HOME}, {@link #MOVE_TASK_NO_USER_ACTION}.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002098 * @param options Additional options for the operation, either null or
2099 * as per {@link Context#startActivity(Intent, android.os.Bundle)
2100 * Context.startActivity(Intent, Bundle)}.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002101 */
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002102 public void moveTaskToFront(int taskId, int flags, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002103 try {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002104 ActivityManagerNative.getDefault().moveTaskToFront(taskId, flags, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002105 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002106 throw e.rethrowFromSystemServer();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002107 }
2108 }
2109
2110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 * Information you can retrieve about a particular Service that is
2112 * currently running in the system.
2113 */
2114 public static class RunningServiceInfo implements Parcelable {
2115 /**
2116 * The service component.
2117 */
2118 public ComponentName service;
2119
2120 /**
2121 * If non-zero, this is the process the service is running in.
2122 */
2123 public int pid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002126 * The UID that owns this service.
2127 */
2128 public int uid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002129
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 * The name of the process this service runs in.
2132 */
2133 public String process;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 /**
2136 * Set to true if the service has asked to run as a foreground process.
2137 */
2138 public boolean foreground;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002141 * The time when the service was first made active, either by someone
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002142 * starting or binding to it. This
2143 * is in units of {@link android.os.SystemClock#elapsedRealtime()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 */
2145 public long activeSince;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 /**
2148 * Set to true if this service has been explicitly started.
2149 */
2150 public boolean started;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 /**
2153 * Number of clients connected to the service.
2154 */
2155 public int clientCount;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 /**
2158 * Number of times the service's process has crashed while the service
2159 * is running.
2160 */
2161 public int crashCount;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 /**
2164 * The time when there was last activity in the service (either
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002165 * explicit requests to start it or clients binding to it). This
2166 * is in units of {@link android.os.SystemClock#uptimeMillis()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 */
2168 public long lastActivityTime;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 /**
2171 * If non-zero, this service is not currently running, but scheduled to
2172 * restart at the given time.
2173 */
2174 public long restarting;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002175
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002176 /**
2177 * Bit for {@link #flags}: set if this service has been
2178 * explicitly started.
2179 */
2180 public static final int FLAG_STARTED = 1<<0;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002181
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002182 /**
2183 * Bit for {@link #flags}: set if the service has asked to
2184 * run as a foreground process.
2185 */
2186 public static final int FLAG_FOREGROUND = 1<<1;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002187
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002188 /**
2189 * Bit for {@link #flags): set if the service is running in a
2190 * core system process.
2191 */
2192 public static final int FLAG_SYSTEM_PROCESS = 1<<2;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002193
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002194 /**
2195 * Bit for {@link #flags): set if the service is running in a
2196 * persistent process.
2197 */
2198 public static final int FLAG_PERSISTENT_PROCESS = 1<<3;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002199
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002200 /**
2201 * Running flags.
2202 */
2203 public int flags;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002204
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002205 /**
2206 * For special services that are bound to by system code, this is
2207 * the package that holds the binding.
2208 */
2209 public String clientPackage;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002210
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002211 /**
2212 * For special services that are bound to by system code, this is
2213 * a string resource providing a user-visible label for who the
2214 * client is.
2215 */
2216 public int clientLabel;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 public RunningServiceInfo() {
2219 }
2220
2221 public int describeContents() {
2222 return 0;
2223 }
2224
2225 public void writeToParcel(Parcel dest, int flags) {
2226 ComponentName.writeToParcel(service, dest);
2227 dest.writeInt(pid);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002228 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 dest.writeString(process);
2230 dest.writeInt(foreground ? 1 : 0);
2231 dest.writeLong(activeSince);
2232 dest.writeInt(started ? 1 : 0);
2233 dest.writeInt(clientCount);
2234 dest.writeInt(crashCount);
2235 dest.writeLong(lastActivityTime);
2236 dest.writeLong(restarting);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002237 dest.writeInt(this.flags);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002238 dest.writeString(clientPackage);
2239 dest.writeInt(clientLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 }
2241
2242 public void readFromParcel(Parcel source) {
2243 service = ComponentName.readFromParcel(source);
2244 pid = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002245 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 process = source.readString();
2247 foreground = source.readInt() != 0;
2248 activeSince = source.readLong();
2249 started = source.readInt() != 0;
2250 clientCount = source.readInt();
2251 crashCount = source.readInt();
2252 lastActivityTime = source.readLong();
2253 restarting = source.readLong();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002254 flags = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002255 clientPackage = source.readString();
2256 clientLabel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 public static final Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() {
2260 public RunningServiceInfo createFromParcel(Parcel source) {
2261 return new RunningServiceInfo(source);
2262 }
2263 public RunningServiceInfo[] newArray(int size) {
2264 return new RunningServiceInfo[size];
2265 }
2266 };
2267
2268 private RunningServiceInfo(Parcel source) {
2269 readFromParcel(source);
2270 }
2271 }
2272
2273 /**
2274 * Return a list of the services that are currently running.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002275 *
2276 * <p><b>Note: this method is only intended for debugging or implementing
2277 * service management type user interfaces.</b></p>
2278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 * @param maxNum The maximum number of entries to return in the list. The
2280 * actual number returned may be smaller, depending on how many services
2281 * are running.
Stefan Kuhne16045c22015-06-05 07:18:06 -07002282 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 * @return Returns a list of RunningServiceInfo records describing each of
2284 * the running tasks.
2285 */
2286 public List<RunningServiceInfo> getRunningServices(int maxNum)
2287 throws SecurityException {
2288 try {
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002289 return ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 .getServices(maxNum, 0);
2291 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002292 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 }
2294 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 /**
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002297 * Returns a PendingIntent you can start to show a control panel for the
2298 * given running service. If the service does not have a control panel,
2299 * null is returned.
2300 */
2301 public PendingIntent getRunningServiceControlPanel(ComponentName service)
2302 throws SecurityException {
2303 try {
2304 return ActivityManagerNative.getDefault()
2305 .getRunningServiceControlPanel(service);
2306 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002307 throw e.rethrowFromSystemServer();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002308 }
2309 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002310
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 * Information you can retrieve about the available memory through
2313 * {@link ActivityManager#getMemoryInfo}.
2314 */
2315 public static class MemoryInfo implements Parcelable {
2316 /**
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002317 * The available memory on the system. This number should not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 * be considered absolute: due to the nature of the kernel, a significant
2319 * portion of this memory is actually in use and needed for the overall
2320 * system to run well.
2321 */
2322 public long availMem;
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002323
2324 /**
2325 * The total memory accessible by the kernel. This is basically the
2326 * RAM size of the device, not including below-kernel fixed allocations
2327 * like DMA buffers, RAM for the baseband CPU, etc.
2328 */
2329 public long totalMem;
2330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 /**
2332 * The threshold of {@link #availMem} at which we consider memory to be
2333 * low and start killing background services and other non-extraneous
2334 * processes.
2335 */
2336 public long threshold;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 /**
2339 * Set to true if the system considers itself to currently be in a low
2340 * memory situation.
2341 */
2342 public boolean lowMemory;
2343
Dianne Hackborn7d608422011-08-07 16:24:18 -07002344 /** @hide */
2345 public long hiddenAppThreshold;
2346 /** @hide */
2347 public long secondaryServerThreshold;
2348 /** @hide */
2349 public long visibleAppThreshold;
2350 /** @hide */
2351 public long foregroundAppThreshold;
2352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 public MemoryInfo() {
2354 }
2355
2356 public int describeContents() {
2357 return 0;
2358 }
2359
2360 public void writeToParcel(Parcel dest, int flags) {
2361 dest.writeLong(availMem);
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002362 dest.writeLong(totalMem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 dest.writeLong(threshold);
2364 dest.writeInt(lowMemory ? 1 : 0);
Dianne Hackborn7d608422011-08-07 16:24:18 -07002365 dest.writeLong(hiddenAppThreshold);
2366 dest.writeLong(secondaryServerThreshold);
2367 dest.writeLong(visibleAppThreshold);
2368 dest.writeLong(foregroundAppThreshold);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 public void readFromParcel(Parcel source) {
2372 availMem = source.readLong();
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002373 totalMem = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 threshold = source.readLong();
2375 lowMemory = source.readInt() != 0;
Dianne Hackborn7d608422011-08-07 16:24:18 -07002376 hiddenAppThreshold = source.readLong();
2377 secondaryServerThreshold = source.readLong();
2378 visibleAppThreshold = source.readLong();
2379 foregroundAppThreshold = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 }
2381
2382 public static final Creator<MemoryInfo> CREATOR
2383 = new Creator<MemoryInfo>() {
2384 public MemoryInfo createFromParcel(Parcel source) {
2385 return new MemoryInfo(source);
2386 }
2387 public MemoryInfo[] newArray(int size) {
2388 return new MemoryInfo[size];
2389 }
2390 };
2391
2392 private MemoryInfo(Parcel source) {
2393 readFromParcel(source);
2394 }
2395 }
2396
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002397 /**
2398 * Return general information about the memory state of the system. This
2399 * can be used to help decide how to manage your own memory, though note
2400 * that polling is not recommended and
2401 * {@link android.content.ComponentCallbacks2#onTrimMemory(int)
2402 * ComponentCallbacks2.onTrimMemory(int)} is the preferred way to do this.
2403 * Also see {@link #getMyMemoryState} for how to retrieve the current trim
2404 * level of your process as needed, which gives a better hint for how to
2405 * manage its memory.
2406 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 public void getMemoryInfo(MemoryInfo outInfo) {
2408 try {
2409 ActivityManagerNative.getDefault().getMemoryInfo(outInfo);
2410 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002411 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 }
2413 }
Craig Mautner967212c2013-04-13 21:10:58 -07002414
Craig Mautner5ff12102013-05-24 12:50:15 -07002415 /**
Craig Mautner967212c2013-04-13 21:10:58 -07002416 * Information you can retrieve about an ActivityStack in the system.
2417 * @hide
2418 */
2419 public static class StackInfo implements Parcelable {
2420 public int stackId;
Craig Mautnered6649f2013-12-02 14:08:25 -08002421 public Rect bounds = new Rect();
Craig Mautner967212c2013-04-13 21:10:58 -07002422 public int[] taskIds;
2423 public String[] taskNames;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002424 public Rect[] taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002425 public int[] taskUserIds;
Winsond46b7272016-04-20 11:54:27 -07002426 public ComponentName topActivity;
Craig Mautnered6649f2013-12-02 14:08:25 -08002427 public int displayId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002428 public int userId;
Winsond46b7272016-04-20 11:54:27 -07002429 public boolean visible;
Winson529c8e42016-05-17 11:08:40 -07002430 // Index of the stack in the display's stack list, can be used for comparison of stack order
2431 public int position;
Craig Mautner967212c2013-04-13 21:10:58 -07002432
Craig Mautner967212c2013-04-13 21:10:58 -07002433 @Override
2434 public int describeContents() {
2435 return 0;
2436 }
2437
2438 @Override
2439 public void writeToParcel(Parcel dest, int flags) {
2440 dest.writeInt(stackId);
2441 dest.writeInt(bounds.left);
2442 dest.writeInt(bounds.top);
2443 dest.writeInt(bounds.right);
2444 dest.writeInt(bounds.bottom);
2445 dest.writeIntArray(taskIds);
2446 dest.writeStringArray(taskNames);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002447 final int boundsCount = taskBounds == null ? 0 : taskBounds.length;
2448 dest.writeInt(boundsCount);
2449 for (int i = 0; i < boundsCount; i++) {
2450 dest.writeInt(taskBounds[i].left);
2451 dest.writeInt(taskBounds[i].top);
2452 dest.writeInt(taskBounds[i].right);
2453 dest.writeInt(taskBounds[i].bottom);
2454 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002455 dest.writeIntArray(taskUserIds);
Craig Mautnered6649f2013-12-02 14:08:25 -08002456 dest.writeInt(displayId);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002457 dest.writeInt(userId);
Winsond46b7272016-04-20 11:54:27 -07002458 dest.writeInt(visible ? 1 : 0);
Winson529c8e42016-05-17 11:08:40 -07002459 dest.writeInt(position);
Winsond46b7272016-04-20 11:54:27 -07002460 if (topActivity != null) {
2461 dest.writeInt(1);
2462 topActivity.writeToParcel(dest, 0);
2463 } else {
2464 dest.writeInt(0);
2465 }
Craig Mautner967212c2013-04-13 21:10:58 -07002466 }
2467
2468 public void readFromParcel(Parcel source) {
2469 stackId = source.readInt();
2470 bounds = new Rect(
2471 source.readInt(), source.readInt(), source.readInt(), source.readInt());
2472 taskIds = source.createIntArray();
2473 taskNames = source.createStringArray();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002474 final int boundsCount = source.readInt();
2475 if (boundsCount > 0) {
2476 taskBounds = new Rect[boundsCount];
2477 for (int i = 0; i < boundsCount; i++) {
2478 taskBounds[i] = new Rect();
2479 taskBounds[i].set(
2480 source.readInt(), source.readInt(), source.readInt(), source.readInt());
2481 }
2482 } else {
2483 taskBounds = null;
2484 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002485 taskUserIds = source.createIntArray();
Craig Mautnered6649f2013-12-02 14:08:25 -08002486 displayId = source.readInt();
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002487 userId = source.readInt();
Winsond46b7272016-04-20 11:54:27 -07002488 visible = source.readInt() > 0;
Winson529c8e42016-05-17 11:08:40 -07002489 position = source.readInt();
Winsond46b7272016-04-20 11:54:27 -07002490 if (source.readInt() > 0) {
2491 topActivity = ComponentName.readFromParcel(source);
2492 }
Craig Mautner967212c2013-04-13 21:10:58 -07002493 }
2494
2495 public static final Creator<StackInfo> CREATOR = new Creator<StackInfo>() {
2496 @Override
2497 public StackInfo createFromParcel(Parcel source) {
2498 return new StackInfo(source);
2499 }
2500 @Override
2501 public StackInfo[] newArray(int size) {
2502 return new StackInfo[size];
2503 }
2504 };
2505
Craig Mautner5ff12102013-05-24 12:50:15 -07002506 public StackInfo() {
2507 }
2508
Craig Mautner967212c2013-04-13 21:10:58 -07002509 private StackInfo(Parcel source) {
2510 readFromParcel(source);
2511 }
2512
Craig Mautner5ff12102013-05-24 12:50:15 -07002513 public String toString(String prefix) {
Craig Mautner967212c2013-04-13 21:10:58 -07002514 StringBuilder sb = new StringBuilder(256);
Craig Mautner5ff12102013-05-24 12:50:15 -07002515 sb.append(prefix); sb.append("Stack id="); sb.append(stackId);
Craig Mautnered6649f2013-12-02 14:08:25 -08002516 sb.append(" bounds="); sb.append(bounds.toShortString());
2517 sb.append(" displayId="); sb.append(displayId);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002518 sb.append(" userId="); sb.append(userId);
Craig Mautnered6649f2013-12-02 14:08:25 -08002519 sb.append("\n");
Craig Mautner5ff12102013-05-24 12:50:15 -07002520 prefix = prefix + " ";
Craig Mautner967212c2013-04-13 21:10:58 -07002521 for (int i = 0; i < taskIds.length; ++i) {
2522 sb.append(prefix); sb.append("taskId="); sb.append(taskIds[i]);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002523 sb.append(": "); sb.append(taskNames[i]);
2524 if (taskBounds != null) {
2525 sb.append(" bounds="); sb.append(taskBounds[i].toShortString());
2526 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002527 sb.append(" userId=").append(taskUserIds[i]);
Winsond46b7272016-04-20 11:54:27 -07002528 sb.append(" visible=").append(visible);
2529 if (topActivity != null) {
2530 sb.append(" topActivity=").append(topActivity);
2531 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002532 sb.append("\n");
Craig Mautner967212c2013-04-13 21:10:58 -07002533 }
2534 return sb.toString();
2535 }
Craig Mautner5ff12102013-05-24 12:50:15 -07002536
2537 @Override
2538 public String toString() {
2539 return toString("");
2540 }
Craig Mautner967212c2013-04-13 21:10:58 -07002541 }
2542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 /**
2544 * @hide
2545 */
2546 public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
2547 try {
Stefan Kuhne16045c22015-06-05 07:18:06 -07002548 return ActivityManagerNative.getDefault().clearApplicationUserData(packageName,
Dianne Hackborn41203752012-08-31 14:05:51 -07002549 observer, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002551 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 }
2553 }
Christopher Tatea3664242013-06-26 15:00:18 -07002554
2555 /**
2556 * Permits an application to erase its own data from disk. This is equivalent to
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002557 * the user choosing to clear the app's data from within the device settings UI. It
2558 * erases all dynamic data associated with the app -- its private data and data in its
2559 * private area on external storage -- but does not remove the installed application
2560 * itself, nor any OBB files.
Christopher Tatea3664242013-06-26 15:00:18 -07002561 *
2562 * @return {@code true} if the application successfully requested that the application's
2563 * data be erased; {@code false} otherwise.
2564 */
2565 public boolean clearApplicationUserData() {
2566 return clearApplicationUserData(mContext.getPackageName(), null);
2567 }
2568
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002569
2570 /**
2571 * Permits an application to get the persistent URI permissions granted to another.
2572 *
2573 * <p>Typically called by Settings.
2574 *
2575 * @param packageName application to look for the granted permissions
2576 * @return list of granted URI permissions
2577 *
2578 * @hide
2579 */
2580 public ParceledListSlice<UriPermission> getGrantedUriPermissions(String packageName) {
2581 try {
2582 return ActivityManagerNative.getDefault().getGrantedUriPermissions(packageName,
2583 UserHandle.myUserId());
2584 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002585 throw e.rethrowFromSystemServer();
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002586 }
2587 }
2588
2589 /**
2590 * Permits an application to clear the persistent URI permissions granted to another.
2591 *
2592 * <p>Typically called by Settings.
2593 *
2594 * @param packageName application to clear its granted permissions
2595 *
2596 * @hide
2597 */
2598 public void clearGrantedUriPermissions(String packageName) {
2599 try {
2600 ActivityManagerNative.getDefault().clearGrantedUriPermissions(packageName,
2601 UserHandle.myUserId());
2602 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002603 throw e.rethrowFromSystemServer();
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002604 }
2605 }
2606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 /**
2608 * Information you can retrieve about any processes that are in an error condition.
2609 */
2610 public static class ProcessErrorStateInfo implements Parcelable {
2611 /**
2612 * Condition codes
2613 */
2614 public static final int NO_ERROR = 0;
2615 public static final int CRASHED = 1;
2616 public static final int NOT_RESPONDING = 2;
2617
2618 /**
2619 * The condition that the process is in.
2620 */
2621 public int condition;
2622
2623 /**
2624 * The process name in which the crash or error occurred.
2625 */
2626 public String processName;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 /**
2629 * The pid of this process; 0 if none
2630 */
2631 public int pid;
2632
2633 /**
2634 * The kernel user-ID that has been assigned to this process;
2635 * currently this is not a unique ID (multiple applications can have
2636 * the same uid).
2637 */
2638 public int uid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 /**
Dan Egnor60d87622009-12-16 16:32:58 -08002641 * The activity name associated with the error, if known. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 */
2643 public String tag;
2644
2645 /**
2646 * A short message describing the error condition.
2647 */
2648 public String shortMsg;
2649
2650 /**
2651 * A long message describing the error condition.
2652 */
2653 public String longMsg;
2654
2655 /**
Dan Egnorb7f03672009-12-09 16:22:32 -08002656 * The stack trace where the error originated. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 */
Dan Egnorb7f03672009-12-09 16:22:32 -08002658 public String stackTrace;
2659
2660 /**
2661 * to be deprecated: This value will always be null.
2662 */
2663 public byte[] crashData = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664
2665 public ProcessErrorStateInfo() {
2666 }
2667
Craig Mautner5ff12102013-05-24 12:50:15 -07002668 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 public int describeContents() {
2670 return 0;
2671 }
2672
Craig Mautner5ff12102013-05-24 12:50:15 -07002673 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 public void writeToParcel(Parcel dest, int flags) {
2675 dest.writeInt(condition);
2676 dest.writeString(processName);
2677 dest.writeInt(pid);
2678 dest.writeInt(uid);
2679 dest.writeString(tag);
2680 dest.writeString(shortMsg);
2681 dest.writeString(longMsg);
Dan Egnorb7f03672009-12-09 16:22:32 -08002682 dest.writeString(stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 }
Craig Mautner5ff12102013-05-24 12:50:15 -07002684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 public void readFromParcel(Parcel source) {
2686 condition = source.readInt();
2687 processName = source.readString();
2688 pid = source.readInt();
2689 uid = source.readInt();
2690 tag = source.readString();
2691 shortMsg = source.readString();
2692 longMsg = source.readString();
Dan Egnorb7f03672009-12-09 16:22:32 -08002693 stackTrace = source.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002695
2696 public static final Creator<ProcessErrorStateInfo> CREATOR =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 new Creator<ProcessErrorStateInfo>() {
2698 public ProcessErrorStateInfo createFromParcel(Parcel source) {
2699 return new ProcessErrorStateInfo(source);
2700 }
2701 public ProcessErrorStateInfo[] newArray(int size) {
2702 return new ProcessErrorStateInfo[size];
2703 }
2704 };
2705
2706 private ProcessErrorStateInfo(Parcel source) {
2707 readFromParcel(source);
2708 }
2709 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 /**
Stefan Kuhne16045c22015-06-05 07:18:06 -07002712 * 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 -08002713 * will be null if all processes are running properly at this time.
Stefan Kuhne16045c22015-06-05 07:18:06 -07002714 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 * @return Returns a list of ProcessErrorStateInfo records, or null if there are no
2716 * current error conditions (it will not return an empty list). This list ordering is not
2717 * specified.
2718 */
2719 public List<ProcessErrorStateInfo> getProcessesInErrorState() {
2720 try {
2721 return ActivityManagerNative.getDefault().getProcessesInErrorState();
2722 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002723 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
2725 }
2726
2727 /**
2728 * Information you can retrieve about a running process.
2729 */
Stefan Kuhne16045c22015-06-05 07:18:06 -07002730 public static class RunningAppProcessInfo implements Parcelable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 /**
2732 * The name of the process that this object is associated with
2733 */
2734 public String processName;
2735
2736 /**
2737 * The pid of this process; 0 if none
2738 */
2739 public int pid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002740
Dianne Hackborneb034652009-09-07 00:49:58 -07002741 /**
2742 * The user id of this process.
2743 */
2744 public int uid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002745
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002746 /**
2747 * All packages that have been loaded into the process.
2748 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 public String pkgList[];
Stefan Kuhne16045c22015-06-05 07:18:06 -07002750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 /**
Dianne Hackborn482566e2010-09-03 12:51:28 -07002752 * Constant for {@link #flags}: this is an app that is unable to
2753 * correctly save its state when going to the background,
2754 * so it can not be killed while in the background.
2755 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002756 */
Dianne Hackborn482566e2010-09-03 12:51:28 -07002757 public static final int FLAG_CANT_SAVE_STATE = 1<<0;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002758
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002759 /**
Dianne Hackborn42499172010-10-15 18:45:07 -07002760 * Constant for {@link #flags}: this process is associated with a
2761 * persistent system app.
2762 * @hide
2763 */
2764 public static final int FLAG_PERSISTENT = 1<<1;
2765
2766 /**
Dianne Hackborn0c380492012-08-20 17:23:30 -07002767 * Constant for {@link #flags}: this process is associated with a
2768 * persistent system app.
2769 * @hide
2770 */
2771 public static final int FLAG_HAS_ACTIVITIES = 1<<2;
2772
2773 /**
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002774 * Flags of information. May be any of
Dianne Hackborn482566e2010-09-03 12:51:28 -07002775 * {@link #FLAG_CANT_SAVE_STATE}.
2776 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002777 */
2778 public int flags;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002779
2780 /**
2781 * Last memory trim level reported to the process: corresponds to
2782 * the values supplied to {@link android.content.ComponentCallbacks2#onTrimMemory(int)
2783 * ComponentCallbacks2.onTrimMemory(int)}.
2784 */
2785 public int lastTrimLevel;
2786
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07002787 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002788 * Constant for {@link #importance}: This process is running the
2789 * foreground UI; that is, it is the thing currently at the top of the screen
2790 * that the user is interacting with.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 */
2792 public static final int IMPORTANCE_FOREGROUND = 100;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002795 * Constant for {@link #importance}: This process is running a foreground
2796 * service, for example to perform music playback even while the user is
2797 * not immediately in the app. This generally indicates that the process
2798 * is doing something the user actively cares about.
2799 */
2800 public static final int IMPORTANCE_FOREGROUND_SERVICE = 125;
2801
2802 /**
2803 * Constant for {@link #importance}: This process is running the foreground
2804 * UI, but the device is asleep so it is not visible to the user. This means
2805 * the user is not really aware of the process, because they can not see or
2806 * interact with it, but it is quite important because it what they expect to
2807 * return to once unlocking the device.
2808 */
2809 public static final int IMPORTANCE_TOP_SLEEPING = 150;
2810
2811 /**
2812 * Constant for {@link #importance}: This process is running something
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002813 * that is actively visible to the user, though not in the immediate
Dianne Hackborn1e383822015-04-10 14:02:33 -07002814 * foreground. This may be running a window that is behind the current
2815 * foreground (so paused and with its state saved, not interacting with
2816 * the user, but visible to them to some degree); it may also be running
2817 * other services under the system's control that it inconsiders important.
Dianne Hackborn860755f2010-06-03 18:47:52 -07002818 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002819 public static final int IMPORTANCE_VISIBLE = 200;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002820
Dianne Hackborn860755f2010-06-03 18:47:52 -07002821 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002822 * Constant for {@link #importance}: This process is not something the user
2823 * is directly aware of, but is otherwise perceptable to them to some degree.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002825 public static final int IMPORTANCE_PERCEPTIBLE = 130;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002826
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002827 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002828 * Constant for {@link #importance}: This process is running an
Dianne Hackborn5383f502010-10-22 12:59:20 -07002829 * application that can not save its state, and thus can't be killed
2830 * while in the background.
2831 * @hide
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002832 */
Dianne Hackborn5383f502010-10-22 12:59:20 -07002833 public static final int IMPORTANCE_CANT_SAVE_STATE = 170;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002836 * Constant for {@link #importance}: This process is contains services
2837 * that should remain running. These are background services apps have
2838 * started, not something the user is aware of, so they may be killed by
2839 * the system relatively freely (though it is generally desired that they
2840 * stay running as long as they want to).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 */
2842 public static final int IMPORTANCE_SERVICE = 300;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002845 * Constant for {@link #importance}: This process process contains
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 * background code that is expendable.
2847 */
2848 public static final int IMPORTANCE_BACKGROUND = 400;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002851 * Constant for {@link #importance}: This process is empty of any
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 * actively running code.
2853 */
2854 public static final int IMPORTANCE_EMPTY = 500;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002855
2856 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07002857 * Constant for {@link #importance}: This process does not exist.
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002858 */
2859 public static final int IMPORTANCE_GONE = 1000;
2860
2861 /** @hide */
2862 public static int procStateToImportance(int procState) {
Dianne Hackborn1e383822015-04-10 14:02:33 -07002863 if (procState == PROCESS_STATE_NONEXISTENT) {
2864 return IMPORTANCE_GONE;
2865 } else if (procState >= PROCESS_STATE_HOME) {
2866 return IMPORTANCE_BACKGROUND;
2867 } else if (procState >= PROCESS_STATE_SERVICE) {
2868 return IMPORTANCE_SERVICE;
2869 } else if (procState > PROCESS_STATE_HEAVY_WEIGHT) {
2870 return IMPORTANCE_CANT_SAVE_STATE;
2871 } else if (procState >= PROCESS_STATE_IMPORTANT_BACKGROUND) {
2872 return IMPORTANCE_PERCEPTIBLE;
2873 } else if (procState >= PROCESS_STATE_IMPORTANT_FOREGROUND) {
2874 return IMPORTANCE_VISIBLE;
2875 } else if (procState >= PROCESS_STATE_TOP_SLEEPING) {
2876 return IMPORTANCE_TOP_SLEEPING;
2877 } else if (procState >= PROCESS_STATE_FOREGROUND_SERVICE) {
2878 return IMPORTANCE_FOREGROUND_SERVICE;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002879 } else {
Dianne Hackborn1e383822015-04-10 14:02:33 -07002880 return IMPORTANCE_FOREGROUND;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002881 }
2882 }
2883
Dianne Hackborn5614bf52016-11-07 17:26:41 -08002884 /** @hide */
2885 public static int importanceToProcState(int importance) {
2886 if (importance == IMPORTANCE_GONE) {
2887 return PROCESS_STATE_NONEXISTENT;
2888 } else if (importance >= IMPORTANCE_BACKGROUND) {
2889 return PROCESS_STATE_HOME;
2890 } else if (importance >= IMPORTANCE_SERVICE) {
2891 return PROCESS_STATE_SERVICE;
2892 } else if (importance > IMPORTANCE_CANT_SAVE_STATE) {
2893 return PROCESS_STATE_HEAVY_WEIGHT;
2894 } else if (importance >= IMPORTANCE_PERCEPTIBLE) {
2895 return PROCESS_STATE_IMPORTANT_BACKGROUND;
2896 } else if (importance >= IMPORTANCE_VISIBLE) {
2897 return PROCESS_STATE_IMPORTANT_FOREGROUND;
2898 } else if (importance >= IMPORTANCE_TOP_SLEEPING) {
2899 return PROCESS_STATE_TOP_SLEEPING;
2900 } else if (importance >= IMPORTANCE_FOREGROUND_SERVICE) {
2901 return PROCESS_STATE_FOREGROUND_SERVICE;
2902 } else {
2903 return PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
2904 }
2905 }
2906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 /**
2908 * The relative importance level that the system places on this
2909 * process. May be one of {@link #IMPORTANCE_FOREGROUND},
2910 * {@link #IMPORTANCE_VISIBLE}, {@link #IMPORTANCE_SERVICE},
2911 * {@link #IMPORTANCE_BACKGROUND}, or {@link #IMPORTANCE_EMPTY}. These
2912 * constants are numbered so that "more important" values are always
2913 * smaller than "less important" values.
2914 */
2915 public int importance;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 /**
2918 * An additional ordering within a particular {@link #importance}
2919 * category, providing finer-grained information about the relative
2920 * utility of processes within a category. This number means nothing
2921 * except that a smaller values are more recently used (and thus
2922 * more important). Currently an LRU value is only maintained for
2923 * the {@link #IMPORTANCE_BACKGROUND} category, though others may
2924 * be maintained in the future.
2925 */
2926 public int lru;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08002927
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002928 /**
2929 * Constant for {@link #importanceReasonCode}: nothing special has
2930 * been specified for the reason for this level.
2931 */
2932 public static final int REASON_UNKNOWN = 0;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002933
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002934 /**
2935 * Constant for {@link #importanceReasonCode}: one of the application's
2936 * content providers is being used by another process. The pid of
2937 * the client process is in {@link #importanceReasonPid} and the
2938 * target provider in this process is in
2939 * {@link #importanceReasonComponent}.
2940 */
2941 public static final int REASON_PROVIDER_IN_USE = 1;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002942
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002943 /**
2944 * Constant for {@link #importanceReasonCode}: one of the application's
2945 * content providers is being used by another process. The pid of
2946 * the client process is in {@link #importanceReasonPid} and the
2947 * target provider in this process is in
2948 * {@link #importanceReasonComponent}.
2949 */
2950 public static final int REASON_SERVICE_IN_USE = 2;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002951
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002952 /**
2953 * The reason for {@link #importance}, if any.
2954 */
2955 public int importanceReasonCode;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002956
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002957 /**
2958 * For the specified values of {@link #importanceReasonCode}, this
2959 * is the process ID of the other process that is a client of this
2960 * process. This will be 0 if no other process is using this one.
2961 */
2962 public int importanceReasonPid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002963
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002964 /**
2965 * For the specified values of {@link #importanceReasonCode}, this
2966 * is the name of the component that is being used in this process.
2967 */
2968 public ComponentName importanceReasonComponent;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002969
Dianne Hackborn905577f2011-09-07 18:31:28 -07002970 /**
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07002971 * When {@link #importanceReasonPid} is non-0, this is the importance
Dianne Hackborn905577f2011-09-07 18:31:28 -07002972 * of the other pid. @hide
2973 */
2974 public int importanceReasonImportance;
2975
Dianne Hackborn684bf342014-04-29 17:56:57 -07002976 /**
2977 * Current process state, as per PROCESS_STATE_* constants.
2978 * @hide
2979 */
2980 public int processState;
2981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 public RunningAppProcessInfo() {
2983 importance = IMPORTANCE_FOREGROUND;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002984 importanceReasonCode = REASON_UNKNOWN;
Dianne Hackborn684bf342014-04-29 17:56:57 -07002985 processState = PROCESS_STATE_IMPORTANT_FOREGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) {
2989 processName = pProcessName;
2990 pid = pPid;
2991 pkgList = pArr;
2992 }
2993
2994 public int describeContents() {
2995 return 0;
2996 }
2997
2998 public void writeToParcel(Parcel dest, int flags) {
2999 dest.writeString(processName);
3000 dest.writeInt(pid);
Dianne Hackborneb034652009-09-07 00:49:58 -07003001 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 dest.writeStringArray(pkgList);
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003003 dest.writeInt(this.flags);
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003004 dest.writeInt(lastTrimLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 dest.writeInt(importance);
3006 dest.writeInt(lru);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003007 dest.writeInt(importanceReasonCode);
3008 dest.writeInt(importanceReasonPid);
3009 ComponentName.writeToParcel(importanceReasonComponent, dest);
Dianne Hackborn905577f2011-09-07 18:31:28 -07003010 dest.writeInt(importanceReasonImportance);
Dianne Hackborn684bf342014-04-29 17:56:57 -07003011 dest.writeInt(processState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 }
3013
3014 public void readFromParcel(Parcel source) {
3015 processName = source.readString();
3016 pid = source.readInt();
Dianne Hackborneb034652009-09-07 00:49:58 -07003017 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 pkgList = source.readStringArray();
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003019 flags = source.readInt();
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003020 lastTrimLevel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 importance = source.readInt();
3022 lru = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003023 importanceReasonCode = source.readInt();
3024 importanceReasonPid = source.readInt();
3025 importanceReasonComponent = ComponentName.readFromParcel(source);
Dianne Hackborn905577f2011-09-07 18:31:28 -07003026 importanceReasonImportance = source.readInt();
Dianne Hackborn684bf342014-04-29 17:56:57 -07003027 processState = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 }
3029
Stefan Kuhne16045c22015-06-05 07:18:06 -07003030 public static final Creator<RunningAppProcessInfo> CREATOR =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 new Creator<RunningAppProcessInfo>() {
3032 public RunningAppProcessInfo createFromParcel(Parcel source) {
3033 return new RunningAppProcessInfo(source);
3034 }
3035 public RunningAppProcessInfo[] newArray(int size) {
3036 return new RunningAppProcessInfo[size];
3037 }
3038 };
3039
3040 private RunningAppProcessInfo(Parcel source) {
3041 readFromParcel(source);
3042 }
3043 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07003044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 /**
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07003046 * Returns a list of application processes installed on external media
3047 * that are running on the device.
3048 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07003049 * <p><b>Note: this method is only intended for debugging or building
3050 * a user-facing process management UI.</b></p>
3051 *
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07003052 * @return Returns a list of ApplicationInfo records, or null if none
3053 * This list ordering is not specified.
3054 * @hide
3055 */
3056 public List<ApplicationInfo> getRunningExternalApplications() {
3057 try {
3058 return ActivityManagerNative.getDefault().getRunningExternalApplications();
3059 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003060 throw e.rethrowFromSystemServer();
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07003061 }
3062 }
3063
3064 /**
Stefan Kuhne16045c22015-06-05 07:18:06 -07003065 * Sets the memory trim mode for a process and schedules a memory trim operation.
3066 *
3067 * <p><b>Note: this method is only intended for testing framework.</b></p>
3068 *
3069 * @return Returns true if successful.
3070 * @hide
3071 */
3072 public boolean setProcessMemoryTrimLevel(String process, int userId, int level) {
3073 try {
3074 return ActivityManagerNative.getDefault().setProcessMemoryTrimLevel(process, userId,
3075 level);
3076 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003077 throw e.rethrowFromSystemServer();
Stefan Kuhne16045c22015-06-05 07:18:06 -07003078 }
3079 }
3080
3081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 * Returns a list of application processes that are running on the device.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07003083 *
3084 * <p><b>Note: this method is only intended for debugging or building
3085 * a user-facing process management UI.</b></p>
3086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 * @return Returns a list of RunningAppProcessInfo records, or null if there are no
3088 * running processes (it will not return an empty list). This list ordering is not
3089 * specified.
3090 */
3091 public List<RunningAppProcessInfo> getRunningAppProcesses() {
3092 try {
3093 return ActivityManagerNative.getDefault().getRunningAppProcesses();
3094 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003095 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 }
3097 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003098
3099 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07003100 * Return the importance of a given package name, based on the processes that are
3101 * currently running. The return value is one of the importance constants defined
3102 * in {@link RunningAppProcessInfo}, giving you the highest importance of all the
3103 * processes that this package has code running inside of. If there are no processes
3104 * running its code, {@link RunningAppProcessInfo#IMPORTANCE_GONE} is returned.
Jeff Sharkeyf85ac772015-05-13 12:52:06 -07003105 * @hide
Dianne Hackborn1e383822015-04-10 14:02:33 -07003106 */
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003107 @SystemApi @TestApi
Dianne Hackborn9df8f512016-10-24 17:58:30 -07003108 @RequiresPermission(Manifest.permission.PACKAGE_USAGE_STATS)
Dianne Hackborn1e383822015-04-10 14:02:33 -07003109 public int getPackageImportance(String packageName) {
3110 try {
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003111 int procState = getService().getPackageProcessState(packageName,
Adam Lesinskic30454c2015-06-24 13:24:35 -07003112 mContext.getOpPackageName());
Dianne Hackborn1e383822015-04-10 14:02:33 -07003113 return RunningAppProcessInfo.procStateToImportance(procState);
3114 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003115 throw e.rethrowFromSystemServer();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003116 }
3117 }
3118
3119 /**
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003120 * Callback to get reports about changes to the importance of a uid. Use with
3121 * {@link #addOnUidImportanceListener}.
3122 * @hide
3123 */
3124 @SystemApi @TestApi
3125 public interface OnUidImportanceListener {
3126 /**
3127 * The importance if a given uid has changed. Will be one of the importance
3128 * values in {@link RunningAppProcessInfo};
3129 * {@link RunningAppProcessInfo#IMPORTANCE_GONE IMPORTANCE_GONE} will be reported
3130 * when the uid is no longer running at all. This callback will happen on a thread
3131 * from a thread pool, not the main UI thread.
3132 * @param uid The uid whose importance has changed.
3133 * @param importance The new importance value as per {@link RunningAppProcessInfo}.
3134 */
3135 void onUidImportance(int uid, int importance);
3136 }
3137
3138 /**
3139 * Start monitoring changes to the imoportance of uids running in the system.
3140 * @param listener The listener callback that will receive change reports.
3141 * @param importanceCutpoint The level of importance in which the caller is interested
3142 * in differences. For example, if {@link RunningAppProcessInfo#IMPORTANCE_PERCEPTIBLE}
3143 * is used here, you will receive a call each time a uids importance transitions between
3144 * being <= {@link RunningAppProcessInfo#IMPORTANCE_PERCEPTIBLE} and
3145 * > {@link RunningAppProcessInfo#IMPORTANCE_PERCEPTIBLE}.
3146 *
3147 * <p>The caller must hold the {@link android.Manifest.permission#PACKAGE_USAGE_STATS}
3148 * permission to use this feature.</p>
3149 *
3150 * @throws IllegalArgumentException If the listener is already registered.
3151 * @throws SecurityException If the caller does not hold
3152 * {@link android.Manifest.permission#PACKAGE_USAGE_STATS}.
3153 * @hide
3154 */
3155 @SystemApi @TestApi
3156 public void addOnUidImportanceListener(OnUidImportanceListener listener,
3157 int importanceCutpoint) {
3158 synchronized (this) {
3159 if (mImportanceListeners.containsKey(listener)) {
3160 throw new IllegalArgumentException("Listener already registered: " + listener);
3161 }
3162 // TODO: implement the cut point in the system process to avoid IPCs.
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003163 UidObserver observer = new UidObserver(listener);
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003164 try {
3165 getService().registerUidObserver(observer,
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003166 UID_OBSERVER_PROCSTATE | UID_OBSERVER_GONE,
3167 RunningAppProcessInfo.importanceToProcState(importanceCutpoint),
3168 mContext.getOpPackageName());
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003169 } catch (RemoteException e) {
3170 throw e.rethrowFromSystemServer();
3171 }
3172 mImportanceListeners.put(listener, observer);
3173 }
3174 }
3175
3176 /**
3177 * Remove an importance listener that was previously registered with
3178 * {@link #addOnUidImportanceListener}.
3179 *
3180 * @throws IllegalArgumentException If the listener is not registered.
3181 * @hide
3182 */
3183 @SystemApi @TestApi
3184 public void removeOnUidImportanceListener(OnUidImportanceListener listener) {
3185 synchronized (this) {
3186 UidObserver observer = mImportanceListeners.remove(listener);
3187 if (observer == null) {
3188 throw new IllegalArgumentException("Listener not registered: " + listener);
3189 }
3190 try {
3191 getService().unregisterUidObserver(observer);
3192 } catch (RemoteException e) {
3193 throw e.rethrowFromSystemServer();
3194 }
3195 }
3196 }
3197
3198 /**
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003199 * Return global memory state information for the calling process. This
3200 * does not fill in all fields of the {@link RunningAppProcessInfo}. The
3201 * only fields that will be filled in are
3202 * {@link RunningAppProcessInfo#pid},
3203 * {@link RunningAppProcessInfo#uid},
3204 * {@link RunningAppProcessInfo#lastTrimLevel},
3205 * {@link RunningAppProcessInfo#importance},
3206 * {@link RunningAppProcessInfo#lru}, and
3207 * {@link RunningAppProcessInfo#importanceReasonCode}.
3208 */
3209 static public void getMyMemoryState(RunningAppProcessInfo outState) {
3210 try {
3211 ActivityManagerNative.getDefault().getMyMemoryState(outState);
3212 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003213 throw e.rethrowFromSystemServer();
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003214 }
3215 }
3216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003218 * Return information about the memory usage of one or more processes.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07003219 *
3220 * <p><b>Note: this method is only intended for debugging or building
3221 * a user-facing process management UI.</b></p>
3222 *
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003223 * @param pids The pids of the processes whose memory usage is to be
3224 * retrieved.
3225 * @return Returns an array of memory information, one for each
3226 * requested pid.
3227 */
3228 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
3229 try {
3230 return ActivityManagerNative.getDefault().getProcessMemoryInfo(pids);
3231 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003232 throw e.rethrowFromSystemServer();
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003233 }
3234 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07003235
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003236 /**
Dianne Hackborn03abb812010-01-04 18:43:19 -08003237 * @deprecated This is now just a wrapper for
3238 * {@link #killBackgroundProcesses(String)}; the previous behavior here
3239 * is no longer available to applications because it allows them to
3240 * break other applications by removing their alarms, stopping their
3241 * services, etc.
3242 */
3243 @Deprecated
3244 public void restartPackage(String packageName) {
3245 killBackgroundProcesses(packageName);
3246 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07003247
Dianne Hackborn03abb812010-01-04 18:43:19 -08003248 /**
3249 * Have the system immediately kill all background processes associated
3250 * with the given package. This is the same as the kernel killing those
3251 * processes to reclaim memory; the system will take care of restarting
3252 * these processes in the future as needed.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003253 *
Dianne Hackborn03abb812010-01-04 18:43:19 -08003254 * <p>You must hold the permission
3255 * {@link android.Manifest.permission#KILL_BACKGROUND_PROCESSES} to be able to
3256 * call this method.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003257 *
Dianne Hackborn03abb812010-01-04 18:43:19 -08003258 * @param packageName The name of the package whose processes are to
3259 * be killed.
3260 */
3261 public void killBackgroundProcesses(String packageName) {
3262 try {
Dianne Hackborn1676c852012-09-10 14:52:30 -07003263 ActivityManagerNative.getDefault().killBackgroundProcesses(packageName,
3264 UserHandle.myUserId());
Dianne Hackborn03abb812010-01-04 18:43:19 -08003265 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003266 throw e.rethrowFromSystemServer();
Dianne Hackborn03abb812010-01-04 18:43:19 -08003267 }
3268 }
Svet Ganov019d2302015-05-04 11:07:38 -07003269
3270 /**
3271 * Kills the specified UID.
3272 * @param uid The UID to kill.
3273 * @param reason The reason for the kill.
3274 *
3275 * @hide
3276 */
Svet Ganovae0e03a2016-02-25 18:22:10 -08003277 @SystemApi
Svet Ganov019d2302015-05-04 11:07:38 -07003278 @RequiresPermission(Manifest.permission.KILL_UID)
3279 public void killUid(int uid, String reason) {
3280 try {
Svetoslavaa41add2015-08-06 15:03:55 -07003281 ActivityManagerNative.getDefault().killUid(UserHandle.getAppId(uid),
3282 UserHandle.getUserId(uid), reason);
Svet Ganov019d2302015-05-04 11:07:38 -07003283 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003284 throw e.rethrowFromSystemServer();
Svet Ganov019d2302015-05-04 11:07:38 -07003285 }
3286 }
3287
Dianne Hackborn03abb812010-01-04 18:43:19 -08003288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 * Have the system perform a force stop of everything associated with
3290 * the given application package. All processes that share its uid
3291 * will be killed, all services it has running stopped, all activities
3292 * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}
3293 * broadcast will be sent, so that any of its registered alarms can
3294 * be stopped, notifications removed, etc.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003295 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 * <p>You must hold the permission
Dianne Hackborn03abb812010-01-04 18:43:19 -08003297 * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 * call this method.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 * @param packageName The name of the package to be stopped.
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003301 * @param userId The user for which the running package is to be stopped.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003302 *
Dianne Hackborn03abb812010-01-04 18:43:19 -08003303 * @hide This is not available to third party applications due to
3304 * it allowing them to break other applications by stopping their
3305 * services, removing their alarms, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 */
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003307 public void forceStopPackageAsUser(String packageName, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 try {
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003309 ActivityManagerNative.getDefault().forceStopPackage(packageName, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003311 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 }
3313 }
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003314
3315 /**
3316 * @see #forceStopPackageAsUser(String, int)
3317 * @hide
3318 */
3319 public void forceStopPackage(String packageName) {
3320 forceStopPackageAsUser(packageName, UserHandle.myUserId());
3321 }
3322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 /**
3324 * Get the device configuration attributes.
3325 */
3326 public ConfigurationInfo getDeviceConfigurationInfo() {
3327 try {
3328 return ActivityManagerNative.getDefault().getDeviceConfigurationInfo();
3329 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003330 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 }
Kenny Root5ef44b72011-01-26 17:22:20 -08003333
3334 /**
3335 * Get the preferred density of icons for the launcher. This is used when
3336 * custom drawables are created (e.g., for shortcuts).
3337 *
3338 * @return density in terms of DPI
3339 */
3340 public int getLauncherLargeIconDensity() {
3341 final Resources res = mContext.getResources();
3342 final int density = res.getDisplayMetrics().densityDpi;
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003343 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08003344
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003345 if (sw < 600) {
3346 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08003347 return density;
3348 }
3349
3350 switch (density) {
3351 case DisplayMetrics.DENSITY_LOW:
3352 return DisplayMetrics.DENSITY_MEDIUM;
3353 case DisplayMetrics.DENSITY_MEDIUM:
3354 return DisplayMetrics.DENSITY_HIGH;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07003355 case DisplayMetrics.DENSITY_TV:
3356 return DisplayMetrics.DENSITY_XHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08003357 case DisplayMetrics.DENSITY_HIGH:
3358 return DisplayMetrics.DENSITY_XHIGH;
3359 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003360 return DisplayMetrics.DENSITY_XXHIGH;
3361 case DisplayMetrics.DENSITY_XXHIGH:
3362 return DisplayMetrics.DENSITY_XHIGH * 2;
Kenny Root5ef44b72011-01-26 17:22:20 -08003363 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003364 // The density is some abnormal value. Return some other
3365 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07003366 return (int)((density*1.5f)+.5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08003367 }
3368 }
3369
3370 /**
3371 * Get the preferred launcher icon size. This is used when custom drawables
3372 * are created (e.g., for shortcuts).
3373 *
3374 * @return dimensions of square icons in terms of pixels
3375 */
3376 public int getLauncherLargeIconSize() {
Craig Mautner2fbd7542014-03-21 09:34:07 -07003377 return getLauncherLargeIconSizeInner(mContext);
3378 }
3379
3380 static int getLauncherLargeIconSizeInner(Context context) {
3381 final Resources res = context.getResources();
Kenny Root5ef44b72011-01-26 17:22:20 -08003382 final int size = res.getDimensionPixelSize(android.R.dimen.app_icon_size);
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003383 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08003384
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003385 if (sw < 600) {
3386 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08003387 return size;
3388 }
3389
3390 final int density = res.getDisplayMetrics().densityDpi;
3391
3392 switch (density) {
3393 case DisplayMetrics.DENSITY_LOW:
3394 return (size * DisplayMetrics.DENSITY_MEDIUM) / DisplayMetrics.DENSITY_LOW;
3395 case DisplayMetrics.DENSITY_MEDIUM:
3396 return (size * DisplayMetrics.DENSITY_HIGH) / DisplayMetrics.DENSITY_MEDIUM;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07003397 case DisplayMetrics.DENSITY_TV:
3398 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08003399 case DisplayMetrics.DENSITY_HIGH:
3400 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
3401 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003402 return (size * DisplayMetrics.DENSITY_XXHIGH) / DisplayMetrics.DENSITY_XHIGH;
3403 case DisplayMetrics.DENSITY_XXHIGH:
3404 return (size * DisplayMetrics.DENSITY_XHIGH*2) / DisplayMetrics.DENSITY_XXHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08003405 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08003406 // The density is some abnormal value. Return some other
3407 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07003408 return (int)((size*1.5f) + .5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08003409 }
3410 }
3411
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08003412 /**
3413 * Returns "true" if the user interface is currently being messed with
3414 * by a monkey.
3415 */
3416 public static boolean isUserAMonkey() {
3417 try {
3418 return ActivityManagerNative.getDefault().isUserAMonkey();
3419 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003420 throw e.rethrowFromSystemServer();
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08003421 }
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08003422 }
Brett Chabot3b4fcbc2011-01-09 13:41:02 -08003423
3424 /**
3425 * Returns "true" if device is running in a test harness.
3426 */
3427 public static boolean isRunningInTestHarness() {
3428 return SystemProperties.getBoolean("ro.test_harness", false);
3429 }
Peter Visontay8d224ca2011-02-18 16:39:19 +00003430
3431 /**
3432 * Returns the launch count of each installed package.
3433 *
3434 * @hide
3435 */
Adam Lesinski0debc9a2014-07-16 19:09:13 -07003436 /*public Map<String, Integer> getAllPackageLaunchCounts() {
Peter Visontay8d224ca2011-02-18 16:39:19 +00003437 try {
3438 IUsageStats usageStatsService = IUsageStats.Stub.asInterface(
3439 ServiceManager.getService("usagestats"));
3440 if (usageStatsService == null) {
3441 return new HashMap<String, Integer>();
3442 }
3443
Dianne Hackborne22b3b12014-05-07 18:06:44 -07003444 UsageStats.PackageStats[] allPkgUsageStats = usageStatsService.getAllPkgUsageStats(
3445 ActivityThread.currentPackageName());
Peter Visontaybfcda392011-03-02 18:53:37 +00003446 if (allPkgUsageStats == null) {
3447 return new HashMap<String, Integer>();
3448 }
3449
Peter Visontay8d224ca2011-02-18 16:39:19 +00003450 Map<String, Integer> launchCounts = new HashMap<String, Integer>();
Dianne Hackborne22b3b12014-05-07 18:06:44 -07003451 for (UsageStats.PackageStats pkgUsageStats : allPkgUsageStats) {
3452 launchCounts.put(pkgUsageStats.getPackageName(), pkgUsageStats.getLaunchCount());
Peter Visontay8d224ca2011-02-18 16:39:19 +00003453 }
3454
3455 return launchCounts;
3456 } catch (RemoteException e) {
3457 Log.w(TAG, "Could not query launch counts", e);
3458 return new HashMap<String, Integer>();
3459 }
Adam Lesinski0debc9a2014-07-16 19:09:13 -07003460 }*/
Amith Yamasani4b2e9342011-03-31 12:38:53 -07003461
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003462 /** @hide */
3463 public static int checkComponentPermission(String permission, int uid,
3464 int owningUid, boolean exported) {
3465 // Root, system server get to do everything.
Svetoslavc6d1c342015-02-26 14:44:43 -08003466 final int appId = UserHandle.getAppId(uid);
3467 if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003468 return PackageManager.PERMISSION_GRANTED;
3469 }
3470 // Isolated processes don't get any permissions.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003471 if (UserHandle.isIsolated(uid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003472 return PackageManager.PERMISSION_DENIED;
3473 }
3474 // If there is a uid that owns whatever is being accessed, it has
3475 // blanket access to it regardless of the permissions it requires.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003476 if (owningUid >= 0 && UserHandle.isSameApp(uid, owningUid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003477 return PackageManager.PERMISSION_GRANTED;
3478 }
3479 // If the target is not exported, then nobody else can get to it.
3480 if (!exported) {
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -07003481 /*
3482 RuntimeException here = new RuntimeException("here");
3483 here.fillInStackTrace();
3484 Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid,
3485 here);
3486 */
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003487 return PackageManager.PERMISSION_DENIED;
3488 }
3489 if (permission == null) {
3490 return PackageManager.PERMISSION_GRANTED;
3491 }
3492 try {
3493 return AppGlobals.getPackageManager()
3494 .checkUidPermission(permission, uid);
3495 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003496 throw e.rethrowFromSystemServer();
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003497 }
Dianne Hackborn5320eb82012-05-18 12:05:04 -07003498 }
3499
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003500 /** @hide */
3501 public static int checkUidPermission(String permission, int uid) {
3502 try {
3503 return AppGlobals.getPackageManager()
3504 .checkUidPermission(permission, uid);
3505 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003506 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003507 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003508 }
3509
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08003510 /**
3511 * @hide
3512 * Helper for dealing with incoming user arguments to system service calls.
3513 * Takes care of checking permissions and converting USER_CURRENT to the
3514 * actual current user.
3515 *
3516 * @param callingPid The pid of the incoming call, as per Binder.getCallingPid().
3517 * @param callingUid The uid of the incoming call, as per Binder.getCallingUid().
3518 * @param userId The user id argument supplied by the caller -- this is the user
3519 * they want to run as.
3520 * @param allowAll If true, we will allow USER_ALL. This means you must be prepared
3521 * to get a USER_ALL returned and deal with it correctly. If false,
3522 * an exception will be thrown if USER_ALL is supplied.
3523 * @param requireFull If true, the caller must hold
3524 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} to be able to run as a
3525 * different user than their current process; otherwise they must hold
3526 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS}.
3527 * @param name Optional textual name of the incoming call; only for generating error messages.
3528 * @param callerPackage Optional package name of caller; only for error messages.
3529 *
3530 * @return Returns the user ID that the call should run as. Will always be a concrete
3531 * user number, unless <var>allowAll</var> is true in which case it could also be
3532 * USER_ALL.
3533 */
Dianne Hackborn41203752012-08-31 14:05:51 -07003534 public static int handleIncomingUser(int callingPid, int callingUid, int userId,
3535 boolean allowAll, boolean requireFull, String name, String callerPackage) {
3536 if (UserHandle.getUserId(callingUid) == userId) {
3537 return userId;
3538 }
3539 try {
3540 return ActivityManagerNative.getDefault().handleIncomingUser(callingPid,
3541 callingUid, userId, allowAll, requireFull, name, callerPackage);
3542 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003543 throw e.rethrowFromSystemServer();
Dianne Hackborn41203752012-08-31 14:05:51 -07003544 }
3545 }
3546
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07003547 /**
3548 * Gets the userId of the current foreground user. Requires system permissions.
3549 * @hide
3550 */
3551 @SystemApi
Dianne Hackborn41203752012-08-31 14:05:51 -07003552 public static int getCurrentUser() {
3553 UserInfo ui;
3554 try {
3555 ui = ActivityManagerNative.getDefault().getCurrentUser();
3556 return ui != null ? ui.id : 0;
3557 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003558 throw e.rethrowFromSystemServer();
Dianne Hackborn41203752012-08-31 14:05:51 -07003559 }
3560 }
3561
Amith Yamasani4b2e9342011-03-31 12:38:53 -07003562 /**
Stefan Kuhne16045c22015-06-05 07:18:06 -07003563 * @param userid the user's id. Zero indicates the default user.
Amith Yamasani4b2e9342011-03-31 12:38:53 -07003564 * @hide
3565 */
3566 public boolean switchUser(int userid) {
3567 try {
3568 return ActivityManagerNative.getDefault().switchUser(userid);
3569 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003570 throw e.rethrowFromSystemServer();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07003571 }
3572 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003573
Fyodor Kupolovb5690bc32016-03-02 17:50:58 -08003574 /**
3575 * Logs out current current foreground user by switching to the system user and stopping the
3576 * user being switched from.
3577 * @hide
3578 */
3579 public static void logoutCurrentUser() {
3580 int currentUser = ActivityManager.getCurrentUser();
3581 if (currentUser != UserHandle.USER_SYSTEM) {
3582 try {
3583 ActivityManagerNative.getDefault().switchUser(UserHandle.USER_SYSTEM);
3584 ActivityManagerNative.getDefault().stopUser(currentUser, /* force= */ false, null);
3585 } catch (RemoteException e) {
3586 e.rethrowFromSystemServer();
3587 }
3588 }
3589 }
3590
Jeff Sharkeye17ac152015-11-06 22:40:29 -08003591 /** {@hide} */
3592 public static final int FLAG_OR_STOPPED = 1 << 0;
3593 /** {@hide} */
Jeff Sharkey8924e872015-11-30 12:52:10 -07003594 public static final int FLAG_AND_LOCKED = 1 << 1;
Jeff Sharkey0825ab22015-12-02 13:04:49 -07003595 /** {@hide} */
3596 public static final int FLAG_AND_UNLOCKED = 1 << 2;
Jeff Sharkeyce18c812016-04-27 16:00:41 -06003597 /** {@hide} */
3598 public static final int FLAG_AND_UNLOCKING_OR_UNLOCKED = 1 << 3;
Jeff Sharkeye17ac152015-11-06 22:40:29 -08003599
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003600 /**
3601 * Return whether the given user is actively running. This means that
3602 * the user is in the "started" state, not "stopped" -- it is currently
3603 * allowed to run code through scheduled alarms, receiving broadcasts,
3604 * etc. A started user may be either the current foreground user or a
3605 * background user; the result here does not distinguish between the two.
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003606 * @param userId the user's id. Zero indicates the default user.
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003607 * @hide
3608 */
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07003609 public boolean isUserRunning(int userId) {
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07003610 try {
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07003611 return ActivityManagerNative.getDefault().isUserRunning(userId, 0);
3612 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003613 throw e.rethrowFromSystemServer();
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07003614 }
3615 }
3616
3617 /** {@hide} */
Ruben Brunke24b9a62016-02-16 21:38:24 -08003618 public boolean isVrModePackageEnabled(ComponentName component) {
3619 try {
3620 return ActivityManagerNative.getDefault().isVrModePackageEnabled(component);
3621 } catch (RemoteException e) {
3622 throw e.rethrowFromSystemServer();
3623 }
3624 }
3625
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003626 /**
3627 * Perform a system dump of various state associated with the given application
3628 * package name. This call blocks while the dump is being performed, so should
3629 * not be done on a UI thread. The data will be written to the given file
3630 * descriptor as text. An application must hold the
3631 * {@link android.Manifest.permission#DUMP} permission to make this call.
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07003632 * @param fd The file descriptor that the dump should be written to. The file
3633 * descriptor is <em>not</em> closed by this function; the caller continues to
3634 * own it.
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003635 * @param packageName The name of the package that is to be dumped.
3636 */
3637 public void dumpPackageState(FileDescriptor fd, String packageName) {
3638 dumpPackageStateStatic(fd, packageName);
3639 }
3640
3641 /**
3642 * @hide
3643 */
3644 public static void dumpPackageStateStatic(FileDescriptor fd, String packageName) {
3645 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07003646 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07003647 dumpService(pw, fd, "package", new String[] { packageName });
3648 pw.println();
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08003649 dumpService(pw, fd, Context.ACTIVITY_SERVICE, new String[] {
3650 "-a", "package", packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003651 pw.println();
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07003652 dumpService(pw, fd, "meminfo", new String[] { "--local", "--package", packageName });
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08003653 pw.println();
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07003654 dumpService(pw, fd, ProcessStats.SERVICE_NAME, new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003655 pw.println();
Dianne Hackborn904a8572013-06-28 18:12:31 -07003656 dumpService(pw, fd, "usagestats", new String[] { "--packages", packageName });
3657 pw.println();
Dianne Hackborn8c841092013-06-24 13:46:13 -07003658 dumpService(pw, fd, BatteryStats.SERVICE_NAME, new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003659 pw.flush();
3660 }
3661
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07003662 /**
3663 * @hide
3664 */
3665 public static void broadcastStickyIntent(Intent intent, int userId) {
3666 broadcastStickyIntent(intent, AppOpsManager.OP_NONE, userId);
3667 }
3668
3669 /**
3670 * Convenience for sending a sticky broadcast. For internal use only.
3671 *
3672 * @hide
3673 */
3674 public static void broadcastStickyIntent(Intent intent, int appOp, int userId) {
3675 try {
3676 getService().broadcastIntent(
3677 null, intent, null, null, Activity.RESULT_OK, null, null,
3678 null /*permission*/, appOp, null, false, true, userId);
3679 } catch (RemoteException ex) {
3680 }
3681 }
3682
3683 /**
3684 * @hide
3685 */
3686 public static void noteWakeupAlarm(PendingIntent ps, int sourceUid, String sourcePkg,
3687 String tag) {
3688 try {
3689 getService().noteWakeupAlarm((ps != null) ? ps.getTarget() : null,
3690 sourceUid, sourcePkg, tag);
3691 } catch (RemoteException ex) {
3692 }
3693 }
3694
3695 /**
3696 * @hide
3697 */
3698 public static void noteAlarmStart(PendingIntent ps, int sourceUid, String tag) {
3699 try {
3700 getService().noteAlarmStart((ps != null) ? ps.getTarget() : null, sourceUid, tag);
3701 } catch (RemoteException ex) {
3702 }
3703 }
3704
3705 /**
3706 * @hide
3707 */
3708 public static void noteAlarmFinish(PendingIntent ps, int sourceUid, String tag) {
3709 try {
3710 getService().noteAlarmFinish((ps != null) ? ps.getTarget() : null, sourceUid, tag);
3711 } catch (RemoteException ex) {
3712 }
3713 }
3714
3715 /**
3716 * @hide
3717 */
3718 public static IActivityManager getService() {
3719 return IActivityManagerSingleton.get();
3720 }
3721
3722 private static final Singleton<IActivityManager> IActivityManagerSingleton =
3723 new Singleton<IActivityManager>() {
3724 @Override
3725 protected IActivityManager create() {
3726 final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
3727 final IActivityManager am = IActivityManager.Stub.asInterface(b);
3728 return am;
3729 }
3730 };
3731
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003732 private static void dumpService(PrintWriter pw, FileDescriptor fd, String name, String[] args) {
3733 pw.print("DUMP OF SERVICE "); pw.print(name); pw.println(":");
3734 IBinder service = ServiceManager.checkService(name);
3735 if (service == null) {
3736 pw.println(" (Service not found)");
3737 return;
3738 }
3739 TransferPipe tp = null;
3740 try {
3741 pw.flush();
3742 tp = new TransferPipe();
3743 tp.setBufferPrefix(" ");
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08003744 service.dumpAsync(tp.getWriteFd().getFileDescriptor(), args);
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07003745 tp.go(fd, 10000);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003746 } catch (Throwable e) {
3747 if (tp != null) {
3748 tp.kill();
3749 }
3750 pw.println("Failure dumping service:");
3751 e.printStackTrace(pw);
3752 }
3753 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003754
3755 /**
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07003756 * Request that the system start watching for the calling process to exceed a pss
Dianne Hackborn17f69352015-07-17 18:04:14 -07003757 * size as given here. Once called, the system will look for any occasions where it
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07003758 * sees the associated process with a larger pss size and, when this happens, automatically
3759 * pull a heap dump from it and allow the user to share the data. Note that this request
3760 * continues running even if the process is killed and restarted. To remove the watch,
3761 * use {@link #clearWatchHeapLimit()}.
3762 *
Dianne Hackbornece0f4f2015-06-11 13:29:01 -07003763 * <p>This API only work if the calling process has been marked as
3764 * {@link ApplicationInfo#FLAG_DEBUGGABLE} or this is running on a debuggable
3765 * (userdebug or eng) build.</p>
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07003766 *
3767 * <p>Callers can optionally implement {@link #ACTION_REPORT_HEAP_LIMIT} to directly
3768 * handle heap limit reports themselves.</p>
3769 *
3770 * @param pssSize The size in bytes to set the limit at.
3771 */
3772 public void setWatchHeapLimit(long pssSize) {
3773 try {
3774 ActivityManagerNative.getDefault().setDumpHeapDebugLimit(null, 0, pssSize,
3775 mContext.getPackageName());
3776 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003777 throw e.rethrowFromSystemServer();
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07003778 }
3779 }
3780
3781 /**
3782 * Action an app can implement to handle reports from {@link #setWatchHeapLimit(long)}.
3783 * If your package has an activity handling this action, it will be launched with the
3784 * heap data provided to it the same way as {@link Intent#ACTION_SEND}. Note that to
3785 * match the activty must support this action and a MIME type of "*&#47;*".
3786 */
3787 public static final String ACTION_REPORT_HEAP_LIMIT = "android.app.action.REPORT_HEAP_LIMIT";
3788
3789 /**
3790 * Clear a heap watch limit previously set by {@link #setWatchHeapLimit(long)}.
3791 */
3792 public void clearWatchHeapLimit() {
3793 try {
3794 ActivityManagerNative.getDefault().setDumpHeapDebugLimit(null, 0, 0, null);
3795 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003796 throw e.rethrowFromSystemServer();
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07003797 }
3798 }
3799
3800 /**
Craig Mautneraea74a52014-03-08 14:23:10 -08003801 * @hide
3802 */
3803 public void startLockTaskMode(int taskId) {
3804 try {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07003805 ActivityManagerNative.getDefault().startLockTaskModeById(taskId);
Craig Mautneraea74a52014-03-08 14:23:10 -08003806 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003807 throw e.rethrowFromSystemServer();
Craig Mautneraea74a52014-03-08 14:23:10 -08003808 }
3809 }
3810
3811 /**
3812 * @hide
3813 */
3814 public void stopLockTaskMode() {
3815 try {
3816 ActivityManagerNative.getDefault().stopLockTaskMode();
3817 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003818 throw e.rethrowFromSystemServer();
Craig Mautneraea74a52014-03-08 14:23:10 -08003819 }
3820 }
3821
3822 /**
Jason Monk386c94f2014-07-14 16:42:24 -04003823 * Return whether currently in lock task mode. When in this mode
3824 * no new tasks can be created or switched to.
3825 *
3826 * @see Activity#startLockTask()
Benjamin Franz43261142015-02-11 15:59:44 +00003827 *
3828 * @deprecated Use {@link #getLockTaskModeState} instead.
Craig Mautneraea74a52014-03-08 14:23:10 -08003829 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003830 @Deprecated
Craig Mautneraea74a52014-03-08 14:23:10 -08003831 public boolean isInLockTaskMode() {
Benjamin Franz43261142015-02-11 15:59:44 +00003832 return getLockTaskModeState() != LOCK_TASK_MODE_NONE;
3833 }
3834
3835 /**
3836 * Return the current state of task locking. The three possible outcomes
3837 * are {@link #LOCK_TASK_MODE_NONE}, {@link #LOCK_TASK_MODE_LOCKED}
3838 * and {@link #LOCK_TASK_MODE_PINNED}.
3839 *
3840 * @see Activity#startLockTask()
3841 */
3842 public int getLockTaskModeState() {
Craig Mautneraea74a52014-03-08 14:23:10 -08003843 try {
Benjamin Franz43261142015-02-11 15:59:44 +00003844 return ActivityManagerNative.getDefault().getLockTaskModeState();
Craig Mautneraea74a52014-03-08 14:23:10 -08003845 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003846 throw e.rethrowFromSystemServer();
Craig Mautneraea74a52014-03-08 14:23:10 -08003847 }
3848 }
Winson Chung1147c402014-05-14 11:05:00 -07003849
3850 /**
Srinath Sridharane535a582016-06-27 18:13:47 -07003851 * Enable more aggressive scheduling for latency-sensitive low-runtime VR threads. Only one
3852 * thread can be a VR thread in a process at a time, and that thread may be subject to
3853 * restrictions on the amount of time it can run.
3854 *
3855 * To reset the VR thread for an application, a tid of 0 can be passed.
3856 *
3857 * @see android.os.Process#myTid()
3858 * @param tid tid of the VR thread
3859 */
3860 public static void setVrThread(int tid) {
3861 try {
3862 ActivityManagerNative.getDefault().setVrThread(tid);
3863 } catch (RemoteException e) {
3864 // pass
3865 }
3866 }
3867
3868 /**
Winson Chung1147c402014-05-14 11:05:00 -07003869 * The AppTask allows you to manage your own application's tasks.
3870 * See {@link android.app.ActivityManager#getAppTasks()}
3871 */
3872 public static class AppTask {
3873 private IAppTask mAppTaskImpl;
3874
3875 /** @hide */
3876 public AppTask(IAppTask task) {
3877 mAppTaskImpl = task;
3878 }
3879
3880 /**
3881 * Finishes all activities in this task and removes it from the recent tasks list.
3882 */
3883 public void finishAndRemoveTask() {
3884 try {
3885 mAppTaskImpl.finishAndRemoveTask();
3886 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003887 throw e.rethrowFromSystemServer();
Winson Chung1147c402014-05-14 11:05:00 -07003888 }
3889 }
3890
3891 /**
3892 * Get the RecentTaskInfo associated with this task.
3893 *
3894 * @return The RecentTaskInfo for this task, or null if the task no longer exists.
3895 */
3896 public RecentTaskInfo getTaskInfo() {
3897 try {
3898 return mAppTaskImpl.getTaskInfo();
3899 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003900 throw e.rethrowFromSystemServer();
Winson Chung1147c402014-05-14 11:05:00 -07003901 }
3902 }
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07003903
3904 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003905 * Bring this task to the foreground. If it contains activities, they will be
3906 * brought to the foreground with it and their instances re-created if needed.
3907 * If it doesn't contain activities, the root activity of the task will be
3908 * re-launched.
3909 */
3910 public void moveToFront() {
3911 try {
3912 mAppTaskImpl.moveToFront();
3913 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003914 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003915 }
3916 }
3917
3918 /**
3919 * Start an activity in this task. Brings the task to the foreground. If this task
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003920 * is not currently active (that is, its id < 0), then a new activity for the given
3921 * Intent will be launched as the root of the task and the task brought to the
3922 * foreground. Otherwise, if this task is currently active and the Intent does not specify
3923 * an activity to launch in a new task, then a new activity for the given Intent will
3924 * be launched on top of the task and the task brought to the foreground. If this
3925 * task is currently active and the Intent specifies {@link Intent#FLAG_ACTIVITY_NEW_TASK}
3926 * or would otherwise be launched in to a new task, then the activity not launched but
3927 * this task be brought to the foreground and a new intent delivered to the top
3928 * activity if appropriate.
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003929 *
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003930 * <p>In other words, you generally want to use an Intent here that does not specify
3931 * {@link Intent#FLAG_ACTIVITY_NEW_TASK} or {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT},
3932 * and let the system do the right thing.</p>
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003933 *
3934 * @param intent The Intent describing the new activity to be launched on the task.
3935 * @param options Optional launch options.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003936 *
3937 * @see Activity#startActivity(android.content.Intent, android.os.Bundle)
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003938 */
3939 public void startActivity(Context context, Intent intent, Bundle options) {
3940 ActivityThread thread = ActivityThread.currentActivityThread();
3941 thread.getInstrumentation().execStartActivityFromAppTask(context,
3942 thread.getApplicationThread(), mAppTaskImpl, intent, options);
3943 }
3944
3945 /**
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07003946 * Modify the {@link Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag in the root
3947 * Intent of this AppTask.
3948 *
3949 * @param exclude If true, {@link Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} will
3950 * be set; otherwise, it will be cleared.
3951 */
3952 public void setExcludeFromRecents(boolean exclude) {
3953 try {
3954 mAppTaskImpl.setExcludeFromRecents(exclude);
3955 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003956 throw e.rethrowFromSystemServer();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07003957 }
3958 }
Winson Chung1147c402014-05-14 11:05:00 -07003959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960}