blob: 73710d3d39d7b1a506307248ad8ccd0f1529f60e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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 com.android.server.am;
18
Winson Chungbb348802017-01-30 12:01:45 -080019import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080020import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Ruben Brunkf53497c2017-03-27 20:26:17 -070021import static android.app.ActivityManager.TaskDescription.ATTR_TASKDESCRIPTION_PREFIX;
22import static android.app.ActivityOptions.ANIM_CLIP_REVEAL;
23import static android.app.ActivityOptions.ANIM_CUSTOM;
Jorim Jaggi33a701a2017-12-01 14:58:18 +010024import static android.app.ActivityOptions.ANIM_REMOTE_ANIMATION;
Ruben Brunkf53497c2017-03-27 20:26:17 -070025import static android.app.ActivityOptions.ANIM_SCALE_UP;
26import static android.app.ActivityOptions.ANIM_SCENE_TRANSITION;
Tony Mak089c35e2017-12-18 20:34:14 +000027import static android.app.ActivityOptions.ANIM_OPEN_CROSS_PROFILE_APPS;
Ruben Brunkf53497c2017-03-27 20:26:17 -070028import static android.app.ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN;
29import static android.app.ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP;
30import static android.app.ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN;
31import static android.app.ActivityOptions.ANIM_THUMBNAIL_SCALE_UP;
Winson Chung59fda9e2017-01-20 16:14:51 -080032import static android.app.AppOpsManager.MODE_ALLOWED;
Winson Chungf4ac0632017-03-17 12:34:12 -070033import static android.app.AppOpsManager.OP_PICTURE_IN_PICTURE;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070034import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
35import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
36import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070037import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
38import static android.app.WindowConfiguration.activityTypeToString;
Ruben Brunkf53497c2017-03-27 20:26:17 -070039import static android.content.Intent.ACTION_MAIN;
40import static android.content.Intent.CATEGORY_HOME;
41import static android.content.Intent.CATEGORY_LAUNCHER;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080042import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Bryce Leeb7c9b802017-05-02 14:20:24 -070043import static android.content.Intent.FLAG_ACTIVITY_NO_HISTORY;
Andrii Kulian21713ac2016-10-12 22:05:05 -070044import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
45import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
46import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
47import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Zak Cohen90e7116742017-01-29 12:59:23 -080048import static android.content.pm.ActivityInfo.CONFIG_UI_MODE;
Wale Ogunwale822e5122017-07-26 06:02:24 -070049import static android.content.pm.ActivityInfo.CONFIG_WINDOW_CONFIGURATION;
Jorim Jaggi02886a82016-12-06 09:10:06 -080050import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080051import static android.content.pm.ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
52import static android.content.pm.ActivityInfo.FLAG_IMMERSIVE;
53import static android.content.pm.ActivityInfo.FLAG_MULTIPROCESS;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020054import static android.content.pm.ActivityInfo.FLAG_NO_HISTORY;
Chong Zhang87761972016-08-22 13:53:24 -070055import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020056import static android.content.pm.ActivityInfo.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080057import static android.content.pm.ActivityInfo.FLAG_STATE_NOT_NEEDED;
chaviw59b98852017-06-13 12:05:44 -070058import static android.content.pm.ActivityInfo.FLAG_TURN_SCREEN_ON;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080059import static android.content.pm.ActivityInfo.LAUNCH_MULTIPLE;
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -080060import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
61import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080062import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Charles He2bf28322017-10-12 22:24:49 +010063import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
64import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
65import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
66import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
Ruben Brunkf53497c2017-03-27 20:26:17 -070067import static android.content.pm.ActivityInfo.PERSIST_ACROSS_REBOOTS;
68import static android.content.pm.ActivityInfo.PERSIST_ROOT_ONLY;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070069import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggicd13d332016-04-27 15:40:20 -070070import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwale72a73e32016-10-13 12:16:39 -070071import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070072import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -070073import static android.content.pm.ActivityInfo.isFixedOrientationLandscape;
74import static android.content.pm.ActivityInfo.isFixedOrientationPortrait;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -070075import static android.content.res.Configuration.EMPTY;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -070076import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
77import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Ruben Brunkf53497c2017-03-27 20:26:17 -070078import static android.content.res.Configuration.UI_MODE_TYPE_MASK;
Zak Cohen90e7116742017-01-29 12:59:23 -080079import static android.content.res.Configuration.UI_MODE_TYPE_VR_HEADSET;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080080import static android.os.Build.VERSION_CODES.HONEYCOMB;
Zak Cohen90e7116742017-01-29 12:59:23 -080081import static android.os.Build.VERSION_CODES.O;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080082import static android.os.Process.SYSTEM_UID;
Ruben Brunkf53497c2017-03-27 20:26:17 -070083import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080084import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
Andrii Kulian21713ac2016-10-12 22:05:05 -070085import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
Andrii Kulian21713ac2016-10-12 22:05:05 -070086import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080087import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
Andrii Kulian21713ac2016-10-12 22:05:05 -070088import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
89import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
90import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080091import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
92import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
Andrii Kulian21713ac2016-10-12 22:05:05 -070093import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080094import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
95import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Ruben Brunkf53497c2017-03-27 20:26:17 -070096import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
97import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
98import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
99import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
100import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
101import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
102import static com.android.server.am.ActivityStack.LAUNCH_TICK;
103import static com.android.server.am.ActivityStack.LAUNCH_TICK_MSG;
104import static com.android.server.am.ActivityStack.PAUSE_TIMEOUT_MSG;
105import static com.android.server.am.ActivityStack.STOP_TIMEOUT_MSG;
106import static com.android.server.am.EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME;
107import static com.android.server.am.EventLogTags.AM_ACTIVITY_LAUNCH_TIME;
108import static com.android.server.am.EventLogTags.AM_RELAUNCH_ACTIVITY;
109import static com.android.server.am.EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY;
110import static com.android.server.am.TaskPersister.DEBUG;
111import static com.android.server.am.TaskPersister.IMAGE_EXTENSION;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800112import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700113import static com.android.server.am.ActivityRecordProto.CONFIGURATION_CONTAINER;
114import static com.android.server.am.ActivityRecordProto.FRONT_OF_TASK;
115import static com.android.server.am.ActivityRecordProto.IDENTIFIER;
116import static com.android.server.am.ActivityRecordProto.PROC_ID;
117import static com.android.server.am.ActivityRecordProto.STATE;
118import static com.android.server.am.ActivityRecordProto.VISIBLE;
Adrian Roose99bc052017-11-20 17:55:31 +0100119import static com.android.server.policy.WindowManagerPolicy.NAV_BAR_LEFT;
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700120import static com.android.server.wm.IdentifierProto.HASH_CODE;
121import static com.android.server.wm.IdentifierProto.TITLE;
122import static com.android.server.wm.IdentifierProto.USER_ID;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700123
Ruben Brunkf53497c2017-03-27 20:26:17 -0700124import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
125import static org.xmlpull.v1.XmlPullParser.END_TAG;
126import static org.xmlpull.v1.XmlPullParser.START_TAG;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800127
Andrii Kulian21713ac2016-10-12 22:05:05 -0700128import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -0700129import android.app.ActivityManager.TaskDescription;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700130import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800131import android.app.PendingIntent;
Winson Chung709904f2017-04-25 11:00:48 -0700132import android.app.PictureInPictureParams;
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800133import android.app.ResultInfo;
Andrii Kulianb372da62018-01-18 10:46:24 -0800134import android.app.servertransaction.ActivityLifecycleItem;
135import android.app.servertransaction.ActivityRelaunchItem;
136import android.app.servertransaction.ClientTransaction;
137import android.app.servertransaction.ClientTransactionItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700138import android.app.servertransaction.MoveToDisplayItem;
139import android.app.servertransaction.MultiWindowModeChangeItem;
140import android.app.servertransaction.NewIntentItem;
Bryce Lee0bd8d422018-01-09 09:45:57 -0800141import android.app.servertransaction.PauseActivityItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700142import android.app.servertransaction.PipModeChangeItem;
Andrii Kulianb372da62018-01-18 10:46:24 -0800143import android.app.servertransaction.ResumeActivityItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700144import android.app.servertransaction.WindowVisibilityItem;
145import android.app.servertransaction.ActivityConfigurationChangeItem;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146import android.content.ComponentName;
147import android.content.Intent;
148import android.content.pm.ActivityInfo;
149import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700150import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151import android.content.res.Configuration;
152import android.graphics.Bitmap;
Winson Chungaa7fa012017-05-24 15:50:06 -0700153import android.graphics.GraphicBuffer;
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700154import android.graphics.Rect;
Patrick Baumann78380272018-04-04 10:41:01 -0700155import android.os.Binder;
Bryce Lee39791592017-04-26 09:29:12 -0700156import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700158import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800161import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700163import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164import android.os.SystemClock;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800165import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700166import android.os.UserHandle;
Bryce Lee8558ec72017-08-17 15:37:26 -0700167import android.os.storage.StorageManager;
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800168import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169import android.util.EventLog;
170import android.util.Log;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700171import android.util.MergedConfiguration;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700172import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700173import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700174import android.util.proto.ProtoOutputStream;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700175import android.view.AppTransitionAnimationSpec;
Jorim Jaggi34795e32017-05-12 17:27:46 +0200176import android.view.IAppTransitionAnimationSpecsFuture;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177import android.view.IApplicationToken;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100178import android.view.RemoteAnimationDefinition;
Jorim Jaggife762342016-10-13 14:33:27 +0200179import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700180
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700181import com.android.internal.R;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800182import com.android.internal.app.ResolverActivity;
183import com.android.internal.content.ReferrerIntent;
184import com.android.internal.util.XmlUtils;
185import com.android.server.AttributeCache;
Ruben Brunkf53497c2017-03-27 20:26:17 -0700186import com.android.server.AttributeCache.Entry;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800187import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800188import com.android.server.wm.AppWindowContainerController;
189import com.android.server.wm.AppWindowContainerListener;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700190import com.android.server.wm.ConfigurationContainer;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800191import com.android.server.wm.TaskWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Jorim Jaggi02886a82016-12-06 09:10:06 -0800193import org.xmlpull.v1.XmlPullParser;
194import org.xmlpull.v1.XmlPullParserException;
195import org.xmlpull.v1.XmlSerializer;
196
Suprabh Shukla23593142015-11-03 17:31:15 -0800197import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -0700198import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199import java.io.PrintWriter;
200import java.lang.ref.WeakReference;
201import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800202import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700204import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700205import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206
207/**
208 * An entry in the history stack, representing an activity.
209 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700210final class ActivityRecord extends ConfigurationContainer implements AppWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800211 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_AM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700212 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
213 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700214 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700215 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700216 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Winson Chung16e185e2017-11-07 08:30:54 -0800217 // TODO(b/67864419): Remove once recents component is overridden
218 private static final String LEGACY_RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800219
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700220 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700221
Craig Mautner21d24a22014-04-23 11:45:37 -0700222 private static final String ATTR_ID = "id";
223 private static final String TAG_INTENT = "intent";
224 private static final String ATTR_USERID = "user_id";
225 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
226 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700227 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700228 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
229 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700230 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 final ActivityManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800233 final IApplicationToken.Stub appToken; // window manager token
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800234 AppWindowContainerController mWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 final ActivityInfo info; // all about me
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -0700236 // TODO: This is duplicated state already contained in info.applicationInfo - remove
237 ApplicationInfo appInfo; // information about activity's app
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800238 final int launchedFromPid; // always the pid who started the activity.
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700239 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800240 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700241 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 final Intent intent; // the original intent that generated us
243 final ComponentName realActivity; // the intent component, or target of an alias.
244 final String shortComponentName; // the short component name of the intent
245 final String resolvedType; // as per original caller;
246 final String packageName; // the package implementing intent's component
247 final String processName; // process where this component wants to run
248 final String taskAffinity; // as per ActivityInfo.taskAffinity
249 final boolean stateNotNeeded; // As per ActivityInfo.flags
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700250 boolean fullscreen; // The activity is opaque and fills the entire space of this task.
251 // TODO: See if it possible to combine this with the fullscreen field.
252 final boolean hasWallpaper; // Has a wallpaper window as a background.
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800253 final boolean noDisplay; // activity is not displayed?
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800254 private final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700255 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700256
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800257 private CharSequence nonLocalizedLabel; // the label information from the package mgr.
258 private int labelRes; // the label information from the package mgr.
259 private int icon; // resource identifier of activity's icon.
260 private int logo; // resource identifier of activity's logo.
261 private int theme; // resource identifier of activity's theme.
262 private int realTheme; // actual theme resource we will use, never 0.
263 private int windowFlags; // custom window flags for preview window.
Bryce Leeaf691c02017-03-20 14:20:22 -0700264 private TaskRecord task; // the task this is in.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800265 private long createTime = System.currentTimeMillis();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700266 long displayStartTime; // when we started launching this activity
267 long fullyDrawnStartTime; // when we started launching this activity
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800268 private long startTime; // last time this activity was started
Dianne Hackborn50685602011-12-01 12:23:37 -0800269 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700270 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700271 long pauseTime; // last time we started pausing the activity
272 long launchTickTime; // base time for launch tick messages
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700273 // Last configuration reported to the activity in the client process.
274 private MergedConfiguration mLastReportedConfiguration;
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800275 private int mLastReportedDisplayId;
Winson Chung609e1e92017-05-08 10:52:12 -0700276 private boolean mLastReportedMultiWindowMode;
277 private boolean mLastReportedPictureInPictureMode;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700278 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700279 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 final String resultWho; // additional identifier for use by resultTo.
281 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800282 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800284 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700285 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700286 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700287 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -0700289 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700290 ProcessRecord app; // if non-null, hosting application
Bryce Lee7ace3952018-02-16 14:34:32 -0800291 private ActivityState mState; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700293 PersistableBundle persistentState; // last persistently saved activity state
Wale Ogunwale66e16852017-10-19 13:35:52 -0700294 // TODO: See if this is still needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 boolean frontOfTask; // is this the root activity of its task?
296 boolean launchFailed; // set if a launched failed, to abort on 2nd try
297 boolean haveState; // have we gotten the last activity state?
298 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700299 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800301 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
302 // completed
303 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 int configChangeFlags; // which config values have changed
Wale Ogunwaleec950642017-04-25 07:44:21 -0700305 private boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 int launchMode; // the launch mode activity attribute.
Charles He2bf28322017-10-12 22:24:49 +0100307 int lockTaskLaunchMode; // the lockTaskMode manifest attribute, subject to override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700309 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
310 // might hide this activity?
Wale Ogunwaleec950642017-04-25 07:44:21 -0700311 private boolean mDeferHidingClient; // If true we told WM to defer reporting to the client
312 // process that it is hidden.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800313 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 boolean nowVisible; // is this activity's window visible?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 boolean idle; // has the activity gone idle?
316 boolean hasBeenLaunched;// has this activity ever been launched?
317 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400318 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400319 boolean forceNewConfig; // force re-create with new config next time
Winson Chungf7e03e12017-08-22 11:32:16 -0700320 boolean supportsEnterPipOnTaskSwitch; // This flag is set by the system to indicate that the
321 // activity can enter picture in picture while pausing (only when switching to another task)
Winson Chung709904f2017-04-25 11:00:48 -0700322 PictureInPictureParams pictureInPictureArgs = new PictureInPictureParams.Builder().build();
323 // The PiP params used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800324 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800325 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800326 ComponentName requestedVrComponent; // the requested component for handling VR mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700328 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700329
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700330 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700331 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800332
333 static final int STARTING_WINDOW_NOT_SHOWN = 0;
334 static final int STARTING_WINDOW_SHOWN = 1;
335 static final int STARTING_WINDOW_REMOVED = 2;
336 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700337 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800338
Craig Mautner21d24a22014-04-23 11:45:37 -0700339 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700340 boolean mLaunchTaskBehind; // this activity is actively being launched with
341 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700342
Filip Gruszczynski23493322015-07-29 17:02:59 -0700343 // These configurations are collected from application's resources based on size-sensitive
344 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
345 // and drawable-sw400dp will be added to both as 400.
346 private int[] mVerticalSizeConfigurations;
347 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700348 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700349
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800350 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
351 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
352
Robert Carrd2265122016-08-05 10:25:21 -0700353 // A hint to override the window specified rotation animation, or -1
354 // to use the window specified value. We use this so that
355 // we can select the right animation in the cases of starting
356 // windows, where the app hasn't had time to set a value
357 // on the window.
358 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700359
chaviw59b98852017-06-13 12:05:44 -0700360 private boolean mShowWhenLocked;
361 private boolean mTurnScreenOn;
362
Andrii Kulian21713ac2016-10-12 22:05:05 -0700363 /**
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -0800364 * Temp configs used in {@link #ensureActivityConfiguration(int, boolean)}
Andrii Kulian21713ac2016-10-12 22:05:05 -0700365 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700366 private final Configuration mTmpConfig = new Configuration();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700367 private final Rect mTmpBounds = new Rect();
Andrii Kulian21713ac2016-10-12 22:05:05 -0700368
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800369 private static String startingWindowStateToString(int state) {
370 switch (state) {
371 case STARTING_WINDOW_NOT_SHOWN:
372 return "STARTING_WINDOW_NOT_SHOWN";
373 case STARTING_WINDOW_SHOWN:
374 return "STARTING_WINDOW_SHOWN";
375 case STARTING_WINDOW_REMOVED:
376 return "STARTING_WINDOW_REMOVED";
377 default:
378 return "unknown state=" + state;
379 }
380 }
381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700383 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700384 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
385 pw.print(" processName="); pw.println(processName);
386 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700387 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800388 pw.print(" userId="); pw.println(userId);
389 pw.print(prefix); pw.print("app="); pw.println(app);
390 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700391 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
392 pw.print(" task="); pw.println(task);
393 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700394 pw.print(prefix); pw.print("realActivity=");
395 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700396 if (appInfo != null) {
397 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
398 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
399 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
400 }
401 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800402 if (appInfo.splitSourceDirs != null) {
403 pw.print(prefix); pw.print("splitDir=");
404 pw.println(Arrays.toString(appInfo.splitSourceDirs));
405 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800406 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700407 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
408 pw.print(" componentSpecified="); pw.print(componentSpecified);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700409 pw.print(" mActivityType="); pw.println(
410 activityTypeToString(getActivityType()));
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700411 if (rootVoiceInteraction) {
412 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
413 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800414 pw.print(prefix); pw.print("compat="); pw.print(compat);
415 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
416 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
417 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700418 pw.println(prefix + "mLastReportedConfigurations:");
419 mLastReportedConfiguration.dump(pw, prefix + " ");
420
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700421 pw.print(prefix); pw.print("CurrentConfiguration="); pw.println(getConfiguration());
422 if (!getOverrideConfiguration().equals(EMPTY)) {
423 pw.println(prefix + "OverrideConfiguration=" + getOverrideConfiguration());
424 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800425 if (!matchParentBounds()) {
426 pw.println(prefix + "bounds=" + getBounds());
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700427 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700428 if (resultTo != null || resultWho != null) {
429 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
430 pw.print(" resultWho="); pw.print(resultWho);
431 pw.print(" resultCode="); pw.println(requestCode);
432 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700433 if (taskDescription != null) {
434 final String iconFilename = taskDescription.getIconFilename();
435 if (iconFilename != null || taskDescription.getLabel() != null ||
436 taskDescription.getPrimaryColor() != 0) {
437 pw.print(prefix); pw.print("taskDescription:");
Craig Mautner29c58ca2014-10-14 16:17:06 -0700438 pw.print(" label=\""); pw.print(taskDescription.getLabel());
439 pw.print("\"");
Matthew Ng54bc9422017-10-02 17:16:28 -0700440 pw.print(" icon="); pw.print(taskDescription.getInMemoryIcon() != null
441 ? taskDescription.getInMemoryIcon().getByteCount() + " bytes"
442 : "null");
443 pw.print(" iconResource="); pw.print(taskDescription.getIconResource());
444 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200445 pw.print(" primaryColor=");
Craig Mautner29c58ca2014-10-14 16:17:06 -0700446 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700447 pw.print(prefix + " backgroundColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200448 pw.println(Integer.toHexString(taskDescription.getBackgroundColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700449 pw.print(prefix + " statusBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200450 pw.println(Integer.toHexString(taskDescription.getStatusBarColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700451 pw.print(prefix + " navigationBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200452 pw.println(Integer.toHexString(taskDescription.getNavigationBarColor()));
Craig Mautner29c58ca2014-10-14 16:17:06 -0700453 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700454 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700455 if (results != null) {
456 pw.print(prefix); pw.print("results="); pw.println(results);
457 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700458 if (pendingResults != null && pendingResults.size() > 0) {
459 pw.print(prefix); pw.println("Pending Results:");
460 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
461 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
462 pw.print(prefix); pw.print(" - ");
463 if (pir == null) {
464 pw.println("null");
465 } else {
466 pw.println(pir);
467 pir.dump(pw, prefix + " ");
468 }
469 }
470 }
471 if (newIntents != null && newIntents.size() > 0) {
472 pw.print(prefix); pw.println("Pending New Intents:");
473 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800474 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700475 pw.print(prefix); pw.print(" - ");
476 if (intent == null) {
477 pw.println("null");
478 } else {
479 pw.println(intent.toShortString(false, true, false, true));
480 }
481 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700482 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700483 if (pendingOptions != null) {
484 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
485 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700486 if (appTimeTracker != null) {
487 appTimeTracker.dumpWithHeader(pw, prefix, false);
488 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700489 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700490 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700491 }
492 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800493 pw.print(" launchCount="); pw.print(launchCount);
494 pw.print(" lastLaunchTime=");
495 if (lastLaunchTime == 0) pw.print("0");
496 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
497 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700498 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700499 pw.print(" icicle="); pw.println(icicle);
Bryce Lee7ace3952018-02-16 14:34:32 -0800500 pw.print(prefix); pw.print("state="); pw.print(mState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700501 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700502 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700503 pw.print(" finishing="); pw.println(finishing);
504 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
505 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700506 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800507 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800508 pw.print(" idle="); pw.print(idle);
509 pw.print(" mStartingWindowState=");
510 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800511 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
512 pw.print(" noDisplay="); pw.print(noDisplay);
513 pw.print(" immersive="); pw.print(immersive);
514 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800515 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400516 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700517 pw.print(prefix); pw.print("mActivityType=");
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700518 pw.println(activityTypeToString(getActivityType()));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800519 if (requestedVrComponent != null) {
520 pw.print(prefix);
521 pw.print("requestedVrComponent=");
522 pw.println(requestedVrComponent);
523 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700524 if (displayStartTime != 0 || startTime != 0) {
525 pw.print(prefix); pw.print("displayStartTime=");
526 if (displayStartTime == 0) pw.print("0");
527 else TimeUtils.formatDuration(displayStartTime, now, pw);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700528 pw.print(" startTime=");
529 if (startTime == 0) pw.print("0");
530 else TimeUtils.formatDuration(startTime, now, pw);
531 pw.println();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700532 }
Bryce Lee4a194382017-04-04 14:32:48 -0700533 final boolean waitingVisible =
534 mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800535 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700536 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800537 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700538 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700539 if (lastVisibleTime == 0) pw.print("0");
540 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
541 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700542 }
Wale Ogunwaleec950642017-04-25 07:44:21 -0700543 if (mDeferHidingClient) {
544 pw.println(prefix + "mDeferHidingClient=" + mDeferHidingClient);
545 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800546 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
547 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700548 pw.print(" configChangeFlags=");
549 pw.println(Integer.toHexString(configChangeFlags));
550 }
551 if (connections != null) {
552 pw.print(prefix); pw.print("connections="); pw.println(connections);
553 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800554 if (info != null) {
555 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
Winson Chung609e1e92017-05-08 10:52:12 -0700556 pw.println(prefix + "mLastReportedMultiWindowMode=" + mLastReportedMultiWindowMode
557 + " mLastReportedPictureInPictureMode=" + mLastReportedPictureInPictureMode);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700558 if (info.supportsPictureInPicture()) {
559 pw.println(prefix + "supportsPictureInPicture=" + info.supportsPictureInPicture());
Winson Chungf7e03e12017-08-22 11:32:16 -0700560 pw.println(prefix + "supportsEnterPipOnTaskSwitch: "
561 + supportsEnterPipOnTaskSwitch);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700562 }
563 if (info.maxAspectRatio != 0) {
564 pw.println(prefix + "maxAspectRatio=" + info.maxAspectRatio);
565 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
568
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -0700569 void updateApplicationInfo(ApplicationInfo aInfo) {
570 appInfo = aInfo;
571 info.applicationInfo = aInfo;
572 }
573
Andrii Kulian21713ac2016-10-12 22:05:05 -0700574 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700575 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
576 }
577
Andrii Kulian21713ac2016-10-12 22:05:05 -0700578 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700579 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
580 }
581
Andrii Kulian21713ac2016-10-12 22:05:05 -0700582 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700583 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
584 }
585
Filip Gruszczynski23493322015-07-29 17:02:59 -0700586 /**
587 * The purpose of this method is to decide whether the activity needs to be relaunched upon
588 * changing its size. In most cases the activities don't need to be relaunched, if the resize
589 * is small, all the activity content has to do is relayout itself within new bounds. There are
590 * cases however, where the activity's content would be completely changed in the new size and
591 * the full relaunch is required.
592 *
593 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
594 * required. These thresholds are collected from the application resource qualifiers. For
595 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
596 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
597 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
598 * of the threshold.
599 */
600 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
601 int secondDp) {
602 if (thresholds == null) {
603 return false;
604 }
605 for (int i = thresholds.length - 1; i >= 0; i--) {
606 final int threshold = thresholds[i];
607 if ((firstDp < threshold && secondDp >= threshold)
608 || (firstDp >= threshold && secondDp < threshold)) {
609 return true;
610 }
611 }
612 return false;
613 }
614
Andrii Kulian21713ac2016-10-12 22:05:05 -0700615 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700616 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700617 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
618 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700619 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700620 }
621
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800622 private void scheduleActivityMovedToDisplay(int displayId, Configuration config) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800623 if (app == null || app.thread == null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800624 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.w(TAG,
625 "Can't report activity moved to display - client not running, activityRecord="
626 + this + ", displayId=" + displayId);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800627 return;
628 }
629 try {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800630 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
631 "Reporting activity moved to display" + ", activityRecord=" + this
632 + ", displayId=" + displayId + ", config=" + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700633
Bryce Leeb0f993f2018-03-02 15:38:01 -0800634 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800635 MoveToDisplayItem.obtain(displayId, config));
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800636 } catch (RemoteException e) {
637 // If process died, whatever.
638 }
639 }
640
641 private void scheduleConfigurationChanged(Configuration config) {
642 if (app == null || app.thread == null) {
643 if (DEBUG_CONFIGURATION) Slog.w(TAG,
644 "Can't report activity configuration update - client not running"
645 + ", activityRecord=" + this);
646 return;
647 }
648 try {
649 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + ", config: "
650 + config);
651
Bryce Leeb0f993f2018-03-02 15:38:01 -0800652 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800653 ActivityConfigurationChangeItem.obtain(config));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800654 } catch (RemoteException e) {
655 // If process died, whatever.
656 }
657 }
658
Winson Chung5af42fc2017-03-24 17:11:33 -0700659 void updateMultiWindowMode() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700660 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800661 return;
662 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700663
Wale Ogunwaleeb76b762017-11-17 10:08:04 -0800664 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
665 // Don't do anything if we are currently deferring multi-window mode change.
666 return;
667 }
668
Winson Chung5af42fc2017-03-24 17:11:33 -0700669 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Wale Ogunwaleeb76b762017-11-17 10:08:04 -0800670 final boolean inMultiWindowMode = inMultiWindowMode();
Winson Chung609e1e92017-05-08 10:52:12 -0700671 if (inMultiWindowMode != mLastReportedMultiWindowMode) {
672 mLastReportedMultiWindowMode = inMultiWindowMode;
Winson Chung5af42fc2017-03-24 17:11:33 -0700673 scheduleMultiWindowModeChanged(getConfiguration());
674 }
675 }
676
677 private void scheduleMultiWindowModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800678 try {
Bryce Leeb0f993f2018-03-02 15:38:01 -0800679 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800680 MultiWindowModeChangeItem.obtain(mLastReportedMultiWindowMode,
Andrii Kulian446e8242017-10-26 15:17:29 -0700681 overrideConfig));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800682 } catch (Exception e) {
683 // If process died, I don't care.
684 }
685 }
686
Winson Chungab76bbc2017-08-14 13:33:51 -0700687 void updatePictureInPictureMode(Rect targetStackBounds, boolean forceUpdate) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700688 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800689 return;
690 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700691
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700692 final boolean inPictureInPictureMode = inPinnedWindowingMode() && targetStackBounds != null;
Winson Chungab76bbc2017-08-14 13:33:51 -0700693 if (inPictureInPictureMode != mLastReportedPictureInPictureMode || forceUpdate) {
Winson Chung5af42fc2017-03-24 17:11:33 -0700694 // Picture-in-picture mode changes also trigger a multi-window mode change as well, so
695 // update that here in order
Winson Chung609e1e92017-05-08 10:52:12 -0700696 mLastReportedPictureInPictureMode = inPictureInPictureMode;
Wale Ogunwaleeb76b762017-11-17 10:08:04 -0800697 mLastReportedMultiWindowMode = inMultiWindowMode();
Winson Chung5af42fc2017-03-24 17:11:33 -0700698 final Configuration newConfig = task.computeNewOverrideConfigurationForBounds(
699 targetStackBounds, null);
700 schedulePictureInPictureModeChanged(newConfig);
701 scheduleMultiWindowModeChanged(newConfig);
702 }
703 }
704
705 private void schedulePictureInPictureModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800706 try {
Bryce Leeb0f993f2018-03-02 15:38:01 -0800707 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800708 PipModeChangeItem.obtain(mLastReportedPictureInPictureMode,
Andrii Kulian446e8242017-10-26 15:17:29 -0700709 overrideConfig));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800710 } catch (Exception e) {
711 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800712 }
713 }
714
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700715 @Override
716 protected int getChildCount() {
717 // {@link ActivityRecord} is a leaf node and has no children.
718 return 0;
719 }
720
721 @Override
722 protected ConfigurationContainer getChildAt(int index) {
723 return null;
724 }
725
726 @Override
727 protected ConfigurationContainer getParent() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700728 return getTask();
729 }
730
731 TaskRecord getTask() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700732 return task;
733 }
734
Bryce Leeaf691c02017-03-20 14:20:22 -0700735 /**
736 * Sets reference to the {@link TaskRecord} the {@link ActivityRecord} will treat as its parent.
737 * Note that this does not actually add the {@link ActivityRecord} as a {@link TaskRecord}
738 * children. However, this method will clean up references to this {@link ActivityRecord} in
739 * {@link ActivityStack}.
740 * @param task The new parent {@link TaskRecord}.
741 */
742 void setTask(TaskRecord task) {
Bryce Lee84730a02018-04-03 14:10:04 -0700743 setTask(task /* task */, false /* reparenting */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700744 }
745
746 /**
747 * This method should only be called by {@link TaskRecord#removeActivity(ActivityRecord)}.
Bryce Lee84730a02018-04-03 14:10:04 -0700748 * @param task The new parent task.
749 * @param reparenting Whether we're in the middle of reparenting.
Bryce Leeaf691c02017-03-20 14:20:22 -0700750 */
751 void setTask(TaskRecord task, boolean reparenting) {
752 // Do nothing if the {@link TaskRecord} is the same as the current {@link getTask}.
753 if (task != null && task == getTask()) {
754 return;
755 }
756
Bryce Lee84730a02018-04-03 14:10:04 -0700757 final ActivityStack oldStack = getStack();
758 final ActivityStack newStack = task != null ? task.getStack() : null;
Bryce Leeaf691c02017-03-20 14:20:22 -0700759
Bryce Lee84730a02018-04-03 14:10:04 -0700760 // Inform old stack (if present) of activity removal and new stack (if set) of activity
761 // addition.
762 if (oldStack != newStack) {
763 if (!reparenting && oldStack != null) {
764 oldStack.onActivityRemovedFromStack(this);
765 }
766
767 if (newStack != null) {
768 newStack.onActivityAddedToStack(this);
769 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700770 }
771
772 this.task = task;
773
774 if (!reparenting) {
775 onParentChanged();
776 }
777 }
778
Dianne Hackbornbe707852011-11-11 14:32:10 -0800779 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700780 private final WeakReference<ActivityRecord> weakActivity;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700781 private final String name;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800782
Steven Timotiusaf03df62017-07-18 16:56:43 -0700783 Token(ActivityRecord activity, Intent intent) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800784 weakActivity = new WeakReference<>(activity);
Steven Timotiusaf03df62017-07-18 16:56:43 -0700785 name = intent.getComponent().flattenToShortString();
Wale Ogunwale7d701172015-03-11 15:36:30 -0700786 }
787
Andrii Kulian21713ac2016-10-12 22:05:05 -0700788 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700789 if (token == null) {
790 return null;
791 }
792 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700793 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700794 return null;
795 }
796 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800797 }
798
Craig Mautnerde4ef022013-04-07 19:01:33 -0700799 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800800 public String toString() {
801 StringBuilder sb = new StringBuilder(128);
802 sb.append("Token{");
803 sb.append(Integer.toHexString(System.identityHashCode(this)));
804 sb.append(' ');
805 sb.append(weakActivity.get());
806 sb.append('}');
807 return sb.toString();
808 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700809
810 @Override
811 public String getName() {
812 return name;
813 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800814 }
815
Wale Ogunwale7d701172015-03-11 15:36:30 -0700816 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800817 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700818 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800819 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800820 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800821 return null;
822 }
823 }
824
Chong Zhang85ee6542015-10-02 13:36:38 -0700825 boolean isResolverActivity() {
826 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700827 }
828
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800829 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller, int _launchedFromPid,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800830 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700832 ActivityRecord _resultTo, String _resultWho, int _reqCode,
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700833 boolean _componentSpecified, boolean _rootVoiceInteraction,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700834 ActivityStackSupervisor supervisor, ActivityOptions options,
835 ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 service = _service;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700837 appToken = new Token(this, _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 info = aInfo;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800839 launchedFromPid = _launchedFromPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800841 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700842 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 intent = _intent;
844 shortComponentName = _intent.getComponent().flattenToShortString();
845 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800846 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700847 rootVoiceInteraction = _rootVoiceInteraction;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700848 mLastReportedConfiguration = new MergedConfiguration(_configuration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 resultTo = _resultTo;
850 resultWho = _resultWho;
851 requestCode = _reqCode;
Bryce Lee7ace3952018-02-16 14:34:32 -0800852 setState(INITIALIZING, "ActivityRecord ctor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 frontOfTask = false;
854 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700856 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800858 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 keysPaused = false;
860 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700861 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 idle = false;
864 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700865 mStackSupervisor = supervisor;
Robert Carr0f5d7532016-10-17 16:39:17 -0700866
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800867 // This starts out true, since the initial state of an activity is that we have everything,
868 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700869 haveState = true;
870
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800871 // If the class name in the intent doesn't match that of the target, this is
872 // probably an alias. We have to create a new ComponentName object to keep track
873 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
874 if (aInfo.targetActivity == null
875 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
876 && (aInfo.launchMode == LAUNCH_MULTIPLE
877 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
878 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800880 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800882 taskAffinity = aInfo.taskAffinity;
883 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
884 appInfo = aInfo.applicationInfo;
885 nonLocalizedLabel = aInfo.nonLocalizedLabel;
886 labelRes = aInfo.labelRes;
887 if (nonLocalizedLabel == null && labelRes == 0) {
888 ApplicationInfo app = aInfo.applicationInfo;
889 nonLocalizedLabel = app.nonLocalizedLabel;
890 labelRes = app.labelRes;
891 }
892 icon = aInfo.getIconResource();
893 logo = aInfo.getLogoResource();
894 theme = aInfo.getThemeResource();
895 realTheme = theme;
896 if (realTheme == 0) {
897 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
898 ? android.R.style.Theme : android.R.style.Theme_Holo;
899 }
900 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
901 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
902 }
903 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
904 && (aInfo.applicationInfo.uid == SYSTEM_UID
905 || aInfo.applicationInfo.uid == _caller.info.uid)) {
906 processName = _caller.processName;
907 } else {
908 processName = aInfo.processName;
909 }
910
911 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
912 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
913 }
914
915 packageName = aInfo.applicationInfo.packageName;
916 launchMode = aInfo.launchMode;
917
Ruben Brunkf53497c2017-03-27 20:26:17 -0700918 Entry ent = AttributeCache.instance().get(packageName,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800919 realTheme, com.android.internal.R.styleable.Window, userId);
Bryce Leee83f34cd2017-10-31 19:50:54 -0700920
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700921 if (ent != null) {
922 fullscreen = !ActivityInfo.isTranslucentOrFloating(ent.array);
923 hasWallpaper = ent.array.getBoolean(R.styleable.Window_windowShowWallpaper, false);
924 noDisplay = ent.array.getBoolean(R.styleable.Window_windowNoDisplay, false);
925 } else {
926 hasWallpaper = false;
927 noDisplay = false;
928 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800929
Winson Chung83471632016-12-13 11:02:12 -0800930 setActivityType(_componentSpecified, _launchedFromUid, _intent, options, sourceRecord);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800931
932 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
933
934 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
935 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
chaviw59b98852017-06-13 12:05:44 -0700936
937 mShowWhenLocked = (aInfo.flags & FLAG_SHOW_WHEN_LOCKED) != 0;
938 mTurnScreenOn = (aInfo.flags & FLAG_TURN_SCREEN_ON) != 0;
Charles He2bf28322017-10-12 22:24:49 +0100939
940 mRotationAnimationHint = aInfo.rotationAnimation;
941 lockTaskLaunchMode = aInfo.lockTaskLaunchMode;
942 if (appInfo.isPrivilegedApp() && (lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_ALWAYS
943 || lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
944 lockTaskLaunchMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
945 }
946
947 if (options != null) {
948 pendingOptions = options;
949 mLaunchTaskBehind = options.getLaunchTaskBehind();
950
951 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
952 // Only override manifest supplied option if set.
953 if (rotationAnimation >= 0) {
954 mRotationAnimationHint = rotationAnimation;
955 }
956 final PendingIntent usageReport = pendingOptions.getUsageTimeReport();
957 if (usageReport != null) {
958 appTimeTracker = new AppTimeTracker(usageReport);
959 }
960 final boolean useLockTask = pendingOptions.getLockTaskMode();
961 if (useLockTask && lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_DEFAULT) {
962 lockTaskLaunchMode = LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
963 }
964 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800965 }
966
Dianne Hackborn68a06332017-11-15 17:54:18 -0800967 void setProcess(ProcessRecord proc) {
968 app = proc;
969 final ActivityRecord root = task != null ? task.getRootActivity() : null;
970 if (root == this) {
971 task.setRootProcess(proc);
972 }
973 }
974
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800975 AppWindowContainerController getWindowContainerController() {
976 return mWindowContainerController;
977 }
978
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800979 void createWindowContainer() {
980 if (mWindowContainerController != null) {
981 throw new IllegalArgumentException("Window container=" + mWindowContainerController
982 + " already created for r=" + this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800983 }
984
985 inHistory = true;
986
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800987 final TaskWindowContainerController taskController = task.getWindowContainerController();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800988
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700989 // TODO(b/36505427): Maybe this call should be moved inside updateOverrideConfiguration()
990 task.updateOverrideConfigurationFromLaunchBounds();
991 // Make sure override configuration is up-to-date before using to create window controller.
992 updateOverrideConfiguration();
993
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800994 mWindowContainerController = new AppWindowContainerController(taskController, appToken,
995 this, Integer.MAX_VALUE /* add on top */, info.screenOrientation, fullscreen,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800996 (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
997 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
998 appInfo.targetSdkVersion, mRotationAnimationHint,
Bryce Leef3c6a472017-11-14 14:53:06 -0800999 ActivityManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001000
1001 task.addActivityToTop(this);
1002
Winson Chung609e1e92017-05-08 10:52:12 -07001003 // When an activity is started directly into a split-screen fullscreen stack, we need to
1004 // update the initial multi-window modes so that the callbacks are scheduled correctly when
1005 // the user leaves that mode.
Bryce Leef3c6a472017-11-14 14:53:06 -08001006 mLastReportedMultiWindowMode = inMultiWindowMode();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001007 mLastReportedPictureInPictureMode = inPinnedWindowingMode();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001008 }
1009
1010 void removeWindowContainer() {
Bryce Lee7ace3952018-02-16 14:34:32 -08001011 // Do not try to remove a window container if we have already removed it.
1012 if (mWindowContainerController == null) {
1013 return;
1014 }
1015
Wale Ogunwalecc367f42017-02-01 08:12:14 -08001016 // Resume key dispatching if it is currently paused before we remove the container.
1017 resumeKeyDispatchingLocked();
1018
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001019 mWindowContainerController.removeContainer(getDisplayId());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001020 mWindowContainerController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022
Winson Chung30480042017-01-26 10:55:34 -08001023 /**
1024 * Reparents this activity into {@param newTask} at the provided {@param position}. The caller
1025 * should ensure that the {@param newTask} is not already the parent of this activity.
1026 */
1027 void reparent(TaskRecord newTask, int position, String reason) {
1028 final TaskRecord prevTask = task;
1029 if (prevTask == newTask) {
1030 throw new IllegalArgumentException(reason + ": task=" + newTask
1031 + " is already the parent of r=" + this);
1032 }
1033
Winson Chung74666102017-02-22 17:49:24 -08001034 // TODO: Ensure that we do not directly reparent activities across stacks, as that may leave
1035 // the stacks in strange states. For now, we should use Task.reparent() to ensure that
1036 // the stack is left in an OK state.
1037 if (prevTask != null && newTask != null && prevTask.getStack() != newTask.getStack()) {
1038 throw new IllegalArgumentException(reason + ": task=" + newTask
1039 + " is in a different stack (" + newTask.getStackId() + ") than the parent of"
1040 + " r=" + this + " (" + prevTask.getStackId() + ")");
1041 }
1042
Winson Chung30480042017-01-26 10:55:34 -08001043 // Must reparent first in window manager
1044 mWindowContainerController.reparent(newTask.getWindowContainerController(), position);
1045
Bryce Lee84730a02018-04-03 14:10:04 -07001046 // Reparenting prevents informing the parent stack of activity removal in the case that
1047 // the new stack has the same parent. we must manually signal here if this is not the case.
1048 final ActivityStack prevStack = prevTask.getStack();
1049
1050 if (prevStack != newTask.getStack()) {
1051 prevStack.onActivityRemovedFromStack(this);
1052 }
Bryce Leeaf691c02017-03-20 14:20:22 -07001053 // Remove the activity from the old task and add it to the new task.
Bryce Lee84730a02018-04-03 14:10:04 -07001054 prevTask.removeActivity(this, true /* reparenting */);
Bryce Lee0f9bde82017-02-22 16:39:06 -08001055
Winson Chung30480042017-01-26 10:55:34 -08001056 newTask.addActivityAtIndex(position, this);
1057 }
1058
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001059 private boolean isHomeIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001060 return ACTION_MAIN.equals(intent.getAction())
1061 && intent.hasCategory(CATEGORY_HOME)
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001062 && intent.getCategories().size() == 1
1063 && intent.getData() == null
1064 && intent.getType() == null;
1065 }
1066
Chong Zhangad24f962016-08-25 12:12:33 -07001067 static boolean isMainIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001068 return ACTION_MAIN.equals(intent.getAction())
1069 && intent.hasCategory(CATEGORY_LAUNCHER)
Chong Zhangad24f962016-08-25 12:12:33 -07001070 && intent.getCategories().size() == 1
1071 && intent.getData() == null
1072 && intent.getType() == null;
1073 }
1074
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001075 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
1076 if (uid == Process.myUid() || uid == 0) {
1077 // System process can launch home activity.
1078 return true;
1079 }
1080 // Resolver activity can launch home activity.
1081 return sourceRecord != null && sourceRecord.isResolverActivity();
1082 }
1083
Winson Chung83471632016-12-13 11:02:12 -08001084 /**
1085 * @return whether the given package name can launch an assist activity.
1086 */
1087 private boolean canLaunchAssistActivity(String packageName) {
Winson Chungbccd4b52017-12-13 11:08:39 -08001088 final ComponentName assistComponent = service.mActiveVoiceInteractionServiceComponent;
Winson Chung83471632016-12-13 11:02:12 -08001089 if (assistComponent != null) {
1090 return assistComponent.getPackageName().equals(packageName);
1091 }
1092 return false;
1093 }
1094
1095 private void setActivityType(boolean componentSpecified, int launchedFromUid, Intent intent,
1096 ActivityOptions options, ActivityRecord sourceRecord) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001097 int activityType = ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001098 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
1099 && isHomeIntent(intent) && !isResolverActivity()) {
1100 // This sure looks like a home activity!
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001101 activityType = ACTIVITY_TYPE_HOME;
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001102
1103 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
1104 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
1105 // We only allow home activities to be resizeable if they explicitly requested it.
1106 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
1107 }
Winson Chung16e185e2017-11-07 08:30:54 -08001108 } else if (realActivity.getClassName().contains(LEGACY_RECENTS_PACKAGE_NAME) ||
1109 service.getRecentTasks().isRecentsComponent(realActivity, appInfo.uid)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001110 activityType = ACTIVITY_TYPE_RECENTS;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001111 } else if (options != null && options.getLaunchActivityType() == ACTIVITY_TYPE_ASSISTANT
Winson Chung83471632016-12-13 11:02:12 -08001112 && canLaunchAssistActivity(launchedFromPackage)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001113 activityType = ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001114 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001115 setActivityType(activityType);
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001116 }
1117
Craig Mautnera228ae92014-07-09 05:44:55 -07001118 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001119 if (launchMode != LAUNCH_SINGLE_INSTANCE && launchMode != LAUNCH_SINGLE_TASK) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001120 task.setTaskToAffiliateWith(taskToAffiliateWith);
1121 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001122 }
1123
Andrii Kulian02b7a832016-10-06 23:11:56 -07001124 /**
1125 * @return Stack value from current task, null if there is no task.
1126 */
Winson Chung55893332017-02-17 17:13:10 -08001127 <T extends ActivityStack> T getStack() {
1128 return task != null ? (T) task.getStack() : null;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001129 }
1130
Jorim Jaggicdfc04e2017-04-28 19:06:24 +02001131 int getStackId() {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001132 return getStack() != null ? getStack().mStackId : INVALID_STACK_ID;
1133 }
1134
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001135 ActivityDisplay getDisplay() {
1136 final ActivityStack stack = getStack();
1137 return stack != null ? stack.getDisplay() : null;
1138 }
1139
Craig Mautner5eda9b32013-07-02 11:58:16 -07001140 boolean changeWindowTranslucency(boolean toOpaque) {
1141 if (fullscreen == toOpaque) {
1142 return false;
1143 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001144
Craig Mautner5eda9b32013-07-02 11:58:16 -07001145 // Keep track of the number of fullscreen activities in this task.
1146 task.numFullscreen += toOpaque ? +1 : -1;
1147
1148 fullscreen = toOpaque;
1149 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -07001150 }
1151
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001152 void takeFromHistory() {
1153 if (inHistory) {
1154 inHistory = false;
1155 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001156 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001157 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001158 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001159 }
1160 }
1161
1162 boolean isInHistory() {
1163 return inHistory;
1164 }
1165
Wale Ogunwale7d701172015-03-11 15:36:30 -07001166 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001167 final ActivityStack stack = getStack();
1168 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001169 }
1170
Craig Mautner21d24a22014-04-23 11:45:37 -07001171 boolean isPersistable() {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001172 return (info.persistableMode == PERSIST_ROOT_ONLY ||
1173 info.persistableMode == PERSIST_ACROSS_REBOOTS) &&
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001174 (intent == null || (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -07001175 }
1176
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001177 boolean isFocusable() {
Bryce Lee7b851cc2018-04-10 14:53:13 -07001178 return mStackSupervisor.isFocusable(this, isAlwaysFocusable());
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001179 }
1180
1181 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001182 return ActivityInfo.isResizeableMode(info.resizeMode) || info.supportsPictureInPicture();
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001183 }
1184
Winson Chungd3395382016-12-13 11:49:09 -08001185 /**
1186 * @return whether this activity is non-resizeable or forced to be resizeable
1187 */
1188 boolean isNonResizableOrForcedResizable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001189 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -07001190 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -07001191 }
1192
Winson Chunge6308042016-10-31 09:24:01 -07001193 /**
Winson Chungd3395382016-12-13 11:49:09 -08001194 * @return whether this activity supports PiP multi-window and can be put in the pinned stack.
Winson Chunge6308042016-10-31 09:24:01 -07001195 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001196 boolean supportsPictureInPicture() {
Wale Ogunwale034a8ec2017-09-02 17:14:40 -07001197 return service.mSupportsPictureInPicture && isActivityTypeStandardOrUndefined()
Winson Chungd3395382016-12-13 11:49:09 -08001198 && info.supportsPictureInPicture();
1199 }
1200
1201 /**
1202 * @return whether this activity supports split-screen multi-window and can be put in the docked
1203 * stack.
1204 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001205 @Override
1206 public boolean supportsSplitScreenWindowingMode() {
Winson Chungd3395382016-12-13 11:49:09 -08001207 // An activity can not be docked even if it is considered resizeable because it only
1208 // supports picture-in-picture mode but has a non-resizeable resizeMode
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001209 return super.supportsSplitScreenWindowingMode()
1210 && service.mSupportsSplitScreenMultiWindow && supportsResizeableMultiWindow();
Winson Chungd3395382016-12-13 11:49:09 -08001211 }
1212
1213 /**
1214 * @return whether this activity supports freeform multi-window and can be put in the freeform
1215 * stack.
1216 */
1217 boolean supportsFreeform() {
1218 return service.mSupportsFreeformWindowManagement && supportsResizeableMultiWindow();
1219 }
1220
1221 /**
1222 * @return whether this activity supports non-PiP multi-window.
1223 */
1224 private boolean supportsResizeableMultiWindow() {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001225 return service.mSupportsMultiWindow && !isActivityTypeHome()
Winson Chungd3395382016-12-13 11:49:09 -08001226 && (ActivityInfo.isResizeableMode(info.resizeMode)
1227 || service.mForceResizableActivities);
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001228 }
1229
Winson Chunge6308042016-10-31 09:24:01 -07001230 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001231 * Check whether this activity can be launched on the specified display.
1232 * @param displayId Target display id.
1233 * @return {@code true} if either it is the default display or this activity is resizeable and
1234 * can be put a secondary screen.
1235 */
1236 boolean canBeLaunchedOnDisplay(int displayId) {
Bryce Lee1533b2b2017-09-14 17:06:41 -07001237 final TaskRecord task = getTask();
1238
1239 // The resizeability of an Activity's parent task takes precendence over the ActivityInfo.
1240 // This allows for a non resizable activity to be launched into a resizeable task.
1241 final boolean resizeable =
1242 task != null ? task.isResizeable() : supportsResizeableMultiWindow();
1243
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001244 return service.mStackSupervisor.canPlaceEntityOnDisplay(displayId,
Bryce Lee1533b2b2017-09-14 17:06:41 -07001245 resizeable, launchedFromPid, launchedFromUid, info);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001246 }
1247
1248 /**
Robert Carrc33658e2017-04-11 18:24:20 -07001249 * @param beforeStopping Whether this check is for an auto-enter-pip operation, that is to say
1250 * the activity has requested to enter PiP when it would otherwise be stopped.
1251 *
Winson Chung298f95b2017-08-10 15:57:18 -07001252 * @return whether this activity is currently allowed to enter PIP.
Winson Chunge6308042016-10-31 09:24:01 -07001253 */
Winson Chung298f95b2017-08-10 15:57:18 -07001254 boolean checkEnterPictureInPictureState(String caller, boolean beforeStopping) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07001255 if (!supportsPictureInPicture()) {
1256 return false;
1257 }
1258
Winson Chungf4ac0632017-03-17 12:34:12 -07001259 // Check app-ops and see if PiP is supported for this package
1260 if (!checkEnterPictureInPictureAppOpsState()) {
1261 return false;
1262 }
1263
Winson Chungf1bfee12017-03-24 17:11:33 -07001264 // Check to see if we are in VR mode, and disallow PiP if so
1265 if (service.shouldDisableNonVrUiLocked()) {
1266 return false;
1267 }
1268
Winson Chungc2baac02017-01-11 13:34:47 -08001269 boolean isKeyguardLocked = service.isKeyguardLocked();
Benjamin Franza83859f2017-07-03 16:34:14 +01001270 boolean isCurrentAppLocked = service.getLockTaskModeState() != LOCK_TASK_MODE_NONE;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001271 final ActivityDisplay display = getDisplay();
1272 boolean hasPinnedStack = display != null && display.hasPinnedStack();
Winson Chungbb348802017-01-30 12:01:45 -08001273 // Don't return early if !isNotLocked, since we want to throw an exception if the activity
1274 // is in an incorrect state
Winson Chunge581ebf2017-02-21 08:25:03 -08001275 boolean isNotLockedOrOnKeyguard = !isKeyguardLocked && !isCurrentAppLocked;
Robert Carrc33658e2017-04-11 18:24:20 -07001276
1277 // We don't allow auto-PiP when something else is already pipped.
1278 if (beforeStopping && hasPinnedStack) {
1279 return false;
1280 }
1281
Bryce Lee7ace3952018-02-16 14:34:32 -08001282 switch (mState) {
Winson Chungc2baac02017-01-11 13:34:47 -08001283 case RESUMED:
Winson Chunge581ebf2017-02-21 08:25:03 -08001284 // When visible, allow entering PiP if the app is not locked. If it is over the
1285 // keyguard, then we will prompt to unlock in the caller before entering PiP.
Robert Carrc33658e2017-04-11 18:24:20 -07001286 return !isCurrentAppLocked &&
Winson Chungf7e03e12017-08-22 11:32:16 -07001287 (supportsEnterPipOnTaskSwitch || !beforeStopping);
Winson Chungc2baac02017-01-11 13:34:47 -08001288 case PAUSING:
1289 case PAUSED:
Winson Chungbb348802017-01-30 12:01:45 -08001290 // When pausing, then only allow enter PiP as in the resume state, and in addition,
1291 // require that there is not an existing PiP activity and that the current system
1292 // state supports entering PiP
Winson Chunge581ebf2017-02-21 08:25:03 -08001293 return isNotLockedOrOnKeyguard && !hasPinnedStack
Winson Chungf7e03e12017-08-22 11:32:16 -07001294 && supportsEnterPipOnTaskSwitch;
Winson Chungc2baac02017-01-11 13:34:47 -08001295 case STOPPING:
1296 // When stopping in a valid state, then only allow enter PiP as in the pause state.
1297 // Otherwise, fall through to throw an exception if the caller is trying to enter
1298 // PiP in an invalid stopping state.
Winson Chungf7e03e12017-08-22 11:32:16 -07001299 if (supportsEnterPipOnTaskSwitch) {
Winson Chungf4ac0632017-03-17 12:34:12 -07001300 return isNotLockedOrOnKeyguard && !hasPinnedStack;
Winson Chungc2baac02017-01-11 13:34:47 -08001301 }
1302 default:
Winson Chung298f95b2017-08-10 15:57:18 -07001303 return false;
Winson Chungb5c41b72016-12-07 15:00:47 -08001304 }
Winson Chunge6308042016-10-31 09:24:01 -07001305 }
1306
Winson Chung59fda9e2017-01-20 16:14:51 -08001307 /**
Winson Chungf4ac0632017-03-17 12:34:12 -07001308 * @return Whether AppOps allows this package to enter picture-in-picture.
Winson Chung59fda9e2017-01-20 16:14:51 -08001309 */
Winson Chungf4ac0632017-03-17 12:34:12 -07001310 private boolean checkEnterPictureInPictureAppOpsState() {
Winson Chung59fda9e2017-01-20 16:14:51 -08001311 try {
Winson Chungf4ac0632017-03-17 12:34:12 -07001312 return service.getAppOpsService().checkOperation(OP_PICTURE_IN_PICTURE,
Winson Chung59fda9e2017-01-20 16:14:51 -08001313 appInfo.uid, packageName) == MODE_ALLOWED;
1314 } catch (RemoteException e) {
1315 // Local call
1316 }
1317 return false;
1318 }
1319
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001320 boolean isAlwaysFocusable() {
1321 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001322 }
1323
Jorim Jaggife762342016-10-13 14:33:27 +02001324
1325 /**
1326 * @return true if the activity contains windows that have
1327 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
1328 */
1329 boolean hasDismissKeyguardWindows() {
1330 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
1331 }
1332
Wale Ogunwale7d701172015-03-11 15:36:30 -07001333 void makeFinishingLocked() {
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001334 if (finishing) {
1335 return;
1336 }
1337 finishing = true;
1338 if (stopped) {
1339 clearOptionsLocked();
1340 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001341
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001342 if (service != null) {
1343 service.mTaskChangeNotificationController.notifyTaskStackChanged();
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001344 }
1345 }
1346
Dianne Hackborn7e269642010-08-25 19:50:20 -07001347 UriPermissionOwner getUriPermissionsLocked() {
1348 if (uriPermissions == null) {
1349 uriPermissions = new UriPermissionOwner(service, this);
1350 }
1351 return uriPermissions;
1352 }
1353
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001354 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 int requestCode, int resultCode,
1356 Intent resultData) {
1357 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -05001358 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001360 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 }
1362 results.add(r);
1363 }
1364
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001365 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 int requestCode) {
1367 if (results != null) {
1368 for (int i=results.size()-1; i>=0; i--) {
1369 ActivityResult r = (ActivityResult)results.get(i);
1370 if (r.mFrom != from) continue;
1371 if (r.mResultWho == null) {
1372 if (resultWho != null) continue;
1373 } else {
1374 if (!r.mResultWho.equals(resultWho)) continue;
1375 }
1376 if (r.mRequestCode != requestCode) continue;
1377
1378 results.remove(i);
1379 }
1380 }
1381 }
1382
Andrii Kulian21713ac2016-10-12 22:05:05 -07001383 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001385 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
1387 newIntents.add(intent);
1388 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001389
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001390 /**
1391 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1392 * method will be called at the proper time.
1393 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001394 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001395 // The activity now gets access to the data associated with this Intent.
1396 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001397 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001398 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001399 boolean unsent = true;
David Stevens9440dc82017-03-16 19:00:20 -07001400 final ActivityStack stack = getStack();
1401 final boolean isTopActivityWhileSleeping = isTopRunningActivity()
1402 && (stack != null ? stack.shouldSleepActivities() : service.isSleepingLocked());
Wale Ogunwale826c7062016-09-13 08:25:54 -07001403
1404 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001405 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1406 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001407 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Bryce Lee7ace3952018-02-16 14:34:32 -08001408 if ((mState == RESUMED || mState == PAUSED
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001409 || isTopActivityWhileSleeping) && app != null && app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001410 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001411 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1412 ar.add(rintent);
Bryce Leeb0f993f2018-03-02 15:38:01 -08001413 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Bryce Lee7ace3952018-02-16 14:34:32 -08001414 NewIntentItem.obtain(ar, mState == PAUSED));
Craig Mautner86d67a42013-05-14 10:34:38 -07001415 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001416 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001417 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001418 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001419 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001420 }
1421 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001422 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001423 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424 }
1425 }
1426
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001427 void updateOptionsLocked(ActivityOptions options) {
1428 if (options != null) {
1429 if (pendingOptions != null) {
1430 pendingOptions.abort();
1431 }
1432 pendingOptions = options;
1433 }
1434 }
1435
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001436 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001437 if (pendingOptions != null
Ruben Brunkf53497c2017-03-27 20:26:17 -07001438 && pendingOptions.getAnimationType() != ANIM_SCENE_TRANSITION) {
Michael Jurka21385cd2012-05-03 10:57:31 -07001439 final int animationType = pendingOptions.getAnimationType();
1440 switch (animationType) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001441 case ANIM_CUSTOM:
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001442 service.mWindowManager.overridePendingAppTransition(
1443 pendingOptions.getPackageName(),
1444 pendingOptions.getCustomEnterResId(),
Dianne Hackborn84375872012-06-01 19:03:50 -07001445 pendingOptions.getCustomExitResId(),
1446 pendingOptions.getOnAnimationStartListener());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001447 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001448 case ANIM_CLIP_REVEAL:
Chet Haase10e23ab2015-02-11 15:08:38 -08001449 service.mWindowManager.overridePendingAppTransitionClipReveal(
1450 pendingOptions.getStartX(), pendingOptions.getStartY(),
1451 pendingOptions.getWidth(), pendingOptions.getHeight());
1452 if (intent.getSourceBounds() == null) {
1453 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1454 pendingOptions.getStartY(),
1455 pendingOptions.getStartX()+pendingOptions.getWidth(),
1456 pendingOptions.getStartY()+pendingOptions.getHeight()));
1457 }
1458 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001459 case ANIM_SCALE_UP:
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001460 service.mWindowManager.overridePendingAppTransitionScaleUp(
1461 pendingOptions.getStartX(), pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001462 pendingOptions.getWidth(), pendingOptions.getHeight());
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001463 if (intent.getSourceBounds() == null) {
1464 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1465 pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001466 pendingOptions.getStartX()+pendingOptions.getWidth(),
1467 pendingOptions.getStartY()+pendingOptions.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001468 }
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001469 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001470 case ANIM_THUMBNAIL_SCALE_UP:
1471 case ANIM_THUMBNAIL_SCALE_DOWN:
Winson Chungaa7fa012017-05-24 15:50:06 -07001472 final boolean scaleUp = (animationType == ANIM_THUMBNAIL_SCALE_UP);
1473 final GraphicBuffer buffer = pendingOptions.getThumbnail();
1474 service.mWindowManager.overridePendingAppTransitionThumb(buffer,
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001475 pendingOptions.getStartX(), pendingOptions.getStartY(),
Michael Jurka21385cd2012-05-03 10:57:31 -07001476 pendingOptions.getOnAnimationStartListener(),
Michael Jurka832cb222012-04-13 09:32:47 -07001477 scaleUp);
Winson Chungf229ae52017-06-02 14:34:52 -07001478 if (intent.getSourceBounds() == null && buffer != null) {
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001479 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1480 pendingOptions.getStartY(),
Winson Chungaa7fa012017-05-24 15:50:06 -07001481 pendingOptions.getStartX() + buffer.getWidth(),
1482 pendingOptions.getStartY() + buffer.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001483 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001484 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001485 case ANIM_THUMBNAIL_ASPECT_SCALE_UP:
1486 case ANIM_THUMBNAIL_ASPECT_SCALE_DOWN:
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001487 final AppTransitionAnimationSpec[] specs = pendingOptions.getAnimSpecs();
Jorim Jaggi34795e32017-05-12 17:27:46 +02001488 final IAppTransitionAnimationSpecsFuture specsFuture =
1489 pendingOptions.getSpecsFuture();
1490 if (specsFuture != null) {
1491 service.mWindowManager.overridePendingAppTransitionMultiThumbFuture(
1492 specsFuture, pendingOptions.getOnAnimationStartListener(),
1493 animationType == ANIM_THUMBNAIL_ASPECT_SCALE_UP);
1494 } else if (animationType == ANIM_THUMBNAIL_ASPECT_SCALE_DOWN
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001495 && specs != null) {
1496 service.mWindowManager.overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -07001497 specs, pendingOptions.getOnAnimationStartListener(),
1498 pendingOptions.getAnimationFinishedListener(), false);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001499 } else {
1500 service.mWindowManager.overridePendingAppTransitionAspectScaledThumb(
1501 pendingOptions.getThumbnail(),
1502 pendingOptions.getStartX(), pendingOptions.getStartY(),
1503 pendingOptions.getWidth(), pendingOptions.getHeight(),
1504 pendingOptions.getOnAnimationStartListener(),
Ruben Brunkf53497c2017-03-27 20:26:17 -07001505 (animationType == ANIM_THUMBNAIL_ASPECT_SCALE_UP));
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001506 if (intent.getSourceBounds() == null) {
1507 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1508 pendingOptions.getStartY(),
1509 pendingOptions.getStartX() + pendingOptions.getWidth(),
1510 pendingOptions.getStartY() + pendingOptions.getHeight()));
1511 }
Winson Chunga4ccb862014-08-22 15:26:27 -07001512 }
1513 break;
Tony Mak089c35e2017-12-18 20:34:14 +00001514 case ANIM_OPEN_CROSS_PROFILE_APPS:
1515 service.mWindowManager.overridePendingAppTransitionStartCrossProfileApps();
1516 break;
Jorim Jaggi33a701a2017-12-01 14:58:18 +01001517 case ANIM_REMOTE_ANIMATION:
1518 service.mWindowManager.overridePendingAppTransitionRemote(
1519 pendingOptions.getRemoteAnimationAdapter());
1520 break;
Craig Mautner233ceee2014-05-09 17:05:11 -07001521 default:
1522 Slog.e(TAG, "applyOptionsLocked: Unknown animationType=" + animationType);
1523 break;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001524 }
chaviw82a0ba82018-03-15 14:26:29 -07001525
1526 if (task == null) {
1527 clearOptionsLocked(false /* withAbort */);
1528 } else {
1529 // This will clear the options for all the ActivityRecords for this Task.
1530 task.clearAllPendingOptions();
1531 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001532 }
1533 }
1534
Adam Powellcfbe9be2013-11-06 14:58:58 -08001535 ActivityOptions getOptionsForTargetActivityLocked() {
1536 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1537 }
1538
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001539 void clearOptionsLocked() {
chaviw82a0ba82018-03-15 14:26:29 -07001540 clearOptionsLocked(true /* withAbort */);
1541 }
1542
1543 void clearOptionsLocked(boolean withAbort) {
1544 if (withAbort && pendingOptions != null) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001545 pendingOptions.abort();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001546 }
chaviw82a0ba82018-03-15 14:26:29 -07001547 pendingOptions = null;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001548 }
1549
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001550 ActivityOptions takeOptionsLocked() {
1551 ActivityOptions opts = pendingOptions;
1552 pendingOptions = null;
1553 return opts;
1554 }
1555
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001557 if (uriPermissions != null) {
1558 uriPermissions.removeUriPermissionsLocked();
1559 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001560 }
1561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 void pauseKeyDispatchingLocked() {
1564 if (!keysPaused) {
1565 keysPaused = true;
Bryce Lee2b8e0372018-04-05 17:01:37 -07001566
1567 if (mWindowContainerController != null) {
1568 mWindowContainerController.pauseKeyDispatching();
1569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571 }
1572
1573 void resumeKeyDispatchingLocked() {
1574 if (keysPaused) {
1575 keysPaused = false;
Bryce Lee2b8e0372018-04-05 17:01:37 -07001576
1577 if (mWindowContainerController != null) {
1578 mWindowContainerController.resumeKeyDispatching();
1579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 }
1581 }
1582
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001583 private void updateTaskDescription(CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001584 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001585 }
1586
Wale Ogunwaleec950642017-04-25 07:44:21 -07001587 void setDeferHidingClient(boolean deferHidingClient) {
1588 if (mDeferHidingClient == deferHidingClient) {
1589 return;
1590 }
1591 mDeferHidingClient = deferHidingClient;
1592 if (!mDeferHidingClient && !visible) {
1593 // Hiding the client is no longer deferred and the app isn't visible still, go ahead and
1594 // update the visibility.
1595 setVisibility(false);
1596 }
Wale Ogunwale89973222017-04-23 18:39:45 -07001597 }
1598
Wale Ogunwaleec950642017-04-25 07:44:21 -07001599 void setVisibility(boolean visible) {
Tim Murray68ed8442017-08-29 23:21:27 +00001600 mWindowContainerController.setVisibility(visible, mDeferHidingClient);
Bryce Lee2a3cc462017-10-27 10:57:35 -07001601 mStackSupervisor.getActivityMetricsLogger().notifyVisibilityChanged(this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001602 }
1603
1604 // TODO: Look into merging with #setVisibility()
Wale Ogunwaleec950642017-04-25 07:44:21 -07001605 void setVisible(boolean newVisible) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001606 visible = newVisible;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001607 mDeferHidingClient = !visible && mDeferHidingClient;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001608 setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001609 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1610 }
1611
Bryce Lee7ace3952018-02-16 14:34:32 -08001612 void setState(ActivityState state, String reason) {
1613 if (DEBUG_STATES) Slog.v(TAG_STATES, "State movement: " + this + " from:" + getState()
1614 + " to:" + state + " reason:" + reason);
Bryce Lee6ff17072018-02-28 07:26:17 -08001615
Bryce Leeb0f993f2018-03-02 15:38:01 -08001616 if (state == mState) {
1617 // No need to do anything if state doesn't change.
1618 if (DEBUG_STATES) Slog.v(TAG_STATES, "State unchanged from:" + state);
1619 return;
1620 }
1621
Bryce Leeb0f993f2018-03-02 15:38:01 -08001622 mState = state;
1623
Bryce Leec4ab62a2018-03-05 14:19:26 -08001624 final TaskRecord parent = getTask();
1625
1626 if (parent != null) {
1627 parent.onActivityStateChanged(this, state, reason);
1628 }
Robert Carr29daa922018-04-27 11:56:48 -07001629
1630 if (state == STOPPING) {
1631 mWindowContainerController.notifyAppStopping();
1632 }
Bryce Lee7ace3952018-02-16 14:34:32 -08001633 }
1634
1635 ActivityState getState() {
1636 return mState;
1637 }
1638
1639 /**
1640 * Returns {@code true} if the Activity is in the specified state.
1641 */
1642 boolean isState(ActivityState state) {
1643 return state == mState;
1644 }
1645
1646 /**
1647 * Returns {@code true} if the Activity is in one of the specified states.
1648 */
1649 boolean isState(ActivityState state1, ActivityState state2) {
1650 return state1 == mState || state2 == mState;
1651 }
1652
1653 /**
1654 * Returns {@code true} if the Activity is in one of the specified states.
1655 */
1656 boolean isState(ActivityState state1, ActivityState state2, ActivityState state3) {
1657 return state1 == mState || state2 == mState || state3 == mState;
1658 }
1659
1660 /**
1661 * Returns {@code true} if the Activity is in one of the specified states.
1662 */
1663 boolean isState(ActivityState state1, ActivityState state2, ActivityState state3,
1664 ActivityState state4) {
1665 return state1 == mState || state2 == mState || state3 == mState || state4 == mState;
1666 }
1667
Jorim Jaggibae01b12017-04-11 16:29:10 -07001668 void notifyAppResumed(boolean wasStopped) {
1669 mWindowContainerController.notifyAppResumed(wasStopped);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001670 }
1671
1672 void notifyUnknownVisibilityLaunched() {
Jorim Jaggi838c2452017-08-28 15:44:43 +02001673
1674 // No display activities never add a window, so there is no point in waiting them for
1675 // relayout.
1676 if (!noDisplay) {
1677 mWindowContainerController.notifyUnknownVisibilityLaunched();
1678 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001679 }
1680
Jorim Jaggi241ae102016-11-02 21:57:33 -07001681 /**
1682 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1683 * might have on the visibility
1684 *
1685 * @see {@link ActivityStack#checkKeyguardVisibility}
1686 */
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001687 boolean shouldBeVisibleIgnoringKeyguard(boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001688 if (!okToShowLocked()) {
1689 return false;
1690 }
1691
Winson Chung3f0e59a2017-10-25 10:19:05 -07001692 return !behindFullscreenActivity || mLaunchTaskBehind;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001693 }
1694
1695 void makeVisibleIfNeeded(ActivityRecord starting) {
1696 // This activity is not currently visible, but is running. Tell it to become visible.
Bryce Lee7ace3952018-02-16 14:34:32 -08001697 if (mState == RESUMED || this == starting) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001698 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
Bryce Lee7ace3952018-02-16 14:34:32 -08001699 "Not making visible, r=" + this + " state=" + mState + " starting=" + starting);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001700 return;
1701 }
1702
1703 // If this activity is paused, tell it to now show its window.
1704 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1705 "Making visible and scheduling visibility: " + this);
1706 final ActivityStack stack = getStack();
1707 try {
1708 if (stack.mTranslucentActivityWaiting != null) {
1709 updateOptionsLocked(returningOptions);
1710 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1711 }
1712 setVisible(true);
1713 sleeping = false;
1714 app.pendingUiClean = true;
Bryce Leeb0f993f2018-03-02 15:38:01 -08001715 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -08001716 WindowVisibilityItem.obtain(true /* showWindow */));
Andrii Kulian21713ac2016-10-12 22:05:05 -07001717 // The activity may be waiting for stop, but that is no longer appropriate for it.
1718 mStackSupervisor.mStoppingActivities.remove(this);
1719 mStackSupervisor.mGoingToSleepActivities.remove(this);
Andrii Kulian0d595f32018-02-21 15:47:33 -08001720
Bryce Leea0fb8e02018-02-28 14:21:07 -08001721 // If the activity is stopped or stopping, cycle to the paused state. We avoid doing
1722 // this when there is an activity waiting to become translucent as the extra binder
1723 // calls will lead to noticeable jank. A later call to
1724 // ActivityStack#ensureActivitiesVisibleLocked will bring the activity to the proper
Bryce Leed939cf02018-03-12 09:04:44 -07001725 // paused state. We also avoid doing this for the activity the stack supervisor
1726 // considers the resumed activity, as normal means will bring the activity from STOPPED
1727 // to RESUMED. Adding PAUSING in this scenario will lead to double lifecycles.
1728 if (isState(STOPPED, STOPPING) && stack.mTranslucentActivityWaiting == null
1729 && mStackSupervisor.getResumedActivityLocked() != this) {
Andrii Kulian0d595f32018-02-21 15:47:33 -08001730 // Capture reason before state change
Andrii Kulian0d595f32018-02-21 15:47:33 -08001731
1732 // An activity must be in the {@link PAUSING} state for the system to validate
1733 // the move to {@link PAUSED}.
Bryce Lee7ace3952018-02-16 14:34:32 -08001734 setState(PAUSING, "makeVisibleIfNeeded");
Bryce Leeb0f993f2018-03-02 15:38:01 -08001735 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian0d595f32018-02-21 15:47:33 -08001736 PauseActivityItem.obtain(finishing, false /* userLeaving */,
Bryce Lee1d0d5142018-04-12 10:35:07 -07001737 configChangeFlags, false /* dontReport */));
Andrii Kulian0d595f32018-02-21 15:47:33 -08001738 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001739 } catch (Exception e) {
1740 // Just skip on any failure; we'll make it visible when it next restarts.
Bryce Leea0fb8e02018-02-28 14:21:07 -08001741 Slog.w(TAG, "Exception thrown making visible: " + intent.getComponent(), e);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001742 }
1743 handleAlreadyVisible();
1744 }
1745
1746 boolean handleAlreadyVisible() {
1747 stopFreezingScreenLocked(false);
1748 try {
1749 if (returningOptions != null) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001750 app.thread.scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001751 }
1752 } catch(RemoteException e) {
1753 }
Bryce Lee7ace3952018-02-16 14:34:32 -08001754 return mState == RESUMED;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001755 }
1756
1757 static void activityResumedLocked(IBinder token) {
1758 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1759 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1760 if (r != null) {
1761 r.icicle = null;
1762 r.haveState = false;
1763 }
1764 }
1765
1766 /**
1767 * Once we know that we have asked an application to put an activity in the resumed state
1768 * (either by launching it or explicitly telling it), this function updates the rest of our
1769 * state to match that fact.
1770 */
1771 void completeResumeLocked() {
1772 final boolean wasVisible = visible;
Jorim Jaggi8d062052017-08-22 14:55:17 +02001773 setVisible(true);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001774 if (!wasVisible) {
1775 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1776 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1777 }
1778 idle = false;
1779 results = null;
1780 newIntents = null;
1781 stopped = false;
1782
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001783 if (isActivityTypeHome()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001784 ProcessRecord app = task.mActivities.get(0).app;
1785 if (app != null && app != service.mHomeProcess) {
1786 service.mHomeProcess = app;
1787 }
1788 }
1789
1790 if (nowVisible) {
1791 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1792 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001793 }
1794
1795 // Schedule an idle timeout in case the app doesn't do it for us.
1796 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1797
1798 mStackSupervisor.reportResumedActivityLocked(this);
1799
1800 resumeKeyDispatchingLocked();
1801 final ActivityStack stack = getStack();
Jorim Jaggifa9ed962018-01-25 00:16:49 +01001802 mStackSupervisor.mNoAnimActivities.clear();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001803
1804 // Mark the point when the activity is resuming
1805 // TODO: To be more accurate, the mark should be before the onCreate,
1806 // not after the onResume. But for subsequent starts, onResume is fine.
1807 if (app != null) {
1808 cpuTimeAtResume = service.mProcessCpuTracker.getCpuTimeForPid(app.pid);
1809 } else {
1810 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1811 }
1812
1813 returningOptions = null;
chaviw59b98852017-06-13 12:05:44 -07001814
1815 if (canTurnScreenOn()) {
1816 mStackSupervisor.wakeUp("turnScreenOnFlag");
1817 } else {
1818 // If the screen is going to turn on because the caller explicitly requested it and
1819 // the keyguard is not showing don't attempt to sleep. Otherwise the Activity will
1820 // pause and then resume again later, which will result in a double life-cycle event.
David Stevens9440dc82017-03-16 19:00:20 -07001821 stack.checkReadyForSleep();
chaviw59b98852017-06-13 12:05:44 -07001822 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001823 }
1824
1825 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1826 CharSequence description) {
1827 final ActivityStack stack = getStack();
Bryce Lee7ace3952018-02-16 14:34:32 -08001828 if (mState != STOPPING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001829 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
Ruben Brunkf53497c2017-03-27 20:26:17 -07001830 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001831 return;
1832 }
1833 if (newPersistentState != null) {
1834 persistentState = newPersistentState;
1835 service.notifyTaskPersisterLocked(task, false);
1836 }
1837 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001838
Andrii Kulian21713ac2016-10-12 22:05:05 -07001839 if (newIcicle != null) {
1840 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1841 // the state.
1842 icicle = newIcicle;
1843 haveState = true;
1844 launchCount = 0;
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001845 updateTaskDescription(description);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001846 }
1847 if (!stopped) {
1848 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
Ruben Brunkf53497c2017-03-27 20:26:17 -07001849 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001850 stopped = true;
Bryce Lee7ace3952018-02-16 14:34:32 -08001851 setState(STOPPED, "activityStoppedLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001852
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001853 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001854
Andrii Kulian21713ac2016-10-12 22:05:05 -07001855 if (finishing) {
1856 clearOptionsLocked();
1857 } else {
1858 if (deferRelaunchUntilPaused) {
1859 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
1860 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1861 } else {
1862 mStackSupervisor.updatePreviousProcessLocked(this);
1863 }
1864 }
1865 }
1866 }
1867
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001868 void startLaunchTickingLocked() {
Jeff Sharkey5ab02432017-06-27 11:01:36 -06001869 if (Build.IS_USER) {
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001870 return;
1871 }
1872 if (launchTickTime == 0) {
1873 launchTickTime = SystemClock.uptimeMillis();
1874 continueLaunchTickingLocked();
1875 }
1876 }
1877
1878 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001879 if (launchTickTime == 0) {
1880 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001881 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001882
Andrii Kulian02b7a832016-10-06 23:11:56 -07001883 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001884 if (stack == null) {
1885 return false;
1886 }
1887
Ruben Brunkf53497c2017-03-27 20:26:17 -07001888 Message msg = stack.mHandler.obtainMessage(LAUNCH_TICK_MSG, this);
1889 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
1890 stack.mHandler.sendMessageDelayed(msg, LAUNCH_TICK);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001891 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001892 }
1893
1894 void finishLaunchTickingLocked() {
1895 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001896 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001897 if (stack != null) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001898 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001899 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001900 }
1901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 // IApplicationToken
1903
1904 public boolean mayFreezeScreenLocked(ProcessRecord app) {
1905 // Only freeze the screen if this activity is currently attached to
1906 // an application, and that application is not blocked or unresponding.
1907 // In any other case, we can't count on getting the screen unfrozen,
1908 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -08001909 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
1913 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001914 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 }
1916 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 public void stopFreezingScreenLocked(boolean force) {
1919 if (force || frozenBeforeDestroy) {
1920 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001921 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001924
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001925 public void reportFullyDrawnLocked(boolean restoredFromBundle) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001926 final long curTime = SystemClock.uptimeMillis();
1927 if (displayStartTime != 0) {
1928 reportLaunchTimeLocked(curTime);
1929 }
Jorim Jaggide8305a2018-05-15 12:06:35 -07001930 final LaunchTimeTracker.Entry entry = mStackSupervisor.getLaunchTimeTracker().getEntry(
1931 getWindowingMode());
1932 if (fullyDrawnStartTime != 0 && entry != null) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001933 final long thisTime = curTime - fullyDrawnStartTime;
Jorim Jaggide8305a2018-05-15 12:06:35 -07001934 final long totalTime = entry.mFullyDrawnStartTime != 0
1935 ? (curTime - entry.mFullyDrawnStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001936 if (SHOW_ACTIVITY_START_TIME) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001937 Trace.asyncTraceEnd(TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1938 EventLog.writeEvent(AM_ACTIVITY_FULLY_DRAWN_TIME,
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001939 userId, System.identityHashCode(this), shortComponentName,
1940 thisTime, totalTime);
1941 StringBuilder sb = service.mStringBuilder;
1942 sb.setLength(0);
1943 sb.append("Fully drawn ");
1944 sb.append(shortComponentName);
1945 sb.append(": ");
1946 TimeUtils.formatDuration(thisTime, sb);
1947 if (thisTime != totalTime) {
1948 sb.append(" (total ");
1949 TimeUtils.formatDuration(totalTime, sb);
1950 sb.append(")");
1951 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001952 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001953 }
1954 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001955 //service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001956 }
Jorim Jaggide8305a2018-05-15 12:06:35 -07001957 entry.mFullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001958 }
Bryce Lee2a3cc462017-10-27 10:57:35 -07001959 mStackSupervisor.getActivityMetricsLogger().logAppTransitionReportedDrawn(this,
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001960 restoredFromBundle);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001961 fullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001962 }
1963
1964 private void reportLaunchTimeLocked(final long curTime) {
Jorim Jaggide8305a2018-05-15 12:06:35 -07001965 final LaunchTimeTracker.Entry entry = mStackSupervisor.getLaunchTimeTracker().getEntry(
1966 getWindowingMode());
1967 if (entry == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001968 return;
1969 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001970 final long thisTime = curTime - displayStartTime;
Jorim Jaggide8305a2018-05-15 12:06:35 -07001971 final long totalTime = entry.mLaunchStartTime != 0
1972 ? (curTime - entry.mLaunchStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001973 if (SHOW_ACTIVITY_START_TIME) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001974 Trace.asyncTraceEnd(TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
1975 EventLog.writeEvent(AM_ACTIVITY_LAUNCH_TIME,
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001976 userId, System.identityHashCode(this), shortComponentName,
1977 thisTime, totalTime);
1978 StringBuilder sb = service.mStringBuilder;
1979 sb.setLength(0);
1980 sb.append("Displayed ");
1981 sb.append(shortComponentName);
1982 sb.append(": ");
1983 TimeUtils.formatDuration(thisTime, sb);
1984 if (thisTime != totalTime) {
1985 sb.append(" (total ");
1986 TimeUtils.formatDuration(totalTime, sb);
1987 sb.append(")");
1988 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001989 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001990 }
1991 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
1992 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001993 //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001994 }
1995 displayStartTime = 0;
Jorim Jaggide8305a2018-05-15 12:06:35 -07001996 entry.mLaunchStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001997 }
1998
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001999 @Override
Sudheer Shankac766db02017-06-12 10:37:29 -07002000 public void onStartingWindowDrawn(long timestamp) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08002001 synchronized (service) {
Bryce Lee2a3cc462017-10-27 10:57:35 -07002002 mStackSupervisor.getActivityMetricsLogger().notifyStartingWindowDrawn(
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +01002003 getWindowingMode(), timestamp);
Jorim Jaggi3878ca32017-02-02 17:13:05 -08002004 }
2005 }
2006
2007 @Override
Sudheer Shankac766db02017-06-12 10:37:29 -07002008 public void onWindowsDrawn(long timestamp) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002009 synchronized (service) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +01002010 mStackSupervisor.getActivityMetricsLogger().notifyWindowsDrawn(getWindowingMode(),
2011 timestamp);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002012 if (displayStartTime != 0) {
Sudheer Shankac766db02017-06-12 10:37:29 -07002013 reportLaunchTimeLocked(timestamp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002015 mStackSupervisor.sendWaitingVisibleReportLocked(this);
2016 startTime = 0;
2017 finishLaunchTickingLocked();
2018 if (task != null) {
2019 task.hasBeenVisible = true;
2020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
2022 }
2023
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002024 @Override
2025 public void onWindowsVisible() {
2026 synchronized (service) {
2027 mStackSupervisor.reportActivityVisibleLocked(this);
2028 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
2029 if (!nowVisible) {
2030 nowVisible = true;
2031 lastVisibleTime = SystemClock.uptimeMillis();
Bryce Leeb7c9b802017-05-02 14:20:24 -07002032 if (idle || mStackSupervisor.isStoppingNoHistoryActivity()) {
2033 // If this activity was already idle or there is an activity that must be
2034 // stopped immediately after visible, then we now need to make sure we perform
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002035 // the full stop of any activities that are waiting to do so. This is because
2036 // we won't do that while they are still waiting for this one to become visible.
Bryce Lee4a194382017-04-04 14:32:48 -07002037 final int size = mStackSupervisor.mActivitiesWaitingForVisibleActivity.size();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002038 if (size > 0) {
2039 for (int i = 0; i < size; i++) {
Bryce Lee4a194382017-04-04 14:32:48 -07002040 final ActivityRecord r =
2041 mStackSupervisor.mActivitiesWaitingForVisibleActivity.get(i);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002042 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
2043 }
Bryce Lee4a194382017-04-04 14:32:48 -07002044 mStackSupervisor.mActivitiesWaitingForVisibleActivity.clear();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002045 mStackSupervisor.scheduleIdleLocked();
2046 }
Bryce Leeb7c9b802017-05-02 14:20:24 -07002047 } else {
2048 // Instead of doing the full stop routine here, let's just hide any activities
2049 // we now can, and let them stop when the normal idle happens.
2050 mStackSupervisor.processStoppingActivitiesLocked(null /* idleActivity */,
2051 false /* remove */, true /* processPausingActivities */);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002052 }
2053 service.scheduleAppGcsLocked();
2054 }
2055 }
2056 }
2057
2058 @Override
2059 public void onWindowsGone() {
2060 synchronized (service) {
2061 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
2062 nowVisible = false;
2063 }
2064 }
2065
2066 @Override
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002067 public boolean keyDispatchingTimedOut(String reason, int windowPid) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002068 ActivityRecord anrActivity;
2069 ProcessRecord anrApp;
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002070 boolean windowFromSameProcessAsActivity;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002071 synchronized (service) {
2072 anrActivity = getWaitingHistoryRecordLocked();
2073 anrApp = app;
Brian Carlstrom7b0f2e82017-03-31 00:24:18 -07002074 windowFromSameProcessAsActivity =
2075 app == null || app.pid == windowPid || windowPid == -1;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002076 }
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002077 if (windowFromSameProcessAsActivity) {
2078 return service.inputDispatchingTimedOut(anrApp, anrActivity, this, false, reason);
2079 } else {
2080 // In this case another process added windows using this activity token. So, we call the
2081 // generic service input dispatch timed out method so that the right process is blamed.
2082 return service.inputDispatchingTimedOut(windowPid, false /* aboveSystem */, reason) < 0;
2083 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002084 }
2085
2086 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08002087 // First find the real culprit... if this activity is waiting for
2088 // another activity to start or has stopped, then the key dispatching
2089 // timeout should not be caused by this.
Bryce Lee4a194382017-04-04 14:32:48 -07002090 if (mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this) || stopped) {
Craig Mautner8f2adcb2014-04-07 22:21:33 +00002091 final ActivityStack stack = mStackSupervisor.getFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08002092 // Try to use the one which is closest to top.
Bryce Leec4ab62a2018-03-05 14:19:26 -08002093 ActivityRecord r = stack.getResumedActivity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002095 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08002097 if (r != null) {
2098 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
2100 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08002101 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
2103
Chong Zhang87761972016-08-22 13:53:24 -07002104 /** Checks whether the activity should be shown for current user. */
2105 public boolean okToShowLocked() {
Bryce Lee8558ec72017-08-17 15:37:26 -07002106 // We cannot show activities when the device is locked and the application is not
2107 // encryption aware.
2108 if (!StorageManager.isUserKeyUnlocked(userId)
2109 && !info.applicationInfo.isEncryptionAware()) {
2110 return false;
2111 }
2112
Chong Zhang87761972016-08-22 13:53:24 -07002113 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
2114 || (mStackSupervisor.isCurrentProfileLocked(userId)
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07002115 && service.mUserController.isUserRunning(userId, 0 /* flags */));
Chong Zhang87761972016-08-22 13:53:24 -07002116 }
2117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 /**
2119 * This method will return true if the activity is either visible, is becoming visible, is
2120 * currently pausing, or is resumed.
2121 */
2122 public boolean isInterestingToUserLocked() {
Bryce Lee7ace3952018-02-16 14:34:32 -08002123 return visible || nowVisible || mState == PAUSING || mState == RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002124 }
2125
Wale Ogunwale3e997362016-09-06 10:37:56 -07002126 void setSleeping(boolean _sleeping) {
2127 setSleeping(_sleeping, false);
2128 }
2129
2130 void setSleeping(boolean _sleeping, boolean force) {
2131 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002132 return;
2133 }
2134 if (app != null && app.thread != null) {
2135 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002136 app.thread.scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002137 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
2138 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002139 }
2140 sleeping = _sleeping;
2141 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002142 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002143 }
2144 }
2145 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08002146
Craig Mautnerd2328952013-03-05 12:46:26 -08002147 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002148 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08002149 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002150 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08002151 }
2152 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002153 final int activityNdx = task.mActivities.indexOf(r);
2154 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002155 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08002156 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002157 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08002158 }
2159
2160 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002161 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002162 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08002163 }
2164
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002165 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002166 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
2167 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002168 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08002169 }
2170 return null;
2171 }
2172
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002173 /**
2174 * @return display id to which this record is attached, -1 if not attached.
2175 */
2176 int getDisplayId() {
2177 final ActivityStack stack = getStack();
2178 if (stack == null) {
2179 return -1;
2180 }
2181 return stack.mDisplayId;
2182 }
2183
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002184 final boolean isDestroyable() {
Bryce Lee7ace3952018-02-16 14:34:32 -08002185 if (finishing || app == null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002186 // This would be redundant.
2187 return false;
2188 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002189 final ActivityStack stack = getStack();
Bryce Leec4ab62a2018-03-05 14:19:26 -08002190 if (stack == null || this == stack.getResumedActivity() || this == stack.mPausingActivity
Andrii Kulian02b7a832016-10-06 23:11:56 -07002191 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002192 // We're not ready for this kind of thing.
2193 return false;
2194 }
2195 if (visible) {
2196 // The user would notice this!
2197 return false;
2198 }
2199 return true;
2200 }
2201
Winson Chung3bad5cc02014-08-19 17:44:32 -07002202 private static String createImageFilename(long createTime, int taskId) {
2203 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Ruben Brunkf53497c2017-03-27 20:26:17 -07002204 IMAGE_EXTENSION;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002205 }
2206
Craig Mautner648f69b2014-09-18 14:16:26 -07002207 void setTaskDescription(TaskDescription _taskDescription) {
2208 Bitmap icon;
2209 if (_taskDescription.getIconFilename() == null &&
2210 (icon = _taskDescription.getIcon()) != null) {
2211 final String iconFilename = createImageFilename(createTime, task.taskId);
Winson Chungc8408b82017-01-25 17:58:56 -08002212 final File iconFile = new File(TaskPersister.getUserImagesDir(task.userId),
2213 iconFilename);
Suprabh Shukla23593142015-11-03 17:31:15 -08002214 final String iconFilePath = iconFile.getAbsolutePath();
Winson Chung3f0e59a2017-10-25 10:19:05 -07002215 service.getRecentTasks().saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08002216 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07002217 }
2218 taskDescription = _taskDescription;
2219 }
2220
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002221 void setVoiceSessionLocked(IVoiceInteractionSession session) {
2222 voiceSession = session;
2223 pendingVoiceInteractionStart = false;
2224 }
2225
2226 void clearVoiceSessionLocked() {
2227 voiceSession = null;
2228 pendingVoiceInteractionStart = false;
2229 }
2230
Jorim Jaggi02886a82016-12-06 09:10:06 -08002231 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch) {
Jorim Jaggi42befc62017-06-13 11:54:04 -07002232 showStartingWindow(prev, newTask, taskSwitch, false /* fromRecents */);
2233 }
2234
2235 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch,
2236 boolean fromRecents) {
Jorim Jaggi70176432017-01-18 12:52:13 +01002237 if (mWindowContainerController == null) {
2238 return;
2239 }
Wale Ogunwale19866e22017-04-19 06:05:13 -07002240 if (mTaskOverlay) {
2241 // We don't show starting window for overlay activities.
2242 return;
2243 }
2244
Wale Ogunwale3b232392016-05-13 15:37:13 -07002245 final CompatibilityInfo compatInfo =
2246 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002247 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
2248 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
Jorim Jaggibae01b12017-04-11 16:29:10 -07002249 prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning(),
Jorim Jaggi70aa4d12017-05-15 00:05:54 +02002250 allowTaskSnapshot(),
Bryce Lee7ace3952018-02-16 14:34:32 -08002251 mState.ordinal() >= RESUMED.ordinal() && mState.ordinal() <= STOPPED.ordinal(),
Jorim Jaggi42befc62017-06-13 11:54:04 -07002252 fromRecents);
Wale Ogunwale3b232392016-05-13 15:37:13 -07002253 if (shown) {
2254 mStartingWindowState = STARTING_WINDOW_SHOWN;
2255 }
2256 }
2257
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002258 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
Jorim Jaggicb956052017-05-09 16:27:24 +02002259 if (mStartingWindowState == STARTING_WINDOW_SHOWN && behindFullscreenActivity) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002260 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
2261 mStartingWindowState = STARTING_WINDOW_REMOVED;
Jorim Jaggi19be6052017-08-03 18:33:43 +02002262 mWindowContainerController.removeStartingWindow();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002263 }
2264 }
2265
2266 int getRequestedOrientation() {
2267 return mWindowContainerController.getOrientation();
2268 }
2269
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002270 void setRequestedOrientation(int requestedOrientation) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002271 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002272 final Configuration displayConfig =
2273 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
2274
2275 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
2276 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002277 if (config != null) {
2278 frozenBeforeDestroy = true;
2279 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
2280 false /* deferResume */, displayId)) {
2281 mStackSupervisor.resumeFocusedStackTopActivityLocked();
2282 }
2283 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07002284 service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
2285 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002286 }
2287
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01002288 void setDisablePreviewScreenshots(boolean disable) {
2289 mWindowContainerController.setDisablePreviewScreenshots(disable);
2290 }
2291
Bryce Leea163b762017-01-24 11:05:01 -08002292 /**
2293 * Set the last reported global configuration to the client. Should be called whenever a new
2294 * global configuration is sent to the client for this activity.
2295 */
2296 void setLastReportedGlobalConfiguration(@NonNull Configuration config) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002297 mLastReportedConfiguration.setGlobalConfiguration(config);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002298 }
2299
Bryce Leea163b762017-01-24 11:05:01 -08002300 /**
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002301 * Set the last reported configuration to the client. Should be called whenever
Bryce Leea163b762017-01-24 11:05:01 -08002302 * a new merged configuration is sent to the client for this activity.
2303 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002304 void setLastReportedConfiguration(@NonNull MergedConfiguration config) {
Bryce Lee8104e7a2017-08-17 09:16:03 -07002305 setLastReportedConfiguration(config.getGlobalConfiguration(),
2306 config.getOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08002307 }
2308
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002309 private void setLastReportedConfiguration(Configuration global, Configuration override) {
Bryce Lee8104e7a2017-08-17 09:16:03 -07002310 mLastReportedConfiguration.setConfiguration(global, override);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002311 }
2312
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002313 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002314 private void updateOverrideConfiguration() {
2315 mTmpConfig.unset();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002316 computeBounds(mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08002317
2318 if (mTmpBounds.equals(getOverrideBounds())) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002319 return;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002320 }
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002321
Bryce Leef3c6a472017-11-14 14:53:06 -08002322 setBounds(mTmpBounds);
2323
2324 final Rect updatedBounds = getOverrideBounds();
2325
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002326 // Bounds changed...update configuration to match.
Bryce Leef3c6a472017-11-14 14:53:06 -08002327 if (!matchParentBounds()) {
2328 task.computeOverrideConfiguration(mTmpConfig, updatedBounds, null /* insetBounds */,
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002329 false /* overrideWidth */, false /* overrideHeight */);
2330 }
Bryce Leef3c6a472017-11-14 14:53:06 -08002331
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002332 onOverrideConfigurationChanged(mTmpConfig);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002333 }
2334
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002335 /** Returns true if the configuration is compatible with this activity. */
Wale Ogunwale42f07d92017-05-01 21:32:58 -07002336 boolean isConfigurationCompatible(Configuration config) {
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002337 final int orientation = mWindowContainerController != null
2338 ? mWindowContainerController.getOrientation() : info.screenOrientation;
2339 if (isFixedOrientationPortrait(orientation)
2340 && config.orientation != ORIENTATION_PORTRAIT) {
2341 return false;
2342 }
2343 if (isFixedOrientationLandscape(orientation)
2344 && config.orientation != ORIENTATION_LANDSCAPE) {
2345 return false;
2346 }
2347 return true;
2348 }
2349
Bryce Lee7566d762017-03-30 09:34:15 -07002350 /**
2351 * Computes the bounds to fit the Activity within the bounds of the {@link Configuration}.
2352 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002353 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
2354 private void computeBounds(Rect outBounds) {
2355 outBounds.setEmpty();
2356 final float maxAspectRatio = info.maxAspectRatio;
2357 final ActivityStack stack = getStack();
Bryce Leef3c6a472017-11-14 14:53:06 -08002358 if (task == null || stack == null || task.inMultiWindowMode() || maxAspectRatio == 0
Bryce Leee5ab4502017-07-11 08:58:05 -07002359 || isInVrUiMode(getConfiguration())) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002360 // We don't set override configuration if that activity task isn't fullscreen. I.e. the
2361 // activity is in multi-window mode. Or, there isn't a max aspect ratio specified for
Bryce Leee5ab4502017-07-11 08:58:05 -07002362 // the activity. This is indicated by an empty {@link outBounds}. We also don't set it
2363 // if we are in VR mode.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002364 return;
2365 }
2366
Bryce Lee7566d762017-03-30 09:34:15 -07002367 // We must base this on the parent configuration, because we set our override
2368 // configuration's appBounds based on the result of this method. If we used our own
2369 // configuration, it would be influenced by past invocations.
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002370 final Rect appBounds = getParent().getWindowConfiguration().getAppBounds();
Wale Ogunwale822e5122017-07-26 06:02:24 -07002371 final int containingAppWidth = appBounds.width();
2372 final int containingAppHeight = appBounds.height();
Bryce Lee7566d762017-03-30 09:34:15 -07002373 int maxActivityWidth = containingAppWidth;
2374 int maxActivityHeight = containingAppHeight;
2375
2376 if (containingAppWidth < containingAppHeight) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002377 // Width is the shorter side, so we use that to figure-out what the max. height
2378 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002379 maxActivityHeight = (int) ((maxActivityWidth * maxAspectRatio) + 0.5f);
2380 } else {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002381 // Height is the shorter side, so we use that to figure-out what the max. width
2382 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002383 maxActivityWidth = (int) ((maxActivityHeight * maxAspectRatio) + 0.5f);
2384 }
2385
Bryce Lee7566d762017-03-30 09:34:15 -07002386 if (containingAppWidth <= maxActivityWidth && containingAppHeight <= maxActivityHeight) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002387 // The display matches or is less than the activity aspect ratio, so nothing else to do.
Bryce Lee7566d762017-03-30 09:34:15 -07002388 // Return the existing bounds. If this method is running for the first time,
Bryce Leef3c6a472017-11-14 14:53:06 -08002389 // {@link #getOverrideBounds()} will be empty (representing no override). If the method has run
2390 // before, then effect of {@link #getOverrideBounds()} will already have been applied to the
Bryce Lee7566d762017-03-30 09:34:15 -07002391 // value returned from {@link getConfiguration}. Refer to
2392 // {@link TaskRecord#computeOverrideConfiguration}.
Bryce Leef3c6a472017-11-14 14:53:06 -08002393 outBounds.set(getOverrideBounds());
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002394 return;
2395 }
2396
2397 // Compute configuration based on max supported width and height.
2398 outBounds.set(0, 0, maxActivityWidth, maxActivityHeight);
Andrii Kulian3a1619d2017-07-07 14:38:09 -07002399 // Position the activity frame on the opposite side of the nav bar.
2400 final int navBarPosition = service.mWindowManager.getNavBarPosition();
Wale Ogunwale822e5122017-07-26 06:02:24 -07002401 final int left = navBarPosition == NAV_BAR_LEFT ? appBounds.right - outBounds.width() : 0;
Andrii Kulian3a1619d2017-07-07 14:38:09 -07002402 outBounds.offsetTo(left, 0 /* top */);
2403 }
2404
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002405 boolean ensureActivityConfiguration(int globalChanges, boolean preserveWindow) {
2406 return ensureActivityConfiguration(globalChanges, preserveWindow,
2407 false /* ignoreStopState */);
2408 }
2409
Andrii Kulian21713ac2016-10-12 22:05:05 -07002410 /**
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002411 * Make sure the given activity matches the current configuration. Ensures the HistoryRecord
2412 * is updated with the correct configuration and all other bookkeeping is handled.
2413 *
2414 * @param globalChanges The changes to the global configuration.
2415 * @param preserveWindow If the activity window should be preserved on screen if the activity
2416 * is relaunched.
2417 * @param ignoreStopState If we should try to relaunch the activity even if it is in the stopped
2418 * state. This is useful for the case where we know the activity will be
2419 * visible soon and we want to ensure its configuration before we make it
2420 * visible.
2421 * @return True if the activity was relaunched and false if it wasn't relaunched because we
2422 * can't or the app handles the specific configuration that is changing.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002423 */
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002424 boolean ensureActivityConfiguration(int globalChanges, boolean preserveWindow,
2425 boolean ignoreStopState) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002426 final ActivityStack stack = getStack();
2427 if (stack.mConfigWillChange) {
2428 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2429 "Skipping config check (will change): " + this);
2430 return true;
2431 }
2432
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002433 // We don't worry about activities that are finishing.
2434 if (finishing) {
2435 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2436 "Configuration doesn't matter in finishing " + this);
2437 stopFreezingScreenLocked(false);
2438 return true;
2439 }
2440
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002441 if (!ignoreStopState && (mState == STOPPING || mState == STOPPED)) {
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002442 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2443 "Skipping config check stopped or stopping: " + this);
2444 return true;
2445 }
2446
Wale Ogunwaleea3d3fd2017-05-01 07:41:08 -07002447 // TODO: We should add ActivityRecord.shouldBeVisible() that checks if the activity should
2448 // be visible based on the stack, task, and lockscreen state and use that here instead. The
2449 // method should be based on the logic in ActivityStack.ensureActivitiesVisibleLocked().
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002450 // Skip updating configuration for activity is a stack that shouldn't be visible.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002451 if (!stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002452 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2453 "Skipping config check invisible stack: " + this);
2454 return true;
2455 }
2456
Andrii Kulian21713ac2016-10-12 22:05:05 -07002457 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2458 "Ensuring correct configuration: " + this);
2459
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002460 final int newDisplayId = getDisplayId();
2461 final boolean displayChanged = mLastReportedDisplayId != newDisplayId;
2462 if (displayChanged) {
2463 mLastReportedDisplayId = newDisplayId;
2464 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002465 // TODO(b/36505427): Is there a better place to do this?
2466 updateOverrideConfiguration();
2467
Winson Chungbdc646f2017-02-13 12:12:22 -08002468 // Short circuit: if the two full configurations are equal (the common case), then there is
2469 // nothing to do. We test the full configuration instead of the global and merged override
2470 // configurations because there are cases (like moving a task to the pinned stack) where
2471 // the combine configurations are equal, but would otherwise differ in the override config
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002472 mTmpConfig.setTo(mLastReportedConfiguration.getMergedConfiguration());
2473 if (getConfiguration().equals(mTmpConfig) && !forceNewConfig && !displayChanged) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002474 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002475 "Configuration & display unchanged in " + this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002476 return true;
2477 }
2478
2479 // Okay we now are going to make this activity have the new config.
2480 // But then we need to figure out how it needs to deal with that.
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002481
2482 // Find changes between last reported merged configuration and the current one. This is used
2483 // to decide whether to relaunch an activity or just report a configuration change.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002484 final int changes = getConfigurationChanges(mTmpConfig);
Ruben Brunkf64af332017-03-22 22:03:25 -07002485
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002486 // Update last reported values.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002487 final Configuration newMergedOverrideConfig = getMergedOverrideConfiguration();
Bryce Lee8104e7a2017-08-17 09:16:03 -07002488
2489 setLastReportedConfiguration(service.getGlobalConfiguration(), newMergedOverrideConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002490
Bryce Lee7ace3952018-02-16 14:34:32 -08002491 if (mState == INITIALIZING) {
Andrii Kulianb372da62018-01-18 10:46:24 -08002492 // No need to relaunch or schedule new config for activity that hasn't been launched
2493 // yet. We do, however, return after applying the config to activity record, so that
2494 // it will use it for launch transaction.
2495 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2496 "Skipping config check for initializing activity: " + this);
2497 return true;
2498 }
2499
Andrii Kulian21713ac2016-10-12 22:05:05 -07002500 if (changes == 0 && !forceNewConfig) {
2501 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2502 "Configuration no differences in " + this);
2503 // There are no significant differences, so we won't relaunch but should still deliver
2504 // the new configuration to the client process.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002505 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002506 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002507 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002508 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002509 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002510 return true;
2511 }
2512
2513 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002514 "Configuration changes for " + this + ", allChanges="
Andrii Kulian21713ac2016-10-12 22:05:05 -07002515 + Configuration.configurationDiffToString(changes));
2516
2517 // If the activity isn't currently running, just leave the new configuration and it will
2518 // pick that up next time it starts.
2519 if (app == null || app.thread == null) {
2520 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2521 "Configuration doesn't matter not running " + this);
2522 stopFreezingScreenLocked(false);
2523 forceNewConfig = false;
2524 return true;
2525 }
2526
2527 // Figure out how to handle the changes between the configurations.
2528 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2529 "Checking to restart " + info.name + ": changed=0x"
2530 + Integer.toHexString(changes) + ", handles=0x"
2531 + Integer.toHexString(info.getRealConfigChanged())
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002532 + ", mLastReportedConfiguration=" + mLastReportedConfiguration);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002533
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002534 if (shouldRelaunchLocked(changes, mTmpConfig) || forceNewConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002535 // Aha, the activity isn't handling the change, so DIE DIE DIE.
2536 configChangeFlags |= changes;
2537 startFreezingScreenLocked(app, globalChanges);
2538 forceNewConfig = false;
2539 preserveWindow &= isResizeOnlyChange(changes);
2540 if (app == null || app.thread == null) {
2541 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2542 "Config is destroying non-running " + this);
2543 stack.destroyActivityLocked(this, true, "config");
Bryce Lee7ace3952018-02-16 14:34:32 -08002544 } else if (mState == PAUSING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002545 // A little annoying: we are waiting for this activity to finish pausing. Let's not
2546 // do anything now, but just flag that it needs to be restarted when done pausing.
2547 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2548 "Config is skipping already pausing " + this);
2549 deferRelaunchUntilPaused = true;
2550 preserveWindowOnDeferredRelaunch = preserveWindow;
2551 return true;
Bryce Lee7ace3952018-02-16 14:34:32 -08002552 } else if (mState == RESUMED) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002553 // Try to optimize this case: the configuration is changing and we need to restart
2554 // the top, resumed activity. Instead of doing the normal handshaking, just say
2555 // "restart!".
2556 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2557 "Config is relaunching resumed " + this);
2558
2559 if (DEBUG_STATES && !visible) {
2560 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
2561 + " called by " + Debug.getCallers(4));
2562 }
2563
2564 relaunchActivityLocked(true /* andResume */, preserveWindow);
2565 } else {
2566 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2567 "Config is relaunching non-resumed " + this);
2568 relaunchActivityLocked(false /* andResume */, preserveWindow);
2569 }
2570
2571 // All done... tell the caller we weren't able to keep this activity around.
2572 return false;
2573 }
2574
2575 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002576 // NOTE: We only forward the override configuration as the system level configuration
Andrii Kulian21713ac2016-10-12 22:05:05 -07002577 // changes is always sent to all processes when they happen so it can just use whatever
2578 // system level configuration it last got.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002579 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002580 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002581 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002582 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002583 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002584 stopFreezingScreenLocked(false);
2585
2586 return true;
2587 }
2588
Zak Cohen90e7116742017-01-29 12:59:23 -08002589 /**
2590 * When assessing a configuration change, decide if the changes flags and the new configurations
2591 * should cause the Activity to relaunch.
Ruben Brunkf64af332017-03-22 22:03:25 -07002592 *
2593 * @param changes the changes due to the given configuration.
2594 * @param changesConfig the configuration that was used to calculate the given changes via a
2595 * call to getConfigurationChanges.
Zak Cohen90e7116742017-01-29 12:59:23 -08002596 */
Ruben Brunkf64af332017-03-22 22:03:25 -07002597 private boolean shouldRelaunchLocked(int changes, Configuration changesConfig) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002598 int configChanged = info.getRealConfigChanged();
Ruben Brunkf64af332017-03-22 22:03:25 -07002599 boolean onlyVrUiModeChanged = onlyVrUiModeChanged(changes, changesConfig);
Zak Cohen90e7116742017-01-29 12:59:23 -08002600
2601 // Override for apps targeting pre-O sdks
2602 // If a device is in VR mode, and we're transitioning into VR ui mode, add ignore ui mode
2603 // to the config change.
2604 // For O and later, apps will be required to add configChanges="uimode" to their manifest.
2605 if (appInfo.targetSdkVersion < O
2606 && requestedVrComponent != null
Ruben Brunkf64af332017-03-22 22:03:25 -07002607 && onlyVrUiModeChanged) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002608 configChanged |= CONFIG_UI_MODE;
2609 }
2610
2611 return (changes&(~configChanged)) != 0;
2612 }
2613
Ruben Brunkf64af332017-03-22 22:03:25 -07002614 /**
2615 * Returns true if the configuration change is solely due to the UI mode switching into or out
2616 * of UI_MODE_TYPE_VR_HEADSET.
2617 */
2618 private boolean onlyVrUiModeChanged(int changes, Configuration lastReportedConfig) {
2619 final Configuration currentConfig = getConfiguration();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002620 return changes == CONFIG_UI_MODE && (isInVrUiMode(currentConfig)
Ruben Brunkf64af332017-03-22 22:03:25 -07002621 != isInVrUiMode(lastReportedConfig));
2622 }
2623
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002624 private int getConfigurationChanges(Configuration lastReportedConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002625 // Determine what has changed. May be nothing, if this is a config that has come back from
2626 // the app after going idle. In that case we just want to leave the official config object
2627 // now in the activity and do nothing else.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002628 final Configuration currentConfig = getConfiguration();
2629 int changes = lastReportedConfig.diff(currentConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002630 // We don't want to use size changes if they don't cross boundaries that are important to
2631 // the app.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002632 if ((changes & CONFIG_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002633 final boolean crosses = crossesHorizontalSizeThreshold(lastReportedConfig.screenWidthDp,
2634 currentConfig.screenWidthDp)
2635 || crossesVerticalSizeThreshold(lastReportedConfig.screenHeightDp,
2636 currentConfig.screenHeightDp);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002637 if (!crosses) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002638 changes &= ~CONFIG_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002639 }
2640 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002641 if ((changes & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002642 final int oldSmallest = lastReportedConfig.smallestScreenWidthDp;
2643 final int newSmallest = currentConfig.smallestScreenWidthDp;
2644 if (!crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002645 changes &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002646 }
2647 }
Wale Ogunwale822e5122017-07-26 06:02:24 -07002648 // We don't want window configuration to cause relaunches.
2649 if ((changes & CONFIG_WINDOW_CONFIGURATION) != 0) {
2650 changes &= ~CONFIG_WINDOW_CONFIGURATION;
2651 }
Bryce Lee600dadd2017-07-25 10:48:42 -07002652
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002653 return changes;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002654 }
2655
2656 private static boolean isResizeOnlyChange(int change) {
2657 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2658 | CONFIG_SCREEN_LAYOUT)) == 0;
2659 }
2660
2661 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
2662 if (service.mSuppressResizeConfigChanges && preserveWindow) {
2663 configChangeFlags = 0;
2664 return;
2665 }
2666
2667 List<ResultInfo> pendingResults = null;
2668 List<ReferrerIntent> pendingNewIntents = null;
2669 if (andResume) {
2670 pendingResults = results;
2671 pendingNewIntents = newIntents;
2672 }
2673 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2674 "Relaunching: " + this + " with results=" + pendingResults
2675 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2676 + " preserveWindow=" + preserveWindow);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002677 EventLog.writeEvent(andResume ? AM_RELAUNCH_RESUME_ACTIVITY
2678 : AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
Andrii Kulian21713ac2016-10-12 22:05:05 -07002679 task.taskId, shortComponentName);
2680
2681 startFreezingScreenLocked(app, 0);
2682
Andrii Kulian21713ac2016-10-12 22:05:05 -07002683 try {
2684 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2685 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2686 + " callers=" + Debug.getCallers(6));
2687 forceNewConfig = false;
2688 mStackSupervisor.activityRelaunchingLocked(this);
Andrii Kulianb372da62018-01-18 10:46:24 -08002689 final ClientTransactionItem callbackItem = ActivityRelaunchItem.obtain(pendingResults,
2690 pendingNewIntents, configChangeFlags,
2691 new MergedConfiguration(service.getGlobalConfiguration(),
2692 getMergedOverrideConfiguration()),
2693 preserveWindow);
2694 final ActivityLifecycleItem lifecycleItem;
2695 if (andResume) {
2696 lifecycleItem = ResumeActivityItem.obtain(service.isNextTransitionForward());
2697 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -07002698 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulianb372da62018-01-18 10:46:24 -08002699 }
2700 final ClientTransaction transaction = ClientTransaction.obtain(app.thread, appToken);
2701 transaction.addCallback(callbackItem);
2702 transaction.setLifecycleStateRequest(lifecycleItem);
Bryce Leeb0f993f2018-03-02 15:38:01 -08002703 service.getLifecycleManager().scheduleTransaction(transaction);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002704 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
Andrii Kulianb372da62018-01-18 10:46:24 -08002705 // request resume if this activity is currently resumed, which implies we aren't
Andrii Kulian21713ac2016-10-12 22:05:05 -07002706 // sleeping.
2707 } catch (RemoteException e) {
2708 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2709 }
2710
2711 if (andResume) {
2712 if (DEBUG_STATES) {
2713 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2714 }
2715 results = null;
2716 newIntents = null;
Alan Viveretteb6a25732017-11-21 14:49:24 -05002717 service.getAppWarningsLocked().onResumeActivity(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002718 service.showAskCompatModeDialogLocked(this);
2719 } else {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002720 service.mHandler.removeMessages(PAUSE_TIMEOUT_MSG, this);
Bryce Lee7ace3952018-02-16 14:34:32 -08002721 setState(PAUSED, "relaunchActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07002722 }
2723
2724 configChangeFlags = 0;
2725 deferRelaunchUntilPaused = false;
2726 preserveWindowOnDeferredRelaunch = false;
2727 }
2728
Jorim Jaggibae01b12017-04-11 16:29:10 -07002729 private boolean isProcessRunning() {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002730 ProcessRecord proc = app;
2731 if (proc == null) {
2732 proc = service.mProcessNames.get(processName, info.applicationInfo.uid);
2733 }
2734 return proc != null && proc.thread != null;
2735 }
2736
Jorim Jaggibae01b12017-04-11 16:29:10 -07002737 /**
2738 * @return Whether a task snapshot starting window may be shown.
2739 */
2740 private boolean allowTaskSnapshot() {
2741 if (newIntents == null) {
2742 return true;
2743 }
2744
2745 // Restrict task snapshot starting window to launcher start, or there is no intent at all
2746 // (eg. task being brought to front). If the intent is something else, likely the app is
2747 // going to show some specific page or view, instead of what's left last time.
2748 for (int i = newIntents.size() - 1; i >= 0; i--) {
2749 final Intent intent = newIntents.get(i);
2750 if (intent != null && !ActivityRecord.isMainIntent(intent)) {
2751 return false;
2752 }
2753 }
2754 return true;
2755 }
2756
Bryce Leeb7c9b802017-05-02 14:20:24 -07002757 /**
2758 * Returns {@code true} if the associated activity has the no history flag set on it.
2759 * {@code false} otherwise.
2760 */
2761 boolean isNoHistory() {
2762 return (intent.getFlags() & FLAG_ACTIVITY_NO_HISTORY) != 0
2763 || (info.flags & FLAG_NO_HISTORY) != 0;
2764 }
2765
Craig Mautner21d24a22014-04-23 11:45:37 -07002766 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2767 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2768 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2769 if (launchedFromPackage != null) {
2770 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2771 }
2772 if (resolvedType != null) {
2773 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2774 }
2775 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2776 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002777
Craig Mautner21d24a22014-04-23 11:45:37 -07002778 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002779 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002780 }
2781
2782 out.startTag(null, TAG_INTENT);
2783 intent.saveToXml(out);
2784 out.endTag(null, TAG_INTENT);
2785
2786 if (isPersistable() && persistentState != null) {
2787 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2788 persistentState.saveToXml(out);
2789 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2790 }
2791 }
2792
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002793 static ActivityRecord restoreFromXml(XmlPullParser in,
2794 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002795 Intent intent = null;
2796 PersistableBundle persistentState = null;
2797 int launchedFromUid = 0;
2798 String launchedFromPackage = null;
2799 String resolvedType = null;
2800 boolean componentSpecified = false;
2801 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002802 long createTime = -1;
2803 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002804 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002805
2806 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2807 final String attrName = in.getAttributeName(attrNdx);
2808 final String attrValue = in.getAttributeValue(attrNdx);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002809 if (DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002810 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002811 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002812 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002813 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002814 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002815 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2816 launchedFromPackage = attrValue;
2817 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2818 resolvedType = attrValue;
2819 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002820 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002821 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002822 userId = Integer.parseInt(attrValue);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002823 } else if (attrName.startsWith(ATTR_TASKDESCRIPTION_PREFIX)) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002824 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002825 } else {
2826 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2827 }
2828 }
2829
2830 int event;
Ruben Brunkf53497c2017-03-27 20:26:17 -07002831 while (((event = in.next()) != END_DOCUMENT) &&
2832 (event != END_TAG || in.getDepth() >= outerDepth)) {
2833 if (event == START_TAG) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002834 final String name = in.getName();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002835 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002836 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002837 if (TAG_INTENT.equals(name)) {
2838 intent = Intent.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002839 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002840 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002841 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2842 persistentState = PersistableBundle.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002843 if (DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002844 "ActivityRecord: persistentState=" + persistentState);
2845 } else {
2846 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2847 XmlUtils.skipCurrentTag(in);
2848 }
2849 }
2850 }
2851
2852 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002853 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002854 }
2855
2856 final ActivityManagerService service = stackSupervisor.mService;
2857 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Patrick Baumann78380272018-04-04 10:41:01 -07002858 userId, Binder.getCallingUid());
Craig Mautnere0129b32014-05-25 16:41:09 -07002859 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002860 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2861 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002862 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002863 final ActivityRecord r = new ActivityRecord(service, null /* caller */,
2864 0 /* launchedFromPid */, launchedFromUid, launchedFromPackage, intent, resolvedType,
2865 aInfo, service.getConfiguration(), null /* resultTo */, null /* resultWho */,
2866 0 /* reqCode */, componentSpecified, false /* rootVoiceInteraction */,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002867 stackSupervisor, null /* options */, null /* sourceRecord */);
Craig Mautner21d24a22014-04-23 11:45:37 -07002868
2869 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002870 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002871 r.createTime = createTime;
2872
2873 return r;
2874 }
2875
Zak Cohen90e7116742017-01-29 12:59:23 -08002876 private static boolean isInVrUiMode(Configuration config) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002877 return (config.uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_VR_HEADSET;
Zak Cohen90e7116742017-01-29 12:59:23 -08002878 }
2879
David Stevens82ea6cb2017-03-03 16:18:50 -08002880 int getUid() {
2881 return info.applicationInfo.uid;
2882 }
2883
chaviw59b98852017-06-13 12:05:44 -07002884 void setShowWhenLocked(boolean showWhenLocked) {
2885 mShowWhenLocked = showWhenLocked;
Kevin Chyn44639482017-10-09 18:34:41 -07002886 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0 /* configChanges */,
2887 false /* preserveWindows */);
chaviw59b98852017-06-13 12:05:44 -07002888 }
2889
2890 /**
chaviw2c500982018-01-04 17:05:05 -08002891 * @return true if the activity windowing mode is not
2892 * {@link android.app.WindowConfiguration#WINDOWING_MODE_PINNED} and activity contains
2893 * windows that have {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set or if the activity
2894 * has set {@link #mShowWhenLocked}.
2895 * Multi-windowing mode will be exited if true is returned.
chaviw59b98852017-06-13 12:05:44 -07002896 */
2897 boolean canShowWhenLocked() {
chaviw2c500982018-01-04 17:05:05 -08002898 return !inPinnedWindowingMode() && (mShowWhenLocked
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002899 || service.mWindowManager.containsShowWhenLockedWindow(appToken));
chaviw59b98852017-06-13 12:05:44 -07002900 }
2901
2902 void setTurnScreenOn(boolean turnScreenOn) {
2903 mTurnScreenOn = turnScreenOn;
2904 }
2905
2906 /**
2907 * Determines whether this ActivityRecord can turn the screen on. It checks whether the flag
2908 * {@link #mTurnScreenOn} is set and checks whether the ActivityRecord should be visible
2909 * depending on Keyguard state
2910 *
2911 * @return true if the screen can be turned on, false otherwise.
2912 */
2913 boolean canTurnScreenOn() {
2914 final ActivityStack stack = getStack();
2915 return mTurnScreenOn && stack != null &&
2916 stack.checkKeyguardVisibility(this, true /* shouldBeVisible */, true /* isTop */);
2917 }
2918
2919 boolean getTurnScreenOnFlag() {
2920 return mTurnScreenOn;
2921 }
2922
2923 boolean isTopRunningActivity() {
2924 return mStackSupervisor.topRunningActivityLocked() == this;
2925 }
2926
Jorim Jaggif84e2f62018-01-16 14:17:59 +01002927 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
2928 mWindowContainerController.registerRemoteAnimations(definition);
2929 }
2930
Craig Mautnerf81b90872013-02-26 13:02:43 -08002931 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002933 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002934 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002935 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002936 }
2937 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002938 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002939 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002940 sb.append(" u");
2941 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002942 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002943 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002944 stringName = sb.toString();
2945 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07002947
2948 void writeIdentifierToProto(ProtoOutputStream proto, long fieldId) {
2949 final long token = proto.start(fieldId);
2950 proto.write(HASH_CODE, System.identityHashCode(this));
2951 proto.write(USER_ID, userId);
2952 proto.write(TITLE, intent.getComponent().flattenToShortString());
2953 proto.end(token);
2954 }
2955
2956 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2957 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002958 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07002959 writeIdentifierToProto(proto, IDENTIFIER);
Bryce Lee7ace3952018-02-16 14:34:32 -08002960 proto.write(STATE, mState.toString());
Steven Timotius4346f0a2017-09-12 11:07:21 -07002961 proto.write(VISIBLE, visible);
2962 proto.write(FRONT_OF_TASK, frontOfTask);
2963 if (app != null) {
2964 proto.write(PROC_ID, app.pid);
2965 }
2966 proto.end(token);
2967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968}