blob: 4433b84af68d4793075b87a920b9d8a670486c4a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Winson Chungbb348802017-01-30 12:01:45 -080019import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
Winson Chung83471632016-12-13 11:02:12 -080020import static android.app.ActivityManager.StackId.ASSISTANT_STACK_ID;
Wale Ogunwaleeb915f22016-10-14 07:18:22 -070021import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080022import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080023import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale22e25262016-02-01 10:32:02 -080024import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Ruben Brunkf53497c2017-03-27 20:26:17 -070025import static android.app.ActivityManager.TaskDescription.ATTR_TASKDESCRIPTION_PREFIX;
26import static android.app.ActivityOptions.ANIM_CLIP_REVEAL;
27import static android.app.ActivityOptions.ANIM_CUSTOM;
28import static android.app.ActivityOptions.ANIM_SCALE_UP;
29import static android.app.ActivityOptions.ANIM_SCENE_TRANSITION;
30import static android.app.ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN;
31import static android.app.ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP;
32import static android.app.ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN;
33import static android.app.ActivityOptions.ANIM_THUMBNAIL_SCALE_UP;
Winson Chung59fda9e2017-01-20 16:14:51 -080034import static android.app.AppOpsManager.MODE_ALLOWED;
Winson Chungf4ac0632017-03-17 12:34:12 -070035import static android.app.AppOpsManager.OP_PICTURE_IN_PICTURE;
Ruben Brunkf53497c2017-03-27 20:26:17 -070036import static android.content.Intent.ACTION_MAIN;
37import static android.content.Intent.CATEGORY_HOME;
38import static android.content.Intent.CATEGORY_LAUNCHER;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080039import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Bryce Leeb7c9b802017-05-02 14:20:24 -070040import static android.content.Intent.FLAG_ACTIVITY_NO_HISTORY;
Andrii Kulian21713ac2016-10-12 22:05:05 -070041import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
42import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
43import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
44import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Zak Cohen90e7116742017-01-29 12:59:23 -080045import static android.content.pm.ActivityInfo.CONFIG_UI_MODE;
Wale Ogunwale822e5122017-07-26 06:02:24 -070046import static android.content.pm.ActivityInfo.CONFIG_WINDOW_CONFIGURATION;
Jorim Jaggi02886a82016-12-06 09:10:06 -080047import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080048import static android.content.pm.ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
49import static android.content.pm.ActivityInfo.FLAG_IMMERSIVE;
50import static android.content.pm.ActivityInfo.FLAG_MULTIPROCESS;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020051import static android.content.pm.ActivityInfo.FLAG_NO_HISTORY;
Chong Zhang87761972016-08-22 13:53:24 -070052import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Jorim Jaggie7d2b852017-08-28 17:55:15 +020053import static android.content.pm.ActivityInfo.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080054import static android.content.pm.ActivityInfo.FLAG_STATE_NOT_NEEDED;
chaviw59b98852017-06-13 12:05:44 -070055import static android.content.pm.ActivityInfo.FLAG_TURN_SCREEN_ON;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080056import static android.content.pm.ActivityInfo.LAUNCH_MULTIPLE;
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -080057import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
58import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080059import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Ruben Brunkf53497c2017-03-27 20:26:17 -070060import static android.content.pm.ActivityInfo.PERSIST_ACROSS_REBOOTS;
61import static android.content.pm.ActivityInfo.PERSIST_ROOT_ONLY;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070062import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggicd13d332016-04-27 15:40:20 -070063import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwale72a73e32016-10-13 12:16:39 -070064import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070065import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -070066import static android.content.pm.ActivityInfo.isFixedOrientationLandscape;
67import static android.content.pm.ActivityInfo.isFixedOrientationPortrait;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -070068import static android.content.res.Configuration.EMPTY;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -070069import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
70import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Ruben Brunkf53497c2017-03-27 20:26:17 -070071import static android.content.res.Configuration.UI_MODE_TYPE_MASK;
Zak Cohen90e7116742017-01-29 12:59:23 -080072import static android.content.res.Configuration.UI_MODE_TYPE_VR_HEADSET;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080073import static android.os.Build.VERSION_CODES.HONEYCOMB;
Zak Cohen90e7116742017-01-29 12:59:23 -080074import static android.os.Build.VERSION_CODES.O;
Bryce Lee0e7b7c92017-08-10 14:59:00 -070075import static android.os.Build.VERSION_CODES.O_MR1;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080076import static android.os.Process.SYSTEM_UID;
Ruben Brunkf53497c2017-03-27 20:26:17 -070077import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Andrii Kulian3a1619d2017-07-07 14:38:09 -070078import static android.view.WindowManagerPolicy.NAV_BAR_LEFT;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080079import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
Andrii Kulian21713ac2016-10-12 22:05:05 -070080import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
Andrii Kulian21713ac2016-10-12 22:05:05 -070081import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080082import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
Andrii Kulian21713ac2016-10-12 22:05:05 -070083import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
84import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
85import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
86import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080087import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
88import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
89import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070090import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080091import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
92import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Ruben Brunkf53497c2017-03-27 20:26:17 -070093import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
94import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
95import static com.android.server.am.ActivityStack.ActivityState.INITIALIZING;
96import static com.android.server.am.ActivityStack.ActivityState.PAUSED;
97import static com.android.server.am.ActivityStack.ActivityState.PAUSING;
98import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
99import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
100import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
101import static com.android.server.am.ActivityStack.LAUNCH_TICK;
102import static com.android.server.am.ActivityStack.LAUNCH_TICK_MSG;
103import static com.android.server.am.ActivityStack.PAUSE_TIMEOUT_MSG;
Wale Ogunwale9b7a8272017-04-10 08:05:41 -0700104import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Ruben Brunkf53497c2017-03-27 20:26:17 -0700105import static com.android.server.am.ActivityStack.STOP_TIMEOUT_MSG;
106import static com.android.server.am.EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME;
107import static com.android.server.am.EventLogTags.AM_ACTIVITY_LAUNCH_TIME;
108import static com.android.server.am.EventLogTags.AM_RELAUNCH_ACTIVITY;
109import static com.android.server.am.EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY;
110import static com.android.server.am.TaskPersister.DEBUG;
111import static com.android.server.am.TaskPersister.IMAGE_EXTENSION;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800112import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
Ruben Brunkf53497c2017-03-27 20:26:17 -0700113import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
114import static org.xmlpull.v1.XmlPullParser.END_TAG;
115import static org.xmlpull.v1.XmlPullParser.START_TAG;
Wale Ogunwale18795a22014-12-03 11:38:33 -0800116
Andrii Kulian21713ac2016-10-12 22:05:05 -0700117import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -0700118import android.app.ActivityManager.TaskDescription;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700119import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800120import android.app.PendingIntent;
Winson Chung709904f2017-04-25 11:00:48 -0700121import android.app.PictureInPictureParams;
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800122import android.app.ResultInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import android.content.ComponentName;
124import android.content.Intent;
125import android.content.pm.ActivityInfo;
126import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700127import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import android.content.res.Configuration;
129import android.graphics.Bitmap;
Winson Chungaa7fa012017-05-24 15:50:06 -0700130import android.graphics.GraphicBuffer;
Dianne Hackbornd367ca82012-05-07 15:49:39 -0700131import android.graphics.Rect;
Bryce Lee39791592017-04-26 09:29:12 -0700132import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700134import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800135import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800137import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700139import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140import android.os.SystemClock;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800141import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700142import android.os.UserHandle;
Bryce Lee8558ec72017-08-17 15:37:26 -0700143import android.os.storage.StorageManager;
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800144import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import android.util.EventLog;
146import android.util.Log;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700147import android.util.MergedConfiguration;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700149import android.util.TimeUtils;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700150import android.view.AppTransitionAnimationSpec;
Jorim Jaggi34795e32017-05-12 17:27:46 +0200151import android.view.IAppTransitionAnimationSpecsFuture;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import android.view.IApplicationToken;
Jorim Jaggife762342016-10-13 14:33:27 +0200153import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700154
Andrii Kulian3a1619d2017-07-07 14:38:09 -0700155import com.android.internal.annotations.VisibleForTesting;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800156import com.android.internal.app.ResolverActivity;
157import com.android.internal.content.ReferrerIntent;
158import com.android.internal.util.XmlUtils;
159import com.android.server.AttributeCache;
Ruben Brunkf53497c2017-03-27 20:26:17 -0700160import com.android.server.AttributeCache.Entry;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800161import com.android.server.am.ActivityStack.ActivityState;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800162import com.android.server.wm.AppWindowContainerController;
163import com.android.server.wm.AppWindowContainerListener;
Wale Ogunwale98d62312017-07-12 09:24:56 -0700164import com.android.server.wm.ConfigurationContainer;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800165import com.android.server.wm.TaskWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
Jorim Jaggi02886a82016-12-06 09:10:06 -0800167import org.xmlpull.v1.XmlPullParser;
168import org.xmlpull.v1.XmlPullParserException;
169import org.xmlpull.v1.XmlSerializer;
170
Suprabh Shukla23593142015-11-03 17:31:15 -0800171import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -0700172import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173import java.io.PrintWriter;
174import java.lang.ref.WeakReference;
175import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800176import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700178import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700179import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
181/**
182 * An entry in the history stack, representing an activity.
183 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700184final class ActivityRecord extends ConfigurationContainer implements AppWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800185 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_AM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700186 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
187 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
188 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700189 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700190 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
191 private static final String TAG_THUMBNAILS = TAG + POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700192 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800193
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700194 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700195 private static final String RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700196
Craig Mautner21d24a22014-04-23 11:45:37 -0700197 private static final String ATTR_ID = "id";
198 private static final String TAG_INTENT = "intent";
199 private static final String ATTR_USERID = "user_id";
200 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
201 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700202 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700203 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
204 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700205 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 final ActivityManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800208 final IApplicationToken.Stub appToken; // window manager token
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800209 AppWindowContainerController mWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 final ActivityInfo info; // all about me
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700211 final ApplicationInfo appInfo; // information about activity's app
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800212 final int launchedFromPid; // always the pid who started the activity.
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700213 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800214 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700215 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 final Intent intent; // the original intent that generated us
217 final ComponentName realActivity; // the intent component, or target of an alias.
218 final String shortComponentName; // the short component name of the intent
219 final String resolvedType; // as per original caller;
220 final String packageName; // the package implementing intent's component
221 final String processName; // process where this component wants to run
222 final String taskAffinity; // as per ActivityInfo.taskAffinity
223 final boolean stateNotNeeded; // As per ActivityInfo.flags
Craig Mautner4addfc52013-06-25 08:05:45 -0700224 boolean fullscreen; // covers the full screen?
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800225 final boolean noDisplay; // activity is not displayed?
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800226 private final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700227 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700228
229 static final int APPLICATION_ACTIVITY_TYPE = 0;
230 static final int HOME_ACTIVITY_TYPE = 1;
231 static final int RECENTS_ACTIVITY_TYPE = 2;
Winson Chung83471632016-12-13 11:02:12 -0800232 static final int ASSISTANT_ACTIVITY_TYPE = 3;
Craig Mautner2c1faed2013-07-23 12:56:02 -0700233 int mActivityType;
Craig Mautner86d67a42013-05-14 10:34:38 -0700234
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800235 private CharSequence nonLocalizedLabel; // the label information from the package mgr.
236 private int labelRes; // the label information from the package mgr.
237 private int icon; // resource identifier of activity's icon.
238 private int logo; // resource identifier of activity's logo.
239 private int theme; // resource identifier of activity's theme.
240 private int realTheme; // actual theme resource we will use, never 0.
241 private int windowFlags; // custom window flags for preview window.
Bryce Leeaf691c02017-03-20 14:20:22 -0700242 private TaskRecord task; // the task this is in.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800243 private long createTime = System.currentTimeMillis();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700244 long displayStartTime; // when we started launching this activity
245 long fullyDrawnStartTime; // when we started launching this activity
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800246 private long startTime; // last time this activity was started
Dianne Hackborn50685602011-12-01 12:23:37 -0800247 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700248 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700249 long pauseTime; // last time we started pausing the activity
250 long launchTickTime; // base time for launch tick messages
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700251 // Last configuration reported to the activity in the client process.
252 private MergedConfiguration mLastReportedConfiguration;
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800253 private int mLastReportedDisplayId;
Winson Chung609e1e92017-05-08 10:52:12 -0700254 private boolean mLastReportedMultiWindowMode;
255 private boolean mLastReportedPictureInPictureMode;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700256 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700257 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final String resultWho; // additional identifier for use by resultTo.
259 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800260 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800262 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700263 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700264 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700265 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -0700267 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700268 ProcessRecord app; // if non-null, hosting application
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700269 ActivityState state; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700271 PersistableBundle persistentState; // last persistently saved activity state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 boolean frontOfTask; // is this the root activity of its task?
273 boolean launchFailed; // set if a launched failed, to abort on 2nd try
274 boolean haveState; // have we gotten the last activity state?
275 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700276 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800278 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
279 // completed
280 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 int configChangeFlags; // which config values have changed
Wale Ogunwaleec950642017-04-25 07:44:21 -0700282 private boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 int launchMode; // the launch mode activity attribute.
284 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700285 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
286 // might hide this activity?
Wale Ogunwaleec950642017-04-25 07:44:21 -0700287 private boolean mDeferHidingClient; // If true we told WM to defer reporting to the client
288 // process that it is hidden.
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800289 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 boolean nowVisible; // is this activity's window visible?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 boolean idle; // has the activity gone idle?
292 boolean hasBeenLaunched;// has this activity ever been launched?
293 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400294 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400295 boolean forceNewConfig; // force re-create with new config next time
Winson Chungf7e03e12017-08-22 11:32:16 -0700296 boolean supportsEnterPipOnTaskSwitch; // This flag is set by the system to indicate that the
297 // activity can enter picture in picture while pausing (only when switching to another task)
Winson Chung709904f2017-04-25 11:00:48 -0700298 PictureInPictureParams pictureInPictureArgs = new PictureInPictureParams.Builder().build();
299 // The PiP params used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800300 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800301 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800302 ComponentName requestedVrComponent; // the requested component for handling VR mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700304 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700305
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700306 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700307 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800308
309 static final int STARTING_WINDOW_NOT_SHOWN = 0;
310 static final int STARTING_WINDOW_SHOWN = 1;
311 static final int STARTING_WINDOW_REMOVED = 2;
312 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700313 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800314
Craig Mautner21d24a22014-04-23 11:45:37 -0700315 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700316 boolean mLaunchTaskBehind; // this activity is actively being launched with
317 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700318
Filip Gruszczynski23493322015-07-29 17:02:59 -0700319 // These configurations are collected from application's resources based on size-sensitive
320 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
321 // and drawable-sw400dp will be added to both as 400.
322 private int[] mVerticalSizeConfigurations;
323 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700324 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700325
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800326 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
327 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
328
Robert Carrd2265122016-08-05 10:25:21 -0700329 // A hint to override the window specified rotation animation, or -1
330 // to use the window specified value. We use this so that
331 // we can select the right animation in the cases of starting
332 // windows, where the app hasn't had time to set a value
333 // on the window.
334 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700335
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700336 // The bounds of this activity. Mainly used for aspect-ratio compatibility.
337 // TODO(b/36505427): Every level on ConfigurationContainer now has bounds information, which
338 // directly affects the configuration. We should probably move this into that class and have it
339 // handle calculating override configuration from the bounds.
340 private final Rect mBounds = new Rect();
341
chaviw59b98852017-06-13 12:05:44 -0700342 private boolean mShowWhenLocked;
343 private boolean mTurnScreenOn;
344
Andrii Kulian21713ac2016-10-12 22:05:05 -0700345 /**
346 * Temp configs used in {@link #ensureActivityConfigurationLocked(int, boolean)}
347 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700348 private final Configuration mTmpConfig = new Configuration();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700349 private final Rect mTmpBounds = new Rect();
Andrii Kulian21713ac2016-10-12 22:05:05 -0700350
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800351 private static String startingWindowStateToString(int state) {
352 switch (state) {
353 case STARTING_WINDOW_NOT_SHOWN:
354 return "STARTING_WINDOW_NOT_SHOWN";
355 case STARTING_WINDOW_SHOWN:
356 return "STARTING_WINDOW_SHOWN";
357 case STARTING_WINDOW_REMOVED:
358 return "STARTING_WINDOW_REMOVED";
359 default:
360 return "unknown state=" + state;
361 }
362 }
363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700365 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700366 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
367 pw.print(" processName="); pw.println(processName);
368 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700369 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800370 pw.print(" userId="); pw.println(userId);
371 pw.print(prefix); pw.print("app="); pw.println(app);
372 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700373 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
374 pw.print(" task="); pw.println(task);
375 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700376 pw.print(prefix); pw.print("realActivity=");
377 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700378 if (appInfo != null) {
379 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
380 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
381 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
382 }
383 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800384 if (appInfo.splitSourceDirs != null) {
385 pw.print(prefix); pw.print("splitDir=");
386 pw.println(Arrays.toString(appInfo.splitSourceDirs));
387 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800388 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700389 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
390 pw.print(" componentSpecified="); pw.print(componentSpecified);
Craig Mautner86d67a42013-05-14 10:34:38 -0700391 pw.print(" mActivityType="); pw.println(mActivityType);
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700392 if (rootVoiceInteraction) {
393 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
394 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800395 pw.print(prefix); pw.print("compat="); pw.print(compat);
396 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
397 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
398 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700399 pw.println(prefix + "mLastReportedConfigurations:");
400 mLastReportedConfiguration.dump(pw, prefix + " ");
401
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700402 pw.print(prefix); pw.print("CurrentConfiguration="); pw.println(getConfiguration());
403 if (!getOverrideConfiguration().equals(EMPTY)) {
404 pw.println(prefix + "OverrideConfiguration=" + getOverrideConfiguration());
405 }
406 if (!mBounds.isEmpty()) {
407 pw.println(prefix + "mBounds=" + mBounds);
408 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700409 if (resultTo != null || resultWho != null) {
410 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
411 pw.print(" resultWho="); pw.print(resultWho);
412 pw.print(" resultCode="); pw.println(requestCode);
413 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700414 if (taskDescription != null) {
415 final String iconFilename = taskDescription.getIconFilename();
416 if (iconFilename != null || taskDescription.getLabel() != null ||
417 taskDescription.getPrimaryColor() != 0) {
418 pw.print(prefix); pw.print("taskDescription:");
419 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
420 pw.print(" label=\""); pw.print(taskDescription.getLabel());
421 pw.print("\"");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200422 pw.print(" primaryColor=");
Craig Mautner29c58ca2014-10-14 16:17:06 -0700423 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700424 pw.print(prefix + " backgroundColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200425 pw.println(Integer.toHexString(taskDescription.getBackgroundColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700426 pw.print(prefix + " statusBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200427 pw.println(Integer.toHexString(taskDescription.getStatusBarColor()));
Wale Ogunwale822e5122017-07-26 06:02:24 -0700428 pw.print(prefix + " navigationBarColor=");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200429 pw.println(Integer.toHexString(taskDescription.getNavigationBarColor()));
Craig Mautner29c58ca2014-10-14 16:17:06 -0700430 }
431 if (iconFilename == null && taskDescription.getIcon() != null) {
432 pw.print(prefix); pw.println("taskDescription contains Bitmap");
433 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700434 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700435 if (results != null) {
436 pw.print(prefix); pw.print("results="); pw.println(results);
437 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700438 if (pendingResults != null && pendingResults.size() > 0) {
439 pw.print(prefix); pw.println("Pending Results:");
440 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
441 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
442 pw.print(prefix); pw.print(" - ");
443 if (pir == null) {
444 pw.println("null");
445 } else {
446 pw.println(pir);
447 pir.dump(pw, prefix + " ");
448 }
449 }
450 }
451 if (newIntents != null && newIntents.size() > 0) {
452 pw.print(prefix); pw.println("Pending New Intents:");
453 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800454 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700455 pw.print(prefix); pw.print(" - ");
456 if (intent == null) {
457 pw.println("null");
458 } else {
459 pw.println(intent.toShortString(false, true, false, true));
460 }
461 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700462 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700463 if (pendingOptions != null) {
464 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
465 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700466 if (appTimeTracker != null) {
467 appTimeTracker.dumpWithHeader(pw, prefix, false);
468 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700469 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700470 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700471 }
472 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800473 pw.print(" launchCount="); pw.print(launchCount);
474 pw.print(" lastLaunchTime=");
475 if (lastLaunchTime == 0) pw.print("0");
476 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
477 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700478 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700479 pw.print(" icicle="); pw.println(icicle);
480 pw.print(prefix); pw.print("state="); pw.print(state);
481 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700482 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700483 pw.print(" finishing="); pw.println(finishing);
484 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
485 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700486 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800487 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800488 pw.print(" idle="); pw.print(idle);
489 pw.print(" mStartingWindowState=");
490 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800491 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
492 pw.print(" noDisplay="); pw.print(noDisplay);
493 pw.print(" immersive="); pw.print(immersive);
494 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800495 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400496 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700497 pw.print(prefix); pw.print("mActivityType=");
498 pw.println(activityTypeToString(mActivityType));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800499 if (requestedVrComponent != null) {
500 pw.print(prefix);
501 pw.print("requestedVrComponent=");
502 pw.println(requestedVrComponent);
503 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700504 if (displayStartTime != 0 || startTime != 0) {
505 pw.print(prefix); pw.print("displayStartTime=");
506 if (displayStartTime == 0) pw.print("0");
507 else TimeUtils.formatDuration(displayStartTime, now, pw);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700508 pw.print(" startTime=");
509 if (startTime == 0) pw.print("0");
510 else TimeUtils.formatDuration(startTime, now, pw);
511 pw.println();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700512 }
Bryce Lee4a194382017-04-04 14:32:48 -0700513 final boolean waitingVisible =
514 mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800515 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700516 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800517 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700518 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700519 if (lastVisibleTime == 0) pw.print("0");
520 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
521 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700522 }
Wale Ogunwaleec950642017-04-25 07:44:21 -0700523 if (mDeferHidingClient) {
524 pw.println(prefix + "mDeferHidingClient=" + mDeferHidingClient);
525 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800526 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
527 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700528 pw.print(" configChangeFlags=");
529 pw.println(Integer.toHexString(configChangeFlags));
530 }
531 if (connections != null) {
532 pw.print(prefix); pw.print("connections="); pw.println(connections);
533 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800534 if (info != null) {
535 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
Winson Chung609e1e92017-05-08 10:52:12 -0700536 pw.println(prefix + "mLastReportedMultiWindowMode=" + mLastReportedMultiWindowMode
537 + " mLastReportedPictureInPictureMode=" + mLastReportedPictureInPictureMode);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700538 if (info.supportsPictureInPicture()) {
539 pw.println(prefix + "supportsPictureInPicture=" + info.supportsPictureInPicture());
Winson Chungf7e03e12017-08-22 11:32:16 -0700540 pw.println(prefix + "supportsEnterPipOnTaskSwitch: "
541 + supportsEnterPipOnTaskSwitch);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700542 }
543 if (info.maxAspectRatio != 0) {
544 pw.println(prefix + "maxAspectRatio=" + info.maxAspectRatio);
545 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
548
Andrii Kulian21713ac2016-10-12 22:05:05 -0700549 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700550 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
551 }
552
Andrii Kulian21713ac2016-10-12 22:05:05 -0700553 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700554 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
555 }
556
Andrii Kulian21713ac2016-10-12 22:05:05 -0700557 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700558 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
559 }
560
Filip Gruszczynski23493322015-07-29 17:02:59 -0700561 /**
562 * The purpose of this method is to decide whether the activity needs to be relaunched upon
563 * changing its size. In most cases the activities don't need to be relaunched, if the resize
564 * is small, all the activity content has to do is relayout itself within new bounds. There are
565 * cases however, where the activity's content would be completely changed in the new size and
566 * the full relaunch is required.
567 *
568 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
569 * required. These thresholds are collected from the application resource qualifiers. For
570 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
571 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
572 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
573 * of the threshold.
574 */
575 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
576 int secondDp) {
577 if (thresholds == null) {
578 return false;
579 }
580 for (int i = thresholds.length - 1; i >= 0; i--) {
581 final int threshold = thresholds[i];
582 if ((firstDp < threshold && secondDp >= threshold)
583 || (firstDp >= threshold && secondDp < threshold)) {
584 return true;
585 }
586 }
587 return false;
588 }
589
Andrii Kulian21713ac2016-10-12 22:05:05 -0700590 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700591 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700592 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
593 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700594 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700595 }
596
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800597 private void scheduleActivityMovedToDisplay(int displayId, Configuration config) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800598 if (app == null || app.thread == null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800599 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.w(TAG,
600 "Can't report activity moved to display - client not running, activityRecord="
601 + this + ", displayId=" + displayId);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800602 return;
603 }
604 try {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800605 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
606 "Reporting activity moved to display" + ", activityRecord=" + this
607 + ", displayId=" + displayId + ", config=" + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700608
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800609 app.thread.scheduleActivityMovedToDisplay(appToken, displayId,
610 new Configuration(config));
611 } catch (RemoteException e) {
612 // If process died, whatever.
613 }
614 }
615
616 private void scheduleConfigurationChanged(Configuration config) {
617 if (app == null || app.thread == null) {
618 if (DEBUG_CONFIGURATION) Slog.w(TAG,
619 "Can't report activity configuration update - client not running"
620 + ", activityRecord=" + this);
621 return;
622 }
623 try {
624 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + ", config: "
625 + config);
626
627 app.thread.scheduleActivityConfigurationChanged(appToken, new Configuration(config));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800628 } catch (RemoteException e) {
629 // If process died, whatever.
630 }
631 }
632
Winson Chung5af42fc2017-03-24 17:11:33 -0700633 void updateMultiWindowMode() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700634 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800635 return;
636 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700637
638 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
639 final boolean inMultiWindowMode = !task.mFullscreen;
Winson Chung609e1e92017-05-08 10:52:12 -0700640 if (inMultiWindowMode != mLastReportedMultiWindowMode) {
641 mLastReportedMultiWindowMode = inMultiWindowMode;
Winson Chung5af42fc2017-03-24 17:11:33 -0700642 scheduleMultiWindowModeChanged(getConfiguration());
643 }
644 }
645
646 private void scheduleMultiWindowModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800647 try {
Winson Chung609e1e92017-05-08 10:52:12 -0700648 app.thread.scheduleMultiWindowModeChanged(appToken, mLastReportedMultiWindowMode,
Winson Chung5af42fc2017-03-24 17:11:33 -0700649 overrideConfig);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800650 } catch (Exception e) {
651 // If process died, I don't care.
652 }
653 }
654
Winson Chung5af42fc2017-03-24 17:11:33 -0700655 void updatePictureInPictureMode(Rect targetStackBounds) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700656 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800657 return;
658 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700659
660 final boolean inPictureInPictureMode = (task.getStackId() == PINNED_STACK_ID) &&
661 (targetStackBounds != null);
Winson Chung609e1e92017-05-08 10:52:12 -0700662 if (inPictureInPictureMode != mLastReportedPictureInPictureMode) {
Winson Chung5af42fc2017-03-24 17:11:33 -0700663 // Picture-in-picture mode changes also trigger a multi-window mode change as well, so
664 // update that here in order
Winson Chung609e1e92017-05-08 10:52:12 -0700665 mLastReportedPictureInPictureMode = inPictureInPictureMode;
666 mLastReportedMultiWindowMode = inPictureInPictureMode;
Winson Chung5af42fc2017-03-24 17:11:33 -0700667 final Configuration newConfig = task.computeNewOverrideConfigurationForBounds(
668 targetStackBounds, null);
669 schedulePictureInPictureModeChanged(newConfig);
670 scheduleMultiWindowModeChanged(newConfig);
671 }
672 }
673
674 private void schedulePictureInPictureModeChanged(Configuration overrideConfig) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800675 try {
Winson Chung609e1e92017-05-08 10:52:12 -0700676 app.thread.schedulePictureInPictureModeChanged(appToken,
677 mLastReportedPictureInPictureMode,
Winson Chung5af42fc2017-03-24 17:11:33 -0700678 overrideConfig);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800679 } catch (Exception e) {
680 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800681 }
682 }
683
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800684 boolean isFreeform() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700685 return task != null && task.getStackId() == FREEFORM_WORKSPACE_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800686 }
687
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700688 @Override
689 protected int getChildCount() {
690 // {@link ActivityRecord} is a leaf node and has no children.
691 return 0;
692 }
693
694 @Override
695 protected ConfigurationContainer getChildAt(int index) {
696 return null;
697 }
698
699 @Override
700 protected ConfigurationContainer getParent() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700701 return getTask();
702 }
703
704 TaskRecord getTask() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700705 return task;
706 }
707
Bryce Leeaf691c02017-03-20 14:20:22 -0700708 /**
709 * Sets reference to the {@link TaskRecord} the {@link ActivityRecord} will treat as its parent.
710 * Note that this does not actually add the {@link ActivityRecord} as a {@link TaskRecord}
711 * children. However, this method will clean up references to this {@link ActivityRecord} in
712 * {@link ActivityStack}.
713 * @param task The new parent {@link TaskRecord}.
714 */
715 void setTask(TaskRecord task) {
716 setTask(task, false /*reparenting*/);
717 }
718
719 /**
720 * This method should only be called by {@link TaskRecord#removeActivity(ActivityRecord)}.
721 */
722 void setTask(TaskRecord task, boolean reparenting) {
723 // Do nothing if the {@link TaskRecord} is the same as the current {@link getTask}.
724 if (task != null && task == getTask()) {
725 return;
726 }
727
728 final ActivityStack stack = getStack();
729
730 // If the new {@link TaskRecord} is from a different {@link ActivityStack}, remove this
731 // {@link ActivityRecord} from its current {@link ActivityStack}.
732 if (!reparenting && stack != null && (task == null || stack != task.getStack())) {
733 stack.onActivityRemovedFromStack(this);
734 }
735
736 this.task = task;
737
738 if (!reparenting) {
739 onParentChanged();
740 }
741 }
742
Dianne Hackbornbe707852011-11-11 14:32:10 -0800743 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700744 private final WeakReference<ActivityRecord> weakActivity;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700745 private final String name;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800746
Steven Timotiusaf03df62017-07-18 16:56:43 -0700747 Token(ActivityRecord activity, Intent intent) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800748 weakActivity = new WeakReference<>(activity);
Steven Timotiusaf03df62017-07-18 16:56:43 -0700749 name = intent.getComponent().flattenToShortString();
Wale Ogunwale7d701172015-03-11 15:36:30 -0700750 }
751
Andrii Kulian21713ac2016-10-12 22:05:05 -0700752 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700753 if (token == null) {
754 return null;
755 }
756 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700757 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700758 return null;
759 }
760 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800761 }
762
Craig Mautnerde4ef022013-04-07 19:01:33 -0700763 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800764 public String toString() {
765 StringBuilder sb = new StringBuilder(128);
766 sb.append("Token{");
767 sb.append(Integer.toHexString(System.identityHashCode(this)));
768 sb.append(' ');
769 sb.append(weakActivity.get());
770 sb.append('}');
771 return sb.toString();
772 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700773
774 @Override
775 public String getName() {
776 return name;
777 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800778 }
779
Wale Ogunwale7d701172015-03-11 15:36:30 -0700780 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800781 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700782 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800783 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800784 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800785 return null;
786 }
787 }
788
Chong Zhang85ee6542015-10-02 13:36:38 -0700789 boolean isResolverActivity() {
790 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700791 }
792
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800793 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller, int _launchedFromPid,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800794 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700796 ActivityRecord _resultTo, String _resultWho, int _reqCode,
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700797 boolean _componentSpecified, boolean _rootVoiceInteraction,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700798 ActivityStackSupervisor supervisor, ActivityOptions options,
799 ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 service = _service;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700801 appToken = new Token(this, _intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 info = aInfo;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800803 launchedFromPid = _launchedFromPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800805 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700806 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 intent = _intent;
808 shortComponentName = _intent.getComponent().flattenToShortString();
809 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800810 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700811 rootVoiceInteraction = _rootVoiceInteraction;
Wale Ogunwalee610d3d2017-04-25 10:23:48 -0700812 mLastReportedConfiguration = new MergedConfiguration(_configuration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 resultTo = _resultTo;
814 resultWho = _resultWho;
815 requestCode = _reqCode;
Ruben Brunkf53497c2017-03-27 20:26:17 -0700816 state = INITIALIZING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 frontOfTask = false;
818 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700820 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800822 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 keysPaused = false;
824 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700825 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 idle = false;
828 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700829 mStackSupervisor = supervisor;
Robert Carr0f5d7532016-10-17 16:39:17 -0700830
831 mRotationAnimationHint = aInfo.rotationAnimation;
832
Craig Mautner233ceee2014-05-09 17:05:11 -0700833 if (options != null) {
Chong Zhang280d3322015-11-03 17:27:26 -0800834 pendingOptions = options;
Craig Mautnerbb742462014-07-07 15:28:55 -0700835 mLaunchTaskBehind = pendingOptions.getLaunchTaskBehind();
Robert Carr0f5d7532016-10-17 16:39:17 -0700836
837 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
838 // Only override manifest supplied option if set.
839 if (rotationAnimation >= 0) {
840 mRotationAnimationHint = rotationAnimation;
841 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700842 PendingIntent usageReport = pendingOptions.getUsageTimeReport();
843 if (usageReport != null) {
844 appTimeTracker = new AppTimeTracker(usageReport);
845 }
Craig Mautner233ceee2014-05-09 17:05:11 -0700846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800848 // This starts out true, since the initial state of an activity is that we have everything,
849 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700850 haveState = true;
851
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800852 // If the class name in the intent doesn't match that of the target, this is
853 // probably an alias. We have to create a new ComponentName object to keep track
854 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
855 if (aInfo.targetActivity == null
856 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
857 && (aInfo.launchMode == LAUNCH_MULTIPLE
858 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
859 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800861 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800863 taskAffinity = aInfo.taskAffinity;
864 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
865 appInfo = aInfo.applicationInfo;
866 nonLocalizedLabel = aInfo.nonLocalizedLabel;
867 labelRes = aInfo.labelRes;
868 if (nonLocalizedLabel == null && labelRes == 0) {
869 ApplicationInfo app = aInfo.applicationInfo;
870 nonLocalizedLabel = app.nonLocalizedLabel;
871 labelRes = app.labelRes;
872 }
873 icon = aInfo.getIconResource();
874 logo = aInfo.getLogoResource();
875 theme = aInfo.getThemeResource();
876 realTheme = theme;
877 if (realTheme == 0) {
878 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
879 ? android.R.style.Theme : android.R.style.Theme_Holo;
880 }
881 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
882 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
883 }
884 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
885 && (aInfo.applicationInfo.uid == SYSTEM_UID
886 || aInfo.applicationInfo.uid == _caller.info.uid)) {
887 processName = _caller.processName;
888 } else {
889 processName = aInfo.processName;
890 }
891
892 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
893 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
894 }
895
896 packageName = aInfo.applicationInfo.packageName;
897 launchMode = aInfo.launchMode;
898
Ruben Brunkf53497c2017-03-27 20:26:17 -0700899 Entry ent = AttributeCache.instance().get(packageName,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800900 realTheme, com.android.internal.R.styleable.Window, userId);
Bryce Lee39791592017-04-26 09:29:12 -0700901 fullscreen = ent != null && !ActivityInfo.isTranslucentOrFloating(ent.array);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800902 noDisplay = ent != null && ent.array.getBoolean(
903 com.android.internal.R.styleable.Window_windowNoDisplay, false);
904
Winson Chung83471632016-12-13 11:02:12 -0800905 setActivityType(_componentSpecified, _launchedFromUid, _intent, options, sourceRecord);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800906
907 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
908
909 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
910 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
chaviw59b98852017-06-13 12:05:44 -0700911
912 mShowWhenLocked = (aInfo.flags & FLAG_SHOW_WHEN_LOCKED) != 0;
913 mTurnScreenOn = (aInfo.flags & FLAG_TURN_SCREEN_ON) != 0;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800914 }
915
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800916 AppWindowContainerController getWindowContainerController() {
917 return mWindowContainerController;
918 }
919
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800920 void createWindowContainer() {
921 if (mWindowContainerController != null) {
922 throw new IllegalArgumentException("Window container=" + mWindowContainerController
923 + " already created for r=" + this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800924 }
925
926 inHistory = true;
927
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800928 final TaskWindowContainerController taskController = task.getWindowContainerController();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800929
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700930 // TODO(b/36505427): Maybe this call should be moved inside updateOverrideConfiguration()
931 task.updateOverrideConfigurationFromLaunchBounds();
932 // Make sure override configuration is up-to-date before using to create window controller.
933 updateOverrideConfiguration();
934
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800935 mWindowContainerController = new AppWindowContainerController(taskController, appToken,
936 this, Integer.MAX_VALUE /* add on top */, info.screenOrientation, fullscreen,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800937 (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
938 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
939 appInfo.targetSdkVersion, mRotationAnimationHint,
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700940 ActivityManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L,
941 getOverrideConfiguration(), mBounds);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800942
943 task.addActivityToTop(this);
944
Winson Chung609e1e92017-05-08 10:52:12 -0700945 // When an activity is started directly into a split-screen fullscreen stack, we need to
946 // update the initial multi-window modes so that the callbacks are scheduled correctly when
947 // the user leaves that mode.
948 mLastReportedMultiWindowMode = !task.mFullscreen;
949 mLastReportedPictureInPictureMode = (task.getStackId() == PINNED_STACK_ID);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800950 }
951
952 void removeWindowContainer() {
Wale Ogunwalecc367f42017-02-01 08:12:14 -0800953 // Resume key dispatching if it is currently paused before we remove the container.
954 resumeKeyDispatchingLocked();
955
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800956 mWindowContainerController.removeContainer(getDisplayId());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800957 mWindowContainerController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 }
959
Winson Chung30480042017-01-26 10:55:34 -0800960 /**
961 * Reparents this activity into {@param newTask} at the provided {@param position}. The caller
962 * should ensure that the {@param newTask} is not already the parent of this activity.
963 */
964 void reparent(TaskRecord newTask, int position, String reason) {
965 final TaskRecord prevTask = task;
966 if (prevTask == newTask) {
967 throw new IllegalArgumentException(reason + ": task=" + newTask
968 + " is already the parent of r=" + this);
969 }
970
Winson Chung74666102017-02-22 17:49:24 -0800971 // TODO: Ensure that we do not directly reparent activities across stacks, as that may leave
972 // the stacks in strange states. For now, we should use Task.reparent() to ensure that
973 // the stack is left in an OK state.
974 if (prevTask != null && newTask != null && prevTask.getStack() != newTask.getStack()) {
975 throw new IllegalArgumentException(reason + ": task=" + newTask
976 + " is in a different stack (" + newTask.getStackId() + ") than the parent of"
977 + " r=" + this + " (" + prevTask.getStackId() + ")");
978 }
979
Winson Chung30480042017-01-26 10:55:34 -0800980 // Must reparent first in window manager
981 mWindowContainerController.reparent(newTask.getWindowContainerController(), position);
982
Bryce Leeaf691c02017-03-20 14:20:22 -0700983 // Remove the activity from the old task and add it to the new task.
984 prevTask.removeActivity(this, true /*reparenting*/);
Bryce Lee0f9bde82017-02-22 16:39:06 -0800985
Winson Chung30480042017-01-26 10:55:34 -0800986 newTask.addActivityAtIndex(position, this);
987 }
988
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700989 private boolean isHomeIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -0700990 return ACTION_MAIN.equals(intent.getAction())
991 && intent.hasCategory(CATEGORY_HOME)
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700992 && intent.getCategories().size() == 1
993 && intent.getData() == null
994 && intent.getType() == null;
995 }
996
Chong Zhangad24f962016-08-25 12:12:33 -0700997 static boolean isMainIntent(Intent intent) {
Ruben Brunkf53497c2017-03-27 20:26:17 -0700998 return ACTION_MAIN.equals(intent.getAction())
999 && intent.hasCategory(CATEGORY_LAUNCHER)
Chong Zhangad24f962016-08-25 12:12:33 -07001000 && intent.getCategories().size() == 1
1001 && intent.getData() == null
1002 && intent.getType() == null;
1003 }
1004
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001005 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
1006 if (uid == Process.myUid() || uid == 0) {
1007 // System process can launch home activity.
1008 return true;
1009 }
1010 // Resolver activity can launch home activity.
1011 return sourceRecord != null && sourceRecord.isResolverActivity();
1012 }
1013
Winson Chung83471632016-12-13 11:02:12 -08001014 /**
1015 * @return whether the given package name can launch an assist activity.
1016 */
1017 private boolean canLaunchAssistActivity(String packageName) {
1018 if (service.mAssistUtils == null) {
1019 return false;
1020 }
1021
1022 final ComponentName assistComponent = service.mAssistUtils.getActiveServiceComponentName();
1023 if (assistComponent != null) {
1024 return assistComponent.getPackageName().equals(packageName);
1025 }
1026 return false;
1027 }
1028
1029 private void setActivityType(boolean componentSpecified, int launchedFromUid, Intent intent,
1030 ActivityOptions options, ActivityRecord sourceRecord) {
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001031 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
1032 && isHomeIntent(intent) && !isResolverActivity()) {
1033 // This sure looks like a home activity!
1034 mActivityType = HOME_ACTIVITY_TYPE;
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001035
1036 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
1037 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
1038 // We only allow home activities to be resizeable if they explicitly requested it.
1039 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
1040 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001041 } else if (realActivity.getClassName().contains(RECENTS_PACKAGE_NAME)) {
1042 mActivityType = RECENTS_ACTIVITY_TYPE;
Winson Chung83471632016-12-13 11:02:12 -08001043 } else if (options != null && options.getLaunchStackId() == ASSISTANT_STACK_ID
1044 && canLaunchAssistActivity(launchedFromPackage)) {
1045 mActivityType = ASSISTANT_ACTIVITY_TYPE;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07001046 } else {
1047 mActivityType = APPLICATION_ACTIVITY_TYPE;
1048 }
1049 }
1050
Craig Mautnera228ae92014-07-09 05:44:55 -07001051 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -08001052 if (launchMode != LAUNCH_SINGLE_INSTANCE && launchMode != LAUNCH_SINGLE_TASK) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001053 task.setTaskToAffiliateWith(taskToAffiliateWith);
1054 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001055 }
1056
Andrii Kulian02b7a832016-10-06 23:11:56 -07001057 /**
1058 * @return Stack value from current task, null if there is no task.
1059 */
Winson Chung55893332017-02-17 17:13:10 -08001060 <T extends ActivityStack> T getStack() {
1061 return task != null ? (T) task.getStack() : null;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001062 }
1063
Jorim Jaggicdfc04e2017-04-28 19:06:24 +02001064 int getStackId() {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001065 return getStack() != null ? getStack().mStackId : INVALID_STACK_ID;
1066 }
1067
Craig Mautner5eda9b32013-07-02 11:58:16 -07001068 boolean changeWindowTranslucency(boolean toOpaque) {
1069 if (fullscreen == toOpaque) {
1070 return false;
1071 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001072
Craig Mautner5eda9b32013-07-02 11:58:16 -07001073 // Keep track of the number of fullscreen activities in this task.
1074 task.numFullscreen += toOpaque ? +1 : -1;
1075
1076 fullscreen = toOpaque;
1077 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -07001078 }
1079
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001080 void takeFromHistory() {
1081 if (inHistory) {
1082 inHistory = false;
1083 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001084 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001085 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001086 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001087 }
1088 }
1089
1090 boolean isInHistory() {
1091 return inHistory;
1092 }
1093
Wale Ogunwale7d701172015-03-11 15:36:30 -07001094 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001095 final ActivityStack stack = getStack();
1096 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001097 }
1098
Craig Mautner86d67a42013-05-14 10:34:38 -07001099 boolean isHomeActivity() {
1100 return mActivityType == HOME_ACTIVITY_TYPE;
1101 }
1102
1103 boolean isRecentsActivity() {
1104 return mActivityType == RECENTS_ACTIVITY_TYPE;
1105 }
1106
Winson Chung83471632016-12-13 11:02:12 -08001107 boolean isAssistantActivity() {
1108 return mActivityType == ASSISTANT_ACTIVITY_TYPE;
1109 }
1110
Craig Mautner21d24a22014-04-23 11:45:37 -07001111 boolean isPersistable() {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001112 return (info.persistableMode == PERSIST_ROOT_ONLY ||
1113 info.persistableMode == PERSIST_ACROSS_REBOOTS) &&
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001114 (intent == null || (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -07001115 }
1116
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001117 boolean isFocusable() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001118 return getWindowConfiguration().canReceiveKeys() || isAlwaysFocusable();
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001119 }
1120
1121 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001122 return ActivityInfo.isResizeableMode(info.resizeMode) || info.supportsPictureInPicture();
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001123 }
1124
Winson Chungd3395382016-12-13 11:49:09 -08001125 /**
1126 * @return whether this activity is non-resizeable or forced to be resizeable
1127 */
1128 boolean isNonResizableOrForcedResizable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001129 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -07001130 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -07001131 }
1132
Winson Chunge6308042016-10-31 09:24:01 -07001133 /**
Winson Chungd3395382016-12-13 11:49:09 -08001134 * @return whether this activity supports PiP multi-window and can be put in the pinned stack.
Winson Chunge6308042016-10-31 09:24:01 -07001135 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001136 boolean supportsPictureInPicture() {
Winson Chungd3395382016-12-13 11:49:09 -08001137 return service.mSupportsPictureInPicture && !isHomeActivity()
1138 && info.supportsPictureInPicture();
1139 }
1140
1141 /**
1142 * @return whether this activity supports split-screen multi-window and can be put in the docked
1143 * stack.
1144 */
1145 boolean supportsSplitScreen() {
1146 // An activity can not be docked even if it is considered resizeable because it only
1147 // supports picture-in-picture mode but has a non-resizeable resizeMode
1148 return service.mSupportsSplitScreenMultiWindow && supportsResizeableMultiWindow();
1149 }
1150
1151 /**
1152 * @return whether this activity supports freeform multi-window and can be put in the freeform
1153 * stack.
1154 */
1155 boolean supportsFreeform() {
1156 return service.mSupportsFreeformWindowManagement && supportsResizeableMultiWindow();
1157 }
1158
1159 /**
1160 * @return whether this activity supports non-PiP multi-window.
1161 */
1162 private boolean supportsResizeableMultiWindow() {
1163 return service.mSupportsMultiWindow && !isHomeActivity()
1164 && (ActivityInfo.isResizeableMode(info.resizeMode)
1165 || service.mForceResizableActivities);
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001166 }
1167
Winson Chunge6308042016-10-31 09:24:01 -07001168 /**
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001169 * Check whether this activity can be launched on the specified display.
1170 * @param displayId Target display id.
1171 * @return {@code true} if either it is the default display or this activity is resizeable and
1172 * can be put a secondary screen.
1173 */
1174 boolean canBeLaunchedOnDisplay(int displayId) {
1175 return service.mStackSupervisor.canPlaceEntityOnDisplay(displayId,
Andrii Kulian02689a72017-07-06 14:28:59 -07001176 supportsResizeableMultiWindow(), launchedFromPid, launchedFromUid, info);
Andrii Kulian036e3ad2017-04-19 10:55:10 -07001177 }
1178
1179 /**
Robert Carrc33658e2017-04-11 18:24:20 -07001180 * @param beforeStopping Whether this check is for an auto-enter-pip operation, that is to say
1181 * the activity has requested to enter PiP when it would otherwise be stopped.
1182 *
Winson Chung298f95b2017-08-10 15:57:18 -07001183 * @return whether this activity is currently allowed to enter PIP.
Winson Chunge6308042016-10-31 09:24:01 -07001184 */
Winson Chung298f95b2017-08-10 15:57:18 -07001185 boolean checkEnterPictureInPictureState(String caller, boolean beforeStopping) {
Wale Ogunwaleb9a0c992017-04-18 07:25:20 -07001186 if (!supportsPictureInPicture()) {
1187 return false;
1188 }
1189
Winson Chungf4ac0632017-03-17 12:34:12 -07001190 // Check app-ops and see if PiP is supported for this package
1191 if (!checkEnterPictureInPictureAppOpsState()) {
1192 return false;
1193 }
1194
Winson Chungf1bfee12017-03-24 17:11:33 -07001195 // Check to see if we are in VR mode, and disallow PiP if so
1196 if (service.shouldDisableNonVrUiLocked()) {
1197 return false;
1198 }
1199
Winson Chungc2baac02017-01-11 13:34:47 -08001200 boolean isKeyguardLocked = service.isKeyguardLocked();
Benjamin Franza83859f2017-07-03 16:34:14 +01001201 boolean isCurrentAppLocked = service.getLockTaskModeState() != LOCK_TASK_MODE_NONE;
Winson Chungc2baac02017-01-11 13:34:47 -08001202 boolean hasPinnedStack = mStackSupervisor.getStack(PINNED_STACK_ID) != null;
Winson Chungbb348802017-01-30 12:01:45 -08001203 // Don't return early if !isNotLocked, since we want to throw an exception if the activity
1204 // is in an incorrect state
Winson Chunge581ebf2017-02-21 08:25:03 -08001205 boolean isNotLockedOrOnKeyguard = !isKeyguardLocked && !isCurrentAppLocked;
Robert Carrc33658e2017-04-11 18:24:20 -07001206
1207 // We don't allow auto-PiP when something else is already pipped.
1208 if (beforeStopping && hasPinnedStack) {
1209 return false;
1210 }
1211
Winson Chungc2baac02017-01-11 13:34:47 -08001212 switch (state) {
1213 case RESUMED:
Winson Chunge581ebf2017-02-21 08:25:03 -08001214 // When visible, allow entering PiP if the app is not locked. If it is over the
1215 // keyguard, then we will prompt to unlock in the caller before entering PiP.
Robert Carrc33658e2017-04-11 18:24:20 -07001216 return !isCurrentAppLocked &&
Winson Chungf7e03e12017-08-22 11:32:16 -07001217 (supportsEnterPipOnTaskSwitch || !beforeStopping);
Winson Chungc2baac02017-01-11 13:34:47 -08001218 case PAUSING:
1219 case PAUSED:
Winson Chungbb348802017-01-30 12:01:45 -08001220 // When pausing, then only allow enter PiP as in the resume state, and in addition,
1221 // require that there is not an existing PiP activity and that the current system
1222 // state supports entering PiP
Winson Chunge581ebf2017-02-21 08:25:03 -08001223 return isNotLockedOrOnKeyguard && !hasPinnedStack
Winson Chungf7e03e12017-08-22 11:32:16 -07001224 && supportsEnterPipOnTaskSwitch;
Winson Chungc2baac02017-01-11 13:34:47 -08001225 case STOPPING:
1226 // When stopping in a valid state, then only allow enter PiP as in the pause state.
1227 // Otherwise, fall through to throw an exception if the caller is trying to enter
1228 // PiP in an invalid stopping state.
Winson Chungf7e03e12017-08-22 11:32:16 -07001229 if (supportsEnterPipOnTaskSwitch) {
Winson Chungf4ac0632017-03-17 12:34:12 -07001230 return isNotLockedOrOnKeyguard && !hasPinnedStack;
Winson Chungc2baac02017-01-11 13:34:47 -08001231 }
1232 default:
Winson Chung298f95b2017-08-10 15:57:18 -07001233 return false;
Winson Chungb5c41b72016-12-07 15:00:47 -08001234 }
Winson Chunge6308042016-10-31 09:24:01 -07001235 }
1236
Winson Chung59fda9e2017-01-20 16:14:51 -08001237 /**
Winson Chungf4ac0632017-03-17 12:34:12 -07001238 * @return Whether AppOps allows this package to enter picture-in-picture.
Winson Chung59fda9e2017-01-20 16:14:51 -08001239 */
Winson Chungf4ac0632017-03-17 12:34:12 -07001240 private boolean checkEnterPictureInPictureAppOpsState() {
Winson Chung59fda9e2017-01-20 16:14:51 -08001241 try {
Winson Chungf4ac0632017-03-17 12:34:12 -07001242 return service.getAppOpsService().checkOperation(OP_PICTURE_IN_PICTURE,
Winson Chung59fda9e2017-01-20 16:14:51 -08001243 appInfo.uid, packageName) == MODE_ALLOWED;
1244 } catch (RemoteException e) {
1245 // Local call
1246 }
1247 return false;
1248 }
1249
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001250 boolean isAlwaysFocusable() {
1251 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001252 }
1253
Jorim Jaggife762342016-10-13 14:33:27 +02001254
1255 /**
1256 * @return true if the activity contains windows that have
1257 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
1258 */
1259 boolean hasDismissKeyguardWindows() {
1260 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
1261 }
1262
Wale Ogunwale7d701172015-03-11 15:36:30 -07001263 void makeFinishingLocked() {
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001264 if (finishing) {
1265 return;
1266 }
1267 finishing = true;
1268 if (stopped) {
1269 clearOptionsLocked();
1270 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001271
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001272 if (service != null) {
1273 service.mTaskChangeNotificationController.notifyTaskStackChanged();
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001274 }
1275 }
1276
Dianne Hackborn7e269642010-08-25 19:50:20 -07001277 UriPermissionOwner getUriPermissionsLocked() {
1278 if (uriPermissions == null) {
1279 uriPermissions = new UriPermissionOwner(service, this);
1280 }
1281 return uriPermissions;
1282 }
1283
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001284 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 int requestCode, int resultCode,
1286 Intent resultData) {
1287 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -05001288 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001290 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292 results.add(r);
1293 }
1294
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001295 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 int requestCode) {
1297 if (results != null) {
1298 for (int i=results.size()-1; i>=0; i--) {
1299 ActivityResult r = (ActivityResult)results.get(i);
1300 if (r.mFrom != from) continue;
1301 if (r.mResultWho == null) {
1302 if (resultWho != null) continue;
1303 } else {
1304 if (!r.mResultWho.equals(resultWho)) continue;
1305 }
1306 if (r.mRequestCode != requestCode) continue;
1307
1308 results.remove(i);
1309 }
1310 }
1311 }
1312
Andrii Kulian21713ac2016-10-12 22:05:05 -07001313 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001315 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
1317 newIntents.add(intent);
1318 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001319
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001320 /**
1321 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1322 * method will be called at the proper time.
1323 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001324 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001325 // The activity now gets access to the data associated with this Intent.
1326 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001327 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001328 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001329 boolean unsent = true;
David Stevens9440dc82017-03-16 19:00:20 -07001330 final ActivityStack stack = getStack();
1331 final boolean isTopActivityWhileSleeping = isTopRunningActivity()
1332 && (stack != null ? stack.shouldSleepActivities() : service.isSleepingLocked());
Wale Ogunwale826c7062016-09-13 08:25:54 -07001333
1334 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001335 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1336 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001337 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Ruben Brunkf53497c2017-03-27 20:26:17 -07001338 if ((state == RESUMED || state == PAUSED
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001339 || isTopActivityWhileSleeping) && app != null && app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001340 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001341 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1342 ar.add(rintent);
Wale Ogunwale826c7062016-09-13 08:25:54 -07001343 app.thread.scheduleNewIntent(
Ruben Brunkf53497c2017-03-27 20:26:17 -07001344 ar, appToken, state == PAUSED /* andPause */);
Craig Mautner86d67a42013-05-14 10:34:38 -07001345 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001346 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001347 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001348 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001349 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001350 }
1351 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001352 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001353 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001354 }
1355 }
1356
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001357 void updateOptionsLocked(ActivityOptions options) {
1358 if (options != null) {
1359 if (pendingOptions != null) {
1360 pendingOptions.abort();
1361 }
1362 pendingOptions = options;
1363 }
1364 }
1365
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001366 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001367 if (pendingOptions != null
Ruben Brunkf53497c2017-03-27 20:26:17 -07001368 && pendingOptions.getAnimationType() != ANIM_SCENE_TRANSITION) {
Michael Jurka21385cd2012-05-03 10:57:31 -07001369 final int animationType = pendingOptions.getAnimationType();
1370 switch (animationType) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001371 case ANIM_CUSTOM:
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001372 service.mWindowManager.overridePendingAppTransition(
1373 pendingOptions.getPackageName(),
1374 pendingOptions.getCustomEnterResId(),
Dianne Hackborn84375872012-06-01 19:03:50 -07001375 pendingOptions.getCustomExitResId(),
1376 pendingOptions.getOnAnimationStartListener());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001377 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001378 case ANIM_CLIP_REVEAL:
Chet Haase10e23ab2015-02-11 15:08:38 -08001379 service.mWindowManager.overridePendingAppTransitionClipReveal(
1380 pendingOptions.getStartX(), pendingOptions.getStartY(),
1381 pendingOptions.getWidth(), pendingOptions.getHeight());
1382 if (intent.getSourceBounds() == null) {
1383 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1384 pendingOptions.getStartY(),
1385 pendingOptions.getStartX()+pendingOptions.getWidth(),
1386 pendingOptions.getStartY()+pendingOptions.getHeight()));
1387 }
1388 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001389 case ANIM_SCALE_UP:
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001390 service.mWindowManager.overridePendingAppTransitionScaleUp(
1391 pendingOptions.getStartX(), pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001392 pendingOptions.getWidth(), pendingOptions.getHeight());
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001393 if (intent.getSourceBounds() == null) {
1394 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1395 pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001396 pendingOptions.getStartX()+pendingOptions.getWidth(),
1397 pendingOptions.getStartY()+pendingOptions.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001398 }
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001399 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001400 case ANIM_THUMBNAIL_SCALE_UP:
1401 case ANIM_THUMBNAIL_SCALE_DOWN:
Winson Chungaa7fa012017-05-24 15:50:06 -07001402 final boolean scaleUp = (animationType == ANIM_THUMBNAIL_SCALE_UP);
1403 final GraphicBuffer buffer = pendingOptions.getThumbnail();
1404 service.mWindowManager.overridePendingAppTransitionThumb(buffer,
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001405 pendingOptions.getStartX(), pendingOptions.getStartY(),
Michael Jurka21385cd2012-05-03 10:57:31 -07001406 pendingOptions.getOnAnimationStartListener(),
Michael Jurka832cb222012-04-13 09:32:47 -07001407 scaleUp);
Winson Chungf229ae52017-06-02 14:34:52 -07001408 if (intent.getSourceBounds() == null && buffer != null) {
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001409 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1410 pendingOptions.getStartY(),
Winson Chungaa7fa012017-05-24 15:50:06 -07001411 pendingOptions.getStartX() + buffer.getWidth(),
1412 pendingOptions.getStartY() + buffer.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001413 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001414 break;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001415 case ANIM_THUMBNAIL_ASPECT_SCALE_UP:
1416 case ANIM_THUMBNAIL_ASPECT_SCALE_DOWN:
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001417 final AppTransitionAnimationSpec[] specs = pendingOptions.getAnimSpecs();
Jorim Jaggi34795e32017-05-12 17:27:46 +02001418 final IAppTransitionAnimationSpecsFuture specsFuture =
1419 pendingOptions.getSpecsFuture();
1420 if (specsFuture != null) {
1421 service.mWindowManager.overridePendingAppTransitionMultiThumbFuture(
1422 specsFuture, pendingOptions.getOnAnimationStartListener(),
1423 animationType == ANIM_THUMBNAIL_ASPECT_SCALE_UP);
1424 } else if (animationType == ANIM_THUMBNAIL_ASPECT_SCALE_DOWN
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001425 && specs != null) {
1426 service.mWindowManager.overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -07001427 specs, pendingOptions.getOnAnimationStartListener(),
1428 pendingOptions.getAnimationFinishedListener(), false);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001429 } else {
1430 service.mWindowManager.overridePendingAppTransitionAspectScaledThumb(
1431 pendingOptions.getThumbnail(),
1432 pendingOptions.getStartX(), pendingOptions.getStartY(),
1433 pendingOptions.getWidth(), pendingOptions.getHeight(),
1434 pendingOptions.getOnAnimationStartListener(),
Ruben Brunkf53497c2017-03-27 20:26:17 -07001435 (animationType == ANIM_THUMBNAIL_ASPECT_SCALE_UP));
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001436 if (intent.getSourceBounds() == null) {
1437 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1438 pendingOptions.getStartY(),
1439 pendingOptions.getStartX() + pendingOptions.getWidth(),
1440 pendingOptions.getStartY() + pendingOptions.getHeight()));
1441 }
Winson Chunga4ccb862014-08-22 15:26:27 -07001442 }
1443 break;
Craig Mautner233ceee2014-05-09 17:05:11 -07001444 default:
1445 Slog.e(TAG, "applyOptionsLocked: Unknown animationType=" + animationType);
1446 break;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001447 }
1448 pendingOptions = null;
1449 }
1450 }
1451
Adam Powellcfbe9be2013-11-06 14:58:58 -08001452 ActivityOptions getOptionsForTargetActivityLocked() {
1453 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1454 }
1455
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001456 void clearOptionsLocked() {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001457 if (pendingOptions != null) {
1458 pendingOptions.abort();
1459 pendingOptions = null;
1460 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001461 }
1462
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001463 ActivityOptions takeOptionsLocked() {
1464 ActivityOptions opts = pendingOptions;
1465 pendingOptions = null;
1466 return opts;
1467 }
1468
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001470 if (uriPermissions != null) {
1471 uriPermissions.removeUriPermissionsLocked();
1472 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001473 }
1474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475
1476 void pauseKeyDispatchingLocked() {
1477 if (!keysPaused) {
1478 keysPaused = true;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001479 mWindowContainerController.pauseKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481 }
1482
1483 void resumeKeyDispatchingLocked() {
1484 if (keysPaused) {
1485 keysPaused = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001486 mWindowContainerController.resumeKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 }
1488 }
1489
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001490 private void updateTaskDescription(CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001491 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001492 }
1493
Wale Ogunwaleec950642017-04-25 07:44:21 -07001494 void setDeferHidingClient(boolean deferHidingClient) {
1495 if (mDeferHidingClient == deferHidingClient) {
1496 return;
1497 }
1498 mDeferHidingClient = deferHidingClient;
1499 if (!mDeferHidingClient && !visible) {
1500 // Hiding the client is no longer deferred and the app isn't visible still, go ahead and
1501 // update the visibility.
1502 setVisibility(false);
1503 }
Wale Ogunwale89973222017-04-23 18:39:45 -07001504 }
1505
Wale Ogunwaleec950642017-04-25 07:44:21 -07001506 void setVisibility(boolean visible) {
Tim Murray68ed8442017-08-29 23:21:27 +00001507 mWindowContainerController.setVisibility(visible, mDeferHidingClient);
Jorim Jaggicdfc04e2017-04-28 19:06:24 +02001508 mStackSupervisor.mActivityMetricsLogger.notifyVisibilityChanged(this, visible);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001509 }
1510
1511 // TODO: Look into merging with #setVisibility()
Wale Ogunwaleec950642017-04-25 07:44:21 -07001512 void setVisible(boolean newVisible) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001513 visible = newVisible;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001514 mDeferHidingClient = !visible && mDeferHidingClient;
Wale Ogunwaleec950642017-04-25 07:44:21 -07001515 setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001516 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1517 }
1518
Jorim Jaggibae01b12017-04-11 16:29:10 -07001519 void notifyAppResumed(boolean wasStopped) {
1520 mWindowContainerController.notifyAppResumed(wasStopped);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001521 }
1522
1523 void notifyUnknownVisibilityLaunched() {
1524 mWindowContainerController.notifyUnknownVisibilityLaunched();
1525 }
1526
Jorim Jaggi241ae102016-11-02 21:57:33 -07001527 /**
1528 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1529 * might have on the visibility
1530 *
1531 * @see {@link ActivityStack#checkKeyguardVisibility}
1532 */
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001533 boolean shouldBeVisibleIgnoringKeyguard(boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001534 if (!okToShowLocked()) {
1535 return false;
1536 }
1537
Wale Ogunwalec981ad52017-06-13 11:40:06 -07001538 boolean isVisible = !behindFullscreenActivity || mLaunchTaskBehind;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001539
1540 if (service.mSupportsLeanbackOnly && isVisible && isRecentsActivity()) {
1541 // On devices that support leanback only (Android TV), Recents activity can only be
1542 // visible if the home stack is the focused stack or we are in split-screen mode.
1543 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1544 || mStackSupervisor.isFocusedStack(getStack());
1545 }
1546
1547 return isVisible;
1548 }
1549
1550 void makeVisibleIfNeeded(ActivityRecord starting) {
1551 // This activity is not currently visible, but is running. Tell it to become visible.
Ruben Brunkf53497c2017-03-27 20:26:17 -07001552 if (state == RESUMED || this == starting) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001553 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1554 "Not making visible, r=" + this + " state=" + state + " starting=" + starting);
1555 return;
1556 }
1557
1558 // If this activity is paused, tell it to now show its window.
1559 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1560 "Making visible and scheduling visibility: " + this);
1561 final ActivityStack stack = getStack();
1562 try {
1563 if (stack.mTranslucentActivityWaiting != null) {
1564 updateOptionsLocked(returningOptions);
1565 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1566 }
1567 setVisible(true);
1568 sleeping = false;
1569 app.pendingUiClean = true;
1570 app.thread.scheduleWindowVisibility(appToken, true /* showWindow */);
1571 // The activity may be waiting for stop, but that is no longer appropriate for it.
1572 mStackSupervisor.mStoppingActivities.remove(this);
1573 mStackSupervisor.mGoingToSleepActivities.remove(this);
1574 } catch (Exception e) {
1575 // Just skip on any failure; we'll make it visible when it next restarts.
1576 Slog.w(TAG, "Exception thrown making visibile: " + intent.getComponent(), e);
1577 }
1578 handleAlreadyVisible();
1579 }
1580
1581 boolean handleAlreadyVisible() {
1582 stopFreezingScreenLocked(false);
1583 try {
1584 if (returningOptions != null) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001585 app.thread.scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001586 }
1587 } catch(RemoteException e) {
1588 }
Ruben Brunkf53497c2017-03-27 20:26:17 -07001589 return state == RESUMED;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001590 }
1591
1592 static void activityResumedLocked(IBinder token) {
1593 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1594 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1595 if (r != null) {
1596 r.icicle = null;
1597 r.haveState = false;
1598 }
1599 }
1600
1601 /**
1602 * Once we know that we have asked an application to put an activity in the resumed state
1603 * (either by launching it or explicitly telling it), this function updates the rest of our
1604 * state to match that fact.
1605 */
1606 void completeResumeLocked() {
1607 final boolean wasVisible = visible;
Jorim Jaggi8d062052017-08-22 14:55:17 +02001608 setVisible(true);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001609 if (!wasVisible) {
1610 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1611 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1612 }
1613 idle = false;
1614 results = null;
1615 newIntents = null;
1616 stopped = false;
1617
1618 if (isHomeActivity()) {
1619 ProcessRecord app = task.mActivities.get(0).app;
1620 if (app != null && app != service.mHomeProcess) {
1621 service.mHomeProcess = app;
1622 }
1623 }
1624
1625 if (nowVisible) {
1626 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1627 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001628 }
1629
1630 // Schedule an idle timeout in case the app doesn't do it for us.
1631 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1632
1633 mStackSupervisor.reportResumedActivityLocked(this);
1634
1635 resumeKeyDispatchingLocked();
1636 final ActivityStack stack = getStack();
1637 stack.mNoAnimActivities.clear();
1638
1639 // Mark the point when the activity is resuming
1640 // TODO: To be more accurate, the mark should be before the onCreate,
1641 // not after the onResume. But for subsequent starts, onResume is fine.
1642 if (app != null) {
1643 cpuTimeAtResume = service.mProcessCpuTracker.getCpuTimeForPid(app.pid);
1644 } else {
1645 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1646 }
1647
1648 returningOptions = null;
chaviw59b98852017-06-13 12:05:44 -07001649
1650 if (canTurnScreenOn()) {
1651 mStackSupervisor.wakeUp("turnScreenOnFlag");
1652 } else {
1653 // If the screen is going to turn on because the caller explicitly requested it and
1654 // the keyguard is not showing don't attempt to sleep. Otherwise the Activity will
1655 // pause and then resume again later, which will result in a double life-cycle event.
David Stevens9440dc82017-03-16 19:00:20 -07001656 stack.checkReadyForSleep();
chaviw59b98852017-06-13 12:05:44 -07001657 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07001658 }
1659
1660 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1661 CharSequence description) {
1662 final ActivityStack stack = getStack();
Ruben Brunkf53497c2017-03-27 20:26:17 -07001663 if (state != STOPPING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001664 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
Ruben Brunkf53497c2017-03-27 20:26:17 -07001665 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001666 return;
1667 }
1668 if (newPersistentState != null) {
1669 persistentState = newPersistentState;
1670 service.notifyTaskPersisterLocked(task, false);
1671 }
1672 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001673
Andrii Kulian21713ac2016-10-12 22:05:05 -07001674 if (newIcicle != null) {
1675 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1676 // the state.
1677 icicle = newIcicle;
1678 haveState = true;
1679 launchCount = 0;
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001680 updateTaskDescription(description);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001681 }
1682 if (!stopped) {
1683 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
Ruben Brunkf53497c2017-03-27 20:26:17 -07001684 stack.mHandler.removeMessages(STOP_TIMEOUT_MSG, this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001685 stopped = true;
Ruben Brunkf53497c2017-03-27 20:26:17 -07001686 state = STOPPED;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001687
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001688 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001689
Andrii Kulian21713ac2016-10-12 22:05:05 -07001690 if (finishing) {
1691 clearOptionsLocked();
1692 } else {
1693 if (deferRelaunchUntilPaused) {
1694 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
1695 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1696 } else {
1697 mStackSupervisor.updatePreviousProcessLocked(this);
1698 }
1699 }
1700 }
1701 }
1702
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001703 void startLaunchTickingLocked() {
Jeff Sharkey5ab02432017-06-27 11:01:36 -06001704 if (Build.IS_USER) {
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001705 return;
1706 }
1707 if (launchTickTime == 0) {
1708 launchTickTime = SystemClock.uptimeMillis();
1709 continueLaunchTickingLocked();
1710 }
1711 }
1712
1713 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001714 if (launchTickTime == 0) {
1715 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001716 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001717
Andrii Kulian02b7a832016-10-06 23:11:56 -07001718 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001719 if (stack == null) {
1720 return false;
1721 }
1722
Ruben Brunkf53497c2017-03-27 20:26:17 -07001723 Message msg = stack.mHandler.obtainMessage(LAUNCH_TICK_MSG, this);
1724 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
1725 stack.mHandler.sendMessageDelayed(msg, LAUNCH_TICK);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001726 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001727 }
1728
1729 void finishLaunchTickingLocked() {
1730 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001731 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001732 if (stack != null) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001733 stack.mHandler.removeMessages(LAUNCH_TICK_MSG);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001734 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001735 }
1736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 // IApplicationToken
1738
1739 public boolean mayFreezeScreenLocked(ProcessRecord app) {
1740 // Only freeze the screen if this activity is currently attached to
1741 // an application, and that application is not blocked or unresponding.
1742 // In any other case, we can't count on getting the screen unfrozen,
1743 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -08001744 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
1748 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001749 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 public void stopFreezingScreenLocked(boolean force) {
1754 if (force || frozenBeforeDestroy) {
1755 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001756 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 }
1758 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001759
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001760 public void reportFullyDrawnLocked(boolean restoredFromBundle) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001761 final long curTime = SystemClock.uptimeMillis();
1762 if (displayStartTime != 0) {
1763 reportLaunchTimeLocked(curTime);
1764 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001765 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001766 if (fullyDrawnStartTime != 0 && stack != null) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001767 final long thisTime = curTime - fullyDrawnStartTime;
1768 final long totalTime = stack.mFullyDrawnStartTime != 0
1769 ? (curTime - stack.mFullyDrawnStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001770 if (SHOW_ACTIVITY_START_TIME) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001771 Trace.asyncTraceEnd(TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
1772 EventLog.writeEvent(AM_ACTIVITY_FULLY_DRAWN_TIME,
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001773 userId, System.identityHashCode(this), shortComponentName,
1774 thisTime, totalTime);
1775 StringBuilder sb = service.mStringBuilder;
1776 sb.setLength(0);
1777 sb.append("Fully drawn ");
1778 sb.append(shortComponentName);
1779 sb.append(": ");
1780 TimeUtils.formatDuration(thisTime, sb);
1781 if (thisTime != totalTime) {
1782 sb.append(" (total ");
1783 TimeUtils.formatDuration(totalTime, sb);
1784 sb.append(")");
1785 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001786 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001787 }
1788 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001789 //service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001790 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001791 stack.mFullyDrawnStartTime = 0;
1792 }
Jorim Jaggi4d27b842017-08-17 17:22:26 +02001793 mStackSupervisor.mActivityMetricsLogger.logAppTransitionReportedDrawn(this,
1794 restoredFromBundle);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001795 fullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001796 }
1797
1798 private void reportLaunchTimeLocked(final long curTime) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001799 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001800 if (stack == null) {
1801 return;
1802 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001803 final long thisTime = curTime - displayStartTime;
1804 final long totalTime = stack.mLaunchStartTime != 0
1805 ? (curTime - stack.mLaunchStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001806 if (SHOW_ACTIVITY_START_TIME) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001807 Trace.asyncTraceEnd(TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
1808 EventLog.writeEvent(AM_ACTIVITY_LAUNCH_TIME,
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001809 userId, System.identityHashCode(this), shortComponentName,
1810 thisTime, totalTime);
1811 StringBuilder sb = service.mStringBuilder;
1812 sb.setLength(0);
1813 sb.append("Displayed ");
1814 sb.append(shortComponentName);
1815 sb.append(": ");
1816 TimeUtils.formatDuration(thisTime, sb);
1817 if (thisTime != totalTime) {
1818 sb.append(" (total ");
1819 TimeUtils.formatDuration(totalTime, sb);
1820 sb.append(")");
1821 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001822 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001823 }
1824 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
1825 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001826 //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001827 }
1828 displayStartTime = 0;
1829 stack.mLaunchStartTime = 0;
1830 }
1831
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001832 @Override
Sudheer Shankac766db02017-06-12 10:37:29 -07001833 public void onStartingWindowDrawn(long timestamp) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001834 synchronized (service) {
Sudheer Shankac766db02017-06-12 10:37:29 -07001835 mStackSupervisor.mActivityMetricsLogger.notifyStartingWindowDrawn(
1836 getStackId(), timestamp);
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001837 }
1838 }
1839
1840 @Override
Sudheer Shankac766db02017-06-12 10:37:29 -07001841 public void onWindowsDrawn(long timestamp) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001842 synchronized (service) {
Sudheer Shankac766db02017-06-12 10:37:29 -07001843 mStackSupervisor.mActivityMetricsLogger.notifyWindowsDrawn(getStackId(), timestamp);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001844 if (displayStartTime != 0) {
Sudheer Shankac766db02017-06-12 10:37:29 -07001845 reportLaunchTimeLocked(timestamp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001847 mStackSupervisor.sendWaitingVisibleReportLocked(this);
1848 startTime = 0;
1849 finishLaunchTickingLocked();
1850 if (task != null) {
1851 task.hasBeenVisible = true;
1852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854 }
1855
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001856 @Override
1857 public void onWindowsVisible() {
1858 synchronized (service) {
1859 mStackSupervisor.reportActivityVisibleLocked(this);
1860 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
1861 if (!nowVisible) {
1862 nowVisible = true;
1863 lastVisibleTime = SystemClock.uptimeMillis();
Bryce Leeb7c9b802017-05-02 14:20:24 -07001864 if (idle || mStackSupervisor.isStoppingNoHistoryActivity()) {
1865 // If this activity was already idle or there is an activity that must be
1866 // stopped immediately after visible, then we now need to make sure we perform
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001867 // the full stop of any activities that are waiting to do so. This is because
1868 // we won't do that while they are still waiting for this one to become visible.
Bryce Lee4a194382017-04-04 14:32:48 -07001869 final int size = mStackSupervisor.mActivitiesWaitingForVisibleActivity.size();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001870 if (size > 0) {
1871 for (int i = 0; i < size; i++) {
Bryce Lee4a194382017-04-04 14:32:48 -07001872 final ActivityRecord r =
1873 mStackSupervisor.mActivitiesWaitingForVisibleActivity.get(i);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001874 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
1875 }
Bryce Lee4a194382017-04-04 14:32:48 -07001876 mStackSupervisor.mActivitiesWaitingForVisibleActivity.clear();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001877 mStackSupervisor.scheduleIdleLocked();
1878 }
Bryce Leeb7c9b802017-05-02 14:20:24 -07001879 } else {
1880 // Instead of doing the full stop routine here, let's just hide any activities
1881 // we now can, and let them stop when the normal idle happens.
1882 mStackSupervisor.processStoppingActivitiesLocked(null /* idleActivity */,
1883 false /* remove */, true /* processPausingActivities */);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001884 }
1885 service.scheduleAppGcsLocked();
1886 }
1887 }
1888 }
1889
1890 @Override
1891 public void onWindowsGone() {
1892 synchronized (service) {
1893 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
1894 nowVisible = false;
1895 }
1896 }
1897
1898 @Override
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07001899 public boolean keyDispatchingTimedOut(String reason, int windowPid) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001900 ActivityRecord anrActivity;
1901 ProcessRecord anrApp;
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07001902 boolean windowFromSameProcessAsActivity;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001903 synchronized (service) {
1904 anrActivity = getWaitingHistoryRecordLocked();
1905 anrApp = app;
Brian Carlstrom7b0f2e82017-03-31 00:24:18 -07001906 windowFromSameProcessAsActivity =
1907 app == null || app.pid == windowPid || windowPid == -1;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001908 }
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07001909 if (windowFromSameProcessAsActivity) {
1910 return service.inputDispatchingTimedOut(anrApp, anrActivity, this, false, reason);
1911 } else {
1912 // In this case another process added windows using this activity token. So, we call the
1913 // generic service input dispatch timed out method so that the right process is blamed.
1914 return service.inputDispatchingTimedOut(windowPid, false /* aboveSystem */, reason) < 0;
1915 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001916 }
1917
1918 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001919 // First find the real culprit... if this activity is waiting for
1920 // another activity to start or has stopped, then the key dispatching
1921 // timeout should not be caused by this.
Bryce Lee4a194382017-04-04 14:32:48 -07001922 if (mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(this) || stopped) {
Craig Mautner8f2adcb2014-04-07 22:21:33 +00001923 final ActivityStack stack = mStackSupervisor.getFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001924 // Try to use the one which is closest to top.
1925 ActivityRecord r = stack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001927 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001929 if (r != null) {
1930 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 }
1932 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001933 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
1935
Chong Zhang87761972016-08-22 13:53:24 -07001936 /** Checks whether the activity should be shown for current user. */
1937 public boolean okToShowLocked() {
Bryce Lee8558ec72017-08-17 15:37:26 -07001938 // We cannot show activities when the device is locked and the application is not
1939 // encryption aware.
1940 if (!StorageManager.isUserKeyUnlocked(userId)
1941 && !info.applicationInfo.isEncryptionAware()) {
1942 return false;
1943 }
1944
Chong Zhang87761972016-08-22 13:53:24 -07001945 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
1946 || (mStackSupervisor.isCurrentProfileLocked(userId)
Fyodor Kupolovce43b1b2017-08-09 10:32:02 -07001947 && service.mUserController.isUserRunningLocked(userId, 0 /* flags */));
Chong Zhang87761972016-08-22 13:53:24 -07001948 }
1949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 /**
1951 * This method will return true if the activity is either visible, is becoming visible, is
1952 * currently pausing, or is resumed.
1953 */
1954 public boolean isInterestingToUserLocked() {
Ruben Brunkf53497c2017-03-27 20:26:17 -07001955 return visible || nowVisible || state == PAUSING ||
1956 state == RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001957 }
1958
Wale Ogunwale3e997362016-09-06 10:37:56 -07001959 void setSleeping(boolean _sleeping) {
1960 setSleeping(_sleeping, false);
1961 }
1962
1963 void setSleeping(boolean _sleeping, boolean force) {
1964 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001965 return;
1966 }
1967 if (app != null && app.thread != null) {
1968 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001969 app.thread.scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001970 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
1971 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001972 }
1973 sleeping = _sleeping;
1974 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001975 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001976 }
1977 }
1978 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08001979
Craig Mautnerd2328952013-03-05 12:46:26 -08001980 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001981 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08001982 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001983 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001984 }
1985 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001986 final int activityNdx = task.mActivities.indexOf(r);
1987 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001988 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001989 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001990 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08001991 }
1992
1993 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001994 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001995 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001996 }
1997
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001998 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001999 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
2000 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07002001 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08002002 }
2003 return null;
2004 }
2005
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002006 /**
2007 * @return display id to which this record is attached, -1 if not attached.
2008 */
2009 int getDisplayId() {
2010 final ActivityStack stack = getStack();
2011 if (stack == null) {
2012 return -1;
2013 }
2014 return stack.mDisplayId;
2015 }
2016
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002017 final boolean isDestroyable() {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002018 if (finishing || app == null || state == DESTROYING
2019 || state == DESTROYED) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002020 // This would be redundant.
2021 return false;
2022 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07002023 final ActivityStack stack = getStack();
2024 if (stack == null || this == stack.mResumedActivity || this == stack.mPausingActivity
2025 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002026 // We're not ready for this kind of thing.
2027 return false;
2028 }
2029 if (visible) {
2030 // The user would notice this!
2031 return false;
2032 }
2033 return true;
2034 }
2035
Winson Chung3bad5cc02014-08-19 17:44:32 -07002036 private static String createImageFilename(long createTime, int taskId) {
2037 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Ruben Brunkf53497c2017-03-27 20:26:17 -07002038 IMAGE_EXTENSION;
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002039 }
2040
Craig Mautner648f69b2014-09-18 14:16:26 -07002041 void setTaskDescription(TaskDescription _taskDescription) {
2042 Bitmap icon;
2043 if (_taskDescription.getIconFilename() == null &&
2044 (icon = _taskDescription.getIcon()) != null) {
2045 final String iconFilename = createImageFilename(createTime, task.taskId);
Winson Chungc8408b82017-01-25 17:58:56 -08002046 final File iconFile = new File(TaskPersister.getUserImagesDir(task.userId),
2047 iconFilename);
Suprabh Shukla23593142015-11-03 17:31:15 -08002048 final String iconFilePath = iconFile.getAbsolutePath();
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002049 service.mRecentTasks.saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08002050 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07002051 }
2052 taskDescription = _taskDescription;
2053 }
2054
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002055 void setVoiceSessionLocked(IVoiceInteractionSession session) {
2056 voiceSession = session;
2057 pendingVoiceInteractionStart = false;
2058 }
2059
2060 void clearVoiceSessionLocked() {
2061 voiceSession = null;
2062 pendingVoiceInteractionStart = false;
2063 }
2064
Jorim Jaggi02886a82016-12-06 09:10:06 -08002065 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch) {
Jorim Jaggi42befc62017-06-13 11:54:04 -07002066 showStartingWindow(prev, newTask, taskSwitch, false /* fromRecents */);
2067 }
2068
2069 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch,
2070 boolean fromRecents) {
Jorim Jaggi70176432017-01-18 12:52:13 +01002071 if (mWindowContainerController == null) {
2072 return;
2073 }
Wale Ogunwale19866e22017-04-19 06:05:13 -07002074 if (mTaskOverlay) {
2075 // We don't show starting window for overlay activities.
2076 return;
2077 }
2078
Wale Ogunwale3b232392016-05-13 15:37:13 -07002079 final CompatibilityInfo compatInfo =
2080 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002081 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
2082 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
Jorim Jaggibae01b12017-04-11 16:29:10 -07002083 prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning(),
Jorim Jaggi70aa4d12017-05-15 00:05:54 +02002084 allowTaskSnapshot(),
Jorim Jaggi42befc62017-06-13 11:54:04 -07002085 state.ordinal() >= RESUMED.ordinal() && state.ordinal() <= STOPPED.ordinal(),
2086 fromRecents);
Wale Ogunwale3b232392016-05-13 15:37:13 -07002087 if (shown) {
2088 mStartingWindowState = STARTING_WINDOW_SHOWN;
2089 }
2090 }
2091
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002092 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
Jorim Jaggicb956052017-05-09 16:27:24 +02002093 if (mStartingWindowState == STARTING_WINDOW_SHOWN && behindFullscreenActivity) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002094 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
2095 mStartingWindowState = STARTING_WINDOW_REMOVED;
Jorim Jaggi19be6052017-08-03 18:33:43 +02002096 mWindowContainerController.removeStartingWindow();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002097 }
2098 }
2099
2100 int getRequestedOrientation() {
2101 return mWindowContainerController.getOrientation();
2102 }
2103
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002104 void setRequestedOrientation(int requestedOrientation) {
Bryce Lee39791592017-04-26 09:29:12 -07002105 if (ActivityInfo.isFixedOrientation(requestedOrientation) && !fullscreen
Bryce Lee0e7b7c92017-08-10 14:59:00 -07002106 && appInfo.targetSdkVersion >= O_MR1) {
Bryce Lee39791592017-04-26 09:29:12 -07002107 throw new IllegalStateException("Only fullscreen activities can request orientation");
2108 }
2109
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002110 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002111 final Configuration displayConfig =
2112 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
2113
2114 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
2115 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002116 if (config != null) {
2117 frozenBeforeDestroy = true;
2118 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
2119 false /* deferResume */, displayId)) {
2120 mStackSupervisor.resumeFocusedStackTopActivityLocked();
2121 }
2122 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07002123 service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
2124 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002125 }
2126
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01002127 void setDisablePreviewScreenshots(boolean disable) {
2128 mWindowContainerController.setDisablePreviewScreenshots(disable);
2129 }
2130
Bryce Leea163b762017-01-24 11:05:01 -08002131 /**
2132 * Set the last reported global configuration to the client. Should be called whenever a new
2133 * global configuration is sent to the client for this activity.
2134 */
2135 void setLastReportedGlobalConfiguration(@NonNull Configuration config) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002136 mLastReportedConfiguration.setGlobalConfiguration(config);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002137 }
2138
Bryce Leea163b762017-01-24 11:05:01 -08002139 /**
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002140 * Set the last reported configuration to the client. Should be called whenever
Bryce Leea163b762017-01-24 11:05:01 -08002141 * a new merged configuration is sent to the client for this activity.
2142 */
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002143 void setLastReportedConfiguration(@NonNull MergedConfiguration config) {
Bryce Lee8104e7a2017-08-17 09:16:03 -07002144 setLastReportedConfiguration(config.getGlobalConfiguration(),
2145 config.getOverrideConfiguration());
Bryce Leea163b762017-01-24 11:05:01 -08002146 }
2147
Bryce Lee8104e7a2017-08-17 09:16:03 -07002148 void setLastReportedConfiguration(Configuration global, Configuration override) {
2149 mLastReportedConfiguration.setConfiguration(global, override);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002150 }
2151
2152 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07002153 public void onOverrideConfigurationChanged(Configuration newConfig) {
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002154 final Configuration currentConfig = getOverrideConfiguration();
2155 if (currentConfig.equals(newConfig)) {
2156 return;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002157 }
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002158 super.onOverrideConfigurationChanged(newConfig);
2159 if (mWindowContainerController == null) {
2160 return;
2161 }
2162 mWindowContainerController.onOverrideConfigurationChanged(newConfig, mBounds);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002163 }
2164
2165 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002166 private void updateOverrideConfiguration() {
2167 mTmpConfig.unset();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002168 computeBounds(mTmpBounds);
2169 if (mTmpBounds.equals(mBounds)) {
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002170 final ActivityStack stack = getStack();
2171 if (!mBounds.isEmpty() || task == null || stack == null || !task.mFullscreen) {
2172 // We don't want to influence the override configuration here if our task is in
2173 // multi-window mode or there is a bounds specified to calculate the override
2174 // config. In both of this cases the app should be compatible with whatever the
2175 // current configuration is or will be.
2176 return;
2177 }
2178
2179 // Currently limited to the top activity for now to avoid situations where non-top
2180 // visible activity and top might have conflicting requests putting the non-top activity
2181 // windows in an odd state.
2182 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2183 final Configuration parentConfig = getParent().getConfiguration();
2184 if (top != this || isConfigurationCompatible(parentConfig)) {
2185 onOverrideConfigurationChanged(mTmpConfig);
2186 } else if (isConfigurationCompatible(
2187 mLastReportedConfiguration.getMergedConfiguration())) {
2188 onOverrideConfigurationChanged(mLastReportedConfiguration.getMergedConfiguration());
2189 }
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002190 return;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002191 }
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002192
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002193 mBounds.set(mTmpBounds);
2194 // Bounds changed...update configuration to match.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002195 if (!mBounds.isEmpty()) {
2196 task.computeOverrideConfiguration(mTmpConfig, mBounds, null /* insetBounds */,
2197 false /* overrideWidth */, false /* overrideHeight */);
2198 }
2199 onOverrideConfigurationChanged(mTmpConfig);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002200 }
2201
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002202 /** Returns true if the configuration is compatible with this activity. */
Wale Ogunwale42f07d92017-05-01 21:32:58 -07002203 boolean isConfigurationCompatible(Configuration config) {
Wale Ogunwaled4b1d1e2017-04-10 06:35:59 -07002204 final int orientation = mWindowContainerController != null
2205 ? mWindowContainerController.getOrientation() : info.screenOrientation;
2206 if (isFixedOrientationPortrait(orientation)
2207 && config.orientation != ORIENTATION_PORTRAIT) {
2208 return false;
2209 }
2210 if (isFixedOrientationLandscape(orientation)
2211 && config.orientation != ORIENTATION_LANDSCAPE) {
2212 return false;
2213 }
2214 return true;
2215 }
2216
Bryce Lee7566d762017-03-30 09:34:15 -07002217 /**
2218 * Computes the bounds to fit the Activity within the bounds of the {@link Configuration}.
2219 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002220 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
2221 private void computeBounds(Rect outBounds) {
2222 outBounds.setEmpty();
2223 final float maxAspectRatio = info.maxAspectRatio;
2224 final ActivityStack stack = getStack();
Bryce Leee5ab4502017-07-11 08:58:05 -07002225 if (task == null || stack == null || !task.mFullscreen || maxAspectRatio == 0
2226 || isInVrUiMode(getConfiguration())) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002227 // We don't set override configuration if that activity task isn't fullscreen. I.e. the
2228 // activity is in multi-window mode. Or, there isn't a max aspect ratio specified for
Bryce Leee5ab4502017-07-11 08:58:05 -07002229 // the activity. This is indicated by an empty {@link outBounds}. We also don't set it
2230 // if we are in VR mode.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002231 return;
2232 }
2233
Bryce Lee7566d762017-03-30 09:34:15 -07002234 // We must base this on the parent configuration, because we set our override
2235 // configuration's appBounds based on the result of this method. If we used our own
2236 // configuration, it would be influenced by past invocations.
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002237 final Rect appBounds = getParent().getWindowConfiguration().getAppBounds();
Wale Ogunwale822e5122017-07-26 06:02:24 -07002238 final int containingAppWidth = appBounds.width();
2239 final int containingAppHeight = appBounds.height();
Bryce Lee7566d762017-03-30 09:34:15 -07002240 int maxActivityWidth = containingAppWidth;
2241 int maxActivityHeight = containingAppHeight;
2242
2243 if (containingAppWidth < containingAppHeight) {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002244 // Width is the shorter side, so we use that to figure-out what the max. height
2245 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002246 maxActivityHeight = (int) ((maxActivityWidth * maxAspectRatio) + 0.5f);
2247 } else {
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002248 // Height is the shorter side, so we use that to figure-out what the max. width
2249 // should be given the aspect ratio.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002250 maxActivityWidth = (int) ((maxActivityHeight * maxAspectRatio) + 0.5f);
2251 }
2252
Bryce Lee7566d762017-03-30 09:34:15 -07002253 if (containingAppWidth <= maxActivityWidth && containingAppHeight <= maxActivityHeight) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002254 // The display matches or is less than the activity aspect ratio, so nothing else to do.
Bryce Lee7566d762017-03-30 09:34:15 -07002255 // Return the existing bounds. If this method is running for the first time,
2256 // {@link mBounds} will be empty (representing no override). If the method has run
2257 // before, then effect of {@link mBounds} will already have been applied to the
2258 // value returned from {@link getConfiguration}. Refer to
2259 // {@link TaskRecord#computeOverrideConfiguration}.
2260 outBounds.set(mBounds);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002261 return;
2262 }
2263
2264 // Compute configuration based on max supported width and height.
2265 outBounds.set(0, 0, maxActivityWidth, maxActivityHeight);
Andrii Kulian3a1619d2017-07-07 14:38:09 -07002266 // Position the activity frame on the opposite side of the nav bar.
2267 final int navBarPosition = service.mWindowManager.getNavBarPosition();
Wale Ogunwale822e5122017-07-26 06:02:24 -07002268 final int left = navBarPosition == NAV_BAR_LEFT ? appBounds.right - outBounds.width() : 0;
Andrii Kulian3a1619d2017-07-07 14:38:09 -07002269 outBounds.offsetTo(left, 0 /* top */);
2270 }
2271
2272 /** Get bounds of the activity. */
2273 @VisibleForTesting
2274 Rect getBounds() {
2275 return new Rect(mBounds);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002276 }
2277
2278 /**
2279 * Make sure the given activity matches the current configuration. Returns false if the activity
2280 * had to be destroyed. Returns true if the configuration is the same, or the activity will
2281 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
2282 * correct configuration and all other bookkeeping is handled.
2283 */
2284 boolean ensureActivityConfigurationLocked(int globalChanges, boolean preserveWindow) {
2285 final ActivityStack stack = getStack();
2286 if (stack.mConfigWillChange) {
2287 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2288 "Skipping config check (will change): " + this);
2289 return true;
2290 }
2291
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002292 // We don't worry about activities that are finishing.
2293 if (finishing) {
2294 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2295 "Configuration doesn't matter in finishing " + this);
2296 stopFreezingScreenLocked(false);
2297 return true;
2298 }
2299
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002300 // Skip updating configuration for activity that are stopping or stopped.
2301 if (state == STOPPING || state == STOPPED) {
2302 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2303 "Skipping config check stopped or stopping: " + this);
2304 return true;
2305 }
2306
Wale Ogunwaleea3d3fd2017-05-01 07:41:08 -07002307 // TODO: We should add ActivityRecord.shouldBeVisible() that checks if the activity should
2308 // be visible based on the stack, task, and lockscreen state and use that here instead. The
2309 // method should be based on the logic in ActivityStack.ensureActivitiesVisibleLocked().
Wale Ogunwale9b7a8272017-04-10 08:05:41 -07002310 // Skip updating configuration for activity is a stack that shouldn't be visible.
2311 if (stack.shouldBeVisible(null /* starting */) == STACK_INVISIBLE) {
2312 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2313 "Skipping config check invisible stack: " + this);
2314 return true;
2315 }
2316
Andrii Kulian21713ac2016-10-12 22:05:05 -07002317 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2318 "Ensuring correct configuration: " + this);
2319
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002320 final int newDisplayId = getDisplayId();
2321 final boolean displayChanged = mLastReportedDisplayId != newDisplayId;
2322 if (displayChanged) {
2323 mLastReportedDisplayId = newDisplayId;
2324 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002325 // TODO(b/36505427): Is there a better place to do this?
2326 updateOverrideConfiguration();
2327
Winson Chungbdc646f2017-02-13 12:12:22 -08002328 // Short circuit: if the two full configurations are equal (the common case), then there is
2329 // nothing to do. We test the full configuration instead of the global and merged override
2330 // configurations because there are cases (like moving a task to the pinned stack) where
2331 // the combine configurations are equal, but would otherwise differ in the override config
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002332 mTmpConfig.setTo(mLastReportedConfiguration.getMergedConfiguration());
2333 if (getConfiguration().equals(mTmpConfig) && !forceNewConfig && !displayChanged) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002334 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002335 "Configuration & display unchanged in " + this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002336 return true;
2337 }
2338
2339 // Okay we now are going to make this activity have the new config.
2340 // But then we need to figure out how it needs to deal with that.
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002341
2342 // Find changes between last reported merged configuration and the current one. This is used
2343 // to decide whether to relaunch an activity or just report a configuration change.
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002344 final int changes = getConfigurationChanges(mTmpConfig);
Ruben Brunkf64af332017-03-22 22:03:25 -07002345
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002346 // Update last reported values.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002347 final Configuration newMergedOverrideConfig = getMergedOverrideConfiguration();
Bryce Lee8104e7a2017-08-17 09:16:03 -07002348
2349 setLastReportedConfiguration(service.getGlobalConfiguration(), newMergedOverrideConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002350
Andrii Kulian21713ac2016-10-12 22:05:05 -07002351 if (changes == 0 && !forceNewConfig) {
2352 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2353 "Configuration no differences in " + this);
2354 // There are no significant differences, so we won't relaunch but should still deliver
2355 // the new configuration to the client process.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002356 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002357 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002358 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002359 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002360 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002361 return true;
2362 }
2363
2364 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002365 "Configuration changes for " + this + ", allChanges="
Andrii Kulian21713ac2016-10-12 22:05:05 -07002366 + Configuration.configurationDiffToString(changes));
2367
2368 // If the activity isn't currently running, just leave the new configuration and it will
2369 // pick that up next time it starts.
2370 if (app == null || app.thread == null) {
2371 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2372 "Configuration doesn't matter not running " + this);
2373 stopFreezingScreenLocked(false);
2374 forceNewConfig = false;
2375 return true;
2376 }
2377
2378 // Figure out how to handle the changes between the configurations.
2379 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2380 "Checking to restart " + info.name + ": changed=0x"
2381 + Integer.toHexString(changes) + ", handles=0x"
2382 + Integer.toHexString(info.getRealConfigChanged())
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002383 + ", mLastReportedConfiguration=" + mLastReportedConfiguration);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002384
Wale Ogunwalee610d3d2017-04-25 10:23:48 -07002385 if (shouldRelaunchLocked(changes, mTmpConfig) || forceNewConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002386 // Aha, the activity isn't handling the change, so DIE DIE DIE.
2387 configChangeFlags |= changes;
2388 startFreezingScreenLocked(app, globalChanges);
2389 forceNewConfig = false;
2390 preserveWindow &= isResizeOnlyChange(changes);
2391 if (app == null || app.thread == null) {
2392 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2393 "Config is destroying non-running " + this);
2394 stack.destroyActivityLocked(this, true, "config");
Ruben Brunkf53497c2017-03-27 20:26:17 -07002395 } else if (state == PAUSING) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002396 // A little annoying: we are waiting for this activity to finish pausing. Let's not
2397 // do anything now, but just flag that it needs to be restarted when done pausing.
2398 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2399 "Config is skipping already pausing " + this);
2400 deferRelaunchUntilPaused = true;
2401 preserveWindowOnDeferredRelaunch = preserveWindow;
2402 return true;
Ruben Brunkf53497c2017-03-27 20:26:17 -07002403 } else if (state == RESUMED) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002404 // Try to optimize this case: the configuration is changing and we need to restart
2405 // the top, resumed activity. Instead of doing the normal handshaking, just say
2406 // "restart!".
2407 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2408 "Config is relaunching resumed " + this);
2409
2410 if (DEBUG_STATES && !visible) {
2411 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
2412 + " called by " + Debug.getCallers(4));
2413 }
2414
2415 relaunchActivityLocked(true /* andResume */, preserveWindow);
2416 } else {
2417 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2418 "Config is relaunching non-resumed " + this);
2419 relaunchActivityLocked(false /* andResume */, preserveWindow);
2420 }
2421
2422 // All done... tell the caller we weren't able to keep this activity around.
2423 return false;
2424 }
2425
2426 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002427 // NOTE: We only forward the override configuration as the system level configuration
Andrii Kulian21713ac2016-10-12 22:05:05 -07002428 // changes is always sent to all processes when they happen so it can just use whatever
2429 // system level configuration it last got.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002430 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002431 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002432 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002433 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002434 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002435 stopFreezingScreenLocked(false);
2436
2437 return true;
2438 }
2439
Zak Cohen90e7116742017-01-29 12:59:23 -08002440 /**
2441 * When assessing a configuration change, decide if the changes flags and the new configurations
2442 * should cause the Activity to relaunch.
Ruben Brunkf64af332017-03-22 22:03:25 -07002443 *
2444 * @param changes the changes due to the given configuration.
2445 * @param changesConfig the configuration that was used to calculate the given changes via a
2446 * call to getConfigurationChanges.
Zak Cohen90e7116742017-01-29 12:59:23 -08002447 */
Ruben Brunkf64af332017-03-22 22:03:25 -07002448 private boolean shouldRelaunchLocked(int changes, Configuration changesConfig) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002449 int configChanged = info.getRealConfigChanged();
Ruben Brunkf64af332017-03-22 22:03:25 -07002450 boolean onlyVrUiModeChanged = onlyVrUiModeChanged(changes, changesConfig);
Zak Cohen90e7116742017-01-29 12:59:23 -08002451
2452 // Override for apps targeting pre-O sdks
2453 // If a device is in VR mode, and we're transitioning into VR ui mode, add ignore ui mode
2454 // to the config change.
2455 // For O and later, apps will be required to add configChanges="uimode" to their manifest.
2456 if (appInfo.targetSdkVersion < O
2457 && requestedVrComponent != null
Ruben Brunkf64af332017-03-22 22:03:25 -07002458 && onlyVrUiModeChanged) {
Zak Cohen90e7116742017-01-29 12:59:23 -08002459 configChanged |= CONFIG_UI_MODE;
2460 }
2461
2462 return (changes&(~configChanged)) != 0;
2463 }
2464
Ruben Brunkf64af332017-03-22 22:03:25 -07002465 /**
2466 * Returns true if the configuration change is solely due to the UI mode switching into or out
2467 * of UI_MODE_TYPE_VR_HEADSET.
2468 */
2469 private boolean onlyVrUiModeChanged(int changes, Configuration lastReportedConfig) {
2470 final Configuration currentConfig = getConfiguration();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002471 return changes == CONFIG_UI_MODE && (isInVrUiMode(currentConfig)
Ruben Brunkf64af332017-03-22 22:03:25 -07002472 != isInVrUiMode(lastReportedConfig));
2473 }
2474
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002475 private int getConfigurationChanges(Configuration lastReportedConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002476 // Determine what has changed. May be nothing, if this is a config that has come back from
2477 // the app after going idle. In that case we just want to leave the official config object
2478 // now in the activity and do nothing else.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002479 final Configuration currentConfig = getConfiguration();
2480 int changes = lastReportedConfig.diff(currentConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002481 // We don't want to use size changes if they don't cross boundaries that are important to
2482 // the app.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002483 if ((changes & CONFIG_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002484 final boolean crosses = crossesHorizontalSizeThreshold(lastReportedConfig.screenWidthDp,
2485 currentConfig.screenWidthDp)
2486 || crossesVerticalSizeThreshold(lastReportedConfig.screenHeightDp,
2487 currentConfig.screenHeightDp);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002488 if (!crosses) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002489 changes &= ~CONFIG_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002490 }
2491 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002492 if ((changes & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002493 final int oldSmallest = lastReportedConfig.smallestScreenWidthDp;
2494 final int newSmallest = currentConfig.smallestScreenWidthDp;
2495 if (!crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002496 changes &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002497 }
2498 }
Wale Ogunwale822e5122017-07-26 06:02:24 -07002499 // We don't want window configuration to cause relaunches.
2500 if ((changes & CONFIG_WINDOW_CONFIGURATION) != 0) {
2501 changes &= ~CONFIG_WINDOW_CONFIGURATION;
2502 }
Bryce Lee600dadd2017-07-25 10:48:42 -07002503
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002504 return changes;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002505 }
2506
2507 private static boolean isResizeOnlyChange(int change) {
2508 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2509 | CONFIG_SCREEN_LAYOUT)) == 0;
2510 }
2511
2512 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
2513 if (service.mSuppressResizeConfigChanges && preserveWindow) {
2514 configChangeFlags = 0;
2515 return;
2516 }
2517
2518 List<ResultInfo> pendingResults = null;
2519 List<ReferrerIntent> pendingNewIntents = null;
2520 if (andResume) {
2521 pendingResults = results;
2522 pendingNewIntents = newIntents;
2523 }
2524 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2525 "Relaunching: " + this + " with results=" + pendingResults
2526 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2527 + " preserveWindow=" + preserveWindow);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002528 EventLog.writeEvent(andResume ? AM_RELAUNCH_RESUME_ACTIVITY
2529 : AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
Andrii Kulian21713ac2016-10-12 22:05:05 -07002530 task.taskId, shortComponentName);
2531
2532 startFreezingScreenLocked(app, 0);
2533
Andrii Kulian21713ac2016-10-12 22:05:05 -07002534 try {
2535 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2536 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2537 + " callers=" + Debug.getCallers(6));
2538 forceNewConfig = false;
2539 mStackSupervisor.activityRelaunchingLocked(this);
2540 app.thread.scheduleRelaunchActivity(appToken, pendingResults, pendingNewIntents,
2541 configChangeFlags, !andResume,
2542 new Configuration(service.getGlobalConfiguration()),
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002543 new Configuration(getMergedOverrideConfiguration()), preserveWindow);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002544 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
2545 // pass in 'andResume' if this activity is currently resumed, which implies we aren't
2546 // sleeping.
2547 } catch (RemoteException e) {
2548 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2549 }
2550
2551 if (andResume) {
2552 if (DEBUG_STATES) {
2553 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2554 }
2555 results = null;
2556 newIntents = null;
2557 service.showUnsupportedZoomDialogIfNeededLocked(this);
2558 service.showAskCompatModeDialogLocked(this);
2559 } else {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002560 service.mHandler.removeMessages(PAUSE_TIMEOUT_MSG, this);
2561 state = PAUSED;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002562 // if the app is relaunched when it's stopped, and we're not resuming,
2563 // put it back into stopped state.
2564 if (stopped) {
Winson Chung4dabf232017-01-25 13:25:22 -08002565 getStack().addToStopping(this, true /* scheduleIdle */, false /* idleDelayed */);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002566 }
2567 }
2568
2569 configChangeFlags = 0;
2570 deferRelaunchUntilPaused = false;
2571 preserveWindowOnDeferredRelaunch = false;
2572 }
2573
Jorim Jaggibae01b12017-04-11 16:29:10 -07002574 private boolean isProcessRunning() {
Jorim Jaggi02886a82016-12-06 09:10:06 -08002575 ProcessRecord proc = app;
2576 if (proc == null) {
2577 proc = service.mProcessNames.get(processName, info.applicationInfo.uid);
2578 }
2579 return proc != null && proc.thread != null;
2580 }
2581
Jorim Jaggibae01b12017-04-11 16:29:10 -07002582 /**
2583 * @return Whether a task snapshot starting window may be shown.
2584 */
2585 private boolean allowTaskSnapshot() {
2586 if (newIntents == null) {
2587 return true;
2588 }
2589
2590 // Restrict task snapshot starting window to launcher start, or there is no intent at all
2591 // (eg. task being brought to front). If the intent is something else, likely the app is
2592 // going to show some specific page or view, instead of what's left last time.
2593 for (int i = newIntents.size() - 1; i >= 0; i--) {
2594 final Intent intent = newIntents.get(i);
2595 if (intent != null && !ActivityRecord.isMainIntent(intent)) {
2596 return false;
2597 }
2598 }
2599 return true;
2600 }
2601
Bryce Leeb7c9b802017-05-02 14:20:24 -07002602 /**
2603 * Returns {@code true} if the associated activity has the no history flag set on it.
2604 * {@code false} otherwise.
2605 */
2606 boolean isNoHistory() {
2607 return (intent.getFlags() & FLAG_ACTIVITY_NO_HISTORY) != 0
2608 || (info.flags & FLAG_NO_HISTORY) != 0;
2609 }
2610
Craig Mautner21d24a22014-04-23 11:45:37 -07002611 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2612 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2613 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2614 if (launchedFromPackage != null) {
2615 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2616 }
2617 if (resolvedType != null) {
2618 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2619 }
2620 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2621 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002622
Craig Mautner21d24a22014-04-23 11:45:37 -07002623 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002624 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002625 }
2626
2627 out.startTag(null, TAG_INTENT);
2628 intent.saveToXml(out);
2629 out.endTag(null, TAG_INTENT);
2630
2631 if (isPersistable() && persistentState != null) {
2632 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2633 persistentState.saveToXml(out);
2634 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2635 }
2636 }
2637
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002638 static ActivityRecord restoreFromXml(XmlPullParser in,
2639 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002640 Intent intent = null;
2641 PersistableBundle persistentState = null;
2642 int launchedFromUid = 0;
2643 String launchedFromPackage = null;
2644 String resolvedType = null;
2645 boolean componentSpecified = false;
2646 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002647 long createTime = -1;
2648 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002649 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002650
2651 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2652 final String attrName = in.getAttributeName(attrNdx);
2653 final String attrValue = in.getAttributeValue(attrNdx);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002654 if (DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002655 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002656 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002657 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002658 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002659 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002660 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2661 launchedFromPackage = attrValue;
2662 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2663 resolvedType = attrValue;
2664 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002665 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002666 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002667 userId = Integer.parseInt(attrValue);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002668 } else if (attrName.startsWith(ATTR_TASKDESCRIPTION_PREFIX)) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002669 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002670 } else {
2671 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2672 }
2673 }
2674
2675 int event;
Ruben Brunkf53497c2017-03-27 20:26:17 -07002676 while (((event = in.next()) != END_DOCUMENT) &&
2677 (event != END_TAG || in.getDepth() >= outerDepth)) {
2678 if (event == START_TAG) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002679 final String name = in.getName();
Ruben Brunkf53497c2017-03-27 20:26:17 -07002680 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002681 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002682 if (TAG_INTENT.equals(name)) {
2683 intent = Intent.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002684 if (DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002685 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002686 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2687 persistentState = PersistableBundle.restoreFromXml(in);
Ruben Brunkf53497c2017-03-27 20:26:17 -07002688 if (DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002689 "ActivityRecord: persistentState=" + persistentState);
2690 } else {
2691 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2692 XmlUtils.skipCurrentTag(in);
2693 }
2694 }
2695 }
2696
2697 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002698 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002699 }
2700
2701 final ActivityManagerService service = stackSupervisor.mService;
2702 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Jeff Hao1b012d32014-08-20 10:35:34 -07002703 userId);
Craig Mautnere0129b32014-05-25 16:41:09 -07002704 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002705 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2706 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002707 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002708 final ActivityRecord r = new ActivityRecord(service, null /* caller */,
2709 0 /* launchedFromPid */, launchedFromUid, launchedFromPackage, intent, resolvedType,
2710 aInfo, service.getConfiguration(), null /* resultTo */, null /* resultWho */,
2711 0 /* reqCode */, componentSpecified, false /* rootVoiceInteraction */,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -07002712 stackSupervisor, null /* options */, null /* sourceRecord */);
Craig Mautner21d24a22014-04-23 11:45:37 -07002713
2714 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002715 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002716 r.createTime = createTime;
2717
2718 return r;
2719 }
2720
2721 private static String activityTypeToString(int type) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002722 switch (type) {
2723 case APPLICATION_ACTIVITY_TYPE: return "APPLICATION_ACTIVITY_TYPE";
2724 case HOME_ACTIVITY_TYPE: return "HOME_ACTIVITY_TYPE";
2725 case RECENTS_ACTIVITY_TYPE: return "RECENTS_ACTIVITY_TYPE";
Winson Chung83471632016-12-13 11:02:12 -08002726 case ASSISTANT_ACTIVITY_TYPE: return "ASSISTANT_ACTIVITY_TYPE";
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002727 default: return Integer.toString(type);
2728 }
2729 }
2730
Zak Cohen90e7116742017-01-29 12:59:23 -08002731 private static boolean isInVrUiMode(Configuration config) {
Ruben Brunkf53497c2017-03-27 20:26:17 -07002732 return (config.uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_VR_HEADSET;
Zak Cohen90e7116742017-01-29 12:59:23 -08002733 }
2734
David Stevens82ea6cb2017-03-03 16:18:50 -08002735 int getUid() {
2736 return info.applicationInfo.uid;
2737 }
2738
chaviw59b98852017-06-13 12:05:44 -07002739 void setShowWhenLocked(boolean showWhenLocked) {
2740 mShowWhenLocked = showWhenLocked;
2741 }
2742
2743 /**
2744 * @return true if the activity contains windows that have
2745 * {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set or if the activity has set
2746 * {@link #mShowWhenLocked}.
2747 */
2748 boolean canShowWhenLocked() {
2749 return mShowWhenLocked || service.mWindowManager.containsShowWhenLockedWindow(appToken);
2750 }
2751
2752 void setTurnScreenOn(boolean turnScreenOn) {
2753 mTurnScreenOn = turnScreenOn;
2754 }
2755
2756 /**
2757 * Determines whether this ActivityRecord can turn the screen on. It checks whether the flag
2758 * {@link #mTurnScreenOn} is set and checks whether the ActivityRecord should be visible
2759 * depending on Keyguard state
2760 *
2761 * @return true if the screen can be turned on, false otherwise.
2762 */
2763 boolean canTurnScreenOn() {
2764 final ActivityStack stack = getStack();
2765 return mTurnScreenOn && stack != null &&
2766 stack.checkKeyguardVisibility(this, true /* shouldBeVisible */, true /* isTop */);
2767 }
2768
2769 boolean getTurnScreenOnFlag() {
2770 return mTurnScreenOn;
2771 }
2772
2773 boolean isTopRunningActivity() {
2774 return mStackSupervisor.topRunningActivityLocked() == this;
2775 }
2776
Craig Mautnerf81b90872013-02-26 13:02:43 -08002777 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002779 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002780 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002781 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002782 }
2783 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002784 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002785 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002786 sb.append(" u");
2787 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002788 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002789 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002790 stringName = sb.toString();
2791 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 }
2793}