blob: 23f812559566b4c120ba953c3edd557ee52eb63f [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;
Winson Chung59fda9e2017-01-20 16:14:51 -080023import static android.app.AppOpsManager.MODE_ALLOWED;
Winson Chungf4ac0632017-03-17 12:34:12 -070024import static android.app.AppOpsManager.OP_PICTURE_IN_PICTURE;
Vishnu Nair132ee832018-09-28 15:00:05 -070025import static android.app.WaitResult.INVALID_DELAY;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070026import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
27import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
28import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070029import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
30import static android.app.WindowConfiguration.activityTypeToString;
Ruben Brunkf53497c2017-03-27 20:26:17 -070031import static android.content.Intent.ACTION_MAIN;
32import static android.content.Intent.CATEGORY_HOME;
33import static android.content.Intent.CATEGORY_LAUNCHER;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080034import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Bryce Leeb7c9b802017-05-02 14:20:24 -070035import static android.content.Intent.FLAG_ACTIVITY_NO_HISTORY;
Andrii Kulian21713ac2016-10-12 22:05:05 -070036import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
37import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
38import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
39import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Zak Cohen90e7116742017-01-29 12:59:23 -080040import static android.content.pm.ActivityInfo.CONFIG_UI_MODE;
Wale Ogunwale822e5122017-07-26 06:02:24 -070041import static android.content.pm.ActivityInfo.CONFIG_WINDOW_CONFIGURATION;
Jorim Jaggi02886a82016-12-06 09:10:06 -080042import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080043import static android.content.pm.ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
44import static android.content.pm.ActivityInfo.FLAG_IMMERSIVE;
45import static android.content.pm.ActivityInfo.FLAG_MULTIPROCESS;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020046import static android.content.pm.ActivityInfo.FLAG_NO_HISTORY;
Chong Zhang87761972016-08-22 13:53:24 -070047import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020048import static android.content.pm.ActivityInfo.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080049import static android.content.pm.ActivityInfo.FLAG_STATE_NOT_NEEDED;
chaviw59b98852017-06-13 12:05:44 -070050import static android.content.pm.ActivityInfo.FLAG_TURN_SCREEN_ON;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080051import static android.content.pm.ActivityInfo.LAUNCH_MULTIPLE;
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -080052import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
53import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080054import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Charles He2bf28322017-10-12 22:24:49 +010055import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
56import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
57import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
58import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
Ruben Brunkf53497c2017-03-27 20:26:17 -070059import static android.content.pm.ActivityInfo.PERSIST_ACROSS_REBOOTS;
60import static android.content.pm.ActivityInfo.PERSIST_ROOT_ONLY;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070061import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggicd13d332016-04-27 15:40:20 -070062import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwale72a73e32016-10-13 12:16:39 -070063import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070064import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -070065import static android.content.pm.ActivityInfo.isFixedOrientationLandscape;
66import static android.content.pm.ActivityInfo.isFixedOrientationPortrait;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -070067import static android.content.res.Configuration.EMPTY;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -070068import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
69import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Ruben Brunkf53497c2017-03-27 20:26:17 -070070import static android.content.res.Configuration.UI_MODE_TYPE_MASK;
Zak Cohen90e7116742017-01-29 12:59:23 -080071import static android.content.res.Configuration.UI_MODE_TYPE_VR_HEADSET;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080072import static android.os.Build.VERSION_CODES.HONEYCOMB;
Zak Cohen90e7116742017-01-29 12:59:23 -080073import static android.os.Build.VERSION_CODES.O;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080074import static android.os.Process.SYSTEM_UID;
Riddle Hsufd4a0502018-10-16 01:05:16 +080075import static android.view.Display.INVALID_DISPLAY;
Riddle Hsu16567132018-08-16 21:37:47 +080076import static android.view.WindowManagerPolicyConstants.NAV_BAR_LEFT;
77
Wale Ogunwale59507092018-10-29 09:00:30 -070078import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CONFIGURATION;
79import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_FOCUS;
80import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SAVED_STATE;
81import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STATES;
82import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
83import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_VISIBILITY;
84import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CONFIGURATION;
85import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_FOCUS;
86import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_SAVED_STATE;
87import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_STATES;
88import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_SWITCH;
89import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_VISIBILITY;
90import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
91import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
Riddle Hsu16567132018-08-16 21:37:47 +080092import static com.android.server.am.ActivityRecordProto.CONFIGURATION_CONTAINER;
93import static com.android.server.am.ActivityRecordProto.FRONT_OF_TASK;
94import static com.android.server.am.ActivityRecordProto.IDENTIFIER;
95import static com.android.server.am.ActivityRecordProto.PROC_ID;
96import static com.android.server.am.ActivityRecordProto.STATE;
97import static com.android.server.am.ActivityRecordProto.TRANSLUCENT;
98import static com.android.server.am.ActivityRecordProto.VISIBLE;
Wale Ogunwale59507092018-10-29 09:00:30 -070099import static com.android.server.wm.ActivityStack.ActivityState.INITIALIZING;
100import static com.android.server.wm.ActivityStack.ActivityState.PAUSED;
101import static com.android.server.wm.ActivityStack.ActivityState.PAUSING;
102import static com.android.server.wm.ActivityStack.ActivityState.RESUMED;
103import static com.android.server.wm.ActivityStack.ActivityState.STOPPED;
104import static com.android.server.wm.ActivityStack.ActivityState.STOPPING;
105import static com.android.server.wm.ActivityStack.LAUNCH_TICK;
106import static com.android.server.wm.ActivityStack.LAUNCH_TICK_MSG;
107import static com.android.server.wm.ActivityStack.PAUSE_TIMEOUT_MSG;
108import static com.android.server.wm.ActivityStack.STOP_TIMEOUT_MSG;
109import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_FREE_RESIZE;
110import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_NONE;
111import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_WINDOWING_MODE_RESIZE;
Ruben Brunkf53497c2017-03-27 20:26:17 -0700112import static com.android.server.am.EventLogTags.AM_RELAUNCH_ACTIVITY;
113import static com.android.server.am.EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY;
Wale Ogunwale59507092018-10-29 09:00:30 -0700114import static com.android.server.wm.TaskPersister.DEBUG;
115import static com.android.server.wm.TaskPersister.IMAGE_EXTENSION;
Wale Ogunwale98875612018-10-12 07:53:02 -0700116import static android.app.ActivityTaskManager.INVALID_TASK_ID;
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700117import static com.android.server.wm.IdentifierProto.HASH_CODE;
118import static com.android.server.wm.IdentifierProto.TITLE;
119import static com.android.server.wm.IdentifierProto.USER_ID;
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;
182import com.android.server.wm.ActivityMetricsLogger.WindowingModeTransitionInfoSnapshot;
183import com.android.server.wm.ActivityStack.ActivityState;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700184import com.android.server.uri.UriPermissionOwner;
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?
Andrii Kuliana39ae3e2018-05-31 12:43:54 -0700306 boolean mClientVisibilityDeferred;// was the visibility change message to client deferred?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 boolean idle; // has the activity gone idle?
308 boolean hasBeenLaunched;// has this activity ever been launched?
309 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400310 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400311 boolean forceNewConfig; // force re-create with new config next time
Winson Chungf7e03e12017-08-22 11:32:16 -0700312 boolean supportsEnterPipOnTaskSwitch; // This flag is set by the system to indicate that the
313 // activity can enter picture in picture while pausing (only when switching to another task)
Winson Chung709904f2017-04-25 11:00:48 -0700314 PictureInPictureParams pictureInPictureArgs = new PictureInPictureParams.Builder().build();
315 // The PiP params used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800316 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800317 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800318 ComponentName requestedVrComponent; // the requested component for handling VR mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700320 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700321
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700322 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700323 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800324
325 static final int STARTING_WINDOW_NOT_SHOWN = 0;
326 static final int STARTING_WINDOW_SHOWN = 1;
327 static final int STARTING_WINDOW_REMOVED = 2;
328 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700329 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800330
Garfield Tan2746ab52018-07-25 12:33:01 -0700331 // Marking the reason why this activity is being relaunched. Mainly used to track that this
332 // activity is being relaunched to fulfill a resize request due to compatibility issues, e.g. in
333 // pre-NYC apps that don't have a sense of being resized.
334 int mRelaunchReason = RELAUNCH_REASON_NONE;
335
Craig Mautner21d24a22014-04-23 11:45:37 -0700336 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700337 boolean mLaunchTaskBehind; // this activity is actively being launched with
338 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700339
Filip Gruszczynski23493322015-07-29 17:02:59 -0700340 // These configurations are collected from application's resources based on size-sensitive
341 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
342 // and drawable-sw400dp will be added to both as 400.
343 private int[] mVerticalSizeConfigurations;
344 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700345 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700346
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800347 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
348 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
349
Robert Carrd2265122016-08-05 10:25:21 -0700350 // A hint to override the window specified rotation animation, or -1
351 // to use the window specified value. We use this so that
352 // we can select the right animation in the cases of starting
353 // windows, where the app hasn't had time to set a value
354 // on the window.
355 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700356
chaviw59b98852017-06-13 12:05:44 -0700357 private boolean mShowWhenLocked;
358 private boolean mTurnScreenOn;
359
Andrii Kulian21713ac2016-10-12 22:05:05 -0700360 /**
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -0800361 * Temp configs used in {@link #ensureActivityConfiguration(int, boolean)}
Andrii Kulian21713ac2016-10-12 22:05:05 -0700362 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700363 private final Configuration mTmpConfig = new Configuration();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700364 private final Rect mTmpBounds = new Rect();
Andrii Kulian21713ac2016-10-12 22:05:05 -0700365
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800366 private static String startingWindowStateToString(int state) {
367 switch (state) {
368 case STARTING_WINDOW_NOT_SHOWN:
369 return "STARTING_WINDOW_NOT_SHOWN";
370 case STARTING_WINDOW_SHOWN:
371 return "STARTING_WINDOW_SHOWN";
372 case STARTING_WINDOW_REMOVED:
373 return "STARTING_WINDOW_REMOVED";
374 default:
375 return "unknown state=" + state;
376 }
377 }
378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700380 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700381 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
382 pw.print(" processName="); pw.println(processName);
383 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700384 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800385 pw.print(" userId="); pw.println(userId);
386 pw.print(prefix); pw.print("app="); pw.println(app);
387 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700388 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
389 pw.print(" task="); pw.println(task);
390 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700391 pw.print(prefix); pw.print("realActivity=");
392 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700393 if (appInfo != null) {
394 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
395 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
396 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
397 }
398 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800399 if (appInfo.splitSourceDirs != null) {
400 pw.print(prefix); pw.print("splitDir=");
401 pw.println(Arrays.toString(appInfo.splitSourceDirs));
402 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800403 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700404 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
405 pw.print(" componentSpecified="); pw.print(componentSpecified);
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700406 pw.print(" mActivityType="); pw.println(
407 activityTypeToString(getActivityType()));
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700408 if (rootVoiceInteraction) {
409 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
410 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800411 pw.print(prefix); pw.print("compat="); pw.print(compat);
412 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
413 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
414 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700415 pw.println(prefix + "mLastReportedConfigurations:");
416 mLastReportedConfiguration.dump(pw, prefix + " ");
417
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700418 pw.print(prefix); pw.print("CurrentConfiguration="); pw.println(getConfiguration());
419 if (!getOverrideConfiguration().equals(EMPTY)) {
420 pw.println(prefix + "OverrideConfiguration=" + getOverrideConfiguration());
421 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800422 if (!matchParentBounds()) {
423 pw.println(prefix + "bounds=" + getBounds());
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700424 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700425 if (resultTo != null || resultWho != null) {
426 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
427 pw.print(" resultWho="); pw.print(resultWho);
428 pw.print(" resultCode="); pw.println(requestCode);
429 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700430 if (taskDescription != null) {
431 final String iconFilename = taskDescription.getIconFilename();
432 if (iconFilename != null || taskDescription.getLabel() != null ||
433 taskDescription.getPrimaryColor() != 0) {
434 pw.print(prefix); pw.print("taskDescription:");
Craig Mautner29c58ca2014-10-14 16:17:06 -0700435 pw.print(" label=\""); pw.print(taskDescription.getLabel());
436 pw.print("\"");
Matthew Ng54bc9422017-10-02 17:16:28 -0700437 pw.print(" icon="); pw.print(taskDescription.getInMemoryIcon() != null
438 ? taskDescription.getInMemoryIcon().getByteCount() + " bytes"
439 : "null");
440 pw.print(" iconResource="); pw.print(taskDescription.getIconResource());
441 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200442 pw.print(" primaryColor=");
Craig Mautner29c58ca2014-10-14 16:17:06 -0700443 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700444 pw.print(prefix + " backgroundColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200445 pw.println(Integer.toHexString(taskDescription.getBackgroundColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700446 pw.print(prefix + " statusBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200447 pw.println(Integer.toHexString(taskDescription.getStatusBarColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700448 pw.print(prefix + " navigationBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200449 pw.println(Integer.toHexString(taskDescription.getNavigationBarColor()));
Craig Mautner29c58ca2014-10-14 16:17:06 -0700450 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700451 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700452 if (results != null) {
453 pw.print(prefix); pw.print("results="); pw.println(results);
454 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700455 if (pendingResults != null && pendingResults.size() > 0) {
456 pw.print(prefix); pw.println("Pending Results:");
457 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
458 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
459 pw.print(prefix); pw.print(" - ");
460 if (pir == null) {
461 pw.println("null");
462 } else {
463 pw.println(pir);
464 pir.dump(pw, prefix + " ");
465 }
466 }
467 }
468 if (newIntents != null && newIntents.size() > 0) {
469 pw.print(prefix); pw.println("Pending New Intents:");
470 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800471 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700472 pw.print(prefix); pw.print(" - ");
473 if (intent == null) {
474 pw.println("null");
475 } else {
476 pw.println(intent.toShortString(false, true, false, true));
477 }
478 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700479 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700480 if (pendingOptions != null) {
481 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
482 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700483 if (appTimeTracker != null) {
484 appTimeTracker.dumpWithHeader(pw, prefix, false);
485 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700486 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700487 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700488 }
489 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800490 pw.print(" launchCount="); pw.print(launchCount);
491 pw.print(" lastLaunchTime=");
492 if (lastLaunchTime == 0) pw.print("0");
493 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
494 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700495 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700496 pw.print(" icicle="); pw.println(icicle);
Bryce Lee7ace3952018-02-16 14:34:32 -0800497 pw.print(prefix); pw.print("state="); pw.print(mState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700498 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700499 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700500 pw.print(" finishing="); pw.println(finishing);
501 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
502 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700503 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800504 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800505 pw.print(" idle="); pw.print(idle);
506 pw.print(" mStartingWindowState=");
507 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800508 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
509 pw.print(" noDisplay="); pw.print(noDisplay);
510 pw.print(" immersive="); pw.print(immersive);
511 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800512 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400513 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700514 pw.print(prefix); pw.print("mActivityType=");
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -0700515 pw.println(activityTypeToString(getActivityType()));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800516 if (requestedVrComponent != null) {
517 pw.print(prefix);
518 pw.print("requestedVrComponent=");
519 pw.println(requestedVrComponent);
520 }
Bryce Lee4a194382017-04-04 14:32:48 -0700521 final boolean waitingVisible =
522 mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800523 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700524 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800525 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700526 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700527 if (lastVisibleTime == 0) pw.print("0");
528 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
529 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700530 }
Wale Ogunwaleec950642017-04-25 07:44:21 -0700531 if (mDeferHidingClient) {
532 pw.println(prefix + "mDeferHidingClient=" + mDeferHidingClient);
533 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800534 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
535 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700536 pw.print(" configChangeFlags=");
537 pw.println(Integer.toHexString(configChangeFlags));
538 }
Wale Ogunwalec4e63a42018-10-02 13:19:54 -0700539 if (mServiceConnectionsHolder != null) {
540 pw.print(prefix); pw.print("connections="); pw.println(mServiceConnectionsHolder);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700541 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800542 if (info != null) {
543 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
Winson Chung609e1e92017-05-08 10:52:12 -0700544 pw.println(prefix + "mLastReportedMultiWindowMode=" + mLastReportedMultiWindowMode
545 + " mLastReportedPictureInPictureMode=" + mLastReportedPictureInPictureMode);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700546 if (info.supportsPictureInPicture()) {
547 pw.println(prefix + "supportsPictureInPicture=" + info.supportsPictureInPicture());
Winson Chungf7e03e12017-08-22 11:32:16 -0700548 pw.println(prefix + "supportsEnterPipOnTaskSwitch: "
549 + supportsEnterPipOnTaskSwitch);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700550 }
551 if (info.maxAspectRatio != 0) {
552 pw.println(prefix + "maxAspectRatio=" + info.maxAspectRatio);
553 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556
Philip P. Moltmanncff8f0f2018-03-27 12:51:51 -0700557 void updateApplicationInfo(ApplicationInfo aInfo) {
558 appInfo = aInfo;
559 info.applicationInfo = aInfo;
560 }
561
Andrii Kulian21713ac2016-10-12 22:05:05 -0700562 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700563 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
564 }
565
Andrii Kulian21713ac2016-10-12 22:05:05 -0700566 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700567 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
568 }
569
Andrii Kulian21713ac2016-10-12 22:05:05 -0700570 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700571 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
572 }
573
Filip Gruszczynski23493322015-07-29 17:02:59 -0700574 /**
575 * The purpose of this method is to decide whether the activity needs to be relaunched upon
576 * changing its size. In most cases the activities don't need to be relaunched, if the resize
577 * is small, all the activity content has to do is relayout itself within new bounds. There are
578 * cases however, where the activity's content would be completely changed in the new size and
579 * the full relaunch is required.
580 *
581 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
582 * required. These thresholds are collected from the application resource qualifiers. For
583 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
584 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
585 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
586 * of the threshold.
587 */
588 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
589 int secondDp) {
590 if (thresholds == null) {
591 return false;
592 }
593 for (int i = thresholds.length - 1; i >= 0; i--) {
594 final int threshold = thresholds[i];
595 if ((firstDp < threshold && secondDp >= threshold)
596 || (firstDp >= threshold && secondDp < threshold)) {
597 return true;
598 }
599 }
600 return false;
601 }
602
Andrii Kulian21713ac2016-10-12 22:05:05 -0700603 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700604 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700605 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
606 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700607 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700608 }
609
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800610 private void scheduleActivityMovedToDisplay(int displayId, Configuration config) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700611 if (!attachedToProcess()) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800612 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.w(TAG,
613 "Can't report activity moved to display - client not running, activityRecord="
614 + this + ", displayId=" + displayId);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800615 return;
616 }
617 try {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800618 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
619 "Reporting activity moved to display" + ", activityRecord=" + this
620 + ", displayId=" + displayId + ", config=" + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700621
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700622 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800623 MoveToDisplayItem.obtain(displayId, config));
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800624 } catch (RemoteException e) {
625 // If process died, whatever.
626 }
627 }
628
629 private void scheduleConfigurationChanged(Configuration config) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700630 if (!attachedToProcess()) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800631 if (DEBUG_CONFIGURATION) Slog.w(TAG,
632 "Can't report activity configuration update - client not running"
633 + ", activityRecord=" + this);
634 return;
635 }
636 try {
637 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + ", config: "
638 + config);
639
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700640 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800641 ActivityConfigurationChangeItem.obtain(config));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800642 } catch (RemoteException e) {
643 // If process died, whatever.
644 }
645 }
646
Winson Chung5af42fc2017-03-24 17:11:33 -0700647 void updateMultiWindowMode() {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700648 if (task == null || task.getStack() == null || !attachedToProcess()) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800649 return;
650 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700651
Wale Ogunwaleeb76b762017-11-17 10:08:04 -0800652 if (task.getStack().deferScheduleMultiWindowModeChanged()) {
653 // Don't do anything if we are currently deferring multi-window mode change.
654 return;
655 }
656
Winson Chung5af42fc2017-03-24 17:11:33 -0700657 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Wale Ogunwaleeb76b762017-11-17 10:08:04 -0800658 final boolean inMultiWindowMode = inMultiWindowMode();
Winson Chung609e1e92017-05-08 10:52:12 -0700659 if (inMultiWindowMode != mLastReportedMultiWindowMode) {
660 mLastReportedMultiWindowMode = inMultiWindowMode;
Winson Chung5af42fc2017-03-24 17:11:33 -0700661 scheduleMultiWindowModeChanged(getConfiguration());
662 }
663 }
664
665 private void scheduleMultiWindowModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800666 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700667 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Wale Ogunwaled0412b32018-05-08 09:25:50 -0700668 MultiWindowModeChangeItem.obtain(mLastReportedMultiWindowMode, overrideConfig));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800669 } catch (Exception e) {
670 // If process died, I don't care.
671 }
672 }
673
Winson Chungab76bbc2017-08-14 13:33:51 -0700674 void updatePictureInPictureMode(Rect targetStackBounds, boolean forceUpdate) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700675 if (task == null || task.getStack() == null || !attachedToProcess()) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800676 return;
677 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700678
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700679 final boolean inPictureInPictureMode = inPinnedWindowingMode() && targetStackBounds != null;
Winson Chungab76bbc2017-08-14 13:33:51 -0700680 if (inPictureInPictureMode != mLastReportedPictureInPictureMode || forceUpdate) {
Winson Chung5af42fc2017-03-24 17:11:33 -0700681 // Picture-in-picture mode changes also trigger a multi-window mode change as well, so
Winson Chung059955f2018-08-08 16:10:20 -0700682 // update that here in order. Set the last reported MW state to the same as the PiP
683 // state since we haven't yet actually resized the task (these callbacks need to
684 // preceed the configuration change from the resiez.
685 // TODO(110009072): Once we move these callbacks to the client, remove all logic related
686 // to forcing the update of the picture-in-picture mode as a part of the PiP animation.
Winson Chung609e1e92017-05-08 10:52:12 -0700687 mLastReportedPictureInPictureMode = inPictureInPictureMode;
Winson Chung059955f2018-08-08 16:10:20 -0700688 mLastReportedMultiWindowMode = inPictureInPictureMode;
Winson Chung5af42fc2017-03-24 17:11:33 -0700689 final Configuration newConfig = task.computeNewOverrideConfigurationForBounds(
690 targetStackBounds, null);
691 schedulePictureInPictureModeChanged(newConfig);
692 scheduleMultiWindowModeChanged(newConfig);
693 }
694 }
695
696 private void schedulePictureInPictureModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800697 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700698 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kulian9c5ea9c2017-12-07 09:31:01 -0800699 PipModeChangeItem.obtain(mLastReportedPictureInPictureMode,
Andrii Kulian446e8242017-10-26 15:17:29 -0700700 overrideConfig));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800701 } catch (Exception e) {
702 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800703 }
704 }
705
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700706 @Override
707 protected int getChildCount() {
708 // {@link ActivityRecord} is a leaf node and has no children.
709 return 0;
710 }
711
712 @Override
713 protected ConfigurationContainer getChildAt(int index) {
714 return null;
715 }
716
717 @Override
718 protected ConfigurationContainer getParent() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700719 return getTask();
720 }
721
722 TaskRecord getTask() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700723 return task;
724 }
725
Bryce Leeaf691c02017-03-20 14:20:22 -0700726 /**
727 * Sets reference to the {@link TaskRecord} the {@link ActivityRecord} will treat as its parent.
728 * Note that this does not actually add the {@link ActivityRecord} as a {@link TaskRecord}
729 * children. However, this method will clean up references to this {@link ActivityRecord} in
730 * {@link ActivityStack}.
731 * @param task The new parent {@link TaskRecord}.
732 */
733 void setTask(TaskRecord task) {
Bryce Lee84730a02018-04-03 14:10:04 -0700734 setTask(task /* task */, false /* reparenting */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700735 }
736
737 /**
738 * This method should only be called by {@link TaskRecord#removeActivity(ActivityRecord)}.
Bryce Lee84730a02018-04-03 14:10:04 -0700739 * @param task The new parent task.
740 * @param reparenting Whether we're in the middle of reparenting.
Bryce Leeaf691c02017-03-20 14:20:22 -0700741 */
742 void setTask(TaskRecord task, boolean reparenting) {
743 // Do nothing if the {@link TaskRecord} is the same as the current {@link getTask}.
744 if (task != null && task == getTask()) {
745 return;
746 }
747
Bryce Lee84730a02018-04-03 14:10:04 -0700748 final ActivityStack oldStack = getStack();
749 final ActivityStack newStack = task != null ? task.getStack() : null;
Bryce Leeaf691c02017-03-20 14:20:22 -0700750
Bryce Lee84730a02018-04-03 14:10:04 -0700751 // Inform old stack (if present) of activity removal and new stack (if set) of activity
752 // addition.
753 if (oldStack != newStack) {
754 if (!reparenting && oldStack != null) {
755 oldStack.onActivityRemovedFromStack(this);
756 }
757
758 if (newStack != null) {
759 newStack.onActivityAddedToStack(this);
760 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700761 }
762
763 this.task = task;
764
765 if (!reparenting) {
766 onParentChanged();
767 }
768 }
769
chaviw4ad54912018-05-30 11:05:44 -0700770 /**
771 * See {@link AppWindowContainerController#setWillCloseOrEnterPip(boolean)}
772 */
773 void setWillCloseOrEnterPip(boolean willCloseOrEnterPip) {
774 getWindowContainerController().setWillCloseOrEnterPip(willCloseOrEnterPip);
775 }
776
Dianne Hackbornbe707852011-11-11 14:32:10 -0800777 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700778 private final WeakReference<ActivityRecord> weakActivity;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700779 private final String name;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800780
Steven Timotiusaf03df62017-07-18 16:56:43 -0700781 Token(ActivityRecord activity, Intent intent) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800782 weakActivity = new WeakReference<>(activity);
Steven Timotiusaf03df62017-07-18 16:56:43 -0700783 name = intent.getComponent().flattenToShortString();
Wale Ogunwale7d701172015-03-11 15:36:30 -0700784 }
785
Andrii Kulian21713ac2016-10-12 22:05:05 -0700786 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700787 if (token == null) {
788 return null;
789 }
790 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700791 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700792 return null;
793 }
794 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800795 }
796
Craig Mautnerde4ef022013-04-07 19:01:33 -0700797 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800798 public String toString() {
799 StringBuilder sb = new StringBuilder(128);
800 sb.append("Token{");
801 sb.append(Integer.toHexString(System.identityHashCode(this)));
802 sb.append(' ');
803 sb.append(weakActivity.get());
804 sb.append('}');
805 return sb.toString();
806 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700807
808 @Override
809 public String getName() {
810 return name;
811 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800812 }
813
Wale Ogunwale7d701172015-03-11 15:36:30 -0700814 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800815 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700816 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800817 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800818 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800819 return null;
820 }
821 }
822
Chong Zhang85ee6542015-10-02 13:36:38 -0700823 boolean isResolverActivity() {
824 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700825 }
826
Patrick Baumann31426b22018-05-21 13:46:40 -0700827 boolean isResolverOrChildActivity() {
828 if (!"android".equals(packageName)) {
829 return false;
830 }
831 try {
832 return ResolverActivity.class.isAssignableFrom(
833 Object.class.getClassLoader().loadClass(realActivity.getClassName()));
834 } catch (ClassNotFoundException e) {
835 return false;
836 }
837 }
838
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700839 ActivityRecord(ActivityTaskManagerService _service, WindowProcessController _caller,
840 int _launchedFromPid, int _launchedFromUid, String _launchedFromPackage, Intent _intent,
841 String _resolvedType, ActivityInfo aInfo, Configuration _configuration,
842 ActivityRecord _resultTo, String _resultWho, int _reqCode, boolean _componentSpecified,
843 boolean _rootVoiceInteraction, ActivityStackSupervisor supervisor,
844 ActivityOptions options, ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 service = _service;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700846 appToken = new Token(this, _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 info = aInfo;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800848 launchedFromPid = _launchedFromPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800850 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700851 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 intent = _intent;
853 shortComponentName = _intent.getComponent().flattenToShortString();
854 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800855 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700856 rootVoiceInteraction = _rootVoiceInteraction;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700857 mLastReportedConfiguration = new MergedConfiguration(_configuration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 resultTo = _resultTo;
859 resultWho = _resultWho;
860 requestCode = _reqCode;
Bryce Lee7ace3952018-02-16 14:34:32 -0800861 setState(INITIALIZING, "ActivityRecord ctor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 frontOfTask = false;
863 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700865 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800867 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 keysPaused = false;
869 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700870 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 idle = false;
873 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700874 mStackSupervisor = supervisor;
Robert Carr0f5d7532016-10-17 16:39:17 -0700875
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800876 // This starts out true, since the initial state of an activity is that we have everything,
877 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700878 haveState = true;
879
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800880 // If the class name in the intent doesn't match that of the target, this is
881 // probably an alias. We have to create a new ComponentName object to keep track
882 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
883 if (aInfo.targetActivity == null
884 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
885 && (aInfo.launchMode == LAUNCH_MULTIPLE
886 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
887 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800889 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800891 taskAffinity = aInfo.taskAffinity;
892 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
893 appInfo = aInfo.applicationInfo;
894 nonLocalizedLabel = aInfo.nonLocalizedLabel;
895 labelRes = aInfo.labelRes;
896 if (nonLocalizedLabel == null && labelRes == 0) {
897 ApplicationInfo app = aInfo.applicationInfo;
898 nonLocalizedLabel = app.nonLocalizedLabel;
899 labelRes = app.labelRes;
900 }
901 icon = aInfo.getIconResource();
902 logo = aInfo.getLogoResource();
903 theme = aInfo.getThemeResource();
904 realTheme = theme;
905 if (realTheme == 0) {
906 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
907 ? android.R.style.Theme : android.R.style.Theme_Holo;
908 }
909 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
910 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
911 }
912 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
913 && (aInfo.applicationInfo.uid == SYSTEM_UID
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700914 || aInfo.applicationInfo.uid == _caller.mInfo.uid)) {
915 processName = _caller.mName;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800916 } else {
917 processName = aInfo.processName;
918 }
919
920 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
921 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
922 }
923
924 packageName = aInfo.applicationInfo.packageName;
925 launchMode = aInfo.launchMode;
926
Ruben Brunkf53497c2017-03-27 20:26:17 -0700927 Entry ent = AttributeCache.instance().get(packageName,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800928 realTheme, com.android.internal.R.styleable.Window, userId);
Bryce Leee83f34cd2017-10-31 19:50:54 -0700929
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700930 if (ent != null) {
931 fullscreen = !ActivityInfo.isTranslucentOrFloating(ent.array);
932 hasWallpaper = ent.array.getBoolean(R.styleable.Window_windowShowWallpaper, false);
933 noDisplay = ent.array.getBoolean(R.styleable.Window_windowNoDisplay, false);
934 } else {
935 hasWallpaper = false;
936 noDisplay = false;
937 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800938
Winson Chung83471632016-12-13 11:02:12 -0800939 setActivityType(_componentSpecified, _launchedFromUid, _intent, options, sourceRecord);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800940
941 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
942
943 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
944 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
chaviw59b98852017-06-13 12:05:44 -0700945
946 mShowWhenLocked = (aInfo.flags & FLAG_SHOW_WHEN_LOCKED) != 0;
947 mTurnScreenOn = (aInfo.flags & FLAG_TURN_SCREEN_ON) != 0;
Charles He2bf28322017-10-12 22:24:49 +0100948
949 mRotationAnimationHint = aInfo.rotationAnimation;
950 lockTaskLaunchMode = aInfo.lockTaskLaunchMode;
951 if (appInfo.isPrivilegedApp() && (lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_ALWAYS
952 || lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_NEVER)) {
953 lockTaskLaunchMode = LOCK_TASK_LAUNCH_MODE_DEFAULT;
954 }
955
956 if (options != null) {
957 pendingOptions = options;
958 mLaunchTaskBehind = options.getLaunchTaskBehind();
959
960 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
961 // Only override manifest supplied option if set.
962 if (rotationAnimation >= 0) {
963 mRotationAnimationHint = rotationAnimation;
964 }
965 final PendingIntent usageReport = pendingOptions.getUsageTimeReport();
966 if (usageReport != null) {
967 appTimeTracker = new AppTimeTracker(usageReport);
968 }
969 final boolean useLockTask = pendingOptions.getLockTaskMode();
970 if (useLockTask && lockTaskLaunchMode == LOCK_TASK_LAUNCH_MODE_DEFAULT) {
971 lockTaskLaunchMode = LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
972 }
973 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800974 }
975
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700976 void setProcess(WindowProcessController proc) {
Dianne Hackborn68a06332017-11-15 17:54:18 -0800977 app = proc;
978 final ActivityRecord root = task != null ? task.getRootActivity() : null;
979 if (root == this) {
980 task.setRootProcess(proc);
981 }
982 }
983
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700984 boolean hasProcess() {
985 return app != null;
986 }
987
988 boolean attachedToProcess() {
989 return hasProcess() && app.hasThread();
990 }
991
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800992 AppWindowContainerController getWindowContainerController() {
993 return mWindowContainerController;
994 }
995
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800996 void createWindowContainer() {
997 if (mWindowContainerController != null) {
998 throw new IllegalArgumentException("Window container=" + mWindowContainerController
999 + " already created for r=" + this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001000 }
1001
1002 inHistory = true;
1003
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001004 final TaskWindowContainerController taskController = task.getWindowContainerController();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001005
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07001006 // TODO(b/36505427): Maybe this call should be moved inside updateOverrideConfiguration()
1007 task.updateOverrideConfigurationFromLaunchBounds();
1008 // Make sure override configuration is up-to-date before using to create window controller.
1009 updateOverrideConfiguration();
1010
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001011 mWindowContainerController = new AppWindowContainerController(taskController, appToken,
Winson Chung48b25652018-10-22 14:04:30 -07001012 realActivity, this, Integer.MAX_VALUE /* add on top */, info.screenOrientation,
1013 fullscreen, (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001014 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
1015 appInfo.targetSdkVersion, mRotationAnimationHint,
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001016 ActivityTaskManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001017
1018 task.addActivityToTop(this);
1019
Winson Chung609e1e92017-05-08 10:52:12 -07001020 // When an activity is started directly into a split-screen fullscreen stack, we need to
1021 // update the initial multi-window modes so that the callbacks are scheduled correctly when
1022 // the user leaves that mode.
Bryce Leef3c6a472017-11-14 14:53:06 -08001023 mLastReportedMultiWindowMode = inMultiWindowMode();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001024 mLastReportedPictureInPictureMode = inPinnedWindowingMode();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001025 }
1026
1027 void removeWindowContainer() {
Bryce Lee7ace3952018-02-16 14:34:32 -08001028 // Do not try to remove a window container if we have already removed it.
1029 if (mWindowContainerController == null) {
1030 return;
1031 }
1032
Wale Ogunwalecc367f42017-02-01 08:12:14 -08001033 // Resume key dispatching if it is currently paused before we remove the container.
1034 resumeKeyDispatchingLocked();
1035
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001036 mWindowContainerController.removeContainer(getDisplayId());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001037 mWindowContainerController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
1039
Winson Chung30480042017-01-26 10:55:34 -08001040 /**
1041 * Reparents this activity into {@param newTask} at the provided {@param position}. The caller
1042 * should ensure that the {@param newTask} is not already the parent of this activity.
1043 */
1044 void reparent(TaskRecord newTask, int position, String reason) {
1045 final TaskRecord prevTask = task;
1046 if (prevTask == newTask) {
1047 throw new IllegalArgumentException(reason + ": task=" + newTask
1048 + " is already the parent of r=" + this);
1049 }
1050
Winson Chung74666102017-02-22 17:49:24 -08001051 // TODO: Ensure that we do not directly reparent activities across stacks, as that may leave
1052 // the stacks in strange states. For now, we should use Task.reparent() to ensure that
1053 // the stack is left in an OK state.
1054 if (prevTask != null && newTask != null && prevTask.getStack() != newTask.getStack()) {
1055 throw new IllegalArgumentException(reason + ": task=" + newTask
1056 + " is in a different stack (" + newTask.getStackId() + ") than the parent of"
1057 + " r=" + this + " (" + prevTask.getStackId() + ")");
1058 }
1059
Winson Chung30480042017-01-26 10:55:34 -08001060 // Must reparent first in window manager
1061 mWindowContainerController.reparent(newTask.getWindowContainerController(), position);
1062
Bryce Lee84730a02018-04-03 14:10:04 -07001063 // Reparenting prevents informing the parent stack of activity removal in the case that
1064 // the new stack has the same parent. we must manually signal here if this is not the case.
1065 final ActivityStack prevStack = prevTask.getStack();
1066
1067 if (prevStack != newTask.getStack()) {
1068 prevStack.onActivityRemovedFromStack(this);
1069 }
Bryce Leeaf691c02017-03-20 14:20:22 -07001070 // Remove the activity from the old task and add it to the new task.
Bryce Lee84730a02018-04-03 14:10:04 -07001071 prevTask.removeActivity(this, true /* reparenting */);
Bryce Lee0f9bde82017-02-22 16:39:06 -08001072
Winson Chung30480042017-01-26 10:55:34 -08001073 newTask.addActivityAtIndex(position, this);
1074 }
1075
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001076 private boolean isHomeIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001077 return ACTION_MAIN.equals(intent.getAction())
1078 && intent.hasCategory(CATEGORY_HOME)
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001079 && intent.getCategories().size() == 1
1080 && intent.getData() == null
1081 && intent.getType() == null;
1082 }
1083
Chong Zhangad24f962016-08-25 12:12:33 -07001084 static boolean isMainIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001085 return ACTION_MAIN.equals(intent.getAction())
1086 && intent.hasCategory(CATEGORY_LAUNCHER)
Chong Zhangad24f962016-08-25 12:12:33 -07001087 && intent.getCategories().size() == 1
1088 && intent.getData() == null
1089 && intent.getType() == null;
1090 }
1091
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001092 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
1093 if (uid == Process.myUid() || uid == 0) {
1094 // System process can launch home activity.
1095 return true;
1096 }
Winson Chung547afd22018-05-17 16:03:25 -07001097 // Allow the recents component to launch the home activity.
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07001098 final RecentTasks recentTasks = mStackSupervisor.mService.getRecentTasks();
Winson Chung547afd22018-05-17 16:03:25 -07001099 if (recentTasks != null && recentTasks.isCallerRecents(uid)) {
1100 return true;
1101 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001102 // Resolver activity can launch home activity.
1103 return sourceRecord != null && sourceRecord.isResolverActivity();
1104 }
1105
Winson Chung83471632016-12-13 11:02:12 -08001106 /**
1107 * @return whether the given package name can launch an assist activity.
1108 */
1109 private boolean canLaunchAssistActivity(String packageName) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001110 final ComponentName assistComponent =
1111 service.mActiveVoiceInteractionServiceComponent;
Winson Chung83471632016-12-13 11:02:12 -08001112 if (assistComponent != null) {
1113 return assistComponent.getPackageName().equals(packageName);
1114 }
1115 return false;
1116 }
1117
1118 private void setActivityType(boolean componentSpecified, int launchedFromUid, Intent intent,
1119 ActivityOptions options, ActivityRecord sourceRecord) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001120 int activityType = ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001121 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
1122 && isHomeIntent(intent) && !isResolverActivity()) {
1123 // This sure looks like a home activity!
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001124 activityType = ACTIVITY_TYPE_HOME;
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001125
1126 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
1127 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
1128 // We only allow home activities to be resizeable if they explicitly requested it.
1129 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
1130 }
Wale Ogunwale16e505a2018-05-07 15:00:49 -07001131 } else if (realActivity.getClassName().contains(LEGACY_RECENTS_PACKAGE_NAME)
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001132 || service.getRecentTasks().isRecentsComponent(realActivity, appInfo.uid)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001133 activityType = ACTIVITY_TYPE_RECENTS;
Wale Ogunwale0568aed2017-09-08 13:29:37 -07001134 } else if (options != null && options.getLaunchActivityType() == ACTIVITY_TYPE_ASSISTANT
Winson Chung83471632016-12-13 11:02:12 -08001135 && canLaunchAssistActivity(launchedFromPackage)) {
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001136 activityType = ACTIVITY_TYPE_ASSISTANT;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001137 }
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001138 setActivityType(activityType);
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001139 }
1140
Craig Mautnera228ae92014-07-09 05:44:55 -07001141 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001142 if (launchMode != LAUNCH_SINGLE_INSTANCE && launchMode != LAUNCH_SINGLE_TASK) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001143 task.setTaskToAffiliateWith(taskToAffiliateWith);
1144 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001145 }
1146
Andrii Kulian02b7a832016-10-06 23:11:56 -07001147 /**
1148 * @return Stack value from current task, null if there is no task.
1149 */
Winson Chung55893332017-02-17 17:13:10 -08001150 <T extends ActivityStack> T getStack() {
1151 return task != null ? (T) task.getStack() : null;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001152 }
1153
Jorim Jaggicdfc04e2017-04-28 19:06:24 +02001154 int getStackId() {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001155 return getStack() != null ? getStack().mStackId : INVALID_STACK_ID;
1156 }
1157
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001158 ActivityDisplay getDisplay() {
1159 final ActivityStack stack = getStack();
1160 return stack != null ? stack.getDisplay() : null;
1161 }
1162
Craig Mautner5eda9b32013-07-02 11:58:16 -07001163 boolean changeWindowTranslucency(boolean toOpaque) {
1164 if (fullscreen == toOpaque) {
1165 return false;
1166 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001167
Craig Mautner5eda9b32013-07-02 11:58:16 -07001168 // Keep track of the number of fullscreen activities in this task.
1169 task.numFullscreen += toOpaque ? +1 : -1;
1170
1171 fullscreen = toOpaque;
1172 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -07001173 }
1174
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001175 void takeFromHistory() {
1176 if (inHistory) {
1177 inHistory = false;
1178 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001179 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001180 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001181 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001182 }
1183 }
1184
1185 boolean isInHistory() {
1186 return inHistory;
1187 }
1188
Wale Ogunwale7d701172015-03-11 15:36:30 -07001189 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001190 final ActivityStack stack = getStack();
1191 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001192 }
1193
Craig Mautner21d24a22014-04-23 11:45:37 -07001194 boolean isPersistable() {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001195 return (info.persistableMode == PERSIST_ROOT_ONLY ||
1196 info.persistableMode == PERSIST_ACROSS_REBOOTS) &&
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001197 (intent == null || (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -07001198 }
1199
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001200 boolean isFocusable() {
Bryce Lee7b851cc2018-04-10 14:53:13 -07001201 return mStackSupervisor.isFocusable(this, isAlwaysFocusable());
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001202 }
1203
1204 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001205 return ActivityInfo.isResizeableMode(info.resizeMode) || info.supportsPictureInPicture();
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001206 }
1207
Winson Chungd3395382016-12-13 11:49:09 -08001208 /**
1209 * @return whether this activity is non-resizeable or forced to be resizeable
1210 */
1211 boolean isNonResizableOrForcedResizable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001212 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -07001213 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -07001214 }
1215
Winson Chunge6308042016-10-31 09:24:01 -07001216 /**
Winson Chungd3395382016-12-13 11:49:09 -08001217 * @return whether this activity supports PiP multi-window and can be put in the pinned stack.
Winson Chunge6308042016-10-31 09:24:01 -07001218 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001219 boolean supportsPictureInPicture() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001220 return service.mSupportsPictureInPicture && isActivityTypeStandardOrUndefined()
Winson Chungd3395382016-12-13 11:49:09 -08001221 && info.supportsPictureInPicture();
1222 }
1223
1224 /**
1225 * @return whether this activity supports split-screen multi-window and can be put in the docked
1226 * stack.
1227 */
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001228 @Override
1229 public boolean supportsSplitScreenWindowingMode() {
Winson Chungd3395382016-12-13 11:49:09 -08001230 // An activity can not be docked even if it is considered resizeable because it only
1231 // supports picture-in-picture mode but has a non-resizeable resizeMode
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001232 return super.supportsSplitScreenWindowingMode()
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001233 && service.mSupportsSplitScreenMultiWindow && supportsResizeableMultiWindow();
Winson Chungd3395382016-12-13 11:49:09 -08001234 }
1235
1236 /**
1237 * @return whether this activity supports freeform multi-window and can be put in the freeform
1238 * stack.
1239 */
1240 boolean supportsFreeform() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001241 return service.mSupportsFreeformWindowManagement && supportsResizeableMultiWindow();
Winson Chungd3395382016-12-13 11:49:09 -08001242 }
1243
1244 /**
1245 * @return whether this activity supports non-PiP multi-window.
1246 */
1247 private boolean supportsResizeableMultiWindow() {
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001248 return service.mSupportsMultiWindow && !isActivityTypeHome()
Winson Chungd3395382016-12-13 11:49:09 -08001249 && (ActivityInfo.isResizeableMode(info.resizeMode)
Wale Ogunwalea6191b42018-05-09 07:41:32 -07001250 || service.mForceResizableActivities);
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001251 }
1252
Winson Chunge6308042016-10-31 09:24:01 -07001253 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001254 * Check whether this activity can be launched on the specified display.
Riddle Hsu16567132018-08-16 21:37:47 +08001255 *
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001256 * @param displayId Target display id.
Riddle Hsu16567132018-08-16 21:37:47 +08001257 * @return {@code true} if either it is the default display or this activity can be put on a
1258 * secondary screen.
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001259 */
1260 boolean canBeLaunchedOnDisplay(int displayId) {
Riddle Hsu16567132018-08-16 21:37:47 +08001261 return service.mStackSupervisor.canPlaceEntityOnDisplay(displayId, launchedFromPid,
1262 launchedFromUid, info);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001263 }
1264
1265 /**
Robert Carrc33658e2017-04-11 18:24:20 -07001266 * @param beforeStopping Whether this check is for an auto-enter-pip operation, that is to say
1267 * the activity has requested to enter PiP when it would otherwise be stopped.
1268 *
Winson Chung298f95b2017-08-10 15:57:18 -07001269 * @return whether this activity is currently allowed to enter PIP.
Winson Chunge6308042016-10-31 09:24:01 -07001270 */
Winson Chung298f95b2017-08-10 15:57:18 -07001271 boolean checkEnterPictureInPictureState(String caller, boolean beforeStopping) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07001272 if (!supportsPictureInPicture()) {
1273 return false;
1274 }
1275
Winson Chungf4ac0632017-03-17 12:34:12 -07001276 // Check app-ops and see if PiP is supported for this package
1277 if (!checkEnterPictureInPictureAppOpsState()) {
1278 return false;
1279 }
1280
Winson Chungf1bfee12017-03-24 17:11:33 -07001281 // Check to see if we are in VR mode, and disallow PiP if so
1282 if (service.shouldDisableNonVrUiLocked()) {
1283 return false;
1284 }
1285
Winson Chungc2baac02017-01-11 13:34:47 -08001286 boolean isKeyguardLocked = service.isKeyguardLocked();
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001287 boolean isCurrentAppLocked =
1288 service.getLockTaskModeState() != LOCK_TASK_MODE_NONE;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -07001289 final ActivityDisplay display = getDisplay();
1290 boolean hasPinnedStack = display != null && display.hasPinnedStack();
Winson Chungbb348802017-01-30 12:01:45 -08001291 // Don't return early if !isNotLocked, since we want to throw an exception if the activity
1292 // is in an incorrect state
Winson Chunge581ebf2017-02-21 08:25:03 -08001293 boolean isNotLockedOrOnKeyguard = !isKeyguardLocked && !isCurrentAppLocked;
Robert Carrc33658e2017-04-11 18:24:20 -07001294
1295 // We don't allow auto-PiP when something else is already pipped.
1296 if (beforeStopping && hasPinnedStack) {
1297 return false;
1298 }
1299
Bryce Lee7ace3952018-02-16 14:34:32 -08001300 switch (mState) {
Winson Chungc2baac02017-01-11 13:34:47 -08001301 case RESUMED:
Winson Chunge581ebf2017-02-21 08:25:03 -08001302 // When visible, allow entering PiP if the app is not locked. If it is over the
1303 // keyguard, then we will prompt to unlock in the caller before entering PiP.
Robert Carrc33658e2017-04-11 18:24:20 -07001304 return !isCurrentAppLocked &&
Winson Chungf7e03e12017-08-22 11:32:16 -07001305 (supportsEnterPipOnTaskSwitch || !beforeStopping);
Winson Chungc2baac02017-01-11 13:34:47 -08001306 case PAUSING:
1307 case PAUSED:
Winson Chungbb348802017-01-30 12:01:45 -08001308 // When pausing, then only allow enter PiP as in the resume state, and in addition,
1309 // require that there is not an existing PiP activity and that the current system
1310 // state supports entering PiP
Winson Chunge581ebf2017-02-21 08:25:03 -08001311 return isNotLockedOrOnKeyguard && !hasPinnedStack
Winson Chungf7e03e12017-08-22 11:32:16 -07001312 && supportsEnterPipOnTaskSwitch;
Winson Chungc2baac02017-01-11 13:34:47 -08001313 case STOPPING:
1314 // When stopping in a valid state, then only allow enter PiP as in the pause state.
1315 // Otherwise, fall through to throw an exception if the caller is trying to enter
1316 // PiP in an invalid stopping state.
Winson Chungf7e03e12017-08-22 11:32:16 -07001317 if (supportsEnterPipOnTaskSwitch) {
Winson Chungf4ac0632017-03-17 12:34:12 -07001318 return isNotLockedOrOnKeyguard && !hasPinnedStack;
Winson Chungc2baac02017-01-11 13:34:47 -08001319 }
1320 default:
Winson Chung298f95b2017-08-10 15:57:18 -07001321 return false;
Winson Chungb5c41b72016-12-07 15:00:47 -08001322 }
Winson Chunge6308042016-10-31 09:24:01 -07001323 }
1324
Winson Chung59fda9e2017-01-20 16:14:51 -08001325 /**
Winson Chungf4ac0632017-03-17 12:34:12 -07001326 * @return Whether AppOps allows this package to enter picture-in-picture.
Winson Chung59fda9e2017-01-20 16:14:51 -08001327 */
Winson Chungf4ac0632017-03-17 12:34:12 -07001328 private boolean checkEnterPictureInPictureAppOpsState() {
Wale Ogunwalef6733932018-06-27 05:14:34 -07001329 return service.getAppOpsService().checkOperation(
1330 OP_PICTURE_IN_PICTURE, appInfo.uid, packageName) == MODE_ALLOWED;
Winson Chung59fda9e2017-01-20 16:14:51 -08001331 }
1332
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001333 boolean isAlwaysFocusable() {
1334 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001335 }
1336
Louis Chang19443452018-10-09 12:10:21 +08001337 /** Move activity with its stack to front and make the stack focused. */
1338 boolean moveFocusableActivityToTop(String reason) {
1339 if (!isFocusable()) {
1340 if (DEBUG_FOCUS) {
1341 Slog.d(TAG_FOCUS, "moveActivityStackToFront: unfocusable activity=" + this);
1342 }
1343 return false;
1344 }
1345
1346 final TaskRecord task = getTask();
1347 final ActivityStack stack = getStack();
1348 if (stack == null) {
1349 Slog.w(TAG, "moveActivityStackToFront: invalid task or stack: activity="
1350 + this + " task=" + task);
1351 return false;
1352 }
1353
1354 if (mStackSupervisor.getTopResumedActivity() == this) {
1355 if (DEBUG_FOCUS) {
1356 Slog.d(TAG_FOCUS, "moveActivityStackToFront: already on top, activity=" + this);
1357 }
1358 return false;
1359 }
1360
1361 if (DEBUG_FOCUS) {
1362 Slog.d(TAG_FOCUS, "moveActivityStackToFront: activity=" + this);
1363 }
1364
1365 stack.moveToFront(reason, task);
1366 // Report top activity change to tracking services and WM
1367 if (mStackSupervisor.getTopResumedActivity() == this) {
1368 // TODO(b/111361570): Support multiple focused apps in WM
1369 service.setResumedActivityUncheckLocked(this, reason);
1370 }
1371 return true;
1372 }
Jorim Jaggife762342016-10-13 14:33:27 +02001373
1374 /**
1375 * @return true if the activity contains windows that have
1376 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
1377 */
1378 boolean hasDismissKeyguardWindows() {
1379 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
1380 }
1381
Wale Ogunwale7d701172015-03-11 15:36:30 -07001382 void makeFinishingLocked() {
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001383 if (finishing) {
1384 return;
1385 }
1386 finishing = true;
1387 if (stopped) {
1388 clearOptionsLocked();
1389 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001390
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001391 if (service != null) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001392 service.getTaskChangeNotificationController().notifyTaskStackChanged();
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001393 }
1394 }
1395
Dianne Hackborn7e269642010-08-25 19:50:20 -07001396 UriPermissionOwner getUriPermissionsLocked() {
1397 if (uriPermissions == null) {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001398 uriPermissions = new UriPermissionOwner(service.mUgmInternal, this);
Dianne Hackborn7e269642010-08-25 19:50:20 -07001399 }
1400 return uriPermissions;
1401 }
1402
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001403 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 int requestCode, int resultCode,
1405 Intent resultData) {
1406 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -05001407 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001409 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 }
1411 results.add(r);
1412 }
1413
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001414 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 int requestCode) {
1416 if (results != null) {
1417 for (int i=results.size()-1; i>=0; i--) {
1418 ActivityResult r = (ActivityResult)results.get(i);
1419 if (r.mFrom != from) continue;
1420 if (r.mResultWho == null) {
1421 if (resultWho != null) continue;
1422 } else {
1423 if (!r.mResultWho.equals(resultWho)) continue;
1424 }
1425 if (r.mRequestCode != requestCode) continue;
1426
1427 results.remove(i);
1428 }
1429 }
1430 }
1431
Andrii Kulian21713ac2016-10-12 22:05:05 -07001432 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001434 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 }
1436 newIntents.add(intent);
1437 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001438
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001439 final boolean isSleeping() {
1440 final ActivityStack stack = getStack();
Wale Ogunwalef6733932018-06-27 05:14:34 -07001441 return stack != null ? stack.shouldSleepActivities() : service.isSleepingLocked();
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001442 }
1443
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001444 /**
1445 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1446 * method will be called at the proper time.
1447 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001448 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001449 // The activity now gets access to the data associated with this Intent.
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001450 service.mUgmInternal.grantUriPermissionFromIntent(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001451 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001452 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001453 boolean unsent = true;
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001454 final boolean isTopActivityWhileSleeping = isTopRunningActivity() && isSleeping();
Wale Ogunwale826c7062016-09-13 08:25:54 -07001455
1456 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001457 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1458 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001459 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001460 if ((mState == RESUMED || mState == PAUSED || isTopActivityWhileSleeping)
1461 && attachedToProcess()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001462 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001463 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1464 ar.add(rintent);
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001465 service.getLifecycleManager().scheduleTransaction(
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001466 app.getThread(), appToken, NewIntentItem.obtain(ar, mState == PAUSED));
Craig Mautner86d67a42013-05-14 10:34:38 -07001467 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001468 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001469 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001470 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001471 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001472 }
1473 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001474 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001475 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476 }
1477 }
1478
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001479 void updateOptionsLocked(ActivityOptions options) {
1480 if (options != null) {
1481 if (pendingOptions != null) {
1482 pendingOptions.abort();
1483 }
1484 pendingOptions = options;
1485 }
1486 }
1487
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001488 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001489 if (pendingOptions != null
Ruben Brunkf53497c2017-03-27 20:26:17 -07001490 && pendingOptions.getAnimationType() != ANIM_SCENE_TRANSITION) {
lumark588a3e82018-07-20 18:53:54 +08001491 mWindowContainerController.applyOptionsLocked(pendingOptions, intent);
chaviw82a0ba82018-03-15 14:26:29 -07001492 if (task == null) {
1493 clearOptionsLocked(false /* withAbort */);
1494 } else {
1495 // This will clear the options for all the ActivityRecords for this Task.
1496 task.clearAllPendingOptions();
1497 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001498 }
1499 }
1500
Adam Powellcfbe9be2013-11-06 14:58:58 -08001501 ActivityOptions getOptionsForTargetActivityLocked() {
1502 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1503 }
1504
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001505 void clearOptionsLocked() {
chaviw82a0ba82018-03-15 14:26:29 -07001506 clearOptionsLocked(true /* withAbort */);
1507 }
1508
1509 void clearOptionsLocked(boolean withAbort) {
1510 if (withAbort && pendingOptions != null) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001511 pendingOptions.abort();
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001512 }
chaviw82a0ba82018-03-15 14:26:29 -07001513 pendingOptions = null;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001514 }
1515
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001516 ActivityOptions takeOptionsLocked() {
1517 ActivityOptions opts = pendingOptions;
1518 pendingOptions = null;
1519 return opts;
1520 }
1521
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001522 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001523 if (uriPermissions != null) {
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001524 uriPermissions.removeUriPermissions();
Dianne Hackborn7e269642010-08-25 19:50:20 -07001525 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 }
1527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 void pauseKeyDispatchingLocked() {
1530 if (!keysPaused) {
1531 keysPaused = true;
Bryce Lee2b8e0372018-04-05 17:01:37 -07001532
1533 if (mWindowContainerController != null) {
1534 mWindowContainerController.pauseKeyDispatching();
1535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 }
1537 }
1538
1539 void resumeKeyDispatchingLocked() {
1540 if (keysPaused) {
1541 keysPaused = false;
Bryce Lee2b8e0372018-04-05 17:01:37 -07001542
1543 if (mWindowContainerController != null) {
1544 mWindowContainerController.resumeKeyDispatching();
1545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 }
1547 }
1548
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001549 private void updateTaskDescription(CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001550 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001551 }
1552
Wale Ogunwaleec950642017-04-25 07:44:21 -07001553 void setDeferHidingClient(boolean deferHidingClient) {
1554 if (mDeferHidingClient == deferHidingClient) {
1555 return;
1556 }
1557 mDeferHidingClient = deferHidingClient;
1558 if (!mDeferHidingClient && !visible) {
1559 // Hiding the client is no longer deferred and the app isn't visible still, go ahead and
1560 // update the visibility.
1561 setVisibility(false);
1562 }
Wale Ogunwale89973222017-04-23 18:39:45 -07001563 }
1564
Wale Ogunwaleec950642017-04-25 07:44:21 -07001565 void setVisibility(boolean visible) {
Tim Murray68ed8442017-08-29 23:21:27 +00001566 mWindowContainerController.setVisibility(visible, mDeferHidingClient);
Bryce Lee2a3cc462017-10-27 10:57:35 -07001567 mStackSupervisor.getActivityMetricsLogger().notifyVisibilityChanged(this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001568 }
1569
1570 // TODO: Look into merging with #setVisibility()
Wale Ogunwaleec950642017-04-25 07:44:21 -07001571 void setVisible(boolean newVisible) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001572 visible = newVisible;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001573 mDeferHidingClient = !visible && mDeferHidingClient;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001574 setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001575 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1576 }
1577
Bryce Lee7ace3952018-02-16 14:34:32 -08001578 void setState(ActivityState state, String reason) {
1579 if (DEBUG_STATES) Slog.v(TAG_STATES, "State movement: " + this + " from:" + getState()
1580 + " to:" + state + " reason:" + reason);
Bryce Lee6ff17072018-02-28 07:26:17 -08001581
Bryce Leeb0f993f2018-03-02 15:38:01 -08001582 if (state == mState) {
1583 // No need to do anything if state doesn't change.
1584 if (DEBUG_STATES) Slog.v(TAG_STATES, "State unchanged from:" + state);
1585 return;
1586 }
1587
Bryce Leeb0f993f2018-03-02 15:38:01 -08001588 mState = state;
1589
Bryce Leec4ab62a2018-03-05 14:19:26 -08001590 final TaskRecord parent = getTask();
1591
1592 if (parent != null) {
1593 parent.onActivityStateChanged(this, state, reason);
1594 }
Robert Carr29daa922018-04-27 11:56:48 -07001595
Robert Carr9e1bf7c2018-05-31 15:39:07 -07001596 // The WindowManager interprets the app stopping signal as
1597 // an indication that the Surface will eventually be destroyed.
1598 // This however isn't necessarily true if we are going to sleep.
1599 if (state == STOPPING && !isSleeping()) {
Robert Carr29daa922018-04-27 11:56:48 -07001600 mWindowContainerController.notifyAppStopping();
1601 }
Bryce Lee7ace3952018-02-16 14:34:32 -08001602 }
1603
1604 ActivityState getState() {
1605 return mState;
1606 }
1607
1608 /**
1609 * Returns {@code true} if the Activity is in the specified state.
1610 */
1611 boolean isState(ActivityState state) {
1612 return state == mState;
1613 }
1614
1615 /**
1616 * Returns {@code true} if the Activity is in one of the specified states.
1617 */
1618 boolean isState(ActivityState state1, ActivityState state2) {
1619 return state1 == mState || state2 == mState;
1620 }
1621
1622 /**
1623 * Returns {@code true} if the Activity is in one of the specified states.
1624 */
1625 boolean isState(ActivityState state1, ActivityState state2, ActivityState state3) {
1626 return state1 == mState || state2 == mState || state3 == mState;
1627 }
1628
1629 /**
1630 * Returns {@code true} if the Activity is in one of the specified states.
1631 */
1632 boolean isState(ActivityState state1, ActivityState state2, ActivityState state3,
1633 ActivityState state4) {
1634 return state1 == mState || state2 == mState || state3 == mState || state4 == mState;
1635 }
1636
Jorim Jaggibae01b12017-04-11 16:29:10 -07001637 void notifyAppResumed(boolean wasStopped) {
1638 mWindowContainerController.notifyAppResumed(wasStopped);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001639 }
1640
1641 void notifyUnknownVisibilityLaunched() {
Jorim Jaggi838c2452017-08-28 15:44:43 +02001642
1643 // No display activities never add a window, so there is no point in waiting them for
1644 // relayout.
1645 if (!noDisplay) {
1646 mWindowContainerController.notifyUnknownVisibilityLaunched();
1647 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001648 }
1649
Jorim Jaggi241ae102016-11-02 21:57:33 -07001650 /**
1651 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1652 * might have on the visibility
1653 *
1654 * @see {@link ActivityStack#checkKeyguardVisibility}
1655 */
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001656 boolean shouldBeVisibleIgnoringKeyguard(boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001657 if (!okToShowLocked()) {
1658 return false;
1659 }
1660
Winson Chung3f0e59a2017-10-25 10:19:05 -07001661 return !behindFullscreenActivity || mLaunchTaskBehind;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001662 }
1663
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001664 void makeVisibleIfNeeded(ActivityRecord starting, boolean reportToClient) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001665 // This activity is not currently visible, but is running. Tell it to become visible.
Bryce Lee7ace3952018-02-16 14:34:32 -08001666 if (mState == RESUMED || this == starting) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001667 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
Bryce Lee7ace3952018-02-16 14:34:32 -08001668 "Not making visible, r=" + this + " state=" + mState + " starting=" + starting);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001669 return;
1670 }
1671
1672 // If this activity is paused, tell it to now show its window.
1673 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1674 "Making visible and scheduling visibility: " + this);
1675 final ActivityStack stack = getStack();
1676 try {
1677 if (stack.mTranslucentActivityWaiting != null) {
1678 updateOptionsLocked(returningOptions);
1679 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1680 }
1681 setVisible(true);
1682 sleeping = false;
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001683 app.postPendingUiCleanMsg(true);
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001684 if (reportToClient) {
1685 makeClientVisible();
1686 } else {
1687 mClientVisibilityDeferred = true;
1688 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001689 // The activity may be waiting for stop, but that is no longer appropriate for it.
1690 mStackSupervisor.mStoppingActivities.remove(this);
1691 mStackSupervisor.mGoingToSleepActivities.remove(this);
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001692 } catch (Exception e) {
1693 // Just skip on any failure; we'll make it visible when it next restarts.
1694 Slog.w(TAG, "Exception thrown making visible: " + intent.getComponent(), e);
1695 }
1696 handleAlreadyVisible();
1697 }
Andrii Kulian0d595f32018-02-21 15:47:33 -08001698
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001699 /** Send visibility change message to the client and pause if needed. */
1700 void makeClientVisible() {
1701 mClientVisibilityDeferred = false;
1702 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001703 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001704 WindowVisibilityItem.obtain(true /* showWindow */));
Andrii Kulian09e1afa2018-05-16 21:29:34 -07001705 if (shouldPauseWhenBecomingVisible()) {
Andrii Kulian0d595f32018-02-21 15:47:33 -08001706 // An activity must be in the {@link PAUSING} state for the system to validate
1707 // the move to {@link PAUSED}.
Bryce Lee7ace3952018-02-16 14:34:32 -08001708 setState(PAUSING, "makeVisibleIfNeeded");
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001709 service.getLifecycleManager().scheduleTransaction(app.getThread(), appToken,
Andrii Kulian0d595f32018-02-21 15:47:33 -08001710 PauseActivityItem.obtain(finishing, false /* userLeaving */,
Bryce Lee1d0d5142018-04-12 10:35:07 -07001711 configChangeFlags, false /* dontReport */));
Andrii Kulian0d595f32018-02-21 15:47:33 -08001712 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001713 } catch (Exception e) {
Andrii Kuliana39ae3e2018-05-31 12:43:54 -07001714 Slog.w(TAG, "Exception thrown sending visibility update: " + intent.getComponent(), e);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001715 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001716 }
1717
Andrii Kulian09e1afa2018-05-16 21:29:34 -07001718 /** Check if activity should be moved to PAUSED state when it becomes visible. */
1719 private boolean shouldPauseWhenBecomingVisible() {
1720 // If the activity is stopped or stopping, cycle to the paused state. We avoid doing
1721 // this when there is an activity waiting to become translucent as the extra binder
1722 // calls will lead to noticeable jank. A later call to
1723 // ActivityStack#ensureActivitiesVisibleLocked will bring the activity to the proper
1724 // paused state. We also avoid doing this for the activity the stack supervisor
1725 // considers the resumed activity, as normal means will bring the activity from STOPPED
1726 // to RESUMED. Adding PAUSING in this scenario will lead to double lifecycles.
1727 if (!isState(STOPPED, STOPPING) || getStack().mTranslucentActivityWaiting != null
Andrii Kulian52d255c2018-07-13 11:32:19 -07001728 || isResumedActivityOnDisplay()) {
Andrii Kulian09e1afa2018-05-16 21:29:34 -07001729 return false;
1730 }
1731
1732 // Check if position in task allows to become paused
1733 final int positionInTask = task.mActivities.indexOf(this);
1734 if (positionInTask == -1) {
1735 throw new IllegalStateException("Activity not found in its task");
1736 }
1737 if (positionInTask == task.mActivities.size() - 1) {
1738 // It's the topmost activity in the task - should become paused now
1739 return true;
1740 }
1741 // Check if activity above is finishing now and this one becomes the topmost in task.
1742 final ActivityRecord activityAbove = task.mActivities.get(positionInTask + 1);
1743 if (activityAbove.finishing && results == null) {
1744 // We will only allow pausing if activity above wasn't launched for result. Otherwise it
1745 // will cause this activity to resume before getting result.
1746 return true;
1747 }
1748 return false;
1749 }
1750
Andrii Kulian21713ac2016-10-12 22:05:05 -07001751 boolean handleAlreadyVisible() {
1752 stopFreezingScreenLocked(false);
1753 try {
1754 if (returningOptions != null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001755 app.getThread().scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001756 }
1757 } catch(RemoteException e) {
1758 }
Bryce Lee7ace3952018-02-16 14:34:32 -08001759 return mState == RESUMED;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001760 }
1761
1762 static void activityResumedLocked(IBinder token) {
1763 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1764 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1765 if (r != null) {
1766 r.icicle = null;
1767 r.haveState = false;
1768 }
1769 }
1770
1771 /**
1772 * Once we know that we have asked an application to put an activity in the resumed state
1773 * (either by launching it or explicitly telling it), this function updates the rest of our
1774 * state to match that fact.
1775 */
1776 void completeResumeLocked() {
1777 final boolean wasVisible = visible;
Jorim Jaggi8d062052017-08-22 14:55:17 +02001778 setVisible(true);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001779 if (!wasVisible) {
1780 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1781 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1782 }
1783 idle = false;
1784 results = null;
1785 newIntents = null;
1786 stopped = false;
1787
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07001788 if (isActivityTypeHome()) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001789 WindowProcessController app = task.mActivities.get(0).app;
1790 if (hasProcess() && app != service.mHomeProcess) {
1791 service.mHomeProcess = app;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001792 }
1793 }
1794
1795 if (nowVisible) {
1796 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1797 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001798 }
1799
1800 // Schedule an idle timeout in case the app doesn't do it for us.
1801 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1802
1803 mStackSupervisor.reportResumedActivityLocked(this);
1804
1805 resumeKeyDispatchingLocked();
1806 final ActivityStack stack = getStack();
Jorim Jaggifa9ed962018-01-25 00:16:49 +01001807 mStackSupervisor.mNoAnimActivities.clear();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001808
1809 // Mark the point when the activity is resuming
1810 // TODO: To be more accurate, the mark should be before the onCreate,
1811 // not after the onResume. But for subsequent starts, onResume is fine.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001812 if (hasProcess()) {
Wale Ogunwale86b74462018-07-02 08:42:43 -07001813 cpuTimeAtResume = app.getCpuTime();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001814 } else {
1815 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1816 }
1817
1818 returningOptions = null;
chaviw59b98852017-06-13 12:05:44 -07001819
1820 if (canTurnScreenOn()) {
1821 mStackSupervisor.wakeUp("turnScreenOnFlag");
1822 } else {
1823 // If the screen is going to turn on because the caller explicitly requested it and
1824 // the keyguard is not showing don't attempt to sleep. Otherwise the Activity will
1825 // pause and then resume again later, which will result in a double life-cycle event.
David Stevens9440dc82017-03-16 19:00:20 -07001826 stack.checkReadyForSleep();
chaviw59b98852017-06-13 12:05:44 -07001827 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001828 }
1829
1830 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1831 CharSequence description) {
1832 final ActivityStack stack = getStack();
Bryce Lee7ace3952018-02-16 14:34:32 -08001833 if (mState != STOPPING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001834 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
Ruben Brunkf53497c2017-03-27 20:26:17 -07001835 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001836 return;
1837 }
1838 if (newPersistentState != null) {
1839 persistentState = newPersistentState;
1840 service.notifyTaskPersisterLocked(task, false);
1841 }
1842 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001843
Andrii Kulian21713ac2016-10-12 22:05:05 -07001844 if (newIcicle != null) {
1845 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1846 // the state.
1847 icicle = newIcicle;
1848 haveState = true;
1849 launchCount = 0;
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001850 updateTaskDescription(description);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001851 }
1852 if (!stopped) {
1853 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
Ruben Brunkf53497c2017-03-27 20:26:17 -07001854 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001855 stopped = true;
Bryce Lee7ace3952018-02-16 14:34:32 -08001856 setState(STOPPED, "activityStoppedLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001857
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001858 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001859
Andrii Kulian21713ac2016-10-12 22:05:05 -07001860 if (finishing) {
1861 clearOptionsLocked();
1862 } else {
1863 if (deferRelaunchUntilPaused) {
1864 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
Andrii Kulianab132ee2018-07-24 22:10:21 +08001865 mStackSupervisor.resumeFocusedStacksTopActivitiesLocked();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001866 } else {
1867 mStackSupervisor.updatePreviousProcessLocked(this);
1868 }
1869 }
1870 }
1871 }
1872
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001873 void startLaunchTickingLocked() {
Jeff Sharkey5ab02432017-06-27 11:01:36 -06001874 if (Build.IS_USER) {
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001875 return;
1876 }
1877 if (launchTickTime == 0) {
1878 launchTickTime = SystemClock.uptimeMillis();
1879 continueLaunchTickingLocked();
1880 }
1881 }
1882
1883 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001884 if (launchTickTime == 0) {
1885 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001886 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001887
Andrii Kulian02b7a832016-10-06 23:11:56 -07001888 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001889 if (stack == null) {
1890 return false;
1891 }
1892
Ruben Brunkf53497c2017-03-27 20:26:17 -07001893 Message msg = stack.mHandler.obtainMessage(LAUNCH_TICK_MSG, this);
1894 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
1895 stack.mHandler.sendMessageDelayed(msg, LAUNCH_TICK);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001896 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001897 }
1898
1899 void finishLaunchTickingLocked() {
1900 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001901 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001902 if (stack != null) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001903 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001904 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001905 }
1906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 // IApplicationToken
1908
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001909 public boolean mayFreezeScreenLocked(WindowProcessController app) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 // Only freeze the screen if this activity is currently attached to
1911 // an application, and that application is not blocked or unresponding.
1912 // In any other case, we can't count on getting the screen unfrozen,
1913 // so it is best to leave as-is.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001914 return hasProcess() && !app.isCrashing() && !app.isNotResponding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001916
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001917 public void startFreezingScreenLocked(WindowProcessController app, int configChanges) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001919 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
1921 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 public void stopFreezingScreenLocked(boolean force) {
1924 if (force || frozenBeforeDestroy) {
1925 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001926 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
1928 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001929
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001930 public void reportFullyDrawnLocked(boolean restoredFromBundle) {
Vishnu Nair132ee832018-09-28 15:00:05 -07001931 final WindowingModeTransitionInfoSnapshot info = mStackSupervisor
1932 .getActivityMetricsLogger().logAppTransitionReportedDrawn(this, restoredFromBundle);
1933 if (info != null) {
1934 mStackSupervisor.reportActivityLaunchedLocked(false /* timeout */, this,
1935 info.windowsFullyDrawnDelayMs);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001936 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001937 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001938 @Override
Sudheer Shankac766db02017-06-12 10:37:29 -07001939 public void onStartingWindowDrawn(long timestamp) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001940 synchronized (service.mGlobalLock) {
Bryce Lee2a3cc462017-10-27 10:57:35 -07001941 mStackSupervisor.getActivityMetricsLogger().notifyStartingWindowDrawn(
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +01001942 getWindowingMode(), timestamp);
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001943 }
1944 }
1945
1946 @Override
Sudheer Shankac766db02017-06-12 10:37:29 -07001947 public void onWindowsDrawn(long timestamp) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001948 synchronized (service.mGlobalLock) {
Vishnu Nair132ee832018-09-28 15:00:05 -07001949 final WindowingModeTransitionInfoSnapshot info = mStackSupervisor
1950 .getActivityMetricsLogger().notifyWindowsDrawn(getWindowingMode(), timestamp);
1951 final int windowsDrawnDelayMs = info != null ? info.windowsDrawnDelayMs : INVALID_DELAY;
1952 mStackSupervisor.reportActivityLaunchedLocked(false /* timeout */, this,
1953 windowsDrawnDelayMs);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001954 mStackSupervisor.sendWaitingVisibleReportLocked(this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001955 finishLaunchTickingLocked();
1956 if (task != null) {
1957 task.hasBeenVisible = true;
1958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
1960 }
1961
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001962 @Override
1963 public void onWindowsVisible() {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001964 synchronized (service.mGlobalLock) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001965 mStackSupervisor.reportActivityVisibleLocked(this);
1966 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
1967 if (!nowVisible) {
1968 nowVisible = true;
1969 lastVisibleTime = SystemClock.uptimeMillis();
Bryce Leeb7c9b802017-05-02 14:20:24 -07001970 if (idle || mStackSupervisor.isStoppingNoHistoryActivity()) {
1971 // If this activity was already idle or there is an activity that must be
1972 // stopped immediately after visible, then we now need to make sure we perform
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001973 // the full stop of any activities that are waiting to do so. This is because
1974 // we won't do that while they are still waiting for this one to become visible.
Bryce Lee4a194382017-04-04 14:32:48 -07001975 final int size = mStackSupervisor.mActivitiesWaitingForVisibleActivity.size();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001976 if (size > 0) {
1977 for (int i = 0; i < size; i++) {
Bryce Lee4a194382017-04-04 14:32:48 -07001978 final ActivityRecord r =
1979 mStackSupervisor.mActivitiesWaitingForVisibleActivity.get(i);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001980 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
1981 }
Bryce Lee4a194382017-04-04 14:32:48 -07001982 mStackSupervisor.mActivitiesWaitingForVisibleActivity.clear();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001983 mStackSupervisor.scheduleIdleLocked();
1984 }
Bryce Leeb7c9b802017-05-02 14:20:24 -07001985 } else {
1986 // Instead of doing the full stop routine here, let's just hide any activities
1987 // we now can, and let them stop when the normal idle happens.
1988 mStackSupervisor.processStoppingActivitiesLocked(null /* idleActivity */,
1989 false /* remove */, true /* processPausingActivities */);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001990 }
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001991 service.scheduleAppGcsLocked();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001992 }
1993 }
1994 }
1995
1996 @Override
1997 public void onWindowsGone() {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07001998 synchronized (service.mGlobalLock) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001999 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
2000 nowVisible = false;
2001 }
2002 }
2003
2004 @Override
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002005 public boolean keyDispatchingTimedOut(String reason, int windowPid) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002006 ActivityRecord anrActivity;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002007 WindowProcessController anrApp;
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002008 boolean windowFromSameProcessAsActivity;
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002009 synchronized (service.mGlobalLock) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002010 anrActivity = getWaitingHistoryRecordLocked();
2011 anrApp = app;
Brian Carlstrom7b0f2e82017-03-31 00:24:18 -07002012 windowFromSameProcessAsActivity =
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002013 !hasProcess() || app.getPid() == windowPid || windowPid == -1;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002014 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07002015
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002016 if (windowFromSameProcessAsActivity) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07002017 return service.mAmInternal.inputDispatchingTimedOut(anrApp.mOwner,
2018 anrActivity.shortComponentName, anrActivity.appInfo, shortComponentName,
2019 app, false, reason);
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002020 } else {
2021 // In this case another process added windows using this activity token. So, we call the
2022 // generic service input dispatch timed out method so that the right process is blamed.
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07002023 return service.mAmInternal.inputDispatchingTimedOut(
2024 windowPid, false /* aboveSystem */, reason) < 0;
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07002025 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002026 }
2027
2028 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08002029 // First find the real culprit... if this activity is waiting for
2030 // another activity to start or has stopped, then the key dispatching
2031 // timeout should not be caused by this.
Bryce Lee4a194382017-04-04 14:32:48 -07002032 if (mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this) || stopped) {
Andrii Kulian5f750bc2018-07-17 08:57:23 -07002033 final ActivityStack stack = mStackSupervisor.getTopDisplayFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08002034 // Try to use the one which is closest to top.
Bryce Leec4ab62a2018-03-05 14:19:26 -08002035 ActivityRecord r = stack.getResumedActivity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002037 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08002039 if (r != null) {
2040 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 }
2042 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08002043 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
2045
Chong Zhang87761972016-08-22 13:53:24 -07002046 /** Checks whether the activity should be shown for current user. */
2047 public boolean okToShowLocked() {
Bryce Lee8558ec72017-08-17 15:37:26 -07002048 // We cannot show activities when the device is locked and the application is not
2049 // encryption aware.
2050 if (!StorageManager.isUserKeyUnlocked(userId)
2051 && !info.applicationInfo.isEncryptionAware()) {
2052 return false;
2053 }
2054
Chong Zhang87761972016-08-22 13:53:24 -07002055 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
2056 || (mStackSupervisor.isCurrentProfileLocked(userId)
Wale Ogunwale86b74462018-07-02 08:42:43 -07002057 && service.mAmInternal.isUserRunning(userId, 0 /* flags */));
Chong Zhang87761972016-08-22 13:53:24 -07002058 }
2059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 /**
2061 * This method will return true if the activity is either visible, is becoming visible, is
2062 * currently pausing, or is resumed.
2063 */
2064 public boolean isInterestingToUserLocked() {
Bryce Lee7ace3952018-02-16 14:34:32 -08002065 return visible || nowVisible || mState == PAUSING || mState == RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002066 }
2067
Wale Ogunwale3e997362016-09-06 10:37:56 -07002068 void setSleeping(boolean _sleeping) {
2069 setSleeping(_sleeping, false);
2070 }
2071
2072 void setSleeping(boolean _sleeping, boolean force) {
2073 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002074 return;
2075 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002076 if (attachedToProcess()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002077 try {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002078 app.getThread().scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002079 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
2080 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002081 }
2082 sleeping = _sleeping;
2083 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002084 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002085 }
2086 }
2087 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08002088
Craig Mautnerd2328952013-03-05 12:46:26 -08002089 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002090 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08002091 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002092 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08002093 }
2094 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002095 final int activityNdx = task.mActivities.indexOf(r);
2096 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002097 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08002098 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002099 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08002100 }
2101
2102 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002103 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07002104 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08002105 }
2106
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002107 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002108 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
2109 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002110 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08002111 }
2112 return null;
2113 }
2114
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002115 /**
Riddle Hsufd4a0502018-10-16 01:05:16 +08002116 * @return display id to which this record is attached,
2117 * {@link android.view.Display#INVALID_DISPLAY} if not attached.
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002118 */
2119 int getDisplayId() {
2120 final ActivityStack stack = getStack();
2121 if (stack == null) {
Riddle Hsufd4a0502018-10-16 01:05:16 +08002122 return INVALID_DISPLAY;
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002123 }
2124 return stack.mDisplayId;
2125 }
2126
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002127 final boolean isDestroyable() {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002128 if (finishing || !hasProcess()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002129 // This would be redundant.
2130 return false;
2131 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002132 final ActivityStack stack = getStack();
Bryce Leec4ab62a2018-03-05 14:19:26 -08002133 if (stack == null || this == stack.getResumedActivity() || this == stack.mPausingActivity
Andrii Kulian02b7a832016-10-06 23:11:56 -07002134 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002135 // We're not ready for this kind of thing.
2136 return false;
2137 }
2138 if (visible) {
2139 // The user would notice this!
2140 return false;
2141 }
2142 return true;
2143 }
2144
Winson Chung3bad5cc02014-08-19 17:44:32 -07002145 private static String createImageFilename(long createTime, int taskId) {
2146 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Ruben Brunkf53497c2017-03-27 20:26:17 -07002147 IMAGE_EXTENSION;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002148 }
2149
Craig Mautner648f69b2014-09-18 14:16:26 -07002150 void setTaskDescription(TaskDescription _taskDescription) {
2151 Bitmap icon;
2152 if (_taskDescription.getIconFilename() == null &&
2153 (icon = _taskDescription.getIcon()) != null) {
2154 final String iconFilename = createImageFilename(createTime, task.taskId);
Winson Chungc8408b82017-01-25 17:58:56 -08002155 final File iconFile = new File(TaskPersister.getUserImagesDir(task.userId),
2156 iconFilename);
Suprabh Shukla23593142015-11-03 17:31:15 -08002157 final String iconFilePath = iconFile.getAbsolutePath();
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002158 service.getRecentTasks().saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08002159 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07002160 }
2161 taskDescription = _taskDescription;
2162 }
2163
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002164 void setVoiceSessionLocked(IVoiceInteractionSession session) {
2165 voiceSession = session;
2166 pendingVoiceInteractionStart = false;
2167 }
2168
2169 void clearVoiceSessionLocked() {
2170 voiceSession = null;
2171 pendingVoiceInteractionStart = false;
2172 }
2173
Jorim Jaggi02886a82016-12-06 09:10:06 -08002174 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch) {
Jorim Jaggi42befc62017-06-13 11:54:04 -07002175 showStartingWindow(prev, newTask, taskSwitch, false /* fromRecents */);
2176 }
2177
2178 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch,
2179 boolean fromRecents) {
Jorim Jaggi70176432017-01-18 12:52:13 +01002180 if (mWindowContainerController == null) {
2181 return;
2182 }
Wale Ogunwale19866e22017-04-19 06:05:13 -07002183 if (mTaskOverlay) {
2184 // We don't show starting window for overlay activities.
2185 return;
2186 }
Sunny Goyald85bed52018-09-25 12:01:01 -07002187 if (pendingOptions != null
2188 && pendingOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
2189 // Don't show starting window when using shared element transition.
2190 return;
2191 }
Wale Ogunwale19866e22017-04-19 06:05:13 -07002192
Wale Ogunwale3b232392016-05-13 15:37:13 -07002193 final CompatibilityInfo compatInfo =
Wale Ogunwale53783742018-09-16 10:21:51 -07002194 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002195 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
2196 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
Jorim Jaggibae01b12017-04-11 16:29:10 -07002197 prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning(),
Jorim Jaggi70aa4d12017-05-15 00:05:54 +02002198 allowTaskSnapshot(),
Bryce Lee7ace3952018-02-16 14:34:32 -08002199 mState.ordinal() >= RESUMED.ordinal() && mState.ordinal() <= STOPPED.ordinal(),
Jorim Jaggi42befc62017-06-13 11:54:04 -07002200 fromRecents);
Wale Ogunwale3b232392016-05-13 15:37:13 -07002201 if (shown) {
2202 mStartingWindowState = STARTING_WINDOW_SHOWN;
2203 }
2204 }
2205
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002206 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
Jorim Jaggicb956052017-05-09 16:27:24 +02002207 if (mStartingWindowState == STARTING_WINDOW_SHOWN && behindFullscreenActivity) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002208 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
2209 mStartingWindowState = STARTING_WINDOW_REMOVED;
Jorim Jaggi19be6052017-08-03 18:33:43 +02002210 mWindowContainerController.removeStartingWindow();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002211 }
2212 }
2213
2214 int getRequestedOrientation() {
2215 return mWindowContainerController.getOrientation();
2216 }
2217
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002218 void setRequestedOrientation(int requestedOrientation) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002219 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002220 final Configuration displayConfig =
2221 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
2222
2223 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
2224 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002225 if (config != null) {
2226 frozenBeforeDestroy = true;
Wale Ogunwalea6191b42018-05-09 07:41:32 -07002227 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002228 false /* deferResume */, displayId)) {
Andrii Kulianab132ee2018-07-24 22:10:21 +08002229 mStackSupervisor.resumeFocusedStacksTopActivitiesLocked();
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002230 }
2231 }
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002232 service.getTaskChangeNotificationController().notifyActivityRequestedOrientationChanged(
Yorke Leebd54c2a2016-10-25 13:49:23 -07002233 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002234 }
2235
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01002236 void setDisablePreviewScreenshots(boolean disable) {
2237 mWindowContainerController.setDisablePreviewScreenshots(disable);
2238 }
2239
Bryce Leea163b762017-01-24 11:05:01 -08002240 /**
2241 * Set the last reported global configuration to the client. Should be called whenever a new
2242 * global configuration is sent to the client for this activity.
2243 */
2244 void setLastReportedGlobalConfiguration(@NonNull Configuration config) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002245 mLastReportedConfiguration.setGlobalConfiguration(config);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002246 }
2247
Bryce Leea163b762017-01-24 11:05:01 -08002248 /**
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002249 * Set the last reported configuration to the client. Should be called whenever
Bryce Leea163b762017-01-24 11:05:01 -08002250 * a new merged configuration is sent to the client for this activity.
2251 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002252 void setLastReportedConfiguration(@NonNull MergedConfiguration config) {
Bryce Lee8104e7a2017-08-17 09:16:03 -07002253 setLastReportedConfiguration(config.getGlobalConfiguration(),
2254 config.getOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08002255 }
2256
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -07002257 private void setLastReportedConfiguration(Configuration global, Configuration override) {
Bryce Lee8104e7a2017-08-17 09:16:03 -07002258 mLastReportedConfiguration.setConfiguration(global, override);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002259 }
2260
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002261 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002262 private void updateOverrideConfiguration() {
2263 mTmpConfig.unset();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002264 computeBounds(mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08002265
2266 if (mTmpBounds.equals(getOverrideBounds())) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002267 return;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002268 }
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002269
Bryce Leef3c6a472017-11-14 14:53:06 -08002270 setBounds(mTmpBounds);
2271
2272 final Rect updatedBounds = getOverrideBounds();
2273
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002274 // Bounds changed...update configuration to match.
Bryce Leef3c6a472017-11-14 14:53:06 -08002275 if (!matchParentBounds()) {
2276 task.computeOverrideConfiguration(mTmpConfig, updatedBounds, null /* insetBounds */,
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002277 false /* overrideWidth */, false /* overrideHeight */);
2278 }
Bryce Leef3c6a472017-11-14 14:53:06 -08002279
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002280 onOverrideConfigurationChanged(mTmpConfig);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002281 }
2282
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002283 /** Returns true if the configuration is compatible with this activity. */
Wale Ogunwale42f07d92017-05-01 21:32:58 -07002284 boolean isConfigurationCompatible(Configuration config) {
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002285 final int orientation = mWindowContainerController != null
2286 ? mWindowContainerController.getOrientation() : info.screenOrientation;
2287 if (isFixedOrientationPortrait(orientation)
2288 && config.orientation != ORIENTATION_PORTRAIT) {
2289 return false;
2290 }
2291 if (isFixedOrientationLandscape(orientation)
2292 && config.orientation != ORIENTATION_LANDSCAPE) {
2293 return false;
2294 }
2295 return true;
2296 }
2297
Bryce Lee7566d762017-03-30 09:34:15 -07002298 /**
2299 * Computes the bounds to fit the Activity within the bounds of the {@link Configuration}.
2300 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002301 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
2302 private void computeBounds(Rect outBounds) {
2303 outBounds.setEmpty();
2304 final float maxAspectRatio = info.maxAspectRatio;
2305 final ActivityStack stack = getStack();
Bryce Leef3c6a472017-11-14 14:53:06 -08002306 if (task == null || stack == null || task.inMultiWindowMode() || maxAspectRatio == 0
Bryce Leee5ab4502017-07-11 08:58:05 -07002307 || isInVrUiMode(getConfiguration())) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002308 // We don't set override configuration if that activity task isn't fullscreen. I.e. the
2309 // activity is in multi-window mode. Or, there isn't a max aspect ratio specified for
Bryce Leee5ab4502017-07-11 08:58:05 -07002310 // the activity. This is indicated by an empty {@link outBounds}. We also don't set it
2311 // if we are in VR mode.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002312 return;
2313 }
2314
Bryce Lee7566d762017-03-30 09:34:15 -07002315 // We must base this on the parent configuration, because we set our override
2316 // configuration's appBounds based on the result of this method. If we used our own
2317 // configuration, it would be influenced by past invocations.
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002318 final Rect appBounds = getParent().getWindowConfiguration().getAppBounds();
Wale Ogunwale822e5122017-07-26 06:02:24 -07002319 final int containingAppWidth = appBounds.width();
2320 final int containingAppHeight = appBounds.height();
Bryce Lee7566d762017-03-30 09:34:15 -07002321 int maxActivityWidth = containingAppWidth;
2322 int maxActivityHeight = containingAppHeight;
2323
2324 if (containingAppWidth < containingAppHeight) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002325 // Width is the shorter side, so we use that to figure-out what the max. height
2326 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002327 maxActivityHeight = (int) ((maxActivityWidth * maxAspectRatio) + 0.5f);
2328 } else {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002329 // Height is the shorter side, so we use that to figure-out what the max. width
2330 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002331 maxActivityWidth = (int) ((maxActivityHeight * maxAspectRatio) + 0.5f);
2332 }
2333
Bryce Lee7566d762017-03-30 09:34:15 -07002334 if (containingAppWidth <= maxActivityWidth && containingAppHeight <= maxActivityHeight) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002335 // The display matches or is less than the activity aspect ratio, so nothing else to do.
Bryce Lee7566d762017-03-30 09:34:15 -07002336 // Return the existing bounds. If this method is running for the first time,
Bryce Leef3c6a472017-11-14 14:53:06 -08002337 // {@link #getOverrideBounds()} will be empty (representing no override). If the method has run
2338 // before, then effect of {@link #getOverrideBounds()} will already have been applied to the
Bryce Lee7566d762017-03-30 09:34:15 -07002339 // value returned from {@link getConfiguration}. Refer to
2340 // {@link TaskRecord#computeOverrideConfiguration}.
Bryce Leef3c6a472017-11-14 14:53:06 -08002341 outBounds.set(getOverrideBounds());
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002342 return;
2343 }
2344
2345 // Compute configuration based on max supported width and height.
Adrian Roos24be34d2018-05-28 18:55:38 +02002346 // Also account for the left / top insets (e.g. from display cutouts), which will be clipped
2347 // away later in StackWindowController.adjustConfigurationForBounds(). Otherwise, the app
2348 // bounds would end up too small.
2349 outBounds.set(0, 0, maxActivityWidth + appBounds.left, maxActivityHeight + appBounds.top);
2350
2351 if (service.mWindowManager.getNavBarPosition() == NAV_BAR_LEFT) {
2352 // Position the activity frame on the opposite side of the nav bar.
2353 outBounds.left = appBounds.right - maxActivityWidth;
2354 outBounds.right = appBounds.right;
2355 }
Andrii Kulian3a1619d2017-07-07 14:38:09 -07002356 }
2357
Riddle Hsu16567132018-08-16 21:37:47 +08002358 /**
2359 * @return {@code true} if this activity was reparented to another display but
2360 * {@link #ensureActivityConfiguration} is not called.
2361 */
2362 boolean shouldUpdateConfigForDisplayChanged() {
2363 return mLastReportedDisplayId != getDisplayId();
2364 }
2365
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002366 boolean ensureActivityConfiguration(int globalChanges, boolean preserveWindow) {
2367 return ensureActivityConfiguration(globalChanges, preserveWindow,
2368 false /* ignoreStopState */);
2369 }
2370
Andrii Kulian21713ac2016-10-12 22:05:05 -07002371 /**
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002372 * Make sure the given activity matches the current configuration. Ensures the HistoryRecord
2373 * is updated with the correct configuration and all other bookkeeping is handled.
2374 *
2375 * @param globalChanges The changes to the global configuration.
2376 * @param preserveWindow If the activity window should be preserved on screen if the activity
2377 * is relaunched.
2378 * @param ignoreStopState If we should try to relaunch the activity even if it is in the stopped
2379 * state. This is useful for the case where we know the activity will be
2380 * visible soon and we want to ensure its configuration before we make it
2381 * visible.
2382 * @return True if the activity was relaunched and false if it wasn't relaunched because we
2383 * can't or the app handles the specific configuration that is changing.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002384 */
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002385 boolean ensureActivityConfiguration(int globalChanges, boolean preserveWindow,
2386 boolean ignoreStopState) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002387 final ActivityStack stack = getStack();
2388 if (stack.mConfigWillChange) {
2389 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2390 "Skipping config check (will change): " + this);
2391 return true;
2392 }
2393
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002394 // We don't worry about activities that are finishing.
2395 if (finishing) {
2396 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2397 "Configuration doesn't matter in finishing " + this);
2398 stopFreezingScreenLocked(false);
2399 return true;
2400 }
2401
Wale Ogunwaleb6d75f32018-02-22 20:44:56 -08002402 if (!ignoreStopState && (mState == STOPPING || mState == STOPPED)) {
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002403 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2404 "Skipping config check stopped or stopping: " + this);
2405 return true;
2406 }
2407
Wale Ogunwaleea3d3fd2017-05-01 07:41:08 -07002408 // TODO: We should add ActivityRecord.shouldBeVisible() that checks if the activity should
2409 // be visible based on the stack, task, and lockscreen state and use that here instead. The
2410 // method should be based on the logic in ActivityStack.ensureActivitiesVisibleLocked().
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002411 // Skip updating configuration for activity is a stack that shouldn't be visible.
Wale Ogunwale9dcf9462017-09-19 15:13:01 -07002412 if (!stack.shouldBeVisible(null /* starting */)) {
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002413 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2414 "Skipping config check invisible stack: " + this);
2415 return true;
2416 }
2417
Andrii Kulian21713ac2016-10-12 22:05:05 -07002418 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2419 "Ensuring correct configuration: " + this);
2420
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002421 final int newDisplayId = getDisplayId();
2422 final boolean displayChanged = mLastReportedDisplayId != newDisplayId;
2423 if (displayChanged) {
2424 mLastReportedDisplayId = newDisplayId;
2425 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002426 // TODO(b/36505427): Is there a better place to do this?
2427 updateOverrideConfiguration();
2428
Winson Chungbdc646f2017-02-13 12:12:22 -08002429 // Short circuit: if the two full configurations are equal (the common case), then there is
2430 // nothing to do. We test the full configuration instead of the global and merged override
2431 // configurations because there are cases (like moving a task to the pinned stack) where
2432 // the combine configurations are equal, but would otherwise differ in the override config
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002433 mTmpConfig.setTo(mLastReportedConfiguration.getMergedConfiguration());
2434 if (getConfiguration().equals(mTmpConfig) && !forceNewConfig && !displayChanged) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002435 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002436 "Configuration & display unchanged in " + this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002437 return true;
2438 }
2439
2440 // Okay we now are going to make this activity have the new config.
2441 // But then we need to figure out how it needs to deal with that.
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002442
2443 // Find changes between last reported merged configuration and the current one. This is used
2444 // to decide whether to relaunch an activity or just report a configuration change.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002445 final int changes = getConfigurationChanges(mTmpConfig);
Ruben Brunkf64af332017-03-22 22:03:25 -07002446
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002447 // Update last reported values.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002448 final Configuration newMergedOverrideConfig = getMergedOverrideConfiguration();
Bryce Lee8104e7a2017-08-17 09:16:03 -07002449
Wale Ogunwalef6733932018-06-27 05:14:34 -07002450 setLastReportedConfiguration(service.getGlobalConfiguration(), newMergedOverrideConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002451
Bryce Lee7ace3952018-02-16 14:34:32 -08002452 if (mState == INITIALIZING) {
Andrii Kulianb372da62018-01-18 10:46:24 -08002453 // No need to relaunch or schedule new config for activity that hasn't been launched
2454 // yet. We do, however, return after applying the config to activity record, so that
2455 // it will use it for launch transaction.
2456 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2457 "Skipping config check for initializing activity: " + this);
2458 return true;
2459 }
2460
Andrii Kulian21713ac2016-10-12 22:05:05 -07002461 if (changes == 0 && !forceNewConfig) {
2462 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2463 "Configuration no differences in " + this);
2464 // There are no significant differences, so we won't relaunch but should still deliver
2465 // the new configuration to the client process.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002466 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002467 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002468 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002469 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002470 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002471 return true;
2472 }
2473
2474 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002475 "Configuration changes for " + this + ", allChanges="
Andrii Kulian21713ac2016-10-12 22:05:05 -07002476 + Configuration.configurationDiffToString(changes));
2477
2478 // If the activity isn't currently running, just leave the new configuration and it will
2479 // pick that up next time it starts.
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002480 if (!attachedToProcess()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002481 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2482 "Configuration doesn't matter not running " + this);
2483 stopFreezingScreenLocked(false);
2484 forceNewConfig = false;
2485 return true;
2486 }
2487
2488 // Figure out how to handle the changes between the configurations.
2489 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2490 "Checking to restart " + info.name + ": changed=0x"
2491 + Integer.toHexString(changes) + ", handles=0x"
2492 + Integer.toHexString(info.getRealConfigChanged())
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002493 + ", mLastReportedConfiguration=" + mLastReportedConfiguration);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002494
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002495 if (shouldRelaunchLocked(changes, mTmpConfig) || forceNewConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002496 // Aha, the activity isn't handling the change, so DIE DIE DIE.
2497 configChangeFlags |= changes;
2498 startFreezingScreenLocked(app, globalChanges);
2499 forceNewConfig = false;
2500 preserveWindow &= isResizeOnlyChange(changes);
Garfield Tan2746ab52018-07-25 12:33:01 -07002501 final boolean hasResizeChange = hasResizeChange(changes & ~info.getRealConfigChanged());
2502 if (hasResizeChange) {
2503 final boolean isDragResizing =
2504 getTask().getWindowContainerController().isDragResizing();
2505 mRelaunchReason = isDragResizing ? RELAUNCH_REASON_FREE_RESIZE
2506 : RELAUNCH_REASON_WINDOWING_MODE_RESIZE;
2507 } else {
2508 mRelaunchReason = RELAUNCH_REASON_NONE;
2509 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002510 if (!attachedToProcess()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002511 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2512 "Config is destroying non-running " + this);
2513 stack.destroyActivityLocked(this, true, "config");
Bryce Lee7ace3952018-02-16 14:34:32 -08002514 } else if (mState == PAUSING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002515 // A little annoying: we are waiting for this activity to finish pausing. Let's not
2516 // do anything now, but just flag that it needs to be restarted when done pausing.
2517 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2518 "Config is skipping already pausing " + this);
2519 deferRelaunchUntilPaused = true;
2520 preserveWindowOnDeferredRelaunch = preserveWindow;
2521 return true;
Bryce Lee7ace3952018-02-16 14:34:32 -08002522 } else if (mState == RESUMED) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002523 // Try to optimize this case: the configuration is changing and we need to restart
2524 // the top, resumed activity. Instead of doing the normal handshaking, just say
2525 // "restart!".
2526 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2527 "Config is relaunching resumed " + this);
2528
2529 if (DEBUG_STATES && !visible) {
2530 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
2531 + " called by " + Debug.getCallers(4));
2532 }
2533
2534 relaunchActivityLocked(true /* andResume */, preserveWindow);
2535 } else {
2536 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2537 "Config is relaunching non-resumed " + this);
2538 relaunchActivityLocked(false /* andResume */, preserveWindow);
2539 }
2540
2541 // All done... tell the caller we weren't able to keep this activity around.
2542 return false;
2543 }
2544
2545 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002546 // NOTE: We only forward the override configuration as the system level configuration
Andrii Kulian21713ac2016-10-12 22:05:05 -07002547 // changes is always sent to all processes when they happen so it can just use whatever
2548 // system level configuration it last got.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002549 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002550 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002551 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002552 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002553 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002554 stopFreezingScreenLocked(false);
2555
2556 return true;
2557 }
2558
Zak Cohen90e7116742017-01-29 12:59:23 -08002559 /**
2560 * When assessing a configuration change, decide if the changes flags and the new configurations
2561 * should cause the Activity to relaunch.
Ruben Brunkf64af332017-03-22 22:03:25 -07002562 *
2563 * @param changes the changes due to the given configuration.
2564 * @param changesConfig the configuration that was used to calculate the given changes via a
2565 * call to getConfigurationChanges.
Zak Cohen90e7116742017-01-29 12:59:23 -08002566 */
Ruben Brunkf64af332017-03-22 22:03:25 -07002567 private boolean shouldRelaunchLocked(int changes, Configuration changesConfig) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002568 int configChanged = info.getRealConfigChanged();
Ruben Brunkf64af332017-03-22 22:03:25 -07002569 boolean onlyVrUiModeChanged = onlyVrUiModeChanged(changes, changesConfig);
Zak Cohen90e7116742017-01-29 12:59:23 -08002570
2571 // Override for apps targeting pre-O sdks
2572 // If a device is in VR mode, and we're transitioning into VR ui mode, add ignore ui mode
2573 // to the config change.
2574 // For O and later, apps will be required to add configChanges="uimode" to their manifest.
2575 if (appInfo.targetSdkVersion < O
2576 && requestedVrComponent != null
Ruben Brunkf64af332017-03-22 22:03:25 -07002577 && onlyVrUiModeChanged) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002578 configChanged |= CONFIG_UI_MODE;
2579 }
2580
2581 return (changes&(~configChanged)) != 0;
2582 }
2583
Ruben Brunkf64af332017-03-22 22:03:25 -07002584 /**
2585 * Returns true if the configuration change is solely due to the UI mode switching into or out
2586 * of UI_MODE_TYPE_VR_HEADSET.
2587 */
2588 private boolean onlyVrUiModeChanged(int changes, Configuration lastReportedConfig) {
2589 final Configuration currentConfig = getConfiguration();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002590 return changes == CONFIG_UI_MODE && (isInVrUiMode(currentConfig)
Ruben Brunkf64af332017-03-22 22:03:25 -07002591 != isInVrUiMode(lastReportedConfig));
2592 }
2593
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002594 private int getConfigurationChanges(Configuration lastReportedConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002595 // Determine what has changed. May be nothing, if this is a config that has come back from
2596 // the app after going idle. In that case we just want to leave the official config object
2597 // now in the activity and do nothing else.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002598 final Configuration currentConfig = getConfiguration();
2599 int changes = lastReportedConfig.diff(currentConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002600 // We don't want to use size changes if they don't cross boundaries that are important to
2601 // the app.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002602 if ((changes & CONFIG_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002603 final boolean crosses = crossesHorizontalSizeThreshold(lastReportedConfig.screenWidthDp,
2604 currentConfig.screenWidthDp)
2605 || crossesVerticalSizeThreshold(lastReportedConfig.screenHeightDp,
2606 currentConfig.screenHeightDp);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002607 if (!crosses) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002608 changes &= ~CONFIG_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002609 }
2610 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002611 if ((changes & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002612 final int oldSmallest = lastReportedConfig.smallestScreenWidthDp;
2613 final int newSmallest = currentConfig.smallestScreenWidthDp;
2614 if (!crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002615 changes &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002616 }
2617 }
Wale Ogunwale822e5122017-07-26 06:02:24 -07002618 // We don't want window configuration to cause relaunches.
2619 if ((changes & CONFIG_WINDOW_CONFIGURATION) != 0) {
2620 changes &= ~CONFIG_WINDOW_CONFIGURATION;
2621 }
Bryce Lee600dadd2017-07-25 10:48:42 -07002622
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002623 return changes;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002624 }
2625
2626 private static boolean isResizeOnlyChange(int change) {
2627 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2628 | CONFIG_SCREEN_LAYOUT)) == 0;
2629 }
2630
Garfield Tan2746ab52018-07-25 12:33:01 -07002631 private static boolean hasResizeChange(int change) {
2632 return (change & (CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2633 | CONFIG_SCREEN_LAYOUT)) != 0;
2634 }
2635
Andrii Kulian21713ac2016-10-12 22:05:05 -07002636 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
Wale Ogunwaled0412b32018-05-08 09:25:50 -07002637 if (service.mSuppressResizeConfigChanges && preserveWindow) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002638 configChangeFlags = 0;
2639 return;
2640 }
2641
2642 List<ResultInfo> pendingResults = null;
2643 List<ReferrerIntent> pendingNewIntents = null;
2644 if (andResume) {
2645 pendingResults = results;
2646 pendingNewIntents = newIntents;
2647 }
2648 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2649 "Relaunching: " + this + " with results=" + pendingResults
2650 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2651 + " preserveWindow=" + preserveWindow);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002652 EventLog.writeEvent(andResume ? AM_RELAUNCH_RESUME_ACTIVITY
2653 : AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
Andrii Kulian21713ac2016-10-12 22:05:05 -07002654 task.taskId, shortComponentName);
2655
2656 startFreezingScreenLocked(app, 0);
2657
Andrii Kulian21713ac2016-10-12 22:05:05 -07002658 try {
2659 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2660 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2661 + " callers=" + Debug.getCallers(6));
2662 forceNewConfig = false;
2663 mStackSupervisor.activityRelaunchingLocked(this);
Andrii Kulianb372da62018-01-18 10:46:24 -08002664 final ClientTransactionItem callbackItem = ActivityRelaunchItem.obtain(pendingResults,
2665 pendingNewIntents, configChangeFlags,
Wale Ogunwalef6733932018-06-27 05:14:34 -07002666 new MergedConfiguration(service.getGlobalConfiguration(),
Andrii Kulianb372da62018-01-18 10:46:24 -08002667 getMergedOverrideConfiguration()),
2668 preserveWindow);
2669 final ActivityLifecycleItem lifecycleItem;
2670 if (andResume) {
lumark588a3e82018-07-20 18:53:54 +08002671 lifecycleItem = ResumeActivityItem.obtain(
2672 getDisplay().getWindowContainerController().isNextTransitionForward());
Andrii Kulianb372da62018-01-18 10:46:24 -08002673 } else {
Bryce Lee1d0d5142018-04-12 10:35:07 -07002674 lifecycleItem = PauseActivityItem.obtain();
Andrii Kulianb372da62018-01-18 10:46:24 -08002675 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002676 final ClientTransaction transaction = ClientTransaction.obtain(app.getThread(), appToken);
Andrii Kulianb372da62018-01-18 10:46:24 -08002677 transaction.addCallback(callbackItem);
2678 transaction.setLifecycleStateRequest(lifecycleItem);
Bryce Leeb0f993f2018-03-02 15:38:01 -08002679 service.getLifecycleManager().scheduleTransaction(transaction);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002680 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
Andrii Kulianb372da62018-01-18 10:46:24 -08002681 // request resume if this activity is currently resumed, which implies we aren't
Andrii Kulian21713ac2016-10-12 22:05:05 -07002682 // sleeping.
2683 } catch (RemoteException e) {
2684 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2685 }
2686
2687 if (andResume) {
2688 if (DEBUG_STATES) {
2689 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2690 }
2691 results = null;
2692 newIntents = null;
Wale Ogunwale008163e2018-07-23 23:11:08 -07002693 service.getAppWarningsLocked().onResumeActivity(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002694 } else {
Wale Ogunwale008163e2018-07-23 23:11:08 -07002695 final ActivityStack stack = getStack();
2696 if (stack != null) {
2697 stack.mHandler.removeMessages(PAUSE_TIMEOUT_MSG, this);
2698 }
Bryce Lee7ace3952018-02-16 14:34:32 -08002699 setState(PAUSED, "relaunchActivityLocked");
Andrii Kulian21713ac2016-10-12 22:05:05 -07002700 }
2701
2702 configChangeFlags = 0;
2703 deferRelaunchUntilPaused = false;
2704 preserveWindowOnDeferredRelaunch = false;
2705 }
2706
Jorim Jaggibae01b12017-04-11 16:29:10 -07002707 private boolean isProcessRunning() {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002708 WindowProcessController proc = app;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002709 if (proc == null) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002710 proc = service.mProcessNames.get(processName, info.applicationInfo.uid);
Jorim Jaggi02886a82016-12-06 09:10:06 -08002711 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002712 return proc != null && proc.hasThread();
Jorim Jaggi02886a82016-12-06 09:10:06 -08002713 }
2714
Jorim Jaggibae01b12017-04-11 16:29:10 -07002715 /**
2716 * @return Whether a task snapshot starting window may be shown.
2717 */
2718 private boolean allowTaskSnapshot() {
2719 if (newIntents == null) {
2720 return true;
2721 }
2722
2723 // Restrict task snapshot starting window to launcher start, or there is no intent at all
2724 // (eg. task being brought to front). If the intent is something else, likely the app is
2725 // going to show some specific page or view, instead of what's left last time.
2726 for (int i = newIntents.size() - 1; i >= 0; i--) {
2727 final Intent intent = newIntents.get(i);
2728 if (intent != null && !ActivityRecord.isMainIntent(intent)) {
2729 return false;
2730 }
2731 }
2732 return true;
2733 }
2734
Bryce Leeb7c9b802017-05-02 14:20:24 -07002735 /**
2736 * Returns {@code true} if the associated activity has the no history flag set on it.
2737 * {@code false} otherwise.
2738 */
2739 boolean isNoHistory() {
2740 return (intent.getFlags() & FLAG_ACTIVITY_NO_HISTORY) != 0
2741 || (info.flags & FLAG_NO_HISTORY) != 0;
2742 }
2743
Craig Mautner21d24a22014-04-23 11:45:37 -07002744 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2745 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2746 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2747 if (launchedFromPackage != null) {
2748 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2749 }
2750 if (resolvedType != null) {
2751 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2752 }
2753 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2754 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002755
Craig Mautner21d24a22014-04-23 11:45:37 -07002756 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002757 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002758 }
2759
2760 out.startTag(null, TAG_INTENT);
2761 intent.saveToXml(out);
2762 out.endTag(null, TAG_INTENT);
2763
2764 if (isPersistable() && persistentState != null) {
2765 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2766 persistentState.saveToXml(out);
2767 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2768 }
2769 }
2770
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002771 static ActivityRecord restoreFromXml(XmlPullParser in,
2772 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002773 Intent intent = null;
2774 PersistableBundle persistentState = null;
2775 int launchedFromUid = 0;
2776 String launchedFromPackage = null;
2777 String resolvedType = null;
2778 boolean componentSpecified = false;
2779 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002780 long createTime = -1;
2781 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002782 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002783
2784 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2785 final String attrName = in.getAttributeName(attrNdx);
2786 final String attrValue = in.getAttributeValue(attrNdx);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002787 if (DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002788 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002789 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002790 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002791 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002792 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002793 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2794 launchedFromPackage = attrValue;
2795 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2796 resolvedType = attrValue;
2797 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002798 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002799 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002800 userId = Integer.parseInt(attrValue);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002801 } else if (attrName.startsWith(ATTR_TASKDESCRIPTION_PREFIX)) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002802 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002803 } else {
2804 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2805 }
2806 }
2807
2808 int event;
Ruben Brunkf53497c2017-03-27 20:26:17 -07002809 while (((event = in.next()) != END_DOCUMENT) &&
2810 (event != END_TAG || in.getDepth() >= outerDepth)) {
2811 if (event == START_TAG) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002812 final String name = in.getName();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002813 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002814 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002815 if (TAG_INTENT.equals(name)) {
2816 intent = Intent.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002817 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002818 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002819 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2820 persistentState = PersistableBundle.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002821 if (DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002822 "ActivityRecord: persistentState=" + persistentState);
2823 } else {
2824 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2825 XmlUtils.skipCurrentTag(in);
2826 }
2827 }
2828 }
2829
2830 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002831 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002832 }
2833
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002834 final ActivityTaskManagerService service = stackSupervisor.mService;
Craig Mautner21d24a22014-04-23 11:45:37 -07002835 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Patrick Baumann78380272018-04-04 10:41:01 -07002836 userId, Binder.getCallingUid());
Craig Mautnere0129b32014-05-25 16:41:09 -07002837 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002838 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2839 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002840 }
Wale Ogunwalec9e57de2018-05-08 14:28:07 -07002841 final ActivityRecord r = new ActivityRecord(service, null /* caller */,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002842 0 /* launchedFromPid */, launchedFromUid, launchedFromPackage, intent, resolvedType,
Wale Ogunwalef6733932018-06-27 05:14:34 -07002843 aInfo, service.getConfiguration(), null /* resultTo */, null /* resultWho */,
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002844 0 /* reqCode */, componentSpecified, false /* rootVoiceInteraction */,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002845 stackSupervisor, null /* options */, null /* sourceRecord */);
Craig Mautner21d24a22014-04-23 11:45:37 -07002846
2847 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002848 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002849 r.createTime = createTime;
2850
2851 return r;
2852 }
2853
Zak Cohen90e7116742017-01-29 12:59:23 -08002854 private static boolean isInVrUiMode(Configuration config) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002855 return (config.uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_VR_HEADSET;
Zak Cohen90e7116742017-01-29 12:59:23 -08002856 }
2857
David Stevens82ea6cb2017-03-03 16:18:50 -08002858 int getUid() {
2859 return info.applicationInfo.uid;
2860 }
2861
chaviw59b98852017-06-13 12:05:44 -07002862 void setShowWhenLocked(boolean showWhenLocked) {
2863 mShowWhenLocked = showWhenLocked;
Kevin Chyn44639482017-10-09 18:34:41 -07002864 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0 /* configChanges */,
2865 false /* preserveWindows */);
chaviw59b98852017-06-13 12:05:44 -07002866 }
2867
2868 /**
chaviw2c500982018-01-04 17:05:05 -08002869 * @return true if the activity windowing mode is not
2870 * {@link android.app.WindowConfiguration#WINDOWING_MODE_PINNED} and activity contains
2871 * windows that have {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set or if the activity
2872 * has set {@link #mShowWhenLocked}.
2873 * Multi-windowing mode will be exited if true is returned.
chaviw59b98852017-06-13 12:05:44 -07002874 */
2875 boolean canShowWhenLocked() {
chaviw2c500982018-01-04 17:05:05 -08002876 return !inPinnedWindowingMode() && (mShowWhenLocked
Wale Ogunwale2cca8622017-12-11 08:40:13 -08002877 || service.mWindowManager.containsShowWhenLockedWindow(appToken));
chaviw59b98852017-06-13 12:05:44 -07002878 }
2879
2880 void setTurnScreenOn(boolean turnScreenOn) {
2881 mTurnScreenOn = turnScreenOn;
2882 }
2883
2884 /**
2885 * Determines whether this ActivityRecord can turn the screen on. It checks whether the flag
2886 * {@link #mTurnScreenOn} is set and checks whether the ActivityRecord should be visible
2887 * depending on Keyguard state
2888 *
2889 * @return true if the screen can be turned on, false otherwise.
2890 */
2891 boolean canTurnScreenOn() {
2892 final ActivityStack stack = getStack();
2893 return mTurnScreenOn && stack != null &&
2894 stack.checkKeyguardVisibility(this, true /* shouldBeVisible */, true /* isTop */);
2895 }
2896
2897 boolean getTurnScreenOnFlag() {
2898 return mTurnScreenOn;
2899 }
2900
2901 boolean isTopRunningActivity() {
2902 return mStackSupervisor.topRunningActivityLocked() == this;
2903 }
2904
Andrii Kulian52d255c2018-07-13 11:32:19 -07002905 /**
2906 * @return {@code true} if this is the resumed activity on its current display, {@code false}
2907 * otherwise.
2908 */
2909 boolean isResumedActivityOnDisplay() {
2910 final ActivityDisplay display = getDisplay();
2911 return display != null && this == display.getResumedActivity();
2912 }
2913
Jorim Jaggif84e2f62018-01-16 14:17:59 +01002914 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
2915 mWindowContainerController.registerRemoteAnimations(definition);
2916 }
2917
Craig Mautnerf81b90872013-02-26 13:02:43 -08002918 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002920 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002921 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002922 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002923 }
2924 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002925 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002926 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002927 sb.append(" u");
2928 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002929 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002930 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002931 stringName = sb.toString();
2932 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 }
Steven Timotius4346f0a2017-09-12 11:07:21 -07002934
2935 void writeIdentifierToProto(ProtoOutputStream proto, long fieldId) {
2936 final long token = proto.start(fieldId);
2937 proto.write(HASH_CODE, System.identityHashCode(this));
2938 proto.write(USER_ID, userId);
2939 proto.write(TITLE, intent.getComponent().flattenToShortString());
2940 proto.end(token);
2941 }
2942
2943 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2944 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002945 super.writeToProto(proto, CONFIGURATION_CONTAINER, false /* trim */);
Steven Timotius4346f0a2017-09-12 11:07:21 -07002946 writeIdentifierToProto(proto, IDENTIFIER);
Bryce Lee7ace3952018-02-16 14:34:32 -08002947 proto.write(STATE, mState.toString());
Steven Timotius4346f0a2017-09-12 11:07:21 -07002948 proto.write(VISIBLE, visible);
2949 proto.write(FRONT_OF_TASK, frontOfTask);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07002950 if (hasProcess()) {
2951 proto.write(PROC_ID, app.getPid());
Steven Timotius4346f0a2017-09-12 11:07:21 -07002952 }
Wale Ogunwale30eab1f2018-05-24 18:25:25 -07002953 proto.write(TRANSLUCENT, !fullscreen);
Steven Timotius4346f0a2017-09-12 11:07:21 -07002954 proto.end(token);
2955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956}