blob: 5e92b9e4d46ada6c6c789e0c096ef5b3ccdf9f8c [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
Wale Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Winson Chungbb348802017-01-30 12:01:45 -080019import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
Ruben Brunkf53497c2017-03-27 20:26:17 -070020import static android.app.ActivityManager.TaskDescription.ATTR_TASKDESCRIPTION_PREFIX;
Ruben Brunkf53497c2017-03-27 20:26:17 -070021import static android.app.ActivityOptions.ANIM_SCENE_TRANSITION;
Riddle Hsu16567132018-08-16 21:37:47 +080022import static android.app.ActivityTaskManager.INVALID_STACK_ID;
Vishnu Nair9ba31652018-11-13 14:34:05 -080023import static android.app.ActivityTaskManager.INVALID_TASK_ID;
Winson Chung59fda9e2017-01-20 16:14:51 -080024import static android.app.AppOpsManager.MODE_ALLOWED;
Winson Chungf4ac0632017-03-17 12:34:12 -070025import static android.app.AppOpsManager.OP_PICTURE_IN_PICTURE;
Vishnu Nair132ee832018-09-28 15:00:05 -070026import static android.app.WaitResult.INVALID_DELAY;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070027import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
28import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
29import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070030import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
31import static android.app.WindowConfiguration.activityTypeToString;
Ruben Brunkf53497c2017-03-27 20:26:17 -070032import static android.content.Intent.ACTION_MAIN;
33import static android.content.Intent.CATEGORY_HOME;
34import static android.content.Intent.CATEGORY_LAUNCHER;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080035import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Bryce Leeb7c9b802017-05-02 14:20:24 -070036import static android.content.Intent.FLAG_ACTIVITY_NO_HISTORY;
Andrii Kulian21713ac2016-10-12 22:05:05 -070037import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
38import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
39import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
40import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Zak Cohen90e7116742017-01-29 12:59:23 -080041import static android.content.pm.ActivityInfo.CONFIG_UI_MODE;
Wale Ogunwale822e5122017-07-26 06:02:24 -070042import static android.content.pm.ActivityInfo.CONFIG_WINDOW_CONFIGURATION;
Jorim Jaggi02886a82016-12-06 09:10:06 -080043import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080044import static android.content.pm.ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
45import static android.content.pm.ActivityInfo.FLAG_IMMERSIVE;
46import static android.content.pm.ActivityInfo.FLAG_MULTIPROCESS;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020047import static android.content.pm.ActivityInfo.FLAG_NO_HISTORY;
Chong Zhang87761972016-08-22 13:53:24 -070048import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020049import static android.content.pm.ActivityInfo.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080050import static android.content.pm.ActivityInfo.FLAG_STATE_NOT_NEEDED;
chaviw59b98852017-06-13 12:05:44 -070051import static android.content.pm.ActivityInfo.FLAG_TURN_SCREEN_ON;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080052import static android.content.pm.ActivityInfo.LAUNCH_MULTIPLE;
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -080053import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
54import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080055import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Charles He2bf28322017-10-12 22:24:49 +010056import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
57import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
58import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
59import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
Ruben Brunkf53497c2017-03-27 20:26:17 -070060import static android.content.pm.ActivityInfo.PERSIST_ACROSS_REBOOTS;
61import static android.content.pm.ActivityInfo.PERSIST_ROOT_ONLY;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070062import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggicd13d332016-04-27 15:40:20 -070063import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwale72a73e32016-10-13 12:16:39 -070064import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070065import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -070066import static android.content.pm.ActivityInfo.isFixedOrientationLandscape;
67import static android.content.pm.ActivityInfo.isFixedOrientationPortrait;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -070068import static android.content.res.Configuration.EMPTY;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -070069import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
70import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Ruben Brunkf53497c2017-03-27 20:26:17 -070071import static android.content.res.Configuration.UI_MODE_TYPE_MASK;
Zak Cohen90e7116742017-01-29 12:59:23 -080072import static android.content.res.Configuration.UI_MODE_TYPE_VR_HEADSET;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080073import static android.os.Build.VERSION_CODES.HONEYCOMB;
Zak Cohen90e7116742017-01-29 12:59:23 -080074import static android.os.Build.VERSION_CODES.O;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080075import static android.os.Process.SYSTEM_UID;
Riddle Hsufd4a0502018-10-16 01:05:16 +080076import static android.view.Display.INVALID_DISPLAY;
Riddle Hsu16567132018-08-16 21:37:47 +080077import static android.view.WindowManagerPolicyConstants.NAV_BAR_LEFT;
78
Vishnu Nair9ba31652018-11-13 14:34:05 -080079import static com.android.server.am.ActivityRecordProto.CONFIGURATION_CONTAINER;
80import static com.android.server.am.ActivityRecordProto.FRONT_OF_TASK;
81import static com.android.server.am.ActivityRecordProto.IDENTIFIER;
82import static com.android.server.am.ActivityRecordProto.PROC_ID;
83import static com.android.server.am.ActivityRecordProto.STATE;
84import static com.android.server.am.ActivityRecordProto.TRANSLUCENT;
85import static com.android.server.am.ActivityRecordProto.VISIBLE;
86import static com.android.server.am.EventLogTags.AM_RELAUNCH_ACTIVITY;
87import static com.android.server.am.EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY;
88import static com.android.server.wm.ActivityStack.ActivityState.INITIALIZING;
89import static com.android.server.wm.ActivityStack.ActivityState.PAUSED;
90import static com.android.server.wm.ActivityStack.ActivityState.PAUSING;
91import static com.android.server.wm.ActivityStack.ActivityState.RESUMED;
92import static com.android.server.wm.ActivityStack.ActivityState.STOPPED;
93import static com.android.server.wm.ActivityStack.ActivityState.STOPPING;
94import static com.android.server.wm.ActivityStack.LAUNCH_TICK;
95import static com.android.server.wm.ActivityStack.LAUNCH_TICK_MSG;
96import static com.android.server.wm.ActivityStack.PAUSE_TIMEOUT_MSG;
97import static com.android.server.wm.ActivityStack.STOP_TIMEOUT_MSG;
Wale Ogunwale59507092018-10-29 09:00:30 -070098import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CONFIGURATION;
99import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_FOCUS;
100import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SAVED_STATE;
101import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STATES;
102import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
103import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_VISIBILITY;
104import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CONFIGURATION;
105import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_FOCUS;
106import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_SAVED_STATE;
107import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_STATES;
108import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_SWITCH;
109import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_VISIBILITY;
110import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
111import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale59507092018-10-29 09:00:30 -0700112import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_FREE_RESIZE;
113import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_NONE;
114import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_WINDOWING_MODE_RESIZE;
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700115import static com.android.server.wm.IdentifierProto.HASH_CODE;
116import static com.android.server.wm.IdentifierProto.TITLE;
117import static com.android.server.wm.IdentifierProto.USER_ID;
Vishnu Nair9ba31652018-11-13 14:34:05 -0800118import static com.android.server.wm.TaskPersister.DEBUG;
119import static com.android.server.wm.TaskPersister.IMAGE_EXTENSION;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700120
Ruben Brunkf53497c2017-03-27 20:26:17 -0700121import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
122import static org.xmlpull.v1.XmlPullParser.END_TAG;
123import static org.xmlpull.v1.XmlPullParser.START_TAG;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800124
Andrii Kulian21713ac2016-10-12 22:05:05 -0700125import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -0700126import android.app.ActivityManager.TaskDescription;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700127import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800128import android.app.PendingIntent;
Winson Chung709904f2017-04-25 11:00:48 -0700129import android.app.PictureInPictureParams;
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800130import android.app.ResultInfo;
Riddle Hsu16567132018-08-16 21:37:47 +0800131import android.app.servertransaction.ActivityConfigurationChangeItem;
Andrii Kulianb372da62018-01-18 10:46:24 -0800132import android.app.servertransaction.ActivityLifecycleItem;
133import android.app.servertransaction.ActivityRelaunchItem;
134import android.app.servertransaction.ClientTransaction;
135import android.app.servertransaction.ClientTransactionItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700136import android.app.servertransaction.MoveToDisplayItem;
137import android.app.servertransaction.MultiWindowModeChangeItem;
138import android.app.servertransaction.NewIntentItem;
Bryce Lee0bd8d422018-01-09 09:45:57 -0800139import android.app.servertransaction.PauseActivityItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700140import android.app.servertransaction.PipModeChangeItem;
Andrii Kulianb372da62018-01-18 10:46:24 -0800141import android.app.servertransaction.ResumeActivityItem;
Andrii Kulian446e8242017-10-26 15:17:29 -0700142import android.app.servertransaction.WindowVisibilityItem;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143import android.content.ComponentName;
144import android.content.Intent;
145import android.content.pm.ActivityInfo;
146import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700147import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148import android.content.res.Configuration;
149import android.graphics.Bitmap;
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700150import android.graphics.Rect;
Patrick Baumann78380272018-04-04 10:41:01 -0700151import android.os.Binder;
Bryce Lee39791592017-04-26 09:29:12 -0700152import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700154import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800155import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800157import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700159import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700161import android.os.UserHandle;
Bryce Lee8558ec72017-08-17 15:37:26 -0700162import android.os.storage.StorageManager;
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800163import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164import android.util.EventLog;
165import android.util.Log;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700166import android.util.MergedConfiguration;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700168import android.util.TimeUtils;
Steven Timotius4346f0a2017-09-12 11:07:21 -0700169import android.util.proto.ProtoOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170import android.view.IApplicationToken;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100171import android.view.RemoteAnimationDefinition;
Jorim Jaggife762342016-10-13 14:33:27 +0200172import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700173
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700174import com.android.internal.R;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800175import com.android.internal.app.ResolverActivity;
176import com.android.internal.content.ReferrerIntent;
177import com.android.internal.util.XmlUtils;
178import com.android.server.AttributeCache;
Ruben Brunkf53497c2017-03-27 20:26:17 -0700179import com.android.server.AttributeCache.Entry;
Wale Ogunwale59507092018-10-29 09:00:30 -0700180import com.android.server.am.AppTimeTracker;
181import com.android.server.am.PendingIntentRecord;
Vishnu Nair9ba31652018-11-13 14:34:05 -0800182import com.android.server.uri.UriPermissionOwner;
Wale Ogunwale59507092018-10-29 09:00:30 -0700183import com.android.server.wm.ActivityMetricsLogger.WindowingModeTransitionInfoSnapshot;
184import com.android.server.wm.ActivityStack.ActivityState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
Jorim Jaggi02886a82016-12-06 09:10:06 -0800186import org.xmlpull.v1.XmlPullParser;
187import org.xmlpull.v1.XmlPullParserException;
188import org.xmlpull.v1.XmlSerializer;
189
Suprabh Shukla23593142015-11-03 17:31:15 -0800190import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -0700191import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192import java.io.PrintWriter;
193import java.lang.ref.WeakReference;
194import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800195import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700197import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700198import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
200/**
201 * An entry in the history stack, representing an activity.
202 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700203final class ActivityRecord extends ConfigurationContainer implements AppWindowContainerListener {
Wale Ogunwale98875612018-10-12 07:53:02 -0700204 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_ATM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700205 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
206 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700207 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700208 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700209 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Louis Chang19443452018-10-09 12:10:21 +0800210 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Winson Chung16e185e2017-11-07 08:30:54 -0800211 // TODO(b/67864419): Remove once recents component is overridden
212 private static final String LEGACY_RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800213
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700214 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700215
Craig Mautner21d24a22014-04-23 11:45:37 -0700216 private static final String ATTR_ID = "id";
217 private static final String TAG_INTENT = "intent";
218 private static final String ATTR_USERID = "user_id";
219 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
220 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700221 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700222 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
223 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700224 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700225
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700226 final ActivityTaskManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800227 final IApplicationToken.Stub appToken; // window manager token
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800228 AppWindowContainerController mWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 final ActivityInfo info; // all about me
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -0700230 // TODO: This is duplicated state already contained in info.applicationInfo - remove
231 ApplicationInfo appInfo; // information about activity's app
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800232 final int launchedFromPid; // always the pid who started the activity.
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700233 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800234 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700235 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final Intent intent; // the original intent that generated us
237 final ComponentName realActivity; // the intent component, or target of an alias.
238 final String shortComponentName; // the short component name of the intent
239 final String resolvedType; // as per original caller;
240 final String packageName; // the package implementing intent's component
241 final String processName; // process where this component wants to run
242 final String taskAffinity; // as per ActivityInfo.taskAffinity
243 final boolean stateNotNeeded; // As per ActivityInfo.flags
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700244 boolean fullscreen; // The activity is opaque and fills the entire space of this task.
245 // TODO: See if it possible to combine this with the fullscreen field.
246 final boolean hasWallpaper; // Has a wallpaper window as a background.
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800247 final boolean noDisplay; // activity is not displayed?
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800248 private final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700249 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700250
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800251 private CharSequence nonLocalizedLabel; // the label information from the package mgr.
252 private int labelRes; // the label information from the package mgr.
253 private int icon; // resource identifier of activity's icon.
254 private int logo; // resource identifier of activity's logo.
255 private int theme; // resource identifier of activity's theme.
256 private int realTheme; // actual theme resource we will use, never 0.
257 private int windowFlags; // custom window flags for preview window.
Bryce Leeaf691c02017-03-20 14:20:22 -0700258 private TaskRecord task; // the task this is in.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800259 private long createTime = System.currentTimeMillis();
Dianne Hackborn50685602011-12-01 12:23:37 -0800260 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700261 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700262 long pauseTime; // last time we started pausing the activity
263 long launchTickTime; // base time for launch tick messages
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700264 // Last configuration reported to the activity in the client process.
265 private MergedConfiguration mLastReportedConfiguration;
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800266 private int mLastReportedDisplayId;
Winson Chung609e1e92017-05-08 10:52:12 -0700267 private boolean mLastReportedMultiWindowMode;
268 private boolean mLastReportedPictureInPictureMode;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700269 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700270 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 final String resultWho; // additional identifier for use by resultTo.
272 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800273 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800275 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700276 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700277 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700278 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
Wale Ogunwalec4e63a42018-10-02 13:19:54 -0700279 ActivityServiceConnectionsHolder mServiceConnectionsHolder; // Service connections.
Dianne Hackborn7e269642010-08-25 19:50:20 -0700280 UriPermissionOwner uriPermissions; // current special URI access perms.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700281 WindowProcessController app; // if non-null, hosting application
Bryce Lee7ace3952018-02-16 14:34:32 -0800282 private ActivityState mState; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700284 PersistableBundle persistentState; // last persistently saved activity state
Wale Ogunwale66e16852017-10-19 13:35:52 -0700285 // TODO: See if this is still needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 boolean frontOfTask; // is this the root activity of its task?
287 boolean launchFailed; // set if a launched failed, to abort on 2nd try
288 boolean haveState; // have we gotten the last activity state?
289 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700290 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800292 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
293 // completed
294 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 int configChangeFlags; // which config values have changed
Wale Ogunwaleec950642017-04-25 07:44:21 -0700296 private boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 int launchMode; // the launch mode activity attribute.
Charles He2bf28322017-10-12 22:24:49 +0100298 int lockTaskLaunchMode; // the lockTaskMode manifest attribute, subject to override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700300 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
301 // might hide this activity?
Wale Ogunwaleec950642017-04-25 07:44:21 -0700302 private boolean mDeferHidingClient; // If true we told WM to defer reporting to the client
303 // process that it is hidden.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800304 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 boolean nowVisible; // is this activity's window visible?
Vishnu Nair9ba31652018-11-13 14:34:05 -0800306 boolean mDrawn; // is this activity's window drawn?
Andrii Kuliana39ae3e2018-05-31 12:43:54 -0700307 boolean mClientVisibilityDeferred;// was the visibility change message to client deferred?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 boolean idle; // has the activity gone idle?
309 boolean hasBeenLaunched;// has this activity ever been launched?
310 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400311 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400312 boolean forceNewConfig; // force re-create with new config next time
Winson Chungf7e03e12017-08-22 11:32:16 -0700313 boolean supportsEnterPipOnTaskSwitch; // This flag is set by the system to indicate that the
314 // activity can enter picture in picture while pausing (only when switching to another task)
Winson Chung709904f2017-04-25 11:00:48 -0700315 PictureInPictureParams pictureInPictureArgs = new PictureInPictureParams.Builder().build();
316 // The PiP params used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800317 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800318 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800319 ComponentName requestedVrComponent; // the requested component for handling VR mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700321 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700322
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700323 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700324 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800325
326 static final int STARTING_WINDOW_NOT_SHOWN = 0;
327 static final int STARTING_WINDOW_SHOWN = 1;
328 static final int STARTING_WINDOW_REMOVED = 2;
329 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700330 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800331
Garfield Tan2746ab52018-07-25 12:33:01 -0700332 // Marking the reason why this activity is being relaunched. Mainly used to track that this
333 // activity is being relaunched to fulfill a resize request due to compatibility issues, e.g. in
334 // pre-NYC apps that don't have a sense of being resized.
335 int mRelaunchReason = RELAUNCH_REASON_NONE;
336
Craig Mautner21d24a22014-04-23 11:45:37 -0700337 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700338 boolean mLaunchTaskBehind; // this activity is actively being launched with
339 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700340
Filip Gruszczynski23493322015-07-29 17:02:59 -0700341 // These configurations are collected from application's resources based on size-sensitive
342 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
343 // and drawable-sw400dp will be added to both as 400.
344 private int[] mVerticalSizeConfigurations;
345 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700346 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700347
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800348 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
349 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
350
Robert Carrd2265122016-08-05 10:25:21 -0700351 // A hint to override the window specified rotation animation, or -1
352 // to use the window specified value. We use this so that
353 // we can select the right animation in the cases of starting
354 // windows, where the app hasn't had time to set a value
355 // on the window.
356 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700357
chaviw59b98852017-06-13 12:05:44 -0700358 private boolean mShowWhenLocked;
359 private boolean mTurnScreenOn;
360
Andrii Kulian21713ac2016-10-12 22:05:05 -0700361 /**
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -0800362 * Temp configs used in {@link #ensureActivityConfiguration(int, boolean)}
Andrii Kulian21713ac2016-10-12 22:05:05 -0700363 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700364 private final Configuration mTmpConfig = new Configuration();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700365 private final Rect mTmpBounds = new Rect();
Andrii Kulian21713ac2016-10-12 22:05:05 -0700366
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800367 private static String startingWindowStateToString(int state) {
368 switch (state) {
369 case STARTING_WINDOW_NOT_SHOWN:
370 return "STARTING_WINDOW_NOT_SHOWN";
371 case STARTING_WINDOW_SHOWN:
372 return "STARTING_WINDOW_SHOWN";
373 case STARTING_WINDOW_REMOVED:
374 return "STARTING_WINDOW_REMOVED";
375 default:
376 return "unknown state=" + state;
377 }
378 }
379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700381 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700382 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
383 pw.print(" processName="); pw.println(processName);
384 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700385 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800386 pw.print(" userId="); pw.println(userId);
387 pw.print(prefix); pw.print("app="); pw.println(app);
388 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700389 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
390 pw.print(" task="); pw.println(task);
391 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700392 pw.print(prefix); pw.print("realActivity=");
393 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700394 if (appInfo != null) {
395 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
396 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
397 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
398 }
399 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800400 if (appInfo.splitSourceDirs != null) {
401 pw.print(prefix); pw.print("splitDir=");
402 pw.println(Arrays.toString(appInfo.splitSourceDirs));
403 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800404 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700405 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
406 pw.print(" componentSpecified="); pw.print(componentSpecified);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700407 pw.print(" mActivityType="); pw.println(
408 activityTypeToString(getActivityType()));
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700409 if (rootVoiceInteraction) {
410 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
411 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800412 pw.print(prefix); pw.print("compat="); pw.print(compat);
413 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
414 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
415 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700416 pw.println(prefix + "mLastReportedConfigurations:");
417 mLastReportedConfiguration.dump(pw, prefix + " ");
418
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700419 pw.print(prefix); pw.print("CurrentConfiguration="); pw.println(getConfiguration());
420 if (!getOverrideConfiguration().equals(EMPTY)) {
421 pw.println(prefix + "OverrideConfiguration=" + getOverrideConfiguration());
422 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800423 if (!matchParentBounds()) {
424 pw.println(prefix + "bounds=" + getBounds());
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700425 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700426 if (resultTo != null || resultWho != null) {
427 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
428 pw.print(" resultWho="); pw.print(resultWho);
429 pw.print(" resultCode="); pw.println(requestCode);
430 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700431 if (taskDescription != null) {
432 final String iconFilename = taskDescription.getIconFilename();
433 if (iconFilename != null || taskDescription.getLabel() != null ||
434 taskDescription.getPrimaryColor() != 0) {
435 pw.print(prefix); pw.print("taskDescription:");
Craig Mautner29c58ca2014-10-14 16:17:06 -0700436 pw.print(" label=\""); pw.print(taskDescription.getLabel());
437 pw.print("\"");
Matthew Ng54bc9422017-10-02 17:16:28 -0700438 pw.print(" icon="); pw.print(taskDescription.getInMemoryIcon() != null
439 ? taskDescription.getInMemoryIcon().getByteCount() + " bytes"
440 : "null");
441 pw.print(" iconResource="); pw.print(taskDescription.getIconResource());
442 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200443 pw.print(" primaryColor=");
Craig Mautner29c58ca2014-10-14 16:17:06 -0700444 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700445 pw.print(prefix + " backgroundColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200446 pw.println(Integer.toHexString(taskDescription.getBackgroundColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700447 pw.print(prefix + " statusBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200448 pw.println(Integer.toHexString(taskDescription.getStatusBarColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700449 pw.print(prefix + " navigationBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200450 pw.println(Integer.toHexString(taskDescription.getNavigationBarColor()));
Craig Mautner29c58ca2014-10-14 16:17:06 -0700451 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700452 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700453 if (results != null) {
454 pw.print(prefix); pw.print("results="); pw.println(results);
455 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700456 if (pendingResults != null && pendingResults.size() > 0) {
457 pw.print(prefix); pw.println("Pending Results:");
458 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
459 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
460 pw.print(prefix); pw.print(" - ");
461 if (pir == null) {
462 pw.println("null");
463 } else {
464 pw.println(pir);
465 pir.dump(pw, prefix + " ");
466 }
467 }
468 }
469 if (newIntents != null && newIntents.size() > 0) {
470 pw.print(prefix); pw.println("Pending New Intents:");
471 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800472 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700473 pw.print(prefix); pw.print(" - ");
474 if (intent == null) {
475 pw.println("null");
476 } else {
477 pw.println(intent.toShortString(false, true, false, true));
478 }
479 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700480 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700481 if (pendingOptions != null) {
482 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
483 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700484 if (appTimeTracker != null) {
485 appTimeTracker.dumpWithHeader(pw, prefix, false);
486 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700487 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700488 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700489 }
490 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800491 pw.print(" launchCount="); pw.print(launchCount);
492 pw.print(" lastLaunchTime=");
493 if (lastLaunchTime == 0) pw.print("0");
494 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
495 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700496 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700497 pw.print(" icicle="); pw.println(icicle);
Bryce Lee7ace3952018-02-16 14:34:32 -0800498 pw.print(prefix); pw.print("state="); pw.print(mState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700499 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700500 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700501 pw.print(" finishing="); pw.println(finishing);
502 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
503 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700504 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800505 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800506 pw.print(" idle="); pw.print(idle);
507 pw.print(" mStartingWindowState=");
508 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800509 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
510 pw.print(" noDisplay="); pw.print(noDisplay);
511 pw.print(" immersive="); pw.print(immersive);
512 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800513 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400514 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700515 pw.print(prefix); pw.print("mActivityType=");
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700516 pw.println(activityTypeToString(getActivityType()));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800517 if (requestedVrComponent != null) {
518 pw.print(prefix);
519 pw.print("requestedVrComponent=");
520 pw.println(requestedVrComponent);
521 }
Bryce Lee4a194382017-04-04 14:32:48 -0700522 final boolean waitingVisible =
523 mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800524 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700525 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800526 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700527 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700528 if (lastVisibleTime == 0) pw.print("0");
529 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
530 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700531 }
Wale Ogunwaleec950642017-04-25 07:44:21 -0700532 if (mDeferHidingClient) {
533 pw.println(prefix + "mDeferHidingClient=" + mDeferHidingClient);
534 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800535 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
536 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700537 pw.print(" configChangeFlags=");
538 pw.println(Integer.toHexString(configChangeFlags));
539 }
Wale Ogunwalec4e63a42018-10-02 13:19:54 -0700540 if (mServiceConnectionsHolder != null) {
541 pw.print(prefix); pw.print("connections="); pw.println(mServiceConnectionsHolder);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700542 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800543 if (info != null) {
544 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
Winson Chung609e1e92017-05-08 10:52:12 -0700545 pw.println(prefix + "mLastReportedMultiWindowMode=" + mLastReportedMultiWindowMode
546 + " mLastReportedPictureInPictureMode=" + mLastReportedPictureInPictureMode);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700547 if (info.supportsPictureInPicture()) {
548 pw.println(prefix + "supportsPictureInPicture=" + info.supportsPictureInPicture());
Winson Chungf7e03e12017-08-22 11:32:16 -0700549 pw.println(prefix + "supportsEnterPipOnTaskSwitch: "
550 + supportsEnterPipOnTaskSwitch);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700551 }
552 if (info.maxAspectRatio != 0) {
553 pw.println(prefix + "maxAspectRatio=" + info.maxAspectRatio);
554 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
557
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -0700558 void updateApplicationInfo(ApplicationInfo aInfo) {
559 appInfo = aInfo;
560 info.applicationInfo = aInfo;
561 }
562
Andrii Kulian21713ac2016-10-12 22:05:05 -0700563 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700564 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
565 }
566
Andrii Kulian21713ac2016-10-12 22:05:05 -0700567 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700568 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
569 }
570
Andrii Kulian21713ac2016-10-12 22:05:05 -0700571 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700572 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
573 }
574
Filip Gruszczynski23493322015-07-29 17:02:59 -0700575 /**
576 * The purpose of this method is to decide whether the activity needs to be relaunched upon
577 * changing its size. In most cases the activities don't need to be relaunched, if the resize
578 * is small, all the activity content has to do is relayout itself within new bounds. There are
579 * cases however, where the activity's content would be completely changed in the new size and
580 * the full relaunch is required.
581 *
582 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
583 * required. These thresholds are collected from the application resource qualifiers. For
584 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
585 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
586 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
587 * of the threshold.
588 */
589 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
590 int secondDp) {
591 if (thresholds == null) {
592 return false;
593 }
594 for (int i = thresholds.length - 1; i >= 0; i--) {
595 final int threshold = thresholds[i];
596 if ((firstDp < threshold && secondDp >= threshold)
597 || (firstDp >= threshold && secondDp < threshold)) {
598 return true;
599 }
600 }
601 return false;
602 }
603
Andrii Kulian21713ac2016-10-12 22:05:05 -0700604 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700605 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700606 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
607 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700608 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700609 }
610
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800611 private void scheduleActivityMovedToDisplay(int displayId, Configuration config) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700612 if (!attachedToProcess()) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800613 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.w(TAG,
614 "Can't report activity moved to display - client not running, activityRecord="
615 + this + ", displayId=" + displayId);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800616 return;
617 }
618 try {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800619 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
620 "Reporting activity moved to display" + ", activityRecord=" + this
621 + ", displayId=" + displayId + ", config=" + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700622
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700623 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800624 MoveToDisplayItem.obtain(displayId, config));
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800625 } catch (RemoteException e) {
626 // If process died, whatever.
627 }
628 }
629
630 private void scheduleConfigurationChanged(Configuration config) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700631 if (!attachedToProcess()) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800632 if (DEBUG_CONFIGURATION) Slog.w(TAG,
633 "Can't report activity configuration update - client not running"
634 + ", activityRecord=" + this);
635 return;
636 }
637 try {
638 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + ", config: "
639 + config);
640
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700641 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800642 ActivityConfigurationChangeItem.obtain(config));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800643 } catch (RemoteException e) {
644 // If process died, whatever.
645 }
646 }
647
Winson Chung5af42fc2017-03-24 17:11:33 -0700648 void updateMultiWindowMode() {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700649 if (task == null || task.getStack() == null || !attachedToProcess()) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800650 return;
651 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700652
Wale Ogunwaleeb76b762017-11-17 10:08:04 -0800653 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
654 // Don't do anything if we are currently deferring multi-window mode change.
655 return;
656 }
657
Winson Chung5af42fc2017-03-24 17:11:33 -0700658 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Wale Ogunwaleeb76b762017-11-17 10:08:04 -0800659 final boolean inMultiWindowMode = inMultiWindowMode();
Winson Chung609e1e92017-05-08 10:52:12 -0700660 if (inMultiWindowMode != mLastReportedMultiWindowMode) {
661 mLastReportedMultiWindowMode = inMultiWindowMode;
Winson Chung5af42fc2017-03-24 17:11:33 -0700662 scheduleMultiWindowModeChanged(getConfiguration());
663 }
664 }
665
666 private void scheduleMultiWindowModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800667 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700668 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700669 MultiWindowModeChangeItem.obtain(mLastReportedMultiWindowMode, overrideConfig));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800670 } catch (Exception e) {
671 // If process died, I don't care.
672 }
673 }
674
Winson Chungab76bbc2017-08-14 13:33:51 -0700675 void updatePictureInPictureMode(Rect targetStackBounds, boolean forceUpdate) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700676 if (task == null || task.getStack() == null || !attachedToProcess()) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800677 return;
678 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700679
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700680 final boolean inPictureInPictureMode = inPinnedWindowingMode() && targetStackBounds != null;
Winson Chungab76bbc2017-08-14 13:33:51 -0700681 if (inPictureInPictureMode != mLastReportedPictureInPictureMode || forceUpdate) {
Winson Chung5af42fc2017-03-24 17:11:33 -0700682 // Picture-in-picture mode changes also trigger a multi-window mode change as well, so
Winson Chung059955f2018-08-08 16:10:20 -0700683 // update that here in order. Set the last reported MW state to the same as the PiP
684 // state since we haven't yet actually resized the task (these callbacks need to
685 // preceed the configuration change from the resiez.
686 // TODO(110009072): Once we move these callbacks to the client, remove all logic related
687 // to forcing the update of the picture-in-picture mode as a part of the PiP animation.
Winson Chung609e1e92017-05-08 10:52:12 -0700688 mLastReportedPictureInPictureMode = inPictureInPictureMode;
Winson Chung059955f2018-08-08 16:10:20 -0700689 mLastReportedMultiWindowMode = inPictureInPictureMode;
Winson Chung5af42fc2017-03-24 17:11:33 -0700690 final Configuration newConfig = task.computeNewOverrideConfigurationForBounds(
691 targetStackBounds, null);
692 schedulePictureInPictureModeChanged(newConfig);
693 scheduleMultiWindowModeChanged(newConfig);
694 }
695 }
696
697 private void schedulePictureInPictureModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800698 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700699 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800700 PipModeChangeItem.obtain(mLastReportedPictureInPictureMode,
Andrii Kulian446e8242017-10-26 15:17:29 -0700701 overrideConfig));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800702 } catch (Exception e) {
703 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800704 }
705 }
706
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700707 @Override
708 protected int getChildCount() {
709 // {@link ActivityRecord} is a leaf node and has no children.
710 return 0;
711 }
712
713 @Override
714 protected ConfigurationContainer getChildAt(int index) {
715 return null;
716 }
717
718 @Override
719 protected ConfigurationContainer getParent() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700720 return getTask();
721 }
722
723 TaskRecord getTask() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700724 return task;
725 }
726
Bryce Leeaf691c02017-03-20 14:20:22 -0700727 /**
728 * Sets reference to the {@link TaskRecord} the {@link ActivityRecord} will treat as its parent.
729 * Note that this does not actually add the {@link ActivityRecord} as a {@link TaskRecord}
730 * children. However, this method will clean up references to this {@link ActivityRecord} in
731 * {@link ActivityStack}.
732 * @param task The new parent {@link TaskRecord}.
733 */
734 void setTask(TaskRecord task) {
Bryce Lee84730a02018-04-03 14:10:04 -0700735 setTask(task /* task */, false /* reparenting */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700736 }
737
738 /**
739 * This method should only be called by {@link TaskRecord#removeActivity(ActivityRecord)}.
Bryce Lee84730a02018-04-03 14:10:04 -0700740 * @param task The new parent task.
741 * @param reparenting Whether we're in the middle of reparenting.
Bryce Leeaf691c02017-03-20 14:20:22 -0700742 */
743 void setTask(TaskRecord task, boolean reparenting) {
744 // Do nothing if the {@link TaskRecord} is the same as the current {@link getTask}.
745 if (task != null && task == getTask()) {
746 return;
747 }
748
Bryce Lee84730a02018-04-03 14:10:04 -0700749 final ActivityStack oldStack = getStack();
750 final ActivityStack newStack = task != null ? task.getStack() : null;
Bryce Leeaf691c02017-03-20 14:20:22 -0700751
Bryce Lee84730a02018-04-03 14:10:04 -0700752 // Inform old stack (if present) of activity removal and new stack (if set) of activity
753 // addition.
754 if (oldStack != newStack) {
755 if (!reparenting && oldStack != null) {
756 oldStack.onActivityRemovedFromStack(this);
757 }
758
759 if (newStack != null) {
760 newStack.onActivityAddedToStack(this);
761 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700762 }
763
764 this.task = task;
765
766 if (!reparenting) {
767 onParentChanged();
768 }
769 }
770
chaviw4ad54912018-05-30 11:05:44 -0700771 /**
772 * See {@link AppWindowContainerController#setWillCloseOrEnterPip(boolean)}
773 */
774 void setWillCloseOrEnterPip(boolean willCloseOrEnterPip) {
775 getWindowContainerController().setWillCloseOrEnterPip(willCloseOrEnterPip);
776 }
777
Dianne Hackbornbe707852011-11-11 14:32:10 -0800778 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700779 private final WeakReference<ActivityRecord> weakActivity;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700780 private final String name;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800781
Steven Timotiusaf03df62017-07-18 16:56:43 -0700782 Token(ActivityRecord activity, Intent intent) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800783 weakActivity = new WeakReference<>(activity);
Steven Timotiusaf03df62017-07-18 16:56:43 -0700784 name = intent.getComponent().flattenToShortString();
Wale Ogunwale7d701172015-03-11 15:36:30 -0700785 }
786
Andrii Kulian21713ac2016-10-12 22:05:05 -0700787 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700788 if (token == null) {
789 return null;
790 }
791 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700792 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700793 return null;
794 }
795 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800796 }
797
Craig Mautnerde4ef022013-04-07 19:01:33 -0700798 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800799 public String toString() {
800 StringBuilder sb = new StringBuilder(128);
801 sb.append("Token{");
802 sb.append(Integer.toHexString(System.identityHashCode(this)));
803 sb.append(' ');
804 sb.append(weakActivity.get());
805 sb.append('}');
806 return sb.toString();
807 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700808
809 @Override
810 public String getName() {
811 return name;
812 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800813 }
814
Wale Ogunwale7d701172015-03-11 15:36:30 -0700815 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800816 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700817 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800818 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800819 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800820 return null;
821 }
822 }
823
Chong Zhang85ee6542015-10-02 13:36:38 -0700824 boolean isResolverActivity() {
825 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700826 }
827
Patrick Baumann31426b22018-05-21 13:46:40 -0700828 boolean isResolverOrChildActivity() {
829 if (!"android".equals(packageName)) {
830 return false;
831 }
832 try {
833 return ResolverActivity.class.isAssignableFrom(
834 Object.class.getClassLoader().loadClass(realActivity.getClassName()));
835 } catch (ClassNotFoundException e) {
836 return false;
837 }
838 }
839
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700840 ActivityRecord(ActivityTaskManagerService _service, WindowProcessController _caller,
841 int _launchedFromPid, int _launchedFromUid, String _launchedFromPackage, Intent _intent,
842 String _resolvedType, ActivityInfo aInfo, Configuration _configuration,
843 ActivityRecord _resultTo, String _resultWho, int _reqCode, boolean _componentSpecified,
844 boolean _rootVoiceInteraction, ActivityStackSupervisor supervisor,
845 ActivityOptions options, ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 service = _service;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700847 appToken = new Token(this, _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 info = aInfo;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800849 launchedFromPid = _launchedFromPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800851 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700852 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 intent = _intent;
854 shortComponentName = _intent.getComponent().flattenToShortString();
855 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800856 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700857 rootVoiceInteraction = _rootVoiceInteraction;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700858 mLastReportedConfiguration = new MergedConfiguration(_configuration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 resultTo = _resultTo;
860 resultWho = _resultWho;
861 requestCode = _reqCode;
Bryce Lee7ace3952018-02-16 14:34:32 -0800862 setState(INITIALIZING, "ActivityRecord ctor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 frontOfTask = false;
864 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700866 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800868 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 keysPaused = false;
870 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700871 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 nowVisible = false;
Vishnu Nair9ba31652018-11-13 14:34:05 -0800873 mDrawn = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 idle = false;
875 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700876 mStackSupervisor = supervisor;
Robert Carr0f5d7532016-10-17 16:39:17 -0700877
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800878 // This starts out true, since the initial state of an activity is that we have everything,
879 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700880 haveState = true;
881
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800882 // If the class name in the intent doesn't match that of the target, this is
883 // probably an alias. We have to create a new ComponentName object to keep track
884 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
885 if (aInfo.targetActivity == null
886 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
887 && (aInfo.launchMode == LAUNCH_MULTIPLE
888 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
889 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800891 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800893 taskAffinity = aInfo.taskAffinity;
894 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
895 appInfo = aInfo.applicationInfo;
896 nonLocalizedLabel = aInfo.nonLocalizedLabel;
897 labelRes = aInfo.labelRes;
898 if (nonLocalizedLabel == null && labelRes == 0) {
899 ApplicationInfo app = aInfo.applicationInfo;
900 nonLocalizedLabel = app.nonLocalizedLabel;
901 labelRes = app.labelRes;
902 }
903 icon = aInfo.getIconResource();
904 logo = aInfo.getLogoResource();
905 theme = aInfo.getThemeResource();
906 realTheme = theme;
907 if (realTheme == 0) {
908 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
909 ? android.R.style.Theme : android.R.style.Theme_Holo;
910 }
911 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
912 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
913 }
914 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
915 && (aInfo.applicationInfo.uid == SYSTEM_UID
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700916 || aInfo.applicationInfo.uid == _caller.mInfo.uid)) {
917 processName = _caller.mName;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800918 } else {
919 processName = aInfo.processName;
920 }
921
922 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
923 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
924 }
925
926 packageName = aInfo.applicationInfo.packageName;
927 launchMode = aInfo.launchMode;
928
Ruben Brunkf53497c2017-03-27 20:26:17 -0700929 Entry ent = AttributeCache.instance().get(packageName,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800930 realTheme, com.android.internal.R.styleable.Window, userId);
Bryce Leee83f34cd2017-10-31 19:50:54 -0700931
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700932 if (ent != null) {
933 fullscreen = !ActivityInfo.isTranslucentOrFloating(ent.array);
934 hasWallpaper = ent.array.getBoolean(R.styleable.Window_windowShowWallpaper, false);
935 noDisplay = ent.array.getBoolean(R.styleable.Window_windowNoDisplay, false);
936 } else {
937 hasWallpaper = false;
938 noDisplay = false;
939 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800940
Winson Chung83471632016-12-13 11:02:12 -0800941 setActivityType(_componentSpecified, _launchedFromUid, _intent, options, sourceRecord);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800942
943 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
944
945 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
946 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
chaviw59b98852017-06-13 12:05:44 -0700947
948 mShowWhenLocked = (aInfo.flags & FLAG_SHOW_WHEN_LOCKED) != 0;
949 mTurnScreenOn = (aInfo.flags & FLAG_TURN_SCREEN_ON) != 0;
Charles He2bf28322017-10-12 22:24:49 +0100950
951 mRotationAnimationHint = aInfo.rotationAnimation;
952 lockTaskLaunchMode = aInfo.lockTaskLaunchMode;
953 if (appInfo.isPrivilegedApp() && (lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_ALWAYS
954 || lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
955 lockTaskLaunchMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
956 }
957
958 if (options != null) {
959 pendingOptions = options;
960 mLaunchTaskBehind = options.getLaunchTaskBehind();
961
962 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
963 // Only override manifest supplied option if set.
964 if (rotationAnimation >= 0) {
965 mRotationAnimationHint = rotationAnimation;
966 }
967 final PendingIntent usageReport = pendingOptions.getUsageTimeReport();
968 if (usageReport != null) {
969 appTimeTracker = new AppTimeTracker(usageReport);
970 }
971 final boolean useLockTask = pendingOptions.getLockTaskMode();
972 if (useLockTask && lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_DEFAULT) {
973 lockTaskLaunchMode = LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
974 }
975 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800976 }
977
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700978 void setProcess(WindowProcessController proc) {
Dianne Hackborn68a06332017-11-15 17:54:18 -0800979 app = proc;
980 final ActivityRecord root = task != null ? task.getRootActivity() : null;
981 if (root == this) {
982 task.setRootProcess(proc);
983 }
984 }
985
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700986 boolean hasProcess() {
987 return app != null;
988 }
989
990 boolean attachedToProcess() {
991 return hasProcess() && app.hasThread();
992 }
993
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800994 AppWindowContainerController getWindowContainerController() {
995 return mWindowContainerController;
996 }
997
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800998 void createWindowContainer() {
999 if (mWindowContainerController != null) {
1000 throw new IllegalArgumentException("Window container=" + mWindowContainerController
1001 + " already created for r=" + this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001002 }
1003
1004 inHistory = true;
1005
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001006 final TaskWindowContainerController taskController = task.getWindowContainerController();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001007
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001008 // TODO(b/36505427): Maybe this call should be moved inside updateOverrideConfiguration()
1009 task.updateOverrideConfigurationFromLaunchBounds();
1010 // Make sure override configuration is up-to-date before using to create window controller.
1011 updateOverrideConfiguration();
1012
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001013 mWindowContainerController = new AppWindowContainerController(taskController, appToken,
Winson Chung48b25652018-10-22 14:04:30 -07001014 realActivity, this, Integer.MAX_VALUE /* add on top */, info.screenOrientation,
1015 fullscreen, (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001016 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
1017 appInfo.targetSdkVersion, mRotationAnimationHint,
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001018 ActivityTaskManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001019
1020 task.addActivityToTop(this);
1021
Winson Chung609e1e92017-05-08 10:52:12 -07001022 // When an activity is started directly into a split-screen fullscreen stack, we need to
1023 // update the initial multi-window modes so that the callbacks are scheduled correctly when
1024 // the user leaves that mode.
Bryce Leef3c6a472017-11-14 14:53:06 -08001025 mLastReportedMultiWindowMode = inMultiWindowMode();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001026 mLastReportedPictureInPictureMode = inPinnedWindowingMode();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001027 }
1028
1029 void removeWindowContainer() {
Bryce Lee7ace3952018-02-16 14:34:32 -08001030 // Do not try to remove a window container if we have already removed it.
1031 if (mWindowContainerController == null) {
1032 return;
1033 }
1034
Wale Ogunwalecc367f42017-02-01 08:12:14 -08001035 // Resume key dispatching if it is currently paused before we remove the container.
1036 resumeKeyDispatchingLocked();
1037
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001038 mWindowContainerController.removeContainer(getDisplayId());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001039 mWindowContainerController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
Winson Chung30480042017-01-26 10:55:34 -08001042 /**
1043 * Reparents this activity into {@param newTask} at the provided {@param position}. The caller
1044 * should ensure that the {@param newTask} is not already the parent of this activity.
1045 */
1046 void reparent(TaskRecord newTask, int position, String reason) {
1047 final TaskRecord prevTask = task;
1048 if (prevTask == newTask) {
1049 throw new IllegalArgumentException(reason + ": task=" + newTask
1050 + " is already the parent of r=" + this);
1051 }
1052
Winson Chung74666102017-02-22 17:49:24 -08001053 // TODO: Ensure that we do not directly reparent activities across stacks, as that may leave
1054 // the stacks in strange states. For now, we should use Task.reparent() to ensure that
1055 // the stack is left in an OK state.
1056 if (prevTask != null && newTask != null && prevTask.getStack() != newTask.getStack()) {
1057 throw new IllegalArgumentException(reason + ": task=" + newTask
1058 + " is in a different stack (" + newTask.getStackId() + ") than the parent of"
1059 + " r=" + this + " (" + prevTask.getStackId() + ")");
1060 }
1061
Winson Chung30480042017-01-26 10:55:34 -08001062 // Must reparent first in window manager
1063 mWindowContainerController.reparent(newTask.getWindowContainerController(), position);
1064
Bryce Lee84730a02018-04-03 14:10:04 -07001065 // Reparenting prevents informing the parent stack of activity removal in the case that
1066 // the new stack has the same parent. we must manually signal here if this is not the case.
1067 final ActivityStack prevStack = prevTask.getStack();
1068
1069 if (prevStack != newTask.getStack()) {
1070 prevStack.onActivityRemovedFromStack(this);
1071 }
Bryce Leeaf691c02017-03-20 14:20:22 -07001072 // Remove the activity from the old task and add it to the new task.
Bryce Lee84730a02018-04-03 14:10:04 -07001073 prevTask.removeActivity(this, true /* reparenting */);
Bryce Lee0f9bde82017-02-22 16:39:06 -08001074
Winson Chung30480042017-01-26 10:55:34 -08001075 newTask.addActivityAtIndex(position, this);
1076 }
1077
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001078 private boolean isHomeIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001079 return ACTION_MAIN.equals(intent.getAction())
1080 && intent.hasCategory(CATEGORY_HOME)
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001081 && intent.getCategories().size() == 1
1082 && intent.getData() == null
1083 && intent.getType() == null;
1084 }
1085
Chong Zhangad24f962016-08-25 12:12:33 -07001086 static boolean isMainIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001087 return ACTION_MAIN.equals(intent.getAction())
1088 && intent.hasCategory(CATEGORY_LAUNCHER)
Chong Zhangad24f962016-08-25 12:12:33 -07001089 && intent.getCategories().size() == 1
1090 && intent.getData() == null
1091 && intent.getType() == null;
1092 }
1093
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001094 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
1095 if (uid == Process.myUid() || uid == 0) {
1096 // System process can launch home activity.
1097 return true;
1098 }
Winson Chung547afd22018-05-17 16:03:25 -07001099 // Allow the recents component to launch the home activity.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001100 final RecentTasks recentTasks = mStackSupervisor.mService.getRecentTasks();
Winson Chung547afd22018-05-17 16:03:25 -07001101 if (recentTasks != null && recentTasks.isCallerRecents(uid)) {
1102 return true;
1103 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001104 // Resolver activity can launch home activity.
1105 return sourceRecord != null && sourceRecord.isResolverActivity();
1106 }
1107
Winson Chung83471632016-12-13 11:02:12 -08001108 /**
1109 * @return whether the given package name can launch an assist activity.
1110 */
1111 private boolean canLaunchAssistActivity(String packageName) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001112 final ComponentName assistComponent =
1113 service.mActiveVoiceInteractionServiceComponent;
Winson Chung83471632016-12-13 11:02:12 -08001114 if (assistComponent != null) {
1115 return assistComponent.getPackageName().equals(packageName);
1116 }
1117 return false;
1118 }
1119
1120 private void setActivityType(boolean componentSpecified, int launchedFromUid, Intent intent,
1121 ActivityOptions options, ActivityRecord sourceRecord) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001122 int activityType = ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001123 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
1124 && isHomeIntent(intent) && !isResolverActivity()) {
1125 // This sure looks like a home activity!
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001126 activityType = ACTIVITY_TYPE_HOME;
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001127
1128 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
1129 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
1130 // We only allow home activities to be resizeable if they explicitly requested it.
1131 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
1132 }
Wale Ogunwale16e505a2018-05-07 15:00:49 -07001133 } else if (realActivity.getClassName().contains(LEGACY_RECENTS_PACKAGE_NAME)
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001134 || service.getRecentTasks().isRecentsComponent(realActivity, appInfo.uid)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001135 activityType = ACTIVITY_TYPE_RECENTS;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001136 } else if (options != null && options.getLaunchActivityType() == ACTIVITY_TYPE_ASSISTANT
Winson Chung83471632016-12-13 11:02:12 -08001137 && canLaunchAssistActivity(launchedFromPackage)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001138 activityType = ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001139 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001140 setActivityType(activityType);
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001141 }
1142
Craig Mautnera228ae92014-07-09 05:44:55 -07001143 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001144 if (launchMode != LAUNCH_SINGLE_INSTANCE && launchMode != LAUNCH_SINGLE_TASK) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001145 task.setTaskToAffiliateWith(taskToAffiliateWith);
1146 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001147 }
1148
Andrii Kulian02b7a832016-10-06 23:11:56 -07001149 /**
1150 * @return Stack value from current task, null if there is no task.
1151 */
Winson Chung55893332017-02-17 17:13:10 -08001152 <T extends ActivityStack> T getStack() {
1153 return task != null ? (T) task.getStack() : null;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001154 }
1155
Jorim Jaggicdfc04e2017-04-28 19:06:24 +02001156 int getStackId() {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001157 return getStack() != null ? getStack().mStackId : INVALID_STACK_ID;
1158 }
1159
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001160 ActivityDisplay getDisplay() {
1161 final ActivityStack stack = getStack();
1162 return stack != null ? stack.getDisplay() : null;
1163 }
1164
Craig Mautner5eda9b32013-07-02 11:58:16 -07001165 boolean changeWindowTranslucency(boolean toOpaque) {
1166 if (fullscreen == toOpaque) {
1167 return false;
1168 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001169
Craig Mautner5eda9b32013-07-02 11:58:16 -07001170 // Keep track of the number of fullscreen activities in this task.
1171 task.numFullscreen += toOpaque ? +1 : -1;
1172
1173 fullscreen = toOpaque;
1174 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -07001175 }
1176
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001177 void takeFromHistory() {
1178 if (inHistory) {
1179 inHistory = false;
1180 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001181 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001182 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001183 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001184 }
1185 }
1186
1187 boolean isInHistory() {
1188 return inHistory;
1189 }
1190
Wale Ogunwale7d701172015-03-11 15:36:30 -07001191 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001192 final ActivityStack stack = getStack();
1193 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001194 }
1195
Craig Mautner21d24a22014-04-23 11:45:37 -07001196 boolean isPersistable() {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001197 return (info.persistableMode == PERSIST_ROOT_ONLY ||
1198 info.persistableMode == PERSIST_ACROSS_REBOOTS) &&
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001199 (intent == null || (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -07001200 }
1201
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001202 boolean isFocusable() {
Bryce Lee7b851cc2018-04-10 14:53:13 -07001203 return mStackSupervisor.isFocusable(this, isAlwaysFocusable());
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001204 }
1205
1206 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001207 return ActivityInfo.isResizeableMode(info.resizeMode) || info.supportsPictureInPicture();
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001208 }
1209
Winson Chungd3395382016-12-13 11:49:09 -08001210 /**
1211 * @return whether this activity is non-resizeable or forced to be resizeable
1212 */
1213 boolean isNonResizableOrForcedResizable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001214 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -07001215 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -07001216 }
1217
Winson Chunge6308042016-10-31 09:24:01 -07001218 /**
Winson Chungd3395382016-12-13 11:49:09 -08001219 * @return whether this activity supports PiP multi-window and can be put in the pinned stack.
Winson Chunge6308042016-10-31 09:24:01 -07001220 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001221 boolean supportsPictureInPicture() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001222 return service.mSupportsPictureInPicture && isActivityTypeStandardOrUndefined()
Winson Chungd3395382016-12-13 11:49:09 -08001223 && info.supportsPictureInPicture();
1224 }
1225
1226 /**
1227 * @return whether this activity supports split-screen multi-window and can be put in the docked
1228 * stack.
1229 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001230 @Override
1231 public boolean supportsSplitScreenWindowingMode() {
Winson Chungd3395382016-12-13 11:49:09 -08001232 // An activity can not be docked even if it is considered resizeable because it only
1233 // supports picture-in-picture mode but has a non-resizeable resizeMode
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001234 return super.supportsSplitScreenWindowingMode()
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001235 && service.mSupportsSplitScreenMultiWindow && supportsResizeableMultiWindow();
Winson Chungd3395382016-12-13 11:49:09 -08001236 }
1237
1238 /**
1239 * @return whether this activity supports freeform multi-window and can be put in the freeform
1240 * stack.
1241 */
1242 boolean supportsFreeform() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001243 return service.mSupportsFreeformWindowManagement && supportsResizeableMultiWindow();
Winson Chungd3395382016-12-13 11:49:09 -08001244 }
1245
1246 /**
1247 * @return whether this activity supports non-PiP multi-window.
1248 */
1249 private boolean supportsResizeableMultiWindow() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001250 return service.mSupportsMultiWindow && !isActivityTypeHome()
Winson Chungd3395382016-12-13 11:49:09 -08001251 && (ActivityInfo.isResizeableMode(info.resizeMode)
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001252 || service.mForceResizableActivities);
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001253 }
1254
Winson Chunge6308042016-10-31 09:24:01 -07001255 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001256 * Check whether this activity can be launched on the specified display.
Riddle Hsu16567132018-08-16 21:37:47 +08001257 *
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001258 * @param displayId Target display id.
Riddle Hsu16567132018-08-16 21:37:47 +08001259 * @return {@code true} if either it is the default display or this activity can be put on a
1260 * secondary screen.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001261 */
1262 boolean canBeLaunchedOnDisplay(int displayId) {
Riddle Hsu16567132018-08-16 21:37:47 +08001263 return service.mStackSupervisor.canPlaceEntityOnDisplay(displayId, launchedFromPid,
1264 launchedFromUid, info);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001265 }
1266
1267 /**
Robert Carrc33658e2017-04-11 18:24:20 -07001268 * @param beforeStopping Whether this check is for an auto-enter-pip operation, that is to say
1269 * the activity has requested to enter PiP when it would otherwise be stopped.
1270 *
Winson Chung298f95b2017-08-10 15:57:18 -07001271 * @return whether this activity is currently allowed to enter PIP.
Winson Chunge6308042016-10-31 09:24:01 -07001272 */
Winson Chung298f95b2017-08-10 15:57:18 -07001273 boolean checkEnterPictureInPictureState(String caller, boolean beforeStopping) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07001274 if (!supportsPictureInPicture()) {
1275 return false;
1276 }
1277
Winson Chungf4ac0632017-03-17 12:34:12 -07001278 // Check app-ops and see if PiP is supported for this package
1279 if (!checkEnterPictureInPictureAppOpsState()) {
1280 return false;
1281 }
1282
Winson Chungf1bfee12017-03-24 17:11:33 -07001283 // Check to see if we are in VR mode, and disallow PiP if so
1284 if (service.shouldDisableNonVrUiLocked()) {
1285 return false;
1286 }
1287
Winson Chungc2baac02017-01-11 13:34:47 -08001288 boolean isKeyguardLocked = service.isKeyguardLocked();
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001289 boolean isCurrentAppLocked =
1290 service.getLockTaskModeState() != LOCK_TASK_MODE_NONE;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001291 final ActivityDisplay display = getDisplay();
1292 boolean hasPinnedStack = display != null && display.hasPinnedStack();
Winson Chungbb348802017-01-30 12:01:45 -08001293 // Don't return early if !isNotLocked, since we want to throw an exception if the activity
1294 // is in an incorrect state
Winson Chunge581ebf2017-02-21 08:25:03 -08001295 boolean isNotLockedOrOnKeyguard = !isKeyguardLocked && !isCurrentAppLocked;
Robert Carrc33658e2017-04-11 18:24:20 -07001296
1297 // We don't allow auto-PiP when something else is already pipped.
1298 if (beforeStopping && hasPinnedStack) {
1299 return false;
1300 }
1301
Bryce Lee7ace3952018-02-16 14:34:32 -08001302 switch (mState) {
Winson Chungc2baac02017-01-11 13:34:47 -08001303 case RESUMED:
Winson Chunge581ebf2017-02-21 08:25:03 -08001304 // When visible, allow entering PiP if the app is not locked. If it is over the
1305 // keyguard, then we will prompt to unlock in the caller before entering PiP.
Robert Carrc33658e2017-04-11 18:24:20 -07001306 return !isCurrentAppLocked &&
Winson Chungf7e03e12017-08-22 11:32:16 -07001307 (supportsEnterPipOnTaskSwitch || !beforeStopping);
Winson Chungc2baac02017-01-11 13:34:47 -08001308 case PAUSING:
1309 case PAUSED:
Winson Chungbb348802017-01-30 12:01:45 -08001310 // When pausing, then only allow enter PiP as in the resume state, and in addition,
1311 // require that there is not an existing PiP activity and that the current system
1312 // state supports entering PiP
Winson Chunge581ebf2017-02-21 08:25:03 -08001313 return isNotLockedOrOnKeyguard && !hasPinnedStack
Winson Chungf7e03e12017-08-22 11:32:16 -07001314 && supportsEnterPipOnTaskSwitch;
Winson Chungc2baac02017-01-11 13:34:47 -08001315 case STOPPING:
1316 // When stopping in a valid state, then only allow enter PiP as in the pause state.
1317 // Otherwise, fall through to throw an exception if the caller is trying to enter
1318 // PiP in an invalid stopping state.
Winson Chungf7e03e12017-08-22 11:32:16 -07001319 if (supportsEnterPipOnTaskSwitch) {
Winson Chungf4ac0632017-03-17 12:34:12 -07001320 return isNotLockedOrOnKeyguard && !hasPinnedStack;
Winson Chungc2baac02017-01-11 13:34:47 -08001321 }
1322 default:
Winson Chung298f95b2017-08-10 15:57:18 -07001323 return false;
Winson Chungb5c41b72016-12-07 15:00:47 -08001324 }
Winson Chunge6308042016-10-31 09:24:01 -07001325 }
1326
Winson Chung59fda9e2017-01-20 16:14:51 -08001327 /**
Winson Chungf4ac0632017-03-17 12:34:12 -07001328 * @return Whether AppOps allows this package to enter picture-in-picture.
Winson Chung59fda9e2017-01-20 16:14:51 -08001329 */
Winson Chungf4ac0632017-03-17 12:34:12 -07001330 private boolean checkEnterPictureInPictureAppOpsState() {
Wale Ogunwalef6733932018-06-27 05:14:34 -07001331 return service.getAppOpsService().checkOperation(
1332 OP_PICTURE_IN_PICTURE, appInfo.uid, packageName) == MODE_ALLOWED;
Winson Chung59fda9e2017-01-20 16:14:51 -08001333 }
1334
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001335 boolean isAlwaysFocusable() {
1336 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001337 }
1338
Louis Chang19443452018-10-09 12:10:21 +08001339 /** Move activity with its stack to front and make the stack focused. */
1340 boolean moveFocusableActivityToTop(String reason) {
1341 if (!isFocusable()) {
1342 if (DEBUG_FOCUS) {
1343 Slog.d(TAG_FOCUS, "moveActivityStackToFront: unfocusable activity=" + this);
1344 }
1345 return false;
1346 }
1347
1348 final TaskRecord task = getTask();
1349 final ActivityStack stack = getStack();
1350 if (stack == null) {
1351 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: activity="
1352 + this + " task=" + task);
1353 return false;
1354 }
1355
1356 if (mStackSupervisor.getTopResumedActivity() == this) {
1357 if (DEBUG_FOCUS) {
1358 Slog.d(TAG_FOCUS, "moveActivityStackToFront: already on top, activity=" + this);
1359 }
1360 return false;
1361 }
1362
1363 if (DEBUG_FOCUS) {
1364 Slog.d(TAG_FOCUS, "moveActivityStackToFront: activity=" + this);
1365 }
1366
1367 stack.moveToFront(reason, task);
1368 // Report top activity change to tracking services and WM
1369 if (mStackSupervisor.getTopResumedActivity() == this) {
1370 // TODO(b/111361570): Support multiple focused apps in WM
1371 service.setResumedActivityUncheckLocked(this, reason);
1372 }
1373 return true;
1374 }
Jorim Jaggife762342016-10-13 14:33:27 +02001375
1376 /**
1377 * @return true if the activity contains windows that have
1378 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
1379 */
1380 boolean hasDismissKeyguardWindows() {
1381 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
1382 }
1383
Wale Ogunwale7d701172015-03-11 15:36:30 -07001384 void makeFinishingLocked() {
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001385 if (finishing) {
1386 return;
1387 }
1388 finishing = true;
1389 if (stopped) {
1390 clearOptionsLocked();
1391 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001392
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001393 if (service != null) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001394 service.getTaskChangeNotificationController().notifyTaskStackChanged();
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001395 }
1396 }
1397
Dianne Hackborn7e269642010-08-25 19:50:20 -07001398 UriPermissionOwner getUriPermissionsLocked() {
1399 if (uriPermissions == null) {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001400 uriPermissions = new UriPermissionOwner(service.mUgmInternal, this);
Dianne Hackborn7e269642010-08-25 19:50:20 -07001401 }
1402 return uriPermissions;
1403 }
1404
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001405 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 int requestCode, int resultCode,
1407 Intent resultData) {
1408 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -05001409 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001411 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
1413 results.add(r);
1414 }
1415
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001416 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 int requestCode) {
1418 if (results != null) {
1419 for (int i=results.size()-1; i>=0; i--) {
1420 ActivityResult r = (ActivityResult)results.get(i);
1421 if (r.mFrom != from) continue;
1422 if (r.mResultWho == null) {
1423 if (resultWho != null) continue;
1424 } else {
1425 if (!r.mResultWho.equals(resultWho)) continue;
1426 }
1427 if (r.mRequestCode != requestCode) continue;
1428
1429 results.remove(i);
1430 }
1431 }
1432 }
1433
Andrii Kulian21713ac2016-10-12 22:05:05 -07001434 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001436 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
1438 newIntents.add(intent);
1439 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001440
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001441 final boolean isSleeping() {
1442 final ActivityStack stack = getStack();
Wale Ogunwalef6733932018-06-27 05:14:34 -07001443 return stack != null ? stack.shouldSleepActivities() : service.isSleepingLocked();
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001444 }
1445
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001446 /**
1447 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1448 * method will be called at the proper time.
1449 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001450 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001451 // The activity now gets access to the data associated with this Intent.
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001452 service.mUgmInternal.grantUriPermissionFromIntent(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001453 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001454 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001455 boolean unsent = true;
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001456 final boolean isTopActivityWhileSleeping = isTopRunningActivity() && isSleeping();
Wale Ogunwale826c7062016-09-13 08:25:54 -07001457
1458 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001459 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1460 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001461 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001462 if ((mState == RESUMED || mState == PAUSED || isTopActivityWhileSleeping)
1463 && attachedToProcess()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001465 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1466 ar.add(rintent);
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001467 service.getLifecycleManager().scheduleTransaction(
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001468 app.getThread(), appToken, NewIntentItem.obtain(ar, mState == PAUSED));
Craig Mautner86d67a42013-05-14 10:34:38 -07001469 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001470 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001471 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001472 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001473 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001474 }
1475 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001476 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001477 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001478 }
1479 }
1480
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001481 void updateOptionsLocked(ActivityOptions options) {
1482 if (options != null) {
1483 if (pendingOptions != null) {
1484 pendingOptions.abort();
1485 }
1486 pendingOptions = options;
1487 }
1488 }
1489
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001490 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001491 if (pendingOptions != null
Ruben Brunkf53497c2017-03-27 20:26:17 -07001492 && pendingOptions.getAnimationType() != ANIM_SCENE_TRANSITION) {
lumark588a3e82018-07-20 18:53:54 +08001493 mWindowContainerController.applyOptionsLocked(pendingOptions, intent);
chaviw82a0ba82018-03-15 14:26:29 -07001494 if (task == null) {
1495 clearOptionsLocked(false /* withAbort */);
1496 } else {
1497 // This will clear the options for all the ActivityRecords for this Task.
1498 task.clearAllPendingOptions();
1499 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001500 }
1501 }
1502
Adam Powellcfbe9be2013-11-06 14:58:58 -08001503 ActivityOptions getOptionsForTargetActivityLocked() {
1504 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1505 }
1506
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001507 void clearOptionsLocked() {
chaviw82a0ba82018-03-15 14:26:29 -07001508 clearOptionsLocked(true /* withAbort */);
1509 }
1510
1511 void clearOptionsLocked(boolean withAbort) {
1512 if (withAbort && pendingOptions != null) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001513 pendingOptions.abort();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001514 }
chaviw82a0ba82018-03-15 14:26:29 -07001515 pendingOptions = null;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001516 }
1517
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001518 ActivityOptions takeOptionsLocked() {
1519 ActivityOptions opts = pendingOptions;
1520 pendingOptions = null;
1521 return opts;
1522 }
1523
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001524 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001525 if (uriPermissions != null) {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001526 uriPermissions.removeUriPermissions();
Dianne Hackborn7e269642010-08-25 19:50:20 -07001527 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 }
1529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530
1531 void pauseKeyDispatchingLocked() {
1532 if (!keysPaused) {
1533 keysPaused = true;
Bryce Lee2b8e0372018-04-05 17:01:37 -07001534
1535 if (mWindowContainerController != null) {
1536 mWindowContainerController.pauseKeyDispatching();
1537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539 }
1540
1541 void resumeKeyDispatchingLocked() {
1542 if (keysPaused) {
1543 keysPaused = false;
Bryce Lee2b8e0372018-04-05 17:01:37 -07001544
1545 if (mWindowContainerController != null) {
1546 mWindowContainerController.resumeKeyDispatching();
1547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 }
1549 }
1550
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001551 private void updateTaskDescription(CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001552 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001553 }
1554
Wale Ogunwaleec950642017-04-25 07:44:21 -07001555 void setDeferHidingClient(boolean deferHidingClient) {
1556 if (mDeferHidingClient == deferHidingClient) {
1557 return;
1558 }
1559 mDeferHidingClient = deferHidingClient;
1560 if (!mDeferHidingClient && !visible) {
1561 // Hiding the client is no longer deferred and the app isn't visible still, go ahead and
1562 // update the visibility.
1563 setVisibility(false);
1564 }
Wale Ogunwale89973222017-04-23 18:39:45 -07001565 }
1566
Wale Ogunwaleec950642017-04-25 07:44:21 -07001567 void setVisibility(boolean visible) {
Tim Murray68ed8442017-08-29 23:21:27 +00001568 mWindowContainerController.setVisibility(visible, mDeferHidingClient);
Bryce Lee2a3cc462017-10-27 10:57:35 -07001569 mStackSupervisor.getActivityMetricsLogger().notifyVisibilityChanged(this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001570 }
1571
1572 // TODO: Look into merging with #setVisibility()
Wale Ogunwaleec950642017-04-25 07:44:21 -07001573 void setVisible(boolean newVisible) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001574 visible = newVisible;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001575 mDeferHidingClient = !visible && mDeferHidingClient;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001576 setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001577 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1578 }
1579
Bryce Lee7ace3952018-02-16 14:34:32 -08001580 void setState(ActivityState state, String reason) {
1581 if (DEBUG_STATES) Slog.v(TAG_STATES, "State movement: " + this + " from:" + getState()
1582 + " to:" + state + " reason:" + reason);
Bryce Lee6ff17072018-02-28 07:26:17 -08001583
Bryce Leeb0f993f2018-03-02 15:38:01 -08001584 if (state == mState) {
1585 // No need to do anything if state doesn't change.
1586 if (DEBUG_STATES) Slog.v(TAG_STATES, "State unchanged from:" + state);
1587 return;
1588 }
1589
Bryce Leeb0f993f2018-03-02 15:38:01 -08001590 mState = state;
1591
Bryce Leec4ab62a2018-03-05 14:19:26 -08001592 final TaskRecord parent = getTask();
1593
1594 if (parent != null) {
1595 parent.onActivityStateChanged(this, state, reason);
1596 }
Robert Carr29daa922018-04-27 11:56:48 -07001597
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001598 // The WindowManager interprets the app stopping signal as
1599 // an indication that the Surface will eventually be destroyed.
1600 // This however isn't necessarily true if we are going to sleep.
1601 if (state == STOPPING && !isSleeping()) {
Robert Carr29daa922018-04-27 11:56:48 -07001602 mWindowContainerController.notifyAppStopping();
1603 }
Bryce Lee7ace3952018-02-16 14:34:32 -08001604 }
1605
1606 ActivityState getState() {
1607 return mState;
1608 }
1609
1610 /**
1611 * Returns {@code true} if the Activity is in the specified state.
1612 */
1613 boolean isState(ActivityState state) {
1614 return state == mState;
1615 }
1616
1617 /**
1618 * Returns {@code true} if the Activity is in one of the specified states.
1619 */
1620 boolean isState(ActivityState state1, ActivityState state2) {
1621 return state1 == mState || state2 == mState;
1622 }
1623
1624 /**
1625 * Returns {@code true} if the Activity is in one of the specified states.
1626 */
1627 boolean isState(ActivityState state1, ActivityState state2, ActivityState state3) {
1628 return state1 == mState || state2 == mState || state3 == mState;
1629 }
1630
1631 /**
1632 * Returns {@code true} if the Activity is in one of the specified states.
1633 */
1634 boolean isState(ActivityState state1, ActivityState state2, ActivityState state3,
1635 ActivityState state4) {
1636 return state1 == mState || state2 == mState || state3 == mState || state4 == mState;
1637 }
1638
Jorim Jaggibae01b12017-04-11 16:29:10 -07001639 void notifyAppResumed(boolean wasStopped) {
1640 mWindowContainerController.notifyAppResumed(wasStopped);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001641 }
1642
1643 void notifyUnknownVisibilityLaunched() {
Jorim Jaggi838c2452017-08-28 15:44:43 +02001644
1645 // No display activities never add a window, so there is no point in waiting them for
1646 // relayout.
1647 if (!noDisplay) {
1648 mWindowContainerController.notifyUnknownVisibilityLaunched();
1649 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001650 }
1651
Jorim Jaggi241ae102016-11-02 21:57:33 -07001652 /**
1653 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1654 * might have on the visibility
1655 *
1656 * @see {@link ActivityStack#checkKeyguardVisibility}
1657 */
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001658 boolean shouldBeVisibleIgnoringKeyguard(boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001659 if (!okToShowLocked()) {
1660 return false;
1661 }
1662
Winson Chung3f0e59a2017-10-25 10:19:05 -07001663 return !behindFullscreenActivity || mLaunchTaskBehind;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001664 }
1665
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001666 void makeVisibleIfNeeded(ActivityRecord starting, boolean reportToClient) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001667 // This activity is not currently visible, but is running. Tell it to become visible.
Bryce Lee7ace3952018-02-16 14:34:32 -08001668 if (mState == RESUMED || this == starting) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001669 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
Bryce Lee7ace3952018-02-16 14:34:32 -08001670 "Not making visible, r=" + this + " state=" + mState + " starting=" + starting);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001671 return;
1672 }
1673
1674 // If this activity is paused, tell it to now show its window.
1675 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1676 "Making visible and scheduling visibility: " + this);
1677 final ActivityStack stack = getStack();
1678 try {
1679 if (stack.mTranslucentActivityWaiting != null) {
1680 updateOptionsLocked(returningOptions);
1681 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1682 }
1683 setVisible(true);
1684 sleeping = false;
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001685 app.postPendingUiCleanMsg(true);
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001686 if (reportToClient) {
1687 makeClientVisible();
1688 } else {
1689 mClientVisibilityDeferred = true;
1690 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001691 // The activity may be waiting for stop, but that is no longer appropriate for it.
1692 mStackSupervisor.mStoppingActivities.remove(this);
1693 mStackSupervisor.mGoingToSleepActivities.remove(this);
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001694 } catch (Exception e) {
1695 // Just skip on any failure; we'll make it visible when it next restarts.
1696 Slog.w(TAG, "Exception thrown making visible: " + intent.getComponent(), e);
1697 }
1698 handleAlreadyVisible();
1699 }
Andrii Kulian0d595f32018-02-21 15:47:33 -08001700
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001701 /** Send visibility change message to the client and pause if needed. */
1702 void makeClientVisible() {
1703 mClientVisibilityDeferred = false;
1704 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001705 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001706 WindowVisibilityItem.obtain(true /* showWindow */));
Andrii Kulian09e1afa2018-05-16 21:29:34 -07001707 if (shouldPauseWhenBecomingVisible()) {
Andrii Kulian0d595f32018-02-21 15:47:33 -08001708 // An activity must be in the {@link PAUSING} state for the system to validate
1709 // the move to {@link PAUSED}.
Bryce Lee7ace3952018-02-16 14:34:32 -08001710 setState(PAUSING, "makeVisibleIfNeeded");
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001711 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kulian0d595f32018-02-21 15:47:33 -08001712 PauseActivityItem.obtain(finishing, false /* userLeaving */,
Bryce Lee1d0d5142018-04-12 10:35:07 -07001713 configChangeFlags, false /* dontReport */));
Andrii Kulian0d595f32018-02-21 15:47:33 -08001714 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001715 } catch (Exception e) {
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001716 Slog.w(TAG, "Exception thrown sending visibility update: " + intent.getComponent(), e);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001717 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001718 }
1719
Andrii Kulian09e1afa2018-05-16 21:29:34 -07001720 /** Check if activity should be moved to PAUSED state when it becomes visible. */
1721 private boolean shouldPauseWhenBecomingVisible() {
1722 // If the activity is stopped or stopping, cycle to the paused state. We avoid doing
1723 // this when there is an activity waiting to become translucent as the extra binder
1724 // calls will lead to noticeable jank. A later call to
1725 // ActivityStack#ensureActivitiesVisibleLocked will bring the activity to the proper
1726 // paused state. We also avoid doing this for the activity the stack supervisor
1727 // considers the resumed activity, as normal means will bring the activity from STOPPED
1728 // to RESUMED. Adding PAUSING in this scenario will lead to double lifecycles.
1729 if (!isState(STOPPED, STOPPING) || getStack().mTranslucentActivityWaiting != null
Andrii Kulian52d255c2018-07-13 11:32:19 -07001730 || isResumedActivityOnDisplay()) {
Andrii Kulian09e1afa2018-05-16 21:29:34 -07001731 return false;
1732 }
1733
1734 // Check if position in task allows to become paused
1735 final int positionInTask = task.mActivities.indexOf(this);
1736 if (positionInTask == -1) {
1737 throw new IllegalStateException("Activity not found in its task");
1738 }
1739 if (positionInTask == task.mActivities.size() - 1) {
1740 // It's the topmost activity in the task - should become paused now
1741 return true;
1742 }
1743 // Check if activity above is finishing now and this one becomes the topmost in task.
1744 final ActivityRecord activityAbove = task.mActivities.get(positionInTask + 1);
1745 if (activityAbove.finishing && results == null) {
1746 // We will only allow pausing if activity above wasn't launched for result. Otherwise it
1747 // will cause this activity to resume before getting result.
1748 return true;
1749 }
1750 return false;
1751 }
1752
Andrii Kulian21713ac2016-10-12 22:05:05 -07001753 boolean handleAlreadyVisible() {
1754 stopFreezingScreenLocked(false);
1755 try {
1756 if (returningOptions != null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001757 app.getThread().scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001758 }
1759 } catch(RemoteException e) {
1760 }
Bryce Lee7ace3952018-02-16 14:34:32 -08001761 return mState == RESUMED;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001762 }
1763
1764 static void activityResumedLocked(IBinder token) {
1765 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1766 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1767 if (r != null) {
1768 r.icicle = null;
1769 r.haveState = false;
1770 }
1771 }
1772
1773 /**
1774 * Once we know that we have asked an application to put an activity in the resumed state
1775 * (either by launching it or explicitly telling it), this function updates the rest of our
1776 * state to match that fact.
1777 */
1778 void completeResumeLocked() {
1779 final boolean wasVisible = visible;
Jorim Jaggi8d062052017-08-22 14:55:17 +02001780 setVisible(true);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001781 if (!wasVisible) {
1782 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1783 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1784 }
1785 idle = false;
1786 results = null;
1787 newIntents = null;
1788 stopped = false;
1789
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001790 if (isActivityTypeHome()) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001791 WindowProcessController app = task.mActivities.get(0).app;
1792 if (hasProcess() && app != service.mHomeProcess) {
1793 service.mHomeProcess = app;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001794 }
1795 }
1796
1797 if (nowVisible) {
1798 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1799 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001800 }
1801
1802 // Schedule an idle timeout in case the app doesn't do it for us.
1803 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1804
1805 mStackSupervisor.reportResumedActivityLocked(this);
1806
1807 resumeKeyDispatchingLocked();
1808 final ActivityStack stack = getStack();
Jorim Jaggifa9ed962018-01-25 00:16:49 +01001809 mStackSupervisor.mNoAnimActivities.clear();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001810
1811 // Mark the point when the activity is resuming
1812 // TODO: To be more accurate, the mark should be before the onCreate,
1813 // not after the onResume. But for subsequent starts, onResume is fine.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001814 if (hasProcess()) {
Wale Ogunwale86b74462018-07-02 08:42:43 -07001815 cpuTimeAtResume = app.getCpuTime();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001816 } else {
1817 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1818 }
1819
1820 returningOptions = null;
chaviw59b98852017-06-13 12:05:44 -07001821
1822 if (canTurnScreenOn()) {
1823 mStackSupervisor.wakeUp("turnScreenOnFlag");
1824 } else {
1825 // If the screen is going to turn on because the caller explicitly requested it and
1826 // the keyguard is not showing don't attempt to sleep. Otherwise the Activity will
1827 // pause and then resume again later, which will result in a double life-cycle event.
David Stevens9440dc82017-03-16 19:00:20 -07001828 stack.checkReadyForSleep();
chaviw59b98852017-06-13 12:05:44 -07001829 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001830 }
1831
1832 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1833 CharSequence description) {
1834 final ActivityStack stack = getStack();
Bryce Lee7ace3952018-02-16 14:34:32 -08001835 if (mState != STOPPING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001836 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
Ruben Brunkf53497c2017-03-27 20:26:17 -07001837 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001838 return;
1839 }
1840 if (newPersistentState != null) {
1841 persistentState = newPersistentState;
1842 service.notifyTaskPersisterLocked(task, false);
1843 }
1844 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001845
Andrii Kulian21713ac2016-10-12 22:05:05 -07001846 if (newIcicle != null) {
1847 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1848 // the state.
1849 icicle = newIcicle;
1850 haveState = true;
1851 launchCount = 0;
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001852 updateTaskDescription(description);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001853 }
1854 if (!stopped) {
1855 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
Ruben Brunkf53497c2017-03-27 20:26:17 -07001856 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001857 stopped = true;
Bryce Lee7ace3952018-02-16 14:34:32 -08001858 setState(STOPPED, "activityStoppedLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001859
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001860 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001861
Andrii Kulian21713ac2016-10-12 22:05:05 -07001862 if (finishing) {
1863 clearOptionsLocked();
1864 } else {
1865 if (deferRelaunchUntilPaused) {
1866 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
Andrii Kulianab132ee2018-07-24 22:10:21 +08001867 mStackSupervisor.resumeFocusedStacksTopActivitiesLocked();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001868 } else {
1869 mStackSupervisor.updatePreviousProcessLocked(this);
1870 }
1871 }
1872 }
1873 }
1874
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001875 void startLaunchTickingLocked() {
Jeff Sharkey5ab02432017-06-27 11:01:36 -06001876 if (Build.IS_USER) {
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001877 return;
1878 }
1879 if (launchTickTime == 0) {
1880 launchTickTime = SystemClock.uptimeMillis();
1881 continueLaunchTickingLocked();
1882 }
1883 }
1884
1885 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001886 if (launchTickTime == 0) {
1887 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001888 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001889
Andrii Kulian02b7a832016-10-06 23:11:56 -07001890 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001891 if (stack == null) {
1892 return false;
1893 }
1894
Ruben Brunkf53497c2017-03-27 20:26:17 -07001895 Message msg = stack.mHandler.obtainMessage(LAUNCH_TICK_MSG, this);
1896 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
1897 stack.mHandler.sendMessageDelayed(msg, LAUNCH_TICK);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001898 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001899 }
1900
1901 void finishLaunchTickingLocked() {
1902 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001903 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001904 if (stack != null) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001905 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001906 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001907 }
1908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 // IApplicationToken
1910
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001911 public boolean mayFreezeScreenLocked(WindowProcessController app) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 // Only freeze the screen if this activity is currently attached to
1913 // an application, and that application is not blocked or unresponding.
1914 // In any other case, we can't count on getting the screen unfrozen,
1915 // so it is best to leave as-is.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001916 return hasProcess() && !app.isCrashing() && !app.isNotResponding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001918
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001919 public void startFreezingScreenLocked(WindowProcessController app, int configChanges) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001921 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 public void stopFreezingScreenLocked(boolean force) {
1926 if (force || frozenBeforeDestroy) {
1927 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001928 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 }
1930 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001931
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001932 public void reportFullyDrawnLocked(boolean restoredFromBundle) {
Vishnu Nair132ee832018-09-28 15:00:05 -07001933 final WindowingModeTransitionInfoSnapshot info = mStackSupervisor
1934 .getActivityMetricsLogger().logAppTransitionReportedDrawn(this, restoredFromBundle);
1935 if (info != null) {
1936 mStackSupervisor.reportActivityLaunchedLocked(false /* timeout */, this,
1937 info.windowsFullyDrawnDelayMs);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001938 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001939 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001940 @Override
Sudheer Shankac766db02017-06-12 10:37:29 -07001941 public void onStartingWindowDrawn(long timestamp) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001942 synchronized (service.mGlobalLock) {
Bryce Lee2a3cc462017-10-27 10:57:35 -07001943 mStackSupervisor.getActivityMetricsLogger().notifyStartingWindowDrawn(
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +01001944 getWindowingMode(), timestamp);
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001945 }
1946 }
1947
1948 @Override
Vishnu Nair9ba31652018-11-13 14:34:05 -08001949 public void onWindowsDrawn(boolean drawn, long timestamp) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001950 synchronized (service.mGlobalLock) {
Vishnu Nair9ba31652018-11-13 14:34:05 -08001951 mDrawn = drawn;
1952 if (!drawn) {
1953 return;
1954 }
Vishnu Nair132ee832018-09-28 15:00:05 -07001955 final WindowingModeTransitionInfoSnapshot info = mStackSupervisor
1956 .getActivityMetricsLogger().notifyWindowsDrawn(getWindowingMode(), timestamp);
1957 final int windowsDrawnDelayMs = info != null ? info.windowsDrawnDelayMs : INVALID_DELAY;
1958 mStackSupervisor.reportActivityLaunchedLocked(false /* timeout */, this,
1959 windowsDrawnDelayMs);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001960 mStackSupervisor.sendWaitingVisibleReportLocked(this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001961 finishLaunchTickingLocked();
1962 if (task != null) {
1963 task.hasBeenVisible = true;
1964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966 }
1967
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001968 @Override
1969 public void onWindowsVisible() {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001970 synchronized (service.mGlobalLock) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001971 mStackSupervisor.reportActivityVisibleLocked(this);
1972 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
1973 if (!nowVisible) {
1974 nowVisible = true;
1975 lastVisibleTime = SystemClock.uptimeMillis();
Bryce Leeb7c9b802017-05-02 14:20:24 -07001976 if (idle || mStackSupervisor.isStoppingNoHistoryActivity()) {
1977 // If this activity was already idle or there is an activity that must be
1978 // stopped immediately after visible, then we now need to make sure we perform
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001979 // the full stop of any activities that are waiting to do so. This is because
1980 // we won't do that while they are still waiting for this one to become visible.
Bryce Lee4a194382017-04-04 14:32:48 -07001981 final int size = mStackSupervisor.mActivitiesWaitingForVisibleActivity.size();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001982 if (size > 0) {
1983 for (int i = 0; i < size; i++) {
Bryce Lee4a194382017-04-04 14:32:48 -07001984 final ActivityRecord r =
1985 mStackSupervisor.mActivitiesWaitingForVisibleActivity.get(i);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001986 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
1987 }
Bryce Lee4a194382017-04-04 14:32:48 -07001988 mStackSupervisor.mActivitiesWaitingForVisibleActivity.clear();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001989 mStackSupervisor.scheduleIdleLocked();
1990 }
Bryce Leeb7c9b802017-05-02 14:20:24 -07001991 } else {
1992 // Instead of doing the full stop routine here, let's just hide any activities
1993 // we now can, and let them stop when the normal idle happens.
1994 mStackSupervisor.processStoppingActivitiesLocked(null /* idleActivity */,
1995 false /* remove */, true /* processPausingActivities */);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001996 }
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001997 service.scheduleAppGcsLocked();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001998 }
1999 }
2000 }
2001
2002 @Override
2003 public void onWindowsGone() {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002004 synchronized (service.mGlobalLock) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002005 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
2006 nowVisible = false;
2007 }
2008 }
2009
2010 @Override
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002011 public boolean keyDispatchingTimedOut(String reason, int windowPid) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002012 ActivityRecord anrActivity;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002013 WindowProcessController anrApp;
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002014 boolean windowFromSameProcessAsActivity;
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002015 synchronized (service.mGlobalLock) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002016 anrActivity = getWaitingHistoryRecordLocked();
2017 anrApp = app;
Brian Carlstrom7b0f2e82017-03-31 00:24:18 -07002018 windowFromSameProcessAsActivity =
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002019 !hasProcess() || app.getPid() == windowPid || windowPid == -1;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002020 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07002021
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002022 if (windowFromSameProcessAsActivity) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07002023 return service.mAmInternal.inputDispatchingTimedOut(anrApp.mOwner,
2024 anrActivity.shortComponentName, anrActivity.appInfo, shortComponentName,
2025 app, false, reason);
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002026 } else {
2027 // In this case another process added windows using this activity token. So, we call the
2028 // generic service input dispatch timed out method so that the right process is blamed.
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07002029 return service.mAmInternal.inputDispatchingTimedOut(
2030 windowPid, false /* aboveSystem */, reason) < 0;
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002031 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002032 }
2033
2034 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08002035 // First find the real culprit... if this activity is waiting for
2036 // another activity to start or has stopped, then the key dispatching
2037 // timeout should not be caused by this.
Bryce Lee4a194382017-04-04 14:32:48 -07002038 if (mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this) || stopped) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07002039 final ActivityStack stack = mStackSupervisor.getTopDisplayFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08002040 // Try to use the one which is closest to top.
Bryce Leec4ab62a2018-03-05 14:19:26 -08002041 ActivityRecord r = stack.getResumedActivity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002043 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08002045 if (r != null) {
2046 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
2048 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08002049 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 }
2051
Chong Zhang87761972016-08-22 13:53:24 -07002052 /** Checks whether the activity should be shown for current user. */
2053 public boolean okToShowLocked() {
Bryce Lee8558ec72017-08-17 15:37:26 -07002054 // We cannot show activities when the device is locked and the application is not
2055 // encryption aware.
2056 if (!StorageManager.isUserKeyUnlocked(userId)
2057 && !info.applicationInfo.isEncryptionAware()) {
2058 return false;
2059 }
2060
Chong Zhang87761972016-08-22 13:53:24 -07002061 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
2062 || (mStackSupervisor.isCurrentProfileLocked(userId)
Wale Ogunwale86b74462018-07-02 08:42:43 -07002063 && service.mAmInternal.isUserRunning(userId, 0 /* flags */));
Chong Zhang87761972016-08-22 13:53:24 -07002064 }
2065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 /**
2067 * This method will return true if the activity is either visible, is becoming visible, is
2068 * currently pausing, or is resumed.
2069 */
2070 public boolean isInterestingToUserLocked() {
Bryce Lee7ace3952018-02-16 14:34:32 -08002071 return visible || nowVisible || mState == PAUSING || mState == RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002072 }
2073
Wale Ogunwale3e997362016-09-06 10:37:56 -07002074 void setSleeping(boolean _sleeping) {
2075 setSleeping(_sleeping, false);
2076 }
2077
2078 void setSleeping(boolean _sleeping, boolean force) {
2079 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002080 return;
2081 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002082 if (attachedToProcess()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002083 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002084 app.getThread().scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002085 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
2086 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002087 }
2088 sleeping = _sleeping;
2089 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002090 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002091 }
2092 }
2093 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08002094
Craig Mautnerd2328952013-03-05 12:46:26 -08002095 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002096 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08002097 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002098 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08002099 }
2100 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002101 final int activityNdx = task.mActivities.indexOf(r);
2102 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002103 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08002104 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002105 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08002106 }
2107
2108 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002109 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002110 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08002111 }
2112
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002113 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002114 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
2115 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002116 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08002117 }
2118 return null;
2119 }
2120
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002121 /**
Riddle Hsufd4a0502018-10-16 01:05:16 +08002122 * @return display id to which this record is attached,
2123 * {@link android.view.Display#INVALID_DISPLAY} if not attached.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002124 */
2125 int getDisplayId() {
2126 final ActivityStack stack = getStack();
2127 if (stack == null) {
Riddle Hsufd4a0502018-10-16 01:05:16 +08002128 return INVALID_DISPLAY;
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002129 }
2130 return stack.mDisplayId;
2131 }
2132
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002133 final boolean isDestroyable() {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002134 if (finishing || !hasProcess()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002135 // This would be redundant.
2136 return false;
2137 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002138 final ActivityStack stack = getStack();
Bryce Leec4ab62a2018-03-05 14:19:26 -08002139 if (stack == null || this == stack.getResumedActivity() || this == stack.mPausingActivity
Andrii Kulian02b7a832016-10-06 23:11:56 -07002140 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002141 // We're not ready for this kind of thing.
2142 return false;
2143 }
2144 if (visible) {
2145 // The user would notice this!
2146 return false;
2147 }
2148 return true;
2149 }
2150
Winson Chung3bad5cc02014-08-19 17:44:32 -07002151 private static String createImageFilename(long createTime, int taskId) {
2152 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Ruben Brunkf53497c2017-03-27 20:26:17 -07002153 IMAGE_EXTENSION;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002154 }
2155
Craig Mautner648f69b2014-09-18 14:16:26 -07002156 void setTaskDescription(TaskDescription _taskDescription) {
2157 Bitmap icon;
2158 if (_taskDescription.getIconFilename() == null &&
2159 (icon = _taskDescription.getIcon()) != null) {
2160 final String iconFilename = createImageFilename(createTime, task.taskId);
Winson Chungc8408b82017-01-25 17:58:56 -08002161 final File iconFile = new File(TaskPersister.getUserImagesDir(task.userId),
2162 iconFilename);
Suprabh Shukla23593142015-11-03 17:31:15 -08002163 final String iconFilePath = iconFile.getAbsolutePath();
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002164 service.getRecentTasks().saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08002165 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07002166 }
2167 taskDescription = _taskDescription;
2168 }
2169
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002170 void setVoiceSessionLocked(IVoiceInteractionSession session) {
2171 voiceSession = session;
2172 pendingVoiceInteractionStart = false;
2173 }
2174
2175 void clearVoiceSessionLocked() {
2176 voiceSession = null;
2177 pendingVoiceInteractionStart = false;
2178 }
2179
Jorim Jaggi02886a82016-12-06 09:10:06 -08002180 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch) {
Jorim Jaggi42befc62017-06-13 11:54:04 -07002181 showStartingWindow(prev, newTask, taskSwitch, false /* fromRecents */);
2182 }
2183
2184 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch,
2185 boolean fromRecents) {
Jorim Jaggi70176432017-01-18 12:52:13 +01002186 if (mWindowContainerController == null) {
2187 return;
2188 }
Wale Ogunwale19866e22017-04-19 06:05:13 -07002189 if (mTaskOverlay) {
2190 // We don't show starting window for overlay activities.
2191 return;
2192 }
Sunny Goyald85bed52018-09-25 12:01:01 -07002193 if (pendingOptions != null
2194 && pendingOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
2195 // Don't show starting window when using shared element transition.
2196 return;
2197 }
Wale Ogunwale19866e22017-04-19 06:05:13 -07002198
Wale Ogunwale3b232392016-05-13 15:37:13 -07002199 final CompatibilityInfo compatInfo =
Wale Ogunwale53783742018-09-16 10:21:51 -07002200 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002201 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
2202 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
Jorim Jaggibae01b12017-04-11 16:29:10 -07002203 prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning(),
Jorim Jaggi70aa4d12017-05-15 00:05:54 +02002204 allowTaskSnapshot(),
Bryce Lee7ace3952018-02-16 14:34:32 -08002205 mState.ordinal() >= RESUMED.ordinal() && mState.ordinal() <= STOPPED.ordinal(),
Jorim Jaggi42befc62017-06-13 11:54:04 -07002206 fromRecents);
Wale Ogunwale3b232392016-05-13 15:37:13 -07002207 if (shown) {
2208 mStartingWindowState = STARTING_WINDOW_SHOWN;
2209 }
2210 }
2211
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002212 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
Jorim Jaggicb956052017-05-09 16:27:24 +02002213 if (mStartingWindowState == STARTING_WINDOW_SHOWN && behindFullscreenActivity) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002214 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
2215 mStartingWindowState = STARTING_WINDOW_REMOVED;
Jorim Jaggi19be6052017-08-03 18:33:43 +02002216 mWindowContainerController.removeStartingWindow();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002217 }
2218 }
2219
2220 int getRequestedOrientation() {
2221 return mWindowContainerController.getOrientation();
2222 }
2223
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002224 void setRequestedOrientation(int requestedOrientation) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002225 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002226 final Configuration displayConfig =
2227 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
2228
2229 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
2230 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002231 if (config != null) {
2232 frozenBeforeDestroy = true;
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002233 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002234 false /* deferResume */, displayId)) {
Andrii Kulianab132ee2018-07-24 22:10:21 +08002235 mStackSupervisor.resumeFocusedStacksTopActivitiesLocked();
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002236 }
2237 }
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002238 service.getTaskChangeNotificationController().notifyActivityRequestedOrientationChanged(
Yorke Leebd54c2a2016-10-25 13:49:23 -07002239 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002240 }
2241
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01002242 void setDisablePreviewScreenshots(boolean disable) {
2243 mWindowContainerController.setDisablePreviewScreenshots(disable);
2244 }
2245
Bryce Leea163b762017-01-24 11:05:01 -08002246 /**
2247 * Set the last reported global configuration to the client. Should be called whenever a new
2248 * global configuration is sent to the client for this activity.
2249 */
2250 void setLastReportedGlobalConfiguration(@NonNull Configuration config) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002251 mLastReportedConfiguration.setGlobalConfiguration(config);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002252 }
2253
Bryce Leea163b762017-01-24 11:05:01 -08002254 /**
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002255 * Set the last reported configuration to the client. Should be called whenever
Bryce Leea163b762017-01-24 11:05:01 -08002256 * a new merged configuration is sent to the client for this activity.
2257 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002258 void setLastReportedConfiguration(@NonNull MergedConfiguration config) {
Bryce Lee8104e7a2017-08-17 09:16:03 -07002259 setLastReportedConfiguration(config.getGlobalConfiguration(),
2260 config.getOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08002261 }
2262
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002263 private void setLastReportedConfiguration(Configuration global, Configuration override) {
Bryce Lee8104e7a2017-08-17 09:16:03 -07002264 mLastReportedConfiguration.setConfiguration(global, override);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002265 }
2266
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002267 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002268 private void updateOverrideConfiguration() {
2269 mTmpConfig.unset();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002270 computeBounds(mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08002271
2272 if (mTmpBounds.equals(getOverrideBounds())) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002273 return;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002274 }
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002275
Bryce Leef3c6a472017-11-14 14:53:06 -08002276 setBounds(mTmpBounds);
2277
2278 final Rect updatedBounds = getOverrideBounds();
2279
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002280 // Bounds changed...update configuration to match.
Bryce Leef3c6a472017-11-14 14:53:06 -08002281 if (!matchParentBounds()) {
2282 task.computeOverrideConfiguration(mTmpConfig, updatedBounds, null /* insetBounds */,
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002283 false /* overrideWidth */, false /* overrideHeight */);
2284 }
Bryce Leef3c6a472017-11-14 14:53:06 -08002285
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002286 onOverrideConfigurationChanged(mTmpConfig);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002287 }
2288
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002289 /** Returns true if the configuration is compatible with this activity. */
Wale Ogunwale42f07d92017-05-01 21:32:58 -07002290 boolean isConfigurationCompatible(Configuration config) {
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002291 final int orientation = mWindowContainerController != null
2292 ? mWindowContainerController.getOrientation() : info.screenOrientation;
2293 if (isFixedOrientationPortrait(orientation)
2294 && config.orientation != ORIENTATION_PORTRAIT) {
2295 return false;
2296 }
2297 if (isFixedOrientationLandscape(orientation)
2298 && config.orientation != ORIENTATION_LANDSCAPE) {
2299 return false;
2300 }
2301 return true;
2302 }
2303
Bryce Lee7566d762017-03-30 09:34:15 -07002304 /**
2305 * Computes the bounds to fit the Activity within the bounds of the {@link Configuration}.
2306 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002307 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
2308 private void computeBounds(Rect outBounds) {
2309 outBounds.setEmpty();
2310 final float maxAspectRatio = info.maxAspectRatio;
2311 final ActivityStack stack = getStack();
Bryce Leef3c6a472017-11-14 14:53:06 -08002312 if (task == null || stack == null || task.inMultiWindowMode() || maxAspectRatio == 0
Bryce Leee5ab4502017-07-11 08:58:05 -07002313 || isInVrUiMode(getConfiguration())) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002314 // We don't set override configuration if that activity task isn't fullscreen. I.e. the
2315 // activity is in multi-window mode. Or, there isn't a max aspect ratio specified for
Bryce Leee5ab4502017-07-11 08:58:05 -07002316 // the activity. This is indicated by an empty {@link outBounds}. We also don't set it
2317 // if we are in VR mode.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002318 return;
2319 }
2320
Bryce Lee7566d762017-03-30 09:34:15 -07002321 // We must base this on the parent configuration, because we set our override
2322 // configuration's appBounds based on the result of this method. If we used our own
2323 // configuration, it would be influenced by past invocations.
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002324 final Rect appBounds = getParent().getWindowConfiguration().getAppBounds();
Wale Ogunwale822e5122017-07-26 06:02:24 -07002325 final int containingAppWidth = appBounds.width();
2326 final int containingAppHeight = appBounds.height();
Bryce Lee7566d762017-03-30 09:34:15 -07002327 int maxActivityWidth = containingAppWidth;
2328 int maxActivityHeight = containingAppHeight;
2329
2330 if (containingAppWidth < containingAppHeight) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002331 // Width is the shorter side, so we use that to figure-out what the max. height
2332 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002333 maxActivityHeight = (int) ((maxActivityWidth * maxAspectRatio) + 0.5f);
2334 } else {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002335 // Height is the shorter side, so we use that to figure-out what the max. width
2336 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002337 maxActivityWidth = (int) ((maxActivityHeight * maxAspectRatio) + 0.5f);
2338 }
2339
Bryce Lee7566d762017-03-30 09:34:15 -07002340 if (containingAppWidth <= maxActivityWidth && containingAppHeight <= maxActivityHeight) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002341 // The display matches or is less than the activity aspect ratio, so nothing else to do.
Bryce Lee7566d762017-03-30 09:34:15 -07002342 // Return the existing bounds. If this method is running for the first time,
Bryce Leef3c6a472017-11-14 14:53:06 -08002343 // {@link #getOverrideBounds()} will be empty (representing no override). If the method has run
2344 // before, then effect of {@link #getOverrideBounds()} will already have been applied to the
Bryce Lee7566d762017-03-30 09:34:15 -07002345 // value returned from {@link getConfiguration}. Refer to
2346 // {@link TaskRecord#computeOverrideConfiguration}.
Bryce Leef3c6a472017-11-14 14:53:06 -08002347 outBounds.set(getOverrideBounds());
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002348 return;
2349 }
2350
2351 // Compute configuration based on max supported width and height.
Adrian Roos24be34d2018-05-28 18:55:38 +02002352 // Also account for the left / top insets (e.g. from display cutouts), which will be clipped
2353 // away later in StackWindowController.adjustConfigurationForBounds(). Otherwise, the app
2354 // bounds would end up too small.
2355 outBounds.set(0, 0, maxActivityWidth + appBounds.left, maxActivityHeight + appBounds.top);
2356
Tiger Huang7c610aa2018-10-27 00:01:01 +08002357 if (service.mWindowManager.getNavBarPosition(getDisplayId()) == NAV_BAR_LEFT) {
Adrian Roos24be34d2018-05-28 18:55:38 +02002358 // Position the activity frame on the opposite side of the nav bar.
2359 outBounds.left = appBounds.right - maxActivityWidth;
2360 outBounds.right = appBounds.right;
2361 }
Andrii Kulian3a1619d2017-07-07 14:38:09 -07002362 }
2363
Riddle Hsu16567132018-08-16 21:37:47 +08002364 /**
2365 * @return {@code true} if this activity was reparented to another display but
2366 * {@link #ensureActivityConfiguration} is not called.
2367 */
2368 boolean shouldUpdateConfigForDisplayChanged() {
2369 return mLastReportedDisplayId != getDisplayId();
2370 }
2371
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002372 boolean ensureActivityConfiguration(int globalChanges, boolean preserveWindow) {
2373 return ensureActivityConfiguration(globalChanges, preserveWindow,
2374 false /* ignoreStopState */);
2375 }
2376
Andrii Kulian21713ac2016-10-12 22:05:05 -07002377 /**
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002378 * Make sure the given activity matches the current configuration. Ensures the HistoryRecord
2379 * is updated with the correct configuration and all other bookkeeping is handled.
2380 *
2381 * @param globalChanges The changes to the global configuration.
2382 * @param preserveWindow If the activity window should be preserved on screen if the activity
2383 * is relaunched.
2384 * @param ignoreStopState If we should try to relaunch the activity even if it is in the stopped
2385 * state. This is useful for the case where we know the activity will be
2386 * visible soon and we want to ensure its configuration before we make it
2387 * visible.
2388 * @return True if the activity was relaunched and false if it wasn't relaunched because we
2389 * can't or the app handles the specific configuration that is changing.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002390 */
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002391 boolean ensureActivityConfiguration(int globalChanges, boolean preserveWindow,
2392 boolean ignoreStopState) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002393 final ActivityStack stack = getStack();
2394 if (stack.mConfigWillChange) {
2395 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2396 "Skipping config check (will change): " + this);
2397 return true;
2398 }
2399
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002400 // We don't worry about activities that are finishing.
2401 if (finishing) {
2402 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2403 "Configuration doesn't matter in finishing " + this);
2404 stopFreezingScreenLocked(false);
2405 return true;
2406 }
2407
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002408 if (!ignoreStopState && (mState == STOPPING || mState == STOPPED)) {
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002409 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2410 "Skipping config check stopped or stopping: " + this);
2411 return true;
2412 }
2413
Wale Ogunwaleea3d3fd2017-05-01 07:41:08 -07002414 // TODO: We should add ActivityRecord.shouldBeVisible() that checks if the activity should
2415 // be visible based on the stack, task, and lockscreen state and use that here instead. The
2416 // method should be based on the logic in ActivityStack.ensureActivitiesVisibleLocked().
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002417 // Skip updating configuration for activity is a stack that shouldn't be visible.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002418 if (!stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002419 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2420 "Skipping config check invisible stack: " + this);
2421 return true;
2422 }
2423
Andrii Kulian21713ac2016-10-12 22:05:05 -07002424 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2425 "Ensuring correct configuration: " + this);
2426
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002427 final int newDisplayId = getDisplayId();
2428 final boolean displayChanged = mLastReportedDisplayId != newDisplayId;
2429 if (displayChanged) {
2430 mLastReportedDisplayId = newDisplayId;
2431 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002432 // TODO(b/36505427): Is there a better place to do this?
2433 updateOverrideConfiguration();
2434
Winson Chungbdc646f2017-02-13 12:12:22 -08002435 // Short circuit: if the two full configurations are equal (the common case), then there is
2436 // nothing to do. We test the full configuration instead of the global and merged override
2437 // configurations because there are cases (like moving a task to the pinned stack) where
2438 // the combine configurations are equal, but would otherwise differ in the override config
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002439 mTmpConfig.setTo(mLastReportedConfiguration.getMergedConfiguration());
2440 if (getConfiguration().equals(mTmpConfig) && !forceNewConfig && !displayChanged) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002441 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002442 "Configuration & display unchanged in " + this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002443 return true;
2444 }
2445
2446 // Okay we now are going to make this activity have the new config.
2447 // But then we need to figure out how it needs to deal with that.
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002448
2449 // Find changes between last reported merged configuration and the current one. This is used
2450 // to decide whether to relaunch an activity or just report a configuration change.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002451 final int changes = getConfigurationChanges(mTmpConfig);
Ruben Brunkf64af332017-03-22 22:03:25 -07002452
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002453 // Update last reported values.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002454 final Configuration newMergedOverrideConfig = getMergedOverrideConfiguration();
Bryce Lee8104e7a2017-08-17 09:16:03 -07002455
Wale Ogunwalef6733932018-06-27 05:14:34 -07002456 setLastReportedConfiguration(service.getGlobalConfiguration(), newMergedOverrideConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002457
Bryce Lee7ace3952018-02-16 14:34:32 -08002458 if (mState == INITIALIZING) {
Andrii Kulianb372da62018-01-18 10:46:24 -08002459 // No need to relaunch or schedule new config for activity that hasn't been launched
2460 // yet. We do, however, return after applying the config to activity record, so that
2461 // it will use it for launch transaction.
2462 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2463 "Skipping config check for initializing activity: " + this);
2464 return true;
2465 }
2466
Andrii Kulian21713ac2016-10-12 22:05:05 -07002467 if (changes == 0 && !forceNewConfig) {
2468 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2469 "Configuration no differences in " + this);
2470 // There are no significant differences, so we won't relaunch but should still deliver
2471 // the new configuration to the client process.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002472 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002473 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002474 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002475 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002476 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002477 return true;
2478 }
2479
2480 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002481 "Configuration changes for " + this + ", allChanges="
Andrii Kulian21713ac2016-10-12 22:05:05 -07002482 + Configuration.configurationDiffToString(changes));
2483
2484 // If the activity isn't currently running, just leave the new configuration and it will
2485 // pick that up next time it starts.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002486 if (!attachedToProcess()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002487 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2488 "Configuration doesn't matter not running " + this);
2489 stopFreezingScreenLocked(false);
2490 forceNewConfig = false;
2491 return true;
2492 }
2493
2494 // Figure out how to handle the changes between the configurations.
2495 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2496 "Checking to restart " + info.name + ": changed=0x"
2497 + Integer.toHexString(changes) + ", handles=0x"
2498 + Integer.toHexString(info.getRealConfigChanged())
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002499 + ", mLastReportedConfiguration=" + mLastReportedConfiguration);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002500
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002501 if (shouldRelaunchLocked(changes, mTmpConfig) || forceNewConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002502 // Aha, the activity isn't handling the change, so DIE DIE DIE.
2503 configChangeFlags |= changes;
2504 startFreezingScreenLocked(app, globalChanges);
2505 forceNewConfig = false;
2506 preserveWindow &= isResizeOnlyChange(changes);
Garfield Tan2746ab52018-07-25 12:33:01 -07002507 final boolean hasResizeChange = hasResizeChange(changes & ~info.getRealConfigChanged());
2508 if (hasResizeChange) {
2509 final boolean isDragResizing =
2510 getTask().getWindowContainerController().isDragResizing();
2511 mRelaunchReason = isDragResizing ? RELAUNCH_REASON_FREE_RESIZE
2512 : RELAUNCH_REASON_WINDOWING_MODE_RESIZE;
2513 } else {
2514 mRelaunchReason = RELAUNCH_REASON_NONE;
2515 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002516 if (!attachedToProcess()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002517 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2518 "Config is destroying non-running " + this);
2519 stack.destroyActivityLocked(this, true, "config");
Bryce Lee7ace3952018-02-16 14:34:32 -08002520 } else if (mState == PAUSING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002521 // A little annoying: we are waiting for this activity to finish pausing. Let's not
2522 // do anything now, but just flag that it needs to be restarted when done pausing.
2523 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2524 "Config is skipping already pausing " + this);
2525 deferRelaunchUntilPaused = true;
2526 preserveWindowOnDeferredRelaunch = preserveWindow;
2527 return true;
Bryce Lee7ace3952018-02-16 14:34:32 -08002528 } else if (mState == RESUMED) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002529 // Try to optimize this case: the configuration is changing and we need to restart
2530 // the top, resumed activity. Instead of doing the normal handshaking, just say
2531 // "restart!".
2532 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2533 "Config is relaunching resumed " + this);
2534
2535 if (DEBUG_STATES && !visible) {
2536 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
2537 + " called by " + Debug.getCallers(4));
2538 }
2539
2540 relaunchActivityLocked(true /* andResume */, preserveWindow);
2541 } else {
2542 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2543 "Config is relaunching non-resumed " + this);
2544 relaunchActivityLocked(false /* andResume */, preserveWindow);
2545 }
2546
2547 // All done... tell the caller we weren't able to keep this activity around.
2548 return false;
2549 }
2550
2551 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002552 // NOTE: We only forward the override configuration as the system level configuration
Andrii Kulian21713ac2016-10-12 22:05:05 -07002553 // changes is always sent to all processes when they happen so it can just use whatever
2554 // system level configuration it last got.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002555 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002556 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002557 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002558 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002559 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002560 stopFreezingScreenLocked(false);
2561
2562 return true;
2563 }
2564
Zak Cohen90e7116742017-01-29 12:59:23 -08002565 /**
2566 * When assessing a configuration change, decide if the changes flags and the new configurations
2567 * should cause the Activity to relaunch.
Ruben Brunkf64af332017-03-22 22:03:25 -07002568 *
2569 * @param changes the changes due to the given configuration.
2570 * @param changesConfig the configuration that was used to calculate the given changes via a
2571 * call to getConfigurationChanges.
Zak Cohen90e7116742017-01-29 12:59:23 -08002572 */
Ruben Brunkf64af332017-03-22 22:03:25 -07002573 private boolean shouldRelaunchLocked(int changes, Configuration changesConfig) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002574 int configChanged = info.getRealConfigChanged();
Ruben Brunkf64af332017-03-22 22:03:25 -07002575 boolean onlyVrUiModeChanged = onlyVrUiModeChanged(changes, changesConfig);
Zak Cohen90e7116742017-01-29 12:59:23 -08002576
2577 // Override for apps targeting pre-O sdks
2578 // If a device is in VR mode, and we're transitioning into VR ui mode, add ignore ui mode
2579 // to the config change.
2580 // For O and later, apps will be required to add configChanges="uimode" to their manifest.
2581 if (appInfo.targetSdkVersion < O
2582 && requestedVrComponent != null
Ruben Brunkf64af332017-03-22 22:03:25 -07002583 && onlyVrUiModeChanged) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002584 configChanged |= CONFIG_UI_MODE;
2585 }
2586
2587 return (changes&(~configChanged)) != 0;
2588 }
2589
Ruben Brunkf64af332017-03-22 22:03:25 -07002590 /**
2591 * Returns true if the configuration change is solely due to the UI mode switching into or out
2592 * of UI_MODE_TYPE_VR_HEADSET.
2593 */
2594 private boolean onlyVrUiModeChanged(int changes, Configuration lastReportedConfig) {
2595 final Configuration currentConfig = getConfiguration();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002596 return changes == CONFIG_UI_MODE && (isInVrUiMode(currentConfig)
Ruben Brunkf64af332017-03-22 22:03:25 -07002597 != isInVrUiMode(lastReportedConfig));
2598 }
2599
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002600 private int getConfigurationChanges(Configuration lastReportedConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002601 // Determine what has changed. May be nothing, if this is a config that has come back from
2602 // the app after going idle. In that case we just want to leave the official config object
2603 // now in the activity and do nothing else.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002604 final Configuration currentConfig = getConfiguration();
2605 int changes = lastReportedConfig.diff(currentConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002606 // We don't want to use size changes if they don't cross boundaries that are important to
2607 // the app.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002608 if ((changes & CONFIG_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002609 final boolean crosses = crossesHorizontalSizeThreshold(lastReportedConfig.screenWidthDp,
2610 currentConfig.screenWidthDp)
2611 || crossesVerticalSizeThreshold(lastReportedConfig.screenHeightDp,
2612 currentConfig.screenHeightDp);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002613 if (!crosses) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002614 changes &= ~CONFIG_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002615 }
2616 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002617 if ((changes & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002618 final int oldSmallest = lastReportedConfig.smallestScreenWidthDp;
2619 final int newSmallest = currentConfig.smallestScreenWidthDp;
2620 if (!crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002621 changes &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002622 }
2623 }
Wale Ogunwale822e5122017-07-26 06:02:24 -07002624 // We don't want window configuration to cause relaunches.
2625 if ((changes & CONFIG_WINDOW_CONFIGURATION) != 0) {
2626 changes &= ~CONFIG_WINDOW_CONFIGURATION;
2627 }
Bryce Lee600dadd2017-07-25 10:48:42 -07002628
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002629 return changes;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002630 }
2631
2632 private static boolean isResizeOnlyChange(int change) {
2633 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2634 | CONFIG_SCREEN_LAYOUT)) == 0;
2635 }
2636
Garfield Tan2746ab52018-07-25 12:33:01 -07002637 private static boolean hasResizeChange(int change) {
2638 return (change & (CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2639 | CONFIG_SCREEN_LAYOUT)) != 0;
2640 }
2641
Andrii Kulian21713ac2016-10-12 22:05:05 -07002642 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002643 if (service.mSuppressResizeConfigChanges && preserveWindow) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002644 configChangeFlags = 0;
2645 return;
2646 }
2647
2648 List<ResultInfo> pendingResults = null;
2649 List<ReferrerIntent> pendingNewIntents = null;
2650 if (andResume) {
2651 pendingResults = results;
2652 pendingNewIntents = newIntents;
2653 }
2654 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2655 "Relaunching: " + this + " with results=" + pendingResults
2656 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2657 + " preserveWindow=" + preserveWindow);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002658 EventLog.writeEvent(andResume ? AM_RELAUNCH_RESUME_ACTIVITY
2659 : AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
Andrii Kulian21713ac2016-10-12 22:05:05 -07002660 task.taskId, shortComponentName);
2661
2662 startFreezingScreenLocked(app, 0);
2663
Andrii Kulian21713ac2016-10-12 22:05:05 -07002664 try {
2665 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2666 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2667 + " callers=" + Debug.getCallers(6));
2668 forceNewConfig = false;
2669 mStackSupervisor.activityRelaunchingLocked(this);
Andrii Kulianb372da62018-01-18 10:46:24 -08002670 final ClientTransactionItem callbackItem = ActivityRelaunchItem.obtain(pendingResults,
2671 pendingNewIntents, configChangeFlags,
Wale Ogunwalef6733932018-06-27 05:14:34 -07002672 new MergedConfiguration(service.getGlobalConfiguration(),
Andrii Kulianb372da62018-01-18 10:46:24 -08002673 getMergedOverrideConfiguration()),
2674 preserveWindow);
2675 final ActivityLifecycleItem lifecycleItem;
2676 if (andResume) {
lumark588a3e82018-07-20 18:53:54 +08002677 lifecycleItem = ResumeActivityItem.obtain(
2678 getDisplay().getWindowContainerController().isNextTransitionForward());
Andrii Kulianb372da62018-01-18 10:46:24 -08002679 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -07002680 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulianb372da62018-01-18 10:46:24 -08002681 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002682 final ClientTransaction transaction = ClientTransaction.obtain(app.getThread(), appToken);
Andrii Kulianb372da62018-01-18 10:46:24 -08002683 transaction.addCallback(callbackItem);
2684 transaction.setLifecycleStateRequest(lifecycleItem);
Bryce Leeb0f993f2018-03-02 15:38:01 -08002685 service.getLifecycleManager().scheduleTransaction(transaction);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002686 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
Andrii Kulianb372da62018-01-18 10:46:24 -08002687 // request resume if this activity is currently resumed, which implies we aren't
Andrii Kulian21713ac2016-10-12 22:05:05 -07002688 // sleeping.
2689 } catch (RemoteException e) {
2690 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2691 }
2692
2693 if (andResume) {
2694 if (DEBUG_STATES) {
2695 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2696 }
2697 results = null;
2698 newIntents = null;
Wale Ogunwale008163e2018-07-23 23:11:08 -07002699 service.getAppWarningsLocked().onResumeActivity(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002700 } else {
Wale Ogunwale008163e2018-07-23 23:11:08 -07002701 final ActivityStack stack = getStack();
2702 if (stack != null) {
2703 stack.mHandler.removeMessages(PAUSE_TIMEOUT_MSG, this);
2704 }
Bryce Lee7ace3952018-02-16 14:34:32 -08002705 setState(PAUSED, "relaunchActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07002706 }
2707
2708 configChangeFlags = 0;
2709 deferRelaunchUntilPaused = false;
2710 preserveWindowOnDeferredRelaunch = false;
2711 }
2712
Jorim Jaggibae01b12017-04-11 16:29:10 -07002713 private boolean isProcessRunning() {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002714 WindowProcessController proc = app;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002715 if (proc == null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002716 proc = service.mProcessNames.get(processName, info.applicationInfo.uid);
Jorim Jaggi02886a82016-12-06 09:10:06 -08002717 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002718 return proc != null && proc.hasThread();
Jorim Jaggi02886a82016-12-06 09:10:06 -08002719 }
2720
Jorim Jaggibae01b12017-04-11 16:29:10 -07002721 /**
2722 * @return Whether a task snapshot starting window may be shown.
2723 */
2724 private boolean allowTaskSnapshot() {
2725 if (newIntents == null) {
2726 return true;
2727 }
2728
2729 // Restrict task snapshot starting window to launcher start, or there is no intent at all
2730 // (eg. task being brought to front). If the intent is something else, likely the app is
2731 // going to show some specific page or view, instead of what's left last time.
2732 for (int i = newIntents.size() - 1; i >= 0; i--) {
2733 final Intent intent = newIntents.get(i);
2734 if (intent != null && !ActivityRecord.isMainIntent(intent)) {
2735 return false;
2736 }
2737 }
2738 return true;
2739 }
2740
Bryce Leeb7c9b802017-05-02 14:20:24 -07002741 /**
2742 * Returns {@code true} if the associated activity has the no history flag set on it.
2743 * {@code false} otherwise.
2744 */
2745 boolean isNoHistory() {
2746 return (intent.getFlags() & FLAG_ACTIVITY_NO_HISTORY) != 0
2747 || (info.flags & FLAG_NO_HISTORY) != 0;
2748 }
2749
Craig Mautner21d24a22014-04-23 11:45:37 -07002750 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2751 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2752 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2753 if (launchedFromPackage != null) {
2754 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2755 }
2756 if (resolvedType != null) {
2757 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2758 }
2759 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2760 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002761
Craig Mautner21d24a22014-04-23 11:45:37 -07002762 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002763 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002764 }
2765
2766 out.startTag(null, TAG_INTENT);
2767 intent.saveToXml(out);
2768 out.endTag(null, TAG_INTENT);
2769
2770 if (isPersistable() && persistentState != null) {
2771 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2772 persistentState.saveToXml(out);
2773 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2774 }
2775 }
2776
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002777 static ActivityRecord restoreFromXml(XmlPullParser in,
2778 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002779 Intent intent = null;
2780 PersistableBundle persistentState = null;
2781 int launchedFromUid = 0;
2782 String launchedFromPackage = null;
2783 String resolvedType = null;
2784 boolean componentSpecified = false;
2785 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002786 long createTime = -1;
2787 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002788 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002789
2790 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2791 final String attrName = in.getAttributeName(attrNdx);
2792 final String attrValue = in.getAttributeValue(attrNdx);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002793 if (DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002794 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002795 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002796 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002797 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002798 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002799 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2800 launchedFromPackage = attrValue;
2801 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2802 resolvedType = attrValue;
2803 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002804 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002805 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002806 userId = Integer.parseInt(attrValue);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002807 } else if (attrName.startsWith(ATTR_TASKDESCRIPTION_PREFIX)) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002808 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002809 } else {
2810 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2811 }
2812 }
2813
2814 int event;
Ruben Brunkf53497c2017-03-27 20:26:17 -07002815 while (((event = in.next()) != END_DOCUMENT) &&
2816 (event != END_TAG || in.getDepth() >= outerDepth)) {
2817 if (event == START_TAG) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002818 final String name = in.getName();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002819 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002820 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002821 if (TAG_INTENT.equals(name)) {
2822 intent = Intent.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002823 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002824 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002825 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2826 persistentState = PersistableBundle.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002827 if (DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002828 "ActivityRecord: persistentState=" + persistentState);
2829 } else {
2830 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2831 XmlUtils.skipCurrentTag(in);
2832 }
2833 }
2834 }
2835
2836 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002837 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002838 }
2839
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002840 final ActivityTaskManagerService service = stackSupervisor.mService;
Craig Mautner21d24a22014-04-23 11:45:37 -07002841 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Patrick Baumann78380272018-04-04 10:41:01 -07002842 userId, Binder.getCallingUid());
Craig Mautnere0129b32014-05-25 16:41:09 -07002843 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002844 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2845 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002846 }
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002847 final ActivityRecord r = new ActivityRecord(service, null /* caller */,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002848 0 /* launchedFromPid */, launchedFromUid, launchedFromPackage, intent, resolvedType,
Wale Ogunwalef6733932018-06-27 05:14:34 -07002849 aInfo, service.getConfiguration(), null /* resultTo */, null /* resultWho */,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002850 0 /* reqCode */, componentSpecified, false /* rootVoiceInteraction */,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002851 stackSupervisor, null /* options */, null /* sourceRecord */);
Craig Mautner21d24a22014-04-23 11:45:37 -07002852
2853 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002854 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002855 r.createTime = createTime;
2856
2857 return r;
2858 }
2859
Zak Cohen90e7116742017-01-29 12:59:23 -08002860 private static boolean isInVrUiMode(Configuration config) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002861 return (config.uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_VR_HEADSET;
Zak Cohen90e7116742017-01-29 12:59:23 -08002862 }
2863
David Stevens82ea6cb2017-03-03 16:18:50 -08002864 int getUid() {
2865 return info.applicationInfo.uid;
2866 }
2867
chaviw59b98852017-06-13 12:05:44 -07002868 void setShowWhenLocked(boolean showWhenLocked) {
2869 mShowWhenLocked = showWhenLocked;
Kevin Chyn44639482017-10-09 18:34:41 -07002870 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0 /* configChanges */,
2871 false /* preserveWindows */);
chaviw59b98852017-06-13 12:05:44 -07002872 }
2873
2874 /**
chaviw2c500982018-01-04 17:05:05 -08002875 * @return true if the activity windowing mode is not
2876 * {@link android.app.WindowConfiguration#WINDOWING_MODE_PINNED} and activity contains
2877 * windows that have {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set or if the activity
2878 * has set {@link #mShowWhenLocked}.
2879 * Multi-windowing mode will be exited if true is returned.
chaviw59b98852017-06-13 12:05:44 -07002880 */
2881 boolean canShowWhenLocked() {
chaviw2c500982018-01-04 17:05:05 -08002882 return !inPinnedWindowingMode() && (mShowWhenLocked
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002883 || service.mWindowManager.containsShowWhenLockedWindow(appToken));
chaviw59b98852017-06-13 12:05:44 -07002884 }
2885
2886 void setTurnScreenOn(boolean turnScreenOn) {
2887 mTurnScreenOn = turnScreenOn;
2888 }
2889
2890 /**
2891 * Determines whether this ActivityRecord can turn the screen on. It checks whether the flag
2892 * {@link #mTurnScreenOn} is set and checks whether the ActivityRecord should be visible
2893 * depending on Keyguard state
2894 *
2895 * @return true if the screen can be turned on, false otherwise.
2896 */
2897 boolean canTurnScreenOn() {
2898 final ActivityStack stack = getStack();
2899 return mTurnScreenOn && stack != null &&
2900 stack.checkKeyguardVisibility(this, true /* shouldBeVisible */, true /* isTop */);
2901 }
2902
2903 boolean getTurnScreenOnFlag() {
2904 return mTurnScreenOn;
2905 }
2906
2907 boolean isTopRunningActivity() {
2908 return mStackSupervisor.topRunningActivityLocked() == this;
2909 }
2910
Andrii Kulian52d255c2018-07-13 11:32:19 -07002911 /**
2912 * @return {@code true} if this is the resumed activity on its current display, {@code false}
2913 * otherwise.
2914 */
2915 boolean isResumedActivityOnDisplay() {
2916 final ActivityDisplay display = getDisplay();
2917 return display != null && this == display.getResumedActivity();
2918 }
2919
Jorim Jaggif84e2f62018-01-16 14:17:59 +01002920 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
2921 mWindowContainerController.registerRemoteAnimations(definition);
2922 }
2923
Craig Mautnerf81b90872013-02-26 13:02:43 -08002924 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002926 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002927 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002928 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002929 }
2930 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002931 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002932 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002933 sb.append(" u");
2934 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002935 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002936 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002937 stringName = sb.toString();
2938 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07002940
2941 void writeIdentifierToProto(ProtoOutputStream proto, long fieldId) {
2942 final long token = proto.start(fieldId);
2943 proto.write(HASH_CODE, System.identityHashCode(this));
2944 proto.write(USER_ID, userId);
2945 proto.write(TITLE, intent.getComponent().flattenToShortString());
2946 proto.end(token);
2947 }
2948
2949 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2950 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002951 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07002952 writeIdentifierToProto(proto, IDENTIFIER);
Bryce Lee7ace3952018-02-16 14:34:32 -08002953 proto.write(STATE, mState.toString());
Steven Timotius4346f0a2017-09-12 11:07:21 -07002954 proto.write(VISIBLE, visible);
2955 proto.write(FRONT_OF_TASK, frontOfTask);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002956 if (hasProcess()) {
2957 proto.write(PROC_ID, app.getPid());
Steven Timotius4346f0a2017-09-12 11:07:21 -07002958 }
Wale Ogunwale30eab1f2018-05-24 18:25:25 -07002959 proto.write(TRANSLUCENT, !fullscreen);
Steven Timotius4346f0a2017-09-12 11:07:21 -07002960 proto.end(token);
2961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962}