blob: b0ce7d1cc18c2ef0b6a2f520537850ea57725431 [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
Winson Chungb4132992018-07-03 15:52:38 -070019import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Yunfan Chen674f9692019-11-05 14:41:08 -080020import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Winson Chungb4132992018-07-03 15:52:38 -070021
Svet Ganov019d2302015-05-04 11:07:38 -070022import android.Manifest;
Matthew Ng54bc9422017-10-02 17:16:28 -070023import android.annotation.DrawableRes;
Michal Karpinski3da5c972015-12-11 18:16:30 +000024import android.annotation.IntDef;
Jing Ji8055a3a2019-12-17 15:55:33 -080025import android.annotation.IntRange;
Dianne Hackbornaec68bb2014-08-20 15:25:13 -070026import android.annotation.NonNull;
27import android.annotation.Nullable;
Svet Ganov019d2302015-05-04 11:07:38 -070028import android.annotation.RequiresPermission;
Amith Yamasani0e8d7d62014-09-03 13:17:28 -070029import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060030import android.annotation.SystemService;
Dianne Hackborn058f1e42016-11-02 17:18:35 -070031import android.annotation.TestApi;
Artur Satayevc895b1b2019-12-10 17:47:51 +000032import android.compat.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.ComponentName;
34import android.content.Context;
35import android.content.Intent;
Yunfan Chen674f9692019-11-05 14:41:08 -080036import android.content.pm.ActivityInfo;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -070037import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.pm.ConfigurationInfo;
39import android.content.pm.IPackageDataObserver;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070040import android.content.pm.PackageManager;
Felipe Lemef3fa0f82016-01-07 12:08:19 -080041import android.content.pm.ParceledListSlice;
Dianne Hackborn41203752012-08-31 14:05:51 -070042import android.content.pm.UserInfo;
Wale Ogunwale68278562017-09-23 17:13:55 -070043import android.content.res.Configuration;
Kenny Root5ef44b72011-01-26 17:22:20 -080044import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.graphics.Bitmap;
Wale Ogunwalec981ad52017-06-13 11:40:06 -070046import android.graphics.Canvas;
Winson Chunga449dc02014-05-16 11:15:04 -070047import android.graphics.Color;
Peiyong Lin9d427402019-01-23 18:39:06 -080048import android.graphics.ColorSpace;
Wale Ogunwalec981ad52017-06-13 11:40:06 -070049import android.graphics.GraphicBuffer;
50import android.graphics.Matrix;
51import android.graphics.Point;
Craig Mautner967212c2013-04-13 21:10:58 -070052import android.graphics.Rect;
Winson Chungd6722032020-02-18 15:16:08 -080053import android.graphics.drawable.Icon;
Wale Ogunwalec981ad52017-06-13 11:40:06 -070054import android.os.BatteryStats;
Dianne Hackbornc81983a2017-10-20 16:16:32 -070055import android.os.Binder;
Wale Ogunwalec981ad52017-06-13 11:40:06 -070056import android.os.Build;
57import android.os.Build.VERSION_CODES;
Dianne Hackborn8078d8c2012-03-20 11:11:26 -070058import android.os.Bundle;
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -070059import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Handler;
Wale Ogunwalec981ad52017-06-13 11:40:06 -070061import android.os.IBinder;
Maurice Lam522ecbe2018-12-04 20:21:40 -080062import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Parcel;
64import android.os.Parcelable;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070065import android.os.Process;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070066import android.os.RemoteException;
Peter Visontay8d224ca2011-02-18 16:39:19 +000067import android.os.ServiceManager;
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -070068import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070069import android.os.UserHandle;
Grace Jia15b4c262020-01-07 14:25:24 -080070import android.os.UserManager;
Narayan Kamath695cf722017-12-21 18:32:47 +000071import android.os.WorkSource;
Winson Chung1df39e22020-04-09 14:30:55 -070072import android.text.TextUtils;
Dianne Hackborn058f1e42016-11-02 17:18:35 -070073import android.util.ArrayMap;
Kenny Root5ef44b72011-01-26 17:22:20 -080074import android.util.DisplayMetrics;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070075import android.util.Singleton;
Winson Chung48a10a52014-08-27 14:36:51 -070076import android.util.Size;
Vinit Nayakffd9dff2019-11-05 15:20:11 -080077import android.view.Surface;
Jing Jie2757e62020-04-02 14:13:42 -070078import android.window.WindowContainerToken;
Felipe Lemef3fa0f82016-01-07 12:08:19 -080079
Maurice Lam522ecbe2018-12-04 20:21:40 -080080import com.android.internal.app.LocalePicker;
Wale Ogunwalec981ad52017-06-13 11:40:06 -070081import com.android.internal.app.procstats.ProcessStats;
82import com.android.internal.os.RoSystemProperties;
83import com.android.internal.os.TransferPipe;
84import com.android.internal.util.FastPrintWriter;
Dianne Hackborn337e01a2018-02-27 17:16:37 -080085import com.android.internal.util.MemInfoReader;
Malcolm Chenc99b83b2020-01-10 15:55:30 -080086import com.android.internal.util.Preconditions;
Wale Ogunwalec981ad52017-06-13 11:40:06 -070087import com.android.server.LocalServices;
88
Winson Chungd6722032020-02-18 15:16:08 -080089import org.xmlpull.v1.XmlPullParser;
Craig Mautner648f69b2014-09-18 14:16:26 -070090import org.xmlpull.v1.XmlSerializer;
Kenny Root5ef44b72011-01-26 17:22:20 -080091
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070092import java.io.FileDescriptor;
93import java.io.FileOutputStream;
Craig Mautner648f69b2014-09-18 14:16:26 -070094import java.io.IOException;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070095import java.io.PrintWriter;
Michal Karpinski3da5c972015-12-11 18:16:30 +000096import java.lang.annotation.Retention;
97import java.lang.annotation.RetentionPolicy;
Winson Chung1147c402014-05-14 11:05:00 -070098import java.util.ArrayList;
Maurice Lam522ecbe2018-12-04 20:21:40 -080099import java.util.Collection;
Jing Jia590e8e2020-02-05 20:25:06 -0800100import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.util.List;
Maurice Lam522ecbe2018-12-04 20:21:40 -0800102import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104/**
Dave Friedman2a3ebad2017-01-04 18:27:26 -0800105 * <p>
106 * This class gives information about, and interacts
107 * with, activities, services, and the containing
108 * process.
109 * </p>
110 *
111 * <p>
112 * A number of the methods in this class are for
113 * debugging or informational purposes and they should
114 * not be used to affect any runtime behavior of
115 * your app. These methods are called out as such in
116 * the method level documentation.
117 * </p>
118 *
119 *<p>
120 * Most application developers should not have the need to
121 * use this class, most of whose methods are for specialized
122 * use cases. However, a few methods are more broadly applicable.
123 * For instance, {@link android.app.ActivityManager#isLowRamDevice() isLowRamDevice()}
124 * enables your app to detect whether it is running on a low-memory device,
125 * and behave accordingly.
126 * {@link android.app.ActivityManager#clearApplicationUserData() clearApplicationUserData()}
127 * is for apps with reset-data functionality.
128 * </p>
129 *
130 * <p>
131 * In some special use cases, where an app interacts with
132 * its Task stack, the app may use the
133 * {@link android.app.ActivityManager.AppTask} and
134 * {@link android.app.ActivityManager.RecentTaskInfo} inner
135 * classes. However, in general, the methods in this class should
136 * be used for testing and debugging purposes only.
137 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600139@SystemService(Context.ACTIVITY_SERVICE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140public class ActivityManager {
141 private static String TAG = "ActivityManager";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100143 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 private final Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800146 private static volatile boolean sSystemReady = false;
147
Bryce Lee7f936862017-05-09 15:33:18 -0700148
149 private static final int FIRST_START_FATAL_ERROR_CODE = -100;
150 private static final int LAST_START_FATAL_ERROR_CODE = -1;
151 private static final int FIRST_START_SUCCESS_CODE = 0;
152 private static final int LAST_START_SUCCESS_CODE = 99;
153 private static final int FIRST_START_NON_FATAL_ERROR_CODE = 100;
154 private static final int LAST_START_NON_FATAL_ERROR_CODE = 199;
155
Sudheer Shanka8f99bff2018-10-21 16:19:53 -0700156 /**
157 * Disable hidden API checks for the newly started instrumentation.
158 * @hide
159 */
160 public static final int INSTR_FLAG_DISABLE_HIDDEN_API_CHECKS = 1 << 0;
161 /**
162 * Mount full external storage for the newly started instrumentation.
163 * @hide
164 */
165 public static final int INSTR_FLAG_MOUNT_EXTERNAL_STORAGE_FULL = 1 << 1;
166
Artur Satayevab4d5af2019-10-31 12:07:55 +0000167 /**
168 * Disable test API access for the newly started instrumentation.
169 * @hide
170 */
171 public static final int INSTR_FLAG_DISABLE_TEST_API_CHECKS = 1 << 2;
172
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700173 static final class UidObserver extends IUidObserver.Stub {
174 final OnUidImportanceListener mListener;
Makoto Onukid7e40582017-04-13 14:54:56 -0700175 final Context mContext;
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700176
Makoto Onukid7e40582017-04-13 14:54:56 -0700177 UidObserver(OnUidImportanceListener listener, Context clientContext) {
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700178 mListener = listener;
Makoto Onukid7e40582017-04-13 14:54:56 -0700179 mContext = clientContext;
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700180 }
181
182 @Override
Hui Yu26969322019-08-21 14:56:35 -0700183 public void onUidStateChanged(int uid, int procState, long procStateSeq, int capability) {
Makoto Onukid7e40582017-04-13 14:54:56 -0700184 mListener.onUidImportance(uid, RunningAppProcessInfo.procStateToImportanceForClient(
185 procState, mContext));
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700186 }
187
188 @Override
Dianne Hackborne07641d2016-11-09 15:07:23 -0800189 public void onUidGone(int uid, boolean disabled) {
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700190 mListener.onUidImportance(uid, RunningAppProcessInfo.IMPORTANCE_GONE);
191 }
192
193 @Override
194 public void onUidActive(int uid) {
195 }
196
197 @Override
Dianne Hackborne07641d2016-11-09 15:07:23 -0800198 public void onUidIdle(int uid, boolean disabled) {
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700199 }
Dianne Hackborn3e99f652017-07-05 16:33:56 -0700200
201 @Override public void onUidCachedChanged(int uid, boolean cached) {
202 }
Dianne Hackborn058f1e42016-11-02 17:18:35 -0700203 }
204
205 final ArrayMap<OnUidImportanceListener, UidObserver> mImportanceListeners = new ArrayMap<>();
206
Felipe Lemedc7af962016-01-22 18:27:03 -0800207 /**
Scott Maincc2195b2013-10-16 13:57:46 -0700208 * <a href="{@docRoot}guide/topics/manifest/meta-data-element.html">{@code
Neil Fuller71fbb812015-11-30 09:51:33 +0000209 * <meta-data>}</a> name for a 'home' Activity that declares a package that is to be
Christopher Tatebd413f62013-09-18 18:31:59 -0700210 * uninstalled in lieu of the declaring one. The package named here must be
Neil Fuller71fbb812015-11-30 09:51:33 +0000211 * signed with the same certificate as the one declaring the {@code <meta-data>}.
Christopher Tatebd413f62013-09-18 18:31:59 -0700212 */
213 public static final String META_HOME_ALTERNATE = "android.app.home.alternate";
214
Bryce Lee7f936862017-05-09 15:33:18 -0700215 // NOTE: Before adding a new start result, please reference the defined ranges to ensure the
216 // result is properly categorized.
217
Christopher Tatebd413f62013-09-18 18:31:59 -0700218 /**
Amith Yamasani42449782016-04-19 11:45:51 -0700219 * Result for IActivityManager.startVoiceActivity: active session is currently hidden.
220 * @hide
221 */
Bryce Lee7f936862017-05-09 15:33:18 -0700222 public static final int START_VOICE_HIDDEN_SESSION = FIRST_START_FATAL_ERROR_CODE;
Amith Yamasani42449782016-04-19 11:45:51 -0700223
224 /**
225 * Result for IActivityManager.startVoiceActivity: active session does not match
226 * the requesting token.
227 * @hide
228 */
Bryce Lee7f936862017-05-09 15:33:18 -0700229 public static final int START_VOICE_NOT_ACTIVE_SESSION = FIRST_START_FATAL_ERROR_CODE + 1;
Amith Yamasani42449782016-04-19 11:45:51 -0700230
231 /**
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700232 * Result for IActivityManager.startActivity: trying to start a background user
233 * activity that shouldn't be displayed for all users.
234 * @hide
235 */
Bryce Lee7f936862017-05-09 15:33:18 -0700236 public static final int START_NOT_CURRENT_USER_ACTIVITY = FIRST_START_FATAL_ERROR_CODE + 2;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700237
238 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700239 * Result for IActivityManager.startActivity: trying to start an activity under voice
240 * control when that activity does not support the VOICE category.
241 * @hide
242 */
Bryce Lee7f936862017-05-09 15:33:18 -0700243 public static final int START_NOT_VOICE_COMPATIBLE = FIRST_START_FATAL_ERROR_CODE + 3;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700244
245 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700246 * Result for IActivityManager.startActivity: an error where the
247 * start had to be canceled.
248 * @hide
249 */
Bryce Lee7f936862017-05-09 15:33:18 -0700250 public static final int START_CANCELED = FIRST_START_FATAL_ERROR_CODE + 4;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700251
252 /**
253 * Result for IActivityManager.startActivity: an error where the
254 * thing being started is not an activity.
255 * @hide
256 */
Bryce Lee7f936862017-05-09 15:33:18 -0700257 public static final int START_NOT_ACTIVITY = FIRST_START_FATAL_ERROR_CODE + 5;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700258
259 /**
260 * Result for IActivityManager.startActivity: an error where the
261 * caller does not have permission to start the activity.
262 * @hide
263 */
Bryce Lee7f936862017-05-09 15:33:18 -0700264 public static final int START_PERMISSION_DENIED = FIRST_START_FATAL_ERROR_CODE + 6;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700265
266 /**
267 * Result for IActivityManager.startActivity: an error where the
268 * caller has requested both to forward a result and to receive
269 * a result.
270 * @hide
271 */
Bryce Lee7f936862017-05-09 15:33:18 -0700272 public static final int START_FORWARD_AND_REQUEST_CONFLICT = FIRST_START_FATAL_ERROR_CODE + 7;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700273
274 /**
275 * Result for IActivityManager.startActivity: an error where the
276 * requested class is not found.
277 * @hide
278 */
Bryce Lee7f936862017-05-09 15:33:18 -0700279 public static final int START_CLASS_NOT_FOUND = FIRST_START_FATAL_ERROR_CODE + 8;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700280
281 /**
282 * Result for IActivityManager.startActivity: an error where the
283 * given Intent could not be resolved to an activity.
284 * @hide
285 */
Bryce Lee7f936862017-05-09 15:33:18 -0700286 public static final int START_INTENT_NOT_RESOLVED = FIRST_START_FATAL_ERROR_CODE + 9;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700287
288 /**
Winson Chungfc3ec4c2017-06-01 15:35:48 -0700289 * Result for IActivityManager.startAssistantActivity: active session is currently hidden.
290 * @hide
291 */
292 public static final int START_ASSISTANT_HIDDEN_SESSION = FIRST_START_FATAL_ERROR_CODE + 10;
293
294 /**
295 * Result for IActivityManager.startAssistantActivity: active session does not match
296 * the requesting token.
297 * @hide
298 */
299 public static final int START_ASSISTANT_NOT_ACTIVE_SESSION = FIRST_START_FATAL_ERROR_CODE + 11;
300
301 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700302 * Result for IActivityManaqer.startActivity: the activity was started
303 * successfully as normal.
304 * @hide
305 */
Bryce Lee7f936862017-05-09 15:33:18 -0700306 public static final int START_SUCCESS = FIRST_START_SUCCESS_CODE;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700307
308 /**
309 * Result for IActivityManaqer.startActivity: the caller asked that the Intent not
310 * be executed if it is the recipient, and that is indeed the case.
311 * @hide
312 */
Bryce Lee7f936862017-05-09 15:33:18 -0700313 public static final int START_RETURN_INTENT_TO_CALLER = FIRST_START_SUCCESS_CODE + 1;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700314
315 /**
316 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
317 * a task was simply brought to the foreground.
318 * @hide
319 */
Bryce Lee7f936862017-05-09 15:33:18 -0700320 public static final int START_TASK_TO_FRONT = FIRST_START_SUCCESS_CODE + 2;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700321
322 /**
323 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
324 * the given Intent was given to the existing top activity.
325 * @hide
326 */
Bryce Lee7f936862017-05-09 15:33:18 -0700327 public static final int START_DELIVERED_TO_TOP = FIRST_START_SUCCESS_CODE + 3;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700328
329 /**
330 * Result for IActivityManaqer.startActivity: request was canceled because
331 * app switches are temporarily canceled to ensure the user's last request
332 * (such as pressing home) is performed.
333 * @hide
334 */
Bryce Lee7f936862017-05-09 15:33:18 -0700335 public static final int START_SWITCHES_CANCELED = FIRST_START_NON_FATAL_ERROR_CODE;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700336
337 /**
Craig Mautneraea74a52014-03-08 14:23:10 -0800338 * Result for IActivityManaqer.startActivity: a new activity was attempted to be started
339 * while in Lock Task Mode.
340 * @hide
341 */
Bryce Lee7f936862017-05-09 15:33:18 -0700342 public static final int START_RETURN_LOCK_TASK_MODE_VIOLATION =
343 FIRST_START_NON_FATAL_ERROR_CODE + 1;
Craig Mautneraea74a52014-03-08 14:23:10 -0800344
345 /**
Bryce Leef9d49542017-06-26 16:27:32 -0700346 * Result for IActivityManaqer.startActivity: a new activity start was aborted. Never returned
347 * externally.
348 * @hide
349 */
350 public static final int START_ABORTED = FIRST_START_NON_FATAL_ERROR_CODE + 2;
351
352 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700353 * Flag for IActivityManaqer.startActivity: do special start mode where
354 * a new activity is launched only if it is needed.
355 * @hide
356 */
357 public static final int START_FLAG_ONLY_IF_NEEDED = 1<<0;
358
359 /**
360 * Flag for IActivityManaqer.startActivity: launch the app for
361 * debugging.
362 * @hide
363 */
364 public static final int START_FLAG_DEBUG = 1<<1;
365
366 /**
367 * Flag for IActivityManaqer.startActivity: launch the app for
Man Caocfa78b22015-06-11 20:14:34 -0700368 * allocation tracking.
369 * @hide
370 */
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700371 public static final int START_FLAG_TRACK_ALLOCATION = 1<<2;
Man Caocfa78b22015-06-11 20:14:34 -0700372
373 /**
Tamas Berghammerdf6cb282016-01-29 12:07:00 +0000374 * Flag for IActivityManaqer.startActivity: launch the app with
375 * native debugging support.
376 * @hide
377 */
378 public static final int START_FLAG_NATIVE_DEBUGGING = 1<<3;
379
380 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700381 * Result for IActivityManaqer.broadcastIntent: success!
382 * @hide
383 */
384 public static final int BROADCAST_SUCCESS = 0;
385
386 /**
387 * Result for IActivityManaqer.broadcastIntent: attempt to broadcast
388 * a sticky intent without appropriate permission.
389 * @hide
390 */
391 public static final int BROADCAST_STICKY_CANT_HAVE_PERMISSION = -1;
392
393 /**
Amith Yamasani83b6ef02014-11-07 15:34:04 -0800394 * Result for IActivityManager.broadcastIntent: trying to send a broadcast
395 * to a stopped user. Fail.
396 * @hide
397 */
398 public static final int BROADCAST_FAILED_USER_STOPPED = -2;
399
400 /**
Dianne Hackborna4972e92012-03-14 10:38:05 -0700401 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
402 * for a sendBroadcast operation.
403 * @hide
404 */
405 public static final int INTENT_SENDER_BROADCAST = 1;
406
407 /**
408 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
409 * for a startActivity operation.
410 * @hide
411 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100412 @UnsupportedAppUsage
Dianne Hackborna4972e92012-03-14 10:38:05 -0700413 public static final int INTENT_SENDER_ACTIVITY = 2;
414
415 /**
416 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
417 * for an activity result operation.
418 * @hide
419 */
420 public static final int INTENT_SENDER_ACTIVITY_RESULT = 3;
421
422 /**
423 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
424 * for a startService operation.
425 * @hide
426 */
427 public static final int INTENT_SENDER_SERVICE = 4;
428
Christopher Tate08992ac2017-03-21 11:37:06 -0700429 /**
430 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
431 * for a startForegroundService operation.
432 * @hide
433 */
434 public static final int INTENT_SENDER_FOREGROUND_SERVICE = 5;
435
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700436 /** @hide User operation call: success! */
437 public static final int USER_OP_SUCCESS = 0;
438
439 /** @hide User operation call: given user id is not known. */
440 public static final int USER_OP_UNKNOWN_USER = -1;
441
442 /** @hide User operation call: given user id is the current user, can't be stopped. */
443 public static final int USER_OP_IS_CURRENT = -2;
444
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700445 /** @hide User operation call: system user can't be stopped. */
446 public static final int USER_OP_ERROR_IS_SYSTEM = -3;
447
448 /** @hide User operation call: one of related users cannot be stopped. */
449 public static final int USER_OP_ERROR_RELATED_USERS_CANNOT_STOP = -4;
450
Dianne Hackbornf097d422017-12-15 16:32:19 -0800451 /**
452 * Process states, describing the kind of state a particular process is in.
453 * When updating these, make sure to also check all related references to the
454 * constant in code, and update these arrays:
455 *
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800456 * @see com.android.internal.app.procstats.ProcessState#PROCESS_STATE_TO_STATE
457 * @see com.android.server.am.ProcessList#sProcStateToProcMem
458 * @see com.android.server.am.ProcessList#sFirstAwakePssTimes
459 * @see com.android.server.am.ProcessList#sSameAwakePssTimes
460 * @see com.android.server.am.ProcessList#sTestFirstPssTimes
461 * @see com.android.server.am.ProcessList#sTestSamePssTimes
Hui Yu26969322019-08-21 14:56:35 -0700462 * @hide
Dianne Hackbornf097d422017-12-15 16:32:19 -0800463 */
Hui Yu26969322019-08-21 14:56:35 -0700464 @IntDef(flag = false, prefix = { "PROCESS_STATE_" }, value = {
465 PROCESS_STATE_UNKNOWN, // -1
466 PROCESS_STATE_PERSISTENT, // 0
467 PROCESS_STATE_PERSISTENT_UI,
468 PROCESS_STATE_TOP,
469 PROCESS_STATE_BOUND_TOP,
470 PROCESS_STATE_FOREGROUND_SERVICE,
471 PROCESS_STATE_BOUND_FOREGROUND_SERVICE,
472 PROCESS_STATE_IMPORTANT_FOREGROUND,
473 PROCESS_STATE_IMPORTANT_BACKGROUND,
474 PROCESS_STATE_TRANSIENT_BACKGROUND,
475 PROCESS_STATE_BACKUP,
476 PROCESS_STATE_SERVICE,
477 PROCESS_STATE_RECEIVER,
478 PROCESS_STATE_TOP_SLEEPING,
479 PROCESS_STATE_HEAVY_WEIGHT,
480 PROCESS_STATE_HOME,
481 PROCESS_STATE_LAST_ACTIVITY,
482 PROCESS_STATE_CACHED_ACTIVITY,
483 PROCESS_STATE_CACHED_ACTIVITY_CLIENT,
484 PROCESS_STATE_CACHED_RECENT,
485 PROCESS_STATE_CACHED_EMPTY,
486 })
487 @Retention(RetentionPolicy.SOURCE)
488 public @interface ProcessState {}
489
Dianne Hackbornf097d422017-12-15 16:32:19 -0800490
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800491 /** @hide Not a real process state. */
492 public static final int PROCESS_STATE_UNKNOWN = -1;
Ruben Brunka27eef42015-01-28 15:04:16 -0800493
Dianne Hackborna413dc02013-07-12 12:02:55 -0700494 /** @hide Process is a persistent system process. */
495 public static final int PROCESS_STATE_PERSISTENT = 0;
496
497 /** @hide Process is a persistent system process and is doing UI. */
498 public static final int PROCESS_STATE_PERSISTENT_UI = 1;
499
Dianne Hackbornc8230512013-07-13 21:32:12 -0700500 /** @hide Process is hosting the current top activities. Note that this covers
501 * all activities that are visible to the user. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100502 @UnsupportedAppUsage
Dianne Hackborna413dc02013-07-12 12:02:55 -0700503 public static final int PROCESS_STATE_TOP = 2;
504
Amith Yamasanif235d0b2019-03-20 22:49:43 -0700505 /** @hide Process is bound to a TOP app. This is ranked below SERVICE_LOCATION so that
506 * it doesn't get the capability of location access while-in-use. */
Hui Yu26969322019-08-21 14:56:35 -0700507 public static final int PROCESS_STATE_BOUND_TOP = 3;
Amith Yamasanif235d0b2019-03-20 22:49:43 -0700508
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700509 /** @hide Process is hosting a foreground service. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100510 @UnsupportedAppUsage
Hui Yu26969322019-08-21 14:56:35 -0700511 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 4;
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -0800512
513 /** @hide Process is hosting a foreground service due to a system binding. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100514 @UnsupportedAppUsage
Hui Yu26969322019-08-21 14:56:35 -0700515 public static final int PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 5;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700516
Dianne Hackborna413dc02013-07-12 12:02:55 -0700517 /** @hide Process is important to the user, and something they are aware of. */
Hui Yu26969322019-08-21 14:56:35 -0700518 public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 6;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700519
520 /** @hide Process is important to the user, but not something they are aware of. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100521 @UnsupportedAppUsage
Hui Yu26969322019-08-21 14:56:35 -0700522 public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 7;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700523
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700524 /** @hide Process is in the background transient so we will try to keep running. */
Hui Yu26969322019-08-21 14:56:35 -0700525 public static final int PROCESS_STATE_TRANSIENT_BACKGROUND = 8;
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700526
Dianne Hackborna413dc02013-07-12 12:02:55 -0700527 /** @hide Process is in the background running a backup/restore operation. */
Hui Yu26969322019-08-21 14:56:35 -0700528 public static final int PROCESS_STATE_BACKUP = 9;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700529
Dianne Hackbornc8230512013-07-13 21:32:12 -0700530 /** @hide Process is in the background running a service. Unlike oom_adj, this level
531 * is used for both the normal running in background state and the executing
532 * operations state. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100533 @UnsupportedAppUsage
Hui Yu26969322019-08-21 14:56:35 -0700534 public static final int PROCESS_STATE_SERVICE = 10;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700535
Dianne Hackbornc8230512013-07-13 21:32:12 -0700536 /** @hide Process is in the background running a receiver. Note that from the
Kweku Adamse6b00c22017-10-23 16:46:45 -0700537 * perspective of oom_adj, receivers run at a higher foreground level, but for our
Dianne Hackbornc8230512013-07-13 21:32:12 -0700538 * prioritization here that is not necessary and putting them below services means
539 * many fewer changes in some process states as they receive broadcasts. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100540 @UnsupportedAppUsage
Hui Yu26969322019-08-21 14:56:35 -0700541 public static final int PROCESS_STATE_RECEIVER = 11;
Dianne Hackbornbad8d912017-12-18 16:45:52 -0800542
543 /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
Hui Yu26969322019-08-21 14:56:35 -0700544 public static final int PROCESS_STATE_TOP_SLEEPING = 12;
Dianne Hackbornf097d422017-12-15 16:32:19 -0800545
546 /** @hide Process is in the background, but it can't restore its state so we want
547 * to try to avoid killing it. */
Hui Yu26969322019-08-21 14:56:35 -0700548 public static final int PROCESS_STATE_HEAVY_WEIGHT = 13;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700549
Dianne Hackborna413dc02013-07-12 12:02:55 -0700550 /** @hide Process is in the background but hosts the home activity. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100551 @UnsupportedAppUsage
Hui Yu26969322019-08-21 14:56:35 -0700552 public static final int PROCESS_STATE_HOME = 14;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700553
554 /** @hide Process is in the background but hosts the last shown activity. */
Hui Yu26969322019-08-21 14:56:35 -0700555 public static final int PROCESS_STATE_LAST_ACTIVITY = 15;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700556
Dianne Hackbornc8230512013-07-13 21:32:12 -0700557 /** @hide Process is being cached for later use and contains activities. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100558 @UnsupportedAppUsage
Hui Yu26969322019-08-21 14:56:35 -0700559 public static final int PROCESS_STATE_CACHED_ACTIVITY = 16;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700560
561 /** @hide Process is being cached for later use and is a client of another cached
562 * process that contains activities. */
Hui Yu26969322019-08-21 14:56:35 -0700563 public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 17;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700564
Dianne Hackborn68a06332017-11-15 17:54:18 -0800565 /** @hide Process is being cached for later use and has an activity that corresponds
566 * to an existing recent task. */
Hui Yu26969322019-08-21 14:56:35 -0700567 public static final int PROCESS_STATE_CACHED_RECENT = 18;
Dianne Hackborn68a06332017-11-15 17:54:18 -0800568
Dianne Hackbornc8230512013-07-13 21:32:12 -0700569 /** @hide Process is being cached for later use and is empty. */
Hui Yu26969322019-08-21 14:56:35 -0700570 public static final int PROCESS_STATE_CACHED_EMPTY = 19;
Dianne Hackborna413dc02013-07-12 12:02:55 -0700571
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800572 /** @hide Process does not exist. */
Hui Yu26969322019-08-21 14:56:35 -0700573 public static final int PROCESS_STATE_NONEXISTENT = 20;
574
575 /**
576 * The set of flags for process capability.
577 * @hide
578 */
579 @IntDef(flag = true, prefix = { "PROCESS_CAPABILITY_" }, value = {
580 PROCESS_CAPABILITY_NONE,
581 PROCESS_CAPABILITY_FOREGROUND_LOCATION,
Hui Yu85679b42020-01-15 15:52:08 -0800582 PROCESS_CAPABILITY_FOREGROUND_CAMERA,
583 PROCESS_CAPABILITY_FOREGROUND_MICROPHONE,
Hui Yu26969322019-08-21 14:56:35 -0700584 })
585 @Retention(RetentionPolicy.SOURCE)
586 public @interface ProcessCapability {}
587
588 /** @hide Process does not have any capability */
589 @TestApi
590 public static final int PROCESS_CAPABILITY_NONE = 0;
591
592 /** @hide Process can access location while in foreground */
593 @TestApi
594 public static final int PROCESS_CAPABILITY_FOREGROUND_LOCATION = 1 << 0;
595
Hui Yu85679b42020-01-15 15:52:08 -0800596 /** @hide Process can access camera while in foreground */
597 @TestApi
598 public static final int PROCESS_CAPABILITY_FOREGROUND_CAMERA = 1 << 1;
599
600 /** @hide Process can access microphone while in foreground */
601 @TestApi
602 public static final int PROCESS_CAPABILITY_FOREGROUND_MICROPHONE = 1 << 2;
603
Hui Yu26969322019-08-21 14:56:35 -0700604 /** @hide all capabilities, the ORing of all flags in {@link ProcessCapability}*/
605 @TestApi
Hui Yu85679b42020-01-15 15:52:08 -0800606 public static final int PROCESS_CAPABILITY_ALL = PROCESS_CAPABILITY_FOREGROUND_LOCATION
607 | PROCESS_CAPABILITY_FOREGROUND_CAMERA
608 | PROCESS_CAPABILITY_FOREGROUND_MICROPHONE;
Hui Yu88910de2019-12-16 14:35:27 -0800609 /**
610 * All explicit capabilities. These are capabilities that need to be specified from manifest
611 * file.
612 * @hide
613 */
614 @TestApi
615 public static final int PROCESS_CAPABILITY_ALL_EXPLICIT =
616 PROCESS_CAPABILITY_FOREGROUND_LOCATION;
617
618 /**
619 * All implicit capabilities. There are capabilities that process automatically have.
620 * @hide
621 */
622 @TestApi
623 public static final int PROCESS_CAPABILITY_ALL_IMPLICIT = PROCESS_CAPABILITY_FOREGROUND_CAMERA
624 | PROCESS_CAPABILITY_FOREGROUND_MICROPHONE;
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800625
Bookatzdb026a22018-01-10 19:01:56 -0800626 // NOTE: If PROCESS_STATEs are added, then new fields must be added
627 // to frameworks/base/core/proto/android/app/enums.proto and the following method must
Kweku Adamse6b00c22017-10-23 16:46:45 -0700628 // be updated to correctly map between them.
Bookatzdb026a22018-01-10 19:01:56 -0800629 // However, if the current ActivityManager values are merely modified, no update should be made
630 // to enums.proto, to which values can only be added but never modified. Note that the proto
631 // versions do NOT have the ordering restrictions of the ActivityManager process state.
Kweku Adamse6b00c22017-10-23 16:46:45 -0700632 /**
Bookatzdb026a22018-01-10 19:01:56 -0800633 * Maps ActivityManager.PROCESS_STATE_ values to enums.proto ProcessStateEnum value.
Kweku Adamse6b00c22017-10-23 16:46:45 -0700634 *
635 * @param amInt a process state of the form ActivityManager.PROCESS_STATE_
Bookatzdb026a22018-01-10 19:01:56 -0800636 * @return the value of the corresponding enums.proto ProcessStateEnum value.
Kweku Adamse6b00c22017-10-23 16:46:45 -0700637 * @hide
638 */
639 public static final int processStateAmToProto(int amInt) {
Bookatzdb026a22018-01-10 19:01:56 -0800640 switch (amInt) {
641 case PROCESS_STATE_UNKNOWN:
642 return AppProtoEnums.PROCESS_STATE_UNKNOWN;
643 case PROCESS_STATE_PERSISTENT:
644 return AppProtoEnums.PROCESS_STATE_PERSISTENT;
645 case PROCESS_STATE_PERSISTENT_UI:
646 return AppProtoEnums.PROCESS_STATE_PERSISTENT_UI;
647 case PROCESS_STATE_TOP:
648 return AppProtoEnums.PROCESS_STATE_TOP;
Amith Yamasanif235d0b2019-03-20 22:49:43 -0700649 case PROCESS_STATE_BOUND_TOP:
650 return AppProtoEnums.PROCESS_STATE_BOUND_TOP;
Bookatzdb026a22018-01-10 19:01:56 -0800651 case PROCESS_STATE_FOREGROUND_SERVICE:
652 return AppProtoEnums.PROCESS_STATE_FOREGROUND_SERVICE;
653 case PROCESS_STATE_BOUND_FOREGROUND_SERVICE:
654 return AppProtoEnums.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
655 case PROCESS_STATE_IMPORTANT_FOREGROUND:
656 return AppProtoEnums.PROCESS_STATE_IMPORTANT_FOREGROUND;
657 case PROCESS_STATE_IMPORTANT_BACKGROUND:
658 return AppProtoEnums.PROCESS_STATE_IMPORTANT_BACKGROUND;
659 case PROCESS_STATE_TRANSIENT_BACKGROUND:
660 return AppProtoEnums.PROCESS_STATE_TRANSIENT_BACKGROUND;
661 case PROCESS_STATE_BACKUP:
662 return AppProtoEnums.PROCESS_STATE_BACKUP;
663 case PROCESS_STATE_SERVICE:
664 return AppProtoEnums.PROCESS_STATE_SERVICE;
665 case PROCESS_STATE_RECEIVER:
666 return AppProtoEnums.PROCESS_STATE_RECEIVER;
667 case PROCESS_STATE_TOP_SLEEPING:
668 return AppProtoEnums.PROCESS_STATE_TOP_SLEEPING;
669 case PROCESS_STATE_HEAVY_WEIGHT:
670 return AppProtoEnums.PROCESS_STATE_HEAVY_WEIGHT;
671 case PROCESS_STATE_HOME:
672 return AppProtoEnums.PROCESS_STATE_HOME;
673 case PROCESS_STATE_LAST_ACTIVITY:
674 return AppProtoEnums.PROCESS_STATE_LAST_ACTIVITY;
675 case PROCESS_STATE_CACHED_ACTIVITY:
676 return AppProtoEnums.PROCESS_STATE_CACHED_ACTIVITY;
677 case PROCESS_STATE_CACHED_ACTIVITY_CLIENT:
678 return AppProtoEnums.PROCESS_STATE_CACHED_ACTIVITY_CLIENT;
679 case PROCESS_STATE_CACHED_RECENT:
680 return AppProtoEnums.PROCESS_STATE_CACHED_RECENT;
681 case PROCESS_STATE_CACHED_EMPTY:
682 return AppProtoEnums.PROCESS_STATE_CACHED_EMPTY;
683 case PROCESS_STATE_NONEXISTENT:
684 return AppProtoEnums.PROCESS_STATE_NONEXISTENT;
685 default:
686 // ActivityManager process state (amInt)
687 // could not be mapped to an AppProtoEnums ProcessState state.
688 return AppProtoEnums.PROCESS_STATE_UNKNOWN_TO_PROTO;
689 }
Kweku Adamse6b00c22017-10-23 16:46:45 -0700690 }
691
Joe Onorato05c9ecc2016-03-30 15:13:46 -0700692 /** @hide The lowest process state number */
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800693 public static final int MIN_PROCESS_STATE = PROCESS_STATE_PERSISTENT;
Joe Onorato05c9ecc2016-03-30 15:13:46 -0700694
695 /** @hide The highest process state number */
Dianne Hackborn5614bf52016-11-07 17:26:41 -0800696 public static final int MAX_PROCESS_STATE = PROCESS_STATE_NONEXISTENT;
Joe Onorato05c9ecc2016-03-30 15:13:46 -0700697
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700698 /** @hide Should this process state be considered a background state? */
699 public static final boolean isProcStateBackground(int procState) {
Hui Yufa7d46b2020-02-27 20:16:19 +0000700 return procState >= PROCESS_STATE_TRANSIENT_BACKGROUND;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700701 }
702
Amith Yamasania0a30a12019-01-22 11:38:06 -0800703 /** @hide Is this a foreground service type? */
704 public static boolean isForegroundService(int procState) {
Hui Yu26969322019-08-21 14:56:35 -0700705 return procState == PROCESS_STATE_FOREGROUND_SERVICE;
Amith Yamasania0a30a12019-01-22 11:38:06 -0800706 }
707
Dianne Hackborna83ce1d2015-03-11 15:16:13 -0700708 /** @hide requestType for assist context: only basic information. */
709 public static final int ASSIST_CONTEXT_BASIC = 0;
710
711 /** @hide requestType for assist context: generate full AssistStructure. */
712 public static final int ASSIST_CONTEXT_FULL = 1;
713
Felipe Leme640f30a2017-03-06 15:44:06 -0800714 /** @hide requestType for assist context: generate full AssistStructure for autofill. */
715 public static final int ASSIST_CONTEXT_AUTOFILL = 2;
Felipe Leme6d553872016-12-08 17:13:25 -0800716
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700717 /** @hide Flag for registerUidObserver: report changes in process state. */
718 public static final int UID_OBSERVER_PROCSTATE = 1<<0;
719
720 /** @hide Flag for registerUidObserver: report uid gone. */
721 public static final int UID_OBSERVER_GONE = 1<<1;
722
723 /** @hide Flag for registerUidObserver: report uid has become idle. */
724 public static final int UID_OBSERVER_IDLE = 1<<2;
725
726 /** @hide Flag for registerUidObserver: report uid has become active. */
727 public static final int UID_OBSERVER_ACTIVE = 1<<3;
728
Dianne Hackborn3e99f652017-07-05 16:33:56 -0700729 /** @hide Flag for registerUidObserver: report uid cached state has changed. */
730 public static final int UID_OBSERVER_CACHED = 1<<4;
731
Dianne Hackbornc3af19a2017-01-20 17:00:44 -0800732 /** @hide Mode for {@link IActivityManager#isAppStartModeDisabled}: normal free-to-run operation. */
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700733 public static final int APP_START_MODE_NORMAL = 0;
734
Dianne Hackbornc3af19a2017-01-20 17:00:44 -0800735 /** @hide Mode for {@link IActivityManager#isAppStartModeDisabled}: delay running until later. */
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700736 public static final int APP_START_MODE_DELAYED = 1;
737
Dianne Hackbornc3af19a2017-01-20 17:00:44 -0800738 /** @hide Mode for {@link IActivityManager#isAppStartModeDisabled}: delay running until later, with
Dianne Hackborn85e35642017-01-12 15:10:57 -0800739 * rigid errors (throwing exception). */
740 public static final int APP_START_MODE_DELAYED_RIGID = 2;
741
Dianne Hackbornc3af19a2017-01-20 17:00:44 -0800742 /** @hide Mode for {@link IActivityManager#isAppStartModeDisabled}: disable/cancel pending
Dianne Hackborn85e35642017-01-12 15:10:57 -0800743 * launches; this is the mode for ephemeral apps. */
744 public static final int APP_START_MODE_DISABLED = 3;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700745
Benjamin Franz43261142015-02-11 15:59:44 +0000746 /**
747 * Lock task mode is not active.
748 */
749 public static final int LOCK_TASK_MODE_NONE = 0;
750
751 /**
752 * Full lock task mode is active.
753 */
754 public static final int LOCK_TASK_MODE_LOCKED = 1;
755
756 /**
757 * App pinning mode is active.
758 */
759 public static final int LOCK_TASK_MODE_PINNED = 2;
760
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700761 Point mAppTaskThumbnailSize;
762
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100763 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 /*package*/ ActivityManager(Context context, Handler handler) {
765 mContext = context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
767
768 /**
Bryce Lee7f936862017-05-09 15:33:18 -0700769 * Returns whether the launch was successful.
770 * @hide
771 */
772 public static final boolean isStartResultSuccessful(int result) {
773 return FIRST_START_SUCCESS_CODE <= result && result <= LAST_START_SUCCESS_CODE;
774 }
775
776 /**
777 * Returns whether the launch result was a fatal error.
778 * @hide
779 */
780 public static final boolean isStartResultFatalError(int result) {
781 return FIRST_START_FATAL_ERROR_CODE <= result && result <= LAST_START_FATAL_ERROR_CODE;
782 }
783
784 /**
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700785 * Screen compatibility mode: the application most always run in
786 * compatibility mode.
787 * @hide
788 */
789 public static final int COMPAT_MODE_ALWAYS = -1;
790
791 /**
792 * Screen compatibility mode: the application can never run in
793 * compatibility mode.
794 * @hide
795 */
796 public static final int COMPAT_MODE_NEVER = -2;
797
798 /**
799 * Screen compatibility mode: unknown.
800 * @hide
801 */
802 public static final int COMPAT_MODE_UNKNOWN = -3;
803
804 /**
805 * Screen compatibility mode: the application currently has compatibility
806 * mode disabled.
807 * @hide
808 */
809 public static final int COMPAT_MODE_DISABLED = 0;
810
811 /**
812 * Screen compatibility mode: the application currently has compatibility
813 * mode enabled.
814 * @hide
815 */
816 public static final int COMPAT_MODE_ENABLED = 1;
817
818 /**
819 * Screen compatibility mode: request to toggle the application's
820 * compatibility mode.
821 * @hide
822 */
823 public static final int COMPAT_MODE_TOGGLE = 2;
824
Marc Hittinger17fb5962017-04-05 15:34:38 -0700825 private static final boolean DEVELOPMENT_FORCE_LOW_RAM =
826 SystemProperties.getBoolean("debug.force_low_ram", false);
827
Wale Ogunwale3797c222015-10-27 14:21:58 -0700828 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700829 public int getFrontActivityScreenCompatMode() {
830 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700831 return getTaskService().getFrontActivityScreenCompatMode();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700832 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700833 throw e.rethrowFromSystemServer();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700834 }
835 }
836
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700837 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700838 public void setFrontActivityScreenCompatMode(int mode) {
839 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700840 getTaskService().setFrontActivityScreenCompatMode(mode);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700841 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700842 throw e.rethrowFromSystemServer();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700843 }
844 }
845
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700846 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700847 public int getPackageScreenCompatMode(String packageName) {
848 try {
Wale Ogunwale53783742018-09-16 10:21:51 -0700849 return getTaskService().getPackageScreenCompatMode(packageName);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700850 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700851 throw e.rethrowFromSystemServer();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700852 }
853 }
854
Dianne Hackborndf9799f2011-05-12 15:16:33 -0700855 /** @hide */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700856 public void setPackageScreenCompatMode(String packageName, int mode) {
857 try {
Wale Ogunwale53783742018-09-16 10:21:51 -0700858 getTaskService().setPackageScreenCompatMode(packageName, mode);
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700859 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700860 throw e.rethrowFromSystemServer();
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700861 }
862 }
863
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700864 /** @hide */
865 public boolean getPackageAskScreenCompat(String packageName) {
866 try {
Wale Ogunwale53783742018-09-16 10:21:51 -0700867 return getTaskService().getPackageAskScreenCompat(packageName);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700868 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700869 throw e.rethrowFromSystemServer();
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700870 }
871 }
872
873 /** @hide */
874 public void setPackageAskScreenCompat(String packageName, boolean ask) {
875 try {
Wale Ogunwale53783742018-09-16 10:21:51 -0700876 getTaskService().setPackageAskScreenCompat(packageName, ask);
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700877 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700878 throw e.rethrowFromSystemServer();
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700879 }
880 }
881
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700882 /**
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700883 * Return the approximate per-application memory class of the current
884 * device. This gives you an idea of how hard a memory limit you should
885 * impose on your application to let the overall system work best. The
886 * returned value is in megabytes; the baseline Android memory class is
887 * 16 (which happens to be the Java heap limit of those devices); some
Kweku Adams983829f2017-12-06 14:53:50 -0800888 * devices with more memory may return 24 or even higher numbers.
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700889 */
890 public int getMemoryClass() {
891 return staticGetMemoryClass();
892 }
Stefan Kuhne16045c22015-06-05 07:18:06 -0700893
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700894 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100895 @UnsupportedAppUsage
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700896 static public int staticGetMemoryClass() {
897 // Really brain dead right now -- just take this from the configured
898 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn7ad33c82011-03-08 15:53:25 -0800899 String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", "");
Dianne Hackbornde398512011-01-18 18:45:21 -0800900 if (vmHeapSize != null && !"".equals(vmHeapSize)) {
901 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
902 }
903 return staticGetLargeMemoryClass();
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800904 }
Stefan Kuhne16045c22015-06-05 07:18:06 -0700905
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800906 /**
907 * Return the approximate per-application memory class of the current
908 * device when an application is running with a large heap. This is the
909 * space available for memory-intensive applications; most applications
910 * should not need this amount of memory, and should instead stay with the
911 * {@link #getMemoryClass()} limit. The returned value is in megabytes.
912 * This may be the same size as {@link #getMemoryClass()} on memory
913 * constrained devices, or it may be significantly larger on devices with
914 * a large amount of available RAM.
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800915 *
Kweku Adams983829f2017-12-06 14:53:50 -0800916 * <p>This is the size of the application's Dalvik heap if it has
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800917 * specified <code>android:largeHeap="true"</code> in its manifest.
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800918 */
919 public int getLargeMemoryClass() {
920 return staticGetLargeMemoryClass();
921 }
Stefan Kuhne16045c22015-06-05 07:18:06 -0700922
Dianne Hackborn4e24aac2010-12-23 11:43:46 -0800923 /** @hide */
924 static public int staticGetLargeMemoryClass() {
925 // Really brain dead right now -- just take this from the configured
926 // vm heap size, and assume it is in megabytes and thus ends with "m".
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700927 String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
Dianne Hackborn852975d2014-08-22 17:42:43 -0700928 return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length() - 1));
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700929 }
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700930
931 /**
932 * Returns true if this is a low-RAM device. Exactly whether a device is low-RAM
933 * is ultimately up to the device configuration, but currently it generally means
Greg Kaisere2c459e2017-10-26 10:36:37 -0700934 * something with 1GB or less of RAM. This is mostly intended to be used by apps
935 * to determine whether they should turn off certain features that require more RAM.
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700936 */
937 public boolean isLowRamDevice() {
938 return isLowRamDeviceStatic();
939 }
940
941 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100942 @UnsupportedAppUsage
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700943 public static boolean isLowRamDeviceStatic() {
Marc Hittinger17fb5962017-04-05 15:34:38 -0700944 return RoSystemProperties.CONFIG_LOW_RAM ||
945 (Build.IS_DEBUGGABLE && DEVELOPMENT_FORCE_LOW_RAM);
Dianne Hackbornb4e12492013-06-26 15:08:19 -0700946 }
947
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -0700948 /**
Michael Kwan15eb9982017-04-14 12:34:46 -0700949 * Returns true if this is a small battery device. Exactly whether a device is considered to be
950 * small battery is ultimately up to the device configuration, but currently it generally means
951 * something in the class of a device with 1000 mAh or less. This is mostly intended to be used
952 * to determine whether certain features should be altered to account for a drastically smaller
953 * battery.
954 * @hide
955 */
956 public static boolean isSmallBatteryDevice() {
957 return RoSystemProperties.CONFIG_SMALL_BATTERY;
958 }
959
960 /**
Dianne Hackbornb3756322011-08-12 13:58:13 -0700961 * Used by persistent processes to determine if they are running on a
962 * higher-end device so should be okay using hardware drawing acceleration
963 * (which tends to consume a lot more RAM).
964 * @hide
965 */
Adrian Roos5af40872019-07-15 12:05:54 +0200966 @TestApi
Jeff Brown98365d72012-08-19 20:30:52 -0700967 static public boolean isHighEndGfx() {
Luis Hector Chavez5e11e5e2018-03-23 14:37:01 -0700968 return !isLowRamDeviceStatic()
969 && !RoSystemProperties.CONFIG_AVOID_GFX_ACCEL
970 && !Resources.getSystem()
971 .getBoolean(com.android.internal.R.bool.config_avoidGfxAccel);
Dianne Hackborn49d228b32011-08-24 17:37:31 -0700972 }
973
974 /**
Dianne Hackborn337e01a2018-02-27 17:16:37 -0800975 * Return the total number of bytes of RAM this device has.
976 * @hide
977 */
978 @TestApi
979 public long getTotalRam() {
980 MemInfoReader memreader = new MemInfoReader();
981 memreader.readMemInfo();
982 return memreader.getTotalSize();
983 }
984
985 /**
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700986 * TODO(b/80414790): Remove once no longer on hiddenapi-light-greylist.txt
Dianne Hackborn852975d2014-08-22 17:42:43 -0700987 * @hide
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700988 * @deprecated Use {@link ActivityTaskManager#getMaxRecentTasksStatic()}
Dianne Hackborn852975d2014-08-22 17:42:43 -0700989 */
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700990 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100991 @UnsupportedAppUsage
Dianne Hackborn852975d2014-08-22 17:42:43 -0700992 static public int getMaxRecentTasksStatic() {
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700993 return ActivityTaskManager.getMaxRecentTasksStatic();
Matthew Ng626e0cc2016-12-07 17:25:53 -0800994 }
995
Jeff Sharkey000ce802017-04-29 13:13:27 -0600996 /** @removed */
Winson Chung709904f2017-04-25 11:00:48 -0700997 @Deprecated
Winson Chunga29eb982016-12-14 12:01:27 -0800998 public static int getMaxNumPictureInPictureActions() {
Winson Chung709904f2017-04-25 11:00:48 -0700999 return 3;
Winson Chunga29eb982016-12-14 12:01:27 -08001000 }
1001
1002 /**
Winson Chung1147c402014-05-14 11:05:00 -07001003 * Information you can set and retrieve about the current activity within the recent task list.
Winson Chung03a9bae2014-05-02 09:56:12 -07001004 */
Winson Chunga449dc02014-05-16 11:15:04 -07001005 public static class TaskDescription implements Parcelable {
Craig Mautner648f69b2014-09-18 14:16:26 -07001006 /** @hide */
1007 public static final String ATTR_TASKDESCRIPTION_PREFIX = "task_description_";
1008 private static final String ATTR_TASKDESCRIPTIONLABEL =
1009 ATTR_TASKDESCRIPTION_PREFIX + "label";
Winson Chung1af8eda2016-02-05 17:55:56 +00001010 private static final String ATTR_TASKDESCRIPTIONCOLOR_PRIMARY =
Craig Mautner648f69b2014-09-18 14:16:26 -07001011 ATTR_TASKDESCRIPTION_PREFIX + "color";
Winson Chung1af8eda2016-02-05 17:55:56 +00001012 private static final String ATTR_TASKDESCRIPTIONCOLOR_BACKGROUND =
1013 ATTR_TASKDESCRIPTION_PREFIX + "colorBackground";
Matthew Ng54bc9422017-10-02 17:16:28 -07001014 private static final String ATTR_TASKDESCRIPTIONICON_FILENAME =
Craig Mautner648f69b2014-09-18 14:16:26 -07001015 ATTR_TASKDESCRIPTION_PREFIX + "icon_filename";
Matthew Ng54bc9422017-10-02 17:16:28 -07001016 private static final String ATTR_TASKDESCRIPTIONICON_RESOURCE =
1017 ATTR_TASKDESCRIPTION_PREFIX + "icon_resource";
Winson Chungd6722032020-02-18 15:16:08 -08001018 private static final String ATTR_TASKDESCRIPTIONICON_RESOURCE_PACKAGE =
1019 ATTR_TASKDESCRIPTION_PREFIX + "icon_package";
Craig Mautner648f69b2014-09-18 14:16:26 -07001020
Winson Chunga449dc02014-05-16 11:15:04 -07001021 private String mLabel;
Winson Chungd6722032020-02-18 15:16:08 -08001022 @Nullable
1023 private Icon mIcon;
Craig Mautner648f69b2014-09-18 14:16:26 -07001024 private String mIconFilename;
Winson Chunga449dc02014-05-16 11:15:04 -07001025 private int mColorPrimary;
Winson Chung1af8eda2016-02-05 17:55:56 +00001026 private int mColorBackground;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001027 private int mStatusBarColor;
1028 private int mNavigationBarColor;
Adrian Roos4c864592019-04-10 14:47:57 +02001029 private boolean mEnsureStatusBarContrastWhenTransparent;
1030 private boolean mEnsureNavigationBarContrastWhenTransparent;
Yunfan Chen674f9692019-11-05 14:41:08 -08001031 private int mResizeMode;
1032 private int mMinWidth;
1033 private int mMinHeight;
Winson Chung03a9bae2014-05-02 09:56:12 -07001034
Winson Chungd6722032020-02-18 15:16:08 -08001035
1036 /**
1037 * Creates the TaskDescription to the specified values.
1038 *
1039 * @param label A label and description of the current state of this task.
1040 * @param iconRes A drawable resource of an icon that represents the current state of this
1041 * activity.
1042 * @param colorPrimary A color to override the theme's primary color. This color must be
1043 * opaque.
1044 */
1045 public TaskDescription(String label, @DrawableRes int iconRes, int colorPrimary) {
1046 this(label, Icon.createWithResource(ActivityThread.currentPackageName(), iconRes),
1047 colorPrimary, 0, 0, 0, false, false, RESIZE_MODE_RESIZEABLE, -1, -1);
1048 if ((colorPrimary != 0) && (Color.alpha(colorPrimary) != 255)) {
1049 throw new RuntimeException("A TaskDescription's primary color should be opaque");
1050 }
1051 }
1052
1053 /**
1054 * Creates the TaskDescription to the specified values.
1055 *
1056 * @param label A label and description of the current state of this activity.
1057 * @param iconRes A drawable resource of an icon that represents the current state of this
1058 * activity.
1059 */
1060 public TaskDescription(String label, @DrawableRes int iconRes) {
1061 this(label, Icon.createWithResource(ActivityThread.currentPackageName(), iconRes),
1062 0, 0, 0, 0, false, false, RESIZE_MODE_RESIZEABLE, -1, -1);
1063 }
1064
1065 /**
1066 * Creates the TaskDescription to the specified values.
1067 *
1068 * @param label A label and description of the current state of this activity.
1069 */
1070 public TaskDescription(String label) {
1071 this(label, null, 0, 0, 0, 0, false, false, RESIZE_MODE_RESIZEABLE, -1, -1);
1072 }
1073
1074 /**
1075 * Creates an empty TaskDescription.
1076 */
1077 public TaskDescription() {
1078 this(null, null, 0, 0, 0, 0, false, false, RESIZE_MODE_RESIZEABLE, -1, -1);
1079 }
1080
Winson Chung03a9bae2014-05-02 09:56:12 -07001081 /**
Winson Chunga449dc02014-05-16 11:15:04 -07001082 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -07001083 *
Winson Chunga449dc02014-05-16 11:15:04 -07001084 * @param label A label and description of the current state of this task.
1085 * @param icon An icon that represents the current state of this task.
Winson Chung1af8eda2016-02-05 17:55:56 +00001086 * @param colorPrimary A color to override the theme's primary color. This color must be
1087 * opaque.
Matthew Ng54bc9422017-10-02 17:16:28 -07001088 * @deprecated use TaskDescription constructor with icon resource instead
Winson Chung03a9bae2014-05-02 09:56:12 -07001089 */
Matthew Ng54bc9422017-10-02 17:16:28 -07001090 @Deprecated
Winson Chunga449dc02014-05-16 11:15:04 -07001091 public TaskDescription(String label, Bitmap icon, int colorPrimary) {
Winson Chungd6722032020-02-18 15:16:08 -08001092 this(label, icon != null ? Icon.createWithBitmap(icon) : null, colorPrimary, 0, 0, 0,
1093 false, false, RESIZE_MODE_RESIZEABLE, -1, -1);
Winson Chunga449dc02014-05-16 11:15:04 -07001094 if ((colorPrimary != 0) && (Color.alpha(colorPrimary) != 255)) {
1095 throw new RuntimeException("A TaskDescription's primary color should be opaque");
1096 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001097 }
1098
Winson Chung03a9bae2014-05-02 09:56:12 -07001099 /**
Winson Chunga449dc02014-05-16 11:15:04 -07001100 * Creates the TaskDescription to the specified values.
Winson Chung03a9bae2014-05-02 09:56:12 -07001101 *
1102 * @param label A label and description of the current state of this activity.
1103 * @param icon An icon that represents the current state of this activity.
Matthew Ng54bc9422017-10-02 17:16:28 -07001104 * @deprecated use TaskDescription constructor with icon resource instead
Winson Chung03a9bae2014-05-02 09:56:12 -07001105 */
Matthew Ng54bc9422017-10-02 17:16:28 -07001106 @Deprecated
Winson Chunga449dc02014-05-16 11:15:04 -07001107 public TaskDescription(String label, Bitmap icon) {
Winson Chungd6722032020-02-18 15:16:08 -08001108 this(label, icon != null ? Icon.createWithBitmap(icon) : null, 0, 0, 0, 0, false, false,
Yunfan Chen674f9692019-11-05 14:41:08 -08001109 RESIZE_MODE_RESIZEABLE, -1, -1);
Winson Chung03a9bae2014-05-02 09:56:12 -07001110 }
1111
Winson Chung1af8eda2016-02-05 17:55:56 +00001112 /** @hide */
Winson Chungd6722032020-02-18 15:16:08 -08001113 public TaskDescription(@Nullable String label, @Nullable Icon icon,
1114 int colorPrimary, int colorBackground,
1115 int statusBarColor, int navigationBarColor,
Adrian Roos4c864592019-04-10 14:47:57 +02001116 boolean ensureStatusBarContrastWhenTransparent,
Yunfan Chen674f9692019-11-05 14:41:08 -08001117 boolean ensureNavigationBarContrastWhenTransparent, int resizeMode, int minWidth,
1118 int minHeight) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001119 mLabel = label;
Winson Chungd6722032020-02-18 15:16:08 -08001120 mIcon = icon;
Winson Chung1af8eda2016-02-05 17:55:56 +00001121 mColorPrimary = colorPrimary;
1122 mColorBackground = colorBackground;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001123 mStatusBarColor = statusBarColor;
1124 mNavigationBarColor = navigationBarColor;
Adrian Roos4c864592019-04-10 14:47:57 +02001125 mEnsureStatusBarContrastWhenTransparent = ensureStatusBarContrastWhenTransparent;
1126 mEnsureNavigationBarContrastWhenTransparent =
1127 ensureNavigationBarContrastWhenTransparent;
Yunfan Chen674f9692019-11-05 14:41:08 -08001128 mResizeMode = resizeMode;
1129 mMinWidth = minWidth;
1130 mMinHeight = minHeight;
Winson Chung03a9bae2014-05-02 09:56:12 -07001131 }
1132
Winson Chunga449dc02014-05-16 11:15:04 -07001133 /**
1134 * Creates a copy of another TaskDescription.
1135 */
1136 public TaskDescription(TaskDescription td) {
Winsonb6403152016-02-23 13:32:09 -08001137 copyFrom(td);
1138 }
1139
1140 /**
1141 * Copies this the values from another TaskDescription.
1142 * @hide
1143 */
1144 public void copyFrom(TaskDescription other) {
1145 mLabel = other.mLabel;
1146 mIcon = other.mIcon;
1147 mIconFilename = other.mIconFilename;
1148 mColorPrimary = other.mColorPrimary;
1149 mColorBackground = other.mColorBackground;
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001150 mStatusBarColor = other.mStatusBarColor;
1151 mNavigationBarColor = other.mNavigationBarColor;
Adrian Roos4c864592019-04-10 14:47:57 +02001152 mEnsureStatusBarContrastWhenTransparent = other.mEnsureStatusBarContrastWhenTransparent;
1153 mEnsureNavigationBarContrastWhenTransparent =
1154 other.mEnsureNavigationBarContrastWhenTransparent;
Yunfan Chen674f9692019-11-05 14:41:08 -08001155 mResizeMode = other.mResizeMode;
1156 mMinWidth = other.mMinWidth;
1157 mMinHeight = other.mMinHeight;
Winson Chunga449dc02014-05-16 11:15:04 -07001158 }
1159
Jorim Jaggif4156ea2017-04-10 18:39:04 -07001160 /**
Mehdi Alizadehc5e29902019-11-14 17:18:44 -08001161 * Copies values from another TaskDescription, but preserves the hidden fields if they
1162 * weren't set on {@code other}. Public fields will be overwritten anyway.
Jorim Jaggif4156ea2017-04-10 18:39:04 -07001163 * @hide
1164 */
1165 public void copyFromPreserveHiddenFields(TaskDescription other) {
1166 mLabel = other.mLabel;
1167 mIcon = other.mIcon;
1168 mIconFilename = other.mIconFilename;
1169 mColorPrimary = other.mColorPrimary;
Mehdi Alizadehc5e29902019-11-14 17:18:44 -08001170
Jorim Jaggif4156ea2017-04-10 18:39:04 -07001171 if (other.mColorBackground != 0) {
1172 mColorBackground = other.mColorBackground;
1173 }
1174 if (other.mStatusBarColor != 0) {
1175 mStatusBarColor = other.mStatusBarColor;
1176 }
1177 if (other.mNavigationBarColor != 0) {
1178 mNavigationBarColor = other.mNavigationBarColor;
1179 }
Mehdi Alizadehc5e29902019-11-14 17:18:44 -08001180
Adrian Roos4c864592019-04-10 14:47:57 +02001181 mEnsureStatusBarContrastWhenTransparent = other.mEnsureStatusBarContrastWhenTransparent;
1182 mEnsureNavigationBarContrastWhenTransparent =
1183 other.mEnsureNavigationBarContrastWhenTransparent;
Mehdi Alizadehc5e29902019-11-14 17:18:44 -08001184
1185 if (other.mResizeMode != RESIZE_MODE_RESIZEABLE) {
1186 mResizeMode = other.mResizeMode;
1187 }
1188 if (other.mMinWidth != -1) {
1189 mMinWidth = other.mMinWidth;
1190 }
1191 if (other.mMinHeight != -1) {
1192 mMinHeight = other.mMinHeight;
1193 }
Jorim Jaggif4156ea2017-04-10 18:39:04 -07001194 }
1195
Winson Chunga449dc02014-05-16 11:15:04 -07001196 private TaskDescription(Parcel source) {
Winson Chung03a9bae2014-05-02 09:56:12 -07001197 readFromParcel(source);
1198 }
1199
1200 /**
Winson Chung2cb86c72014-06-25 12:03:30 -07001201 * Sets the label for this task description.
1202 * @hide
1203 */
1204 public void setLabel(String label) {
1205 mLabel = label;
1206 }
1207
1208 /**
1209 * Sets the primary color for this task description.
1210 * @hide
1211 */
1212 public void setPrimaryColor(int primaryColor) {
Winson Chungae12dae2014-09-29 14:21:26 -07001213 // Ensure that the given color is valid
1214 if ((primaryColor != 0) && (Color.alpha(primaryColor) != 255)) {
1215 throw new RuntimeException("A TaskDescription's primary color should be opaque");
1216 }
1217 mColorPrimary = primaryColor;
Winson Chung2cb86c72014-06-25 12:03:30 -07001218 }
1219
1220 /**
Winson Chung1af8eda2016-02-05 17:55:56 +00001221 * Sets the background color for this task description.
1222 * @hide
1223 */
1224 public void setBackgroundColor(int backgroundColor) {
1225 // Ensure that the given color is valid
1226 if ((backgroundColor != 0) && (Color.alpha(backgroundColor) != 255)) {
1227 throw new RuntimeException("A TaskDescription's background color should be opaque");
1228 }
1229 mColorBackground = backgroundColor;
1230 }
1231
1232 /**
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001233 * @hide
1234 */
1235 public void setStatusBarColor(int statusBarColor) {
1236 mStatusBarColor = statusBarColor;
1237 }
1238
1239 /**
1240 * @hide
1241 */
1242 public void setNavigationBarColor(int navigationBarColor) {
1243 mNavigationBarColor = navigationBarColor;
1244 }
1245
1246 /**
Matthew Ng54bc9422017-10-02 17:16:28 -07001247 * Sets the icon resource for this task description.
1248 * @hide
1249 */
Winson Chungd6722032020-02-18 15:16:08 -08001250 public void setIcon(Icon icon) {
1251 mIcon = icon;
Matthew Ng54bc9422017-10-02 17:16:28 -07001252 }
1253
1254 /**
Craig Mautner648f69b2014-09-18 14:16:26 -07001255 * Moves the icon bitmap reference from an actual Bitmap to a file containing the
1256 * bitmap.
1257 * @hide
1258 */
1259 public void setIconFilename(String iconFilename) {
1260 mIconFilename = iconFilename;
Winson Chungd6722032020-02-18 15:16:08 -08001261 if (iconFilename != null) {
1262 // Only reset the icon if an actual persisted icon filepath was set
1263 mIcon = null;
1264 }
Craig Mautner648f69b2014-09-18 14:16:26 -07001265 }
1266
1267 /**
Yunfan Chen674f9692019-11-05 14:41:08 -08001268 * Sets the resize mode for this task description. Resize mode as in
1269 * {@link android.content.pm.ActivityInfo}.
1270 * @hide
1271 */
1272 public void setResizeMode(int resizeMode) {
1273 mResizeMode = resizeMode;
1274 }
1275
1276 /**
1277 * The minimal width size to show the app content in freeform mode.
1278 * @param minWidth minimal width, -1 for system default.
1279 * @hide
1280 */
1281 public void setMinWidth(int minWidth) {
1282 mMinWidth = minWidth;
1283 }
1284
1285 /**
1286 * The minimal height size to show the app content in freeform mode.
1287 * @param minHeight minimal height, -1 for system default.
1288 * @hide
1289 */
1290 public void setMinHeight(int minHeight) {
1291 mMinHeight = minHeight;
1292 }
1293
1294 /**
Winson Chunga449dc02014-05-16 11:15:04 -07001295 * @return The label and description of the current state of this task.
Winson Chung03a9bae2014-05-02 09:56:12 -07001296 */
Winson Chunga449dc02014-05-16 11:15:04 -07001297 public String getLabel() {
1298 return mLabel;
1299 }
1300
1301 /**
Winson Chungd6722032020-02-18 15:16:08 -08001302 * @return The actual icon that represents the current state of this task if it is in memory
1303 * or loads it from disk if available.
1304 * @hide
Winson Chunga449dc02014-05-16 11:15:04 -07001305 */
Winson Chungd6722032020-02-18 15:16:08 -08001306 public Icon loadIcon() {
Craig Mautner648f69b2014-09-18 14:16:26 -07001307 if (mIcon != null) {
1308 return mIcon;
1309 }
Winson Chungd6722032020-02-18 15:16:08 -08001310 Bitmap loadedIcon = loadTaskDescriptionIcon(mIconFilename, UserHandle.myUserId());
1311 if (loadedIcon != null) {
1312 return Icon.createWithBitmap(loadedIcon);
1313 }
1314 return null;
1315 }
1316
1317 /**
1318 * @return The in-memory or loaded icon that represents the current state of this task.
Winson Chung023ebdf2020-03-10 11:01:36 -07001319 * @deprecated This call is no longer supported. The caller should keep track of any icons
1320 * it sets for the task descriptions internally.
Winson Chungd6722032020-02-18 15:16:08 -08001321 */
1322 @Deprecated
1323 public Bitmap getIcon() {
1324 Bitmap icon = getInMemoryIcon();
1325 if (icon != null) {
1326 return icon;
1327 }
Suprabh Shukla23593142015-11-03 17:31:15 -08001328 return loadTaskDescriptionIcon(mIconFilename, UserHandle.myUserId());
Craig Mautner648f69b2014-09-18 14:16:26 -07001329 }
1330
1331 /** @hide */
Winson Chungd6722032020-02-18 15:16:08 -08001332 @Nullable
1333 public Icon getRawIcon() {
1334 return mIcon;
1335 }
1336
1337 /** @hide */
1338 @TestApi
1339 @Nullable
1340 public String getIconResourcePackage() {
1341 if (mIcon != null && mIcon.getType() == Icon.TYPE_RESOURCE) {
1342 return mIcon.getResPackage();
1343 }
1344 return "";
1345 }
1346
1347 /** @hide */
Matthew Ng54bc9422017-10-02 17:16:28 -07001348 @TestApi
1349 public int getIconResource() {
Winson Chungd6722032020-02-18 15:16:08 -08001350 if (mIcon != null && mIcon.getType() == Icon.TYPE_RESOURCE) {
1351 return mIcon.getResId();
1352 }
1353 return 0;
Matthew Ng54bc9422017-10-02 17:16:28 -07001354 }
1355
1356 /** @hide */
1357 @TestApi
Craig Mautner648f69b2014-09-18 14:16:26 -07001358 public String getIconFilename() {
1359 return mIconFilename;
Winson Chunga449dc02014-05-16 11:15:04 -07001360 }
1361
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001362 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001363 @UnsupportedAppUsage
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001364 public Bitmap getInMemoryIcon() {
Winson Chungd6722032020-02-18 15:16:08 -08001365 if (mIcon != null && mIcon.getType() == Icon.TYPE_BITMAP) {
1366 return mIcon.getBitmap();
1367 }
1368 return null;
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001369 }
1370
1371 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001372 @UnsupportedAppUsage
Suprabh Shukla23593142015-11-03 17:31:15 -08001373 public static Bitmap loadTaskDescriptionIcon(String iconFilename, int userId) {
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001374 if (iconFilename != null) {
1375 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001376 return getTaskService().getTaskDescriptionIcon(iconFilename,
Winson Chung1af8eda2016-02-05 17:55:56 +00001377 userId);
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001378 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001379 throw e.rethrowFromSystemServer();
Jorim Jaggibdd4b202014-09-26 18:29:07 +02001380 }
1381 }
1382 return null;
1383 }
1384
Winson Chunga449dc02014-05-16 11:15:04 -07001385 /**
1386 * @return The color override on the theme's primary color.
1387 */
1388 public int getPrimaryColor() {
1389 return mColorPrimary;
Winson Chung03a9bae2014-05-02 09:56:12 -07001390 }
1391
Winson Chung1af8eda2016-02-05 17:55:56 +00001392 /**
1393 * @return The background color.
1394 * @hide
1395 */
Mathew Inwood8c854f82018-09-14 12:35:36 +01001396 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Winson Chung1af8eda2016-02-05 17:55:56 +00001397 public int getBackgroundColor() {
1398 return mColorBackground;
1399 }
1400
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001401 /**
1402 * @hide
1403 */
1404 public int getStatusBarColor() {
1405 return mStatusBarColor;
1406 }
1407
1408 /**
1409 * @hide
1410 */
1411 public int getNavigationBarColor() {
1412 return mNavigationBarColor;
1413 }
1414
Adrian Roos4c864592019-04-10 14:47:57 +02001415 /**
1416 * @hide
1417 */
1418 public boolean getEnsureStatusBarContrastWhenTransparent() {
1419 return mEnsureStatusBarContrastWhenTransparent;
1420 }
1421
1422 /**
1423 * @hide
1424 */
1425 public void setEnsureStatusBarContrastWhenTransparent(
1426 boolean ensureStatusBarContrastWhenTransparent) {
1427 mEnsureStatusBarContrastWhenTransparent = ensureStatusBarContrastWhenTransparent;
1428 }
1429
1430 /**
1431 * @hide
1432 */
1433 public boolean getEnsureNavigationBarContrastWhenTransparent() {
1434 return mEnsureNavigationBarContrastWhenTransparent;
1435 }
1436
1437 /**
1438 * @hide
1439 */
1440 public void setEnsureNavigationBarContrastWhenTransparent(
1441 boolean ensureNavigationBarContrastWhenTransparent) {
1442 mEnsureNavigationBarContrastWhenTransparent =
1443 ensureNavigationBarContrastWhenTransparent;
1444 }
1445
Yunfan Chen674f9692019-11-05 14:41:08 -08001446 /**
1447 * @hide
1448 */
1449 public int getResizeMode() {
1450 return mResizeMode;
1451 }
1452
1453 /**
1454 * @hide
1455 */
1456 public int getMinWidth() {
1457 return mMinWidth;
1458 }
1459
1460 /**
1461 * @hide
1462 */
1463 public int getMinHeight() {
1464 return mMinHeight;
1465 }
1466
Craig Mautner648f69b2014-09-18 14:16:26 -07001467 /** @hide */
1468 public void saveToXml(XmlSerializer out) throws IOException {
1469 if (mLabel != null) {
1470 out.attribute(null, ATTR_TASKDESCRIPTIONLABEL, mLabel);
1471 }
1472 if (mColorPrimary != 0) {
Winson Chung1af8eda2016-02-05 17:55:56 +00001473 out.attribute(null, ATTR_TASKDESCRIPTIONCOLOR_PRIMARY,
1474 Integer.toHexString(mColorPrimary));
1475 }
1476 if (mColorBackground != 0) {
1477 out.attribute(null, ATTR_TASKDESCRIPTIONCOLOR_BACKGROUND,
1478 Integer.toHexString(mColorBackground));
Craig Mautner648f69b2014-09-18 14:16:26 -07001479 }
1480 if (mIconFilename != null) {
Matthew Ng54bc9422017-10-02 17:16:28 -07001481 out.attribute(null, ATTR_TASKDESCRIPTIONICON_FILENAME, mIconFilename);
1482 }
Winson Chungd6722032020-02-18 15:16:08 -08001483 if (mIcon != null && mIcon.getType() == Icon.TYPE_RESOURCE) {
1484 out.attribute(null, ATTR_TASKDESCRIPTIONICON_RESOURCE,
1485 Integer.toString(mIcon.getResId()));
1486 out.attribute(null, ATTR_TASKDESCRIPTIONICON_RESOURCE_PACKAGE,
1487 mIcon.getResPackage());
Craig Mautner648f69b2014-09-18 14:16:26 -07001488 }
1489 }
1490
1491 /** @hide */
Winson Chungd6722032020-02-18 15:16:08 -08001492 public void restoreFromXml(XmlPullParser in) {
1493 final String label = in.getAttributeValue(null, ATTR_TASKDESCRIPTIONLABEL);
1494 if (label != null) {
1495 setLabel(label);
1496 }
1497 final String colorPrimary = in.getAttributeValue(null,
1498 ATTR_TASKDESCRIPTIONCOLOR_PRIMARY);
1499 if (colorPrimary != null) {
1500 setPrimaryColor((int) Long.parseLong(colorPrimary, 16));
1501 }
1502 final String colorBackground = in.getAttributeValue(null,
1503 ATTR_TASKDESCRIPTIONCOLOR_BACKGROUND);
1504 if (colorBackground != null) {
1505 setBackgroundColor((int) Long.parseLong(colorBackground, 16));
1506 }
1507 final String iconFilename = in.getAttributeValue(null,
1508 ATTR_TASKDESCRIPTIONICON_FILENAME);
1509 if (iconFilename != null) {
1510 setIconFilename(iconFilename);
1511 }
1512 final String iconResourceId = in.getAttributeValue(null,
1513 ATTR_TASKDESCRIPTIONICON_RESOURCE);
1514 final String iconResourcePackage = in.getAttributeValue(null,
1515 ATTR_TASKDESCRIPTIONICON_RESOURCE_PACKAGE);
1516 if (iconResourceId != null && iconResourcePackage != null) {
1517 setIcon(Icon.createWithResource(iconResourcePackage,
1518 Integer.parseInt(iconResourceId, 10)));
Craig Mautner648f69b2014-09-18 14:16:26 -07001519 }
1520 }
1521
Winson Chung03a9bae2014-05-02 09:56:12 -07001522 @Override
1523 public int describeContents() {
1524 return 0;
1525 }
1526
1527 @Override
1528 public void writeToParcel(Parcel dest, int flags) {
Winson Chunga449dc02014-05-16 11:15:04 -07001529 if (mLabel == null) {
Winson Chung03a9bae2014-05-02 09:56:12 -07001530 dest.writeInt(0);
1531 } else {
1532 dest.writeInt(1);
Winson Chunga449dc02014-05-16 11:15:04 -07001533 dest.writeString(mLabel);
Winson Chung03a9bae2014-05-02 09:56:12 -07001534 }
Winson Chungd6722032020-02-18 15:16:08 -08001535 final Bitmap bitmapIcon = getInMemoryIcon();
1536 if (mIcon == null || (bitmapIcon != null && bitmapIcon.isRecycled())) {
1537 // If there is no icon, or if the icon is a bitmap that has been recycled, then
1538 // don't write anything to disk
Winson Chunga449dc02014-05-16 11:15:04 -07001539 dest.writeInt(0);
1540 } else {
1541 dest.writeInt(1);
1542 mIcon.writeToParcel(dest, 0);
1543 }
1544 dest.writeInt(mColorPrimary);
Winson Chung1af8eda2016-02-05 17:55:56 +00001545 dest.writeInt(mColorBackground);
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001546 dest.writeInt(mStatusBarColor);
1547 dest.writeInt(mNavigationBarColor);
Adrian Roos4c864592019-04-10 14:47:57 +02001548 dest.writeBoolean(mEnsureStatusBarContrastWhenTransparent);
1549 dest.writeBoolean(mEnsureNavigationBarContrastWhenTransparent);
Yunfan Chen674f9692019-11-05 14:41:08 -08001550 dest.writeInt(mResizeMode);
1551 dest.writeInt(mMinWidth);
1552 dest.writeInt(mMinHeight);
Craig Mautner648f69b2014-09-18 14:16:26 -07001553 if (mIconFilename == null) {
1554 dest.writeInt(0);
1555 } else {
1556 dest.writeInt(1);
1557 dest.writeString(mIconFilename);
1558 }
Winson Chung03a9bae2014-05-02 09:56:12 -07001559 }
1560
1561 public void readFromParcel(Parcel source) {
Winson Chunga449dc02014-05-16 11:15:04 -07001562 mLabel = source.readInt() > 0 ? source.readString() : null;
Winson Chungd6722032020-02-18 15:16:08 -08001563 if (source.readInt() > 0) {
1564 mIcon = Icon.CREATOR.createFromParcel(source);
1565 }
Winson Chunga449dc02014-05-16 11:15:04 -07001566 mColorPrimary = source.readInt();
Winson Chung1af8eda2016-02-05 17:55:56 +00001567 mColorBackground = source.readInt();
Jorim Jaggi30d64f32017-04-07 16:33:17 +02001568 mStatusBarColor = source.readInt();
1569 mNavigationBarColor = source.readInt();
Adrian Roos4c864592019-04-10 14:47:57 +02001570 mEnsureStatusBarContrastWhenTransparent = source.readBoolean();
1571 mEnsureNavigationBarContrastWhenTransparent = source.readBoolean();
Yunfan Chen674f9692019-11-05 14:41:08 -08001572 mResizeMode = source.readInt();
1573 mMinWidth = source.readInt();
1574 mMinHeight = source.readInt();
Craig Mautner648f69b2014-09-18 14:16:26 -07001575 mIconFilename = source.readInt() > 0 ? source.readString() : null;
Winson Chung03a9bae2014-05-02 09:56:12 -07001576 }
1577
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001578 public static final @android.annotation.NonNull Creator<TaskDescription> CREATOR
Winson Chunga449dc02014-05-16 11:15:04 -07001579 = new Creator<TaskDescription>() {
1580 public TaskDescription createFromParcel(Parcel source) {
1581 return new TaskDescription(source);
Winson Chung03a9bae2014-05-02 09:56:12 -07001582 }
Winson Chunga449dc02014-05-16 11:15:04 -07001583 public TaskDescription[] newArray(int size) {
1584 return new TaskDescription[size];
Winson Chung03a9bae2014-05-02 09:56:12 -07001585 }
1586 };
1587
1588 @Override
1589 public String toString() {
Yunfan Chen674f9692019-11-05 14:41:08 -08001590 return "TaskDescription Label: " + mLabel + " Icon: " + mIcon
Winson Chungd6722032020-02-18 15:16:08 -08001591 + " IconFilename: " + mIconFilename
Yunfan Chen674f9692019-11-05 14:41:08 -08001592 + " colorPrimary: " + mColorPrimary + " colorBackground: " + mColorBackground
1593 + " statusBarColor: " + mStatusBarColor
1594 + (mEnsureStatusBarContrastWhenTransparent ? " (contrast when transparent)"
1595 : "") + " navigationBarColor: " + mNavigationBarColor
1596 + (mEnsureNavigationBarContrastWhenTransparent
1597 ? " (contrast when transparent)" : "")
1598 + " resizeMode: " + ActivityInfo.resizeModeToString(mResizeMode)
1599 + " minWidth: " + mMinWidth + " minHeight: " + mMinHeight;
Winson Chung03a9bae2014-05-02 09:56:12 -07001600 }
Winson Chung1df39e22020-04-09 14:30:55 -07001601
1602 @Override
1603 public boolean equals(Object obj) {
1604 if (!(obj instanceof TaskDescription)) {
1605 return false;
1606 }
1607
1608 TaskDescription other = (TaskDescription) obj;
1609 return TextUtils.equals(mLabel, other.mLabel)
1610 && TextUtils.equals(mIconFilename, other.mIconFilename)
1611 && mIcon == other.mIcon
1612 && mColorPrimary == other.mColorPrimary
1613 && mColorBackground == other.mColorBackground
1614 && mStatusBarColor == other.mStatusBarColor
1615 && mNavigationBarColor == other.mNavigationBarColor
1616 && mEnsureStatusBarContrastWhenTransparent
1617 == other.mEnsureStatusBarContrastWhenTransparent
1618 && mEnsureNavigationBarContrastWhenTransparent
1619 == other.mEnsureNavigationBarContrastWhenTransparent
1620 && mResizeMode == other.mResizeMode
1621 && mMinWidth == other.mMinWidth
1622 && mMinHeight == other.mMinHeight;
1623 }
1624
1625 /** @hide */
1626 public static boolean equals(TaskDescription td1, TaskDescription td2) {
1627 if (td1 == null && td2 == null) {
1628 return true;
1629 } else if (td1 != null && td2 != null) {
1630 return td1.equals(td2);
1631 }
1632 return false;
1633 }
Winson Chung03a9bae2014-05-02 09:56:12 -07001634 }
1635
1636 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 * Information you can retrieve about tasks that the user has most recently
1638 * started or visited.
1639 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001640 public static class RecentTaskInfo extends TaskInfo implements Parcelable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 /**
1642 * If this task is currently running, this is the identifier for it.
1643 * If it is not running, this will be -1.
Winson Chungabfdcce2018-07-02 17:23:33 -07001644 *
1645 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, use
1646 * {@link RecentTaskInfo#taskId} to get the task id and {@link RecentTaskInfo#isRunning}
1647 * to determine if it is running.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001649 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 public int id;
1651
1652 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -08001653 * The true identifier of this task, valid even if it is not running.
Winson Chungabfdcce2018-07-02 17:23:33 -07001654 *
1655 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, use
1656 * {@link RecentTaskInfo#taskId}.
Dianne Hackbornd94df452011-02-16 18:53:31 -08001657 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001658 @Deprecated
Dianne Hackbornd94df452011-02-16 18:53:31 -08001659 public int persistentId;
Stefan Kuhne16045c22015-06-05 07:18:06 -07001660
Dianne Hackbornd94df452011-02-16 18:53:31 -08001661 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -08001662 * Description of the task's last state.
Winson Chungabfdcce2018-07-02 17:23:33 -07001663 *
1664 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, currently always null.
Dianne Hackbornd2835932010-12-13 16:28:46 -08001665 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001666 @Deprecated
Dianne Hackbornd2835932010-12-13 16:28:46 -08001667 public CharSequence description;
Craig Mautner6d90fed2013-05-22 15:03:10 -07001668
1669 /**
Craig Mautnera228ae92014-07-09 05:44:55 -07001670 * Task affiliation for grouping with other tasks.
Winson Chungabfdcce2018-07-02 17:23:33 -07001671 *
1672 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, currently always 0.
Craig Mautnera228ae92014-07-09 05:44:55 -07001673 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001674 @Deprecated
Craig Mautnera228ae92014-07-09 05:44:55 -07001675 public int affiliatedTaskId;
1676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 public RecentTaskInfo() {
1678 }
1679
Winson Chungabfdcce2018-07-02 17:23:33 -07001680 private RecentTaskInfo(Parcel source) {
1681 readFromParcel(source);
1682 }
1683
Craig Mautner6d90fed2013-05-22 15:03:10 -07001684 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 public int describeContents() {
1686 return 0;
1687 }
1688
Winson Chungabfdcce2018-07-02 17:23:33 -07001689 public void readFromParcel(Parcel source) {
1690 id = source.readInt();
1691 persistentId = source.readInt();
1692 super.readFromParcel(source);
1693 }
1694
Craig Mautner6d90fed2013-05-22 15:03:10 -07001695 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 public void writeToParcel(Parcel dest, int flags) {
1697 dest.writeInt(id);
Dianne Hackbornd94df452011-02-16 18:53:31 -08001698 dest.writeInt(persistentId);
Winson Chungabfdcce2018-07-02 17:23:33 -07001699 super.writeToParcel(dest, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
Craig Mautner6d90fed2013-05-22 15:03:10 -07001701
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001702 public static final @android.annotation.NonNull Creator<RecentTaskInfo> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 = new Creator<RecentTaskInfo>() {
1704 public RecentTaskInfo createFromParcel(Parcel source) {
1705 return new RecentTaskInfo(source);
1706 }
1707 public RecentTaskInfo[] newArray(int size) {
1708 return new RecentTaskInfo[size];
1709 }
1710 };
Winson Chungb4132992018-07-03 15:52:38 -07001711
1712 /**
1713 * @hide
1714 */
1715 public void dump(PrintWriter pw, String indent) {
1716 final String activityType = WindowConfiguration.activityTypeToString(
1717 configuration.windowConfiguration.getActivityType());
1718 final String windowingMode = WindowConfiguration.activityTypeToString(
1719 configuration.windowConfiguration.getActivityType());
1720
1721 pw.println(); pw.print(" ");
Dianne Hackborn9106f2a2020-04-30 16:23:20 -07001722 pw.print(" id="); pw.print(persistentId);
1723 pw.print(" stackId="); pw.print(stackId);
1724 pw.print(" userId="); pw.print(userId);
1725 pw.print(" hasTask="); pw.print((id != -1));
1726 pw.print(" lastActiveTime="); pw.println(lastActiveTime);
1727 pw.print(" "); pw.print(" baseIntent="); pw.println(baseIntent);
1728 if (baseActivity != null) {
1729 pw.print(" "); pw.print(" baseActivity=");
1730 pw.println(baseActivity.toShortString());
1731 }
1732 if (topActivity != null) {
1733 pw.print(" "); pw.print(" topActivity="); pw.println(topActivity.toShortString());
1734 }
1735 if (origActivity != null) {
1736 pw.print(" "); pw.print(" origActivity=");
1737 pw.println(origActivity.toShortString());
1738 }
1739 if (realActivity != null) {
1740 pw.print(" "); pw.print(" realActivity=");
1741 pw.println(realActivity.toShortString());
1742 }
1743 pw.print(" ");
1744 pw.print(" isExcluded=");
1745 pw.print(((baseIntent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0));
1746 pw.print(" activityType="); pw.print(activityType);
1747 pw.print(" windowingMode="); pw.print(windowingMode);
1748 pw.print(" supportsSplitScreenMultiWindow=");
1749 pw.println(supportsSplitScreenMultiWindow);
Winson Chungb4132992018-07-03 15:52:38 -07001750 if (taskDescription != null) {
Dianne Hackborn9106f2a2020-04-30 16:23:20 -07001751 pw.print(" ");
Winson Chungb4132992018-07-03 15:52:38 -07001752 final ActivityManager.TaskDescription td = taskDescription;
1753 pw.print(" taskDescription {");
Dianne Hackborn9106f2a2020-04-30 16:23:20 -07001754 pw.print(" colorBackground=#");
1755 pw.print(Integer.toHexString(td.getBackgroundColor()));
1756 pw.print(" colorPrimary=#");
1757 pw.print(Integer.toHexString(td.getPrimaryColor()));
1758 pw.print(" iconRes=");
1759 pw.print(td.getIconResourcePackage() + "/" + td.getIconResource());
1760 pw.print(" iconBitmap=");
1761 pw.print(td.getIconFilename() != null || td.getInMemoryIcon() != null);
1762 pw.print(" resizeMode=");
1763 pw.print(ActivityInfo.resizeModeToString(td.getResizeMode()));
1764 pw.print(" minWidth="); pw.print(td.getMinWidth());
1765 pw.print(" minHeight="); pw.print(td.getMinHeight());
Winson Chungb4132992018-07-03 15:52:38 -07001766 pw.println(" }");
1767 }
1768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
1770
1771 /**
1772 * Flag for use with {@link #getRecentTasks}: return all tasks, even those
1773 * that have set their
1774 * {@link android.content.Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag.
1775 */
1776 public static final int RECENT_WITH_EXCLUDED = 0x0001;
Kenny Guy82326a92014-03-17 17:16:06 +00001777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 /**
Dianne Hackbornd2835932010-12-13 16:28:46 -08001779 * Provides a list that does not contain any
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07001780 * recent tasks that currently are not available to the user.
1781 */
1782 public static final int RECENT_IGNORE_UNAVAILABLE = 0x0002;
Dianne Hackbornd2835932010-12-13 16:28:46 -08001783
1784 /**
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001785 * <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 -08001786 * the most recent being first and older ones after in order.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001787 *
1788 * <p><b>Note: this method is only intended for debugging and presenting
1789 * task management user interfaces</b>. This should never be used for
1790 * core logic in an application, such as deciding between different
1791 * behaviors based on the information found here. Such uses are
1792 * <em>not</em> supported, and will likely break in the future. For
1793 * example, if multiple applications can be actively running at the
1794 * same time, assumptions made about the meaning of the data here for
1795 * purposes of control flow will be incorrect.</p>
1796 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001797 * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method is
Dianne Hackborn2d7576b2014-10-03 16:26:04 -07001798 * no longer available to third party applications: the introduction of
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001799 * document-centric recents means
1800 * it can leak personal information to the caller. For backwards compatibility,
1801 * it will still return a small subset of its data: at least the caller's
1802 * own tasks (though see {@link #getAppTasks()} for the correct supported
1803 * way to retrieve that information), and possibly some other tasks
1804 * such as home that are known to not be sensitive.
1805 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 * @param maxNum The maximum number of entries to return in the list. The
1807 * actual number returned may be smaller, depending on how many tasks the
1808 * user has started and the maximum number the system can remember.
Dianne Hackbornd2835932010-12-13 16:28:46 -08001809 * @param flags Information about what to return. May be any combination
Dianne Hackborn805fd7e2011-01-16 18:30:29 -08001810 * of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
Stefan Kuhne16045c22015-06-05 07:18:06 -07001811 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 * @return Returns a list of RecentTaskInfo records describing each of
1813 * the recent tasks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 */
Dianne Hackborna25a8b22014-05-30 12:30:35 -07001815 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 public List<RecentTaskInfo> getRecentTasks(int maxNum, int flags)
1817 throws SecurityException {
1818 try {
Winson Chungf067ac72017-10-24 17:32:32 -07001819 if (maxNum < 0) {
1820 throw new IllegalArgumentException("The requested number of tasks should be >= 0");
1821 }
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001822 return getTaskService().getRecentTasks(maxNum, flags, mContext.getUserId()).getList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001824 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 }
1826 }
1827
1828 /**
1829 * Information you can retrieve about a particular task that is currently
1830 * "running" in the system. Note that a running task does not mean the
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07001831 * given task actually has a process it is actively running in; it simply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 * means that the user has gone to it and never closed it, but currently
1833 * the system may have killed its process and is only holding on to its
1834 * last state in order to restart it when the user returns.
1835 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001836 public static class RunningTaskInfo extends TaskInfo implements Parcelable {
1837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 /**
1839 * A unique identifier for this task.
Winson Chungabfdcce2018-07-02 17:23:33 -07001840 *
1841 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, use
1842 * {@link RunningTaskInfo#taskId}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001844 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 public int id;
1846
1847 /**
Winson Chungabfdcce2018-07-02 17:23:33 -07001848 * Thumbnail representation of the task's current state.
1849 *
1850 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, currently always null.
Winson5510f6c2015-10-27 12:11:26 -07001851 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001852 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 public Bitmap thumbnail;
1854
1855 /**
1856 * Description of the task's current state.
Winson Chungabfdcce2018-07-02 17:23:33 -07001857 *
1858 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, currently always null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001860 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 public CharSequence description;
1862
1863 /**
Winson Chungabfdcce2018-07-02 17:23:33 -07001864 * Number of activities that are currently running (not stopped and persisted) in this task.
1865 *
1866 * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, currently always 0.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 */
Winson Chungabfdcce2018-07-02 17:23:33 -07001868 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 public int numRunning;
1870
1871 public RunningTaskInfo() {
1872 }
1873
Winson Chungabfdcce2018-07-02 17:23:33 -07001874 private RunningTaskInfo(Parcel source) {
1875 readFromParcel(source);
1876 }
1877
1878 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 public int describeContents() {
1880 return 0;
1881 }
1882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 public void readFromParcel(Parcel source) {
1884 id = source.readInt();
Winson Chungabfdcce2018-07-02 17:23:33 -07001885 super.readFromParcel(source);
1886 }
1887
1888 @Override
1889 public void writeToParcel(Parcel dest, int flags) {
1890 dest.writeInt(id);
1891 super.writeToParcel(dest, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07001893
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001894 public static final @android.annotation.NonNull Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 public RunningTaskInfo createFromParcel(Parcel source) {
1896 return new RunningTaskInfo(source);
1897 }
1898 public RunningTaskInfo[] newArray(int size) {
1899 return new RunningTaskInfo[size];
1900 }
1901 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
Winson Chung1147c402014-05-14 11:05:00 -07001903
1904 /**
1905 * Get the list of tasks associated with the calling application.
1906 *
1907 * @return The list of tasks associated with the application making this call.
1908 * @throws SecurityException
1909 */
1910 public List<ActivityManager.AppTask> getAppTasks() {
1911 ArrayList<AppTask> tasks = new ArrayList<AppTask>();
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07001912 List<IBinder> appTasks;
Winson Chung1147c402014-05-14 11:05:00 -07001913 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001914 appTasks = getTaskService().getAppTasks(mContext.getPackageName());
Winson Chung1147c402014-05-14 11:05:00 -07001915 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001916 throw e.rethrowFromSystemServer();
Winson Chung1147c402014-05-14 11:05:00 -07001917 }
1918 int numAppTasks = appTasks.size();
1919 for (int i = 0; i < numAppTasks; i++) {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07001920 tasks.add(new AppTask(IAppTask.Stub.asInterface(appTasks.get(i))));
Winson Chung1147c402014-05-14 11:05:00 -07001921 }
1922 return tasks;
1923 }
1924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 /**
Winson Chung48a10a52014-08-27 14:36:51 -07001926 * Return the current design dimensions for {@link AppTask} thumbnails, for use
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001927 * with {@link #addAppTask}.
1928 */
Winson Chung48a10a52014-08-27 14:36:51 -07001929 public Size getAppTaskThumbnailSize() {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001930 synchronized (this) {
1931 ensureAppTaskThumbnailSizeLocked();
Winson Chung48a10a52014-08-27 14:36:51 -07001932 return new Size(mAppTaskThumbnailSize.x, mAppTaskThumbnailSize.y);
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001933 }
1934 }
1935
1936 private void ensureAppTaskThumbnailSizeLocked() {
1937 if (mAppTaskThumbnailSize == null) {
1938 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001939 mAppTaskThumbnailSize = getTaskService().getAppTaskThumbnailSize();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001940 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001941 throw e.rethrowFromSystemServer();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001942 }
1943 }
1944 }
1945
1946 /**
1947 * Add a new {@link AppTask} for the calling application. This will create a new
1948 * recents entry that is added to the <b>end</b> of all existing recents.
1949 *
1950 * @param activity The activity that is adding the entry. This is used to help determine
1951 * the context that the new recents entry will be in.
1952 * @param intent The Intent that describes the recents entry. This is the same Intent that
1953 * you would have used to launch the activity for it. In generally you will want to set
1954 * both {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT} and
1955 * {@link Intent#FLAG_ACTIVITY_RETAIN_IN_RECENTS}; the latter is required since this recents
1956 * entry will exist without an activity, so it doesn't make sense to not retain it when
1957 * its activity disappears. The given Intent here also must have an explicit ComponentName
1958 * set on it.
1959 * @param description Optional additional description information.
1960 * @param thumbnail Thumbnail to use for the recents entry. Should be the size given by
Winson Chung48a10a52014-08-27 14:36:51 -07001961 * {@link #getAppTaskThumbnailSize()}. If the bitmap is not that exact size, it will be
1962 * recreated in your process, probably in a way you don't like, before the recents entry
1963 * is added.
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001964 *
1965 * @return Returns the task id of the newly added app task, or -1 if the add failed. The
1966 * most likely cause of failure is that there is no more room for more tasks for your app.
1967 */
1968 public int addAppTask(@NonNull Activity activity, @NonNull Intent intent,
1969 @Nullable TaskDescription description, @NonNull Bitmap thumbnail) {
1970 Point size;
1971 synchronized (this) {
1972 ensureAppTaskThumbnailSizeLocked();
1973 size = mAppTaskThumbnailSize;
1974 }
1975 final int tw = thumbnail.getWidth();
1976 final int th = thumbnail.getHeight();
1977 if (tw != size.x || th != size.y) {
1978 Bitmap bm = Bitmap.createBitmap(size.x, size.y, thumbnail.getConfig());
1979
1980 // Use ScaleType.CENTER_CROP, except we leave the top edge at the top.
1981 float scale;
1982 float dx = 0, dy = 0;
1983 if (tw * size.x > size.y * th) {
1984 scale = (float) size.x / (float) th;
1985 dx = (size.y - tw * scale) * 0.5f;
1986 } else {
1987 scale = (float) size.y / (float) tw;
1988 dy = (size.x - th * scale) * 0.5f;
1989 }
1990 Matrix matrix = new Matrix();
1991 matrix.setScale(scale, scale);
1992 matrix.postTranslate((int) (dx + 0.5f), 0);
1993
1994 Canvas canvas = new Canvas(bm);
1995 canvas.drawBitmap(thumbnail, matrix, null);
1996 canvas.setBitmap(null);
1997
1998 thumbnail = bm;
1999 }
2000 if (description == null) {
2001 description = new TaskDescription();
2002 }
2003 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002004 return getTaskService().addAppTask(activity.getActivityToken(),
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002005 intent, description, thumbnail);
2006 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002007 throw e.rethrowFromSystemServer();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07002008 }
2009 }
2010
2011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 * Return a list of the tasks that are currently running, with
2013 * the most recent being first and older ones after in order. Note that
2014 * "running" does not mean any of the task's code is currently loaded or
2015 * activity -- the task may have been frozen by the system, so that it
2016 * can be restarted in its previous state when next brought to the
2017 * foreground.
Jim Miller0b2a6d02010-07-13 18:01:29 -07002018 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002019 * <p><b>Note: this method is only intended for debugging and presenting
2020 * task management user interfaces</b>. This should never be used for
2021 * core logic in an application, such as deciding between different
2022 * behaviors based on the information found here. Such uses are
2023 * <em>not</em> supported, and will likely break in the future. For
2024 * example, if multiple applications can be actively running at the
2025 * same time, assumptions made about the meaning of the data here for
2026 * purposes of control flow will be incorrect.</p>
2027 *
Dianne Hackborn955d8d62014-10-07 20:17:19 -07002028 * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method
Dianne Hackborna25a8b22014-05-30 12:30:35 -07002029 * is no longer available to third party
2030 * applications: the introduction of document-centric recents means
2031 * it can leak person information to the caller. For backwards compatibility,
Alan Stokes1e4e74a2017-08-02 11:17:37 +01002032 * it will still return a small subset of its data: at least the caller's
Dianne Hackborna25a8b22014-05-30 12:30:35 -07002033 * own tasks, and possibly some other tasks
2034 * such as home that are known to not be sensitive.
2035 *
Jim Miller0b2a6d02010-07-13 18:01:29 -07002036 * @param maxNum The maximum number of entries to return in the list. The
2037 * actual number returned may be smaller, depending on how many tasks the
2038 * user has started.
2039 *
2040 * @return Returns a list of RunningTaskInfo records describing each of
2041 * the running tasks.
Jim Miller0b2a6d02010-07-13 18:01:29 -07002042 */
Dianne Hackborna25a8b22014-05-30 12:30:35 -07002043 @Deprecated
Jim Miller0b2a6d02010-07-13 18:01:29 -07002044 public List<RunningTaskInfo> getRunningTasks(int maxNum)
2045 throws SecurityException {
Dianne Hackborn09233282014-04-30 11:33:59 -07002046 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07002047 return getTaskService().getTasks(maxNum);
Dianne Hackborn09233282014-04-30 11:33:59 -07002048 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002049 throw e.rethrowFromSystemServer();
Dianne Hackborn09233282014-04-30 11:33:59 -07002050 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002051 }
2052
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002053 /**
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002054 * Represents a task snapshot.
2055 * @hide
2056 */
2057 public static class TaskSnapshot implements Parcelable {
Hongwei Wang3b9bdcf2019-07-15 10:23:03 -07002058 // Identifier of this snapshot
2059 private final long mId;
Winson Chung48b25652018-10-22 14:04:30 -07002060 // Top activity in task when snapshot was taken
2061 private final ComponentName mTopActivityComponent;
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002062 private final GraphicBuffer mSnapshot;
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002063 /** Indicates whether task was in landscape or portrait */
2064 @Configuration.Orientation
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002065 private final int mOrientation;
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002066 /** See {@link android.view.Surface.Rotation} */
2067 @Surface.Rotation
2068 private int mRotation;
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002069 /** The size of the snapshot before scaling */
2070 private final Point mTaskSize;
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002071 private final Rect mContentInsets;
Peter Kalauskas84f02a82020-01-22 16:06:18 -08002072 // Whether this snapshot is a down-sampled version of the high resolution snapshot, used
2073 // mainly for loading snapshots quickly from disk when user is flinging fast
Peter Kalauskas4dc04602020-02-12 18:49:03 -08002074 private final boolean mIsLowResolution;
Winson Chunga4fa8d52018-04-20 15:54:51 -07002075 // Whether or not the snapshot is a real snapshot or an app-theme generated snapshot due to
2076 // the task having a secure window or having previews disabled
Winson Chungf3e412e2018-03-08 11:07:40 -08002077 private final boolean mIsRealSnapshot;
Winson Chunga4fa8d52018-04-20 15:54:51 -07002078 private final int mWindowingMode;
Winson Chung173020c2018-05-04 15:36:47 -07002079 private final int mSystemUiVisibility;
2080 private final boolean mIsTranslucent;
Peiyong Lin21e499a2019-04-03 16:37:46 -07002081 // Must be one of the named color spaces, otherwise, always use SRGB color space.
2082 private final ColorSpace mColorSpace;
Peiyong Lin9d427402019-01-23 18:39:06 -08002083
Hongwei Wang3b9bdcf2019-07-15 10:23:03 -07002084 public TaskSnapshot(long id,
2085 @NonNull ComponentName topActivityComponent, GraphicBuffer snapshot,
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002086 @NonNull ColorSpace colorSpace, int orientation, int rotation, Point taskSize,
2087 Rect contentInsets, boolean isLowResolution, boolean isRealSnapshot,
2088 int windowingMode, int systemUiVisibility, boolean isTranslucent) {
Hongwei Wang3b9bdcf2019-07-15 10:23:03 -07002089 mId = id;
Winson Chung48b25652018-10-22 14:04:30 -07002090 mTopActivityComponent = topActivityComponent;
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002091 mSnapshot = snapshot;
Peiyong Lin21e499a2019-04-03 16:37:46 -07002092 mColorSpace = colorSpace.getId() < 0
2093 ? ColorSpace.get(ColorSpace.Named.SRGB) : colorSpace;
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002094 mOrientation = orientation;
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002095 mRotation = rotation;
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002096 mTaskSize = new Point(taskSize);
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002097 mContentInsets = new Rect(contentInsets);
Peter Kalauskas4dc04602020-02-12 18:49:03 -08002098 mIsLowResolution = isLowResolution;
Winson Chungf3e412e2018-03-08 11:07:40 -08002099 mIsRealSnapshot = isRealSnapshot;
Winson Chunga4fa8d52018-04-20 15:54:51 -07002100 mWindowingMode = windowingMode;
Winson Chung173020c2018-05-04 15:36:47 -07002101 mSystemUiVisibility = systemUiVisibility;
2102 mIsTranslucent = isTranslucent;
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002103 }
2104
2105 private TaskSnapshot(Parcel source) {
Hongwei Wang3b9bdcf2019-07-15 10:23:03 -07002106 mId = source.readLong();
Winson Chung48b25652018-10-22 14:04:30 -07002107 mTopActivityComponent = ComponentName.readFromParcel(source);
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002108 mSnapshot = source.readParcelable(null /* classLoader */);
Peiyong Lin21e499a2019-04-03 16:37:46 -07002109 int colorSpaceId = source.readInt();
Peiyong Lind088e6b2019-04-16 14:04:43 -07002110 mColorSpace = colorSpaceId >= 0 && colorSpaceId < ColorSpace.Named.values().length
Peiyong Lin21e499a2019-04-03 16:37:46 -07002111 ? ColorSpace.get(ColorSpace.Named.values()[colorSpaceId])
2112 : ColorSpace.get(ColorSpace.Named.SRGB);
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002113 mOrientation = source.readInt();
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002114 mRotation = source.readInt();
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002115 mTaskSize = source.readParcelable(null /* classLoader */);
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002116 mContentInsets = source.readParcelable(null /* classLoader */);
Peter Kalauskas4dc04602020-02-12 18:49:03 -08002117 mIsLowResolution = source.readBoolean();
Winson Chungf3e412e2018-03-08 11:07:40 -08002118 mIsRealSnapshot = source.readBoolean();
Winson Chunga4fa8d52018-04-20 15:54:51 -07002119 mWindowingMode = source.readInt();
Winson Chung173020c2018-05-04 15:36:47 -07002120 mSystemUiVisibility = source.readInt();
2121 mIsTranslucent = source.readBoolean();
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002122 }
2123
Jorim Jaggi35e3f532017-03-17 17:06:50 +01002124 /**
Hongwei Wang3b9bdcf2019-07-15 10:23:03 -07002125 * @return Identifier of this snapshot.
2126 */
2127 public long getId() {
2128 return mId;
2129 }
2130
2131 /**
Winson Chung48b25652018-10-22 14:04:30 -07002132 * @return The top activity component for the task at the point this snapshot was taken.
2133 */
2134 public ComponentName getTopActivityComponent() {
2135 return mTopActivityComponent;
2136 }
2137
2138 /**
Jorim Jaggi35e3f532017-03-17 17:06:50 +01002139 * @return The graphic buffer representing the screenshot.
2140 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002141 @UnsupportedAppUsage
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002142 public GraphicBuffer getSnapshot() {
2143 return mSnapshot;
2144 }
2145
Jorim Jaggi35e3f532017-03-17 17:06:50 +01002146 /**
Peiyong Lin9d427402019-01-23 18:39:06 -08002147 * @return The color space of graphic buffer representing the screenshot.
2148 */
2149 public ColorSpace getColorSpace() {
2150 return mColorSpace;
2151 }
2152
2153 /**
Jorim Jaggi35e3f532017-03-17 17:06:50 +01002154 * @return The screen orientation the screenshot was taken in.
2155 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002156 @UnsupportedAppUsage
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002157 public int getOrientation() {
2158 return mOrientation;
2159 }
2160
Jorim Jaggi35e3f532017-03-17 17:06:50 +01002161 /**
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002162 * @return The screen rotation the screenshot was taken in.
2163 */
2164 public int getRotation() {
2165 return mRotation;
2166 }
2167
2168 /**
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002169 * @return The size of the task at the point this snapshot was taken.
2170 */
2171 @UnsupportedAppUsage
2172 public Point getTaskSize() {
2173 return mTaskSize;
2174 }
2175
2176 /**
Jorim Jaggi35e3f532017-03-17 17:06:50 +01002177 * @return The system/content insets on the snapshot. These can be clipped off in order to
2178 * remove any areas behind system bars in the snapshot.
2179 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002180 @UnsupportedAppUsage
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002181 public Rect getContentInsets() {
2182 return mContentInsets;
2183 }
2184
Jorim Jaggi35e3f532017-03-17 17:06:50 +01002185 /**
2186 * @return Whether this snapshot is a down-sampled version of the full resolution.
2187 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002188 @UnsupportedAppUsage
Peter Kalauskas4dc04602020-02-12 18:49:03 -08002189 public boolean isLowResolution() {
2190 return mIsLowResolution;
Jorim Jaggi35e3f532017-03-17 17:06:50 +01002191 }
2192
2193 /**
Winson Chungf3e412e2018-03-08 11:07:40 -08002194 * @return Whether or not the snapshot is a real snapshot or an app-theme generated snapshot
2195 * due to the task having a secure window or having previews disabled.
2196 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002197 @UnsupportedAppUsage
Winson Chungf3e412e2018-03-08 11:07:40 -08002198 public boolean isRealSnapshot() {
2199 return mIsRealSnapshot;
2200 }
2201
2202 /**
Winson Chung02378b42018-06-04 16:40:09 -07002203 * @return Whether or not the snapshot is of a translucent app window (non-fullscreen or has
2204 * a non-opaque pixel format).
Winson Chung173020c2018-05-04 15:36:47 -07002205 */
2206 public boolean isTranslucent() {
2207 return mIsTranslucent;
2208 }
2209
2210 /**
Winson Chunga4fa8d52018-04-20 15:54:51 -07002211 * @return The windowing mode of the task when this snapshot was taken.
2212 */
2213 public int getWindowingMode() {
2214 return mWindowingMode;
2215 }
2216
2217 /**
Winson Chung173020c2018-05-04 15:36:47 -07002218 * @return The system ui visibility flags for the top most visible fullscreen window at the
2219 * time that the snapshot was taken.
2220 */
2221 public int getSystemUiVisibility() {
2222 return mSystemUiVisibility;
2223 }
2224
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002225 @Override
2226 public int describeContents() {
2227 return 0;
2228 }
2229
2230 @Override
2231 public void writeToParcel(Parcel dest, int flags) {
Hongwei Wang3b9bdcf2019-07-15 10:23:03 -07002232 dest.writeLong(mId);
Winson Chung48b25652018-10-22 14:04:30 -07002233 ComponentName.writeToParcel(mTopActivityComponent, dest);
Winson Chung5bd2a152020-05-12 14:47:13 -07002234 dest.writeParcelable(mSnapshot != null && !mSnapshot.isDestroyed() ? mSnapshot : null,
2235 0);
Peiyong Lin21e499a2019-04-03 16:37:46 -07002236 dest.writeInt(mColorSpace.getId());
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002237 dest.writeInt(mOrientation);
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002238 dest.writeInt(mRotation);
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002239 dest.writeParcelable(mTaskSize, 0);
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002240 dest.writeParcelable(mContentInsets, 0);
Peter Kalauskas4dc04602020-02-12 18:49:03 -08002241 dest.writeBoolean(mIsLowResolution);
Winson Chungf3e412e2018-03-08 11:07:40 -08002242 dest.writeBoolean(mIsRealSnapshot);
Winson Chunga4fa8d52018-04-20 15:54:51 -07002243 dest.writeInt(mWindowingMode);
Winson Chung173020c2018-05-04 15:36:47 -07002244 dest.writeInt(mSystemUiVisibility);
2245 dest.writeBoolean(mIsTranslucent);
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002246 }
2247
Jorim Jaggi10abe2f2017-01-03 16:44:46 +01002248 @Override
2249 public String toString() {
Winson Chunga4fa8d52018-04-20 15:54:51 -07002250 final int width = mSnapshot != null ? mSnapshot.getWidth() : 0;
2251 final int height = mSnapshot != null ? mSnapshot.getHeight() : 0;
Winson Chung48b25652018-10-22 14:04:30 -07002252 return "TaskSnapshot{"
Hongwei Wang3b9bdcf2019-07-15 10:23:03 -07002253 + " mId=" + mId
Winson Chung48b25652018-10-22 14:04:30 -07002254 + " mTopActivityComponent=" + mTopActivityComponent.flattenToShortString()
2255 + " mSnapshot=" + mSnapshot + " (" + width + "x" + height + ")"
Peiyong Lin21e499a2019-04-03 16:37:46 -07002256 + " mColorSpace=" + mColorSpace.toString()
Winson Chunga4fa8d52018-04-20 15:54:51 -07002257 + " mOrientation=" + mOrientation
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002258 + " mRotation=" + mRotation
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002259 + " mTaskSize=" + mTaskSize.toString()
Jorim Jaggi35e3f532017-03-17 17:06:50 +01002260 + " mContentInsets=" + mContentInsets.toShortString()
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002261 + " mIsLowResolution=" + mIsLowResolution
2262 + " mIsRealSnapshot=" + mIsRealSnapshot
2263 + " mWindowingMode=" + mWindowingMode
Winson Chung173020c2018-05-04 15:36:47 -07002264 + " mSystemUiVisibility=" + mSystemUiVisibility
2265 + " mIsTranslucent=" + mIsTranslucent;
Jorim Jaggi10abe2f2017-01-03 16:44:46 +01002266 }
2267
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002268 public static final @android.annotation.NonNull Creator<TaskSnapshot> CREATOR = new Creator<TaskSnapshot>() {
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002269 public TaskSnapshot createFromParcel(Parcel source) {
2270 return new TaskSnapshot(source);
2271 }
2272 public TaskSnapshot[] newArray(int size) {
2273 return new TaskSnapshot[size];
2274 }
2275 };
Mark Renouf98ba8e62019-11-18 12:42:16 -05002276
2277 /** Builder for a {@link TaskSnapshot} object */
2278 public static final class Builder {
2279 private long mId;
2280 private ComponentName mTopActivity;
2281 private GraphicBuffer mSnapshot;
2282 private ColorSpace mColorSpace;
2283 private int mOrientation;
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002284 private int mRotation;
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002285 private Point mTaskSize;
Mark Renouf98ba8e62019-11-18 12:42:16 -05002286 private Rect mContentInsets;
Mark Renouf98ba8e62019-11-18 12:42:16 -05002287 private boolean mIsRealSnapshot;
2288 private int mWindowingMode;
2289 private int mSystemUiVisibility;
2290 private boolean mIsTranslucent;
2291 private int mPixelFormat;
2292
2293 public Builder setId(long id) {
2294 mId = id;
2295 return this;
2296 }
2297
2298 public Builder setTopActivityComponent(ComponentName name) {
2299 mTopActivity = name;
2300 return this;
2301 }
2302
2303 public Builder setSnapshot(GraphicBuffer buffer) {
2304 mSnapshot = buffer;
2305 return this;
2306 }
2307
2308 public Builder setColorSpace(ColorSpace colorSpace) {
2309 mColorSpace = colorSpace;
2310 return this;
2311 }
2312
2313 public Builder setOrientation(int orientation) {
2314 mOrientation = orientation;
2315 return this;
2316 }
2317
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002318 public Builder setRotation(int rotation) {
2319 mRotation = rotation;
2320 return this;
2321 }
2322
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002323 /**
2324 * Sets the original size of the task
2325 */
2326 public Builder setTaskSize(Point size) {
2327 mTaskSize = size;
2328 return this;
2329 }
2330
Mark Renouf98ba8e62019-11-18 12:42:16 -05002331 public Builder setContentInsets(Rect contentInsets) {
2332 mContentInsets = contentInsets;
2333 return this;
2334 }
2335
Mark Renouf98ba8e62019-11-18 12:42:16 -05002336 public Builder setIsRealSnapshot(boolean realSnapshot) {
2337 mIsRealSnapshot = realSnapshot;
2338 return this;
2339 }
2340
2341 public Builder setWindowingMode(int windowingMode) {
2342 mWindowingMode = windowingMode;
2343 return this;
2344 }
2345
2346 public Builder setSystemUiVisibility(int systemUiVisibility) {
2347 mSystemUiVisibility = systemUiVisibility;
2348 return this;
2349 }
2350
2351 public Builder setIsTranslucent(boolean isTranslucent) {
2352 mIsTranslucent = isTranslucent;
2353 return this;
2354 }
2355
2356 public int getPixelFormat() {
2357 return mPixelFormat;
2358 }
2359
2360 public Builder setPixelFormat(int pixelFormat) {
2361 mPixelFormat = pixelFormat;
2362 return this;
2363 }
2364
2365 public TaskSnapshot build() {
2366 return new TaskSnapshot(
2367 mId,
2368 mTopActivity,
2369 mSnapshot,
2370 mColorSpace,
2371 mOrientation,
Vinit Nayakffd9dff2019-11-05 15:20:11 -08002372 mRotation,
Peter Kalauskasfe0a4132020-01-31 16:50:42 -08002373 mTaskSize,
Mark Renouf98ba8e62019-11-18 12:42:16 -05002374 mContentInsets,
Peter Kalauskas84f02a82020-01-22 16:06:18 -08002375 // When building a TaskSnapshot with the Builder class, isLowResolution
2376 // is always false. Low-res snapshots are only created when loading from
2377 // disk.
2378 false /* isLowResolution */,
Mark Renouf98ba8e62019-11-18 12:42:16 -05002379 mIsRealSnapshot,
2380 mWindowingMode,
2381 mSystemUiVisibility,
2382 mIsTranslucent);
2383
2384 }
2385 }
Jorim Jaggie2c77f92016-12-29 14:57:22 +01002386 }
2387
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002388 /** @hide */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002389 @IntDef(flag = true, prefix = { "MOVE_TASK_" }, value = {
2390 MOVE_TASK_WITH_HOME,
2391 MOVE_TASK_NO_USER_ACTION,
2392 })
2393 @Retention(RetentionPolicy.SOURCE)
2394 public @interface MoveTaskFlags {}
2395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002397 * Flag for {@link #moveTaskToFront(int, int)}: also move the "home"
2398 * activity along with the task, so it is positioned immediately behind
2399 * the task.
2400 */
2401 public static final int MOVE_TASK_WITH_HOME = 0x00000001;
2402
2403 /**
Dianne Hackbornd94df452011-02-16 18:53:31 -08002404 * Flag for {@link #moveTaskToFront(int, int)}: don't count this as a
2405 * user-instigated action, so the current activity will not receive a
2406 * hint that the user is leaving.
2407 */
2408 public static final int MOVE_TASK_NO_USER_ACTION = 0x00000002;
2409
2410 /**
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002411 * Equivalent to calling {@link #moveTaskToFront(int, int, Bundle)}
2412 * with a null options argument.
2413 *
2414 * @param taskId The identifier of the task to be moved, as found in
2415 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002416 * @param flags Additional operational flags.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002417 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002418 @RequiresPermission(android.Manifest.permission.REORDER_TASKS)
2419 public void moveTaskToFront(int taskId, @MoveTaskFlags int flags) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002420 moveTaskToFront(taskId, flags, null);
2421 }
2422
2423 /**
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002424 * Ask that the task associated with a given task ID be moved to the
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002425 * front of the stack, so it is now visible to the user.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002426 *
2427 * @param taskId The identifier of the task to be moved, as found in
2428 * {@link RunningTaskInfo} or {@link RecentTaskInfo}.
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002429 * @param flags Additional operational flags.
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002430 * @param options Additional options for the operation, either null or
2431 * as per {@link Context#startActivity(Intent, android.os.Bundle)
2432 * Context.startActivity(Intent, Bundle)}.
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002433 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002434 @RequiresPermission(android.Manifest.permission.REORDER_TASKS)
2435 public void moveTaskToFront(int taskId, @MoveTaskFlags int flags, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002436 try {
Ricky Waiaca8a772019-04-04 16:01:06 +01002437 ActivityThread thread = ActivityThread.currentActivityThread();
2438 IApplicationThread appThread = thread.getApplicationThread();
2439 String packageName = mContext.getPackageName();
2440 getTaskService().moveTaskToFront(appThread, packageName, taskId, flags, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002441 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002442 throw e.rethrowFromSystemServer();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002443 }
2444 }
2445
2446 /**
Andrii Kulian2eb84b22018-12-13 18:18:54 -08002447 * Check if the context is allowed to start an activity on specified display. Some launch
2448 * restrictions may apply to secondary displays that are private, virtual, or owned by the
2449 * system, in which case an activity start may throw a {@link SecurityException}. Call this
2450 * method prior to starting an activity on a secondary display to check if the current context
2451 * has access to it.
2452 *
2453 * @see ActivityOptions#setLaunchDisplayId(int)
Andrew Sapperstein8fe35e52020-04-28 12:29:20 -07002454 * @see android.view.Display#FLAG_PRIVATE
Andrii Kulian2eb84b22018-12-13 18:18:54 -08002455 *
2456 * @param context Source context, from which an activity will be started.
2457 * @param displayId Target display id.
2458 * @param intent Intent used to launch an activity.
2459 * @return {@code true} if a call to start an activity on the target display is allowed for the
2460 * provided context and no {@link SecurityException} will be thrown, {@code false} otherwise.
2461 */
wilsonshih40285442019-03-08 15:09:53 +08002462 public boolean isActivityStartAllowedOnDisplay(@NonNull Context context, int displayId,
2463 @NonNull Intent intent) {
Andrii Kulian2eb84b22018-12-13 18:18:54 -08002464 try {
2465 return getTaskService().isActivityStartAllowedOnDisplay(displayId, intent,
2466 intent.resolveTypeIfNeeded(context.getContentResolver()), context.getUserId());
2467 } catch (RemoteException e) {
Charles Chencfec9182019-02-12 20:58:12 +08002468 e.rethrowFromSystemServer();
Andrii Kulian2eb84b22018-12-13 18:18:54 -08002469 }
Charles Chencfec9182019-02-12 20:58:12 +08002470 return false;
Andrii Kulian2eb84b22018-12-13 18:18:54 -08002471 }
2472
2473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 * Information you can retrieve about a particular Service that is
2475 * currently running in the system.
2476 */
2477 public static class RunningServiceInfo implements Parcelable {
2478 /**
2479 * The service component.
2480 */
2481 public ComponentName service;
2482
2483 /**
2484 * If non-zero, this is the process the service is running in.
2485 */
2486 public int pid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002489 * The UID that owns this service.
2490 */
2491 public int uid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002492
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 * The name of the process this service runs in.
2495 */
2496 public String process;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 /**
2499 * Set to true if the service has asked to run as a foreground process.
2500 */
2501 public boolean foreground;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002504 * The time when the service was first made active, either by someone
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002505 * starting or binding to it. This
2506 * is in units of {@link android.os.SystemClock#elapsedRealtime()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 */
2508 public long activeSince;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 /**
2511 * Set to true if this service has been explicitly started.
2512 */
2513 public boolean started;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 /**
2516 * Number of clients connected to the service.
2517 */
2518 public int clientCount;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 /**
2521 * Number of times the service's process has crashed while the service
2522 * is running.
2523 */
2524 public int crashCount;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 /**
2527 * The time when there was last activity in the service (either
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002528 * explicit requests to start it or clients binding to it). This
2529 * is in units of {@link android.os.SystemClock#uptimeMillis()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 */
2531 public long lastActivityTime;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 /**
2534 * If non-zero, this service is not currently running, but scheduled to
2535 * restart at the given time.
2536 */
2537 public long restarting;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002538
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002539 /**
2540 * Bit for {@link #flags}: set if this service has been
2541 * explicitly started.
2542 */
2543 public static final int FLAG_STARTED = 1<<0;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002544
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002545 /**
2546 * Bit for {@link #flags}: set if the service has asked to
2547 * run as a foreground process.
2548 */
2549 public static final int FLAG_FOREGROUND = 1<<1;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002550
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002551 /**
Elliot Waite54de7742017-01-11 15:30:35 -08002552 * Bit for {@link #flags}: set if the service is running in a
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002553 * core system process.
2554 */
2555 public static final int FLAG_SYSTEM_PROCESS = 1<<2;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002556
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002557 /**
Elliot Waite54de7742017-01-11 15:30:35 -08002558 * Bit for {@link #flags}: set if the service is running in a
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002559 * persistent process.
2560 */
2561 public static final int FLAG_PERSISTENT_PROCESS = 1<<3;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002562
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002563 /**
2564 * Running flags.
2565 */
2566 public int flags;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002567
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002568 /**
2569 * For special services that are bound to by system code, this is
2570 * the package that holds the binding.
2571 */
2572 public String clientPackage;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002573
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002574 /**
2575 * For special services that are bound to by system code, this is
2576 * a string resource providing a user-visible label for who the
2577 * client is.
2578 */
2579 public int clientLabel;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 public RunningServiceInfo() {
2582 }
2583
2584 public int describeContents() {
2585 return 0;
2586 }
2587
2588 public void writeToParcel(Parcel dest, int flags) {
2589 ComponentName.writeToParcel(service, dest);
2590 dest.writeInt(pid);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002591 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 dest.writeString(process);
2593 dest.writeInt(foreground ? 1 : 0);
2594 dest.writeLong(activeSince);
2595 dest.writeInt(started ? 1 : 0);
2596 dest.writeInt(clientCount);
2597 dest.writeInt(crashCount);
2598 dest.writeLong(lastActivityTime);
2599 dest.writeLong(restarting);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002600 dest.writeInt(this.flags);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002601 dest.writeString(clientPackage);
2602 dest.writeInt(clientLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 }
2604
2605 public void readFromParcel(Parcel source) {
2606 service = ComponentName.readFromParcel(source);
2607 pid = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002608 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 process = source.readString();
2610 foreground = source.readInt() != 0;
2611 activeSince = source.readLong();
2612 started = source.readInt() != 0;
2613 clientCount = source.readInt();
2614 crashCount = source.readInt();
2615 lastActivityTime = source.readLong();
2616 restarting = source.readLong();
Dianne Hackborn3025ef32009-08-31 21:31:47 -07002617 flags = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002618 clientPackage = source.readString();
2619 clientLabel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002621
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002622 public static final @android.annotation.NonNull Creator<RunningServiceInfo> CREATOR = new Creator<RunningServiceInfo>() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 public RunningServiceInfo createFromParcel(Parcel source) {
2624 return new RunningServiceInfo(source);
2625 }
2626 public RunningServiceInfo[] newArray(int size) {
2627 return new RunningServiceInfo[size];
2628 }
2629 };
2630
2631 private RunningServiceInfo(Parcel source) {
2632 readFromParcel(source);
2633 }
2634 }
2635
2636 /**
2637 * Return a list of the services that are currently running.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002638 *
2639 * <p><b>Note: this method is only intended for debugging or implementing
2640 * service management type user interfaces.</b></p>
2641 *
Bryce Leef17e1d1a42017-04-17 15:52:50 -07002642 * @deprecated As of {@link android.os.Build.VERSION_CODES#O}, this method
2643 * is no longer available to third party applications. For backwards compatibility,
2644 * it will still return the caller's own services.
2645 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 * @param maxNum The maximum number of entries to return in the list. The
2647 * actual number returned may be smaller, depending on how many services
2648 * are running.
Stefan Kuhne16045c22015-06-05 07:18:06 -07002649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 * @return Returns a list of RunningServiceInfo records describing each of
2651 * the running tasks.
2652 */
Bryce Leef17e1d1a42017-04-17 15:52:50 -07002653 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 public List<RunningServiceInfo> getRunningServices(int maxNum)
2655 throws SecurityException {
2656 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002657 return getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 .getServices(maxNum, 0);
2659 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002660 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 }
2662 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 /**
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002665 * Returns a PendingIntent you can start to show a control panel for the
2666 * given running service. If the service does not have a control panel,
2667 * null is returned.
2668 */
2669 public PendingIntent getRunningServiceControlPanel(ComponentName service)
2670 throws SecurityException {
2671 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002672 return getService()
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002673 .getRunningServiceControlPanel(service);
2674 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002675 throw e.rethrowFromSystemServer();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002676 }
2677 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002678
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 * Information you can retrieve about the available memory through
2681 * {@link ActivityManager#getMemoryInfo}.
2682 */
2683 public static class MemoryInfo implements Parcelable {
2684 /**
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002685 * The available memory on the system. This number should not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 * be considered absolute: due to the nature of the kernel, a significant
2687 * portion of this memory is actually in use and needed for the overall
2688 * system to run well.
2689 */
2690 public long availMem;
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002691
2692 /**
2693 * The total memory accessible by the kernel. This is basically the
2694 * RAM size of the device, not including below-kernel fixed allocations
2695 * like DMA buffers, RAM for the baseband CPU, etc.
2696 */
2697 public long totalMem;
2698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 /**
2700 * The threshold of {@link #availMem} at which we consider memory to be
2701 * low and start killing background services and other non-extraneous
2702 * processes.
2703 */
2704 public long threshold;
Stefan Kuhne16045c22015-06-05 07:18:06 -07002705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 /**
2707 * Set to true if the system considers itself to currently be in a low
2708 * memory situation.
2709 */
2710 public boolean lowMemory;
2711
Dianne Hackborn7d608422011-08-07 16:24:18 -07002712 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002713 @UnsupportedAppUsage
Dianne Hackborn7d608422011-08-07 16:24:18 -07002714 public long hiddenAppThreshold;
2715 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002716 @UnsupportedAppUsage
Dianne Hackborn7d608422011-08-07 16:24:18 -07002717 public long secondaryServerThreshold;
2718 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002719 @UnsupportedAppUsage
Dianne Hackborn7d608422011-08-07 16:24:18 -07002720 public long visibleAppThreshold;
2721 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002722 @UnsupportedAppUsage
Dianne Hackborn7d608422011-08-07 16:24:18 -07002723 public long foregroundAppThreshold;
2724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 public MemoryInfo() {
2726 }
2727
2728 public int describeContents() {
2729 return 0;
2730 }
2731
2732 public void writeToParcel(Parcel dest, int flags) {
2733 dest.writeLong(availMem);
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002734 dest.writeLong(totalMem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 dest.writeLong(threshold);
2736 dest.writeInt(lowMemory ? 1 : 0);
Dianne Hackborn7d608422011-08-07 16:24:18 -07002737 dest.writeLong(hiddenAppThreshold);
2738 dest.writeLong(secondaryServerThreshold);
2739 dest.writeLong(visibleAppThreshold);
2740 dest.writeLong(foregroundAppThreshold);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07002742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 public void readFromParcel(Parcel source) {
2744 availMem = source.readLong();
Dianne Hackborn59325eb2012-05-09 18:45:20 -07002745 totalMem = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 threshold = source.readLong();
2747 lowMemory = source.readInt() != 0;
Dianne Hackborn7d608422011-08-07 16:24:18 -07002748 hiddenAppThreshold = source.readLong();
2749 secondaryServerThreshold = source.readLong();
2750 visibleAppThreshold = source.readLong();
2751 foregroundAppThreshold = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 }
2753
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002754 public static final @android.annotation.NonNull Creator<MemoryInfo> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 = new Creator<MemoryInfo>() {
2756 public MemoryInfo createFromParcel(Parcel source) {
2757 return new MemoryInfo(source);
2758 }
2759 public MemoryInfo[] newArray(int size) {
2760 return new MemoryInfo[size];
2761 }
2762 };
2763
2764 private MemoryInfo(Parcel source) {
2765 readFromParcel(source);
2766 }
2767 }
2768
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07002769 /**
2770 * Return general information about the memory state of the system. This
2771 * can be used to help decide how to manage your own memory, though note
2772 * that polling is not recommended and
2773 * {@link android.content.ComponentCallbacks2#onTrimMemory(int)
2774 * ComponentCallbacks2.onTrimMemory(int)} is the preferred way to do this.
2775 * Also see {@link #getMyMemoryState} for how to retrieve the current trim
2776 * level of your process as needed, which gives a better hint for how to
2777 * manage its memory.
2778 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 public void getMemoryInfo(MemoryInfo outInfo) {
2780 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002781 getService().getMemoryInfo(outInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002783 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 }
2785 }
Craig Mautner967212c2013-04-13 21:10:58 -07002786
Craig Mautner5ff12102013-05-24 12:50:15 -07002787 /**
Craig Mautner967212c2013-04-13 21:10:58 -07002788 * Information you can retrieve about an ActivityStack in the system.
2789 * @hide
2790 */
2791 public static class StackInfo implements Parcelable {
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002792 @UnsupportedAppUsage
Craig Mautner967212c2013-04-13 21:10:58 -07002793 public int stackId;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002794 @UnsupportedAppUsage
Craig Mautnered6649f2013-12-02 14:08:25 -08002795 public Rect bounds = new Rect();
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002796 @UnsupportedAppUsage
Craig Mautner967212c2013-04-13 21:10:58 -07002797 public int[] taskIds;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002798 @UnsupportedAppUsage
Craig Mautner967212c2013-04-13 21:10:58 -07002799 public String[] taskNames;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002800 @UnsupportedAppUsage
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002801 public Rect[] taskBounds;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002802 @UnsupportedAppUsage
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002803 public int[] taskUserIds;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002804 @UnsupportedAppUsage
Winsond46b7272016-04-20 11:54:27 -07002805 public ComponentName topActivity;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002806 @UnsupportedAppUsage
Craig Mautnered6649f2013-12-02 14:08:25 -08002807 public int displayId;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002808 @UnsupportedAppUsage
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002809 public int userId;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002810 @UnsupportedAppUsage
Winsond46b7272016-04-20 11:54:27 -07002811 public boolean visible;
Winson529c8e42016-05-17 11:08:40 -07002812 // Index of the stack in the display's stack list, can be used for comparison of stack order
Wale Ogunwale0db64ac2020-04-11 10:00:42 -07002813 // TODO: Can be removed since no one is using it.
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002814 @UnsupportedAppUsage
Wale Ogunwale0db64ac2020-04-11 10:00:42 -07002815 @Deprecated
Winson529c8e42016-05-17 11:08:40 -07002816 public int position;
Wale Ogunwaleadf116e2020-03-27 16:36:01 -07002817 public WindowContainerToken stackToken;
Wale Ogunwale68278562017-09-23 17:13:55 -07002818 /**
2819 * The full configuration the stack is currently running in.
2820 * @hide
2821 */
Wale Ogunwale89be5762017-10-04 13:27:49 -07002822 final public Configuration configuration = new Configuration();
Craig Mautner967212c2013-04-13 21:10:58 -07002823
Craig Mautner967212c2013-04-13 21:10:58 -07002824 @Override
2825 public int describeContents() {
2826 return 0;
2827 }
2828
2829 @Override
2830 public void writeToParcel(Parcel dest, int flags) {
2831 dest.writeInt(stackId);
2832 dest.writeInt(bounds.left);
2833 dest.writeInt(bounds.top);
2834 dest.writeInt(bounds.right);
2835 dest.writeInt(bounds.bottom);
2836 dest.writeIntArray(taskIds);
2837 dest.writeStringArray(taskNames);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002838 final int boundsCount = taskBounds == null ? 0 : taskBounds.length;
2839 dest.writeInt(boundsCount);
2840 for (int i = 0; i < boundsCount; i++) {
2841 dest.writeInt(taskBounds[i].left);
2842 dest.writeInt(taskBounds[i].top);
2843 dest.writeInt(taskBounds[i].right);
2844 dest.writeInt(taskBounds[i].bottom);
2845 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002846 dest.writeIntArray(taskUserIds);
Craig Mautnered6649f2013-12-02 14:08:25 -08002847 dest.writeInt(displayId);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002848 dest.writeInt(userId);
Winsond46b7272016-04-20 11:54:27 -07002849 dest.writeInt(visible ? 1 : 0);
Winson529c8e42016-05-17 11:08:40 -07002850 dest.writeInt(position);
Wale Ogunwaleadf116e2020-03-27 16:36:01 -07002851 stackToken.writeToParcel(dest, 0);
Winsond46b7272016-04-20 11:54:27 -07002852 if (topActivity != null) {
2853 dest.writeInt(1);
2854 topActivity.writeToParcel(dest, 0);
2855 } else {
2856 dest.writeInt(0);
2857 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002858 configuration.writeToParcel(dest, flags);
Craig Mautner967212c2013-04-13 21:10:58 -07002859 }
2860
2861 public void readFromParcel(Parcel source) {
2862 stackId = source.readInt();
2863 bounds = new Rect(
2864 source.readInt(), source.readInt(), source.readInt(), source.readInt());
2865 taskIds = source.createIntArray();
2866 taskNames = source.createStringArray();
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002867 final int boundsCount = source.readInt();
2868 if (boundsCount > 0) {
2869 taskBounds = new Rect[boundsCount];
2870 for (int i = 0; i < boundsCount; i++) {
2871 taskBounds[i] = new Rect();
2872 taskBounds[i].set(
2873 source.readInt(), source.readInt(), source.readInt(), source.readInt());
2874 }
2875 } else {
2876 taskBounds = null;
2877 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002878 taskUserIds = source.createIntArray();
Craig Mautnered6649f2013-12-02 14:08:25 -08002879 displayId = source.readInt();
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002880 userId = source.readInt();
Winsond46b7272016-04-20 11:54:27 -07002881 visible = source.readInt() > 0;
Winson529c8e42016-05-17 11:08:40 -07002882 position = source.readInt();
Wale Ogunwaleadf116e2020-03-27 16:36:01 -07002883 stackToken = WindowContainerToken.CREATOR.createFromParcel(source);
Winsond46b7272016-04-20 11:54:27 -07002884 if (source.readInt() > 0) {
2885 topActivity = ComponentName.readFromParcel(source);
2886 }
Wale Ogunwale68278562017-09-23 17:13:55 -07002887 configuration.readFromParcel(source);
Craig Mautner967212c2013-04-13 21:10:58 -07002888 }
2889
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002890 public static final @android.annotation.NonNull Creator<StackInfo> CREATOR = new Creator<StackInfo>() {
Craig Mautner967212c2013-04-13 21:10:58 -07002891 @Override
2892 public StackInfo createFromParcel(Parcel source) {
2893 return new StackInfo(source);
2894 }
2895 @Override
2896 public StackInfo[] newArray(int size) {
2897 return new StackInfo[size];
2898 }
2899 };
2900
Craig Mautner5ff12102013-05-24 12:50:15 -07002901 public StackInfo() {
2902 }
2903
Craig Mautner967212c2013-04-13 21:10:58 -07002904 private StackInfo(Parcel source) {
2905 readFromParcel(source);
2906 }
2907
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002908 @UnsupportedAppUsage
Craig Mautner5ff12102013-05-24 12:50:15 -07002909 public String toString(String prefix) {
Craig Mautner967212c2013-04-13 21:10:58 -07002910 StringBuilder sb = new StringBuilder(256);
Craig Mautner5ff12102013-05-24 12:50:15 -07002911 sb.append(prefix); sb.append("Stack id="); sb.append(stackId);
Craig Mautnered6649f2013-12-02 14:08:25 -08002912 sb.append(" bounds="); sb.append(bounds.toShortString());
2913 sb.append(" displayId="); sb.append(displayId);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002914 sb.append(" userId="); sb.append(userId);
Craig Mautnered6649f2013-12-02 14:08:25 -08002915 sb.append("\n");
Wale Ogunwale68278562017-09-23 17:13:55 -07002916 sb.append(" configuration="); sb.append(configuration);
2917 sb.append("\n");
Craig Mautner5ff12102013-05-24 12:50:15 -07002918 prefix = prefix + " ";
Craig Mautner967212c2013-04-13 21:10:58 -07002919 for (int i = 0; i < taskIds.length; ++i) {
2920 sb.append(prefix); sb.append("taskId="); sb.append(taskIds[i]);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002921 sb.append(": "); sb.append(taskNames[i]);
2922 if (taskBounds != null) {
2923 sb.append(" bounds="); sb.append(taskBounds[i].toShortString());
2924 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002925 sb.append(" userId=").append(taskUserIds[i]);
Winsond46b7272016-04-20 11:54:27 -07002926 sb.append(" visible=").append(visible);
2927 if (topActivity != null) {
2928 sb.append(" topActivity=").append(topActivity);
2929 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002930 sb.append("\n");
Craig Mautner967212c2013-04-13 21:10:58 -07002931 }
2932 return sb.toString();
2933 }
Craig Mautner5ff12102013-05-24 12:50:15 -07002934
2935 @Override
2936 public String toString() {
2937 return toString("");
2938 }
Craig Mautner967212c2013-04-13 21:10:58 -07002939 }
2940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 /**
2942 * @hide
2943 */
Svet Ganovf935a702017-08-22 12:15:58 -07002944 @RequiresPermission(anyOf={Manifest.permission.CLEAR_APP_USER_DATA,
2945 Manifest.permission.ACCESS_INSTANT_APPS})
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002946 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
2948 try {
Christopher Tate1d99c392017-12-07 16:54:04 -08002949 return getService().clearApplicationUserData(packageName, false,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07002950 observer, mContext.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002952 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954 }
Christopher Tatea3664242013-06-26 15:00:18 -07002955
2956 /**
2957 * Permits an application to erase its own data from disk. This is equivalent to
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07002958 * the user choosing to clear the app's data from within the device settings UI. It
2959 * erases all dynamic data associated with the app -- its private data and data in its
2960 * private area on external storage -- but does not remove the installed application
Benjamin Franza77e3572017-06-23 12:01:44 +01002961 * itself, nor any OBB files. It also revokes all runtime permissions that the app has acquired,
2962 * clears all notifications and removes all Uri grants related to this application.
Christopher Tatea3664242013-06-26 15:00:18 -07002963 *
2964 * @return {@code true} if the application successfully requested that the application's
2965 * data be erased; {@code false} otherwise.
2966 */
2967 public boolean clearApplicationUserData() {
2968 return clearApplicationUserData(mContext.getPackageName(), null);
2969 }
2970
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002971 /**
2972 * Permits an application to get the persistent URI permissions granted to another.
2973 *
Felipe Leme23a0c7a2018-01-24 08:43:34 -08002974 * <p>Typically called by Settings or DocumentsUI, requires
2975 * {@code GET_APP_GRANTED_URI_PERMISSIONS}.
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002976 *
Felipe Leme23a0c7a2018-01-24 08:43:34 -08002977 * @param packageName application to look for the granted permissions, or {@code null} to get
2978 * granted permissions for all applications
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002979 * @return list of granted URI permissions
2980 *
2981 * @hide
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002982 * @deprecated use {@link UriGrantsManager#getGrantedUriPermissions(String)} instead.
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002983 */
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002984 @Deprecated
Felipe Leme23a0c7a2018-01-24 08:43:34 -08002985 public ParceledListSlice<GrantedUriPermission> getGrantedUriPermissions(
2986 @Nullable String packageName) {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002987 return ((UriGrantsManager) mContext.getSystemService(Context.URI_GRANTS_SERVICE))
2988 .getGrantedUriPermissions(packageName);
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002989 }
2990
2991 /**
2992 * Permits an application to clear the persistent URI permissions granted to another.
2993 *
Felipe Leme23a0c7a2018-01-24 08:43:34 -08002994 * <p>Typically called by Settings, requires {@code CLEAR_APP_GRANTED_URI_PERMISSIONS}.
Felipe Lemef3fa0f82016-01-07 12:08:19 -08002995 *
2996 * @param packageName application to clear its granted permissions
2997 *
2998 * @hide
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07002999 * @deprecated use {@link UriGrantsManager#clearGrantedUriPermissions(String)} instead.
Felipe Lemef3fa0f82016-01-07 12:08:19 -08003000 */
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07003001 @Deprecated
Felipe Lemef3fa0f82016-01-07 12:08:19 -08003002 public void clearGrantedUriPermissions(String packageName) {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07003003 ((UriGrantsManager) mContext.getSystemService(Context.URI_GRANTS_SERVICE))
3004 .clearGrantedUriPermissions(packageName);
Felipe Lemef3fa0f82016-01-07 12:08:19 -08003005 }
3006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 /**
3008 * Information you can retrieve about any processes that are in an error condition.
3009 */
3010 public static class ProcessErrorStateInfo implements Parcelable {
3011 /**
3012 * Condition codes
3013 */
3014 public static final int NO_ERROR = 0;
3015 public static final int CRASHED = 1;
3016 public static final int NOT_RESPONDING = 2;
3017
3018 /**
3019 * The condition that the process is in.
3020 */
3021 public int condition;
3022
3023 /**
3024 * The process name in which the crash or error occurred.
3025 */
3026 public String processName;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 /**
3029 * The pid of this process; 0 if none
3030 */
3031 public int pid;
3032
3033 /**
3034 * The kernel user-ID that has been assigned to this process;
3035 * currently this is not a unique ID (multiple applications can have
3036 * the same uid).
3037 */
3038 public int uid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 /**
Dan Egnor60d87622009-12-16 16:32:58 -08003041 * The activity name associated with the error, if known. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 */
3043 public String tag;
3044
3045 /**
3046 * A short message describing the error condition.
3047 */
3048 public String shortMsg;
3049
3050 /**
3051 * A long message describing the error condition.
3052 */
3053 public String longMsg;
3054
3055 /**
Dan Egnorb7f03672009-12-09 16:22:32 -08003056 * The stack trace where the error originated. May be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 */
Dan Egnorb7f03672009-12-09 16:22:32 -08003058 public String stackTrace;
3059
3060 /**
3061 * to be deprecated: This value will always be null.
3062 */
3063 public byte[] crashData = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064
3065 public ProcessErrorStateInfo() {
3066 }
3067
Craig Mautner5ff12102013-05-24 12:50:15 -07003068 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 public int describeContents() {
3070 return 0;
3071 }
3072
Craig Mautner5ff12102013-05-24 12:50:15 -07003073 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 public void writeToParcel(Parcel dest, int flags) {
3075 dest.writeInt(condition);
3076 dest.writeString(processName);
3077 dest.writeInt(pid);
3078 dest.writeInt(uid);
3079 dest.writeString(tag);
3080 dest.writeString(shortMsg);
3081 dest.writeString(longMsg);
Dan Egnorb7f03672009-12-09 16:22:32 -08003082 dest.writeString(stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 }
Craig Mautner5ff12102013-05-24 12:50:15 -07003084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 public void readFromParcel(Parcel source) {
3086 condition = source.readInt();
3087 processName = source.readString();
3088 pid = source.readInt();
3089 uid = source.readInt();
3090 tag = source.readString();
3091 shortMsg = source.readString();
3092 longMsg = source.readString();
Dan Egnorb7f03672009-12-09 16:22:32 -08003093 stackTrace = source.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07003095
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07003096 public static final @android.annotation.NonNull Creator<ProcessErrorStateInfo> CREATOR =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 new Creator<ProcessErrorStateInfo>() {
3098 public ProcessErrorStateInfo createFromParcel(Parcel source) {
3099 return new ProcessErrorStateInfo(source);
3100 }
3101 public ProcessErrorStateInfo[] newArray(int size) {
3102 return new ProcessErrorStateInfo[size];
3103 }
3104 };
3105
3106 private ProcessErrorStateInfo(Parcel source) {
3107 readFromParcel(source);
3108 }
3109 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07003110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 /**
Stefan Kuhne16045c22015-06-05 07:18:06 -07003112 * 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 -08003113 * will be null if all processes are running properly at this time.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003114 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 * @return Returns a list of ProcessErrorStateInfo records, or null if there are no
3116 * current error conditions (it will not return an empty list). This list ordering is not
3117 * specified.
3118 */
3119 public List<ProcessErrorStateInfo> getProcessesInErrorState() {
3120 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003121 return getService().getProcessesInErrorState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003123 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 }
3125 }
3126
3127 /**
3128 * Information you can retrieve about a running process.
3129 */
Stefan Kuhne16045c22015-06-05 07:18:06 -07003130 public static class RunningAppProcessInfo implements Parcelable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 /**
3132 * The name of the process that this object is associated with
3133 */
3134 public String processName;
3135
3136 /**
3137 * The pid of this process; 0 if none
3138 */
3139 public int pid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003140
Dianne Hackborneb034652009-09-07 00:49:58 -07003141 /**
3142 * The user id of this process.
3143 */
3144 public int uid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003145
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003146 /**
3147 * All packages that have been loaded into the process.
3148 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 public String pkgList[];
Stefan Kuhne16045c22015-06-05 07:18:06 -07003150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 /**
Dianne Hackborn482566e2010-09-03 12:51:28 -07003152 * Constant for {@link #flags}: this is an app that is unable to
3153 * correctly save its state when going to the background,
3154 * so it can not be killed while in the background.
3155 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003156 */
Dianne Hackborn482566e2010-09-03 12:51:28 -07003157 public static final int FLAG_CANT_SAVE_STATE = 1<<0;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003158
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003159 /**
Dianne Hackborn42499172010-10-15 18:45:07 -07003160 * Constant for {@link #flags}: this process is associated with a
3161 * persistent system app.
3162 * @hide
3163 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003164 @UnsupportedAppUsage
Dianne Hackborn42499172010-10-15 18:45:07 -07003165 public static final int FLAG_PERSISTENT = 1<<1;
3166
3167 /**
Dianne Hackborn0c380492012-08-20 17:23:30 -07003168 * Constant for {@link #flags}: this process is associated with a
3169 * persistent system app.
3170 * @hide
3171 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003172 @UnsupportedAppUsage
Dianne Hackborn0c380492012-08-20 17:23:30 -07003173 public static final int FLAG_HAS_ACTIVITIES = 1<<2;
3174
3175 /**
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003176 * Flags of information. May be any of
Dianne Hackborn482566e2010-09-03 12:51:28 -07003177 * {@link #FLAG_CANT_SAVE_STATE}.
3178 * @hide
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003179 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003180 @UnsupportedAppUsage
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003181 public int flags;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003182
3183 /**
3184 * Last memory trim level reported to the process: corresponds to
3185 * the values supplied to {@link android.content.ComponentCallbacks2#onTrimMemory(int)
3186 * ComponentCallbacks2.onTrimMemory(int)}.
3187 */
3188 public int lastTrimLevel;
3189
Jeff Sharkey910e0812017-04-21 16:29:27 -06003190 /** @hide */
3191 @IntDef(prefix = { "IMPORTANCE_" }, value = {
3192 IMPORTANCE_FOREGROUND,
3193 IMPORTANCE_FOREGROUND_SERVICE,
3194 IMPORTANCE_TOP_SLEEPING,
3195 IMPORTANCE_VISIBLE,
3196 IMPORTANCE_PERCEPTIBLE,
3197 IMPORTANCE_CANT_SAVE_STATE,
3198 IMPORTANCE_SERVICE,
3199 IMPORTANCE_CACHED,
3200 IMPORTANCE_GONE,
3201 })
3202 @Retention(RetentionPolicy.SOURCE)
3203 public @interface Importance {}
3204
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003205 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07003206 * Constant for {@link #importance}: This process is running the
3207 * foreground UI; that is, it is the thing currently at the top of the screen
3208 * that the user is interacting with.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 */
3210 public static final int IMPORTANCE_FOREGROUND = 100;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07003213 * Constant for {@link #importance}: This process is running a foreground
3214 * service, for example to perform music playback even while the user is
3215 * not immediately in the app. This generally indicates that the process
3216 * is doing something the user actively cares about.
3217 */
3218 public static final int IMPORTANCE_FOREGROUND_SERVICE = 125;
3219
3220 /**
Dianne Hackbornbad8d912017-12-18 16:45:52 -08003221 * @deprecated Pre-{@link android.os.Build.VERSION_CODES#P} version of
3222 * {@link #IMPORTANCE_TOP_SLEEPING}. As of Android
3223 * {@link android.os.Build.VERSION_CODES#P}, this is considered much less
3224 * important since we want to reduce what apps can do when the screen is off.
Dianne Hackborn1e383822015-04-10 14:02:33 -07003225 */
Dianne Hackbornbad8d912017-12-18 16:45:52 -08003226 @Deprecated
3227 public static final int IMPORTANCE_TOP_SLEEPING_PRE_28 = 150;
Dianne Hackborn1e383822015-04-10 14:02:33 -07003228
3229 /**
3230 * Constant for {@link #importance}: This process is running something
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003231 * that is actively visible to the user, though not in the immediate
Dianne Hackborn1e383822015-04-10 14:02:33 -07003232 * foreground. This may be running a window that is behind the current
3233 * foreground (so paused and with its state saved, not interacting with
3234 * the user, but visible to them to some degree); it may also be running
3235 * other services under the system's control that it inconsiders important.
Dianne Hackborn860755f2010-06-03 18:47:52 -07003236 */
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003237 public static final int IMPORTANCE_VISIBLE = 200;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003238
Dianne Hackborn860755f2010-06-03 18:47:52 -07003239 /**
Makoto Onukid7e40582017-04-13 14:54:56 -07003240 * Constant for {@link #importance}: {@link #IMPORTANCE_PERCEPTIBLE} had this wrong value
3241 * before {@link Build.VERSION_CODES#O}. Since the {@link Build.VERSION_CODES#O} SDK,
3242 * the value of {@link #IMPORTANCE_PERCEPTIBLE} has been fixed.
3243 *
Makoto Onukie92f7942017-04-26 14:38:18 -07003244 * <p>The system will return this value instead of {@link #IMPORTANCE_PERCEPTIBLE}
3245 * on Android versions below {@link Build.VERSION_CODES#O}.
3246 *
3247 * <p>On Android version {@link Build.VERSION_CODES#O} and later, this value will still be
3248 * returned for apps with the target API level below {@link Build.VERSION_CODES#O}.
3249 * For apps targeting version {@link Build.VERSION_CODES#O} and later,
3250 * the correct value {@link #IMPORTANCE_PERCEPTIBLE} will be returned.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 */
Makoto Onukie92f7942017-04-26 14:38:18 -07003252 public static final int IMPORTANCE_PERCEPTIBLE_PRE_26 = 130;
Makoto Onukid7e40582017-04-13 14:54:56 -07003253
3254 /**
3255 * Constant for {@link #importance}: This process is not something the user
3256 * is directly aware of, but is otherwise perceptible to them to some degree.
3257 */
3258 public static final int IMPORTANCE_PERCEPTIBLE = 230;
3259
3260 /**
3261 * Constant for {@link #importance}: {@link #IMPORTANCE_CANT_SAVE_STATE} had
3262 * this wrong value
3263 * before {@link Build.VERSION_CODES#O}. Since the {@link Build.VERSION_CODES#O} SDK,
3264 * the value of {@link #IMPORTANCE_CANT_SAVE_STATE} has been fixed.
3265 *
Makoto Onukie92f7942017-04-26 14:38:18 -07003266 * <p>The system will return this value instead of {@link #IMPORTANCE_CANT_SAVE_STATE}
3267 * on Android versions below {@link Build.VERSION_CODES#O}.
3268 *
3269 * <p>On Android version {@link Build.VERSION_CODES#O} after, this value will still be
3270 * returned for apps with the target API level below {@link Build.VERSION_CODES#O}.
3271 * For apps targeting version {@link Build.VERSION_CODES#O} and later,
3272 * the correct value {@link #IMPORTANCE_CANT_SAVE_STATE} will be returned.
3273 *
Makoto Onukid7e40582017-04-13 14:54:56 -07003274 * @hide
3275 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +00003276 @UnsupportedAppUsage
Sudheer Shankabe85ed62018-08-13 13:10:50 -07003277 @TestApi
Makoto Onukie92f7942017-04-26 14:38:18 -07003278 public static final int IMPORTANCE_CANT_SAVE_STATE_PRE_26 = 170;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003279
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003280 /**
Kevin Hufnagle08a4e472020-01-03 20:05:28 +00003281 * Constant for {@link #importance}: This process contains services
Dianne Hackborn1e383822015-04-10 14:02:33 -07003282 * that should remain running. These are background services apps have
3283 * started, not something the user is aware of, so they may be killed by
3284 * the system relatively freely (though it is generally desired that they
3285 * stay running as long as they want to).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 */
3287 public static final int IMPORTANCE_SERVICE = 300;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 /**
Dianne Hackbornbad8d912017-12-18 16:45:52 -08003290 * Constant for {@link #importance}: This process is running the foreground
3291 * UI, but the device is asleep so it is not visible to the user. Though the
3292 * system will try hard to keep its process from being killed, in all other
3293 * ways we consider it a kind of cached process, with the limitations that go
3294 * along with that state: network access, running background services, etc.
3295 */
3296 public static final int IMPORTANCE_TOP_SLEEPING = 325;
3297
3298 /**
Dianne Hackbornf097d422017-12-15 16:32:19 -08003299 * Constant for {@link #importance}: This process is running an
3300 * application that can not save its state, and thus can't be killed
3301 * while in the background. This will be used with apps that have
3302 * {@link android.R.attr#cantSaveState} set on their application tag.
3303 */
3304 public static final int IMPORTANCE_CANT_SAVE_STATE = 350;
3305
3306 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07003307 * Constant for {@link #importance}: This process process contains
Dianne Hackborn45e69d62017-03-01 16:02:15 -08003308 * cached code that is expendable, not actively running any app components
3309 * we care about.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 */
Dianne Hackborn45e69d62017-03-01 16:02:15 -08003311 public static final int IMPORTANCE_CACHED = 400;
3312
3313 /**
3314 * @deprecated Renamed to {@link #IMPORTANCE_CACHED}.
3315 */
3316 public static final int IMPORTANCE_BACKGROUND = IMPORTANCE_CACHED;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07003319 * Constant for {@link #importance}: This process is empty of any
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 * actively running code.
Dianne Hackborn45e69d62017-03-01 16:02:15 -08003321 * @deprecated This value is no longer reported, use {@link #IMPORTANCE_CACHED} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 */
Dianne Hackborn45e69d62017-03-01 16:02:15 -08003323 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 public static final int IMPORTANCE_EMPTY = 500;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07003325
3326 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07003327 * Constant for {@link #importance}: This process does not exist.
Dianne Hackbornfee756f2014-07-16 17:31:10 -07003328 */
3329 public static final int IMPORTANCE_GONE = 1000;
3330
Makoto Onukid7e40582017-04-13 14:54:56 -07003331 /**
3332 * Convert a proc state to the correspondent IMPORTANCE_* constant. If the return value
3333 * will be passed to a client, use {@link #procStateToImportanceForClient}.
3334 * @hide
3335 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003336 @UnsupportedAppUsage
Jeff Sharkey910e0812017-04-21 16:29:27 -06003337 public static @Importance int procStateToImportance(int procState) {
Dianne Hackborn1e383822015-04-10 14:02:33 -07003338 if (procState == PROCESS_STATE_NONEXISTENT) {
3339 return IMPORTANCE_GONE;
3340 } else if (procState >= PROCESS_STATE_HOME) {
Dianne Hackborn45e69d62017-03-01 16:02:15 -08003341 return IMPORTANCE_CACHED;
Dianne Hackbornf4ef6eb2017-12-07 13:57:21 -08003342 } else if (procState == PROCESS_STATE_HEAVY_WEIGHT) {
Dianne Hackborn1e383822015-04-10 14:02:33 -07003343 return IMPORTANCE_CANT_SAVE_STATE;
Dianne Hackbornbad8d912017-12-18 16:45:52 -08003344 } else if (procState >= PROCESS_STATE_TOP_SLEEPING) {
3345 return IMPORTANCE_TOP_SLEEPING;
Dianne Hackbornf097d422017-12-15 16:32:19 -08003346 } else if (procState >= PROCESS_STATE_SERVICE) {
3347 return IMPORTANCE_SERVICE;
Dianne Hackborn83b40f62017-04-26 13:59:47 -07003348 } else if (procState >= PROCESS_STATE_TRANSIENT_BACKGROUND) {
Dianne Hackborn1e383822015-04-10 14:02:33 -07003349 return IMPORTANCE_PERCEPTIBLE;
3350 } else if (procState >= PROCESS_STATE_IMPORTANT_FOREGROUND) {
3351 return IMPORTANCE_VISIBLE;
Hui Yu26969322019-08-21 14:56:35 -07003352 } else if (procState >= PROCESS_STATE_FOREGROUND_SERVICE) {
Dianne Hackborn1e383822015-04-10 14:02:33 -07003353 return IMPORTANCE_FOREGROUND_SERVICE;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07003354 } else {
Dianne Hackborn1e383822015-04-10 14:02:33 -07003355 return IMPORTANCE_FOREGROUND;
Dianne Hackbornfee756f2014-07-16 17:31:10 -07003356 }
3357 }
3358
Makoto Onukid7e40582017-04-13 14:54:56 -07003359 /**
3360 * Convert a proc state to the correspondent IMPORTANCE_* constant for a client represented
3361 * by a given {@link Context}, with converting {@link #IMPORTANCE_PERCEPTIBLE}
3362 * and {@link #IMPORTANCE_CANT_SAVE_STATE} to the corresponding "wrong" value if the
3363 * client's target SDK < {@link VERSION_CODES#O}.
3364 * @hide
3365 */
Jeff Sharkey910e0812017-04-21 16:29:27 -06003366 public static @Importance int procStateToImportanceForClient(int procState,
3367 Context clientContext) {
Makoto Onukie92f7942017-04-26 14:38:18 -07003368 return procStateToImportanceForTargetSdk(procState,
3369 clientContext.getApplicationInfo().targetSdkVersion);
3370 }
3371
3372 /**
3373 * See {@link #procStateToImportanceForClient}.
3374 * @hide
3375 */
3376 public static @Importance int procStateToImportanceForTargetSdk(int procState,
3377 int targetSdkVersion) {
Makoto Onukid7e40582017-04-13 14:54:56 -07003378 final int importance = procStateToImportance(procState);
3379
3380 // For pre O apps, convert to the old, wrong values.
Makoto Onukie92f7942017-04-26 14:38:18 -07003381 if (targetSdkVersion < VERSION_CODES.O) {
Makoto Onukid7e40582017-04-13 14:54:56 -07003382 switch (importance) {
3383 case IMPORTANCE_PERCEPTIBLE:
Makoto Onukie92f7942017-04-26 14:38:18 -07003384 return IMPORTANCE_PERCEPTIBLE_PRE_26;
Dianne Hackbornbad8d912017-12-18 16:45:52 -08003385 case IMPORTANCE_TOP_SLEEPING:
3386 return IMPORTANCE_TOP_SLEEPING_PRE_28;
Makoto Onukid7e40582017-04-13 14:54:56 -07003387 case IMPORTANCE_CANT_SAVE_STATE:
Makoto Onukie92f7942017-04-26 14:38:18 -07003388 return IMPORTANCE_CANT_SAVE_STATE_PRE_26;
Makoto Onukid7e40582017-04-13 14:54:56 -07003389 }
3390 }
3391 return importance;
3392 }
3393
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003394 /** @hide */
Jeff Sharkey910e0812017-04-21 16:29:27 -06003395 public static int importanceToProcState(@Importance int importance) {
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003396 if (importance == IMPORTANCE_GONE) {
3397 return PROCESS_STATE_NONEXISTENT;
Dianne Hackborn45e69d62017-03-01 16:02:15 -08003398 } else if (importance >= IMPORTANCE_CACHED) {
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003399 return PROCESS_STATE_HOME;
Dianne Hackbornbad8d912017-12-18 16:45:52 -08003400 } else if (importance >= IMPORTANCE_CANT_SAVE_STATE) {
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003401 return PROCESS_STATE_HEAVY_WEIGHT;
Dianne Hackbornbad8d912017-12-18 16:45:52 -08003402 } else if (importance >= IMPORTANCE_TOP_SLEEPING) {
3403 return PROCESS_STATE_TOP_SLEEPING;
Dianne Hackbornf097d422017-12-15 16:32:19 -08003404 } else if (importance >= IMPORTANCE_SERVICE) {
3405 return PROCESS_STATE_SERVICE;
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003406 } else if (importance >= IMPORTANCE_PERCEPTIBLE) {
Dianne Hackborn83b40f62017-04-26 13:59:47 -07003407 return PROCESS_STATE_TRANSIENT_BACKGROUND;
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003408 } else if (importance >= IMPORTANCE_VISIBLE) {
3409 return PROCESS_STATE_IMPORTANT_FOREGROUND;
Dianne Hackbornbad8d912017-12-18 16:45:52 -08003410 } else if (importance >= IMPORTANCE_TOP_SLEEPING_PRE_28) {
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -08003411 return PROCESS_STATE_IMPORTANT_FOREGROUND;
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003412 } else if (importance >= IMPORTANCE_FOREGROUND_SERVICE) {
3413 return PROCESS_STATE_FOREGROUND_SERVICE;
Amith Yamasania0a30a12019-01-22 11:38:06 -08003414 // TODO: Asymmetrical mapping for LOCATION service type. Ok?
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003415 } else {
Dianne Hackborn10fc4fd2017-12-19 17:23:13 -08003416 return PROCESS_STATE_TOP;
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003417 }
3418 }
3419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 /**
Jeff Sharkey910e0812017-04-21 16:29:27 -06003421 * The relative importance level that the system places on this process.
3422 * These constants are numbered so that "more important" values are
3423 * always smaller than "less important" values.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 */
Jeff Sharkey910e0812017-04-21 16:29:27 -06003425 public @Importance int importance;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 /**
3428 * An additional ordering within a particular {@link #importance}
3429 * category, providing finer-grained information about the relative
3430 * utility of processes within a category. This number means nothing
3431 * except that a smaller values are more recently used (and thus
3432 * more important). Currently an LRU value is only maintained for
Dianne Hackborn45e69d62017-03-01 16:02:15 -08003433 * the {@link #IMPORTANCE_CACHED} category, though others may
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 * be maintained in the future.
3435 */
3436 public int lru;
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003437
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003438 /**
3439 * Constant for {@link #importanceReasonCode}: nothing special has
3440 * been specified for the reason for this level.
3441 */
3442 public static final int REASON_UNKNOWN = 0;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003443
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003444 /**
3445 * Constant for {@link #importanceReasonCode}: one of the application's
3446 * content providers is being used by another process. The pid of
3447 * the client process is in {@link #importanceReasonPid} and the
3448 * target provider in this process is in
3449 * {@link #importanceReasonComponent}.
3450 */
3451 public static final int REASON_PROVIDER_IN_USE = 1;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003452
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003453 /**
3454 * Constant for {@link #importanceReasonCode}: one of the application's
3455 * content providers is being used by another process. The pid of
3456 * the client process is in {@link #importanceReasonPid} and the
3457 * target provider in this process is in
3458 * {@link #importanceReasonComponent}.
3459 */
3460 public static final int REASON_SERVICE_IN_USE = 2;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003461
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003462 /**
3463 * The reason for {@link #importance}, if any.
3464 */
3465 public int importanceReasonCode;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003466
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003467 /**
3468 * For the specified values of {@link #importanceReasonCode}, this
3469 * is the process ID of the other process that is a client of this
3470 * process. This will be 0 if no other process is using this one.
3471 */
3472 public int importanceReasonPid;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003473
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003474 /**
3475 * For the specified values of {@link #importanceReasonCode}, this
3476 * is the name of the component that is being used in this process.
3477 */
3478 public ComponentName importanceReasonComponent;
Stefan Kuhne16045c22015-06-05 07:18:06 -07003479
Dianne Hackborn905577f2011-09-07 18:31:28 -07003480 /**
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07003481 * When {@link #importanceReasonPid} is non-0, this is the importance
Dianne Hackborn905577f2011-09-07 18:31:28 -07003482 * of the other pid. @hide
3483 */
3484 public int importanceReasonImportance;
3485
Dianne Hackborn684bf342014-04-29 17:56:57 -07003486 /**
3487 * Current process state, as per PROCESS_STATE_* constants.
3488 * @hide
3489 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003490 @UnsupportedAppUsage
Dianne Hackborn684bf342014-04-29 17:56:57 -07003491 public int processState;
3492
Chris Morin0260f3d2018-07-10 12:42:06 -07003493 /**
3494 * Whether the app is focused in multi-window environment.
3495 * @hide
3496 */
3497 public boolean isFocused;
3498
3499 /**
3500 * Copy of {@link com.android.server.am.ProcessRecord#lastActivityTime} of the process.
3501 * @hide
3502 */
3503 public long lastActivityTime;
3504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 public RunningAppProcessInfo() {
3506 importance = IMPORTANCE_FOREGROUND;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003507 importanceReasonCode = REASON_UNKNOWN;
Dianne Hackborn684bf342014-04-29 17:56:57 -07003508 processState = PROCESS_STATE_IMPORTANT_FOREGROUND;
Chris Morin0260f3d2018-07-10 12:42:06 -07003509 isFocused = false;
3510 lastActivityTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07003512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 public RunningAppProcessInfo(String pProcessName, int pPid, String pArr[]) {
3514 processName = pProcessName;
3515 pid = pPid;
3516 pkgList = pArr;
Chris Morin0260f3d2018-07-10 12:42:06 -07003517 isFocused = false;
3518 lastActivityTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 }
3520
3521 public int describeContents() {
3522 return 0;
3523 }
3524
3525 public void writeToParcel(Parcel dest, int flags) {
3526 dest.writeString(processName);
3527 dest.writeInt(pid);
Dianne Hackborneb034652009-09-07 00:49:58 -07003528 dest.writeInt(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 dest.writeStringArray(pkgList);
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003530 dest.writeInt(this.flags);
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003531 dest.writeInt(lastTrimLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 dest.writeInt(importance);
3533 dest.writeInt(lru);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003534 dest.writeInt(importanceReasonCode);
3535 dest.writeInt(importanceReasonPid);
3536 ComponentName.writeToParcel(importanceReasonComponent, dest);
Dianne Hackborn905577f2011-09-07 18:31:28 -07003537 dest.writeInt(importanceReasonImportance);
Dianne Hackborn684bf342014-04-29 17:56:57 -07003538 dest.writeInt(processState);
Chris Morin0260f3d2018-07-10 12:42:06 -07003539 dest.writeInt(isFocused ? 1 : 0);
3540 dest.writeLong(lastActivityTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 }
3542
3543 public void readFromParcel(Parcel source) {
3544 processName = source.readString();
3545 pid = source.readInt();
Dianne Hackborneb034652009-09-07 00:49:58 -07003546 uid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 pkgList = source.readStringArray();
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07003548 flags = source.readInt();
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003549 lastTrimLevel = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 importance = source.readInt();
3551 lru = source.readInt();
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07003552 importanceReasonCode = source.readInt();
3553 importanceReasonPid = source.readInt();
3554 importanceReasonComponent = ComponentName.readFromParcel(source);
Dianne Hackborn905577f2011-09-07 18:31:28 -07003555 importanceReasonImportance = source.readInt();
Dianne Hackborn684bf342014-04-29 17:56:57 -07003556 processState = source.readInt();
Chris Morin0260f3d2018-07-10 12:42:06 -07003557 isFocused = source.readInt() != 0;
3558 lastActivityTime = source.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 }
3560
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07003561 public static final @android.annotation.NonNull Creator<RunningAppProcessInfo> CREATOR =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 new Creator<RunningAppProcessInfo>() {
3563 public RunningAppProcessInfo createFromParcel(Parcel source) {
3564 return new RunningAppProcessInfo(source);
3565 }
3566 public RunningAppProcessInfo[] newArray(int size) {
3567 return new RunningAppProcessInfo[size];
3568 }
3569 };
3570
3571 private RunningAppProcessInfo(Parcel source) {
3572 readFromParcel(source);
3573 }
3574 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07003575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 /**
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07003577 * Returns a list of application processes installed on external media
3578 * that are running on the device.
3579 *
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07003580 * <p><b>Note: this method is only intended for debugging or building
3581 * a user-facing process management UI.</b></p>
3582 *
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07003583 * @return Returns a list of ApplicationInfo records, or null if none
3584 * This list ordering is not specified.
3585 * @hide
3586 */
3587 public List<ApplicationInfo> getRunningExternalApplications() {
3588 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003589 return getService().getRunningExternalApplications();
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07003590 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003591 throw e.rethrowFromSystemServer();
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07003592 }
3593 }
3594
3595 /**
Suprabh Shukla4d64c092018-02-21 20:06:14 -08003596 * Query whether the user has enabled background restrictions for this app.
3597 *
3598 * <p> The user may chose to do this, if they see that an app is consuming an unreasonable
3599 * amount of battery while in the background. </p>
3600 *
3601 * <p> If true, any work that the app tries to do will be aggressively restricted while it is in
3602 * the background. At a minimum, jobs and alarms will not execute and foreground services
3603 * cannot be started unless an app activity is in the foreground. </p>
3604 *
3605 * <p><b> Note that these restrictions stay in effect even when the device is charging.</b></p>
3606 *
3607 * @return true if user has enforced background restrictions for this app, false otherwise.
3608 */
3609 public boolean isBackgroundRestricted() {
3610 try {
3611 return getService().isBackgroundRestricted(mContext.getOpPackageName());
3612 } catch (RemoteException e) {
3613 throw e.rethrowFromSystemServer();
3614 }
3615 }
3616
3617 /**
Stefan Kuhne16045c22015-06-05 07:18:06 -07003618 * Sets the memory trim mode for a process and schedules a memory trim operation.
3619 *
3620 * <p><b>Note: this method is only intended for testing framework.</b></p>
3621 *
3622 * @return Returns true if successful.
3623 * @hide
3624 */
3625 public boolean setProcessMemoryTrimLevel(String process, int userId, int level) {
3626 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003627 return getService().setProcessMemoryTrimLevel(process, userId,
Stefan Kuhne16045c22015-06-05 07:18:06 -07003628 level);
3629 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003630 throw e.rethrowFromSystemServer();
Stefan Kuhne16045c22015-06-05 07:18:06 -07003631 }
3632 }
3633
3634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 * Returns a list of application processes that are running on the device.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07003636 *
3637 * <p><b>Note: this method is only intended for debugging or building
3638 * a user-facing process management UI.</b></p>
3639 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 * @return Returns a list of RunningAppProcessInfo records, or null if there are no
3641 * running processes (it will not return an empty list). This list ordering is not
3642 * specified.
3643 */
3644 public List<RunningAppProcessInfo> getRunningAppProcesses() {
3645 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003646 return getService().getRunningAppProcesses();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003648 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 }
3650 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003651
3652 /**
Jing Ji8055a3a2019-12-17 15:55:33 -08003653 * Return a list of {@link ApplicationExitInfo} records containing the reasons for the most
3654 * recent app deaths.
3655 *
3656 * <p class="note"> Note: System stores this historical information in a ring buffer and only
3657 * the most recent records will be returned. </p>
3658 *
3659 * <p class="note"> Note: In the case that this application was bound to an external service
3660 * with flag {@link android.content.Context#BIND_EXTERNAL_SERVICE}, the process of that external
3661 * service will be included in this package's exit info. </p>
3662 *
3663 * @param packageName Optional, a null value means match all packages belonging to the
3664 * caller's UID. If this package belongs to another UID, you must hold
3665 * {@link android.Manifest.permission#DUMP} in order to retrieve it.
3666 * @param pid A process ID that used to belong to this package but died later; a value
3667 * of 0 means to ignore this parameter and return all matching records.
3668 * @param maxNum The maximum number of results to be returned; a value of 0
3669 * means to ignore this parameter and return all matching records
3670 *
3671 * @return a list of {@link ApplicationExitInfo} records matching the criteria, sorted in
3672 * the order from most recent to least recent.
3673 */
Jing Jia590e8e2020-02-05 20:25:06 -08003674 @NonNull
Jing Ji8055a3a2019-12-17 15:55:33 -08003675 public List<ApplicationExitInfo> getHistoricalProcessExitReasons(@Nullable String packageName,
3676 @IntRange(from = 0) int pid, @IntRange(from = 0) int maxNum) {
3677 try {
3678 ParceledListSlice<ApplicationExitInfo> r = getService().getHistoricalProcessExitReasons(
3679 packageName, pid, maxNum, mContext.getUserId());
Jing Jia590e8e2020-02-05 20:25:06 -08003680 return r == null ? Collections.emptyList() : r.getList();
Jing Ji8055a3a2019-12-17 15:55:33 -08003681 } catch (RemoteException e) {
3682 throw e.rethrowFromSystemServer();
3683 }
3684 }
3685
Jing Ji56006fd2020-03-08 00:32:33 -08003686 /**
3687 * Set custom state data for this process. It will be included in the record of
3688 * {@link ApplicationExitInfo} on the death of the current calling process; the new process
3689 * of the app can retrieve this state data by calling
Jing Jie2757e62020-04-02 14:13:42 -07003690 * {@link android.app.ApplicationExitInfo#getProcessStateSummary()
3691 * ApplicationExitInfo.getProcessStateSummary()} on the record returned by
Jing Ji56006fd2020-03-08 00:32:33 -08003692 * {@link #getHistoricalProcessExitReasons}.
3693 *
3694 * <p> This would be useful for the calling app to save its stateful data: if it's
3695 * killed later for any reason, the new process of the app can know what the
3696 * previous process of the app was doing. For instance, you could use this to encode
3697 * the current level in a game, or a set of features/experiments that were enabled. Later you
3698 * could analyze under what circumstances the app tends to crash or use too much memory.
3699 * However, it's not suggested to rely on this to restore the applications previous UI state
3700 * or so, it's only meant for analyzing application healthy status.</p>
3701 *
3702 * <p> System might decide to throttle the calls to this API; so call this API in a reasonable
3703 * manner, excessive calls to this API could result a {@link java.lang.RuntimeException}.
3704 * </p>
3705 *
3706 * @param state The state data
3707 */
3708 public void setProcessStateSummary(@Nullable byte[] state) {
3709 try {
3710 getService().setProcessStateSummary(state);
3711 } catch (RemoteException e) {
3712 throw e.rethrowFromSystemServer();
3713 }
3714 }
3715
Jing Jie2757e62020-04-02 14:13:42 -07003716 /**
Jing Ji8055a3a2019-12-17 15:55:33 -08003717 * @return Whether or not the low memory kill will be reported in
3718 * {@link #getHistoricalProcessExitReasons}.
3719 *
Jing Ji56006fd2020-03-08 00:32:33 -08003720 * @see ApplicationExitInfo#REASON_LOW_MEMORY
Jing Ji8055a3a2019-12-17 15:55:33 -08003721 */
3722 public static boolean isLowMemoryKillReportSupported() {
3723 return SystemProperties.getBoolean("persist.sys.lmk.reportkills", false);
3724 }
3725
3726 /**
Dianne Hackborn1e383822015-04-10 14:02:33 -07003727 * Return the importance of a given package name, based on the processes that are
3728 * currently running. The return value is one of the importance constants defined
3729 * in {@link RunningAppProcessInfo}, giving you the highest importance of all the
3730 * processes that this package has code running inside of. If there are no processes
3731 * running its code, {@link RunningAppProcessInfo#IMPORTANCE_GONE} is returned.
Jeff Sharkeyf85ac772015-05-13 12:52:06 -07003732 * @hide
Dianne Hackborn1e383822015-04-10 14:02:33 -07003733 */
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003734 @SystemApi @TestApi
Dianne Hackborn9df8f512016-10-24 17:58:30 -07003735 @RequiresPermission(Manifest.permission.PACKAGE_USAGE_STATS)
Jeff Sharkey910e0812017-04-21 16:29:27 -06003736 public @RunningAppProcessInfo.Importance int getPackageImportance(String packageName) {
Dianne Hackborn1e383822015-04-10 14:02:33 -07003737 try {
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003738 int procState = getService().getPackageProcessState(packageName,
Adam Lesinskic30454c2015-06-24 13:24:35 -07003739 mContext.getOpPackageName());
Makoto Onukid7e40582017-04-13 14:54:56 -07003740 return RunningAppProcessInfo.procStateToImportanceForClient(procState, mContext);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003741 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003742 throw e.rethrowFromSystemServer();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003743 }
3744 }
3745
3746 /**
Dianne Hackborn83249ec2017-04-12 10:54:03 -07003747 * Return the importance of a given uid, based on the processes that are
3748 * currently running. The return value is one of the importance constants defined
3749 * in {@link RunningAppProcessInfo}, giving you the highest importance of all the
3750 * processes that this uid has running. If there are no processes
3751 * running its code, {@link RunningAppProcessInfo#IMPORTANCE_GONE} is returned.
3752 * @hide
3753 */
3754 @SystemApi @TestApi
3755 @RequiresPermission(Manifest.permission.PACKAGE_USAGE_STATS)
Jeff Sharkey910e0812017-04-21 16:29:27 -06003756 public @RunningAppProcessInfo.Importance int getUidImportance(int uid) {
Dianne Hackborn83249ec2017-04-12 10:54:03 -07003757 try {
3758 int procState = getService().getUidProcessState(uid,
3759 mContext.getOpPackageName());
Makoto Onukid7e40582017-04-13 14:54:56 -07003760 return RunningAppProcessInfo.procStateToImportanceForClient(procState, mContext);
Dianne Hackborn83249ec2017-04-12 10:54:03 -07003761 } catch (RemoteException e) {
3762 throw e.rethrowFromSystemServer();
3763 }
3764 }
3765
3766 /**
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003767 * Callback to get reports about changes to the importance of a uid. Use with
3768 * {@link #addOnUidImportanceListener}.
3769 * @hide
3770 */
3771 @SystemApi @TestApi
3772 public interface OnUidImportanceListener {
3773 /**
3774 * The importance if a given uid has changed. Will be one of the importance
3775 * values in {@link RunningAppProcessInfo};
3776 * {@link RunningAppProcessInfo#IMPORTANCE_GONE IMPORTANCE_GONE} will be reported
3777 * when the uid is no longer running at all. This callback will happen on a thread
3778 * from a thread pool, not the main UI thread.
3779 * @param uid The uid whose importance has changed.
3780 * @param importance The new importance value as per {@link RunningAppProcessInfo}.
3781 */
Jeff Sharkey910e0812017-04-21 16:29:27 -06003782 void onUidImportance(int uid, @RunningAppProcessInfo.Importance int importance);
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003783 }
3784
3785 /**
3786 * Start monitoring changes to the imoportance of uids running in the system.
3787 * @param listener The listener callback that will receive change reports.
3788 * @param importanceCutpoint The level of importance in which the caller is interested
3789 * in differences. For example, if {@link RunningAppProcessInfo#IMPORTANCE_PERCEPTIBLE}
3790 * is used here, you will receive a call each time a uids importance transitions between
3791 * being <= {@link RunningAppProcessInfo#IMPORTANCE_PERCEPTIBLE} and
3792 * > {@link RunningAppProcessInfo#IMPORTANCE_PERCEPTIBLE}.
3793 *
3794 * <p>The caller must hold the {@link android.Manifest.permission#PACKAGE_USAGE_STATS}
3795 * permission to use this feature.</p>
3796 *
3797 * @throws IllegalArgumentException If the listener is already registered.
3798 * @throws SecurityException If the caller does not hold
3799 * {@link android.Manifest.permission#PACKAGE_USAGE_STATS}.
3800 * @hide
3801 */
3802 @SystemApi @TestApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06003803 @RequiresPermission(Manifest.permission.PACKAGE_USAGE_STATS)
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003804 public void addOnUidImportanceListener(OnUidImportanceListener listener,
Jeff Sharkey910e0812017-04-21 16:29:27 -06003805 @RunningAppProcessInfo.Importance int importanceCutpoint) {
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003806 synchronized (this) {
3807 if (mImportanceListeners.containsKey(listener)) {
3808 throw new IllegalArgumentException("Listener already registered: " + listener);
3809 }
3810 // TODO: implement the cut point in the system process to avoid IPCs.
Makoto Onukid7e40582017-04-13 14:54:56 -07003811 UidObserver observer = new UidObserver(listener, mContext);
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003812 try {
3813 getService().registerUidObserver(observer,
Dianne Hackborn5614bf52016-11-07 17:26:41 -08003814 UID_OBSERVER_PROCSTATE | UID_OBSERVER_GONE,
3815 RunningAppProcessInfo.importanceToProcState(importanceCutpoint),
3816 mContext.getOpPackageName());
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003817 } catch (RemoteException e) {
3818 throw e.rethrowFromSystemServer();
3819 }
3820 mImportanceListeners.put(listener, observer);
3821 }
3822 }
3823
3824 /**
3825 * Remove an importance listener that was previously registered with
3826 * {@link #addOnUidImportanceListener}.
3827 *
3828 * @throws IllegalArgumentException If the listener is not registered.
3829 * @hide
3830 */
3831 @SystemApi @TestApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06003832 @RequiresPermission(Manifest.permission.PACKAGE_USAGE_STATS)
Dianne Hackborn058f1e42016-11-02 17:18:35 -07003833 public void removeOnUidImportanceListener(OnUidImportanceListener listener) {
3834 synchronized (this) {
3835 UidObserver observer = mImportanceListeners.remove(listener);
3836 if (observer == null) {
3837 throw new IllegalArgumentException("Listener not registered: " + listener);
3838 }
3839 try {
3840 getService().unregisterUidObserver(observer);
3841 } catch (RemoteException e) {
3842 throw e.rethrowFromSystemServer();
3843 }
3844 }
3845 }
3846
3847 /**
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003848 * Return global memory state information for the calling process. This
3849 * does not fill in all fields of the {@link RunningAppProcessInfo}. The
3850 * only fields that will be filled in are
3851 * {@link RunningAppProcessInfo#pid},
3852 * {@link RunningAppProcessInfo#uid},
3853 * {@link RunningAppProcessInfo#lastTrimLevel},
3854 * {@link RunningAppProcessInfo#importance},
3855 * {@link RunningAppProcessInfo#lru}, and
3856 * {@link RunningAppProcessInfo#importanceReasonCode}.
3857 */
3858 static public void getMyMemoryState(RunningAppProcessInfo outState) {
3859 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003860 getService().getMyMemoryState(outState);
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003861 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003862 throw e.rethrowFromSystemServer();
Dianne Hackborn27ff9132012-03-06 14:57:58 -08003863 }
3864 }
3865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003867 * Return information about the memory usage of one or more processes.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07003868 *
3869 * <p><b>Note: this method is only intended for debugging or building
3870 * a user-facing process management UI.</b></p>
3871 *
Dianne Hackborn8c76d912018-08-23 15:20:05 -07003872 * <p>As of {@link android.os.Build.VERSION_CODES#Q Android Q}, for regular apps this method
3873 * will only return information about the memory info for the processes running as the
3874 * caller's uid; no other process memory info is available and will be zero.
3875 * Also of {@link android.os.Build.VERSION_CODES#Q Android Q} the sample rate allowed
3876 * by this API is significantly limited, if called faster the limit you will receive the
3877 * same data as the previous call.</p>
3878 *
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003879 * @param pids The pids of the processes whose memory usage is to be
3880 * retrieved.
3881 * @return Returns an array of memory information, one for each
3882 * requested pid.
3883 */
3884 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) {
3885 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003886 return getService().getProcessMemoryInfo(pids);
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003887 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003888 throw e.rethrowFromSystemServer();
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003889 }
3890 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07003891
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003892 /**
Dianne Hackborn03abb812010-01-04 18:43:19 -08003893 * @deprecated This is now just a wrapper for
3894 * {@link #killBackgroundProcesses(String)}; the previous behavior here
3895 * is no longer available to applications because it allows them to
3896 * break other applications by removing their alarms, stopping their
3897 * services, etc.
3898 */
3899 @Deprecated
3900 public void restartPackage(String packageName) {
3901 killBackgroundProcesses(packageName);
3902 }
Stefan Kuhne16045c22015-06-05 07:18:06 -07003903
Dianne Hackborn03abb812010-01-04 18:43:19 -08003904 /**
3905 * Have the system immediately kill all background processes associated
3906 * with the given package. This is the same as the kernel killing those
3907 * processes to reclaim memory; the system will take care of restarting
3908 * these processes in the future as needed.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003909 *
Dianne Hackborn03abb812010-01-04 18:43:19 -08003910 * @param packageName The name of the package whose processes are to
3911 * be killed.
3912 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003913 @RequiresPermission(Manifest.permission.KILL_BACKGROUND_PROCESSES)
Dianne Hackborn03abb812010-01-04 18:43:19 -08003914 public void killBackgroundProcesses(String packageName) {
3915 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003916 getService().killBackgroundProcesses(packageName,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07003917 mContext.getUserId());
Dianne Hackborn03abb812010-01-04 18:43:19 -08003918 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003919 throw e.rethrowFromSystemServer();
Dianne Hackborn03abb812010-01-04 18:43:19 -08003920 }
3921 }
Svet Ganov019d2302015-05-04 11:07:38 -07003922
3923 /**
3924 * Kills the specified UID.
3925 * @param uid The UID to kill.
3926 * @param reason The reason for the kill.
3927 *
3928 * @hide
3929 */
Svet Ganovae0e03a2016-02-25 18:22:10 -08003930 @SystemApi
Svet Ganov019d2302015-05-04 11:07:38 -07003931 @RequiresPermission(Manifest.permission.KILL_UID)
3932 public void killUid(int uid, String reason) {
3933 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003934 getService().killUid(UserHandle.getAppId(uid),
Svetoslavaa41add2015-08-06 15:03:55 -07003935 UserHandle.getUserId(uid), reason);
Svet Ganov019d2302015-05-04 11:07:38 -07003936 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003937 throw e.rethrowFromSystemServer();
Svet Ganov019d2302015-05-04 11:07:38 -07003938 }
3939 }
3940
Dianne Hackborn03abb812010-01-04 18:43:19 -08003941 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 * Have the system perform a force stop of everything associated with
3943 * the given application package. All processes that share its uid
3944 * will be killed, all services it has running stopped, all activities
3945 * removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}
3946 * broadcast will be sent, so that any of its registered alarms can
3947 * be stopped, notifications removed, etc.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003948 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 * <p>You must hold the permission
Dianne Hackborn03abb812010-01-04 18:43:19 -08003950 * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 * call this method.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003952 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 * @param packageName The name of the package to be stopped.
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003954 * @param userId The user for which the running package is to be stopped.
Stefan Kuhne16045c22015-06-05 07:18:06 -07003955 *
Dianne Hackborn03abb812010-01-04 18:43:19 -08003956 * @hide This is not available to third party applications due to
3957 * it allowing them to break other applications by stopping their
3958 * services, removing their alarms, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003960 @UnsupportedAppUsage
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003961 public void forceStopPackageAsUser(String packageName, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003963 getService().forceStopPackage(packageName, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003965 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 }
3967 }
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003968
3969 /**
3970 * @see #forceStopPackageAsUser(String, int)
3971 * @hide
3972 */
lumark793e0562018-07-09 22:14:33 +08003973 @SystemApi @TestApi
Jeff Schumacherb8cadef2017-01-03 13:52:48 -08003974 @RequiresPermission(Manifest.permission.FORCE_STOP_PACKAGES)
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003975 public void forceStopPackage(String packageName) {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07003976 forceStopPackageAsUser(packageName, mContext.getUserId());
Zoltan Szatmary-Ban0bec95d2014-09-23 17:27:17 +01003977 }
3978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 /**
Maurice Lam522ecbe2018-12-04 20:21:40 -08003980 * Sets the current locales of the device. Calling app must have the permission
3981 * {@code android.permission.CHANGE_CONFIGURATION} and
3982 * {@code android.permission.WRITE_SETTINGS}.
3983 *
3984 * @hide
3985 */
3986 @SystemApi
3987 public void setDeviceLocales(@NonNull LocaleList locales) {
3988 LocalePicker.updateLocales(locales);
3989 }
3990
3991 /**
3992 * Returns a list of supported locales by this system. It includes all locales that are
3993 * selectable by the user, potentially including locales that the framework does not have
3994 * translated resources for. To get locales that the framework has translated resources for, use
3995 * {@code Resources.getSystem().getAssets().getLocales()} instead.
3996 *
3997 * @hide
3998 */
3999 @SystemApi
4000 public @NonNull Collection<Locale> getSupportedLocales() {
4001 ArrayList<Locale> locales = new ArrayList<>();
4002 for (String localeTag : LocalePicker.getSupportedLocales(mContext)) {
4003 locales.add(Locale.forLanguageTag(localeTag));
4004 }
4005 return locales;
4006 }
4007
4008 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 * Get the device configuration attributes.
4010 */
4011 public ConfigurationInfo getDeviceConfigurationInfo() {
4012 try {
Yunfan Chen75157d72018-07-27 14:47:21 +09004013 return getTaskService().getDeviceConfigurationInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004015 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 }
Kenny Root5ef44b72011-01-26 17:22:20 -08004018
4019 /**
4020 * Get the preferred density of icons for the launcher. This is used when
4021 * custom drawables are created (e.g., for shortcuts).
4022 *
4023 * @return density in terms of DPI
4024 */
4025 public int getLauncherLargeIconDensity() {
4026 final Resources res = mContext.getResources();
4027 final int density = res.getDisplayMetrics().densityDpi;
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08004028 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08004029
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08004030 if (sw < 600) {
4031 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08004032 return density;
4033 }
4034
4035 switch (density) {
4036 case DisplayMetrics.DENSITY_LOW:
4037 return DisplayMetrics.DENSITY_MEDIUM;
4038 case DisplayMetrics.DENSITY_MEDIUM:
4039 return DisplayMetrics.DENSITY_HIGH;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07004040 case DisplayMetrics.DENSITY_TV:
4041 return DisplayMetrics.DENSITY_XHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08004042 case DisplayMetrics.DENSITY_HIGH:
4043 return DisplayMetrics.DENSITY_XHIGH;
4044 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08004045 return DisplayMetrics.DENSITY_XXHIGH;
4046 case DisplayMetrics.DENSITY_XXHIGH:
4047 return DisplayMetrics.DENSITY_XHIGH * 2;
Kenny Root5ef44b72011-01-26 17:22:20 -08004048 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08004049 // The density is some abnormal value. Return some other
4050 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07004051 return (int)((density*1.5f)+.5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08004052 }
4053 }
4054
4055 /**
4056 * Get the preferred launcher icon size. This is used when custom drawables
4057 * are created (e.g., for shortcuts).
4058 *
4059 * @return dimensions of square icons in terms of pixels
4060 */
4061 public int getLauncherLargeIconSize() {
Craig Mautner2fbd7542014-03-21 09:34:07 -07004062 return getLauncherLargeIconSizeInner(mContext);
4063 }
4064
4065 static int getLauncherLargeIconSizeInner(Context context) {
4066 final Resources res = context.getResources();
Kenny Root5ef44b72011-01-26 17:22:20 -08004067 final int size = res.getDimensionPixelSize(android.R.dimen.app_icon_size);
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08004068 final int sw = res.getConfiguration().smallestScreenWidthDp;
Kenny Root5ef44b72011-01-26 17:22:20 -08004069
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08004070 if (sw < 600) {
4071 // Smaller than approx 7" tablets, use the regular icon size.
Kenny Root5ef44b72011-01-26 17:22:20 -08004072 return size;
4073 }
4074
4075 final int density = res.getDisplayMetrics().densityDpi;
4076
4077 switch (density) {
4078 case DisplayMetrics.DENSITY_LOW:
4079 return (size * DisplayMetrics.DENSITY_MEDIUM) / DisplayMetrics.DENSITY_LOW;
4080 case DisplayMetrics.DENSITY_MEDIUM:
4081 return (size * DisplayMetrics.DENSITY_HIGH) / DisplayMetrics.DENSITY_MEDIUM;
Dianne Hackbornd0356a12012-04-26 19:03:12 -07004082 case DisplayMetrics.DENSITY_TV:
4083 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08004084 case DisplayMetrics.DENSITY_HIGH:
4085 return (size * DisplayMetrics.DENSITY_XHIGH) / DisplayMetrics.DENSITY_HIGH;
4086 case DisplayMetrics.DENSITY_XHIGH:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08004087 return (size * DisplayMetrics.DENSITY_XXHIGH) / DisplayMetrics.DENSITY_XHIGH;
4088 case DisplayMetrics.DENSITY_XXHIGH:
4089 return (size * DisplayMetrics.DENSITY_XHIGH*2) / DisplayMetrics.DENSITY_XXHIGH;
Kenny Root5ef44b72011-01-26 17:22:20 -08004090 default:
Dianne Hackbornd96e3df2012-01-25 15:12:23 -08004091 // The density is some abnormal value. Return some other
4092 // abnormal value that is a reasonable scaling of it.
Dianne Hackbornd0356a12012-04-26 19:03:12 -07004093 return (int)((size*1.5f) + .5f);
Kenny Root5ef44b72011-01-26 17:22:20 -08004094 }
4095 }
4096
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08004097 /**
4098 * Returns "true" if the user interface is currently being messed with
4099 * by a monkey.
4100 */
4101 public static boolean isUserAMonkey() {
4102 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004103 return getService().isUserAMonkey();
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08004104 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004105 throw e.rethrowFromSystemServer();
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08004106 }
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08004107 }
Brett Chabot3b4fcbc2011-01-09 13:41:02 -08004108
4109 /**
4110 * Returns "true" if device is running in a test harness.
William Hestera96d3d32018-12-19 13:14:51 -08004111 *
4112 * @deprecated this method is false for all user builds. Users looking to check if their device
4113 * is running in a device farm should see {@link #isRunningInUserTestHarness()}.
Brett Chabot3b4fcbc2011-01-09 13:41:02 -08004114 */
William Hestera96d3d32018-12-19 13:14:51 -08004115 @Deprecated
Brett Chabot3b4fcbc2011-01-09 13:41:02 -08004116 public static boolean isRunningInTestHarness() {
4117 return SystemProperties.getBoolean("ro.test_harness", false);
4118 }
Peter Visontay8d224ca2011-02-18 16:39:19 +00004119
4120 /**
William Hestera96d3d32018-12-19 13:14:51 -08004121 * Returns "true" if the device is running in Test Harness Mode.
4122 *
4123 * <p>Test Harness Mode is a feature that allows devices to run without human interaction in a
4124 * device farm/testing harness (such as Firebase Test Lab). You should check this method if you
4125 * want your app to behave differently when running in a test harness to skip setup screens that
4126 * would impede UI testing. e.g. a keyboard application that has a full screen setup page for
4127 * the first time it is launched.
4128 *
4129 * <p>Note that you should <em>not</em> use this to determine whether or not your app is running
4130 * an instrumentation test, as it is not set for a standard device running a test.
4131 */
4132 public static boolean isRunningInUserTestHarness() {
4133 return SystemProperties.getBoolean("persist.sys.test_harness", false);
4134 }
4135
4136 /**
Wale Ogunwale402de822018-03-22 10:03:06 -07004137 * Unsupported compiled sdk warning should always be shown for the intput activity
4138 * even in cases where the system would normally not show the warning. E.g. when running in a
4139 * test harness.
4140 *
4141 * @param activity The component name of the activity to always show the warning for.
4142 *
4143 * @hide
4144 */
4145 @TestApi
4146 public void alwaysShowUnsupportedCompileSdkWarning(ComponentName activity) {
4147 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004148 getTaskService().alwaysShowUnsupportedCompileSdkWarning(activity);
Wale Ogunwale402de822018-03-22 10:03:06 -07004149 } catch (RemoteException e) {
4150 throw e.rethrowFromSystemServer();
4151 }
4152 }
4153
4154 /**
Peter Visontay8d224ca2011-02-18 16:39:19 +00004155 * Returns the launch count of each installed package.
4156 *
4157 * @hide
4158 */
Adam Lesinski0debc9a2014-07-16 19:09:13 -07004159 /*public Map<String, Integer> getAllPackageLaunchCounts() {
Peter Visontay8d224ca2011-02-18 16:39:19 +00004160 try {
4161 IUsageStats usageStatsService = IUsageStats.Stub.asInterface(
4162 ServiceManager.getService("usagestats"));
4163 if (usageStatsService == null) {
4164 return new HashMap<String, Integer>();
4165 }
4166
Dianne Hackborne22b3b12014-05-07 18:06:44 -07004167 UsageStats.PackageStats[] allPkgUsageStats = usageStatsService.getAllPkgUsageStats(
4168 ActivityThread.currentPackageName());
Peter Visontaybfcda392011-03-02 18:53:37 +00004169 if (allPkgUsageStats == null) {
4170 return new HashMap<String, Integer>();
4171 }
4172
Peter Visontay8d224ca2011-02-18 16:39:19 +00004173 Map<String, Integer> launchCounts = new HashMap<String, Integer>();
Dianne Hackborne22b3b12014-05-07 18:06:44 -07004174 for (UsageStats.PackageStats pkgUsageStats : allPkgUsageStats) {
4175 launchCounts.put(pkgUsageStats.getPackageName(), pkgUsageStats.getLaunchCount());
Peter Visontay8d224ca2011-02-18 16:39:19 +00004176 }
4177
4178 return launchCounts;
4179 } catch (RemoteException e) {
4180 Log.w(TAG, "Could not query launch counts", e);
4181 return new HashMap<String, Integer>();
4182 }
Adam Lesinski0debc9a2014-07-16 19:09:13 -07004183 }*/
Amith Yamasani4b2e9342011-03-31 12:38:53 -07004184
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004185 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004186 @UnsupportedAppUsage
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004187 public static int checkComponentPermission(String permission, int uid,
4188 int owningUid, boolean exported) {
4189 // Root, system server get to do everything.
Svetoslavc6d1c342015-02-26 14:44:43 -08004190 final int appId = UserHandle.getAppId(uid);
4191 if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004192 return PackageManager.PERMISSION_GRANTED;
4193 }
4194 // Isolated processes don't get any permissions.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004195 if (UserHandle.isIsolated(uid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004196 return PackageManager.PERMISSION_DENIED;
4197 }
4198 // If there is a uid that owns whatever is being accessed, it has
4199 // blanket access to it regardless of the permissions it requires.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004200 if (owningUid >= 0 && UserHandle.isSameApp(uid, owningUid)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004201 return PackageManager.PERMISSION_GRANTED;
4202 }
4203 // If the target is not exported, then nobody else can get to it.
4204 if (!exported) {
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -07004205 /*
4206 RuntimeException here = new RuntimeException("here");
4207 here.fillInStackTrace();
4208 Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid,
4209 here);
4210 */
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004211 return PackageManager.PERMISSION_DENIED;
4212 }
4213 if (permission == null) {
4214 return PackageManager.PERMISSION_GRANTED;
4215 }
Tim Murrayd06c6d292019-05-07 06:53:55 +00004216 try {
4217 return AppGlobals.getPackageManager()
4218 .checkUidPermission(permission, uid);
4219 } catch (RemoteException e) {
4220 throw e.rethrowFromSystemServer();
4221 }
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004222 }
4223
Dianne Hackborn7d19e022012-08-07 19:12:33 -07004224 /** @hide */
4225 public static int checkUidPermission(String permission, int uid) {
4226 try {
Tim Murrayd06c6d292019-05-07 06:53:55 +00004227 return AppGlobals.getPackageManager()
4228 .checkUidPermission(permission, uid);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07004229 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004230 throw e.rethrowFromSystemServer();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07004231 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07004232 }
4233
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08004234 /**
4235 * @hide
4236 * Helper for dealing with incoming user arguments to system service calls.
4237 * Takes care of checking permissions and converting USER_CURRENT to the
4238 * actual current user.
4239 *
4240 * @param callingPid The pid of the incoming call, as per Binder.getCallingPid().
4241 * @param callingUid The uid of the incoming call, as per Binder.getCallingUid().
4242 * @param userId The user id argument supplied by the caller -- this is the user
4243 * they want to run as.
4244 * @param allowAll If true, we will allow USER_ALL. This means you must be prepared
4245 * to get a USER_ALL returned and deal with it correctly. If false,
4246 * an exception will be thrown if USER_ALL is supplied.
4247 * @param requireFull If true, the caller must hold
4248 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} to be able to run as a
4249 * different user than their current process; otherwise they must hold
4250 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS}.
4251 * @param name Optional textual name of the incoming call; only for generating error messages.
4252 * @param callerPackage Optional package name of caller; only for error messages.
4253 *
4254 * @return Returns the user ID that the call should run as. Will always be a concrete
4255 * user number, unless <var>allowAll</var> is true in which case it could also be
4256 * USER_ALL.
4257 */
Dianne Hackborn41203752012-08-31 14:05:51 -07004258 public static int handleIncomingUser(int callingPid, int callingUid, int userId,
4259 boolean allowAll, boolean requireFull, String name, String callerPackage) {
4260 if (UserHandle.getUserId(callingUid) == userId) {
4261 return userId;
4262 }
4263 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004264 return getService().handleIncomingUser(callingPid,
Dianne Hackborn41203752012-08-31 14:05:51 -07004265 callingUid, userId, allowAll, requireFull, name, callerPackage);
4266 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004267 throw e.rethrowFromSystemServer();
Dianne Hackborn41203752012-08-31 14:05:51 -07004268 }
4269 }
4270
Amith Yamasani0e8d7d62014-09-03 13:17:28 -07004271 /**
4272 * Gets the userId of the current foreground user. Requires system permissions.
4273 * @hide
4274 */
4275 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06004276 @RequiresPermission(anyOf = {
4277 "android.permission.INTERACT_ACROSS_USERS",
4278 "android.permission.INTERACT_ACROSS_USERS_FULL"
4279 })
Dianne Hackborn41203752012-08-31 14:05:51 -07004280 public static int getCurrentUser() {
4281 UserInfo ui;
4282 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004283 ui = getService().getCurrentUser();
Dianne Hackborn41203752012-08-31 14:05:51 -07004284 return ui != null ? ui.id : 0;
4285 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004286 throw e.rethrowFromSystemServer();
Dianne Hackborn41203752012-08-31 14:05:51 -07004287 }
4288 }
4289
Amith Yamasani4b2e9342011-03-31 12:38:53 -07004290 /**
Stefan Kuhne16045c22015-06-05 07:18:06 -07004291 * @param userid the user's id. Zero indicates the default user.
Amith Yamasani4b2e9342011-03-31 12:38:53 -07004292 * @hide
4293 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004294 @UnsupportedAppUsage
Amith Yamasani4b2e9342011-03-31 12:38:53 -07004295 public boolean switchUser(int userid) {
4296 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004297 return getService().switchUser(userid);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07004298 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004299 throw e.rethrowFromSystemServer();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07004300 }
4301 }
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07004302
Fyodor Kupolovb5690bc32016-03-02 17:50:58 -08004303 /**
Varun Shah3e75d752018-11-30 18:47:05 -08004304 * Returns whether switching to provided user was successful.
4305 *
4306 * @param user the user to switch to.
Varun Shahe7f6b542019-01-15 17:04:59 -08004307 *
4308 * @throws IllegalArgumentException if the user is null.
Varun Shah3e75d752018-11-30 18:47:05 -08004309 * @hide
4310 */
4311 @SystemApi
Nate Myren3f262712019-05-31 09:36:08 -07004312 @TestApi
Varun Shah3e75d752018-11-30 18:47:05 -08004313 @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
Varun Shahe7f6b542019-01-15 17:04:59 -08004314 public boolean switchUser(@NonNull UserHandle user) {
4315 if (user == null) {
4316 throw new IllegalArgumentException("UserHandle cannot be null.");
4317 }
Varun Shah3e75d752018-11-30 18:47:05 -08004318 return switchUser(user.getIdentifier());
4319 }
4320
4321 /**
Shuo Qianb9c9bfa2020-01-08 17:59:42 -08004322 * Updates mcc mnc configuration and applies changes to the entire system.
4323 *
4324 * @param mcc mcc configuration to update.
4325 * @param mnc mnc configuration to update.
4326 * @throws RemoteException; IllegalArgumentException if mcc or mnc is null;
4327 * @return Returns {@code true} if the configuration was updated successfully;
4328 * {@code false} otherwise.
4329 * @hide
4330 */
Shuo Qianb9c9bfa2020-01-08 17:59:42 -08004331 @RequiresPermission(android.Manifest.permission.CHANGE_CONFIGURATION)
4332 public boolean updateMccMncConfiguration(@NonNull String mcc, @NonNull String mnc) {
4333 if (mcc == null || mnc == null) {
4334 throw new IllegalArgumentException("mcc or mnc cannot be null.");
4335 }
4336 try {
4337 return getService().updateMccMncConfiguration(mcc, mnc);
4338 } catch (RemoteException e) {
4339 throw e.rethrowFromSystemServer();
4340 }
4341 }
4342
4343 /**
Fyodor Kupolovb5690bc32016-03-02 17:50:58 -08004344 * Logs out current current foreground user by switching to the system user and stopping the
4345 * user being switched from.
4346 * @hide
4347 */
4348 public static void logoutCurrentUser() {
4349 int currentUser = ActivityManager.getCurrentUser();
4350 if (currentUser != UserHandle.USER_SYSTEM) {
4351 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004352 getService().switchUser(UserHandle.USER_SYSTEM);
4353 getService().stopUser(currentUser, /* force= */ false, null);
Fyodor Kupolovb5690bc32016-03-02 17:50:58 -08004354 } catch (RemoteException e) {
4355 e.rethrowFromSystemServer();
4356 }
4357 }
4358 }
4359
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004360 /** {@hide} */
4361 public static final int FLAG_OR_STOPPED = 1 << 0;
4362 /** {@hide} */
Jeff Sharkey8924e872015-11-30 12:52:10 -07004363 public static final int FLAG_AND_LOCKED = 1 << 1;
Jeff Sharkey0825ab22015-12-02 13:04:49 -07004364 /** {@hide} */
4365 public static final int FLAG_AND_UNLOCKED = 1 << 2;
Jeff Sharkeyce18c812016-04-27 16:00:41 -06004366 /** {@hide} */
4367 public static final int FLAG_AND_UNLOCKING_OR_UNLOCKED = 1 << 3;
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004368
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07004369 /**
4370 * Return whether the given user is actively running. This means that
4371 * the user is in the "started" state, not "stopped" -- it is currently
4372 * allowed to run code through scheduled alarms, receiving broadcasts,
4373 * etc. A started user may be either the current foreground user or a
4374 * background user; the result here does not distinguish between the two.
Dianne Hackborn5614bf52016-11-07 17:26:41 -08004375 * @param userId the user's id. Zero indicates the default user.
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07004376 * @hide
4377 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004378 @UnsupportedAppUsage
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07004379 public boolean isUserRunning(int userId) {
Dianne Hackborn5e03e2c2012-09-06 14:21:19 -07004380 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004381 return getService().isUserRunning(userId, 0);
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07004382 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004383 throw e.rethrowFromSystemServer();
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07004384 }
4385 }
4386
4387 /** {@hide} */
Ruben Brunke24b9a62016-02-16 21:38:24 -08004388 public boolean isVrModePackageEnabled(ComponentName component) {
4389 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004390 return getService().isVrModePackageEnabled(component);
Ruben Brunke24b9a62016-02-16 21:38:24 -08004391 } catch (RemoteException e) {
4392 throw e.rethrowFromSystemServer();
4393 }
4394 }
4395
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004396 /**
4397 * Perform a system dump of various state associated with the given application
4398 * package name. This call blocks while the dump is being performed, so should
4399 * not be done on a UI thread. The data will be written to the given file
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004400 * descriptor as text.
Dianne Hackbornebc15ef2013-10-09 17:36:57 -07004401 * @param fd The file descriptor that the dump should be written to. The file
4402 * descriptor is <em>not</em> closed by this function; the caller continues to
4403 * own it.
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004404 * @param packageName The name of the package that is to be dumped.
4405 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004406 @RequiresPermission(Manifest.permission.DUMP)
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004407 public void dumpPackageState(FileDescriptor fd, String packageName) {
4408 dumpPackageStateStatic(fd, packageName);
4409 }
4410
4411 /**
4412 * @hide
4413 */
4414 public static void dumpPackageStateStatic(FileDescriptor fd, String packageName) {
4415 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07004416 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07004417 dumpService(pw, fd, "package", new String[] { packageName });
4418 pw.println();
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08004419 dumpService(pw, fd, Context.ACTIVITY_SERVICE, new String[] {
4420 "-a", "package", packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004421 pw.println();
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07004422 dumpService(pw, fd, "meminfo", new String[] { "--local", "--package", packageName });
Dianne Hackborndaa0d5c2013-11-06 16:30:29 -08004423 pw.println();
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07004424 dumpService(pw, fd, ProcessStats.SERVICE_NAME, new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004425 pw.println();
Esteban Talavera838ea242017-12-19 11:48:43 +00004426 dumpService(pw, fd, "usagestats", new String[] { packageName });
Dianne Hackborn904a8572013-06-28 18:12:31 -07004427 pw.println();
Dianne Hackborn8c841092013-06-24 13:46:13 -07004428 dumpService(pw, fd, BatteryStats.SERVICE_NAME, new String[] { packageName });
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004429 pw.flush();
4430 }
4431
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004432 /**
4433 * @hide
4434 */
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004435 public static boolean isSystemReady() {
4436 if (!sSystemReady) {
4437 if (ActivityThread.isSystem()) {
4438 sSystemReady =
4439 LocalServices.getService(ActivityManagerInternal.class).isSystemReady();
4440 } else {
4441 // Since this is being called from outside system server, system should be
4442 // ready by now.
4443 sSystemReady = true;
4444 }
4445 }
4446 return sSystemReady;
4447 }
4448
4449 /**
4450 * @hide
4451 */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004452 public static void broadcastStickyIntent(Intent intent, int userId) {
4453 broadcastStickyIntent(intent, AppOpsManager.OP_NONE, userId);
4454 }
4455
4456 /**
4457 * Convenience for sending a sticky broadcast. For internal use only.
4458 *
4459 * @hide
4460 */
4461 public static void broadcastStickyIntent(Intent intent, int appOp, int userId) {
4462 try {
Philip P. Moltmannee295092020-02-10 08:46:26 -08004463 getService().broadcastIntentWithFeature(
4464 null, null, intent, null, null, Activity.RESULT_OK, null, null,
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004465 null /*permission*/, appOp, null, false, true, userId);
4466 } catch (RemoteException ex) {
4467 }
4468 }
4469
4470 /**
4471 * @hide
4472 */
Wale Ogunwale691af682019-02-11 03:09:10 -08004473 @TestApi
4474 public static void resumeAppSwitches() throws RemoteException {
4475 getService().resumeAppSwitches();
4476 }
4477
4478 /**
4479 * @hide
4480 */
Narayan Kamath695cf722017-12-21 18:32:47 +00004481 public static void noteWakeupAlarm(PendingIntent ps, WorkSource workSource, int sourceUid,
4482 String sourcePkg, String tag) {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004483 try {
Narayan Kamath695cf722017-12-21 18:32:47 +00004484 getService().noteWakeupAlarm((ps != null) ? ps.getTarget() : null, workSource,
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004485 sourceUid, sourcePkg, tag);
4486 } catch (RemoteException ex) {
4487 }
4488 }
4489
4490 /**
4491 * @hide
4492 */
Narayan Kamath695cf722017-12-21 18:32:47 +00004493 public static void noteAlarmStart(PendingIntent ps, WorkSource workSource, int sourceUid,
4494 String tag) {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004495 try {
Narayan Kamath695cf722017-12-21 18:32:47 +00004496 getService().noteAlarmStart((ps != null) ? ps.getTarget() : null, workSource,
4497 sourceUid, tag);
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004498 } catch (RemoteException ex) {
4499 }
4500 }
4501
Narayan Kamath695cf722017-12-21 18:32:47 +00004502
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004503 /**
4504 * @hide
4505 */
Narayan Kamath695cf722017-12-21 18:32:47 +00004506 public static void noteAlarmFinish(PendingIntent ps, WorkSource workSource, int sourceUid,
4507 String tag) {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004508 try {
Narayan Kamath695cf722017-12-21 18:32:47 +00004509 getService().noteAlarmFinish((ps != null) ? ps.getTarget() : null, workSource,
4510 sourceUid, tag);
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004511 } catch (RemoteException ex) {
4512 }
4513 }
4514
4515 /**
4516 * @hide
4517 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004518 @UnsupportedAppUsage
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004519 public static IActivityManager getService() {
4520 return IActivityManagerSingleton.get();
4521 }
4522
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004523 private static IActivityTaskManager getTaskService() {
4524 return ActivityTaskManager.getService();
4525 }
4526
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004527 @UnsupportedAppUsage
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004528 private static final Singleton<IActivityManager> IActivityManagerSingleton =
4529 new Singleton<IActivityManager>() {
4530 @Override
4531 protected IActivityManager create() {
4532 final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
4533 final IActivityManager am = IActivityManager.Stub.asInterface(b);
4534 return am;
4535 }
4536 };
4537
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004538 private static void dumpService(PrintWriter pw, FileDescriptor fd, String name, String[] args) {
4539 pw.print("DUMP OF SERVICE "); pw.print(name); pw.println(":");
4540 IBinder service = ServiceManager.checkService(name);
4541 if (service == null) {
4542 pw.println(" (Service not found)");
Dianne Hackbornc81983a2017-10-20 16:16:32 -07004543 pw.flush();
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004544 return;
4545 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07004546 pw.flush();
4547 if (service instanceof Binder) {
4548 // If this is a local object, it doesn't make sense to do an async dump with it,
4549 // just directly dump.
4550 try {
4551 service.dump(fd, args);
4552 } catch (Throwable e) {
4553 pw.println("Failure dumping service:");
4554 e.printStackTrace(pw);
4555 pw.flush();
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004556 }
Dianne Hackbornc81983a2017-10-20 16:16:32 -07004557 } else {
4558 // Otherwise, it is remote, do the dump asynchronously to avoid blocking.
4559 TransferPipe tp = null;
4560 try {
4561 pw.flush();
4562 tp = new TransferPipe();
4563 tp.setBufferPrefix(" ");
4564 service.dumpAsync(tp.getWriteFd().getFileDescriptor(), args);
4565 tp.go(fd, 10000);
4566 } catch (Throwable e) {
4567 if (tp != null) {
4568 tp.kill();
4569 }
4570 pw.println("Failure dumping service:");
4571 e.printStackTrace(pw);
4572 }
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07004573 }
4574 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004575
4576 /**
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07004577 * Request that the system start watching for the calling process to exceed a pss
Dianne Hackborn17f69352015-07-17 18:04:14 -07004578 * size as given here. Once called, the system will look for any occasions where it
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07004579 * sees the associated process with a larger pss size and, when this happens, automatically
4580 * pull a heap dump from it and allow the user to share the data. Note that this request
4581 * continues running even if the process is killed and restarted. To remove the watch,
4582 * use {@link #clearWatchHeapLimit()}.
4583 *
Kweku Adamsfa825632019-03-04 16:19:19 -08004584 * <p>This API only works if the calling process has been marked as
Dianne Hackbornece0f4f2015-06-11 13:29:01 -07004585 * {@link ApplicationInfo#FLAG_DEBUGGABLE} or this is running on a debuggable
4586 * (userdebug or eng) build.</p>
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07004587 *
4588 * <p>Callers can optionally implement {@link #ACTION_REPORT_HEAP_LIMIT} to directly
4589 * handle heap limit reports themselves.</p>
4590 *
4591 * @param pssSize The size in bytes to set the limit at.
4592 */
4593 public void setWatchHeapLimit(long pssSize) {
4594 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004595 getService().setDumpHeapDebugLimit(null, 0, pssSize,
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07004596 mContext.getPackageName());
4597 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004598 throw e.rethrowFromSystemServer();
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07004599 }
4600 }
4601
4602 /**
4603 * Action an app can implement to handle reports from {@link #setWatchHeapLimit(long)}.
4604 * If your package has an activity handling this action, it will be launched with the
4605 * heap data provided to it the same way as {@link Intent#ACTION_SEND}. Note that to
Kweku Adams6d4bd8e2019-06-26 17:05:55 -07004606 * match, the activity must support this action and a MIME type of "*&#47;*".
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07004607 */
4608 public static final String ACTION_REPORT_HEAP_LIMIT = "android.app.action.REPORT_HEAP_LIMIT";
4609
4610 /**
4611 * Clear a heap watch limit previously set by {@link #setWatchHeapLimit(long)}.
4612 */
4613 public void clearWatchHeapLimit() {
4614 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004615 getService().setDumpHeapDebugLimit(null, 0, 0, null);
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07004616 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004617 throw e.rethrowFromSystemServer();
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07004618 }
4619 }
4620
4621 /**
Jason Monk386c94f2014-07-14 16:42:24 -04004622 * Return whether currently in lock task mode. When in this mode
4623 * no new tasks can be created or switched to.
4624 *
4625 * @see Activity#startLockTask()
Benjamin Franz43261142015-02-11 15:59:44 +00004626 *
4627 * @deprecated Use {@link #getLockTaskModeState} instead.
Craig Mautneraea74a52014-03-08 14:23:10 -08004628 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07004629 @Deprecated
Craig Mautneraea74a52014-03-08 14:23:10 -08004630 public boolean isInLockTaskMode() {
Benjamin Franz43261142015-02-11 15:59:44 +00004631 return getLockTaskModeState() != LOCK_TASK_MODE_NONE;
4632 }
4633
4634 /**
4635 * Return the current state of task locking. The three possible outcomes
4636 * are {@link #LOCK_TASK_MODE_NONE}, {@link #LOCK_TASK_MODE_LOCKED}
4637 * and {@link #LOCK_TASK_MODE_PINNED}.
4638 *
4639 * @see Activity#startLockTask()
4640 */
4641 public int getLockTaskModeState() {
Craig Mautneraea74a52014-03-08 14:23:10 -08004642 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07004643 return getTaskService().getLockTaskModeState();
Craig Mautneraea74a52014-03-08 14:23:10 -08004644 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004645 throw e.rethrowFromSystemServer();
Craig Mautneraea74a52014-03-08 14:23:10 -08004646 }
4647 }
Winson Chung1147c402014-05-14 11:05:00 -07004648
4649 /**
Srinath Sridharane535a582016-06-27 18:13:47 -07004650 * Enable more aggressive scheduling for latency-sensitive low-runtime VR threads. Only one
4651 * thread can be a VR thread in a process at a time, and that thread may be subject to
4652 * restrictions on the amount of time it can run.
4653 *
Craig Donnerdc4796c2017-03-08 09:51:47 -08004654 * If persistent VR mode is set, whatever thread has been granted aggressive scheduling via this
4655 * method will return to normal operation, and calling this method will do nothing while
4656 * persistent VR mode is enabled.
4657 *
Srinath Sridharane535a582016-06-27 18:13:47 -07004658 * To reset the VR thread for an application, a tid of 0 can be passed.
4659 *
4660 * @see android.os.Process#myTid()
4661 * @param tid tid of the VR thread
4662 */
4663 public static void setVrThread(int tid) {
4664 try {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07004665 getTaskService().setVrThread(tid);
Srinath Sridharane535a582016-06-27 18:13:47 -07004666 } catch (RemoteException e) {
4667 // pass
4668 }
4669 }
4670
4671 /**
Craig Donnerdc4796c2017-03-08 09:51:47 -08004672 * Enable more aggressive scheduling for latency-sensitive low-runtime VR threads that persist
Jeff Sharkey910e0812017-04-21 16:29:27 -06004673 * beyond a single process. Only one thread can be a
Craig Donnerdc4796c2017-03-08 09:51:47 -08004674 * persistent VR thread at a time, and that thread may be subject to restrictions on the amount
4675 * of time it can run. Calling this method will disable aggressive scheduling for non-persistent
4676 * VR threads set via {@link #setVrThread}. If persistent VR mode is disabled then the
4677 * persistent VR thread loses its new scheduling priority; this method must be called again to
4678 * set the persistent thread.
4679 *
4680 * To reset the persistent VR thread, a tid of 0 can be passed.
4681 *
4682 * @see android.os.Process#myTid()
4683 * @param tid tid of the VR thread
4684 * @hide
4685 */
rongliu55492662018-03-21 14:34:58 -07004686 @SystemApi
Craig Donnerdc4796c2017-03-08 09:51:47 -08004687 @RequiresPermission(Manifest.permission.RESTRICTED_VR_ACCESS)
4688 public static void setPersistentVrThread(int tid) {
4689 try {
4690 getService().setPersistentVrThread(tid);
4691 } catch (RemoteException e) {
4692 // pass
4693 }
4694 }
4695
4696 /**
lumark793e0562018-07-09 22:14:33 +08004697 * @hide
4698 */
4699 @TestApi
4700 @RequiresPermission(Manifest.permission.CHANGE_CONFIGURATION)
4701 public void scheduleApplicationInfoChanged(List<String> packages, int userId) {
4702 try {
4703 getService().scheduleApplicationInfoChanged(packages, userId);
4704 } catch (RemoteException e) {
4705 throw e.rethrowFromSystemServer();
4706 }
4707 }
4708
4709 /**
Grace Jia15b4c262020-01-07 14:25:24 -08004710 * Return if a given profile is in the foreground.
4711 * @param userHandle UserHandle to check
4712 * @return Returns the boolean result.
4713 * @hide
4714 */
Grace Jia15b4c262020-01-07 14:25:24 -08004715 @RequiresPermission(anyOf = {
4716 android.Manifest.permission.MANAGE_USERS,
4717 android.Manifest.permission.CREATE_USERS
4718 })
4719 public boolean isProfileForeground(@NonNull UserHandle userHandle) {
4720 UserManager userManager = mContext.getSystemService(UserManager.class);
4721 if (userManager != null) {
4722 for (UserInfo userInfo : userManager.getProfiles(getCurrentUser())) {
4723 if (userInfo.id == userHandle.getIdentifier()) {
4724 return true;
4725 }
4726 }
4727 }
4728 return false;
4729 }
4730
4731 /**
Jing Jie423f762019-12-10 15:05:18 -08004732 * Kill the given PIDs, but the killing will be delayed until the device is idle
4733 * and the given process is imperceptible.
4734 *
4735 * <p>You must hold the permission
4736 * {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to
4737 * call this method.
4738 * </p>
4739 *
4740 * @param pids The list of the pids to be killed
4741 * @pram reason The reason of the kill
4742 *
4743 * @hide
4744 */
4745 @SystemApi @TestApi
4746 @RequiresPermission(Manifest.permission.FORCE_STOP_PACKAGES)
4747 public void killProcessesWhenImperceptible(@NonNull int[] pids, @NonNull String reason) {
4748 try {
4749 getService().killProcessesWhenImperceptible(pids, reason);
4750 } catch (RemoteException e) {
4751 throw e.rethrowFromSystemServer();
4752 }
4753 }
4754
4755 /**
Winson Chung1147c402014-05-14 11:05:00 -07004756 * The AppTask allows you to manage your own application's tasks.
4757 * See {@link android.app.ActivityManager#getAppTasks()}
4758 */
4759 public static class AppTask {
4760 private IAppTask mAppTaskImpl;
4761
4762 /** @hide */
4763 public AppTask(IAppTask task) {
4764 mAppTaskImpl = task;
4765 }
4766
4767 /**
4768 * Finishes all activities in this task and removes it from the recent tasks list.
4769 */
4770 public void finishAndRemoveTask() {
4771 try {
4772 mAppTaskImpl.finishAndRemoveTask();
4773 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004774 throw e.rethrowFromSystemServer();
Winson Chung1147c402014-05-14 11:05:00 -07004775 }
4776 }
4777
4778 /**
4779 * Get the RecentTaskInfo associated with this task.
4780 *
4781 * @return The RecentTaskInfo for this task, or null if the task no longer exists.
4782 */
4783 public RecentTaskInfo getTaskInfo() {
4784 try {
4785 return mAppTaskImpl.getTaskInfo();
4786 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004787 throw e.rethrowFromSystemServer();
Winson Chung1147c402014-05-14 11:05:00 -07004788 }
4789 }
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07004790
4791 /**
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004792 * Bring this task to the foreground. If it contains activities, they will be
4793 * brought to the foreground with it and their instances re-created if needed.
4794 * If it doesn't contain activities, the root activity of the task will be
4795 * re-launched.
4796 */
4797 public void moveToFront() {
4798 try {
Ricky Waiaca8a772019-04-04 16:01:06 +01004799 ActivityThread thread = ActivityThread.currentActivityThread();
4800 IApplicationThread appThread = thread.getApplicationThread();
4801 String packageName = ActivityThread.currentPackageName();
4802 mAppTaskImpl.moveToFront(appThread, packageName);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004803 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004804 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004805 }
4806 }
4807
4808 /**
4809 * Start an activity in this task. Brings the task to the foreground. If this task
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004810 * is not currently active (that is, its id < 0), then a new activity for the given
4811 * Intent will be launched as the root of the task and the task brought to the
4812 * foreground. Otherwise, if this task is currently active and the Intent does not specify
4813 * an activity to launch in a new task, then a new activity for the given Intent will
4814 * be launched on top of the task and the task brought to the foreground. If this
4815 * task is currently active and the Intent specifies {@link Intent#FLAG_ACTIVITY_NEW_TASK}
4816 * or would otherwise be launched in to a new task, then the activity not launched but
4817 * this task be brought to the foreground and a new intent delivered to the top
4818 * activity if appropriate.
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004819 *
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004820 * <p>In other words, you generally want to use an Intent here that does not specify
4821 * {@link Intent#FLAG_ACTIVITY_NEW_TASK} or {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT},
4822 * and let the system do the right thing.</p>
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004823 *
4824 * @param intent The Intent describing the new activity to be launched on the task.
4825 * @param options Optional launch options.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004826 *
4827 * @see Activity#startActivity(android.content.Intent, android.os.Bundle)
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004828 */
4829 public void startActivity(Context context, Intent intent, Bundle options) {
4830 ActivityThread thread = ActivityThread.currentActivityThread();
4831 thread.getInstrumentation().execStartActivityFromAppTask(context,
4832 thread.getApplicationThread(), mAppTaskImpl, intent, options);
4833 }
4834
4835 /**
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07004836 * Modify the {@link Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} flag in the root
4837 * Intent of this AppTask.
4838 *
4839 * @param exclude If true, {@link Intent#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS} will
4840 * be set; otherwise, it will be cleared.
4841 */
4842 public void setExcludeFromRecents(boolean exclude) {
4843 try {
4844 mAppTaskImpl.setExcludeFromRecents(exclude);
4845 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004846 throw e.rethrowFromSystemServer();
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07004847 }
4848 }
Winson Chung1147c402014-05-14 11:05:00 -07004849 }
Paul Changc4153512019-12-08 21:35:27 +08004850
4851 /**
4852 * Get packages of bugreport-whitelisted apps to handle a bug report.
4853 *
4854 * @return packages of bugreport-whitelisted apps to handle a bug report.
4855 * @hide
4856 */
4857 public List<String> getBugreportWhitelistedPackages() {
4858 try {
4859 return getService().getBugreportWhitelistedPackages();
4860 } catch (RemoteException e) {
4861 throw e.rethrowFromSystemServer();
4862 }
4863 }
Jing Ji83c6bc52019-12-19 11:53:31 -08004864
4865 /**
4866 * Method for the app to tell system that it's wedged and would like to trigger an ANR.
4867 *
4868 * @param reason The description of that what happened
4869 */
4870 public void appNotResponding(@NonNull final String reason) {
4871 try {
4872 getService().appNotResponding(reason);
4873 } catch (RemoteException e) {
4874 throw e.rethrowFromSystemServer();
4875 }
4876 }
Malcolm Chenc99b83b2020-01-10 15:55:30 -08004877
4878 /**
4879 * Register with {@link HomeVisibilityObserver} with ActivityManager.
Malcolm Chen629f9112020-03-18 18:05:20 -07004880 * TODO: b/144351078 expose as SystemApi
Malcolm Chenc99b83b2020-01-10 15:55:30 -08004881 * @hide
4882 */
Malcolm Chenc99b83b2020-01-10 15:55:30 -08004883 public void registerHomeVisibilityObserver(@NonNull HomeVisibilityObserver observer) {
4884 Preconditions.checkNotNull(observer);
4885 try {
4886 observer.init(mContext, this);
4887 getService().registerProcessObserver(observer.mObserver);
4888 // Notify upon first registration.
4889 observer.onHomeVisibilityChanged(observer.mIsHomeActivityVisible);
4890 } catch (RemoteException e) {
4891 throw e.rethrowFromSystemServer();
4892 }
4893 }
4894
4895 /**
4896 * Unregister with {@link HomeVisibilityObserver} with ActivityManager.
Malcolm Chen629f9112020-03-18 18:05:20 -07004897 * TODO: b/144351078 expose as SystemApi
Malcolm Chenc99b83b2020-01-10 15:55:30 -08004898 * @hide
4899 */
Malcolm Chenc99b83b2020-01-10 15:55:30 -08004900 public void unregisterHomeVisibilityObserver(@NonNull HomeVisibilityObserver observer) {
4901 Preconditions.checkNotNull(observer);
4902 try {
4903 getService().unregisterProcessObserver(observer.mObserver);
4904 } catch (RemoteException e) {
4905 throw e.rethrowFromSystemServer();
4906 }
4907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908}