blob: 1b193825c41de2b3a8a80505046697f722ffef47 [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
Jorim Jaggi02886a82016-12-06 09:10:06 -080019import static android.app.ActivityManager.ENABLE_TASK_SNAPSHOTS;
Winson Chungbb348802017-01-30 12:01:45 -080020import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -080021import static android.app.ActivityManager.StackId;
Winson Chung83471632016-12-13 11:02:12 -080022import static android.app.ActivityManager.StackId.ASSISTANT_STACK_ID;
Wale Ogunwaleeb915f22016-10-14 07:18:22 -070023import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080024import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Winson Chung83471632016-12-13 11:02:12 -080025import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -080026import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwale22e25262016-02-01 10:32:02 -080027import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Winson Chung59fda9e2017-01-20 16:14:51 -080028import static android.app.AppOpsManager.MODE_ALLOWED;
29import static android.app.AppOpsManager.OP_ENTER_PICTURE_IN_PICTURE_ON_HIDE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080030import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070031import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
32import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
33import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
34import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Zak Cohen90e7116742017-01-29 12:59:23 -080035import static android.content.pm.ActivityInfo.CONFIG_UI_MODE;
Jorim Jaggi02886a82016-12-06 09:10:06 -080036import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080037import static android.content.pm.ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
38import static android.content.pm.ActivityInfo.FLAG_IMMERSIVE;
39import static android.content.pm.ActivityInfo.FLAG_MULTIPROCESS;
Chong Zhang87761972016-08-22 13:53:24 -070040import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080041import static android.content.pm.ActivityInfo.FLAG_STATE_NOT_NEEDED;
42import static android.content.pm.ActivityInfo.LAUNCH_MULTIPLE;
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -080043import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
44import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080045import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070046import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggicd13d332016-04-27 15:40:20 -070047import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwale72a73e32016-10-13 12:16:39 -070048import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070049import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Zak Cohen90e7116742017-01-29 12:59:23 -080050import static android.content.res.Configuration.UI_MODE_TYPE_MASK;
51import static android.content.res.Configuration.UI_MODE_TYPE_VR_HEADSET;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080052import static android.os.Build.VERSION_CODES.HONEYCOMB;
Zak Cohen90e7116742017-01-29 12:59:23 -080053import static android.os.Build.VERSION_CODES.O;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080054import static android.os.Process.SYSTEM_UID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080055import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
Andrii Kulian21713ac2016-10-12 22:05:05 -070056import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SCREENSHOTS;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080059import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070061import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080065import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070068import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080069import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Andrii Kulian21713ac2016-10-12 22:05:05 -070071import static com.android.server.am.ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS;
Wale Ogunwale18795a22014-12-03 11:38:33 -080072import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
73
Andrii Kulian21713ac2016-10-12 22:05:05 -070074import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -070075import android.app.ActivityManager.TaskDescription;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070076import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080077import android.app.PendingIntent;
Winson Chungc2baac02017-01-11 13:34:47 -080078import android.app.PictureInPictureArgs;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080079import android.app.ResultInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.content.ComponentName;
81import android.content.Intent;
82import android.content.pm.ActivityInfo;
83import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -070084import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.content.res.Configuration;
86import android.graphics.Bitmap;
Dianne Hackbornd367ca82012-05-07 15:49:39 -070087import android.graphics.Rect;
Zak Cohen90e7116742017-01-29 12:59:23 -080088import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -070090import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -080091import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080093import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -070095import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.SystemClock;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080097import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070098import android.os.UserHandle;
Amith Yamasani0af6fa72016-01-17 15:36:19 -080099import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.util.EventLog;
101import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700102import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700103import android.util.TimeUtils;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700104import android.view.AppTransitionAnimationSpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.IApplicationToken;
Jorim Jaggife762342016-10-13 14:33:27 +0200106import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700107
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800108import com.android.internal.app.ResolverActivity;
109import com.android.internal.content.ReferrerIntent;
110import com.android.internal.util.XmlUtils;
111import com.android.server.AttributeCache;
112import com.android.server.am.ActivityStack.ActivityState;
113import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800114import com.android.server.wm.AppWindowContainerController;
115import com.android.server.wm.AppWindowContainerListener;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800116import com.android.server.wm.TaskWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Jorim Jaggi02886a82016-12-06 09:10:06 -0800118import org.xmlpull.v1.XmlPullParser;
119import org.xmlpull.v1.XmlPullParserException;
120import org.xmlpull.v1.XmlSerializer;
121
Suprabh Shukla23593142015-11-03 17:31:15 -0800122import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -0700123import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import java.io.PrintWriter;
125import java.lang.ref.WeakReference;
126import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800127import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700129import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700130import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132/**
133 * An entry in the history stack, representing an activity.
134 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800135final class ActivityRecord implements AppWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800136 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_AM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700137 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
138 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
139 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700140 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700141 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
142 private static final String TAG_THUMBNAILS = TAG + POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700143 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800144
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700145 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700146 private static final String RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700147
Craig Mautner21d24a22014-04-23 11:45:37 -0700148 private static final String ATTR_ID = "id";
149 private static final String TAG_INTENT = "intent";
150 private static final String ATTR_USERID = "user_id";
151 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
152 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700153 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700154 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
155 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700156 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 final ActivityManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159 final IApplicationToken.Stub appToken; // window manager token
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800160 AppWindowContainerController mWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 final ActivityInfo info; // all about me
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700162 final ApplicationInfo appInfo; // information about activity's app
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800163 final int launchedFromPid; // always the pid who started the activity.
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700164 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800165 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700166 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final Intent intent; // the original intent that generated us
168 final ComponentName realActivity; // the intent component, or target of an alias.
169 final String shortComponentName; // the short component name of the intent
170 final String resolvedType; // as per original caller;
171 final String packageName; // the package implementing intent's component
172 final String processName; // process where this component wants to run
173 final String taskAffinity; // as per ActivityInfo.taskAffinity
174 final boolean stateNotNeeded; // As per ActivityInfo.flags
Craig Mautner4addfc52013-06-25 08:05:45 -0700175 boolean fullscreen; // covers the full screen?
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800176 final boolean noDisplay; // activity is not displayed?
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800177 private final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700178 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700179
180 static final int APPLICATION_ACTIVITY_TYPE = 0;
181 static final int HOME_ACTIVITY_TYPE = 1;
182 static final int RECENTS_ACTIVITY_TYPE = 2;
Winson Chung83471632016-12-13 11:02:12 -0800183 static final int ASSISTANT_ACTIVITY_TYPE = 3;
Craig Mautner2c1faed2013-07-23 12:56:02 -0700184 int mActivityType;
Craig Mautner86d67a42013-05-14 10:34:38 -0700185
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800186 private CharSequence nonLocalizedLabel; // the label information from the package mgr.
187 private int labelRes; // the label information from the package mgr.
188 private int icon; // resource identifier of activity's icon.
189 private int logo; // resource identifier of activity's logo.
190 private int theme; // resource identifier of activity's theme.
191 private int realTheme; // actual theme resource we will use, never 0.
192 private int windowFlags; // custom window flags for preview window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 TaskRecord task; // the task this is in.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800194 private long createTime = System.currentTimeMillis();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700195 long displayStartTime; // when we started launching this activity
196 long fullyDrawnStartTime; // when we started launching this activity
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800197 private long startTime; // last time this activity was started
Dianne Hackborn50685602011-12-01 12:23:37 -0800198 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700199 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700200 long pauseTime; // last time we started pausing the activity
201 long launchTickTime; // base time for launch tick messages
Andrii Kulian21713ac2016-10-12 22:05:05 -0700202 private Configuration mLastReportedConfiguration; // configuration activity was last running in
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700203 // Overridden configuration by the activity task
Andrii Kulian1779e612016-10-12 21:58:25 -0700204 // WARNING: Reference points to {@link TaskRecord#getMergedOverrideConfig}, so its internal
205 // state should never be altered directly.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700206 private Configuration mLastReportedOverrideConfiguration;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700207 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700208 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final String resultWho; // additional identifier for use by resultTo.
210 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800211 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800213 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700214 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700215 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700216 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -0700218 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700219 ProcessRecord app; // if non-null, hosting application
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700220 ActivityState state; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700222 PersistableBundle persistentState; // last persistently saved activity state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 boolean frontOfTask; // is this the root activity of its task?
224 boolean launchFailed; // set if a launched failed, to abort on 2nd try
225 boolean haveState; // have we gotten the last activity state?
226 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700227 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800229 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
230 // completed
231 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 int configChangeFlags; // which config values have changed
233 boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 int launchMode; // the launch mode activity attribute.
235 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700236 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
237 // might hide this activity?
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800238 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 boolean nowVisible; // is this activity's window visible?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 boolean idle; // has the activity gone idle?
241 boolean hasBeenLaunched;// has this activity ever been launched?
242 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400243 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400244 boolean forceNewConfig; // force re-create with new config next time
Winson Chungc2baac02017-01-11 13:34:47 -0800245 boolean supportsPictureInPictureWhilePausing; // This flag is set by the system to indicate
246 // that the activity can enter picture in picture while pausing (ie. only when another
247 // task is brought to front or started)
248 PictureInPictureArgs pictureInPictureArgs = new PictureInPictureArgs(); // The PiP
Winson Chunga29eb982016-12-14 12:01:27 -0800249 // arguments used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800250 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800251 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800252 ComponentName requestedVrComponent; // the requested component for handling VR mode.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800253 ArrayList<ActivityContainer> mChildContainers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700255 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700256
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700257 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700258 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800259
260 static final int STARTING_WINDOW_NOT_SHOWN = 0;
261 static final int STARTING_WINDOW_SHOWN = 1;
262 static final int STARTING_WINDOW_REMOVED = 2;
263 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700264 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800265
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800266 boolean mUpdateTaskThumbnailWhenHidden;
Craig Mautnere0a38842013-12-16 16:14:02 -0800267 ActivityContainer mInitialActivityContainer;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700268
Craig Mautner21d24a22014-04-23 11:45:37 -0700269 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700270 boolean mLaunchTaskBehind; // this activity is actively being launched with
271 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700272
Filip Gruszczynski23493322015-07-29 17:02:59 -0700273 // These configurations are collected from application's resources based on size-sensitive
274 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
275 // and drawable-sw400dp will be added to both as 400.
276 private int[] mVerticalSizeConfigurations;
277 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700278 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700279
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800280 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
281 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
282
Robert Carrd2265122016-08-05 10:25:21 -0700283 // A hint to override the window specified rotation animation, or -1
284 // to use the window specified value. We use this so that
285 // we can select the right animation in the cases of starting
286 // windows, where the app hasn't had time to set a value
287 // on the window.
288 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700289
Andrii Kulian21713ac2016-10-12 22:05:05 -0700290 /**
291 * Temp configs used in {@link #ensureActivityConfigurationLocked(int, boolean)}
292 */
293 private final Configuration mTmpGlobalConfig = new Configuration();
294 private final Configuration mTmpTaskConfig = new Configuration();
295
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800296 private static String startingWindowStateToString(int state) {
297 switch (state) {
298 case STARTING_WINDOW_NOT_SHOWN:
299 return "STARTING_WINDOW_NOT_SHOWN";
300 case STARTING_WINDOW_SHOWN:
301 return "STARTING_WINDOW_SHOWN";
302 case STARTING_WINDOW_REMOVED:
303 return "STARTING_WINDOW_REMOVED";
304 default:
305 return "unknown state=" + state;
306 }
307 }
308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700310 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700311 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
312 pw.print(" processName="); pw.println(processName);
313 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700314 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800315 pw.print(" userId="); pw.println(userId);
316 pw.print(prefix); pw.print("app="); pw.println(app);
317 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700318 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
319 pw.print(" task="); pw.println(task);
320 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700321 pw.print(prefix); pw.print("realActivity=");
322 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700323 if (appInfo != null) {
324 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
325 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
326 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
327 }
328 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800329 if (appInfo.splitSourceDirs != null) {
330 pw.print(prefix); pw.print("splitDir=");
331 pw.println(Arrays.toString(appInfo.splitSourceDirs));
332 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800333 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700334 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
335 pw.print(" componentSpecified="); pw.print(componentSpecified);
Craig Mautner86d67a42013-05-14 10:34:38 -0700336 pw.print(" mActivityType="); pw.println(mActivityType);
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700337 if (rootVoiceInteraction) {
338 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
339 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800340 pw.print(prefix); pw.print("compat="); pw.print(compat);
341 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
342 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
343 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Andrii Kulian1779e612016-10-12 21:58:25 -0700344 pw.print(prefix); pw.print("mLastReportedConfiguration=");
345 pw.println(mLastReportedConfiguration);
346 pw.print(prefix); pw.print("mLastReportedOverrideConfiguration=");
347 pw.println(mLastReportedOverrideConfiguration);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700348 if (resultTo != null || resultWho != null) {
349 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
350 pw.print(" resultWho="); pw.print(resultWho);
351 pw.print(" resultCode="); pw.println(requestCode);
352 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700353 if (taskDescription != null) {
354 final String iconFilename = taskDescription.getIconFilename();
355 if (iconFilename != null || taskDescription.getLabel() != null ||
356 taskDescription.getPrimaryColor() != 0) {
357 pw.print(prefix); pw.print("taskDescription:");
358 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
359 pw.print(" label=\""); pw.print(taskDescription.getLabel());
360 pw.print("\"");
361 pw.print(" color=");
362 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
363 }
364 if (iconFilename == null && taskDescription.getIcon() != null) {
365 pw.print(prefix); pw.println("taskDescription contains Bitmap");
366 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700367 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700368 if (results != null) {
369 pw.print(prefix); pw.print("results="); pw.println(results);
370 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700371 if (pendingResults != null && pendingResults.size() > 0) {
372 pw.print(prefix); pw.println("Pending Results:");
373 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
374 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
375 pw.print(prefix); pw.print(" - ");
376 if (pir == null) {
377 pw.println("null");
378 } else {
379 pw.println(pir);
380 pir.dump(pw, prefix + " ");
381 }
382 }
383 }
384 if (newIntents != null && newIntents.size() > 0) {
385 pw.print(prefix); pw.println("Pending New Intents:");
386 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800387 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700388 pw.print(prefix); pw.print(" - ");
389 if (intent == null) {
390 pw.println("null");
391 } else {
392 pw.println(intent.toShortString(false, true, false, true));
393 }
394 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700395 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700396 if (pendingOptions != null) {
397 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
398 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700399 if (appTimeTracker != null) {
400 appTimeTracker.dumpWithHeader(pw, prefix, false);
401 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700402 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700403 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700404 }
405 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800406 pw.print(" launchCount="); pw.print(launchCount);
407 pw.print(" lastLaunchTime=");
408 if (lastLaunchTime == 0) pw.print("0");
409 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
410 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700411 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700412 pw.print(" icicle="); pw.println(icicle);
413 pw.print(prefix); pw.print("state="); pw.print(state);
414 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700415 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700416 pw.print(" finishing="); pw.println(finishing);
417 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
418 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700419 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800420 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800421 pw.print(" idle="); pw.print(idle);
422 pw.print(" mStartingWindowState=");
423 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800424 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
425 pw.print(" noDisplay="); pw.print(noDisplay);
426 pw.print(" immersive="); pw.print(immersive);
427 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800428 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400429 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700430 pw.print(prefix); pw.print("mActivityType=");
431 pw.println(activityTypeToString(mActivityType));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800432 if (requestedVrComponent != null) {
433 pw.print(prefix);
434 pw.print("requestedVrComponent=");
435 pw.println(requestedVrComponent);
436 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700437 if (displayStartTime != 0 || startTime != 0) {
438 pw.print(prefix); pw.print("displayStartTime=");
439 if (displayStartTime == 0) pw.print("0");
440 else TimeUtils.formatDuration(displayStartTime, now, pw);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700441 pw.print(" startTime=");
442 if (startTime == 0) pw.print("0");
443 else TimeUtils.formatDuration(startTime, now, pw);
444 pw.println();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700445 }
Craig Mautner8c14c152015-01-15 17:32:07 -0800446 final boolean waitingVisible = mStackSupervisor.mWaitingVisibleActivities.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800447 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700448 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800449 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700450 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700451 if (lastVisibleTime == 0) pw.print("0");
452 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
453 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700454 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800455 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
456 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700457 pw.print(" configChangeFlags=");
458 pw.println(Integer.toHexString(configChangeFlags));
459 }
460 if (connections != null) {
461 pw.print(prefix); pw.print("connections="); pw.println(connections);
462 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800463 if (info != null) {
464 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
Winson Chungd3395382016-12-13 11:49:09 -0800465 pw.println(prefix + "supportsPictureInPicture=" + info.supportsPictureInPicture());
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800466 }
Winson Chungc2baac02017-01-11 13:34:47 -0800467 pw.println(prefix + "supportsPictureInPictureWhilePausing: "
468 + supportsPictureInPictureWhilePausing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
470
Andrii Kulian21713ac2016-10-12 22:05:05 -0700471 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700472 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
473 }
474
Andrii Kulian21713ac2016-10-12 22:05:05 -0700475 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700476 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
477 }
478
Andrii Kulian21713ac2016-10-12 22:05:05 -0700479 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700480 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
481 }
482
Filip Gruszczynski23493322015-07-29 17:02:59 -0700483 /**
484 * The purpose of this method is to decide whether the activity needs to be relaunched upon
485 * changing its size. In most cases the activities don't need to be relaunched, if the resize
486 * is small, all the activity content has to do is relayout itself within new bounds. There are
487 * cases however, where the activity's content would be completely changed in the new size and
488 * the full relaunch is required.
489 *
490 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
491 * required. These thresholds are collected from the application resource qualifiers. For
492 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
493 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
494 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
495 * of the threshold.
496 */
497 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
498 int secondDp) {
499 if (thresholds == null) {
500 return false;
501 }
502 for (int i = thresholds.length - 1; i >= 0; i--) {
503 final int threshold = thresholds[i];
504 if ((firstDp < threshold && secondDp >= threshold)
505 || (firstDp >= threshold && secondDp < threshold)) {
506 return true;
507 }
508 }
509 return false;
510 }
511
Andrii Kulian21713ac2016-10-12 22:05:05 -0700512 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700513 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700514 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
515 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700516 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700517 }
518
Andrii Kulian21713ac2016-10-12 22:05:05 -0700519 private void scheduleConfigurationChanged(Configuration config, boolean reportToActivity) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800520 if (app == null || app.thread == null) {
521 return;
522 }
523 try {
524 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + " " +
Adam Lesinskibad43fc2016-07-19 13:35:01 -0700525 "reportToActivity=" + reportToActivity + " and config: " + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700526
Andrii Kulian1779e612016-10-12 21:58:25 -0700527 app.thread.scheduleActivityConfigurationChanged(appToken, new Configuration(config),
528 reportToActivity);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800529 } catch (RemoteException e) {
530 // If process died, whatever.
531 }
532 }
533
Andrii Kulian933076d2016-03-29 17:04:42 -0700534 void scheduleMultiWindowModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700535 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800536 return;
537 }
538 try {
539 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Andrii Kulian933076d2016-03-29 17:04:42 -0700540 app.thread.scheduleMultiWindowModeChanged(appToken, !task.mFullscreen);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800541 } catch (Exception e) {
542 // If process died, I don't care.
543 }
544 }
545
Andrii Kulian933076d2016-03-29 17:04:42 -0700546 void schedulePictureInPictureModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700547 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800548 return;
549 }
550 try {
Andrii Kulian933076d2016-03-29 17:04:42 -0700551 app.thread.schedulePictureInPictureModeChanged(
Andrii Kulian02b7a832016-10-06 23:11:56 -0700552 appToken, task.getStackId() == PINNED_STACK_ID);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800553 } catch (Exception e) {
554 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800555 }
556 }
557
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800558 boolean isFreeform() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700559 return task != null && task.getStackId() == FREEFORM_WORKSPACE_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800560 }
561
Dianne Hackbornbe707852011-11-11 14:32:10 -0800562 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700563 private final WeakReference<ActivityRecord> weakActivity;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800564
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800565 Token(ActivityRecord activity) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800566 weakActivity = new WeakReference<>(activity);
Wale Ogunwale7d701172015-03-11 15:36:30 -0700567 }
568
Andrii Kulian21713ac2016-10-12 22:05:05 -0700569 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700570 if (token == null) {
571 return null;
572 }
573 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700574 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700575 return null;
576 }
577 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800578 }
579
Craig Mautnerde4ef022013-04-07 19:01:33 -0700580 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800581 public String toString() {
582 StringBuilder sb = new StringBuilder(128);
583 sb.append("Token{");
584 sb.append(Integer.toHexString(System.identityHashCode(this)));
585 sb.append(' ');
586 sb.append(weakActivity.get());
587 sb.append('}');
588 return sb.toString();
589 }
590 }
591
Wale Ogunwale7d701172015-03-11 15:36:30 -0700592 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800593 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700594 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800595 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800596 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800597 return null;
598 }
599 }
600
Chong Zhang85ee6542015-10-02 13:36:38 -0700601 boolean isResolverActivity() {
602 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700603 }
604
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800605 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller, int _launchedFromPid,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800606 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700608 ActivityRecord _resultTo, String _resultWho, int _reqCode,
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700609 boolean _componentSpecified, boolean _rootVoiceInteraction,
610 ActivityStackSupervisor supervisor,
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700611 ActivityContainer container, ActivityOptions options, ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 service = _service;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800613 appToken = new Token(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 info = aInfo;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800615 launchedFromPid = _launchedFromPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800617 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700618 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 intent = _intent;
620 shortComponentName = _intent.getComponent().flattenToShortString();
621 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800622 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700623 rootVoiceInteraction = _rootVoiceInteraction;
Andrii Kulian1779e612016-10-12 21:58:25 -0700624 mLastReportedConfiguration = new Configuration(_configuration);
625 mLastReportedOverrideConfiguration = new Configuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 resultTo = _resultTo;
627 resultWho = _resultWho;
628 requestCode = _reqCode;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700629 state = ActivityState.INITIALIZING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 frontOfTask = false;
631 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700633 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800635 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 keysPaused = false;
637 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700638 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 idle = false;
641 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700642 mStackSupervisor = supervisor;
Craig Mautnere0a38842013-12-16 16:14:02 -0800643 mInitialActivityContainer = container;
Robert Carr0f5d7532016-10-17 16:39:17 -0700644
645 mRotationAnimationHint = aInfo.rotationAnimation;
646
Craig Mautner233ceee2014-05-09 17:05:11 -0700647 if (options != null) {
Chong Zhang280d3322015-11-03 17:27:26 -0800648 pendingOptions = options;
Craig Mautnerbb742462014-07-07 15:28:55 -0700649 mLaunchTaskBehind = pendingOptions.getLaunchTaskBehind();
Robert Carr0f5d7532016-10-17 16:39:17 -0700650
651 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
652 // Only override manifest supplied option if set.
653 if (rotationAnimation >= 0) {
654 mRotationAnimationHint = rotationAnimation;
655 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700656 PendingIntent usageReport = pendingOptions.getUsageTimeReport();
657 if (usageReport != null) {
658 appTimeTracker = new AppTimeTracker(usageReport);
659 }
Craig Mautner233ceee2014-05-09 17:05:11 -0700660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800662 // This starts out true, since the initial state of an activity is that we have everything,
663 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700664 haveState = true;
665
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800666 // If the class name in the intent doesn't match that of the target, this is
667 // probably an alias. We have to create a new ComponentName object to keep track
668 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
669 if (aInfo.targetActivity == null
670 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
671 && (aInfo.launchMode == LAUNCH_MULTIPLE
672 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
673 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800675 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800677 taskAffinity = aInfo.taskAffinity;
678 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
679 appInfo = aInfo.applicationInfo;
680 nonLocalizedLabel = aInfo.nonLocalizedLabel;
681 labelRes = aInfo.labelRes;
682 if (nonLocalizedLabel == null && labelRes == 0) {
683 ApplicationInfo app = aInfo.applicationInfo;
684 nonLocalizedLabel = app.nonLocalizedLabel;
685 labelRes = app.labelRes;
686 }
687 icon = aInfo.getIconResource();
688 logo = aInfo.getLogoResource();
689 theme = aInfo.getThemeResource();
690 realTheme = theme;
691 if (realTheme == 0) {
692 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
693 ? android.R.style.Theme : android.R.style.Theme_Holo;
694 }
695 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
696 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
697 }
698 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
699 && (aInfo.applicationInfo.uid == SYSTEM_UID
700 || aInfo.applicationInfo.uid == _caller.info.uid)) {
701 processName = _caller.processName;
702 } else {
703 processName = aInfo.processName;
704 }
705
706 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
707 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
708 }
709
710 packageName = aInfo.applicationInfo.packageName;
711 launchMode = aInfo.launchMode;
712
713 AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
714 realTheme, com.android.internal.R.styleable.Window, userId);
715 final boolean translucent = ent != null && (ent.array.getBoolean(
716 com.android.internal.R.styleable.Window_windowIsTranslucent, false)
717 || (!ent.array.hasValue(
718 com.android.internal.R.styleable.Window_windowIsTranslucent)
719 && ent.array.getBoolean(
720 com.android.internal.R.styleable.Window_windowSwipeToDismiss,
721 false)));
722 fullscreen = ent != null && !ent.array.getBoolean(
723 com.android.internal.R.styleable.Window_windowIsFloating, false) && !translucent;
724 noDisplay = ent != null && ent.array.getBoolean(
725 com.android.internal.R.styleable.Window_windowNoDisplay, false);
726
Winson Chung83471632016-12-13 11:02:12 -0800727 setActivityType(_componentSpecified, _launchedFromUid, _intent, options, sourceRecord);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800728
729 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
730
731 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
732 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
733 }
734
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800735 AppWindowContainerController getWindowContainerController() {
736 return mWindowContainerController;
737 }
738
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800739 void createWindowContainer() {
740 if (mWindowContainerController != null) {
741 throw new IllegalArgumentException("Window container=" + mWindowContainerController
742 + " already created for r=" + this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800743 }
744
745 inHistory = true;
746
747 task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800748 final TaskWindowContainerController taskController = task.getWindowContainerController();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800749
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800750 mWindowContainerController = new AppWindowContainerController(taskController, appToken,
751 this, Integer.MAX_VALUE /* add on top */, info.screenOrientation, fullscreen,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800752 (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
753 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
754 appInfo.targetSdkVersion, mRotationAnimationHint,
755 ActivityManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L);
756
757 task.addActivityToTop(this);
758
759 onOverrideConfigurationSent();
760 }
761
762 void removeWindowContainer() {
Wale Ogunwalecc367f42017-02-01 08:12:14 -0800763 // Resume key dispatching if it is currently paused before we remove the container.
764 resumeKeyDispatchingLocked();
765
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800766 mWindowContainerController.removeContainer(getDisplayId());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800767 mWindowContainerController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
Winson Chung30480042017-01-26 10:55:34 -0800770 /**
771 * Reparents this activity into {@param newTask} at the provided {@param position}. The caller
772 * should ensure that the {@param newTask} is not already the parent of this activity.
773 */
774 void reparent(TaskRecord newTask, int position, String reason) {
775 final TaskRecord prevTask = task;
776 if (prevTask == newTask) {
777 throw new IllegalArgumentException(reason + ": task=" + newTask
778 + " is already the parent of r=" + this);
779 }
780
781 // Must reparent first in window manager
782 mWindowContainerController.reparent(newTask.getWindowContainerController(), position);
783
784 // Remove the activity from the old task and add it to the new task
785 prevTask.removeActivity(this);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -0800786 // TODO(b/34179495): This should really be set to null in removeActivity() call above,
787 // but really bad things that I can't track down right now happen when I do that.
788 // So, setting it here now and will change later when there is time for investigation.
789 task = null;
Winson Chung30480042017-01-26 10:55:34 -0800790 newTask.addActivityAtIndex(position, this);
791 }
792
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700793 private boolean isHomeIntent(Intent intent) {
794 return Intent.ACTION_MAIN.equals(intent.getAction())
795 && intent.hasCategory(Intent.CATEGORY_HOME)
796 && intent.getCategories().size() == 1
797 && intent.getData() == null
798 && intent.getType() == null;
799 }
800
Chong Zhangad24f962016-08-25 12:12:33 -0700801 static boolean isMainIntent(Intent intent) {
802 return Intent.ACTION_MAIN.equals(intent.getAction())
803 && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
804 && intent.getCategories().size() == 1
805 && intent.getData() == null
806 && intent.getType() == null;
807 }
808
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700809 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
810 if (uid == Process.myUid() || uid == 0) {
811 // System process can launch home activity.
812 return true;
813 }
814 // Resolver activity can launch home activity.
815 return sourceRecord != null && sourceRecord.isResolverActivity();
816 }
817
Winson Chung83471632016-12-13 11:02:12 -0800818 /**
819 * @return whether the given package name can launch an assist activity.
820 */
821 private boolean canLaunchAssistActivity(String packageName) {
822 if (service.mAssistUtils == null) {
823 return false;
824 }
825
826 final ComponentName assistComponent = service.mAssistUtils.getActiveServiceComponentName();
827 if (assistComponent != null) {
828 return assistComponent.getPackageName().equals(packageName);
829 }
830 return false;
831 }
832
833 private void setActivityType(boolean componentSpecified, int launchedFromUid, Intent intent,
834 ActivityOptions options, ActivityRecord sourceRecord) {
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700835 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
836 && isHomeIntent(intent) && !isResolverActivity()) {
837 // This sure looks like a home activity!
838 mActivityType = HOME_ACTIVITY_TYPE;
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700839
840 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
841 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
842 // We only allow home activities to be resizeable if they explicitly requested it.
843 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
844 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700845 } else if (realActivity.getClassName().contains(RECENTS_PACKAGE_NAME)) {
846 mActivityType = RECENTS_ACTIVITY_TYPE;
Winson Chung83471632016-12-13 11:02:12 -0800847 } else if (options != null && options.getLaunchStackId() == ASSISTANT_STACK_ID
848 && canLaunchAssistActivity(launchedFromPackage)) {
849 mActivityType = ASSISTANT_ACTIVITY_TYPE;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700850 } else {
851 mActivityType = APPLICATION_ACTIVITY_TYPE;
852 }
853 }
854
Craig Mautnera228ae92014-07-09 05:44:55 -0700855 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -0800856 if (launchMode != LAUNCH_SINGLE_INSTANCE && launchMode != LAUNCH_SINGLE_TASK) {
Craig Mautnera228ae92014-07-09 05:44:55 -0700857 task.setTaskToAffiliateWith(taskToAffiliateWith);
858 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700859 }
860
Andrii Kulian02b7a832016-10-06 23:11:56 -0700861 /**
862 * @return Stack value from current task, null if there is no task.
863 */
864 ActivityStack getStack() {
865 return task != null ? task.getStack() : null;
866 }
867
Craig Mautner5eda9b32013-07-02 11:58:16 -0700868 boolean changeWindowTranslucency(boolean toOpaque) {
869 if (fullscreen == toOpaque) {
870 return false;
871 }
Craig Mautner4addfc52013-06-25 08:05:45 -0700872
Craig Mautner5eda9b32013-07-02 11:58:16 -0700873 // Keep track of the number of fullscreen activities in this task.
874 task.numFullscreen += toOpaque ? +1 : -1;
875
876 fullscreen = toOpaque;
877 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -0700878 }
879
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700880 void takeFromHistory() {
881 if (inHistory) {
882 inHistory = false;
883 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800884 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700885 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700886 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700887 }
888 }
889
890 boolean isInHistory() {
891 return inHistory;
892 }
893
Wale Ogunwale7d701172015-03-11 15:36:30 -0700894 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700895 final ActivityStack stack = getStack();
896 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700897 }
898
Craig Mautner86d67a42013-05-14 10:34:38 -0700899 boolean isHomeActivity() {
900 return mActivityType == HOME_ACTIVITY_TYPE;
901 }
902
903 boolean isRecentsActivity() {
904 return mActivityType == RECENTS_ACTIVITY_TYPE;
905 }
906
Winson Chung83471632016-12-13 11:02:12 -0800907 boolean isAssistantActivity() {
908 return mActivityType == ASSISTANT_ACTIVITY_TYPE;
909 }
910
Craig Mautner86d67a42013-05-14 10:34:38 -0700911 boolean isApplicationActivity() {
912 return mActivityType == APPLICATION_ACTIVITY_TYPE;
913 }
914
Craig Mautner21d24a22014-04-23 11:45:37 -0700915 boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700916 return (info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY ||
917 info.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS) &&
918 (intent == null ||
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800919 (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -0700920 }
921
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800922 boolean isFocusable() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700923 return StackId.canReceiveKeys(task.getStackId()) || isAlwaysFocusable();
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800924 }
925
926 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -0800927 return ActivityInfo.isResizeableMode(info.resizeMode) || info.supportsPictureInPicture();
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800928 }
929
Winson Chungd3395382016-12-13 11:49:09 -0800930 /**
931 * @return whether this activity is non-resizeable or forced to be resizeable
932 */
933 boolean isNonResizableOrForcedResizable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700934 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -0700935 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -0700936 }
937
Winson Chunge6308042016-10-31 09:24:01 -0700938 /**
Winson Chungd3395382016-12-13 11:49:09 -0800939 * @return whether this activity supports PiP multi-window and can be put in the pinned stack.
Winson Chunge6308042016-10-31 09:24:01 -0700940 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800941 boolean supportsPictureInPicture() {
Winson Chungd3395382016-12-13 11:49:09 -0800942 return service.mSupportsPictureInPicture && !isHomeActivity()
943 && info.supportsPictureInPicture();
944 }
945
946 /**
947 * @return whether this activity supports split-screen multi-window and can be put in the docked
948 * stack.
949 */
950 boolean supportsSplitScreen() {
951 // An activity can not be docked even if it is considered resizeable because it only
952 // supports picture-in-picture mode but has a non-resizeable resizeMode
953 return service.mSupportsSplitScreenMultiWindow && supportsResizeableMultiWindow();
954 }
955
956 /**
957 * @return whether this activity supports freeform multi-window and can be put in the freeform
958 * stack.
959 */
960 boolean supportsFreeform() {
961 return service.mSupportsFreeformWindowManagement && supportsResizeableMultiWindow();
962 }
963
964 /**
965 * @return whether this activity supports non-PiP multi-window.
966 */
967 private boolean supportsResizeableMultiWindow() {
968 return service.mSupportsMultiWindow && !isHomeActivity()
969 && (ActivityInfo.isResizeableMode(info.resizeMode)
970 || service.mForceResizableActivities);
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800971 }
972
Winson Chunge6308042016-10-31 09:24:01 -0700973 /**
Winson Chungc2baac02017-01-11 13:34:47 -0800974 * @return whether this activity is currently allowed to enter PIP, throwing an exception if
Winson Chung4dabf232017-01-25 13:25:22 -0800975 * the activity is not currently visible and {@param noThrow} is not set.
Winson Chunge6308042016-10-31 09:24:01 -0700976 */
Winson Chung4dabf232017-01-25 13:25:22 -0800977 boolean checkEnterPictureInPictureState(String caller, boolean noThrow) {
Winson Chungbb348802017-01-30 12:01:45 -0800978 boolean isCurrentAppLocked = mStackSupervisor.getLockTaskModeState() != LOCK_TASK_MODE_NONE;
Winson Chungc2baac02017-01-11 13:34:47 -0800979 boolean isKeyguardLocked = service.isKeyguardLocked();
980 boolean hasPinnedStack = mStackSupervisor.getStack(PINNED_STACK_ID) != null;
Winson Chungbb348802017-01-30 12:01:45 -0800981 // Don't return early if !isNotLocked, since we want to throw an exception if the activity
982 // is in an incorrect state
983 boolean isNotLocked = !isKeyguardLocked && !isCurrentAppLocked;
Winson Chungc2baac02017-01-11 13:34:47 -0800984 switch (state) {
985 case RESUMED:
Winson Chungbb348802017-01-30 12:01:45 -0800986 // When visible, allow entering PiP if not on the lockscreen and if the task is not
987 // locked
988 return isNotLocked;
Winson Chungc2baac02017-01-11 13:34:47 -0800989 case PAUSING:
990 case PAUSED:
Winson Chungbb348802017-01-30 12:01:45 -0800991 // When pausing, then only allow enter PiP as in the resume state, and in addition,
992 // require that there is not an existing PiP activity and that the current system
993 // state supports entering PiP
994 return isNotLocked && !hasPinnedStack && supportsPictureInPictureWhilePausing
Winson Chung59fda9e2017-01-20 16:14:51 -0800995 && checkEnterPictureInPictureOnHideAppOpsState();
Winson Chungc2baac02017-01-11 13:34:47 -0800996 case STOPPING:
997 // When stopping in a valid state, then only allow enter PiP as in the pause state.
998 // Otherwise, fall through to throw an exception if the caller is trying to enter
999 // PiP in an invalid stopping state.
1000 if (supportsPictureInPictureWhilePausing) {
Winson Chungbb348802017-01-30 12:01:45 -08001001 return isNotLocked && !hasPinnedStack
Winson Chung59fda9e2017-01-20 16:14:51 -08001002 && checkEnterPictureInPictureOnHideAppOpsState();
Winson Chungc2baac02017-01-11 13:34:47 -08001003 }
1004 default:
Winson Chung4dabf232017-01-25 13:25:22 -08001005 if (noThrow) {
1006 return false;
1007 } else {
1008 throw new IllegalStateException(caller
1009 + ": Current activity is not visible (state=" + state.name() + ") "
1010 + "r=" + this);
1011 }
Winson Chungb5c41b72016-12-07 15:00:47 -08001012 }
Winson Chunge6308042016-10-31 09:24:01 -07001013 }
1014
Winson Chung59fda9e2017-01-20 16:14:51 -08001015 /**
1016 * @return Whether AppOps allows this package to enter picture-in-picture when it is hidden.
1017 */
1018 private boolean checkEnterPictureInPictureOnHideAppOpsState() {
1019 try {
1020 return service.getAppOpsService().checkOperation(OP_ENTER_PICTURE_IN_PICTURE_ON_HIDE,
1021 appInfo.uid, packageName) == MODE_ALLOWED;
1022 } catch (RemoteException e) {
1023 // Local call
1024 }
1025 return false;
1026 }
1027
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001028 boolean isAlwaysFocusable() {
1029 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001030 }
1031
Jorim Jaggife762342016-10-13 14:33:27 +02001032 /**
1033 * @return true if the activity contains windows that have
1034 * {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set
1035 */
1036 boolean hasShowWhenLockedWindows() {
1037 return service.mWindowManager.containsShowWhenLockedWindow(appToken);
1038 }
1039
1040 /**
1041 * @return true if the activity contains windows that have
1042 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
1043 */
1044 boolean hasDismissKeyguardWindows() {
1045 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
1046 }
1047
Wale Ogunwale7d701172015-03-11 15:36:30 -07001048 void makeFinishingLocked() {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001049 if (!finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001050 final ActivityStack stack = getStack();
1051 if (stack != null && this == stack.getVisibleBehindActivity()) {
Jose Lima34ff4922014-08-18 15:19:41 -07001052 // A finishing activity should not remain as visible in the background
1053 mStackSupervisor.requestVisibleBehindLocked(this, false);
1054 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001055 finishing = true;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001056 if (stopped) {
1057 clearOptionsLocked();
1058 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001059
1060 if (service != null) {
1061 service.mTaskChangeNotificationController.notifyTaskStackChanged();
1062 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001063 }
1064 }
1065
Dianne Hackborn7e269642010-08-25 19:50:20 -07001066 UriPermissionOwner getUriPermissionsLocked() {
1067 if (uriPermissions == null) {
1068 uriPermissions = new UriPermissionOwner(service, this);
1069 }
1070 return uriPermissions;
1071 }
1072
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001073 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 int requestCode, int resultCode,
1075 Intent resultData) {
1076 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -05001077 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001079 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 results.add(r);
1082 }
1083
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001084 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 int requestCode) {
1086 if (results != null) {
1087 for (int i=results.size()-1; i>=0; i--) {
1088 ActivityResult r = (ActivityResult)results.get(i);
1089 if (r.mFrom != from) continue;
1090 if (r.mResultWho == null) {
1091 if (resultWho != null) continue;
1092 } else {
1093 if (!r.mResultWho.equals(resultWho)) continue;
1094 }
1095 if (r.mRequestCode != requestCode) continue;
1096
1097 results.remove(i);
1098 }
1099 }
1100 }
1101
Andrii Kulian21713ac2016-10-12 22:05:05 -07001102 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001104 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106 newIntents.add(intent);
1107 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001108
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001109 /**
1110 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1111 * method will be called at the proper time.
1112 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001113 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001114 // The activity now gets access to the data associated with this Intent.
1115 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001116 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001117 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001118 boolean unsent = true;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001119 final ActivityStack stack = getStack();
Wale Ogunwale826c7062016-09-13 08:25:54 -07001120 final boolean isTopActivityInStack =
1121 stack != null && stack.topRunningActivityLocked() == this;
1122 final boolean isTopActivityWhileSleeping =
1123 service.isSleepingLocked() && isTopActivityInStack;
Wale Ogunwale826c7062016-09-13 08:25:54 -07001124
1125 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001126 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1127 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001128 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001129 if ((state == ActivityState.RESUMED || state == ActivityState.PAUSED
1130 || isTopActivityWhileSleeping) && app != null && app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001131 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001132 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1133 ar.add(rintent);
Wale Ogunwale826c7062016-09-13 08:25:54 -07001134 app.thread.scheduleNewIntent(
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001135 ar, appToken, state == ActivityState.PAUSED /* andPause */);
Craig Mautner86d67a42013-05-14 10:34:38 -07001136 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001137 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001138 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001139 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001140 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001141 }
1142 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001143 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001144 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001145 }
1146 }
1147
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001148 void updateOptionsLocked(ActivityOptions options) {
1149 if (options != null) {
1150 if (pendingOptions != null) {
1151 pendingOptions.abort();
1152 }
1153 pendingOptions = options;
1154 }
1155 }
1156
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001157 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001158 if (pendingOptions != null
1159 && pendingOptions.getAnimationType() != ActivityOptions.ANIM_SCENE_TRANSITION) {
Michael Jurka21385cd2012-05-03 10:57:31 -07001160 final int animationType = pendingOptions.getAnimationType();
1161 switch (animationType) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001162 case ActivityOptions.ANIM_CUSTOM:
1163 service.mWindowManager.overridePendingAppTransition(
1164 pendingOptions.getPackageName(),
1165 pendingOptions.getCustomEnterResId(),
Dianne Hackborn84375872012-06-01 19:03:50 -07001166 pendingOptions.getCustomExitResId(),
1167 pendingOptions.getOnAnimationStartListener());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001168 break;
Chet Haase10e23ab2015-02-11 15:08:38 -08001169 case ActivityOptions.ANIM_CLIP_REVEAL:
1170 service.mWindowManager.overridePendingAppTransitionClipReveal(
1171 pendingOptions.getStartX(), pendingOptions.getStartY(),
1172 pendingOptions.getWidth(), pendingOptions.getHeight());
1173 if (intent.getSourceBounds() == null) {
1174 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1175 pendingOptions.getStartY(),
1176 pendingOptions.getStartX()+pendingOptions.getWidth(),
1177 pendingOptions.getStartY()+pendingOptions.getHeight()));
1178 }
1179 break;
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001180 case ActivityOptions.ANIM_SCALE_UP:
1181 service.mWindowManager.overridePendingAppTransitionScaleUp(
1182 pendingOptions.getStartX(), pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001183 pendingOptions.getWidth(), pendingOptions.getHeight());
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001184 if (intent.getSourceBounds() == null) {
1185 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1186 pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001187 pendingOptions.getStartX()+pendingOptions.getWidth(),
1188 pendingOptions.getStartY()+pendingOptions.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001189 }
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001190 break;
Michael Jurka832cb222012-04-13 09:32:47 -07001191 case ActivityOptions.ANIM_THUMBNAIL_SCALE_UP:
1192 case ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN:
1193 boolean scaleUp = (animationType == ActivityOptions.ANIM_THUMBNAIL_SCALE_UP);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001194 service.mWindowManager.overridePendingAppTransitionThumb(
1195 pendingOptions.getThumbnail(),
1196 pendingOptions.getStartX(), pendingOptions.getStartY(),
Michael Jurka21385cd2012-05-03 10:57:31 -07001197 pendingOptions.getOnAnimationStartListener(),
Michael Jurka832cb222012-04-13 09:32:47 -07001198 scaleUp);
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001199 if (intent.getSourceBounds() == null) {
1200 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1201 pendingOptions.getStartY(),
1202 pendingOptions.getStartX()
1203 + pendingOptions.getThumbnail().getWidth(),
1204 pendingOptions.getStartY()
1205 + pendingOptions.getThumbnail().getHeight()));
1206 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001207 break;
Winson Chunga4ccb862014-08-22 15:26:27 -07001208 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP:
1209 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN:
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001210 final AppTransitionAnimationSpec[] specs = pendingOptions.getAnimSpecs();
1211 if (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN
1212 && specs != null) {
1213 service.mWindowManager.overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -07001214 specs, pendingOptions.getOnAnimationStartListener(),
1215 pendingOptions.getAnimationFinishedListener(), false);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001216 } else {
1217 service.mWindowManager.overridePendingAppTransitionAspectScaledThumb(
1218 pendingOptions.getThumbnail(),
1219 pendingOptions.getStartX(), pendingOptions.getStartY(),
1220 pendingOptions.getWidth(), pendingOptions.getHeight(),
1221 pendingOptions.getOnAnimationStartListener(),
1222 (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP));
1223 if (intent.getSourceBounds() == null) {
1224 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1225 pendingOptions.getStartY(),
1226 pendingOptions.getStartX() + pendingOptions.getWidth(),
1227 pendingOptions.getStartY() + pendingOptions.getHeight()));
1228 }
Winson Chunga4ccb862014-08-22 15:26:27 -07001229 }
1230 break;
Craig Mautner233ceee2014-05-09 17:05:11 -07001231 default:
1232 Slog.e(TAG, "applyOptionsLocked: Unknown animationType=" + animationType);
1233 break;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001234 }
1235 pendingOptions = null;
1236 }
1237 }
1238
Adam Powellcfbe9be2013-11-06 14:58:58 -08001239 ActivityOptions getOptionsForTargetActivityLocked() {
1240 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1241 }
1242
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001243 void clearOptionsLocked() {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001244 if (pendingOptions != null) {
1245 pendingOptions.abort();
1246 pendingOptions = null;
1247 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001248 }
1249
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001250 ActivityOptions takeOptionsLocked() {
1251 ActivityOptions opts = pendingOptions;
1252 pendingOptions = null;
1253 return opts;
1254 }
1255
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001257 if (uriPermissions != null) {
1258 uriPermissions.removeUriPermissionsLocked();
1259 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001260 }
1261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262
1263 void pauseKeyDispatchingLocked() {
1264 if (!keysPaused) {
1265 keysPaused = true;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001266 mWindowContainerController.pauseKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 }
1268 }
1269
1270 void resumeKeyDispatchingLocked() {
1271 if (keysPaused) {
1272 keysPaused = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001273 mWindowContainerController.resumeKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
1275 }
1276
Winson Chung740c3ac2014-11-12 16:14:38 -08001277 void updateThumbnailLocked(Bitmap newThumbnail, CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001278 if (newThumbnail != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001279 if (DEBUG_THUMBNAILS) Slog.i(TAG_THUMBNAILS,
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001280 "Setting thumbnail of " + this + " to " + newThumbnail);
Winsonc809cbb2015-11-02 12:06:15 -08001281 boolean thumbnailUpdated = task.setLastThumbnailLocked(newThumbnail);
Winson Chung096f36b2014-08-20 15:39:01 -07001282 if (thumbnailUpdated && isPersistable()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001283 service.notifyTaskPersisterLocked(task, false);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001284 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001285 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001286 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001287 }
1288
Andrii Kulian21713ac2016-10-12 22:05:05 -07001289 final Bitmap screenshotActivityLocked() {
1290 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivityLocked: " + this);
Jorim Jaggi02886a82016-12-06 09:10:06 -08001291
1292 if (ENABLE_TASK_SNAPSHOTS) {
1293 // No need to screenshot if snapshots are enabled.
1294 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS,
1295 "\tSnapshots are enabled, abort taking screenshot");
1296 return null;
1297 }
1298
Andrii Kulian21713ac2016-10-12 22:05:05 -07001299 if (noDisplay) {
1300 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
1301 return null;
1302 }
1303
1304 final ActivityStack stack = getStack();
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001305 if (stack.isHomeOrRecentsStack()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001306 // This is an optimization -- since we never show Home or Recents within Recents itself,
1307 // we can just go ahead and skip taking the screenshot if this is the home stack.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001308 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, stack.getStackId() == HOME_STACK_ID ?
1309 "\tHome stack" : "\tRecents stack");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001310 return null;
1311 }
1312
1313 int w = service.mThumbnailWidth;
1314 int h = service.mThumbnailHeight;
1315
1316 if (w <= 0) {
1317 Slog.e(TAG, "\tInvalid thumbnail dimensions: " + w + "x" + h);
1318 return null;
1319 }
1320
1321 if (stack.mStackId == DOCKED_STACK_ID && mStackSupervisor.mIsDockMinimized) {
1322 // When the docked stack is minimized its app windows are cropped significantly so any
1323 // screenshot taken will not display the apps contain. So, we avoid taking a screenshot
1324 // in that case.
1325 if (DEBUG_SCREENSHOTS) Slog.e(TAG, "\tIn minimized docked stack");
1326 return null;
1327 }
1328
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001329 float scale = 0;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001330 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
1331
1332 // When this flag is set, we currently take the fullscreen screenshot of the activity but
1333 // scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to use within
1334 // SystemUI while keeping memory usage low.
1335 if (TAKE_FULLSCREEN_SCREENSHOTS) {
1336 w = h = -1;
1337 scale = service.mFullscreenThumbnailScale;
1338 }
1339
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001340 return mWindowContainerController.screenshotApplications(getDisplayId(), w, h, scale);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001341 }
1342
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001343 void setVisibility(boolean visible) {
1344 mWindowContainerController.setVisibility(visible);
1345 }
1346
1347 // TODO: Look into merging with #setVisibility()
Andrii Kulian21713ac2016-10-12 22:05:05 -07001348 void setVisible(boolean newVisible) {
1349 visible = newVisible;
1350 if (!visible && mUpdateTaskThumbnailWhenHidden) {
1351 updateThumbnailLocked(screenshotActivityLocked(), null /* description */);
1352 mUpdateTaskThumbnailWhenHidden = false;
1353 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001354 mWindowContainerController.setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001355 final ArrayList<ActivityContainer> containers = mChildContainers;
1356 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
1357 final ActivityContainer container = containers.get(containerNdx);
1358 container.setVisible(visible);
1359 }
1360 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1361 }
1362
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001363 void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
1364 mWindowContainerController.notifyAppResumed(wasStopped, allowSavedSurface);
1365 }
1366
1367 void notifyUnknownVisibilityLaunched() {
1368 mWindowContainerController.notifyUnknownVisibilityLaunched();
1369 }
1370
Jorim Jaggi241ae102016-11-02 21:57:33 -07001371 /**
1372 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1373 * might have on the visibility
1374 *
1375 * @see {@link ActivityStack#checkKeyguardVisibility}
1376 */
1377 boolean shouldBeVisibleIgnoringKeyguard(boolean behindTranslucentActivity,
1378 boolean stackVisibleBehind, ActivityRecord visibleBehind,
1379 boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001380 if (!okToShowLocked()) {
1381 return false;
1382 }
1383
1384 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1385 // but must be drawn initially for the animation as though they were visible.
1386 final boolean activityVisibleBehind =
1387 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == this;
1388
1389 boolean isVisible =
1390 !behindFullscreenActivity || mLaunchTaskBehind || activityVisibleBehind;
1391
1392 if (service.mSupportsLeanbackOnly && isVisible && isRecentsActivity()) {
1393 // On devices that support leanback only (Android TV), Recents activity can only be
1394 // visible if the home stack is the focused stack or we are in split-screen mode.
1395 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1396 || mStackSupervisor.isFocusedStack(getStack());
1397 }
1398
1399 return isVisible;
1400 }
1401
1402 void makeVisibleIfNeeded(ActivityRecord starting) {
1403 // This activity is not currently visible, but is running. Tell it to become visible.
1404 if (state == ActivityState.RESUMED || this == starting) {
1405 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1406 "Not making visible, r=" + this + " state=" + state + " starting=" + starting);
1407 return;
1408 }
1409
1410 // If this activity is paused, tell it to now show its window.
1411 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1412 "Making visible and scheduling visibility: " + this);
1413 final ActivityStack stack = getStack();
1414 try {
1415 if (stack.mTranslucentActivityWaiting != null) {
1416 updateOptionsLocked(returningOptions);
1417 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1418 }
1419 setVisible(true);
1420 sleeping = false;
1421 app.pendingUiClean = true;
1422 app.thread.scheduleWindowVisibility(appToken, true /* showWindow */);
1423 // The activity may be waiting for stop, but that is no longer appropriate for it.
1424 mStackSupervisor.mStoppingActivities.remove(this);
1425 mStackSupervisor.mGoingToSleepActivities.remove(this);
1426 } catch (Exception e) {
1427 // Just skip on any failure; we'll make it visible when it next restarts.
1428 Slog.w(TAG, "Exception thrown making visibile: " + intent.getComponent(), e);
1429 }
1430 handleAlreadyVisible();
1431 }
1432
1433 boolean handleAlreadyVisible() {
1434 stopFreezingScreenLocked(false);
1435 try {
1436 if (returningOptions != null) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001437 app.thread.scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001438 }
1439 } catch(RemoteException e) {
1440 }
1441 return state == ActivityState.RESUMED;
1442 }
1443
1444 static void activityResumedLocked(IBinder token) {
1445 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1446 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1447 if (r != null) {
1448 r.icicle = null;
1449 r.haveState = false;
1450 }
1451 }
1452
1453 /**
1454 * Once we know that we have asked an application to put an activity in the resumed state
1455 * (either by launching it or explicitly telling it), this function updates the rest of our
1456 * state to match that fact.
1457 */
1458 void completeResumeLocked() {
1459 final boolean wasVisible = visible;
1460 visible = true;
1461 if (!wasVisible) {
1462 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1463 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1464 }
1465 idle = false;
1466 results = null;
1467 newIntents = null;
1468 stopped = false;
1469
1470 if (isHomeActivity()) {
1471 ProcessRecord app = task.mActivities.get(0).app;
1472 if (app != null && app != service.mHomeProcess) {
1473 service.mHomeProcess = app;
1474 }
1475 }
1476
1477 if (nowVisible) {
1478 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1479 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001480 }
1481
1482 // Schedule an idle timeout in case the app doesn't do it for us.
1483 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1484
1485 mStackSupervisor.reportResumedActivityLocked(this);
1486
1487 resumeKeyDispatchingLocked();
1488 final ActivityStack stack = getStack();
1489 stack.mNoAnimActivities.clear();
1490
1491 // Mark the point when the activity is resuming
1492 // TODO: To be more accurate, the mark should be before the onCreate,
1493 // not after the onResume. But for subsequent starts, onResume is fine.
1494 if (app != null) {
1495 cpuTimeAtResume = service.mProcessCpuTracker.getCpuTimeForPid(app.pid);
1496 } else {
1497 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1498 }
1499
1500 returningOptions = null;
1501
1502 if (stack.getVisibleBehindActivity() == this) {
1503 // When resuming an activity, require it to call requestVisibleBehind() again.
1504 stack.setVisibleBehindActivity(null /* ActivityRecord */);
1505 }
1506 mStackSupervisor.checkReadyForSleepLocked();
1507 }
1508
1509 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1510 CharSequence description) {
1511 final ActivityStack stack = getStack();
1512 if (state != ActivityState.STOPPING) {
1513 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
1514 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1515 return;
1516 }
1517 if (newPersistentState != null) {
1518 persistentState = newPersistentState;
1519 service.notifyTaskPersisterLocked(task, false);
1520 }
1521 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001522
Andrii Kulian21713ac2016-10-12 22:05:05 -07001523 if (newIcicle != null) {
1524 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1525 // the state.
1526 icicle = newIcicle;
1527 haveState = true;
1528 launchCount = 0;
1529 updateThumbnailLocked(null /* newThumbnail */, description);
1530 }
1531 if (!stopped) {
1532 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
1533 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1534 stopped = true;
1535 state = ActivityState.STOPPED;
1536
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001537 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001538
1539 if (stack.getVisibleBehindActivity() == this) {
1540 mStackSupervisor.requestVisibleBehindLocked(this, false /* visible */);
1541 }
1542 if (finishing) {
1543 clearOptionsLocked();
1544 } else {
1545 if (deferRelaunchUntilPaused) {
1546 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
1547 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1548 } else {
1549 mStackSupervisor.updatePreviousProcessLocked(this);
1550 }
1551 }
1552 }
1553 }
1554
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001555 void startLaunchTickingLocked() {
1556 if (ActivityManagerService.IS_USER_BUILD) {
1557 return;
1558 }
1559 if (launchTickTime == 0) {
1560 launchTickTime = SystemClock.uptimeMillis();
1561 continueLaunchTickingLocked();
1562 }
1563 }
1564
1565 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001566 if (launchTickTime == 0) {
1567 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001568 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001569
Andrii Kulian02b7a832016-10-06 23:11:56 -07001570 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001571 if (stack == null) {
1572 return false;
1573 }
1574
1575 Message msg = stack.mHandler.obtainMessage(ActivityStack.LAUNCH_TICK_MSG, this);
1576 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1577 stack.mHandler.sendMessageDelayed(msg, ActivityStack.LAUNCH_TICK);
1578 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001579 }
1580
1581 void finishLaunchTickingLocked() {
1582 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001583 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001584 if (stack != null) {
1585 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1586 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001587 }
1588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 // IApplicationToken
1590
1591 public boolean mayFreezeScreenLocked(ProcessRecord app) {
1592 // Only freeze the screen if this activity is currently attached to
1593 // an application, and that application is not blocked or unresponding.
1594 // In any other case, we can't count on getting the screen unfrozen,
1595 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -08001596 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
1600 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001601 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 }
1603 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 public void stopFreezingScreenLocked(boolean force) {
1606 if (force || frozenBeforeDestroy) {
1607 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001608 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 }
1610 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001611
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001612 public void reportFullyDrawnLocked() {
1613 final long curTime = SystemClock.uptimeMillis();
1614 if (displayStartTime != 0) {
1615 reportLaunchTimeLocked(curTime);
1616 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001617 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001618 if (fullyDrawnStartTime != 0 && stack != null) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001619 final long thisTime = curTime - fullyDrawnStartTime;
1620 final long totalTime = stack.mFullyDrawnStartTime != 0
1621 ? (curTime - stack.mFullyDrawnStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001622 if (SHOW_ACTIVITY_START_TIME) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001623 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001624 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME,
1625 userId, System.identityHashCode(this), shortComponentName,
1626 thisTime, totalTime);
1627 StringBuilder sb = service.mStringBuilder;
1628 sb.setLength(0);
1629 sb.append("Fully drawn ");
1630 sb.append(shortComponentName);
1631 sb.append(": ");
1632 TimeUtils.formatDuration(thisTime, sb);
1633 if (thisTime != totalTime) {
1634 sb.append(" (total ");
1635 TimeUtils.formatDuration(totalTime, sb);
1636 sb.append(")");
1637 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001638 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001639 }
1640 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001641 //service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001642 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001643 stack.mFullyDrawnStartTime = 0;
1644 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001645 fullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001646 }
1647
1648 private void reportLaunchTimeLocked(final long curTime) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001649 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001650 if (stack == null) {
1651 return;
1652 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001653 final long thisTime = curTime - displayStartTime;
1654 final long totalTime = stack.mLaunchStartTime != 0
1655 ? (curTime - stack.mLaunchStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001656 if (SHOW_ACTIVITY_START_TIME) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001657 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001658 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME,
1659 userId, System.identityHashCode(this), shortComponentName,
1660 thisTime, totalTime);
1661 StringBuilder sb = service.mStringBuilder;
1662 sb.setLength(0);
1663 sb.append("Displayed ");
1664 sb.append(shortComponentName);
1665 sb.append(": ");
1666 TimeUtils.formatDuration(thisTime, sb);
1667 if (thisTime != totalTime) {
1668 sb.append(" (total ");
1669 TimeUtils.formatDuration(totalTime, sb);
1670 sb.append(")");
1671 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001672 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001673 }
1674 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
1675 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001676 //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001677 }
1678 displayStartTime = 0;
1679 stack.mLaunchStartTime = 0;
1680 }
1681
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001682 @Override
1683 public void onWindowsDrawn() {
1684 synchronized (service) {
1685 mStackSupervisor.mActivityMetricsLogger.notifyWindowsDrawn();
1686 if (displayStartTime != 0) {
1687 reportLaunchTimeLocked(SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001689 mStackSupervisor.sendWaitingVisibleReportLocked(this);
1690 startTime = 0;
1691 finishLaunchTickingLocked();
1692 if (task != null) {
1693 task.hasBeenVisible = true;
1694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
1696 }
1697
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001698 @Override
1699 public void onWindowsVisible() {
1700 synchronized (service) {
1701 mStackSupervisor.reportActivityVisibleLocked(this);
1702 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
1703 if (!nowVisible) {
1704 nowVisible = true;
1705 lastVisibleTime = SystemClock.uptimeMillis();
1706 if (!idle) {
1707 // Instead of doing the full stop routine here, let's just hide any activities
1708 // we now can, and let them stop when the normal idle happens.
Winson Chung4dabf232017-01-25 13:25:22 -08001709 mStackSupervisor.processStoppingActivitiesLocked(null /* idleActivity */,
1710 false /* remove */, true /* processPausingActivities */);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001711 } else {
1712 // If this activity was already idle, then we now need to make sure we perform
1713 // the full stop of any activities that are waiting to do so. This is because
1714 // we won't do that while they are still waiting for this one to become visible.
1715 final int size = mStackSupervisor.mWaitingVisibleActivities.size();
1716 if (size > 0) {
1717 for (int i = 0; i < size; i++) {
1718 ActivityRecord r = mStackSupervisor.mWaitingVisibleActivities.get(i);
1719 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
1720 }
1721 mStackSupervisor.mWaitingVisibleActivities.clear();
1722 mStackSupervisor.scheduleIdleLocked();
1723 }
1724 }
1725 service.scheduleAppGcsLocked();
1726 }
1727 }
1728 }
1729
1730 @Override
1731 public void onWindowsGone() {
1732 synchronized (service) {
1733 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
1734 nowVisible = false;
1735 }
1736 }
1737
1738 @Override
1739 public boolean keyDispatchingTimedOut(String reason) {
1740 ActivityRecord anrActivity;
1741 ProcessRecord anrApp;
1742 synchronized (service) {
1743 anrActivity = getWaitingHistoryRecordLocked();
1744 anrApp = app;
1745 }
1746 return service.inputDispatchingTimedOut(anrApp, anrActivity, this, false, reason);
1747 }
1748
1749 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001750 // First find the real culprit... if this activity is waiting for
1751 // another activity to start or has stopped, then the key dispatching
1752 // timeout should not be caused by this.
Wale Ogunwale16a62792015-04-01 11:50:31 -07001753 if (mStackSupervisor.mWaitingVisibleActivities.contains(this) || stopped) {
Craig Mautner8f2adcb2014-04-07 22:21:33 +00001754 final ActivityStack stack = mStackSupervisor.getFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001755 // Try to use the one which is closest to top.
1756 ActivityRecord r = stack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001758 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001760 if (r != null) {
1761 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001764 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
1766
Chong Zhang87761972016-08-22 13:53:24 -07001767 /** Checks whether the activity should be shown for current user. */
1768 public boolean okToShowLocked() {
1769 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
1770 || (mStackSupervisor.isCurrentProfileLocked(userId)
1771 && !service.mUserController.isUserStoppingOrShuttingDownLocked(userId));
1772 }
1773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 /**
1775 * This method will return true if the activity is either visible, is becoming visible, is
1776 * currently pausing, or is resumed.
1777 */
1778 public boolean isInterestingToUserLocked() {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001779 return visible || nowVisible || state == ActivityState.PAUSING ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 state == ActivityState.RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001781 }
1782
Wale Ogunwale3e997362016-09-06 10:37:56 -07001783 void setSleeping(boolean _sleeping) {
1784 setSleeping(_sleeping, false);
1785 }
1786
1787 void setSleeping(boolean _sleeping, boolean force) {
1788 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001789 return;
1790 }
1791 if (app != null && app.thread != null) {
1792 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001793 app.thread.scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001794 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
1795 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001796 }
1797 sleeping = _sleeping;
1798 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001799 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001800 }
1801 }
1802 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08001803
Craig Mautnerd2328952013-03-05 12:46:26 -08001804 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001805 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08001806 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001807 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001808 }
1809 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001810 final int activityNdx = task.mActivities.indexOf(r);
1811 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001812 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001813 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001814 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08001815 }
1816
1817 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001818 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001819 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001820 }
1821
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001822 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001823 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1824 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001825 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08001826 }
1827 return null;
1828 }
1829
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001830 /**
1831 * @return display id to which this record is attached, -1 if not attached.
1832 */
1833 int getDisplayId() {
1834 final ActivityStack stack = getStack();
1835 if (stack == null) {
1836 return -1;
1837 }
1838 return stack.mDisplayId;
1839 }
1840
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001841 final boolean isDestroyable() {
1842 if (finishing || app == null || state == ActivityState.DESTROYING
1843 || state == ActivityState.DESTROYED) {
1844 // This would be redundant.
1845 return false;
1846 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001847 final ActivityStack stack = getStack();
1848 if (stack == null || this == stack.mResumedActivity || this == stack.mPausingActivity
1849 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001850 // We're not ready for this kind of thing.
1851 return false;
1852 }
1853 if (visible) {
1854 // The user would notice this!
1855 return false;
1856 }
1857 return true;
1858 }
1859
Winson Chung3bad5cc02014-08-19 17:44:32 -07001860 private static String createImageFilename(long createTime, int taskId) {
1861 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001862 TaskPersister.IMAGE_EXTENSION;
1863 }
1864
Craig Mautner648f69b2014-09-18 14:16:26 -07001865 void setTaskDescription(TaskDescription _taskDescription) {
1866 Bitmap icon;
1867 if (_taskDescription.getIconFilename() == null &&
1868 (icon = _taskDescription.getIcon()) != null) {
1869 final String iconFilename = createImageFilename(createTime, task.taskId);
Winson Chungc8408b82017-01-25 17:58:56 -08001870 final File iconFile = new File(TaskPersister.getUserImagesDir(task.userId),
1871 iconFilename);
Suprabh Shukla23593142015-11-03 17:31:15 -08001872 final String iconFilePath = iconFile.getAbsolutePath();
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001873 service.mRecentTasks.saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08001874 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07001875 }
1876 taskDescription = _taskDescription;
1877 }
1878
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001879 void setVoiceSessionLocked(IVoiceInteractionSession session) {
1880 voiceSession = session;
1881 pendingVoiceInteractionStart = false;
1882 }
1883
1884 void clearVoiceSessionLocked() {
1885 voiceSession = null;
1886 pendingVoiceInteractionStart = false;
1887 }
1888
Jorim Jaggi02886a82016-12-06 09:10:06 -08001889 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch) {
Jorim Jaggi70176432017-01-18 12:52:13 +01001890 if (mWindowContainerController == null) {
1891 return;
1892 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001893 final CompatibilityInfo compatInfo =
1894 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001895 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
1896 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
Jorim Jaggi02886a82016-12-06 09:10:06 -08001897 prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning());
Wale Ogunwale3b232392016-05-13 15:37:13 -07001898 if (shown) {
1899 mStartingWindowState = STARTING_WINDOW_SHOWN;
1900 }
1901 }
1902
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001903 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
1904 if (state == ActivityState.INITIALIZING
1905 && mStartingWindowState == STARTING_WINDOW_SHOWN
1906 && behindFullscreenActivity) {
1907 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
1908 mStartingWindowState = STARTING_WINDOW_REMOVED;
1909 mWindowContainerController.removeStartingWindow();
1910 }
1911 }
1912
1913 int getRequestedOrientation() {
1914 return mWindowContainerController.getOrientation();
1915 }
1916
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001917 void setRequestedOrientation(int requestedOrientation) {
1918 if (task != null && (!task.mFullscreen || !task.getStack().mFullscreen)) {
1919 // Fixed screen orientation isn't supported when activities aren't in full screen mode.
1920 return;
1921 }
1922
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001923 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001924 final Configuration displayConfig =
1925 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
1926
1927 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
1928 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001929 if (config != null) {
1930 frozenBeforeDestroy = true;
1931 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
1932 false /* deferResume */, displayId)) {
1933 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1934 }
1935 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001936 service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
1937 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001938 }
1939
Bryce Leea163b762017-01-24 11:05:01 -08001940 /**
1941 * Set the last reported global configuration to the client. Should be called whenever a new
1942 * global configuration is sent to the client for this activity.
1943 */
1944 void setLastReportedGlobalConfiguration(@NonNull Configuration config) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001945 mLastReportedConfiguration.setTo(config);
1946 }
1947
Bryce Leea163b762017-01-24 11:05:01 -08001948 /**
1949 * Set the last reported merged override configuration to the client. Should be called whenever
1950 * a new merged configuration is sent to the client for this activity.
1951 */
1952 void setLastReportedMergedOverrideConfiguration(@NonNull Configuration config) {
1953 mLastReportedOverrideConfiguration.setTo(config);
1954 }
1955
Andrii Kulian21713ac2016-10-12 22:05:05 -07001956 /** Call when override config was sent to the Window Manager to update internal records. */
1957 void onOverrideConfigurationSent() {
1958 mLastReportedOverrideConfiguration.setTo(task.getMergedOverrideConfiguration());
1959 }
1960
1961 /**
1962 * Make sure the given activity matches the current configuration. Returns false if the activity
1963 * had to be destroyed. Returns true if the configuration is the same, or the activity will
1964 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
1965 * correct configuration and all other bookkeeping is handled.
1966 */
1967 boolean ensureActivityConfigurationLocked(int globalChanges, boolean preserveWindow) {
1968 final ActivityStack stack = getStack();
1969 if (stack.mConfigWillChange) {
1970 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1971 "Skipping config check (will change): " + this);
1972 return true;
1973 }
1974
1975 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1976 "Ensuring correct configuration: " + this);
1977
1978 // Short circuit: if the two configurations are equal (the common case), then there is
1979 // nothing to do.
1980 final Configuration newGlobalConfig = service.getGlobalConfiguration();
1981 final Configuration newTaskMergedOverrideConfig = task.getMergedOverrideConfiguration();
1982 if (mLastReportedConfiguration.equals(newGlobalConfig)
1983 && mLastReportedOverrideConfiguration.equals(newTaskMergedOverrideConfig)
1984 && !forceNewConfig) {
1985 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1986 "Configuration unchanged in " + this);
1987 return true;
1988 }
1989
1990 // We don't worry about activities that are finishing.
1991 if (finishing) {
1992 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1993 "Configuration doesn't matter in finishing " + this);
1994 stopFreezingScreenLocked(false);
1995 return true;
1996 }
1997
1998 // Okay we now are going to make this activity have the new config.
1999 // But then we need to figure out how it needs to deal with that.
2000 mTmpGlobalConfig.setTo(mLastReportedConfiguration);
2001 mTmpTaskConfig.setTo(mLastReportedOverrideConfiguration);
2002 mLastReportedConfiguration.setTo(newGlobalConfig);
2003 mLastReportedOverrideConfiguration.setTo(newTaskMergedOverrideConfig);
2004
2005 int taskChanges = getTaskConfigurationChanges(this, newTaskMergedOverrideConfig,
2006 mTmpTaskConfig);
2007 final int changes = mTmpGlobalConfig.diff(newGlobalConfig) | taskChanges;
2008 if (changes == 0 && !forceNewConfig) {
2009 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2010 "Configuration no differences in " + this);
2011 // There are no significant differences, so we won't relaunch but should still deliver
2012 // the new configuration to the client process.
2013 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
2014 return true;
2015 }
2016
2017 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2018 "Configuration changes for " + this + " ; taskChanges="
2019 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
2020 + Configuration.configurationDiffToString(changes));
2021
2022 // If the activity isn't currently running, just leave the new configuration and it will
2023 // pick that up next time it starts.
2024 if (app == null || app.thread == null) {
2025 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2026 "Configuration doesn't matter not running " + this);
2027 stopFreezingScreenLocked(false);
2028 forceNewConfig = false;
2029 return true;
2030 }
2031
2032 // Figure out how to handle the changes between the configurations.
2033 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2034 "Checking to restart " + info.name + ": changed=0x"
2035 + Integer.toHexString(changes) + ", handles=0x"
2036 + Integer.toHexString(info.getRealConfigChanged())
2037 + ", newGlobalConfig=" + newGlobalConfig
2038 + ", newTaskMergedOverrideConfig=" + newTaskMergedOverrideConfig);
2039
Zak Cohen90e7116742017-01-29 12:59:23 -08002040 if (shouldRelaunchLocked(changes, newGlobalConfig, newTaskMergedOverrideConfig)
2041 || forceNewConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002042 // Aha, the activity isn't handling the change, so DIE DIE DIE.
2043 configChangeFlags |= changes;
2044 startFreezingScreenLocked(app, globalChanges);
2045 forceNewConfig = false;
2046 preserveWindow &= isResizeOnlyChange(changes);
2047 if (app == null || app.thread == null) {
2048 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2049 "Config is destroying non-running " + this);
2050 stack.destroyActivityLocked(this, true, "config");
2051 } else if (state == ActivityState.PAUSING) {
2052 // A little annoying: we are waiting for this activity to finish pausing. Let's not
2053 // do anything now, but just flag that it needs to be restarted when done pausing.
2054 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2055 "Config is skipping already pausing " + this);
2056 deferRelaunchUntilPaused = true;
2057 preserveWindowOnDeferredRelaunch = preserveWindow;
2058 return true;
2059 } else if (state == ActivityState.RESUMED) {
2060 // Try to optimize this case: the configuration is changing and we need to restart
2061 // the top, resumed activity. Instead of doing the normal handshaking, just say
2062 // "restart!".
2063 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2064 "Config is relaunching resumed " + this);
2065
2066 if (DEBUG_STATES && !visible) {
2067 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
2068 + " called by " + Debug.getCallers(4));
2069 }
2070
2071 relaunchActivityLocked(true /* andResume */, preserveWindow);
2072 } else {
2073 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2074 "Config is relaunching non-resumed " + this);
2075 relaunchActivityLocked(false /* andResume */, preserveWindow);
2076 }
2077
2078 // All done... tell the caller we weren't able to keep this activity around.
2079 return false;
2080 }
2081
2082 // Default case: the activity can handle this new configuration, so hand it over.
2083 // NOTE: We only forward the task override configuration as the system level configuration
2084 // changes is always sent to all processes when they happen so it can just use whatever
2085 // system level configuration it last got.
2086 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
2087 stopFreezingScreenLocked(false);
2088
2089 return true;
2090 }
2091
Zak Cohen90e7116742017-01-29 12:59:23 -08002092 /**
2093 * When assessing a configuration change, decide if the changes flags and the new configurations
2094 * should cause the Activity to relaunch.
2095 */
2096 private boolean shouldRelaunchLocked(int changes, Configuration newGlobalConfig,
2097 Configuration newTaskMergedOverrideConfig) {
2098 int configChanged = info.getRealConfigChanged();
2099
2100 // Override for apps targeting pre-O sdks
2101 // If a device is in VR mode, and we're transitioning into VR ui mode, add ignore ui mode
2102 // to the config change.
2103 // For O and later, apps will be required to add configChanges="uimode" to their manifest.
2104 if (appInfo.targetSdkVersion < O
2105 && requestedVrComponent != null
2106 && (isInVrUiMode(newGlobalConfig) || isInVrUiMode(newTaskMergedOverrideConfig))) {
2107 configChanged |= CONFIG_UI_MODE;
2108 }
2109
2110 return (changes&(~configChanged)) != 0;
2111 }
2112
Andrii Kulian21713ac2016-10-12 22:05:05 -07002113 private static int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
2114 Configuration oldTaskOverride) {
2115 // If we went from full-screen to non-full-screen, make sure to use the correct
2116 // configuration task diff, so the diff stays as small as possible.
2117 if (Configuration.EMPTY.equals(oldTaskOverride)
2118 && !Configuration.EMPTY.equals(taskConfig)) {
2119 oldTaskOverride = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
2120 }
2121
2122 // Conversely, do the same when going the other direction.
2123 if (Configuration.EMPTY.equals(taskConfig)
2124 && !Configuration.EMPTY.equals(oldTaskOverride)) {
2125 taskConfig = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
2126 }
2127
2128 // Determine what has changed. May be nothing, if this is a config that has come back from
2129 // the app after going idle. In that case we just want to leave the official config object
2130 // now in the activity and do nothing else.
2131 int taskChanges = oldTaskOverride.diff(taskConfig, true /* skipUndefined */);
2132 // We don't want to use size changes if they don't cross boundaries that are important to
2133 // the app.
2134 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
2135 final boolean crosses = record.crossesHorizontalSizeThreshold(
2136 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
2137 || record.crossesVerticalSizeThreshold(
2138 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
2139 if (!crosses) {
2140 taskChanges &= ~CONFIG_SCREEN_SIZE;
2141 }
2142 }
2143 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
2144 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
2145 final int newSmallest = taskConfig.smallestScreenWidthDp;
2146 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
2147 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
2148 }
2149 }
2150 return taskChanges;
2151 }
2152
2153 private static boolean isResizeOnlyChange(int change) {
2154 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2155 | CONFIG_SCREEN_LAYOUT)) == 0;
2156 }
2157
2158 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
2159 if (service.mSuppressResizeConfigChanges && preserveWindow) {
2160 configChangeFlags = 0;
2161 return;
2162 }
2163
2164 List<ResultInfo> pendingResults = null;
2165 List<ReferrerIntent> pendingNewIntents = null;
2166 if (andResume) {
2167 pendingResults = results;
2168 pendingNewIntents = newIntents;
2169 }
2170 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2171 "Relaunching: " + this + " with results=" + pendingResults
2172 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2173 + " preserveWindow=" + preserveWindow);
2174 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
2175 : EventLogTags.AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
2176 task.taskId, shortComponentName);
2177
2178 startFreezingScreenLocked(app, 0);
2179
2180 mStackSupervisor.removeChildActivityContainers(this);
2181
2182 try {
2183 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2184 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2185 + " callers=" + Debug.getCallers(6));
2186 forceNewConfig = false;
2187 mStackSupervisor.activityRelaunchingLocked(this);
2188 app.thread.scheduleRelaunchActivity(appToken, pendingResults, pendingNewIntents,
2189 configChangeFlags, !andResume,
2190 new Configuration(service.getGlobalConfiguration()),
2191 new Configuration(task.getMergedOverrideConfiguration()), preserveWindow);
2192 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
2193 // pass in 'andResume' if this activity is currently resumed, which implies we aren't
2194 // sleeping.
2195 } catch (RemoteException e) {
2196 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2197 }
2198
2199 if (andResume) {
2200 if (DEBUG_STATES) {
2201 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2202 }
2203 results = null;
2204 newIntents = null;
2205 service.showUnsupportedZoomDialogIfNeededLocked(this);
2206 service.showAskCompatModeDialogLocked(this);
2207 } else {
2208 service.mHandler.removeMessages(ActivityStack.PAUSE_TIMEOUT_MSG, this);
2209 state = ActivityState.PAUSED;
2210 // if the app is relaunched when it's stopped, and we're not resuming,
2211 // put it back into stopped state.
2212 if (stopped) {
Winson Chung4dabf232017-01-25 13:25:22 -08002213 getStack().addToStopping(this, true /* scheduleIdle */, false /* idleDelayed */);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002214 }
2215 }
2216
2217 configChangeFlags = 0;
2218 deferRelaunchUntilPaused = false;
2219 preserveWindowOnDeferredRelaunch = false;
2220 }
2221
Jorim Jaggi02886a82016-12-06 09:10:06 -08002222 boolean isProcessRunning() {
2223 ProcessRecord proc = app;
2224 if (proc == null) {
2225 proc = service.mProcessNames.get(processName, info.applicationInfo.uid);
2226 }
2227 return proc != null && proc.thread != null;
2228 }
2229
Craig Mautner21d24a22014-04-23 11:45:37 -07002230 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2231 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2232 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2233 if (launchedFromPackage != null) {
2234 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2235 }
2236 if (resolvedType != null) {
2237 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2238 }
2239 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2240 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002241
Craig Mautner21d24a22014-04-23 11:45:37 -07002242 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002243 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002244 }
2245
2246 out.startTag(null, TAG_INTENT);
2247 intent.saveToXml(out);
2248 out.endTag(null, TAG_INTENT);
2249
2250 if (isPersistable() && persistentState != null) {
2251 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2252 persistentState.saveToXml(out);
2253 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2254 }
2255 }
2256
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002257 static ActivityRecord restoreFromXml(XmlPullParser in,
2258 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002259 Intent intent = null;
2260 PersistableBundle persistentState = null;
2261 int launchedFromUid = 0;
2262 String launchedFromPackage = null;
2263 String resolvedType = null;
2264 boolean componentSpecified = false;
2265 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002266 long createTime = -1;
2267 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002268 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002269
2270 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2271 final String attrName = in.getAttributeName(attrNdx);
2272 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002273 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002274 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002275 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002276 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002277 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002278 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002279 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2280 launchedFromPackage = attrValue;
2281 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2282 resolvedType = attrValue;
2283 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002284 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002285 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002286 userId = Integer.parseInt(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07002287 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
2288 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002289 } else {
2290 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2291 }
2292 }
2293
2294 int event;
2295 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07002296 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002297 if (event == XmlPullParser.START_TAG) {
2298 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002299 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002300 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002301 if (TAG_INTENT.equals(name)) {
2302 intent = Intent.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002303 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002304 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002305 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2306 persistentState = PersistableBundle.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002307 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002308 "ActivityRecord: persistentState=" + persistentState);
2309 } else {
2310 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2311 XmlUtils.skipCurrentTag(in);
2312 }
2313 }
2314 }
2315
2316 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002317 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002318 }
2319
2320 final ActivityManagerService service = stackSupervisor.mService;
2321 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Jeff Hao1b012d32014-08-20 10:35:34 -07002322 userId);
Craig Mautnere0129b32014-05-25 16:41:09 -07002323 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002324 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2325 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002326 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002327 final ActivityRecord r = new ActivityRecord(service, null /* caller */,
2328 0 /* launchedFromPid */, launchedFromUid, launchedFromPackage, intent, resolvedType,
2329 aInfo, service.getConfiguration(), null /* resultTo */, null /* resultWho */,
2330 0 /* reqCode */, componentSpecified, false /* rootVoiceInteraction */,
2331 stackSupervisor, null /* container */, null /* options */, null /* sourceRecord */);
Craig Mautner21d24a22014-04-23 11:45:37 -07002332
2333 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002334 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002335 r.createTime = createTime;
2336
2337 return r;
2338 }
2339
2340 private static String activityTypeToString(int type) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002341 switch (type) {
2342 case APPLICATION_ACTIVITY_TYPE: return "APPLICATION_ACTIVITY_TYPE";
2343 case HOME_ACTIVITY_TYPE: return "HOME_ACTIVITY_TYPE";
2344 case RECENTS_ACTIVITY_TYPE: return "RECENTS_ACTIVITY_TYPE";
Winson Chung83471632016-12-13 11:02:12 -08002345 case ASSISTANT_ACTIVITY_TYPE: return "ASSISTANT_ACTIVITY_TYPE";
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002346 default: return Integer.toString(type);
2347 }
2348 }
2349
Zak Cohen90e7116742017-01-29 12:59:23 -08002350 private static boolean isInVrUiMode(Configuration config) {
2351 return (config.uiMode & Configuration.UI_MODE_TYPE_MASK) == UI_MODE_TYPE_VR_HEADSET;
2352 }
2353
Craig Mautnerf81b90872013-02-26 13:02:43 -08002354 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002356 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002357 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002358 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002359 }
2360 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002361 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002362 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002363 sb.append(" u");
2364 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002365 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002366 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002367 stringName = sb.toString();
2368 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
2370}