blob: 60f79f7202423f0519feae4d16594bc3280bfef8 [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;
Wale Ogunwale30eab1f2018-05-24 18:25:25 -0700118import static com.android.server.am.ActivityRecordProto.TRANSLUCENT;
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700119import static com.android.server.am.ActivityRecordProto.VISIBLE;
Adrian Roose99bc052017-11-20 17:55:31 +0100120import static com.android.server.policy.WindowManagerPolicy.NAV_BAR_LEFT;
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700121import static com.android.server.wm.IdentifierProto.HASH_CODE;
122import static com.android.server.wm.IdentifierProto.TITLE;
123import static com.android.server.wm.IdentifierProto.USER_ID;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700124
Ruben Brunkf53497c2017-03-27 20:26:17 -0700125import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
126import static org.xmlpull.v1.XmlPullParser.END_TAG;
127import static org.xmlpull.v1.XmlPullParser.START_TAG;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800128
Andrii Kulian21713ac2016-10-12 22:05:05 -0700129import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -0700130import android.app.ActivityManager.TaskDescription;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700131import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800132import android.app.PendingIntent;
Winson Chung709904f2017-04-25 11:00:48 -0700133import android.app.PictureInPictureParams;
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800134import android.app.ResultInfo;
Andrii Kulianb372da62018-01-18 10:46:24 -0800135import android.app.servertransaction.ActivityLifecycleItem;
136import android.app.servertransaction.ActivityRelaunchItem;
137import android.app.servertransaction.ClientTransaction;
138import android.app.servertransaction.ClientTransactionItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700139import android.app.servertransaction.MoveToDisplayItem;
140import android.app.servertransaction.MultiWindowModeChangeItem;
141import android.app.servertransaction.NewIntentItem;
Bryce Lee0bd8d422018-01-09 09:45:57 -0800142import android.app.servertransaction.PauseActivityItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700143import android.app.servertransaction.PipModeChangeItem;
Andrii Kulianb372da62018-01-18 10:46:24 -0800144import android.app.servertransaction.ResumeActivityItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700145import android.app.servertransaction.WindowVisibilityItem;
146import android.app.servertransaction.ActivityConfigurationChangeItem;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147import android.content.ComponentName;
148import android.content.Intent;
149import android.content.pm.ActivityInfo;
150import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700151import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import android.content.res.Configuration;
153import android.graphics.Bitmap;
Winson Chungaa7fa012017-05-24 15:50:06 -0700154import android.graphics.GraphicBuffer;
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700155import android.graphics.Rect;
Patrick Baumann78380272018-04-04 10:41:01 -0700156import android.os.Binder;
Bryce Lee39791592017-04-26 09:29:12 -0700157import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700159import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800160import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800162import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700164import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165import android.os.SystemClock;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800166import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700167import android.os.UserHandle;
Bryce Lee8558ec72017-08-17 15:37:26 -0700168import android.os.storage.StorageManager;
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800169import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170import android.util.EventLog;
171import android.util.Log;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700172import android.util.MergedConfiguration;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700174import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700175import android.util.proto.ProtoOutputStream;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700176import android.view.AppTransitionAnimationSpec;
Jorim Jaggi34795e32017-05-12 17:27:46 +0200177import android.view.IAppTransitionAnimationSpecsFuture;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178import android.view.IApplicationToken;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100179import android.view.RemoteAnimationDefinition;
Jorim Jaggife762342016-10-13 14:33:27 +0200180import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700181
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700182import com.android.internal.R;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800183import com.android.internal.app.ResolverActivity;
184import com.android.internal.content.ReferrerIntent;
185import com.android.internal.util.XmlUtils;
186import com.android.server.AttributeCache;
Ruben Brunkf53497c2017-03-27 20:26:17 -0700187import com.android.server.AttributeCache.Entry;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800188import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800189import com.android.server.wm.AppWindowContainerController;
190import com.android.server.wm.AppWindowContainerListener;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700191import com.android.server.wm.ConfigurationContainer;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800192import com.android.server.wm.TaskWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
Jorim Jaggi02886a82016-12-06 09:10:06 -0800194import org.xmlpull.v1.XmlPullParser;
195import org.xmlpull.v1.XmlPullParserException;
196import org.xmlpull.v1.XmlSerializer;
197
Suprabh Shukla23593142015-11-03 17:31:15 -0800198import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -0700199import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200import java.io.PrintWriter;
201import java.lang.ref.WeakReference;
202import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800203import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700205import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700206import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
208/**
209 * An entry in the history stack, representing an activity.
210 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700211final class ActivityRecord extends ConfigurationContainer implements AppWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800212 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_AM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700213 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
214 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700215 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700216 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700217 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Winson Chung16e185e2017-11-07 08:30:54 -0800218 // TODO(b/67864419): Remove once recents component is overridden
219 private static final String LEGACY_RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800220
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700221 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700222
Craig Mautner21d24a22014-04-23 11:45:37 -0700223 private static final String ATTR_ID = "id";
224 private static final String TAG_INTENT = "intent";
225 private static final String ATTR_USERID = "user_id";
226 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
227 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700228 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700229 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
230 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700231 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 final ActivityManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800234 final IApplicationToken.Stub appToken; // window manager token
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800235 AppWindowContainerController mWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final ActivityInfo info; // all about me
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -0700237 // TODO: This is duplicated state already contained in info.applicationInfo - remove
238 ApplicationInfo appInfo; // information about activity's app
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800239 final int launchedFromPid; // always the pid who started the activity.
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700240 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800241 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700242 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 final Intent intent; // the original intent that generated us
244 final ComponentName realActivity; // the intent component, or target of an alias.
245 final String shortComponentName; // the short component name of the intent
246 final String resolvedType; // as per original caller;
247 final String packageName; // the package implementing intent's component
248 final String processName; // process where this component wants to run
249 final String taskAffinity; // as per ActivityInfo.taskAffinity
250 final boolean stateNotNeeded; // As per ActivityInfo.flags
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700251 boolean fullscreen; // The activity is opaque and fills the entire space of this task.
252 // TODO: See if it possible to combine this with the fullscreen field.
253 final boolean hasWallpaper; // Has a wallpaper window as a background.
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800254 final boolean noDisplay; // activity is not displayed?
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800255 private final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700256 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700257
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800258 private CharSequence nonLocalizedLabel; // the label information from the package mgr.
259 private int labelRes; // the label information from the package mgr.
260 private int icon; // resource identifier of activity's icon.
261 private int logo; // resource identifier of activity's logo.
262 private int theme; // resource identifier of activity's theme.
263 private int realTheme; // actual theme resource we will use, never 0.
264 private int windowFlags; // custom window flags for preview window.
Bryce Leeaf691c02017-03-20 14:20:22 -0700265 private TaskRecord task; // the task this is in.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800266 private long createTime = System.currentTimeMillis();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700267 long displayStartTime; // when we started launching this activity
268 long fullyDrawnStartTime; // when we started launching this activity
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800269 private long startTime; // last time this activity was started
Dianne Hackborn50685602011-12-01 12:23:37 -0800270 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700271 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700272 long pauseTime; // last time we started pausing the activity
273 long launchTickTime; // base time for launch tick messages
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700274 // Last configuration reported to the activity in the client process.
275 private MergedConfiguration mLastReportedConfiguration;
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800276 private int mLastReportedDisplayId;
Winson Chung609e1e92017-05-08 10:52:12 -0700277 private boolean mLastReportedMultiWindowMode;
278 private boolean mLastReportedPictureInPictureMode;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700279 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700280 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 final String resultWho; // additional identifier for use by resultTo.
282 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800283 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800285 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700286 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700287 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700288 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -0700290 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700291 ProcessRecord app; // if non-null, hosting application
Bryce Lee7ace3952018-02-16 14:34:32 -0800292 private ActivityState mState; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700294 PersistableBundle persistentState; // last persistently saved activity state
Wale Ogunwale66e16852017-10-19 13:35:52 -0700295 // TODO: See if this is still needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 boolean frontOfTask; // is this the root activity of its task?
297 boolean launchFailed; // set if a launched failed, to abort on 2nd try
298 boolean haveState; // have we gotten the last activity state?
299 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700300 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800302 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
303 // completed
304 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 int configChangeFlags; // which config values have changed
Wale Ogunwaleec950642017-04-25 07:44:21 -0700306 private boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 int launchMode; // the launch mode activity attribute.
Charles He2bf28322017-10-12 22:24:49 +0100308 int lockTaskLaunchMode; // the lockTaskMode manifest attribute, subject to override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700310 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
311 // might hide this activity?
Wale Ogunwaleec950642017-04-25 07:44:21 -0700312 private boolean mDeferHidingClient; // If true we told WM to defer reporting to the client
313 // process that it is hidden.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800314 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 boolean nowVisible; // is this activity's window visible?
Andrii Kuliana39ae3e2018-05-31 12:43:54 -0700316 boolean mClientVisibilityDeferred;// was the visibility change message to client deferred?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 boolean idle; // has the activity gone idle?
318 boolean hasBeenLaunched;// has this activity ever been launched?
319 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400320 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400321 boolean forceNewConfig; // force re-create with new config next time
Winson Chungf7e03e12017-08-22 11:32:16 -0700322 boolean supportsEnterPipOnTaskSwitch; // This flag is set by the system to indicate that the
323 // activity can enter picture in picture while pausing (only when switching to another task)
Winson Chung709904f2017-04-25 11:00:48 -0700324 PictureInPictureParams pictureInPictureArgs = new PictureInPictureParams.Builder().build();
325 // The PiP params used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800326 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800327 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800328 ComponentName requestedVrComponent; // the requested component for handling VR mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700330 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700331
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700332 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700333 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800334
335 static final int STARTING_WINDOW_NOT_SHOWN = 0;
336 static final int STARTING_WINDOW_SHOWN = 1;
337 static final int STARTING_WINDOW_REMOVED = 2;
338 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700339 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800340
Craig Mautner21d24a22014-04-23 11:45:37 -0700341 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700342 boolean mLaunchTaskBehind; // this activity is actively being launched with
343 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700344
Filip Gruszczynski23493322015-07-29 17:02:59 -0700345 // These configurations are collected from application's resources based on size-sensitive
346 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
347 // and drawable-sw400dp will be added to both as 400.
348 private int[] mVerticalSizeConfigurations;
349 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700350 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700351
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800352 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
353 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
354
Robert Carrd2265122016-08-05 10:25:21 -0700355 // A hint to override the window specified rotation animation, or -1
356 // to use the window specified value. We use this so that
357 // we can select the right animation in the cases of starting
358 // windows, where the app hasn't had time to set a value
359 // on the window.
360 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700361
chaviw59b98852017-06-13 12:05:44 -0700362 private boolean mShowWhenLocked;
363 private boolean mTurnScreenOn;
364
Andrii Kulian21713ac2016-10-12 22:05:05 -0700365 /**
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -0800366 * Temp configs used in {@link #ensureActivityConfiguration(int, boolean)}
Andrii Kulian21713ac2016-10-12 22:05:05 -0700367 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700368 private final Configuration mTmpConfig = new Configuration();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700369 private final Rect mTmpBounds = new Rect();
Andrii Kulian21713ac2016-10-12 22:05:05 -0700370
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800371 private static String startingWindowStateToString(int state) {
372 switch (state) {
373 case STARTING_WINDOW_NOT_SHOWN:
374 return "STARTING_WINDOW_NOT_SHOWN";
375 case STARTING_WINDOW_SHOWN:
376 return "STARTING_WINDOW_SHOWN";
377 case STARTING_WINDOW_REMOVED:
378 return "STARTING_WINDOW_REMOVED";
379 default:
380 return "unknown state=" + state;
381 }
382 }
383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700385 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700386 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
387 pw.print(" processName="); pw.println(processName);
388 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700389 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800390 pw.print(" userId="); pw.println(userId);
391 pw.print(prefix); pw.print("app="); pw.println(app);
392 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700393 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
394 pw.print(" task="); pw.println(task);
395 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700396 pw.print(prefix); pw.print("realActivity=");
397 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700398 if (appInfo != null) {
399 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
400 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
401 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
402 }
403 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800404 if (appInfo.splitSourceDirs != null) {
405 pw.print(prefix); pw.print("splitDir=");
406 pw.println(Arrays.toString(appInfo.splitSourceDirs));
407 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800408 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700409 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
410 pw.print(" componentSpecified="); pw.print(componentSpecified);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700411 pw.print(" mActivityType="); pw.println(
412 activityTypeToString(getActivityType()));
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700413 if (rootVoiceInteraction) {
414 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
415 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800416 pw.print(prefix); pw.print("compat="); pw.print(compat);
417 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
418 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
419 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700420 pw.println(prefix + "mLastReportedConfigurations:");
421 mLastReportedConfiguration.dump(pw, prefix + " ");
422
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700423 pw.print(prefix); pw.print("CurrentConfiguration="); pw.println(getConfiguration());
424 if (!getOverrideConfiguration().equals(EMPTY)) {
425 pw.println(prefix + "OverrideConfiguration=" + getOverrideConfiguration());
426 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800427 if (!matchParentBounds()) {
428 pw.println(prefix + "bounds=" + getBounds());
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700429 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700430 if (resultTo != null || resultWho != null) {
431 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
432 pw.print(" resultWho="); pw.print(resultWho);
433 pw.print(" resultCode="); pw.println(requestCode);
434 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700435 if (taskDescription != null) {
436 final String iconFilename = taskDescription.getIconFilename();
437 if (iconFilename != null || taskDescription.getLabel() != null ||
438 taskDescription.getPrimaryColor() != 0) {
439 pw.print(prefix); pw.print("taskDescription:");
Craig Mautner29c58ca2014-10-14 16:17:06 -0700440 pw.print(" label=\""); pw.print(taskDescription.getLabel());
441 pw.print("\"");
Matthew Ng54bc9422017-10-02 17:16:28 -0700442 pw.print(" icon="); pw.print(taskDescription.getInMemoryIcon() != null
443 ? taskDescription.getInMemoryIcon().getByteCount() + " bytes"
444 : "null");
445 pw.print(" iconResource="); pw.print(taskDescription.getIconResource());
446 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200447 pw.print(" primaryColor=");
Craig Mautner29c58ca2014-10-14 16:17:06 -0700448 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700449 pw.print(prefix + " backgroundColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200450 pw.println(Integer.toHexString(taskDescription.getBackgroundColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700451 pw.print(prefix + " statusBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200452 pw.println(Integer.toHexString(taskDescription.getStatusBarColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700453 pw.print(prefix + " navigationBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200454 pw.println(Integer.toHexString(taskDescription.getNavigationBarColor()));
Craig Mautner29c58ca2014-10-14 16:17:06 -0700455 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700456 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700457 if (results != null) {
458 pw.print(prefix); pw.print("results="); pw.println(results);
459 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700460 if (pendingResults != null && pendingResults.size() > 0) {
461 pw.print(prefix); pw.println("Pending Results:");
462 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
463 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
464 pw.print(prefix); pw.print(" - ");
465 if (pir == null) {
466 pw.println("null");
467 } else {
468 pw.println(pir);
469 pir.dump(pw, prefix + " ");
470 }
471 }
472 }
473 if (newIntents != null && newIntents.size() > 0) {
474 pw.print(prefix); pw.println("Pending New Intents:");
475 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800476 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700477 pw.print(prefix); pw.print(" - ");
478 if (intent == null) {
479 pw.println("null");
480 } else {
481 pw.println(intent.toShortString(false, true, false, true));
482 }
483 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700484 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700485 if (pendingOptions != null) {
486 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
487 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700488 if (appTimeTracker != null) {
489 appTimeTracker.dumpWithHeader(pw, prefix, false);
490 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700491 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700492 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700493 }
494 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800495 pw.print(" launchCount="); pw.print(launchCount);
496 pw.print(" lastLaunchTime=");
497 if (lastLaunchTime == 0) pw.print("0");
498 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
499 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700500 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700501 pw.print(" icicle="); pw.println(icicle);
Bryce Lee7ace3952018-02-16 14:34:32 -0800502 pw.print(prefix); pw.print("state="); pw.print(mState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700503 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700504 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700505 pw.print(" finishing="); pw.println(finishing);
506 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
507 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700508 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800509 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800510 pw.print(" idle="); pw.print(idle);
511 pw.print(" mStartingWindowState=");
512 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800513 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
514 pw.print(" noDisplay="); pw.print(noDisplay);
515 pw.print(" immersive="); pw.print(immersive);
516 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800517 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400518 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700519 pw.print(prefix); pw.print("mActivityType=");
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700520 pw.println(activityTypeToString(getActivityType()));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800521 if (requestedVrComponent != null) {
522 pw.print(prefix);
523 pw.print("requestedVrComponent=");
524 pw.println(requestedVrComponent);
525 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700526 if (displayStartTime != 0 || startTime != 0) {
527 pw.print(prefix); pw.print("displayStartTime=");
528 if (displayStartTime == 0) pw.print("0");
529 else TimeUtils.formatDuration(displayStartTime, now, pw);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700530 pw.print(" startTime=");
531 if (startTime == 0) pw.print("0");
532 else TimeUtils.formatDuration(startTime, now, pw);
533 pw.println();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700534 }
Bryce Lee4a194382017-04-04 14:32:48 -0700535 final boolean waitingVisible =
536 mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800537 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700538 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800539 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700540 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700541 if (lastVisibleTime == 0) pw.print("0");
542 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
543 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700544 }
Wale Ogunwaleec950642017-04-25 07:44:21 -0700545 if (mDeferHidingClient) {
546 pw.println(prefix + "mDeferHidingClient=" + mDeferHidingClient);
547 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800548 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
549 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700550 pw.print(" configChangeFlags=");
551 pw.println(Integer.toHexString(configChangeFlags));
552 }
553 if (connections != null) {
554 pw.print(prefix); pw.print("connections="); pw.println(connections);
555 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800556 if (info != null) {
557 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
Winson Chung609e1e92017-05-08 10:52:12 -0700558 pw.println(prefix + "mLastReportedMultiWindowMode=" + mLastReportedMultiWindowMode
559 + " mLastReportedPictureInPictureMode=" + mLastReportedPictureInPictureMode);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700560 if (info.supportsPictureInPicture()) {
561 pw.println(prefix + "supportsPictureInPicture=" + info.supportsPictureInPicture());
Winson Chungf7e03e12017-08-22 11:32:16 -0700562 pw.println(prefix + "supportsEnterPipOnTaskSwitch: "
563 + supportsEnterPipOnTaskSwitch);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700564 }
565 if (info.maxAspectRatio != 0) {
566 pw.println(prefix + "maxAspectRatio=" + info.maxAspectRatio);
567 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -0700571 void updateApplicationInfo(ApplicationInfo aInfo) {
572 appInfo = aInfo;
573 info.applicationInfo = aInfo;
574 }
575
Andrii Kulian21713ac2016-10-12 22:05:05 -0700576 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700577 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
578 }
579
Andrii Kulian21713ac2016-10-12 22:05:05 -0700580 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700581 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
582 }
583
Andrii Kulian21713ac2016-10-12 22:05:05 -0700584 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700585 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
586 }
587
Filip Gruszczynski23493322015-07-29 17:02:59 -0700588 /**
589 * The purpose of this method is to decide whether the activity needs to be relaunched upon
590 * changing its size. In most cases the activities don't need to be relaunched, if the resize
591 * is small, all the activity content has to do is relayout itself within new bounds. There are
592 * cases however, where the activity's content would be completely changed in the new size and
593 * the full relaunch is required.
594 *
595 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
596 * required. These thresholds are collected from the application resource qualifiers. For
597 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
598 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
599 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
600 * of the threshold.
601 */
602 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
603 int secondDp) {
604 if (thresholds == null) {
605 return false;
606 }
607 for (int i = thresholds.length - 1; i >= 0; i--) {
608 final int threshold = thresholds[i];
609 if ((firstDp < threshold && secondDp >= threshold)
610 || (firstDp >= threshold && secondDp < threshold)) {
611 return true;
612 }
613 }
614 return false;
615 }
616
Andrii Kulian21713ac2016-10-12 22:05:05 -0700617 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700618 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700619 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
620 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700621 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700622 }
623
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800624 private void scheduleActivityMovedToDisplay(int displayId, Configuration config) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800625 if (app == null || app.thread == null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800626 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.w(TAG,
627 "Can't report activity moved to display - client not running, activityRecord="
628 + this + ", displayId=" + displayId);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800629 return;
630 }
631 try {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800632 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
633 "Reporting activity moved to display" + ", activityRecord=" + this
634 + ", displayId=" + displayId + ", config=" + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700635
Bryce Leeb0f993f2018-03-02 15:38:01 -0800636 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800637 MoveToDisplayItem.obtain(displayId, config));
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800638 } catch (RemoteException e) {
639 // If process died, whatever.
640 }
641 }
642
643 private void scheduleConfigurationChanged(Configuration config) {
644 if (app == null || app.thread == null) {
645 if (DEBUG_CONFIGURATION) Slog.w(TAG,
646 "Can't report activity configuration update - client not running"
647 + ", activityRecord=" + this);
648 return;
649 }
650 try {
651 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + ", config: "
652 + config);
653
Bryce Leeb0f993f2018-03-02 15:38:01 -0800654 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800655 ActivityConfigurationChangeItem.obtain(config));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800656 } catch (RemoteException e) {
657 // If process died, whatever.
658 }
659 }
660
Winson Chung5af42fc2017-03-24 17:11:33 -0700661 void updateMultiWindowMode() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700662 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800663 return;
664 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700665
Wale Ogunwaleeb76b762017-11-17 10:08:04 -0800666 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
667 // Don't do anything if we are currently deferring multi-window mode change.
668 return;
669 }
670
Winson Chung5af42fc2017-03-24 17:11:33 -0700671 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Wale Ogunwaleeb76b762017-11-17 10:08:04 -0800672 final boolean inMultiWindowMode = inMultiWindowMode();
Winson Chung609e1e92017-05-08 10:52:12 -0700673 if (inMultiWindowMode != mLastReportedMultiWindowMode) {
674 mLastReportedMultiWindowMode = inMultiWindowMode;
Winson Chung5af42fc2017-03-24 17:11:33 -0700675 scheduleMultiWindowModeChanged(getConfiguration());
676 }
677 }
678
679 private void scheduleMultiWindowModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800680 try {
Bryce Leeb0f993f2018-03-02 15:38:01 -0800681 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800682 MultiWindowModeChangeItem.obtain(mLastReportedMultiWindowMode,
Andrii Kulian446e8242017-10-26 15:17:29 -0700683 overrideConfig));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800684 } catch (Exception e) {
685 // If process died, I don't care.
686 }
687 }
688
Winson Chungab76bbc2017-08-14 13:33:51 -0700689 void updatePictureInPictureMode(Rect targetStackBounds, boolean forceUpdate) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700690 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800691 return;
692 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700693
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700694 final boolean inPictureInPictureMode = inPinnedWindowingMode() && targetStackBounds != null;
Winson Chungab76bbc2017-08-14 13:33:51 -0700695 if (inPictureInPictureMode != mLastReportedPictureInPictureMode || forceUpdate) {
Winson Chung5af42fc2017-03-24 17:11:33 -0700696 // Picture-in-picture mode changes also trigger a multi-window mode change as well, so
Winson Chungfe0a59b2018-08-08 16:10:20 -0700697 // update that here in order. Set the last reported MW state to the same as the PiP
698 // state since we haven't yet actually resized the task (these callbacks need to
699 // preceed the configuration change from the resiez.
700 // TODO(110009072): Once we move these callbacks to the client, remove all logic related
701 // to forcing the update of the picture-in-picture mode as a part of the PiP animation.
Winson Chung609e1e92017-05-08 10:52:12 -0700702 mLastReportedPictureInPictureMode = inPictureInPictureMode;
Winson Chungfe0a59b2018-08-08 16:10:20 -0700703 mLastReportedMultiWindowMode = inPictureInPictureMode;
Winson Chung5af42fc2017-03-24 17:11:33 -0700704 final Configuration newConfig = task.computeNewOverrideConfigurationForBounds(
705 targetStackBounds, null);
706 schedulePictureInPictureModeChanged(newConfig);
707 scheduleMultiWindowModeChanged(newConfig);
708 }
709 }
710
711 private void schedulePictureInPictureModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800712 try {
Bryce Leeb0f993f2018-03-02 15:38:01 -0800713 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800714 PipModeChangeItem.obtain(mLastReportedPictureInPictureMode,
Andrii Kulian446e8242017-10-26 15:17:29 -0700715 overrideConfig));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800716 } catch (Exception e) {
717 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800718 }
719 }
720
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700721 @Override
722 protected int getChildCount() {
723 // {@link ActivityRecord} is a leaf node and has no children.
724 return 0;
725 }
726
727 @Override
728 protected ConfigurationContainer getChildAt(int index) {
729 return null;
730 }
731
732 @Override
733 protected ConfigurationContainer getParent() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700734 return getTask();
735 }
736
737 TaskRecord getTask() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700738 return task;
739 }
740
Bryce Leeaf691c02017-03-20 14:20:22 -0700741 /**
742 * Sets reference to the {@link TaskRecord} the {@link ActivityRecord} will treat as its parent.
743 * Note that this does not actually add the {@link ActivityRecord} as a {@link TaskRecord}
744 * children. However, this method will clean up references to this {@link ActivityRecord} in
745 * {@link ActivityStack}.
746 * @param task The new parent {@link TaskRecord}.
747 */
748 void setTask(TaskRecord task) {
Bryce Lee84730a02018-04-03 14:10:04 -0700749 setTask(task /* task */, false /* reparenting */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700750 }
751
752 /**
753 * This method should only be called by {@link TaskRecord#removeActivity(ActivityRecord)}.
Bryce Lee84730a02018-04-03 14:10:04 -0700754 * @param task The new parent task.
755 * @param reparenting Whether we're in the middle of reparenting.
Bryce Leeaf691c02017-03-20 14:20:22 -0700756 */
757 void setTask(TaskRecord task, boolean reparenting) {
758 // Do nothing if the {@link TaskRecord} is the same as the current {@link getTask}.
759 if (task != null && task == getTask()) {
760 return;
761 }
762
Bryce Lee84730a02018-04-03 14:10:04 -0700763 final ActivityStack oldStack = getStack();
764 final ActivityStack newStack = task != null ? task.getStack() : null;
Bryce Leeaf691c02017-03-20 14:20:22 -0700765
Bryce Lee84730a02018-04-03 14:10:04 -0700766 // Inform old stack (if present) of activity removal and new stack (if set) of activity
767 // addition.
768 if (oldStack != newStack) {
769 if (!reparenting && oldStack != null) {
770 oldStack.onActivityRemovedFromStack(this);
771 }
772
773 if (newStack != null) {
774 newStack.onActivityAddedToStack(this);
775 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700776 }
777
778 this.task = task;
779
780 if (!reparenting) {
781 onParentChanged();
782 }
783 }
784
chaviw4ad54912018-05-30 11:05:44 -0700785 /**
786 * See {@link AppWindowContainerController#setWillCloseOrEnterPip(boolean)}
787 */
788 void setWillCloseOrEnterPip(boolean willCloseOrEnterPip) {
789 getWindowContainerController().setWillCloseOrEnterPip(willCloseOrEnterPip);
790 }
791
Dianne Hackbornbe707852011-11-11 14:32:10 -0800792 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700793 private final WeakReference<ActivityRecord> weakActivity;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700794 private final String name;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800795
Steven Timotiusaf03df62017-07-18 16:56:43 -0700796 Token(ActivityRecord activity, Intent intent) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800797 weakActivity = new WeakReference<>(activity);
Steven Timotiusaf03df62017-07-18 16:56:43 -0700798 name = intent.getComponent().flattenToShortString();
Wale Ogunwale7d701172015-03-11 15:36:30 -0700799 }
800
Andrii Kulian21713ac2016-10-12 22:05:05 -0700801 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700802 if (token == null) {
803 return null;
804 }
805 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700806 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700807 return null;
808 }
809 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800810 }
811
Craig Mautnerde4ef022013-04-07 19:01:33 -0700812 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800813 public String toString() {
814 StringBuilder sb = new StringBuilder(128);
815 sb.append("Token{");
816 sb.append(Integer.toHexString(System.identityHashCode(this)));
817 sb.append(' ');
818 sb.append(weakActivity.get());
819 sb.append('}');
820 return sb.toString();
821 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700822
823 @Override
824 public String getName() {
825 return name;
826 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800827 }
828
Wale Ogunwale7d701172015-03-11 15:36:30 -0700829 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800830 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700831 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800832 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800833 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800834 return null;
835 }
836 }
837
Chong Zhang85ee6542015-10-02 13:36:38 -0700838 boolean isResolverActivity() {
839 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700840 }
841
Patrick Baumann31426b22018-05-21 13:46:40 -0700842 boolean isResolverOrChildActivity() {
843 if (!"android".equals(packageName)) {
844 return false;
845 }
846 try {
847 return ResolverActivity.class.isAssignableFrom(
848 Object.class.getClassLoader().loadClass(realActivity.getClassName()));
849 } catch (ClassNotFoundException e) {
850 return false;
851 }
852 }
853
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800854 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller, int _launchedFromPid,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800855 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700857 ActivityRecord _resultTo, String _resultWho, int _reqCode,
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700858 boolean _componentSpecified, boolean _rootVoiceInteraction,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700859 ActivityStackSupervisor supervisor, ActivityOptions options,
860 ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 service = _service;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700862 appToken = new Token(this, _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 info = aInfo;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800864 launchedFromPid = _launchedFromPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800866 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700867 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 intent = _intent;
869 shortComponentName = _intent.getComponent().flattenToShortString();
870 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800871 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700872 rootVoiceInteraction = _rootVoiceInteraction;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700873 mLastReportedConfiguration = new MergedConfiguration(_configuration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 resultTo = _resultTo;
875 resultWho = _resultWho;
876 requestCode = _reqCode;
Bryce Lee7ace3952018-02-16 14:34:32 -0800877 setState(INITIALIZING, "ActivityRecord ctor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 frontOfTask = false;
879 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700881 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800883 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 keysPaused = false;
885 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700886 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 idle = false;
889 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700890 mStackSupervisor = supervisor;
Robert Carr0f5d7532016-10-17 16:39:17 -0700891
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800892 // This starts out true, since the initial state of an activity is that we have everything,
893 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700894 haveState = true;
895
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800896 // If the class name in the intent doesn't match that of the target, this is
897 // probably an alias. We have to create a new ComponentName object to keep track
898 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
899 if (aInfo.targetActivity == null
900 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
901 && (aInfo.launchMode == LAUNCH_MULTIPLE
902 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
903 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800905 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800907 taskAffinity = aInfo.taskAffinity;
908 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
909 appInfo = aInfo.applicationInfo;
910 nonLocalizedLabel = aInfo.nonLocalizedLabel;
911 labelRes = aInfo.labelRes;
912 if (nonLocalizedLabel == null && labelRes == 0) {
913 ApplicationInfo app = aInfo.applicationInfo;
914 nonLocalizedLabel = app.nonLocalizedLabel;
915 labelRes = app.labelRes;
916 }
917 icon = aInfo.getIconResource();
918 logo = aInfo.getLogoResource();
919 theme = aInfo.getThemeResource();
920 realTheme = theme;
921 if (realTheme == 0) {
922 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
923 ? android.R.style.Theme : android.R.style.Theme_Holo;
924 }
925 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
926 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
927 }
928 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
929 && (aInfo.applicationInfo.uid == SYSTEM_UID
930 || aInfo.applicationInfo.uid == _caller.info.uid)) {
931 processName = _caller.processName;
932 } else {
933 processName = aInfo.processName;
934 }
935
936 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
937 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
938 }
939
940 packageName = aInfo.applicationInfo.packageName;
941 launchMode = aInfo.launchMode;
942
Ruben Brunkf53497c2017-03-27 20:26:17 -0700943 Entry ent = AttributeCache.instance().get(packageName,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800944 realTheme, com.android.internal.R.styleable.Window, userId);
Bryce Leee83f34cd2017-10-31 19:50:54 -0700945
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700946 if (ent != null) {
947 fullscreen = !ActivityInfo.isTranslucentOrFloating(ent.array);
948 hasWallpaper = ent.array.getBoolean(R.styleable.Window_windowShowWallpaper, false);
949 noDisplay = ent.array.getBoolean(R.styleable.Window_windowNoDisplay, false);
950 } else {
951 hasWallpaper = false;
952 noDisplay = false;
953 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800954
Winson Chung83471632016-12-13 11:02:12 -0800955 setActivityType(_componentSpecified, _launchedFromUid, _intent, options, sourceRecord);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800956
957 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
958
959 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
960 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
chaviw59b98852017-06-13 12:05:44 -0700961
962 mShowWhenLocked = (aInfo.flags & FLAG_SHOW_WHEN_LOCKED) != 0;
963 mTurnScreenOn = (aInfo.flags & FLAG_TURN_SCREEN_ON) != 0;
Charles He2bf28322017-10-12 22:24:49 +0100964
965 mRotationAnimationHint = aInfo.rotationAnimation;
966 lockTaskLaunchMode = aInfo.lockTaskLaunchMode;
967 if (appInfo.isPrivilegedApp() && (lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_ALWAYS
968 || lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
969 lockTaskLaunchMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
970 }
971
972 if (options != null) {
973 pendingOptions = options;
974 mLaunchTaskBehind = options.getLaunchTaskBehind();
975
976 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
977 // Only override manifest supplied option if set.
978 if (rotationAnimation >= 0) {
979 mRotationAnimationHint = rotationAnimation;
980 }
981 final PendingIntent usageReport = pendingOptions.getUsageTimeReport();
982 if (usageReport != null) {
983 appTimeTracker = new AppTimeTracker(usageReport);
984 }
985 final boolean useLockTask = pendingOptions.getLockTaskMode();
986 if (useLockTask && lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_DEFAULT) {
987 lockTaskLaunchMode = LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
988 }
989 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800990 }
991
Dianne Hackborn68a06332017-11-15 17:54:18 -0800992 void setProcess(ProcessRecord proc) {
993 app = proc;
994 final ActivityRecord root = task != null ? task.getRootActivity() : null;
995 if (root == this) {
996 task.setRootProcess(proc);
997 }
998 }
999
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001000 AppWindowContainerController getWindowContainerController() {
1001 return mWindowContainerController;
1002 }
1003
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001004 void createWindowContainer() {
1005 if (mWindowContainerController != null) {
1006 throw new IllegalArgumentException("Window container=" + mWindowContainerController
1007 + " already created for r=" + this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001008 }
1009
1010 inHistory = true;
1011
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001012 final TaskWindowContainerController taskController = task.getWindowContainerController();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001013
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001014 // TODO(b/36505427): Maybe this call should be moved inside updateOverrideConfiguration()
1015 task.updateOverrideConfigurationFromLaunchBounds();
1016 // Make sure override configuration is up-to-date before using to create window controller.
1017 updateOverrideConfiguration();
1018
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001019 mWindowContainerController = new AppWindowContainerController(taskController, appToken,
1020 this, Integer.MAX_VALUE /* add on top */, info.screenOrientation, fullscreen,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001021 (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
1022 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
1023 appInfo.targetSdkVersion, mRotationAnimationHint,
Bryce Leef3c6a472017-11-14 14:53:06 -08001024 ActivityManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001025
1026 task.addActivityToTop(this);
1027
Winson Chung609e1e92017-05-08 10:52:12 -07001028 // When an activity is started directly into a split-screen fullscreen stack, we need to
1029 // update the initial multi-window modes so that the callbacks are scheduled correctly when
1030 // the user leaves that mode.
Bryce Leef3c6a472017-11-14 14:53:06 -08001031 mLastReportedMultiWindowMode = inMultiWindowMode();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001032 mLastReportedPictureInPictureMode = inPinnedWindowingMode();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001033 }
1034
1035 void removeWindowContainer() {
Bryce Lee7ace3952018-02-16 14:34:32 -08001036 // Do not try to remove a window container if we have already removed it.
1037 if (mWindowContainerController == null) {
1038 return;
1039 }
1040
Wale Ogunwalecc367f42017-02-01 08:12:14 -08001041 // Resume key dispatching if it is currently paused before we remove the container.
1042 resumeKeyDispatchingLocked();
1043
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001044 mWindowContainerController.removeContainer(getDisplayId());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001045 mWindowContainerController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047
Winson Chung30480042017-01-26 10:55:34 -08001048 /**
1049 * Reparents this activity into {@param newTask} at the provided {@param position}. The caller
1050 * should ensure that the {@param newTask} is not already the parent of this activity.
1051 */
1052 void reparent(TaskRecord newTask, int position, String reason) {
1053 final TaskRecord prevTask = task;
1054 if (prevTask == newTask) {
1055 throw new IllegalArgumentException(reason + ": task=" + newTask
1056 + " is already the parent of r=" + this);
1057 }
1058
Winson Chung74666102017-02-22 17:49:24 -08001059 // TODO: Ensure that we do not directly reparent activities across stacks, as that may leave
1060 // the stacks in strange states. For now, we should use Task.reparent() to ensure that
1061 // the stack is left in an OK state.
1062 if (prevTask != null && newTask != null && prevTask.getStack() != newTask.getStack()) {
1063 throw new IllegalArgumentException(reason + ": task=" + newTask
1064 + " is in a different stack (" + newTask.getStackId() + ") than the parent of"
1065 + " r=" + this + " (" + prevTask.getStackId() + ")");
1066 }
1067
Winson Chung30480042017-01-26 10:55:34 -08001068 // Must reparent first in window manager
1069 mWindowContainerController.reparent(newTask.getWindowContainerController(), position);
1070
Bryce Lee84730a02018-04-03 14:10:04 -07001071 // Reparenting prevents informing the parent stack of activity removal in the case that
1072 // the new stack has the same parent. we must manually signal here if this is not the case.
1073 final ActivityStack prevStack = prevTask.getStack();
1074
1075 if (prevStack != newTask.getStack()) {
1076 prevStack.onActivityRemovedFromStack(this);
1077 }
Bryce Leeaf691c02017-03-20 14:20:22 -07001078 // Remove the activity from the old task and add it to the new task.
Bryce Lee84730a02018-04-03 14:10:04 -07001079 prevTask.removeActivity(this, true /* reparenting */);
Bryce Lee0f9bde82017-02-22 16:39:06 -08001080
Winson Chung30480042017-01-26 10:55:34 -08001081 newTask.addActivityAtIndex(position, this);
1082 }
1083
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001084 private boolean isHomeIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001085 return ACTION_MAIN.equals(intent.getAction())
1086 && intent.hasCategory(CATEGORY_HOME)
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001087 && intent.getCategories().size() == 1
1088 && intent.getData() == null
1089 && intent.getType() == null;
1090 }
1091
Chong Zhangad24f962016-08-25 12:12:33 -07001092 static boolean isMainIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001093 return ACTION_MAIN.equals(intent.getAction())
1094 && intent.hasCategory(CATEGORY_LAUNCHER)
Chong Zhangad24f962016-08-25 12:12:33 -07001095 && intent.getCategories().size() == 1
1096 && intent.getData() == null
1097 && intent.getType() == null;
1098 }
1099
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001100 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
1101 if (uid == Process.myUid() || uid == 0) {
1102 // System process can launch home activity.
1103 return true;
1104 }
Winson Chung547afd22018-05-17 16:03:25 -07001105 // Allow the recents component to launch the home activity.
1106 final RecentTasks recentTasks = mStackSupervisor.mService.getRecentTasks();
1107 if (recentTasks != null && recentTasks.isCallerRecents(uid)) {
1108 return true;
1109 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001110 // Resolver activity can launch home activity.
1111 return sourceRecord != null && sourceRecord.isResolverActivity();
1112 }
1113
Winson Chung83471632016-12-13 11:02:12 -08001114 /**
1115 * @return whether the given package name can launch an assist activity.
1116 */
1117 private boolean canLaunchAssistActivity(String packageName) {
Winson Chungbccd4b52017-12-13 11:08:39 -08001118 final ComponentName assistComponent = service.mActiveVoiceInteractionServiceComponent;
Winson Chung83471632016-12-13 11:02:12 -08001119 if (assistComponent != null) {
1120 return assistComponent.getPackageName().equals(packageName);
1121 }
1122 return false;
1123 }
1124
1125 private void setActivityType(boolean componentSpecified, int launchedFromUid, Intent intent,
1126 ActivityOptions options, ActivityRecord sourceRecord) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001127 int activityType = ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001128 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
1129 && isHomeIntent(intent) && !isResolverActivity()) {
1130 // This sure looks like a home activity!
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001131 activityType = ACTIVITY_TYPE_HOME;
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001132
1133 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
1134 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
1135 // We only allow home activities to be resizeable if they explicitly requested it.
1136 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
1137 }
Winson Chung16e185e2017-11-07 08:30:54 -08001138 } else if (realActivity.getClassName().contains(LEGACY_RECENTS_PACKAGE_NAME) ||
1139 service.getRecentTasks().isRecentsComponent(realActivity, appInfo.uid)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001140 activityType = ACTIVITY_TYPE_RECENTS;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001141 } else if (options != null && options.getLaunchActivityType() == ACTIVITY_TYPE_ASSISTANT
Winson Chung83471632016-12-13 11:02:12 -08001142 && canLaunchAssistActivity(launchedFromPackage)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001143 activityType = ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001144 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001145 setActivityType(activityType);
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001146 }
1147
Craig Mautnera228ae92014-07-09 05:44:55 -07001148 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001149 if (launchMode != LAUNCH_SINGLE_INSTANCE && launchMode != LAUNCH_SINGLE_TASK) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001150 task.setTaskToAffiliateWith(taskToAffiliateWith);
1151 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001152 }
1153
Andrii Kulian02b7a832016-10-06 23:11:56 -07001154 /**
1155 * @return Stack value from current task, null if there is no task.
1156 */
Winson Chung55893332017-02-17 17:13:10 -08001157 <T extends ActivityStack> T getStack() {
1158 return task != null ? (T) task.getStack() : null;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001159 }
1160
Jorim Jaggicdfc04e2017-04-28 19:06:24 +02001161 int getStackId() {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001162 return getStack() != null ? getStack().mStackId : INVALID_STACK_ID;
1163 }
1164
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001165 ActivityDisplay getDisplay() {
1166 final ActivityStack stack = getStack();
1167 return stack != null ? stack.getDisplay() : null;
1168 }
1169
Craig Mautner5eda9b32013-07-02 11:58:16 -07001170 boolean changeWindowTranslucency(boolean toOpaque) {
1171 if (fullscreen == toOpaque) {
1172 return false;
1173 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001174
Craig Mautner5eda9b32013-07-02 11:58:16 -07001175 // Keep track of the number of fullscreen activities in this task.
1176 task.numFullscreen += toOpaque ? +1 : -1;
1177
1178 fullscreen = toOpaque;
1179 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -07001180 }
1181
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001182 void takeFromHistory() {
1183 if (inHistory) {
1184 inHistory = false;
1185 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001186 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001187 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001188 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001189 }
1190 }
1191
1192 boolean isInHistory() {
1193 return inHistory;
1194 }
1195
Wale Ogunwale7d701172015-03-11 15:36:30 -07001196 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001197 final ActivityStack stack = getStack();
1198 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001199 }
1200
Craig Mautner21d24a22014-04-23 11:45:37 -07001201 boolean isPersistable() {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001202 return (info.persistableMode == PERSIST_ROOT_ONLY ||
1203 info.persistableMode == PERSIST_ACROSS_REBOOTS) &&
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001204 (intent == null || (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -07001205 }
1206
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001207 boolean isFocusable() {
Bryce Lee7b851cc2018-04-10 14:53:13 -07001208 return mStackSupervisor.isFocusable(this, isAlwaysFocusable());
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001209 }
1210
1211 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001212 return ActivityInfo.isResizeableMode(info.resizeMode) || info.supportsPictureInPicture();
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001213 }
1214
Winson Chungd3395382016-12-13 11:49:09 -08001215 /**
1216 * @return whether this activity is non-resizeable or forced to be resizeable
1217 */
1218 boolean isNonResizableOrForcedResizable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001219 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -07001220 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -07001221 }
1222
Winson Chunge6308042016-10-31 09:24:01 -07001223 /**
Winson Chungd3395382016-12-13 11:49:09 -08001224 * @return whether this activity supports PiP multi-window and can be put in the pinned stack.
Winson Chunge6308042016-10-31 09:24:01 -07001225 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001226 boolean supportsPictureInPicture() {
Wale Ogunwale034a8ec2017-09-02 17:14:40 -07001227 return service.mSupportsPictureInPicture && isActivityTypeStandardOrUndefined()
Winson Chungd3395382016-12-13 11:49:09 -08001228 && info.supportsPictureInPicture();
1229 }
1230
1231 /**
1232 * @return whether this activity supports split-screen multi-window and can be put in the docked
1233 * stack.
1234 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001235 @Override
1236 public boolean supportsSplitScreenWindowingMode() {
Winson Chungd3395382016-12-13 11:49:09 -08001237 // An activity can not be docked even if it is considered resizeable because it only
1238 // supports picture-in-picture mode but has a non-resizeable resizeMode
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001239 return super.supportsSplitScreenWindowingMode()
1240 && service.mSupportsSplitScreenMultiWindow && supportsResizeableMultiWindow();
Winson Chungd3395382016-12-13 11:49:09 -08001241 }
1242
1243 /**
1244 * @return whether this activity supports freeform multi-window and can be put in the freeform
1245 * stack.
1246 */
1247 boolean supportsFreeform() {
1248 return service.mSupportsFreeformWindowManagement && supportsResizeableMultiWindow();
1249 }
1250
1251 /**
1252 * @return whether this activity supports non-PiP multi-window.
1253 */
1254 private boolean supportsResizeableMultiWindow() {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001255 return service.mSupportsMultiWindow && !isActivityTypeHome()
Winson Chungd3395382016-12-13 11:49:09 -08001256 && (ActivityInfo.isResizeableMode(info.resizeMode)
1257 || service.mForceResizableActivities);
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001258 }
1259
Winson Chunge6308042016-10-31 09:24:01 -07001260 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001261 * Check whether this activity can be launched on the specified display.
1262 * @param displayId Target display id.
1263 * @return {@code true} if either it is the default display or this activity is resizeable and
1264 * can be put a secondary screen.
1265 */
1266 boolean canBeLaunchedOnDisplay(int displayId) {
Bryce Lee1533b2b2017-09-14 17:06:41 -07001267 final TaskRecord task = getTask();
1268
1269 // The resizeability of an Activity's parent task takes precendence over the ActivityInfo.
1270 // This allows for a non resizable activity to be launched into a resizeable task.
1271 final boolean resizeable =
1272 task != null ? task.isResizeable() : supportsResizeableMultiWindow();
1273
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001274 return service.mStackSupervisor.canPlaceEntityOnDisplay(displayId,
Bryce Lee1533b2b2017-09-14 17:06:41 -07001275 resizeable, launchedFromPid, launchedFromUid, info);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001276 }
1277
1278 /**
Robert Carrc33658e2017-04-11 18:24:20 -07001279 * @param beforeStopping Whether this check is for an auto-enter-pip operation, that is to say
1280 * the activity has requested to enter PiP when it would otherwise be stopped.
1281 *
Winson Chung298f95b2017-08-10 15:57:18 -07001282 * @return whether this activity is currently allowed to enter PIP.
Winson Chunge6308042016-10-31 09:24:01 -07001283 */
Winson Chung298f95b2017-08-10 15:57:18 -07001284 boolean checkEnterPictureInPictureState(String caller, boolean beforeStopping) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07001285 if (!supportsPictureInPicture()) {
1286 return false;
1287 }
1288
Winson Chungf4ac0632017-03-17 12:34:12 -07001289 // Check app-ops and see if PiP is supported for this package
1290 if (!checkEnterPictureInPictureAppOpsState()) {
1291 return false;
1292 }
1293
Winson Chungf1bfee12017-03-24 17:11:33 -07001294 // Check to see if we are in VR mode, and disallow PiP if so
1295 if (service.shouldDisableNonVrUiLocked()) {
1296 return false;
1297 }
1298
Winson Chungc2baac02017-01-11 13:34:47 -08001299 boolean isKeyguardLocked = service.isKeyguardLocked();
Benjamin Franza83859f2017-07-03 16:34:14 +01001300 boolean isCurrentAppLocked = service.getLockTaskModeState() != LOCK_TASK_MODE_NONE;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001301 final ActivityDisplay display = getDisplay();
1302 boolean hasPinnedStack = display != null && display.hasPinnedStack();
Winson Chungbb348802017-01-30 12:01:45 -08001303 // Don't return early if !isNotLocked, since we want to throw an exception if the activity
1304 // is in an incorrect state
Winson Chunge581ebf2017-02-21 08:25:03 -08001305 boolean isNotLockedOrOnKeyguard = !isKeyguardLocked && !isCurrentAppLocked;
Robert Carrc33658e2017-04-11 18:24:20 -07001306
1307 // We don't allow auto-PiP when something else is already pipped.
1308 if (beforeStopping && hasPinnedStack) {
1309 return false;
1310 }
1311
Bryce Lee7ace3952018-02-16 14:34:32 -08001312 switch (mState) {
Winson Chungc2baac02017-01-11 13:34:47 -08001313 case RESUMED:
Winson Chunge581ebf2017-02-21 08:25:03 -08001314 // When visible, allow entering PiP if the app is not locked. If it is over the
1315 // keyguard, then we will prompt to unlock in the caller before entering PiP.
Robert Carrc33658e2017-04-11 18:24:20 -07001316 return !isCurrentAppLocked &&
Winson Chungf7e03e12017-08-22 11:32:16 -07001317 (supportsEnterPipOnTaskSwitch || !beforeStopping);
Winson Chungc2baac02017-01-11 13:34:47 -08001318 case PAUSING:
1319 case PAUSED:
Winson Chungbb348802017-01-30 12:01:45 -08001320 // When pausing, then only allow enter PiP as in the resume state, and in addition,
1321 // require that there is not an existing PiP activity and that the current system
1322 // state supports entering PiP
Winson Chunge581ebf2017-02-21 08:25:03 -08001323 return isNotLockedOrOnKeyguard && !hasPinnedStack
Winson Chungf7e03e12017-08-22 11:32:16 -07001324 && supportsEnterPipOnTaskSwitch;
Winson Chungc2baac02017-01-11 13:34:47 -08001325 case STOPPING:
1326 // When stopping in a valid state, then only allow enter PiP as in the pause state.
1327 // Otherwise, fall through to throw an exception if the caller is trying to enter
1328 // PiP in an invalid stopping state.
Winson Chungf7e03e12017-08-22 11:32:16 -07001329 if (supportsEnterPipOnTaskSwitch) {
Winson Chungf4ac0632017-03-17 12:34:12 -07001330 return isNotLockedOrOnKeyguard && !hasPinnedStack;
Winson Chungc2baac02017-01-11 13:34:47 -08001331 }
1332 default:
Winson Chung298f95b2017-08-10 15:57:18 -07001333 return false;
Winson Chungb5c41b72016-12-07 15:00:47 -08001334 }
Winson Chunge6308042016-10-31 09:24:01 -07001335 }
1336
Winson Chung59fda9e2017-01-20 16:14:51 -08001337 /**
Winson Chungf4ac0632017-03-17 12:34:12 -07001338 * @return Whether AppOps allows this package to enter picture-in-picture.
Winson Chung59fda9e2017-01-20 16:14:51 -08001339 */
Winson Chungf4ac0632017-03-17 12:34:12 -07001340 private boolean checkEnterPictureInPictureAppOpsState() {
Winson Chung59fda9e2017-01-20 16:14:51 -08001341 try {
Winson Chungf4ac0632017-03-17 12:34:12 -07001342 return service.getAppOpsService().checkOperation(OP_PICTURE_IN_PICTURE,
Winson Chung59fda9e2017-01-20 16:14:51 -08001343 appInfo.uid, packageName) == MODE_ALLOWED;
1344 } catch (RemoteException e) {
1345 // Local call
1346 }
1347 return false;
1348 }
1349
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001350 boolean isAlwaysFocusable() {
1351 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001352 }
1353
Jorim Jaggife762342016-10-13 14:33:27 +02001354
1355 /**
1356 * @return true if the activity contains windows that have
1357 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
1358 */
1359 boolean hasDismissKeyguardWindows() {
1360 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
1361 }
1362
Wale Ogunwale7d701172015-03-11 15:36:30 -07001363 void makeFinishingLocked() {
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001364 if (finishing) {
1365 return;
1366 }
1367 finishing = true;
1368 if (stopped) {
1369 clearOptionsLocked();
1370 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001371
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001372 if (service != null) {
1373 service.mTaskChangeNotificationController.notifyTaskStackChanged();
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001374 }
1375 }
1376
Dianne Hackborn7e269642010-08-25 19:50:20 -07001377 UriPermissionOwner getUriPermissionsLocked() {
1378 if (uriPermissions == null) {
1379 uriPermissions = new UriPermissionOwner(service, this);
1380 }
1381 return uriPermissions;
1382 }
1383
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001384 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 int requestCode, int resultCode,
1386 Intent resultData) {
1387 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -05001388 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001390 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 }
1392 results.add(r);
1393 }
1394
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001395 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 int requestCode) {
1397 if (results != null) {
1398 for (int i=results.size()-1; i>=0; i--) {
1399 ActivityResult r = (ActivityResult)results.get(i);
1400 if (r.mFrom != from) continue;
1401 if (r.mResultWho == null) {
1402 if (resultWho != null) continue;
1403 } else {
1404 if (!r.mResultWho.equals(resultWho)) continue;
1405 }
1406 if (r.mRequestCode != requestCode) continue;
1407
1408 results.remove(i);
1409 }
1410 }
1411 }
1412
Andrii Kulian21713ac2016-10-12 22:05:05 -07001413 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001415 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 }
1417 newIntents.add(intent);
1418 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001419
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001420 final boolean isSleeping() {
1421 final ActivityStack stack = getStack();
1422 return stack != null ? stack.shouldSleepActivities() : service.isSleepingLocked();
1423 }
1424
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001425 /**
1426 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1427 * method will be called at the proper time.
1428 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001429 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001430 // The activity now gets access to the data associated with this Intent.
1431 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001432 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001433 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001434 boolean unsent = true;
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001435 final boolean isTopActivityWhileSleeping = isTopRunningActivity() && isSleeping();
Wale Ogunwale826c7062016-09-13 08:25:54 -07001436
1437 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001438 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1439 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001440 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Bryce Lee7ace3952018-02-16 14:34:32 -08001441 if ((mState == RESUMED || mState == PAUSED
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001442 || isTopActivityWhileSleeping) && app != null && app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001443 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001444 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1445 ar.add(rintent);
Bryce Leeb0f993f2018-03-02 15:38:01 -08001446 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Bryce Lee7ace3952018-02-16 14:34:32 -08001447 NewIntentItem.obtain(ar, mState == PAUSED));
Craig Mautner86d67a42013-05-14 10:34:38 -07001448 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001449 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001450 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001451 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001452 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001453 }
1454 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001455 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001456 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001457 }
1458 }
1459
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001460 void updateOptionsLocked(ActivityOptions options) {
1461 if (options != null) {
1462 if (pendingOptions != null) {
1463 pendingOptions.abort();
1464 }
1465 pendingOptions = options;
1466 }
1467 }
1468
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001469 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001470 if (pendingOptions != null
Ruben Brunkf53497c2017-03-27 20:26:17 -07001471 && pendingOptions.getAnimationType() != ANIM_SCENE_TRANSITION) {
Michael Jurka21385cd2012-05-03 10:57:31 -07001472 final int animationType = pendingOptions.getAnimationType();
1473 switch (animationType) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001474 case ANIM_CUSTOM:
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001475 service.mWindowManager.overridePendingAppTransition(
1476 pendingOptions.getPackageName(),
1477 pendingOptions.getCustomEnterResId(),
Dianne Hackborn84375872012-06-01 19:03:50 -07001478 pendingOptions.getCustomExitResId(),
1479 pendingOptions.getOnAnimationStartListener());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001480 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001481 case ANIM_CLIP_REVEAL:
Chet Haase10e23ab2015-02-11 15:08:38 -08001482 service.mWindowManager.overridePendingAppTransitionClipReveal(
1483 pendingOptions.getStartX(), pendingOptions.getStartY(),
1484 pendingOptions.getWidth(), pendingOptions.getHeight());
1485 if (intent.getSourceBounds() == null) {
1486 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1487 pendingOptions.getStartY(),
1488 pendingOptions.getStartX()+pendingOptions.getWidth(),
1489 pendingOptions.getStartY()+pendingOptions.getHeight()));
1490 }
1491 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001492 case ANIM_SCALE_UP:
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001493 service.mWindowManager.overridePendingAppTransitionScaleUp(
1494 pendingOptions.getStartX(), pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001495 pendingOptions.getWidth(), pendingOptions.getHeight());
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001496 if (intent.getSourceBounds() == null) {
1497 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1498 pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001499 pendingOptions.getStartX()+pendingOptions.getWidth(),
1500 pendingOptions.getStartY()+pendingOptions.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001501 }
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001502 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001503 case ANIM_THUMBNAIL_SCALE_UP:
1504 case ANIM_THUMBNAIL_SCALE_DOWN:
Winson Chungaa7fa012017-05-24 15:50:06 -07001505 final boolean scaleUp = (animationType == ANIM_THUMBNAIL_SCALE_UP);
1506 final GraphicBuffer buffer = pendingOptions.getThumbnail();
1507 service.mWindowManager.overridePendingAppTransitionThumb(buffer,
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001508 pendingOptions.getStartX(), pendingOptions.getStartY(),
Michael Jurka21385cd2012-05-03 10:57:31 -07001509 pendingOptions.getOnAnimationStartListener(),
Michael Jurka832cb222012-04-13 09:32:47 -07001510 scaleUp);
Winson Chungf229ae52017-06-02 14:34:52 -07001511 if (intent.getSourceBounds() == null && buffer != null) {
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001512 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1513 pendingOptions.getStartY(),
Winson Chungaa7fa012017-05-24 15:50:06 -07001514 pendingOptions.getStartX() + buffer.getWidth(),
1515 pendingOptions.getStartY() + buffer.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001516 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001517 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001518 case ANIM_THUMBNAIL_ASPECT_SCALE_UP:
1519 case ANIM_THUMBNAIL_ASPECT_SCALE_DOWN:
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001520 final AppTransitionAnimationSpec[] specs = pendingOptions.getAnimSpecs();
Jorim Jaggi34795e32017-05-12 17:27:46 +02001521 final IAppTransitionAnimationSpecsFuture specsFuture =
1522 pendingOptions.getSpecsFuture();
1523 if (specsFuture != null) {
1524 service.mWindowManager.overridePendingAppTransitionMultiThumbFuture(
1525 specsFuture, pendingOptions.getOnAnimationStartListener(),
1526 animationType == ANIM_THUMBNAIL_ASPECT_SCALE_UP);
1527 } else if (animationType == ANIM_THUMBNAIL_ASPECT_SCALE_DOWN
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001528 && specs != null) {
1529 service.mWindowManager.overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -07001530 specs, pendingOptions.getOnAnimationStartListener(),
1531 pendingOptions.getAnimationFinishedListener(), false);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001532 } else {
1533 service.mWindowManager.overridePendingAppTransitionAspectScaledThumb(
1534 pendingOptions.getThumbnail(),
1535 pendingOptions.getStartX(), pendingOptions.getStartY(),
1536 pendingOptions.getWidth(), pendingOptions.getHeight(),
1537 pendingOptions.getOnAnimationStartListener(),
Ruben Brunkf53497c2017-03-27 20:26:17 -07001538 (animationType == ANIM_THUMBNAIL_ASPECT_SCALE_UP));
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001539 if (intent.getSourceBounds() == null) {
1540 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1541 pendingOptions.getStartY(),
1542 pendingOptions.getStartX() + pendingOptions.getWidth(),
1543 pendingOptions.getStartY() + pendingOptions.getHeight()));
1544 }
Winson Chunga4ccb862014-08-22 15:26:27 -07001545 }
1546 break;
Tony Mak089c35e2017-12-18 20:34:14 +00001547 case ANIM_OPEN_CROSS_PROFILE_APPS:
1548 service.mWindowManager.overridePendingAppTransitionStartCrossProfileApps();
1549 break;
Jorim Jaggi33a701a2017-12-01 14:58:18 +01001550 case ANIM_REMOTE_ANIMATION:
1551 service.mWindowManager.overridePendingAppTransitionRemote(
1552 pendingOptions.getRemoteAnimationAdapter());
1553 break;
Craig Mautner233ceee2014-05-09 17:05:11 -07001554 default:
1555 Slog.e(TAG, "applyOptionsLocked: Unknown animationType=" + animationType);
1556 break;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001557 }
chaviw82a0ba82018-03-15 14:26:29 -07001558
1559 if (task == null) {
1560 clearOptionsLocked(false /* withAbort */);
1561 } else {
1562 // This will clear the options for all the ActivityRecords for this Task.
1563 task.clearAllPendingOptions();
1564 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001565 }
1566 }
1567
Adam Powellcfbe9be2013-11-06 14:58:58 -08001568 ActivityOptions getOptionsForTargetActivityLocked() {
1569 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1570 }
1571
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001572 void clearOptionsLocked() {
chaviw82a0ba82018-03-15 14:26:29 -07001573 clearOptionsLocked(true /* withAbort */);
1574 }
1575
1576 void clearOptionsLocked(boolean withAbort) {
1577 if (withAbort && pendingOptions != null) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001578 pendingOptions.abort();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001579 }
chaviw82a0ba82018-03-15 14:26:29 -07001580 pendingOptions = null;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001581 }
1582
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001583 ActivityOptions takeOptionsLocked() {
1584 ActivityOptions opts = pendingOptions;
1585 pendingOptions = null;
1586 return opts;
1587 }
1588
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001589 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001590 if (uriPermissions != null) {
1591 uriPermissions.removeUriPermissionsLocked();
1592 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001593 }
1594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595
1596 void pauseKeyDispatchingLocked() {
1597 if (!keysPaused) {
1598 keysPaused = true;
Bryce Lee2b8e0372018-04-05 17:01:37 -07001599
1600 if (mWindowContainerController != null) {
1601 mWindowContainerController.pauseKeyDispatching();
1602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
1604 }
1605
1606 void resumeKeyDispatchingLocked() {
1607 if (keysPaused) {
1608 keysPaused = false;
Bryce Lee2b8e0372018-04-05 17:01:37 -07001609
1610 if (mWindowContainerController != null) {
1611 mWindowContainerController.resumeKeyDispatching();
1612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 }
1614 }
1615
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001616 private void updateTaskDescription(CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001617 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001618 }
1619
Wale Ogunwaleec950642017-04-25 07:44:21 -07001620 void setDeferHidingClient(boolean deferHidingClient) {
1621 if (mDeferHidingClient == deferHidingClient) {
1622 return;
1623 }
1624 mDeferHidingClient = deferHidingClient;
1625 if (!mDeferHidingClient && !visible) {
1626 // Hiding the client is no longer deferred and the app isn't visible still, go ahead and
1627 // update the visibility.
1628 setVisibility(false);
1629 }
Wale Ogunwale89973222017-04-23 18:39:45 -07001630 }
1631
Wale Ogunwaleec950642017-04-25 07:44:21 -07001632 void setVisibility(boolean visible) {
Tim Murray68ed8442017-08-29 23:21:27 +00001633 mWindowContainerController.setVisibility(visible, mDeferHidingClient);
Bryce Lee2a3cc462017-10-27 10:57:35 -07001634 mStackSupervisor.getActivityMetricsLogger().notifyVisibilityChanged(this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001635 }
1636
1637 // TODO: Look into merging with #setVisibility()
Wale Ogunwaleec950642017-04-25 07:44:21 -07001638 void setVisible(boolean newVisible) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001639 visible = newVisible;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001640 mDeferHidingClient = !visible && mDeferHidingClient;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001641 setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001642 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1643 }
1644
Bryce Lee7ace3952018-02-16 14:34:32 -08001645 void setState(ActivityState state, String reason) {
1646 if (DEBUG_STATES) Slog.v(TAG_STATES, "State movement: " + this + " from:" + getState()
1647 + " to:" + state + " reason:" + reason);
Bryce Lee6ff17072018-02-28 07:26:17 -08001648
Bryce Leeb0f993f2018-03-02 15:38:01 -08001649 if (state == mState) {
1650 // No need to do anything if state doesn't change.
1651 if (DEBUG_STATES) Slog.v(TAG_STATES, "State unchanged from:" + state);
1652 return;
1653 }
1654
Bryce Leeb0f993f2018-03-02 15:38:01 -08001655 mState = state;
1656
Bryce Leec4ab62a2018-03-05 14:19:26 -08001657 final TaskRecord parent = getTask();
1658
1659 if (parent != null) {
1660 parent.onActivityStateChanged(this, state, reason);
1661 }
Robert Carr29daa922018-04-27 11:56:48 -07001662
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001663 // The WindowManager interprets the app stopping signal as
1664 // an indication that the Surface will eventually be destroyed.
1665 // This however isn't necessarily true if we are going to sleep.
1666 if (state == STOPPING && !isSleeping()) {
Robert Carr29daa922018-04-27 11:56:48 -07001667 mWindowContainerController.notifyAppStopping();
1668 }
Bryce Lee7ace3952018-02-16 14:34:32 -08001669 }
1670
1671 ActivityState getState() {
1672 return mState;
1673 }
1674
1675 /**
1676 * Returns {@code true} if the Activity is in the specified state.
1677 */
1678 boolean isState(ActivityState state) {
1679 return state == mState;
1680 }
1681
1682 /**
1683 * Returns {@code true} if the Activity is in one of the specified states.
1684 */
1685 boolean isState(ActivityState state1, ActivityState state2) {
1686 return state1 == mState || state2 == mState;
1687 }
1688
1689 /**
1690 * Returns {@code true} if the Activity is in one of the specified states.
1691 */
1692 boolean isState(ActivityState state1, ActivityState state2, ActivityState state3) {
1693 return state1 == mState || state2 == mState || state3 == mState;
1694 }
1695
1696 /**
1697 * Returns {@code true} if the Activity is in one of the specified states.
1698 */
1699 boolean isState(ActivityState state1, ActivityState state2, ActivityState state3,
1700 ActivityState state4) {
1701 return state1 == mState || state2 == mState || state3 == mState || state4 == mState;
1702 }
1703
Jorim Jaggibae01b12017-04-11 16:29:10 -07001704 void notifyAppResumed(boolean wasStopped) {
1705 mWindowContainerController.notifyAppResumed(wasStopped);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001706 }
1707
1708 void notifyUnknownVisibilityLaunched() {
Jorim Jaggi838c2452017-08-28 15:44:43 +02001709
1710 // No display activities never add a window, so there is no point in waiting them for
1711 // relayout.
1712 if (!noDisplay) {
1713 mWindowContainerController.notifyUnknownVisibilityLaunched();
1714 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001715 }
1716
Jorim Jaggi241ae102016-11-02 21:57:33 -07001717 /**
1718 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1719 * might have on the visibility
1720 *
1721 * @see {@link ActivityStack#checkKeyguardVisibility}
1722 */
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001723 boolean shouldBeVisibleIgnoringKeyguard(boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001724 if (!okToShowLocked()) {
1725 return false;
1726 }
1727
Winson Chung3f0e59a2017-10-25 10:19:05 -07001728 return !behindFullscreenActivity || mLaunchTaskBehind;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001729 }
1730
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001731 void makeVisibleIfNeeded(ActivityRecord starting, boolean reportToClient) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001732 // This activity is not currently visible, but is running. Tell it to become visible.
Bryce Lee7ace3952018-02-16 14:34:32 -08001733 if (mState == RESUMED || this == starting) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001734 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
Bryce Lee7ace3952018-02-16 14:34:32 -08001735 "Not making visible, r=" + this + " state=" + mState + " starting=" + starting);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001736 return;
1737 }
1738
1739 // If this activity is paused, tell it to now show its window.
1740 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1741 "Making visible and scheduling visibility: " + this);
1742 final ActivityStack stack = getStack();
1743 try {
1744 if (stack.mTranslucentActivityWaiting != null) {
1745 updateOptionsLocked(returningOptions);
1746 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1747 }
1748 setVisible(true);
1749 sleeping = false;
1750 app.pendingUiClean = true;
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001751 if (reportToClient) {
1752 makeClientVisible();
1753 } else {
1754 mClientVisibilityDeferred = true;
1755 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001756 // The activity may be waiting for stop, but that is no longer appropriate for it.
1757 mStackSupervisor.mStoppingActivities.remove(this);
1758 mStackSupervisor.mGoingToSleepActivities.remove(this);
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001759 } catch (Exception e) {
1760 // Just skip on any failure; we'll make it visible when it next restarts.
1761 Slog.w(TAG, "Exception thrown making visible: " + intent.getComponent(), e);
1762 }
1763 handleAlreadyVisible();
1764 }
Andrii Kulian0d595f32018-02-21 15:47:33 -08001765
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001766 /** Send visibility change message to the client and pause if needed. */
1767 void makeClientVisible() {
1768 mClientVisibilityDeferred = false;
1769 try {
1770 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
1771 WindowVisibilityItem.obtain(true /* showWindow */));
Andrii Kulian09e1afa2018-05-16 21:29:34 -07001772 if (shouldPauseWhenBecomingVisible()) {
Andrii Kulian0d595f32018-02-21 15:47:33 -08001773 // An activity must be in the {@link PAUSING} state for the system to validate
1774 // the move to {@link PAUSED}.
Bryce Lee7ace3952018-02-16 14:34:32 -08001775 setState(PAUSING, "makeVisibleIfNeeded");
Bryce Leeb0f993f2018-03-02 15:38:01 -08001776 service.getLifecycleManager().scheduleTransaction(app.thread, appToken,
Andrii Kulian0d595f32018-02-21 15:47:33 -08001777 PauseActivityItem.obtain(finishing, false /* userLeaving */,
Bryce Lee1d0d5142018-04-12 10:35:07 -07001778 configChangeFlags, false /* dontReport */));
Andrii Kulian0d595f32018-02-21 15:47:33 -08001779 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001780 } catch (Exception e) {
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001781 Slog.w(TAG, "Exception thrown sending visibility update: " + intent.getComponent(), e);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001782 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001783 }
1784
Andrii Kulian09e1afa2018-05-16 21:29:34 -07001785 /** Check if activity should be moved to PAUSED state when it becomes visible. */
1786 private boolean shouldPauseWhenBecomingVisible() {
1787 // If the activity is stopped or stopping, cycle to the paused state. We avoid doing
1788 // this when there is an activity waiting to become translucent as the extra binder
1789 // calls will lead to noticeable jank. A later call to
1790 // ActivityStack#ensureActivitiesVisibleLocked will bring the activity to the proper
1791 // paused state. We also avoid doing this for the activity the stack supervisor
1792 // considers the resumed activity, as normal means will bring the activity from STOPPED
1793 // to RESUMED. Adding PAUSING in this scenario will lead to double lifecycles.
1794 if (!isState(STOPPED, STOPPING) || getStack().mTranslucentActivityWaiting != null
1795 || mStackSupervisor.getResumedActivityLocked() == this) {
1796 return false;
1797 }
1798
1799 // Check if position in task allows to become paused
1800 final int positionInTask = task.mActivities.indexOf(this);
1801 if (positionInTask == -1) {
1802 throw new IllegalStateException("Activity not found in its task");
1803 }
1804 if (positionInTask == task.mActivities.size() - 1) {
1805 // It's the topmost activity in the task - should become paused now
1806 return true;
1807 }
1808 // Check if activity above is finishing now and this one becomes the topmost in task.
1809 final ActivityRecord activityAbove = task.mActivities.get(positionInTask + 1);
1810 if (activityAbove.finishing && results == null) {
1811 // We will only allow pausing if activity above wasn't launched for result. Otherwise it
1812 // will cause this activity to resume before getting result.
1813 return true;
1814 }
1815 return false;
1816 }
1817
Andrii Kulian21713ac2016-10-12 22:05:05 -07001818 boolean handleAlreadyVisible() {
1819 stopFreezingScreenLocked(false);
1820 try {
1821 if (returningOptions != null) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001822 app.thread.scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001823 }
1824 } catch(RemoteException e) {
1825 }
Bryce Lee7ace3952018-02-16 14:34:32 -08001826 return mState == RESUMED;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001827 }
1828
1829 static void activityResumedLocked(IBinder token) {
1830 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1831 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1832 if (r != null) {
1833 r.icicle = null;
1834 r.haveState = false;
1835 }
1836 }
1837
1838 /**
1839 * Once we know that we have asked an application to put an activity in the resumed state
1840 * (either by launching it or explicitly telling it), this function updates the rest of our
1841 * state to match that fact.
1842 */
1843 void completeResumeLocked() {
1844 final boolean wasVisible = visible;
Jorim Jaggi8d062052017-08-22 14:55:17 +02001845 setVisible(true);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001846 if (!wasVisible) {
1847 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1848 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1849 }
1850 idle = false;
1851 results = null;
1852 newIntents = null;
1853 stopped = false;
1854
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001855 if (isActivityTypeHome()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001856 ProcessRecord app = task.mActivities.get(0).app;
1857 if (app != null && app != service.mHomeProcess) {
1858 service.mHomeProcess = app;
1859 }
1860 }
1861
1862 if (nowVisible) {
1863 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1864 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001865 }
1866
1867 // Schedule an idle timeout in case the app doesn't do it for us.
1868 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1869
1870 mStackSupervisor.reportResumedActivityLocked(this);
1871
1872 resumeKeyDispatchingLocked();
1873 final ActivityStack stack = getStack();
Jorim Jaggifa9ed962018-01-25 00:16:49 +01001874 mStackSupervisor.mNoAnimActivities.clear();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001875
1876 // Mark the point when the activity is resuming
1877 // TODO: To be more accurate, the mark should be before the onCreate,
1878 // not after the onResume. But for subsequent starts, onResume is fine.
1879 if (app != null) {
1880 cpuTimeAtResume = service.mProcessCpuTracker.getCpuTimeForPid(app.pid);
1881 } else {
1882 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1883 }
1884
1885 returningOptions = null;
chaviw59b98852017-06-13 12:05:44 -07001886
1887 if (canTurnScreenOn()) {
1888 mStackSupervisor.wakeUp("turnScreenOnFlag");
1889 } else {
1890 // If the screen is going to turn on because the caller explicitly requested it and
1891 // the keyguard is not showing don't attempt to sleep. Otherwise the Activity will
1892 // pause and then resume again later, which will result in a double life-cycle event.
David Stevens9440dc82017-03-16 19:00:20 -07001893 stack.checkReadyForSleep();
chaviw59b98852017-06-13 12:05:44 -07001894 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001895 }
1896
1897 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1898 CharSequence description) {
1899 final ActivityStack stack = getStack();
Bryce Lee7ace3952018-02-16 14:34:32 -08001900 if (mState != STOPPING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001901 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
Ruben Brunkf53497c2017-03-27 20:26:17 -07001902 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001903 return;
1904 }
1905 if (newPersistentState != null) {
1906 persistentState = newPersistentState;
1907 service.notifyTaskPersisterLocked(task, false);
1908 }
1909 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001910
Andrii Kulian21713ac2016-10-12 22:05:05 -07001911 if (newIcicle != null) {
1912 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1913 // the state.
1914 icicle = newIcicle;
1915 haveState = true;
1916 launchCount = 0;
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001917 updateTaskDescription(description);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001918 }
1919 if (!stopped) {
1920 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
Ruben Brunkf53497c2017-03-27 20:26:17 -07001921 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001922 stopped = true;
Bryce Lee7ace3952018-02-16 14:34:32 -08001923 setState(STOPPED, "activityStoppedLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001924
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001925 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001926
Andrii Kulian21713ac2016-10-12 22:05:05 -07001927 if (finishing) {
1928 clearOptionsLocked();
1929 } else {
1930 if (deferRelaunchUntilPaused) {
1931 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
1932 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1933 } else {
1934 mStackSupervisor.updatePreviousProcessLocked(this);
1935 }
1936 }
1937 }
1938 }
1939
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001940 void startLaunchTickingLocked() {
Jeff Sharkey5ab02432017-06-27 11:01:36 -06001941 if (Build.IS_USER) {
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001942 return;
1943 }
1944 if (launchTickTime == 0) {
1945 launchTickTime = SystemClock.uptimeMillis();
1946 continueLaunchTickingLocked();
1947 }
1948 }
1949
1950 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001951 if (launchTickTime == 0) {
1952 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001953 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001954
Andrii Kulian02b7a832016-10-06 23:11:56 -07001955 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001956 if (stack == null) {
1957 return false;
1958 }
1959
Ruben Brunkf53497c2017-03-27 20:26:17 -07001960 Message msg = stack.mHandler.obtainMessage(LAUNCH_TICK_MSG, this);
1961 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
1962 stack.mHandler.sendMessageDelayed(msg, LAUNCH_TICK);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001963 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001964 }
1965
1966 void finishLaunchTickingLocked() {
1967 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001968 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001969 if (stack != null) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001970 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001971 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001972 }
1973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 // IApplicationToken
1975
1976 public boolean mayFreezeScreenLocked(ProcessRecord app) {
1977 // Only freeze the screen if this activity is currently attached to
1978 // an application, and that application is not blocked or unresponding.
1979 // In any other case, we can't count on getting the screen unfrozen,
1980 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -08001981 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
1985 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001986 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 }
1988 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 public void stopFreezingScreenLocked(boolean force) {
1991 if (force || frozenBeforeDestroy) {
1992 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001993 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
1995 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001996
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001997 public void reportFullyDrawnLocked(boolean restoredFromBundle) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001998 final long curTime = SystemClock.uptimeMillis();
1999 if (displayStartTime != 0) {
2000 reportLaunchTimeLocked(curTime);
2001 }
Jorim Jaggide8305a2018-05-15 12:06:35 -07002002 final LaunchTimeTracker.Entry entry = mStackSupervisor.getLaunchTimeTracker().getEntry(
2003 getWindowingMode());
2004 if (fullyDrawnStartTime != 0 && entry != null) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002005 final long thisTime = curTime - fullyDrawnStartTime;
Jorim Jaggide8305a2018-05-15 12:06:35 -07002006 final long totalTime = entry.mFullyDrawnStartTime != 0
2007 ? (curTime - entry.mFullyDrawnStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07002008 if (SHOW_ACTIVITY_START_TIME) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002009 Trace.asyncTraceEnd(TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
2010 EventLog.writeEvent(AM_ACTIVITY_FULLY_DRAWN_TIME,
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002011 userId, System.identityHashCode(this), shortComponentName,
2012 thisTime, totalTime);
2013 StringBuilder sb = service.mStringBuilder;
2014 sb.setLength(0);
2015 sb.append("Fully drawn ");
2016 sb.append(shortComponentName);
2017 sb.append(": ");
2018 TimeUtils.formatDuration(thisTime, sb);
2019 if (thisTime != totalTime) {
2020 sb.append(" (total ");
2021 TimeUtils.formatDuration(totalTime, sb);
2022 sb.append(")");
2023 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002024 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002025 }
2026 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07002027 //service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002028 }
Jorim Jaggide8305a2018-05-15 12:06:35 -07002029 entry.mFullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002030 }
Bryce Lee2a3cc462017-10-27 10:57:35 -07002031 mStackSupervisor.getActivityMetricsLogger().logAppTransitionReportedDrawn(this,
Jorim Jaggi4d27b842017-08-17 17:22:26 +02002032 restoredFromBundle);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002033 fullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002034 }
2035
2036 private void reportLaunchTimeLocked(final long curTime) {
Jorim Jaggide8305a2018-05-15 12:06:35 -07002037 final LaunchTimeTracker.Entry entry = mStackSupervisor.getLaunchTimeTracker().getEntry(
2038 getWindowingMode());
2039 if (entry == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002040 return;
2041 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002042 final long thisTime = curTime - displayStartTime;
Jorim Jaggide8305a2018-05-15 12:06:35 -07002043 final long totalTime = entry.mLaunchStartTime != 0
2044 ? (curTime - entry.mLaunchStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07002045 if (SHOW_ACTIVITY_START_TIME) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002046 Trace.asyncTraceEnd(TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
2047 EventLog.writeEvent(AM_ACTIVITY_LAUNCH_TIME,
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002048 userId, System.identityHashCode(this), shortComponentName,
2049 thisTime, totalTime);
2050 StringBuilder sb = service.mStringBuilder;
2051 sb.setLength(0);
2052 sb.append("Displayed ");
2053 sb.append(shortComponentName);
2054 sb.append(": ");
2055 TimeUtils.formatDuration(thisTime, sb);
2056 if (thisTime != totalTime) {
2057 sb.append(" (total ");
2058 TimeUtils.formatDuration(totalTime, sb);
2059 sb.append(")");
2060 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002061 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002062 }
2063 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
2064 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07002065 //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002066 }
2067 displayStartTime = 0;
Jorim Jaggide8305a2018-05-15 12:06:35 -07002068 entry.mLaunchStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07002069 }
2070
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002071 @Override
Sudheer Shankac766db02017-06-12 10:37:29 -07002072 public void onStartingWindowDrawn(long timestamp) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08002073 synchronized (service) {
Bryce Lee2a3cc462017-10-27 10:57:35 -07002074 mStackSupervisor.getActivityMetricsLogger().notifyStartingWindowDrawn(
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +01002075 getWindowingMode(), timestamp);
Jorim Jaggi3878ca32017-02-02 17:13:05 -08002076 }
2077 }
2078
2079 @Override
Sudheer Shankac766db02017-06-12 10:37:29 -07002080 public void onWindowsDrawn(long timestamp) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002081 synchronized (service) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +01002082 mStackSupervisor.getActivityMetricsLogger().notifyWindowsDrawn(getWindowingMode(),
2083 timestamp);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002084 if (displayStartTime != 0) {
Sudheer Shankac766db02017-06-12 10:37:29 -07002085 reportLaunchTimeLocked(timestamp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002087 mStackSupervisor.sendWaitingVisibleReportLocked(this);
2088 startTime = 0;
2089 finishLaunchTickingLocked();
2090 if (task != null) {
2091 task.hasBeenVisible = true;
2092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
2094 }
2095
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002096 @Override
2097 public void onWindowsVisible() {
2098 synchronized (service) {
2099 mStackSupervisor.reportActivityVisibleLocked(this);
2100 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
2101 if (!nowVisible) {
2102 nowVisible = true;
2103 lastVisibleTime = SystemClock.uptimeMillis();
Bryce Leeb7c9b802017-05-02 14:20:24 -07002104 if (idle || mStackSupervisor.isStoppingNoHistoryActivity()) {
2105 // If this activity was already idle or there is an activity that must be
2106 // stopped immediately after visible, then we now need to make sure we perform
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002107 // the full stop of any activities that are waiting to do so. This is because
2108 // we won't do that while they are still waiting for this one to become visible.
Bryce Lee4a194382017-04-04 14:32:48 -07002109 final int size = mStackSupervisor.mActivitiesWaitingForVisibleActivity.size();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002110 if (size > 0) {
2111 for (int i = 0; i < size; i++) {
Bryce Lee4a194382017-04-04 14:32:48 -07002112 final ActivityRecord r =
2113 mStackSupervisor.mActivitiesWaitingForVisibleActivity.get(i);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002114 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
2115 }
Bryce Lee4a194382017-04-04 14:32:48 -07002116 mStackSupervisor.mActivitiesWaitingForVisibleActivity.clear();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002117 mStackSupervisor.scheduleIdleLocked();
2118 }
Bryce Leeb7c9b802017-05-02 14:20:24 -07002119 } else {
2120 // Instead of doing the full stop routine here, let's just hide any activities
2121 // we now can, and let them stop when the normal idle happens.
2122 mStackSupervisor.processStoppingActivitiesLocked(null /* idleActivity */,
2123 false /* remove */, true /* processPausingActivities */);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002124 }
2125 service.scheduleAppGcsLocked();
2126 }
2127 }
2128 }
2129
2130 @Override
2131 public void onWindowsGone() {
2132 synchronized (service) {
2133 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
2134 nowVisible = false;
2135 }
2136 }
2137
2138 @Override
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002139 public boolean keyDispatchingTimedOut(String reason, int windowPid) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002140 ActivityRecord anrActivity;
2141 ProcessRecord anrApp;
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002142 boolean windowFromSameProcessAsActivity;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002143 synchronized (service) {
2144 anrActivity = getWaitingHistoryRecordLocked();
2145 anrApp = app;
Brian Carlstrom7b0f2e82017-03-31 00:24:18 -07002146 windowFromSameProcessAsActivity =
2147 app == null || app.pid == windowPid || windowPid == -1;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002148 }
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002149 if (windowFromSameProcessAsActivity) {
2150 return service.inputDispatchingTimedOut(anrApp, anrActivity, this, false, reason);
2151 } else {
2152 // In this case another process added windows using this activity token. So, we call the
2153 // generic service input dispatch timed out method so that the right process is blamed.
2154 return service.inputDispatchingTimedOut(windowPid, false /* aboveSystem */, reason) < 0;
2155 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002156 }
2157
2158 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08002159 // First find the real culprit... if this activity is waiting for
2160 // another activity to start or has stopped, then the key dispatching
2161 // timeout should not be caused by this.
Bryce Lee4a194382017-04-04 14:32:48 -07002162 if (mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this) || stopped) {
Craig Mautner8f2adcb2014-04-07 22:21:33 +00002163 final ActivityStack stack = mStackSupervisor.getFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08002164 // Try to use the one which is closest to top.
Bryce Leec4ab62a2018-03-05 14:19:26 -08002165 ActivityRecord r = stack.getResumedActivity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002167 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08002169 if (r != null) {
2170 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
2172 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08002173 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 }
2175
Chong Zhang87761972016-08-22 13:53:24 -07002176 /** Checks whether the activity should be shown for current user. */
2177 public boolean okToShowLocked() {
Bryce Lee8558ec72017-08-17 15:37:26 -07002178 // We cannot show activities when the device is locked and the application is not
2179 // encryption aware.
2180 if (!StorageManager.isUserKeyUnlocked(userId)
2181 && !info.applicationInfo.isEncryptionAware()) {
2182 return false;
2183 }
2184
Chong Zhang87761972016-08-22 13:53:24 -07002185 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
2186 || (mStackSupervisor.isCurrentProfileLocked(userId)
Fyodor Kupolov1b3edac2017-09-19 15:48:06 -07002187 && service.mUserController.isUserRunning(userId, 0 /* flags */));
Chong Zhang87761972016-08-22 13:53:24 -07002188 }
2189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 /**
2191 * This method will return true if the activity is either visible, is becoming visible, is
2192 * currently pausing, or is resumed.
2193 */
2194 public boolean isInterestingToUserLocked() {
Bryce Lee7ace3952018-02-16 14:34:32 -08002195 return visible || nowVisible || mState == PAUSING || mState == RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002196 }
2197
Wale Ogunwale3e997362016-09-06 10:37:56 -07002198 void setSleeping(boolean _sleeping) {
2199 setSleeping(_sleeping, false);
2200 }
2201
2202 void setSleeping(boolean _sleeping, boolean force) {
2203 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002204 return;
2205 }
2206 if (app != null && app.thread != null) {
2207 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002208 app.thread.scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002209 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
2210 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002211 }
2212 sleeping = _sleeping;
2213 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002214 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002215 }
2216 }
2217 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08002218
Craig Mautnerd2328952013-03-05 12:46:26 -08002219 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002220 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08002221 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002222 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08002223 }
2224 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002225 final int activityNdx = task.mActivities.indexOf(r);
2226 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002227 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08002228 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002229 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08002230 }
2231
2232 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002233 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002234 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08002235 }
2236
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002237 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002238 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
2239 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002240 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08002241 }
2242 return null;
2243 }
2244
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002245 /**
2246 * @return display id to which this record is attached, -1 if not attached.
2247 */
2248 int getDisplayId() {
2249 final ActivityStack stack = getStack();
2250 if (stack == null) {
2251 return -1;
2252 }
2253 return stack.mDisplayId;
2254 }
2255
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002256 final boolean isDestroyable() {
Bryce Lee7ace3952018-02-16 14:34:32 -08002257 if (finishing || app == null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002258 // This would be redundant.
2259 return false;
2260 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002261 final ActivityStack stack = getStack();
Bryce Leec4ab62a2018-03-05 14:19:26 -08002262 if (stack == null || this == stack.getResumedActivity() || this == stack.mPausingActivity
Andrii Kulian02b7a832016-10-06 23:11:56 -07002263 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002264 // We're not ready for this kind of thing.
2265 return false;
2266 }
2267 if (visible) {
2268 // The user would notice this!
2269 return false;
2270 }
2271 return true;
2272 }
2273
Winson Chung3bad5cc02014-08-19 17:44:32 -07002274 private static String createImageFilename(long createTime, int taskId) {
2275 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Ruben Brunkf53497c2017-03-27 20:26:17 -07002276 IMAGE_EXTENSION;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002277 }
2278
Craig Mautner648f69b2014-09-18 14:16:26 -07002279 void setTaskDescription(TaskDescription _taskDescription) {
2280 Bitmap icon;
2281 if (_taskDescription.getIconFilename() == null &&
2282 (icon = _taskDescription.getIcon()) != null) {
2283 final String iconFilename = createImageFilename(createTime, task.taskId);
Winson Chungc8408b82017-01-25 17:58:56 -08002284 final File iconFile = new File(TaskPersister.getUserImagesDir(task.userId),
2285 iconFilename);
Suprabh Shukla23593142015-11-03 17:31:15 -08002286 final String iconFilePath = iconFile.getAbsolutePath();
Winson Chung3f0e59a2017-10-25 10:19:05 -07002287 service.getRecentTasks().saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08002288 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07002289 }
2290 taskDescription = _taskDescription;
2291 }
2292
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002293 void setVoiceSessionLocked(IVoiceInteractionSession session) {
2294 voiceSession = session;
2295 pendingVoiceInteractionStart = false;
2296 }
2297
2298 void clearVoiceSessionLocked() {
2299 voiceSession = null;
2300 pendingVoiceInteractionStart = false;
2301 }
2302
Jorim Jaggi02886a82016-12-06 09:10:06 -08002303 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch) {
Jorim Jaggi42befc62017-06-13 11:54:04 -07002304 showStartingWindow(prev, newTask, taskSwitch, false /* fromRecents */);
2305 }
2306
2307 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch,
2308 boolean fromRecents) {
Jorim Jaggi70176432017-01-18 12:52:13 +01002309 if (mWindowContainerController == null) {
2310 return;
2311 }
Wale Ogunwale19866e22017-04-19 06:05:13 -07002312 if (mTaskOverlay) {
2313 // We don't show starting window for overlay activities.
2314 return;
2315 }
2316
Wale Ogunwale3b232392016-05-13 15:37:13 -07002317 final CompatibilityInfo compatInfo =
2318 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002319 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
2320 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
Jorim Jaggibae01b12017-04-11 16:29:10 -07002321 prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning(),
Jorim Jaggi70aa4d12017-05-15 00:05:54 +02002322 allowTaskSnapshot(),
Bryce Lee7ace3952018-02-16 14:34:32 -08002323 mState.ordinal() >= RESUMED.ordinal() && mState.ordinal() <= STOPPED.ordinal(),
Jorim Jaggi42befc62017-06-13 11:54:04 -07002324 fromRecents);
Wale Ogunwale3b232392016-05-13 15:37:13 -07002325 if (shown) {
2326 mStartingWindowState = STARTING_WINDOW_SHOWN;
2327 }
2328 }
2329
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002330 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
Jorim Jaggicb956052017-05-09 16:27:24 +02002331 if (mStartingWindowState == STARTING_WINDOW_SHOWN && behindFullscreenActivity) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002332 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
2333 mStartingWindowState = STARTING_WINDOW_REMOVED;
Jorim Jaggi19be6052017-08-03 18:33:43 +02002334 mWindowContainerController.removeStartingWindow();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002335 }
2336 }
2337
2338 int getRequestedOrientation() {
2339 return mWindowContainerController.getOrientation();
2340 }
2341
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002342 void setRequestedOrientation(int requestedOrientation) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002343 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002344 final Configuration displayConfig =
2345 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
2346
2347 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
2348 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002349 if (config != null) {
2350 frozenBeforeDestroy = true;
2351 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
2352 false /* deferResume */, displayId)) {
2353 mStackSupervisor.resumeFocusedStackTopActivityLocked();
2354 }
2355 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07002356 service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
2357 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002358 }
2359
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01002360 void setDisablePreviewScreenshots(boolean disable) {
2361 mWindowContainerController.setDisablePreviewScreenshots(disable);
2362 }
2363
Bryce Leea163b762017-01-24 11:05:01 -08002364 /**
2365 * Set the last reported global configuration to the client. Should be called whenever a new
2366 * global configuration is sent to the client for this activity.
2367 */
2368 void setLastReportedGlobalConfiguration(@NonNull Configuration config) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002369 mLastReportedConfiguration.setGlobalConfiguration(config);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002370 }
2371
Bryce Leea163b762017-01-24 11:05:01 -08002372 /**
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002373 * Set the last reported configuration to the client. Should be called whenever
Bryce Leea163b762017-01-24 11:05:01 -08002374 * a new merged configuration is sent to the client for this activity.
2375 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002376 void setLastReportedConfiguration(@NonNull MergedConfiguration config) {
Bryce Lee8104e7a2017-08-17 09:16:03 -07002377 setLastReportedConfiguration(config.getGlobalConfiguration(),
2378 config.getOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08002379 }
2380
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002381 private void setLastReportedConfiguration(Configuration global, Configuration override) {
Bryce Lee8104e7a2017-08-17 09:16:03 -07002382 mLastReportedConfiguration.setConfiguration(global, override);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002383 }
2384
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002385 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002386 private void updateOverrideConfiguration() {
2387 mTmpConfig.unset();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002388 computeBounds(mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08002389
2390 if (mTmpBounds.equals(getOverrideBounds())) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002391 return;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002392 }
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002393
Bryce Leef3c6a472017-11-14 14:53:06 -08002394 setBounds(mTmpBounds);
2395
2396 final Rect updatedBounds = getOverrideBounds();
2397
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002398 // Bounds changed...update configuration to match.
Bryce Leef3c6a472017-11-14 14:53:06 -08002399 if (!matchParentBounds()) {
2400 task.computeOverrideConfiguration(mTmpConfig, updatedBounds, null /* insetBounds */,
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002401 false /* overrideWidth */, false /* overrideHeight */);
2402 }
Bryce Leef3c6a472017-11-14 14:53:06 -08002403
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002404 onOverrideConfigurationChanged(mTmpConfig);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002405 }
2406
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002407 /** Returns true if the configuration is compatible with this activity. */
Wale Ogunwale42f07d92017-05-01 21:32:58 -07002408 boolean isConfigurationCompatible(Configuration config) {
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002409 final int orientation = mWindowContainerController != null
2410 ? mWindowContainerController.getOrientation() : info.screenOrientation;
2411 if (isFixedOrientationPortrait(orientation)
2412 && config.orientation != ORIENTATION_PORTRAIT) {
2413 return false;
2414 }
2415 if (isFixedOrientationLandscape(orientation)
2416 && config.orientation != ORIENTATION_LANDSCAPE) {
2417 return false;
2418 }
2419 return true;
2420 }
2421
Bryce Lee7566d762017-03-30 09:34:15 -07002422 /**
2423 * Computes the bounds to fit the Activity within the bounds of the {@link Configuration}.
2424 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002425 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
2426 private void computeBounds(Rect outBounds) {
2427 outBounds.setEmpty();
2428 final float maxAspectRatio = info.maxAspectRatio;
2429 final ActivityStack stack = getStack();
Bryce Leef3c6a472017-11-14 14:53:06 -08002430 if (task == null || stack == null || task.inMultiWindowMode() || maxAspectRatio == 0
Bryce Leee5ab4502017-07-11 08:58:05 -07002431 || isInVrUiMode(getConfiguration())) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002432 // We don't set override configuration if that activity task isn't fullscreen. I.e. the
2433 // activity is in multi-window mode. Or, there isn't a max aspect ratio specified for
Bryce Leee5ab4502017-07-11 08:58:05 -07002434 // the activity. This is indicated by an empty {@link outBounds}. We also don't set it
2435 // if we are in VR mode.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002436 return;
2437 }
2438
Bryce Lee7566d762017-03-30 09:34:15 -07002439 // We must base this on the parent configuration, because we set our override
2440 // configuration's appBounds based on the result of this method. If we used our own
2441 // configuration, it would be influenced by past invocations.
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002442 final Rect appBounds = getParent().getWindowConfiguration().getAppBounds();
Wale Ogunwale822e5122017-07-26 06:02:24 -07002443 final int containingAppWidth = appBounds.width();
2444 final int containingAppHeight = appBounds.height();
Bryce Lee7566d762017-03-30 09:34:15 -07002445 int maxActivityWidth = containingAppWidth;
2446 int maxActivityHeight = containingAppHeight;
2447
2448 if (containingAppWidth < containingAppHeight) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002449 // Width is the shorter side, so we use that to figure-out what the max. height
2450 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002451 maxActivityHeight = (int) ((maxActivityWidth * maxAspectRatio) + 0.5f);
2452 } else {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002453 // Height is the shorter side, so we use that to figure-out what the max. width
2454 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002455 maxActivityWidth = (int) ((maxActivityHeight * maxAspectRatio) + 0.5f);
2456 }
2457
Bryce Lee7566d762017-03-30 09:34:15 -07002458 if (containingAppWidth <= maxActivityWidth && containingAppHeight <= maxActivityHeight) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002459 // The display matches or is less than the activity aspect ratio, so nothing else to do.
Bryce Lee7566d762017-03-30 09:34:15 -07002460 // Return the existing bounds. If this method is running for the first time,
Bryce Leef3c6a472017-11-14 14:53:06 -08002461 // {@link #getOverrideBounds()} will be empty (representing no override). If the method has run
2462 // before, then effect of {@link #getOverrideBounds()} will already have been applied to the
Bryce Lee7566d762017-03-30 09:34:15 -07002463 // value returned from {@link getConfiguration}. Refer to
2464 // {@link TaskRecord#computeOverrideConfiguration}.
Bryce Leef3c6a472017-11-14 14:53:06 -08002465 outBounds.set(getOverrideBounds());
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002466 return;
2467 }
2468
2469 // Compute configuration based on max supported width and height.
Adrian Roos24be34d2018-05-28 18:55:38 +02002470 // Also account for the left / top insets (e.g. from display cutouts), which will be clipped
2471 // away later in StackWindowController.adjustConfigurationForBounds(). Otherwise, the app
2472 // bounds would end up too small.
2473 outBounds.set(0, 0, maxActivityWidth + appBounds.left, maxActivityHeight + appBounds.top);
2474
2475 if (service.mWindowManager.getNavBarPosition() == NAV_BAR_LEFT) {
2476 // Position the activity frame on the opposite side of the nav bar.
2477 outBounds.left = appBounds.right - maxActivityWidth;
2478 outBounds.right = appBounds.right;
2479 }
Andrii Kulian3a1619d2017-07-07 14:38:09 -07002480 }
2481
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002482 boolean ensureActivityConfiguration(int globalChanges, boolean preserveWindow) {
2483 return ensureActivityConfiguration(globalChanges, preserveWindow,
2484 false /* ignoreStopState */);
2485 }
2486
Andrii Kulian21713ac2016-10-12 22:05:05 -07002487 /**
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002488 * Make sure the given activity matches the current configuration. Ensures the HistoryRecord
2489 * is updated with the correct configuration and all other bookkeeping is handled.
2490 *
2491 * @param globalChanges The changes to the global configuration.
2492 * @param preserveWindow If the activity window should be preserved on screen if the activity
2493 * is relaunched.
2494 * @param ignoreStopState If we should try to relaunch the activity even if it is in the stopped
2495 * state. This is useful for the case where we know the activity will be
2496 * visible soon and we want to ensure its configuration before we make it
2497 * visible.
2498 * @return True if the activity was relaunched and false if it wasn't relaunched because we
2499 * can't or the app handles the specific configuration that is changing.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002500 */
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002501 boolean ensureActivityConfiguration(int globalChanges, boolean preserveWindow,
2502 boolean ignoreStopState) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002503 final ActivityStack stack = getStack();
2504 if (stack.mConfigWillChange) {
2505 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2506 "Skipping config check (will change): " + this);
2507 return true;
2508 }
2509
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002510 // We don't worry about activities that are finishing.
2511 if (finishing) {
2512 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2513 "Configuration doesn't matter in finishing " + this);
2514 stopFreezingScreenLocked(false);
2515 return true;
2516 }
2517
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002518 if (!ignoreStopState && (mState == STOPPING || mState == STOPPED)) {
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002519 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2520 "Skipping config check stopped or stopping: " + this);
2521 return true;
2522 }
2523
Wale Ogunwaleea3d3fd2017-05-01 07:41:08 -07002524 // TODO: We should add ActivityRecord.shouldBeVisible() that checks if the activity should
2525 // be visible based on the stack, task, and lockscreen state and use that here instead. The
2526 // method should be based on the logic in ActivityStack.ensureActivitiesVisibleLocked().
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002527 // Skip updating configuration for activity is a stack that shouldn't be visible.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002528 if (!stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002529 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2530 "Skipping config check invisible stack: " + this);
2531 return true;
2532 }
2533
Andrii Kulian21713ac2016-10-12 22:05:05 -07002534 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2535 "Ensuring correct configuration: " + this);
2536
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002537 final int newDisplayId = getDisplayId();
2538 final boolean displayChanged = mLastReportedDisplayId != newDisplayId;
2539 if (displayChanged) {
2540 mLastReportedDisplayId = newDisplayId;
2541 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002542 // TODO(b/36505427): Is there a better place to do this?
2543 updateOverrideConfiguration();
2544
Winson Chungbdc646f2017-02-13 12:12:22 -08002545 // Short circuit: if the two full configurations are equal (the common case), then there is
2546 // nothing to do. We test the full configuration instead of the global and merged override
2547 // configurations because there are cases (like moving a task to the pinned stack) where
2548 // the combine configurations are equal, but would otherwise differ in the override config
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002549 mTmpConfig.setTo(mLastReportedConfiguration.getMergedConfiguration());
2550 if (getConfiguration().equals(mTmpConfig) && !forceNewConfig && !displayChanged) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002551 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002552 "Configuration & display unchanged in " + this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002553 return true;
2554 }
2555
2556 // Okay we now are going to make this activity have the new config.
2557 // But then we need to figure out how it needs to deal with that.
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002558
2559 // Find changes between last reported merged configuration and the current one. This is used
2560 // to decide whether to relaunch an activity or just report a configuration change.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002561 final int changes = getConfigurationChanges(mTmpConfig);
Ruben Brunkf64af332017-03-22 22:03:25 -07002562
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002563 // Update last reported values.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002564 final Configuration newMergedOverrideConfig = getMergedOverrideConfiguration();
Bryce Lee8104e7a2017-08-17 09:16:03 -07002565
2566 setLastReportedConfiguration(service.getGlobalConfiguration(), newMergedOverrideConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002567
Bryce Lee7ace3952018-02-16 14:34:32 -08002568 if (mState == INITIALIZING) {
Andrii Kulianb372da62018-01-18 10:46:24 -08002569 // No need to relaunch or schedule new config for activity that hasn't been launched
2570 // yet. We do, however, return after applying the config to activity record, so that
2571 // it will use it for launch transaction.
2572 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2573 "Skipping config check for initializing activity: " + this);
2574 return true;
2575 }
2576
Andrii Kulian21713ac2016-10-12 22:05:05 -07002577 if (changes == 0 && !forceNewConfig) {
2578 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2579 "Configuration no differences in " + this);
2580 // There are no significant differences, so we won't relaunch but should still deliver
2581 // the new configuration to the client process.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002582 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002583 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002584 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002585 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002586 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002587 return true;
2588 }
2589
2590 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002591 "Configuration changes for " + this + ", allChanges="
Andrii Kulian21713ac2016-10-12 22:05:05 -07002592 + Configuration.configurationDiffToString(changes));
2593
2594 // If the activity isn't currently running, just leave the new configuration and it will
2595 // pick that up next time it starts.
2596 if (app == null || app.thread == null) {
2597 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2598 "Configuration doesn't matter not running " + this);
2599 stopFreezingScreenLocked(false);
2600 forceNewConfig = false;
2601 return true;
2602 }
2603
2604 // Figure out how to handle the changes between the configurations.
2605 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2606 "Checking to restart " + info.name + ": changed=0x"
2607 + Integer.toHexString(changes) + ", handles=0x"
2608 + Integer.toHexString(info.getRealConfigChanged())
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002609 + ", mLastReportedConfiguration=" + mLastReportedConfiguration);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002610
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002611 if (shouldRelaunchLocked(changes, mTmpConfig) || forceNewConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002612 // Aha, the activity isn't handling the change, so DIE DIE DIE.
2613 configChangeFlags |= changes;
2614 startFreezingScreenLocked(app, globalChanges);
2615 forceNewConfig = false;
2616 preserveWindow &= isResizeOnlyChange(changes);
2617 if (app == null || app.thread == null) {
2618 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2619 "Config is destroying non-running " + this);
2620 stack.destroyActivityLocked(this, true, "config");
Bryce Lee7ace3952018-02-16 14:34:32 -08002621 } else if (mState == PAUSING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002622 // A little annoying: we are waiting for this activity to finish pausing. Let's not
2623 // do anything now, but just flag that it needs to be restarted when done pausing.
2624 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2625 "Config is skipping already pausing " + this);
2626 deferRelaunchUntilPaused = true;
2627 preserveWindowOnDeferredRelaunch = preserveWindow;
2628 return true;
Bryce Lee7ace3952018-02-16 14:34:32 -08002629 } else if (mState == RESUMED) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002630 // Try to optimize this case: the configuration is changing and we need to restart
2631 // the top, resumed activity. Instead of doing the normal handshaking, just say
2632 // "restart!".
2633 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2634 "Config is relaunching resumed " + this);
2635
2636 if (DEBUG_STATES && !visible) {
2637 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
2638 + " called by " + Debug.getCallers(4));
2639 }
2640
2641 relaunchActivityLocked(true /* andResume */, preserveWindow);
2642 } else {
2643 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2644 "Config is relaunching non-resumed " + this);
2645 relaunchActivityLocked(false /* andResume */, preserveWindow);
2646 }
2647
2648 // All done... tell the caller we weren't able to keep this activity around.
2649 return false;
2650 }
2651
2652 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002653 // NOTE: We only forward the override configuration as the system level configuration
Andrii Kulian21713ac2016-10-12 22:05:05 -07002654 // changes is always sent to all processes when they happen so it can just use whatever
2655 // system level configuration it last got.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002656 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002657 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002658 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002659 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002660 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002661 stopFreezingScreenLocked(false);
2662
2663 return true;
2664 }
2665
Zak Cohen90e7116742017-01-29 12:59:23 -08002666 /**
2667 * When assessing a configuration change, decide if the changes flags and the new configurations
2668 * should cause the Activity to relaunch.
Ruben Brunkf64af332017-03-22 22:03:25 -07002669 *
2670 * @param changes the changes due to the given configuration.
2671 * @param changesConfig the configuration that was used to calculate the given changes via a
2672 * call to getConfigurationChanges.
Zak Cohen90e7116742017-01-29 12:59:23 -08002673 */
Ruben Brunkf64af332017-03-22 22:03:25 -07002674 private boolean shouldRelaunchLocked(int changes, Configuration changesConfig) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002675 int configChanged = info.getRealConfigChanged();
Ruben Brunkf64af332017-03-22 22:03:25 -07002676 boolean onlyVrUiModeChanged = onlyVrUiModeChanged(changes, changesConfig);
Zak Cohen90e7116742017-01-29 12:59:23 -08002677
2678 // Override for apps targeting pre-O sdks
2679 // If a device is in VR mode, and we're transitioning into VR ui mode, add ignore ui mode
2680 // to the config change.
2681 // For O and later, apps will be required to add configChanges="uimode" to their manifest.
2682 if (appInfo.targetSdkVersion < O
2683 && requestedVrComponent != null
Ruben Brunkf64af332017-03-22 22:03:25 -07002684 && onlyVrUiModeChanged) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002685 configChanged |= CONFIG_UI_MODE;
2686 }
2687
2688 return (changes&(~configChanged)) != 0;
2689 }
2690
Ruben Brunkf64af332017-03-22 22:03:25 -07002691 /**
2692 * Returns true if the configuration change is solely due to the UI mode switching into or out
2693 * of UI_MODE_TYPE_VR_HEADSET.
2694 */
2695 private boolean onlyVrUiModeChanged(int changes, Configuration lastReportedConfig) {
2696 final Configuration currentConfig = getConfiguration();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002697 return changes == CONFIG_UI_MODE && (isInVrUiMode(currentConfig)
Ruben Brunkf64af332017-03-22 22:03:25 -07002698 != isInVrUiMode(lastReportedConfig));
2699 }
2700
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002701 private int getConfigurationChanges(Configuration lastReportedConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002702 // Determine what has changed. May be nothing, if this is a config that has come back from
2703 // the app after going idle. In that case we just want to leave the official config object
2704 // now in the activity and do nothing else.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002705 final Configuration currentConfig = getConfiguration();
2706 int changes = lastReportedConfig.diff(currentConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002707 // We don't want to use size changes if they don't cross boundaries that are important to
2708 // the app.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002709 if ((changes & CONFIG_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002710 final boolean crosses = crossesHorizontalSizeThreshold(lastReportedConfig.screenWidthDp,
2711 currentConfig.screenWidthDp)
2712 || crossesVerticalSizeThreshold(lastReportedConfig.screenHeightDp,
2713 currentConfig.screenHeightDp);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002714 if (!crosses) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002715 changes &= ~CONFIG_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002716 }
2717 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002718 if ((changes & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002719 final int oldSmallest = lastReportedConfig.smallestScreenWidthDp;
2720 final int newSmallest = currentConfig.smallestScreenWidthDp;
2721 if (!crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002722 changes &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002723 }
2724 }
Wale Ogunwale822e5122017-07-26 06:02:24 -07002725 // We don't want window configuration to cause relaunches.
2726 if ((changes & CONFIG_WINDOW_CONFIGURATION) != 0) {
2727 changes &= ~CONFIG_WINDOW_CONFIGURATION;
2728 }
Bryce Lee600dadd2017-07-25 10:48:42 -07002729
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002730 return changes;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002731 }
2732
2733 private static boolean isResizeOnlyChange(int change) {
2734 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2735 | CONFIG_SCREEN_LAYOUT)) == 0;
2736 }
2737
2738 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
2739 if (service.mSuppressResizeConfigChanges && preserveWindow) {
2740 configChangeFlags = 0;
2741 return;
2742 }
2743
2744 List<ResultInfo> pendingResults = null;
2745 List<ReferrerIntent> pendingNewIntents = null;
2746 if (andResume) {
2747 pendingResults = results;
2748 pendingNewIntents = newIntents;
2749 }
2750 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2751 "Relaunching: " + this + " with results=" + pendingResults
2752 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2753 + " preserveWindow=" + preserveWindow);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002754 EventLog.writeEvent(andResume ? AM_RELAUNCH_RESUME_ACTIVITY
2755 : AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
Andrii Kulian21713ac2016-10-12 22:05:05 -07002756 task.taskId, shortComponentName);
2757
2758 startFreezingScreenLocked(app, 0);
2759
Andrii Kulian21713ac2016-10-12 22:05:05 -07002760 try {
2761 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2762 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2763 + " callers=" + Debug.getCallers(6));
2764 forceNewConfig = false;
2765 mStackSupervisor.activityRelaunchingLocked(this);
Andrii Kulianb372da62018-01-18 10:46:24 -08002766 final ClientTransactionItem callbackItem = ActivityRelaunchItem.obtain(pendingResults,
2767 pendingNewIntents, configChangeFlags,
2768 new MergedConfiguration(service.getGlobalConfiguration(),
2769 getMergedOverrideConfiguration()),
2770 preserveWindow);
2771 final ActivityLifecycleItem lifecycleItem;
2772 if (andResume) {
2773 lifecycleItem = ResumeActivityItem.obtain(service.isNextTransitionForward());
2774 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -07002775 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulianb372da62018-01-18 10:46:24 -08002776 }
2777 final ClientTransaction transaction = ClientTransaction.obtain(app.thread, appToken);
2778 transaction.addCallback(callbackItem);
2779 transaction.setLifecycleStateRequest(lifecycleItem);
Bryce Leeb0f993f2018-03-02 15:38:01 -08002780 service.getLifecycleManager().scheduleTransaction(transaction);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002781 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
Andrii Kulianb372da62018-01-18 10:46:24 -08002782 // request resume if this activity is currently resumed, which implies we aren't
Andrii Kulian21713ac2016-10-12 22:05:05 -07002783 // sleeping.
2784 } catch (RemoteException e) {
2785 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2786 }
2787
2788 if (andResume) {
2789 if (DEBUG_STATES) {
2790 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2791 }
2792 results = null;
2793 newIntents = null;
Alan Viveretteb6a25732017-11-21 14:49:24 -05002794 service.getAppWarningsLocked().onResumeActivity(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002795 service.showAskCompatModeDialogLocked(this);
2796 } else {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002797 service.mHandler.removeMessages(PAUSE_TIMEOUT_MSG, this);
Bryce Lee7ace3952018-02-16 14:34:32 -08002798 setState(PAUSED, "relaunchActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07002799 }
2800
2801 configChangeFlags = 0;
2802 deferRelaunchUntilPaused = false;
2803 preserveWindowOnDeferredRelaunch = false;
2804 }
2805
Jorim Jaggibae01b12017-04-11 16:29:10 -07002806 private boolean isProcessRunning() {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002807 ProcessRecord proc = app;
2808 if (proc == null) {
2809 proc = service.mProcessNames.get(processName, info.applicationInfo.uid);
2810 }
2811 return proc != null && proc.thread != null;
2812 }
2813
Jorim Jaggibae01b12017-04-11 16:29:10 -07002814 /**
2815 * @return Whether a task snapshot starting window may be shown.
2816 */
2817 private boolean allowTaskSnapshot() {
2818 if (newIntents == null) {
2819 return true;
2820 }
2821
2822 // Restrict task snapshot starting window to launcher start, or there is no intent at all
2823 // (eg. task being brought to front). If the intent is something else, likely the app is
2824 // going to show some specific page or view, instead of what's left last time.
2825 for (int i = newIntents.size() - 1; i >= 0; i--) {
2826 final Intent intent = newIntents.get(i);
2827 if (intent != null && !ActivityRecord.isMainIntent(intent)) {
2828 return false;
2829 }
2830 }
2831 return true;
2832 }
2833
Bryce Leeb7c9b802017-05-02 14:20:24 -07002834 /**
2835 * Returns {@code true} if the associated activity has the no history flag set on it.
2836 * {@code false} otherwise.
2837 */
2838 boolean isNoHistory() {
2839 return (intent.getFlags() & FLAG_ACTIVITY_NO_HISTORY) != 0
2840 || (info.flags & FLAG_NO_HISTORY) != 0;
2841 }
2842
Craig Mautner21d24a22014-04-23 11:45:37 -07002843 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2844 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2845 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2846 if (launchedFromPackage != null) {
2847 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2848 }
2849 if (resolvedType != null) {
2850 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2851 }
2852 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2853 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002854
Craig Mautner21d24a22014-04-23 11:45:37 -07002855 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002856 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002857 }
2858
2859 out.startTag(null, TAG_INTENT);
2860 intent.saveToXml(out);
2861 out.endTag(null, TAG_INTENT);
2862
2863 if (isPersistable() && persistentState != null) {
2864 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2865 persistentState.saveToXml(out);
2866 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2867 }
2868 }
2869
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002870 static ActivityRecord restoreFromXml(XmlPullParser in,
2871 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002872 Intent intent = null;
2873 PersistableBundle persistentState = null;
2874 int launchedFromUid = 0;
2875 String launchedFromPackage = null;
2876 String resolvedType = null;
2877 boolean componentSpecified = false;
2878 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002879 long createTime = -1;
2880 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002881 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002882
2883 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2884 final String attrName = in.getAttributeName(attrNdx);
2885 final String attrValue = in.getAttributeValue(attrNdx);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002886 if (DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002887 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002888 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002889 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002890 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002891 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002892 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2893 launchedFromPackage = attrValue;
2894 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2895 resolvedType = attrValue;
2896 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002897 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002898 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002899 userId = Integer.parseInt(attrValue);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002900 } else if (attrName.startsWith(ATTR_TASKDESCRIPTION_PREFIX)) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002901 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002902 } else {
2903 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2904 }
2905 }
2906
2907 int event;
Ruben Brunkf53497c2017-03-27 20:26:17 -07002908 while (((event = in.next()) != END_DOCUMENT) &&
2909 (event != END_TAG || in.getDepth() >= outerDepth)) {
2910 if (event == START_TAG) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002911 final String name = in.getName();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002912 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002913 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002914 if (TAG_INTENT.equals(name)) {
2915 intent = Intent.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002916 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002917 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002918 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2919 persistentState = PersistableBundle.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002920 if (DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002921 "ActivityRecord: persistentState=" + persistentState);
2922 } else {
2923 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2924 XmlUtils.skipCurrentTag(in);
2925 }
2926 }
2927 }
2928
2929 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002930 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002931 }
2932
2933 final ActivityManagerService service = stackSupervisor.mService;
2934 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Patrick Baumann78380272018-04-04 10:41:01 -07002935 userId, Binder.getCallingUid());
Craig Mautnere0129b32014-05-25 16:41:09 -07002936 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002937 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2938 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002939 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002940 final ActivityRecord r = new ActivityRecord(service, null /* caller */,
2941 0 /* launchedFromPid */, launchedFromUid, launchedFromPackage, intent, resolvedType,
2942 aInfo, service.getConfiguration(), null /* resultTo */, null /* resultWho */,
2943 0 /* reqCode */, componentSpecified, false /* rootVoiceInteraction */,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002944 stackSupervisor, null /* options */, null /* sourceRecord */);
Craig Mautner21d24a22014-04-23 11:45:37 -07002945
2946 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002947 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002948 r.createTime = createTime;
2949
2950 return r;
2951 }
2952
Zak Cohen90e7116742017-01-29 12:59:23 -08002953 private static boolean isInVrUiMode(Configuration config) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002954 return (config.uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_VR_HEADSET;
Zak Cohen90e7116742017-01-29 12:59:23 -08002955 }
2956
David Stevens82ea6cb2017-03-03 16:18:50 -08002957 int getUid() {
2958 return info.applicationInfo.uid;
2959 }
2960
chaviw59b98852017-06-13 12:05:44 -07002961 void setShowWhenLocked(boolean showWhenLocked) {
2962 mShowWhenLocked = showWhenLocked;
Kevin Chyn44639482017-10-09 18:34:41 -07002963 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0 /* configChanges */,
2964 false /* preserveWindows */);
chaviw59b98852017-06-13 12:05:44 -07002965 }
2966
2967 /**
chaviw2c500982018-01-04 17:05:05 -08002968 * @return true if the activity windowing mode is not
2969 * {@link android.app.WindowConfiguration#WINDOWING_MODE_PINNED} and activity contains
2970 * windows that have {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set or if the activity
2971 * has set {@link #mShowWhenLocked}.
2972 * Multi-windowing mode will be exited if true is returned.
chaviw59b98852017-06-13 12:05:44 -07002973 */
2974 boolean canShowWhenLocked() {
chaviw2c500982018-01-04 17:05:05 -08002975 return !inPinnedWindowingMode() && (mShowWhenLocked
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002976 || service.mWindowManager.containsShowWhenLockedWindow(appToken));
chaviw59b98852017-06-13 12:05:44 -07002977 }
2978
2979 void setTurnScreenOn(boolean turnScreenOn) {
2980 mTurnScreenOn = turnScreenOn;
2981 }
2982
2983 /**
2984 * Determines whether this ActivityRecord can turn the screen on. It checks whether the flag
2985 * {@link #mTurnScreenOn} is set and checks whether the ActivityRecord should be visible
2986 * depending on Keyguard state
2987 *
2988 * @return true if the screen can be turned on, false otherwise.
2989 */
2990 boolean canTurnScreenOn() {
2991 final ActivityStack stack = getStack();
2992 return mTurnScreenOn && stack != null &&
2993 stack.checkKeyguardVisibility(this, true /* shouldBeVisible */, true /* isTop */);
2994 }
2995
2996 boolean getTurnScreenOnFlag() {
2997 return mTurnScreenOn;
2998 }
2999
3000 boolean isTopRunningActivity() {
3001 return mStackSupervisor.topRunningActivityLocked() == this;
3002 }
3003
Jorim Jaggif84e2f62018-01-16 14:17:59 +01003004 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
3005 mWindowContainerController.registerRemoteAnimations(definition);
3006 }
3007
Craig Mautnerf81b90872013-02-26 13:02:43 -08003008 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07003010 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08003011 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07003012 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07003013 }
3014 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08003015 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07003016 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003017 sb.append(" u");
3018 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07003019 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003020 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08003021 stringName = sb.toString();
3022 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07003024
3025 void writeIdentifierToProto(ProtoOutputStream proto, long fieldId) {
3026 final long token = proto.start(fieldId);
3027 proto.write(HASH_CODE, System.identityHashCode(this));
3028 proto.write(USER_ID, userId);
3029 proto.write(TITLE, intent.getComponent().flattenToShortString());
3030 proto.end(token);
3031 }
3032
3033 public void writeToProto(ProtoOutputStream proto, long fieldId) {
3034 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02003035 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003036 writeIdentifierToProto(proto, IDENTIFIER);
Bryce Lee7ace3952018-02-16 14:34:32 -08003037 proto.write(STATE, mState.toString());
Steven Timotius4346f0a2017-09-12 11:07:21 -07003038 proto.write(VISIBLE, visible);
3039 proto.write(FRONT_OF_TASK, frontOfTask);
3040 if (app != null) {
3041 proto.write(PROC_ID, app.pid);
3042 }
Wale Ogunwale30eab1f2018-05-24 18:25:25 -07003043 proto.write(TRANSLUCENT, !fullscreen);
Steven Timotius4346f0a2017-09-12 11:07:21 -07003044 proto.end(token);
3045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046}