blob: 082b6b50fd49fc799c927c49e6ecbd50566c7b2a [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;
Matthew Ngae1ff4f2016-11-10 15:49:14 -080025import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwale22e25262016-02-01 10:32:02 -080026import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Winson Chung59fda9e2017-01-20 16:14:51 -080027import static android.app.AppOpsManager.MODE_ALLOWED;
28import static android.app.AppOpsManager.OP_ENTER_PICTURE_IN_PICTURE_ON_HIDE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080029import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070030import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
31import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
32import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
33import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Zak Cohen90e7116742017-01-29 12:59:23 -080034import static android.content.pm.ActivityInfo.CONFIG_UI_MODE;
Jorim Jaggi02886a82016-12-06 09:10:06 -080035import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080036import static android.content.pm.ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
37import static android.content.pm.ActivityInfo.FLAG_IMMERSIVE;
38import static android.content.pm.ActivityInfo.FLAG_MULTIPROCESS;
Chong Zhang87761972016-08-22 13:53:24 -070039import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080040import static android.content.pm.ActivityInfo.FLAG_STATE_NOT_NEEDED;
41import static android.content.pm.ActivityInfo.LAUNCH_MULTIPLE;
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -080042import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
43import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080044import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070045import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggicd13d332016-04-27 15:40:20 -070046import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwale72a73e32016-10-13 12:16:39 -070047import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070048import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Zak Cohen90e7116742017-01-29 12:59:23 -080049import static android.content.res.Configuration.UI_MODE_TYPE_VR_HEADSET;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080050import static android.os.Build.VERSION_CODES.HONEYCOMB;
Zak Cohen90e7116742017-01-29 12:59:23 -080051import static android.os.Build.VERSION_CODES.O;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080052import static android.os.Process.SYSTEM_UID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080053import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
Andrii Kulian21713ac2016-10-12 22:05:05 -070054import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
55import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SCREENSHOTS;
56import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080057import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070059import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
60import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
61import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080063import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
65import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070066import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080067import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
68import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Andrii Kulian21713ac2016-10-12 22:05:05 -070069import static com.android.server.am.ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS;
Wale Ogunwale18795a22014-12-03 11:38:33 -080070import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
71
Andrii Kulian21713ac2016-10-12 22:05:05 -070072import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -070073import android.app.ActivityManager.TaskDescription;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070074import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080075import android.app.PendingIntent;
Winson Chungc2baac02017-01-11 13:34:47 -080076import android.app.PictureInPictureArgs;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080077import android.app.ResultInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.content.ComponentName;
79import android.content.Intent;
80import android.content.pm.ActivityInfo;
81import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -070082import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.content.res.Configuration;
84import android.graphics.Bitmap;
Dianne Hackbornd367ca82012-05-07 15:49:39 -070085import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -070087import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -080088import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080090import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -070092import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.os.SystemClock;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080094import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070095import android.os.UserHandle;
Amith Yamasani0af6fa72016-01-17 15:36:19 -080096import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.util.EventLog;
98import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070099import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700100import android.util.TimeUtils;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700101import android.view.AppTransitionAnimationSpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.view.IApplicationToken;
Jorim Jaggife762342016-10-13 14:33:27 +0200103import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700104
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800105import com.android.internal.app.ResolverActivity;
106import com.android.internal.content.ReferrerIntent;
107import com.android.internal.util.XmlUtils;
108import com.android.server.AttributeCache;
109import com.android.server.am.ActivityStack.ActivityState;
110import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800111import com.android.server.wm.AppWindowContainerController;
112import com.android.server.wm.AppWindowContainerListener;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800113import com.android.server.wm.TaskWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
Jorim Jaggi02886a82016-12-06 09:10:06 -0800115import org.xmlpull.v1.XmlPullParser;
116import org.xmlpull.v1.XmlPullParserException;
117import org.xmlpull.v1.XmlSerializer;
118
Suprabh Shukla23593142015-11-03 17:31:15 -0800119import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -0700120import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import java.io.PrintWriter;
122import java.lang.ref.WeakReference;
123import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800124import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700126import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700127import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
129/**
130 * An entry in the history stack, representing an activity.
131 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800132final class ActivityRecord implements AppWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800133 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_AM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700134 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
135 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
136 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700137 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700138 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
139 private static final String TAG_THUMBNAILS = TAG + POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700140 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800141
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700142 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700143 private static final String RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700144
Craig Mautner21d24a22014-04-23 11:45:37 -0700145 private static final String ATTR_ID = "id";
146 private static final String TAG_INTENT = "intent";
147 private static final String ATTR_USERID = "user_id";
148 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
149 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700150 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700151 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
152 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700153 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 final ActivityManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800156 final IApplicationToken.Stub appToken; // window manager token
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800157 AppWindowContainerController mWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 final ActivityInfo info; // all about me
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700159 final ApplicationInfo appInfo; // information about activity's app
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800160 final int launchedFromPid; // always the pid who started the activity.
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700161 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800162 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700163 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final Intent intent; // the original intent that generated us
165 final ComponentName realActivity; // the intent component, or target of an alias.
166 final String shortComponentName; // the short component name of the intent
167 final String resolvedType; // as per original caller;
168 final String packageName; // the package implementing intent's component
169 final String processName; // process where this component wants to run
170 final String taskAffinity; // as per ActivityInfo.taskAffinity
171 final boolean stateNotNeeded; // As per ActivityInfo.flags
Craig Mautner4addfc52013-06-25 08:05:45 -0700172 boolean fullscreen; // covers the full screen?
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800173 final boolean noDisplay; // activity is not displayed?
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800174 private final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700175 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700176
177 static final int APPLICATION_ACTIVITY_TYPE = 0;
178 static final int HOME_ACTIVITY_TYPE = 1;
179 static final int RECENTS_ACTIVITY_TYPE = 2;
Winson Chung83471632016-12-13 11:02:12 -0800180 static final int ASSISTANT_ACTIVITY_TYPE = 3;
Craig Mautner2c1faed2013-07-23 12:56:02 -0700181 int mActivityType;
Craig Mautner86d67a42013-05-14 10:34:38 -0700182
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800183 private CharSequence nonLocalizedLabel; // the label information from the package mgr.
184 private int labelRes; // the label information from the package mgr.
185 private int icon; // resource identifier of activity's icon.
186 private int logo; // resource identifier of activity's logo.
187 private int theme; // resource identifier of activity's theme.
188 private int realTheme; // actual theme resource we will use, never 0.
189 private int windowFlags; // custom window flags for preview window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 TaskRecord task; // the task this is in.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800191 private long createTime = System.currentTimeMillis();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700192 long displayStartTime; // when we started launching this activity
193 long fullyDrawnStartTime; // when we started launching this activity
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800194 private long startTime; // last time this activity was started
Dianne Hackborn50685602011-12-01 12:23:37 -0800195 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700196 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700197 long pauseTime; // last time we started pausing the activity
198 long launchTickTime; // base time for launch tick messages
Andrii Kulian21713ac2016-10-12 22:05:05 -0700199 private Configuration mLastReportedConfiguration; // configuration activity was last running in
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700200 // Overridden configuration by the activity task
Andrii Kulian1779e612016-10-12 21:58:25 -0700201 // WARNING: Reference points to {@link TaskRecord#getMergedOverrideConfig}, so its internal
202 // state should never be altered directly.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700203 private Configuration mLastReportedOverrideConfiguration;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700204 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700205 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 final String resultWho; // additional identifier for use by resultTo.
207 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800208 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800210 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700211 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700212 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700213 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -0700215 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700216 ProcessRecord app; // if non-null, hosting application
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700217 ActivityState state; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700219 PersistableBundle persistentState; // last persistently saved activity state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 boolean frontOfTask; // is this the root activity of its task?
221 boolean launchFailed; // set if a launched failed, to abort on 2nd try
222 boolean haveState; // have we gotten the last activity state?
223 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700224 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800226 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
227 // completed
228 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 int configChangeFlags; // which config values have changed
230 boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 int launchMode; // the launch mode activity attribute.
232 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700233 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
234 // might hide this activity?
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800235 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 boolean nowVisible; // is this activity's window visible?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 boolean idle; // has the activity gone idle?
238 boolean hasBeenLaunched;// has this activity ever been launched?
239 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400240 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400241 boolean forceNewConfig; // force re-create with new config next time
Winson Chungc2baac02017-01-11 13:34:47 -0800242 boolean supportsPictureInPictureWhilePausing; // This flag is set by the system to indicate
243 // that the activity can enter picture in picture while pausing (ie. only when another
244 // task is brought to front or started)
245 PictureInPictureArgs pictureInPictureArgs = new PictureInPictureArgs(); // The PiP
Winson Chunga29eb982016-12-14 12:01:27 -0800246 // arguments used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800247 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800248 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800249 ComponentName requestedVrComponent; // the requested component for handling VR mode.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800250 ArrayList<ActivityContainer> mChildContainers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700252 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700253
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700254 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700255 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800256
257 static final int STARTING_WINDOW_NOT_SHOWN = 0;
258 static final int STARTING_WINDOW_SHOWN = 1;
259 static final int STARTING_WINDOW_REMOVED = 2;
260 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700261 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800262
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800263 boolean mUpdateTaskThumbnailWhenHidden;
Craig Mautnere0a38842013-12-16 16:14:02 -0800264 ActivityContainer mInitialActivityContainer;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700265
Craig Mautner21d24a22014-04-23 11:45:37 -0700266 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700267 boolean mLaunchTaskBehind; // this activity is actively being launched with
268 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700269
Filip Gruszczynski23493322015-07-29 17:02:59 -0700270 // These configurations are collected from application's resources based on size-sensitive
271 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
272 // and drawable-sw400dp will be added to both as 400.
273 private int[] mVerticalSizeConfigurations;
274 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700275 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700276
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800277 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
278 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
279
Robert Carrd2265122016-08-05 10:25:21 -0700280 // A hint to override the window specified rotation animation, or -1
281 // to use the window specified value. We use this so that
282 // we can select the right animation in the cases of starting
283 // windows, where the app hasn't had time to set a value
284 // on the window.
285 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700286
Andrii Kulian21713ac2016-10-12 22:05:05 -0700287 /**
288 * Temp configs used in {@link #ensureActivityConfigurationLocked(int, boolean)}
289 */
Winson Chungbdc646f2017-02-13 12:12:22 -0800290 private final Configuration mTmpConfig1 = new Configuration();
291 private final Configuration mTmpConfig2 = new Configuration();
Andrii Kulian21713ac2016-10-12 22:05:05 -0700292
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800293 private static String startingWindowStateToString(int state) {
294 switch (state) {
295 case STARTING_WINDOW_NOT_SHOWN:
296 return "STARTING_WINDOW_NOT_SHOWN";
297 case STARTING_WINDOW_SHOWN:
298 return "STARTING_WINDOW_SHOWN";
299 case STARTING_WINDOW_REMOVED:
300 return "STARTING_WINDOW_REMOVED";
301 default:
302 return "unknown state=" + state;
303 }
304 }
305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700307 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700308 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
309 pw.print(" processName="); pw.println(processName);
310 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700311 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800312 pw.print(" userId="); pw.println(userId);
313 pw.print(prefix); pw.print("app="); pw.println(app);
314 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700315 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
316 pw.print(" task="); pw.println(task);
317 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700318 pw.print(prefix); pw.print("realActivity=");
319 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700320 if (appInfo != null) {
321 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
322 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
323 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
324 }
325 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800326 if (appInfo.splitSourceDirs != null) {
327 pw.print(prefix); pw.print("splitDir=");
328 pw.println(Arrays.toString(appInfo.splitSourceDirs));
329 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800330 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700331 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
332 pw.print(" componentSpecified="); pw.print(componentSpecified);
Craig Mautner86d67a42013-05-14 10:34:38 -0700333 pw.print(" mActivityType="); pw.println(mActivityType);
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700334 if (rootVoiceInteraction) {
335 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
336 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800337 pw.print(prefix); pw.print("compat="); pw.print(compat);
338 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
339 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
340 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Andrii Kulian1779e612016-10-12 21:58:25 -0700341 pw.print(prefix); pw.print("mLastReportedConfiguration=");
342 pw.println(mLastReportedConfiguration);
343 pw.print(prefix); pw.print("mLastReportedOverrideConfiguration=");
344 pw.println(mLastReportedOverrideConfiguration);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700345 if (resultTo != null || resultWho != null) {
346 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
347 pw.print(" resultWho="); pw.print(resultWho);
348 pw.print(" resultCode="); pw.println(requestCode);
349 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700350 if (taskDescription != null) {
351 final String iconFilename = taskDescription.getIconFilename();
352 if (iconFilename != null || taskDescription.getLabel() != null ||
353 taskDescription.getPrimaryColor() != 0) {
354 pw.print(prefix); pw.print("taskDescription:");
355 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
356 pw.print(" label=\""); pw.print(taskDescription.getLabel());
357 pw.print("\"");
358 pw.print(" color=");
359 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
360 }
361 if (iconFilename == null && taskDescription.getIcon() != null) {
362 pw.print(prefix); pw.println("taskDescription contains Bitmap");
363 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700364 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700365 if (results != null) {
366 pw.print(prefix); pw.print("results="); pw.println(results);
367 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700368 if (pendingResults != null && pendingResults.size() > 0) {
369 pw.print(prefix); pw.println("Pending Results:");
370 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
371 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
372 pw.print(prefix); pw.print(" - ");
373 if (pir == null) {
374 pw.println("null");
375 } else {
376 pw.println(pir);
377 pir.dump(pw, prefix + " ");
378 }
379 }
380 }
381 if (newIntents != null && newIntents.size() > 0) {
382 pw.print(prefix); pw.println("Pending New Intents:");
383 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800384 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700385 pw.print(prefix); pw.print(" - ");
386 if (intent == null) {
387 pw.println("null");
388 } else {
389 pw.println(intent.toShortString(false, true, false, true));
390 }
391 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700392 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700393 if (pendingOptions != null) {
394 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
395 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700396 if (appTimeTracker != null) {
397 appTimeTracker.dumpWithHeader(pw, prefix, false);
398 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700399 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700400 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700401 }
402 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800403 pw.print(" launchCount="); pw.print(launchCount);
404 pw.print(" lastLaunchTime=");
405 if (lastLaunchTime == 0) pw.print("0");
406 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
407 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700408 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700409 pw.print(" icicle="); pw.println(icicle);
410 pw.print(prefix); pw.print("state="); pw.print(state);
411 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700412 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700413 pw.print(" finishing="); pw.println(finishing);
414 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
415 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700416 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800417 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800418 pw.print(" idle="); pw.print(idle);
419 pw.print(" mStartingWindowState=");
420 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800421 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
422 pw.print(" noDisplay="); pw.print(noDisplay);
423 pw.print(" immersive="); pw.print(immersive);
424 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800425 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400426 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700427 pw.print(prefix); pw.print("mActivityType=");
428 pw.println(activityTypeToString(mActivityType));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800429 if (requestedVrComponent != null) {
430 pw.print(prefix);
431 pw.print("requestedVrComponent=");
432 pw.println(requestedVrComponent);
433 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700434 if (displayStartTime != 0 || startTime != 0) {
435 pw.print(prefix); pw.print("displayStartTime=");
436 if (displayStartTime == 0) pw.print("0");
437 else TimeUtils.formatDuration(displayStartTime, now, pw);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700438 pw.print(" startTime=");
439 if (startTime == 0) pw.print("0");
440 else TimeUtils.formatDuration(startTime, now, pw);
441 pw.println();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700442 }
Craig Mautner8c14c152015-01-15 17:32:07 -0800443 final boolean waitingVisible = mStackSupervisor.mWaitingVisibleActivities.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800444 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700445 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800446 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700447 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700448 if (lastVisibleTime == 0) pw.print("0");
449 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
450 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700451 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800452 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
453 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700454 pw.print(" configChangeFlags=");
455 pw.println(Integer.toHexString(configChangeFlags));
456 }
457 if (connections != null) {
458 pw.print(prefix); pw.print("connections="); pw.println(connections);
459 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800460 if (info != null) {
461 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
Winson Chungd3395382016-12-13 11:49:09 -0800462 pw.println(prefix + "supportsPictureInPicture=" + info.supportsPictureInPicture());
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800463 }
Winson Chungc2baac02017-01-11 13:34:47 -0800464 pw.println(prefix + "supportsPictureInPictureWhilePausing: "
465 + supportsPictureInPictureWhilePausing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467
Andrii Kulian21713ac2016-10-12 22:05:05 -0700468 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700469 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
470 }
471
Andrii Kulian21713ac2016-10-12 22:05:05 -0700472 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700473 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
474 }
475
Andrii Kulian21713ac2016-10-12 22:05:05 -0700476 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700477 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
478 }
479
Filip Gruszczynski23493322015-07-29 17:02:59 -0700480 /**
481 * The purpose of this method is to decide whether the activity needs to be relaunched upon
482 * changing its size. In most cases the activities don't need to be relaunched, if the resize
483 * is small, all the activity content has to do is relayout itself within new bounds. There are
484 * cases however, where the activity's content would be completely changed in the new size and
485 * the full relaunch is required.
486 *
487 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
488 * required. These thresholds are collected from the application resource qualifiers. For
489 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
490 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
491 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
492 * of the threshold.
493 */
494 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
495 int secondDp) {
496 if (thresholds == null) {
497 return false;
498 }
499 for (int i = thresholds.length - 1; i >= 0; i--) {
500 final int threshold = thresholds[i];
501 if ((firstDp < threshold && secondDp >= threshold)
502 || (firstDp >= threshold && secondDp < threshold)) {
503 return true;
504 }
505 }
506 return false;
507 }
508
Andrii Kulian21713ac2016-10-12 22:05:05 -0700509 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700510 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700511 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
512 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700513 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700514 }
515
Andrii Kulian21713ac2016-10-12 22:05:05 -0700516 private void scheduleConfigurationChanged(Configuration config, boolean reportToActivity) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800517 if (app == null || app.thread == null) {
518 return;
519 }
520 try {
521 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + " " +
Adam Lesinskibad43fc2016-07-19 13:35:01 -0700522 "reportToActivity=" + reportToActivity + " and config: " + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700523
Andrii Kulian1779e612016-10-12 21:58:25 -0700524 app.thread.scheduleActivityConfigurationChanged(appToken, new Configuration(config),
525 reportToActivity);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800526 } catch (RemoteException e) {
527 // If process died, whatever.
528 }
529 }
530
Andrii Kulian933076d2016-03-29 17:04:42 -0700531 void scheduleMultiWindowModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700532 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800533 return;
534 }
535 try {
536 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Andrii Kulian933076d2016-03-29 17:04:42 -0700537 app.thread.scheduleMultiWindowModeChanged(appToken, !task.mFullscreen);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800538 } catch (Exception e) {
539 // If process died, I don't care.
540 }
541 }
542
Andrii Kulian933076d2016-03-29 17:04:42 -0700543 void schedulePictureInPictureModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700544 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800545 return;
546 }
547 try {
Andrii Kulian933076d2016-03-29 17:04:42 -0700548 app.thread.schedulePictureInPictureModeChanged(
Andrii Kulian02b7a832016-10-06 23:11:56 -0700549 appToken, task.getStackId() == PINNED_STACK_ID);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800550 } catch (Exception e) {
551 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800552 }
553 }
554
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800555 boolean isFreeform() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700556 return task != null && task.getStackId() == FREEFORM_WORKSPACE_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800557 }
558
Dianne Hackbornbe707852011-11-11 14:32:10 -0800559 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700560 private final WeakReference<ActivityRecord> weakActivity;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800561
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800562 Token(ActivityRecord activity) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800563 weakActivity = new WeakReference<>(activity);
Wale Ogunwale7d701172015-03-11 15:36:30 -0700564 }
565
Andrii Kulian21713ac2016-10-12 22:05:05 -0700566 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700567 if (token == null) {
568 return null;
569 }
570 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700571 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700572 return null;
573 }
574 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800575 }
576
Craig Mautnerde4ef022013-04-07 19:01:33 -0700577 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800578 public String toString() {
579 StringBuilder sb = new StringBuilder(128);
580 sb.append("Token{");
581 sb.append(Integer.toHexString(System.identityHashCode(this)));
582 sb.append(' ');
583 sb.append(weakActivity.get());
584 sb.append('}');
585 return sb.toString();
586 }
587 }
588
Wale Ogunwale7d701172015-03-11 15:36:30 -0700589 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800590 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700591 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800592 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800593 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800594 return null;
595 }
596 }
597
Chong Zhang85ee6542015-10-02 13:36:38 -0700598 boolean isResolverActivity() {
599 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700600 }
601
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800602 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller, int _launchedFromPid,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800603 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700605 ActivityRecord _resultTo, String _resultWho, int _reqCode,
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700606 boolean _componentSpecified, boolean _rootVoiceInteraction,
607 ActivityStackSupervisor supervisor,
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700608 ActivityContainer container, ActivityOptions options, ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 service = _service;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800610 appToken = new Token(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 info = aInfo;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800612 launchedFromPid = _launchedFromPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800614 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700615 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 intent = _intent;
617 shortComponentName = _intent.getComponent().flattenToShortString();
618 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800619 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700620 rootVoiceInteraction = _rootVoiceInteraction;
Andrii Kulian1779e612016-10-12 21:58:25 -0700621 mLastReportedConfiguration = new Configuration(_configuration);
622 mLastReportedOverrideConfiguration = new Configuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 resultTo = _resultTo;
624 resultWho = _resultWho;
625 requestCode = _reqCode;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700626 state = ActivityState.INITIALIZING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 frontOfTask = false;
628 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700630 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800632 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 keysPaused = false;
634 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700635 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 idle = false;
638 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700639 mStackSupervisor = supervisor;
Craig Mautnere0a38842013-12-16 16:14:02 -0800640 mInitialActivityContainer = container;
Robert Carr0f5d7532016-10-17 16:39:17 -0700641
642 mRotationAnimationHint = aInfo.rotationAnimation;
643
Craig Mautner233ceee2014-05-09 17:05:11 -0700644 if (options != null) {
Chong Zhang280d3322015-11-03 17:27:26 -0800645 pendingOptions = options;
Craig Mautnerbb742462014-07-07 15:28:55 -0700646 mLaunchTaskBehind = pendingOptions.getLaunchTaskBehind();
Robert Carr0f5d7532016-10-17 16:39:17 -0700647
648 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
649 // Only override manifest supplied option if set.
650 if (rotationAnimation >= 0) {
651 mRotationAnimationHint = rotationAnimation;
652 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700653 PendingIntent usageReport = pendingOptions.getUsageTimeReport();
654 if (usageReport != null) {
655 appTimeTracker = new AppTimeTracker(usageReport);
656 }
Craig Mautner233ceee2014-05-09 17:05:11 -0700657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800659 // This starts out true, since the initial state of an activity is that we have everything,
660 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700661 haveState = true;
662
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800663 // If the class name in the intent doesn't match that of the target, this is
664 // probably an alias. We have to create a new ComponentName object to keep track
665 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
666 if (aInfo.targetActivity == null
667 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
668 && (aInfo.launchMode == LAUNCH_MULTIPLE
669 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
670 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800672 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800674 taskAffinity = aInfo.taskAffinity;
675 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
676 appInfo = aInfo.applicationInfo;
677 nonLocalizedLabel = aInfo.nonLocalizedLabel;
678 labelRes = aInfo.labelRes;
679 if (nonLocalizedLabel == null && labelRes == 0) {
680 ApplicationInfo app = aInfo.applicationInfo;
681 nonLocalizedLabel = app.nonLocalizedLabel;
682 labelRes = app.labelRes;
683 }
684 icon = aInfo.getIconResource();
685 logo = aInfo.getLogoResource();
686 theme = aInfo.getThemeResource();
687 realTheme = theme;
688 if (realTheme == 0) {
689 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
690 ? android.R.style.Theme : android.R.style.Theme_Holo;
691 }
692 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
693 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
694 }
695 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
696 && (aInfo.applicationInfo.uid == SYSTEM_UID
697 || aInfo.applicationInfo.uid == _caller.info.uid)) {
698 processName = _caller.processName;
699 } else {
700 processName = aInfo.processName;
701 }
702
703 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
704 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
705 }
706
707 packageName = aInfo.applicationInfo.packageName;
708 launchMode = aInfo.launchMode;
709
710 AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
711 realTheme, com.android.internal.R.styleable.Window, userId);
712 final boolean translucent = ent != null && (ent.array.getBoolean(
713 com.android.internal.R.styleable.Window_windowIsTranslucent, false)
714 || (!ent.array.hasValue(
715 com.android.internal.R.styleable.Window_windowIsTranslucent)
716 && ent.array.getBoolean(
717 com.android.internal.R.styleable.Window_windowSwipeToDismiss,
718 false)));
719 fullscreen = ent != null && !ent.array.getBoolean(
720 com.android.internal.R.styleable.Window_windowIsFloating, false) && !translucent;
721 noDisplay = ent != null && ent.array.getBoolean(
722 com.android.internal.R.styleable.Window_windowNoDisplay, false);
723
Winson Chung83471632016-12-13 11:02:12 -0800724 setActivityType(_componentSpecified, _launchedFromUid, _intent, options, sourceRecord);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800725
726 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
727
728 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
729 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
730 }
731
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800732 AppWindowContainerController getWindowContainerController() {
733 return mWindowContainerController;
734 }
735
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800736 void createWindowContainer() {
737 if (mWindowContainerController != null) {
738 throw new IllegalArgumentException("Window container=" + mWindowContainerController
739 + " already created for r=" + this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800740 }
741
742 inHistory = true;
743
744 task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800745 final TaskWindowContainerController taskController = task.getWindowContainerController();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800746
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800747 mWindowContainerController = new AppWindowContainerController(taskController, appToken,
748 this, Integer.MAX_VALUE /* add on top */, info.screenOrientation, fullscreen,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800749 (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
750 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
751 appInfo.targetSdkVersion, mRotationAnimationHint,
752 ActivityManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L);
753
754 task.addActivityToTop(this);
755
756 onOverrideConfigurationSent();
757 }
758
759 void removeWindowContainer() {
Wale Ogunwalecc367f42017-02-01 08:12:14 -0800760 // Resume key dispatching if it is currently paused before we remove the container.
761 resumeKeyDispatchingLocked();
762
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800763 mWindowContainerController.removeContainer(getDisplayId());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800764 mWindowContainerController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 }
766
Winson Chung30480042017-01-26 10:55:34 -0800767 /**
768 * Reparents this activity into {@param newTask} at the provided {@param position}. The caller
769 * should ensure that the {@param newTask} is not already the parent of this activity.
770 */
771 void reparent(TaskRecord newTask, int position, String reason) {
772 final TaskRecord prevTask = task;
773 if (prevTask == newTask) {
774 throw new IllegalArgumentException(reason + ": task=" + newTask
775 + " is already the parent of r=" + this);
776 }
777
778 // Must reparent first in window manager
779 mWindowContainerController.reparent(newTask.getWindowContainerController(), position);
780
781 // Remove the activity from the old task and add it to the new task
782 prevTask.removeActivity(this);
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -0800783 // TODO(b/34179495): This should really be set to null in removeActivity() call above,
784 // but really bad things that I can't track down right now happen when I do that.
785 // So, setting it here now and will change later when there is time for investigation.
786 task = null;
Winson Chung30480042017-01-26 10:55:34 -0800787 newTask.addActivityAtIndex(position, this);
788 }
789
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700790 private boolean isHomeIntent(Intent intent) {
791 return Intent.ACTION_MAIN.equals(intent.getAction())
792 && intent.hasCategory(Intent.CATEGORY_HOME)
793 && intent.getCategories().size() == 1
794 && intent.getData() == null
795 && intent.getType() == null;
796 }
797
Chong Zhangad24f962016-08-25 12:12:33 -0700798 static boolean isMainIntent(Intent intent) {
799 return Intent.ACTION_MAIN.equals(intent.getAction())
800 && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
801 && intent.getCategories().size() == 1
802 && intent.getData() == null
803 && intent.getType() == null;
804 }
805
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700806 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
807 if (uid == Process.myUid() || uid == 0) {
808 // System process can launch home activity.
809 return true;
810 }
811 // Resolver activity can launch home activity.
812 return sourceRecord != null && sourceRecord.isResolverActivity();
813 }
814
Winson Chung83471632016-12-13 11:02:12 -0800815 /**
816 * @return whether the given package name can launch an assist activity.
817 */
818 private boolean canLaunchAssistActivity(String packageName) {
819 if (service.mAssistUtils == null) {
820 return false;
821 }
822
823 final ComponentName assistComponent = service.mAssistUtils.getActiveServiceComponentName();
824 if (assistComponent != null) {
825 return assistComponent.getPackageName().equals(packageName);
826 }
827 return false;
828 }
829
830 private void setActivityType(boolean componentSpecified, int launchedFromUid, Intent intent,
831 ActivityOptions options, ActivityRecord sourceRecord) {
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700832 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
833 && isHomeIntent(intent) && !isResolverActivity()) {
834 // This sure looks like a home activity!
835 mActivityType = HOME_ACTIVITY_TYPE;
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700836
837 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
838 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
839 // We only allow home activities to be resizeable if they explicitly requested it.
840 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
841 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700842 } else if (realActivity.getClassName().contains(RECENTS_PACKAGE_NAME)) {
843 mActivityType = RECENTS_ACTIVITY_TYPE;
Winson Chung83471632016-12-13 11:02:12 -0800844 } else if (options != null && options.getLaunchStackId() == ASSISTANT_STACK_ID
845 && canLaunchAssistActivity(launchedFromPackage)) {
846 mActivityType = ASSISTANT_ACTIVITY_TYPE;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700847 } else {
848 mActivityType = APPLICATION_ACTIVITY_TYPE;
849 }
850 }
851
Craig Mautnera228ae92014-07-09 05:44:55 -0700852 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -0800853 if (launchMode != LAUNCH_SINGLE_INSTANCE && launchMode != LAUNCH_SINGLE_TASK) {
Craig Mautnera228ae92014-07-09 05:44:55 -0700854 task.setTaskToAffiliateWith(taskToAffiliateWith);
855 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700856 }
857
Andrii Kulian02b7a832016-10-06 23:11:56 -0700858 /**
859 * @return Stack value from current task, null if there is no task.
860 */
861 ActivityStack getStack() {
862 return task != null ? task.getStack() : null;
863 }
864
Craig Mautner5eda9b32013-07-02 11:58:16 -0700865 boolean changeWindowTranslucency(boolean toOpaque) {
866 if (fullscreen == toOpaque) {
867 return false;
868 }
Craig Mautner4addfc52013-06-25 08:05:45 -0700869
Craig Mautner5eda9b32013-07-02 11:58:16 -0700870 // Keep track of the number of fullscreen activities in this task.
871 task.numFullscreen += toOpaque ? +1 : -1;
872
873 fullscreen = toOpaque;
874 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -0700875 }
876
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700877 void takeFromHistory() {
878 if (inHistory) {
879 inHistory = false;
880 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800881 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700882 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700883 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700884 }
885 }
886
887 boolean isInHistory() {
888 return inHistory;
889 }
890
Wale Ogunwale7d701172015-03-11 15:36:30 -0700891 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700892 final ActivityStack stack = getStack();
893 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700894 }
895
Craig Mautner86d67a42013-05-14 10:34:38 -0700896 boolean isHomeActivity() {
897 return mActivityType == HOME_ACTIVITY_TYPE;
898 }
899
900 boolean isRecentsActivity() {
901 return mActivityType == RECENTS_ACTIVITY_TYPE;
902 }
903
Winson Chung83471632016-12-13 11:02:12 -0800904 boolean isAssistantActivity() {
905 return mActivityType == ASSISTANT_ACTIVITY_TYPE;
906 }
907
Craig Mautner86d67a42013-05-14 10:34:38 -0700908 boolean isApplicationActivity() {
909 return mActivityType == APPLICATION_ACTIVITY_TYPE;
910 }
911
Craig Mautner21d24a22014-04-23 11:45:37 -0700912 boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700913 return (info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY ||
914 info.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS) &&
915 (intent == null ||
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800916 (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -0700917 }
918
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800919 boolean isFocusable() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700920 return StackId.canReceiveKeys(task.getStackId()) || isAlwaysFocusable();
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800921 }
922
923 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -0800924 return ActivityInfo.isResizeableMode(info.resizeMode) || info.supportsPictureInPicture();
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800925 }
926
Winson Chungd3395382016-12-13 11:49:09 -0800927 /**
928 * @return whether this activity is non-resizeable or forced to be resizeable
929 */
930 boolean isNonResizableOrForcedResizable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700931 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -0700932 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -0700933 }
934
Winson Chunge6308042016-10-31 09:24:01 -0700935 /**
Winson Chungd3395382016-12-13 11:49:09 -0800936 * @return whether this activity supports PiP multi-window and can be put in the pinned stack.
Winson Chunge6308042016-10-31 09:24:01 -0700937 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800938 boolean supportsPictureInPicture() {
Winson Chungd3395382016-12-13 11:49:09 -0800939 return service.mSupportsPictureInPicture && !isHomeActivity()
940 && info.supportsPictureInPicture();
941 }
942
943 /**
944 * @return whether this activity supports split-screen multi-window and can be put in the docked
945 * stack.
946 */
947 boolean supportsSplitScreen() {
948 // An activity can not be docked even if it is considered resizeable because it only
949 // supports picture-in-picture mode but has a non-resizeable resizeMode
950 return service.mSupportsSplitScreenMultiWindow && supportsResizeableMultiWindow();
951 }
952
953 /**
954 * @return whether this activity supports freeform multi-window and can be put in the freeform
955 * stack.
956 */
957 boolean supportsFreeform() {
958 return service.mSupportsFreeformWindowManagement && supportsResizeableMultiWindow();
959 }
960
961 /**
962 * @return whether this activity supports non-PiP multi-window.
963 */
964 private boolean supportsResizeableMultiWindow() {
965 return service.mSupportsMultiWindow && !isHomeActivity()
966 && (ActivityInfo.isResizeableMode(info.resizeMode)
967 || service.mForceResizableActivities);
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800968 }
969
Winson Chunge6308042016-10-31 09:24:01 -0700970 /**
Winson Chungc2baac02017-01-11 13:34:47 -0800971 * @return whether this activity is currently allowed to enter PIP, throwing an exception if
Winson Chung4dabf232017-01-25 13:25:22 -0800972 * the activity is not currently visible and {@param noThrow} is not set.
Winson Chunge6308042016-10-31 09:24:01 -0700973 */
Winson Chung4dabf232017-01-25 13:25:22 -0800974 boolean checkEnterPictureInPictureState(String caller, boolean noThrow) {
Winson Chungbb348802017-01-30 12:01:45 -0800975 boolean isCurrentAppLocked = mStackSupervisor.getLockTaskModeState() != LOCK_TASK_MODE_NONE;
Winson Chungc2baac02017-01-11 13:34:47 -0800976 boolean isKeyguardLocked = service.isKeyguardLocked();
977 boolean hasPinnedStack = mStackSupervisor.getStack(PINNED_STACK_ID) != null;
Winson Chungbb348802017-01-30 12:01:45 -0800978 // Don't return early if !isNotLocked, since we want to throw an exception if the activity
979 // is in an incorrect state
980 boolean isNotLocked = !isKeyguardLocked && !isCurrentAppLocked;
Winson Chungc2baac02017-01-11 13:34:47 -0800981 switch (state) {
982 case RESUMED:
Winson Chungbb348802017-01-30 12:01:45 -0800983 // When visible, allow entering PiP if not on the lockscreen and if the task is not
984 // locked
985 return isNotLocked;
Winson Chungc2baac02017-01-11 13:34:47 -0800986 case PAUSING:
987 case PAUSED:
Winson Chungbb348802017-01-30 12:01:45 -0800988 // When pausing, then only allow enter PiP as in the resume state, and in addition,
989 // require that there is not an existing PiP activity and that the current system
990 // state supports entering PiP
991 return isNotLocked && !hasPinnedStack && supportsPictureInPictureWhilePausing
Winson Chung59fda9e2017-01-20 16:14:51 -0800992 && checkEnterPictureInPictureOnHideAppOpsState();
Winson Chungc2baac02017-01-11 13:34:47 -0800993 case STOPPING:
994 // When stopping in a valid state, then only allow enter PiP as in the pause state.
995 // Otherwise, fall through to throw an exception if the caller is trying to enter
996 // PiP in an invalid stopping state.
997 if (supportsPictureInPictureWhilePausing) {
Winson Chungbb348802017-01-30 12:01:45 -0800998 return isNotLocked && !hasPinnedStack
Winson Chung59fda9e2017-01-20 16:14:51 -0800999 && checkEnterPictureInPictureOnHideAppOpsState();
Winson Chungc2baac02017-01-11 13:34:47 -08001000 }
1001 default:
Winson Chung4dabf232017-01-25 13:25:22 -08001002 if (noThrow) {
1003 return false;
1004 } else {
1005 throw new IllegalStateException(caller
1006 + ": Current activity is not visible (state=" + state.name() + ") "
1007 + "r=" + this);
1008 }
Winson Chungb5c41b72016-12-07 15:00:47 -08001009 }
Winson Chunge6308042016-10-31 09:24:01 -07001010 }
1011
Winson Chung59fda9e2017-01-20 16:14:51 -08001012 /**
1013 * @return Whether AppOps allows this package to enter picture-in-picture when it is hidden.
1014 */
1015 private boolean checkEnterPictureInPictureOnHideAppOpsState() {
1016 try {
1017 return service.getAppOpsService().checkOperation(OP_ENTER_PICTURE_IN_PICTURE_ON_HIDE,
1018 appInfo.uid, packageName) == MODE_ALLOWED;
1019 } catch (RemoteException e) {
1020 // Local call
1021 }
1022 return false;
1023 }
1024
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001025 boolean isAlwaysFocusable() {
1026 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001027 }
1028
Jorim Jaggife762342016-10-13 14:33:27 +02001029 /**
1030 * @return true if the activity contains windows that have
1031 * {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set
1032 */
1033 boolean hasShowWhenLockedWindows() {
1034 return service.mWindowManager.containsShowWhenLockedWindow(appToken);
1035 }
1036
1037 /**
1038 * @return true if the activity contains windows that have
1039 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
1040 */
1041 boolean hasDismissKeyguardWindows() {
1042 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
1043 }
1044
Wale Ogunwale7d701172015-03-11 15:36:30 -07001045 void makeFinishingLocked() {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001046 if (!finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001047 final ActivityStack stack = getStack();
1048 if (stack != null && this == stack.getVisibleBehindActivity()) {
Jose Lima34ff4922014-08-18 15:19:41 -07001049 // A finishing activity should not remain as visible in the background
1050 mStackSupervisor.requestVisibleBehindLocked(this, false);
1051 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001052 finishing = true;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001053 if (stopped) {
1054 clearOptionsLocked();
1055 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001056
1057 if (service != null) {
1058 service.mTaskChangeNotificationController.notifyTaskStackChanged();
1059 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001060 }
1061 }
1062
Dianne Hackborn7e269642010-08-25 19:50:20 -07001063 UriPermissionOwner getUriPermissionsLocked() {
1064 if (uriPermissions == null) {
1065 uriPermissions = new UriPermissionOwner(service, this);
1066 }
1067 return uriPermissions;
1068 }
1069
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001070 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 int requestCode, int resultCode,
1072 Intent resultData) {
1073 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -05001074 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001076 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
1078 results.add(r);
1079 }
1080
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001081 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 int requestCode) {
1083 if (results != null) {
1084 for (int i=results.size()-1; i>=0; i--) {
1085 ActivityResult r = (ActivityResult)results.get(i);
1086 if (r.mFrom != from) continue;
1087 if (r.mResultWho == null) {
1088 if (resultWho != null) continue;
1089 } else {
1090 if (!r.mResultWho.equals(resultWho)) continue;
1091 }
1092 if (r.mRequestCode != requestCode) continue;
1093
1094 results.remove(i);
1095 }
1096 }
1097 }
1098
Andrii Kulian21713ac2016-10-12 22:05:05 -07001099 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001101 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103 newIntents.add(intent);
1104 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001105
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001106 /**
1107 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1108 * method will be called at the proper time.
1109 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001110 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001111 // The activity now gets access to the data associated with this Intent.
1112 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001113 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001114 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001115 boolean unsent = true;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001116 final ActivityStack stack = getStack();
Wale Ogunwale826c7062016-09-13 08:25:54 -07001117 final boolean isTopActivityInStack =
1118 stack != null && stack.topRunningActivityLocked() == this;
1119 final boolean isTopActivityWhileSleeping =
1120 service.isSleepingLocked() && isTopActivityInStack;
Wale Ogunwale826c7062016-09-13 08:25:54 -07001121
1122 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001123 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1124 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001125 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001126 if ((state == ActivityState.RESUMED || state == ActivityState.PAUSED
1127 || isTopActivityWhileSleeping) && app != null && app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001128 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001129 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1130 ar.add(rintent);
Wale Ogunwale826c7062016-09-13 08:25:54 -07001131 app.thread.scheduleNewIntent(
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001132 ar, appToken, state == ActivityState.PAUSED /* andPause */);
Craig Mautner86d67a42013-05-14 10:34:38 -07001133 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001134 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001135 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001136 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001137 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 }
1139 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001140 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001141 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001142 }
1143 }
1144
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001145 void updateOptionsLocked(ActivityOptions options) {
1146 if (options != null) {
1147 if (pendingOptions != null) {
1148 pendingOptions.abort();
1149 }
1150 pendingOptions = options;
1151 }
1152 }
1153
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001154 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001155 if (pendingOptions != null
1156 && pendingOptions.getAnimationType() != ActivityOptions.ANIM_SCENE_TRANSITION) {
Michael Jurka21385cd2012-05-03 10:57:31 -07001157 final int animationType = pendingOptions.getAnimationType();
1158 switch (animationType) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001159 case ActivityOptions.ANIM_CUSTOM:
1160 service.mWindowManager.overridePendingAppTransition(
1161 pendingOptions.getPackageName(),
1162 pendingOptions.getCustomEnterResId(),
Dianne Hackborn84375872012-06-01 19:03:50 -07001163 pendingOptions.getCustomExitResId(),
1164 pendingOptions.getOnAnimationStartListener());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001165 break;
Chet Haase10e23ab2015-02-11 15:08:38 -08001166 case ActivityOptions.ANIM_CLIP_REVEAL:
1167 service.mWindowManager.overridePendingAppTransitionClipReveal(
1168 pendingOptions.getStartX(), pendingOptions.getStartY(),
1169 pendingOptions.getWidth(), pendingOptions.getHeight());
1170 if (intent.getSourceBounds() == null) {
1171 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1172 pendingOptions.getStartY(),
1173 pendingOptions.getStartX()+pendingOptions.getWidth(),
1174 pendingOptions.getStartY()+pendingOptions.getHeight()));
1175 }
1176 break;
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001177 case ActivityOptions.ANIM_SCALE_UP:
1178 service.mWindowManager.overridePendingAppTransitionScaleUp(
1179 pendingOptions.getStartX(), pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001180 pendingOptions.getWidth(), pendingOptions.getHeight());
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001181 if (intent.getSourceBounds() == null) {
1182 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1183 pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001184 pendingOptions.getStartX()+pendingOptions.getWidth(),
1185 pendingOptions.getStartY()+pendingOptions.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001186 }
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001187 break;
Michael Jurka832cb222012-04-13 09:32:47 -07001188 case ActivityOptions.ANIM_THUMBNAIL_SCALE_UP:
1189 case ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN:
1190 boolean scaleUp = (animationType == ActivityOptions.ANIM_THUMBNAIL_SCALE_UP);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001191 service.mWindowManager.overridePendingAppTransitionThumb(
1192 pendingOptions.getThumbnail(),
1193 pendingOptions.getStartX(), pendingOptions.getStartY(),
Michael Jurka21385cd2012-05-03 10:57:31 -07001194 pendingOptions.getOnAnimationStartListener(),
Michael Jurka832cb222012-04-13 09:32:47 -07001195 scaleUp);
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001196 if (intent.getSourceBounds() == null) {
1197 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1198 pendingOptions.getStartY(),
1199 pendingOptions.getStartX()
1200 + pendingOptions.getThumbnail().getWidth(),
1201 pendingOptions.getStartY()
1202 + pendingOptions.getThumbnail().getHeight()));
1203 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001204 break;
Winson Chunga4ccb862014-08-22 15:26:27 -07001205 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP:
1206 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN:
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001207 final AppTransitionAnimationSpec[] specs = pendingOptions.getAnimSpecs();
1208 if (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN
1209 && specs != null) {
1210 service.mWindowManager.overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -07001211 specs, pendingOptions.getOnAnimationStartListener(),
1212 pendingOptions.getAnimationFinishedListener(), false);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001213 } else {
1214 service.mWindowManager.overridePendingAppTransitionAspectScaledThumb(
1215 pendingOptions.getThumbnail(),
1216 pendingOptions.getStartX(), pendingOptions.getStartY(),
1217 pendingOptions.getWidth(), pendingOptions.getHeight(),
1218 pendingOptions.getOnAnimationStartListener(),
1219 (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP));
1220 if (intent.getSourceBounds() == null) {
1221 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1222 pendingOptions.getStartY(),
1223 pendingOptions.getStartX() + pendingOptions.getWidth(),
1224 pendingOptions.getStartY() + pendingOptions.getHeight()));
1225 }
Winson Chunga4ccb862014-08-22 15:26:27 -07001226 }
1227 break;
Craig Mautner233ceee2014-05-09 17:05:11 -07001228 default:
1229 Slog.e(TAG, "applyOptionsLocked: Unknown animationType=" + animationType);
1230 break;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001231 }
1232 pendingOptions = null;
1233 }
1234 }
1235
Adam Powellcfbe9be2013-11-06 14:58:58 -08001236 ActivityOptions getOptionsForTargetActivityLocked() {
1237 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1238 }
1239
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001240 void clearOptionsLocked() {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001241 if (pendingOptions != null) {
1242 pendingOptions.abort();
1243 pendingOptions = null;
1244 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001245 }
1246
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001247 ActivityOptions takeOptionsLocked() {
1248 ActivityOptions opts = pendingOptions;
1249 pendingOptions = null;
1250 return opts;
1251 }
1252
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001253 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001254 if (uriPermissions != null) {
1255 uriPermissions.removeUriPermissionsLocked();
1256 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001257 }
1258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259
1260 void pauseKeyDispatchingLocked() {
1261 if (!keysPaused) {
1262 keysPaused = true;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001263 mWindowContainerController.pauseKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265 }
1266
1267 void resumeKeyDispatchingLocked() {
1268 if (keysPaused) {
1269 keysPaused = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001270 mWindowContainerController.resumeKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272 }
1273
Winson Chung740c3ac2014-11-12 16:14:38 -08001274 void updateThumbnailLocked(Bitmap newThumbnail, CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001275 if (newThumbnail != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001276 if (DEBUG_THUMBNAILS) Slog.i(TAG_THUMBNAILS,
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001277 "Setting thumbnail of " + this + " to " + newThumbnail);
Winsonc809cbb2015-11-02 12:06:15 -08001278 boolean thumbnailUpdated = task.setLastThumbnailLocked(newThumbnail);
Winson Chung096f36b2014-08-20 15:39:01 -07001279 if (thumbnailUpdated && isPersistable()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001280 service.notifyTaskPersisterLocked(task, false);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001281 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001282 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001283 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001284 }
1285
Andrii Kulian21713ac2016-10-12 22:05:05 -07001286 final Bitmap screenshotActivityLocked() {
1287 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivityLocked: " + this);
Jorim Jaggi02886a82016-12-06 09:10:06 -08001288
1289 if (ENABLE_TASK_SNAPSHOTS) {
1290 // No need to screenshot if snapshots are enabled.
1291 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS,
1292 "\tSnapshots are enabled, abort taking screenshot");
1293 return null;
1294 }
1295
Andrii Kulian21713ac2016-10-12 22:05:05 -07001296 if (noDisplay) {
1297 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
1298 return null;
1299 }
1300
1301 final ActivityStack stack = getStack();
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001302 if (stack.isHomeOrRecentsStack()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001303 // This is an optimization -- since we never show Home or Recents within Recents itself,
1304 // we can just go ahead and skip taking the screenshot if this is the home stack.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001305 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, stack.getStackId() == HOME_STACK_ID ?
1306 "\tHome stack" : "\tRecents stack");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001307 return null;
1308 }
1309
1310 int w = service.mThumbnailWidth;
1311 int h = service.mThumbnailHeight;
1312
1313 if (w <= 0) {
1314 Slog.e(TAG, "\tInvalid thumbnail dimensions: " + w + "x" + h);
1315 return null;
1316 }
1317
1318 if (stack.mStackId == DOCKED_STACK_ID && mStackSupervisor.mIsDockMinimized) {
1319 // When the docked stack is minimized its app windows are cropped significantly so any
1320 // screenshot taken will not display the apps contain. So, we avoid taking a screenshot
1321 // in that case.
1322 if (DEBUG_SCREENSHOTS) Slog.e(TAG, "\tIn minimized docked stack");
1323 return null;
1324 }
1325
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001326 float scale = 0;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001327 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
1328
1329 // When this flag is set, we currently take the fullscreen screenshot of the activity but
1330 // scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to use within
1331 // SystemUI while keeping memory usage low.
1332 if (TAKE_FULLSCREEN_SCREENSHOTS) {
1333 w = h = -1;
1334 scale = service.mFullscreenThumbnailScale;
1335 }
1336
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001337 return mWindowContainerController.screenshotApplications(getDisplayId(), w, h, scale);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001338 }
1339
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001340 void setVisibility(boolean visible) {
1341 mWindowContainerController.setVisibility(visible);
1342 }
1343
1344 // TODO: Look into merging with #setVisibility()
Andrii Kulian21713ac2016-10-12 22:05:05 -07001345 void setVisible(boolean newVisible) {
1346 visible = newVisible;
1347 if (!visible && mUpdateTaskThumbnailWhenHidden) {
1348 updateThumbnailLocked(screenshotActivityLocked(), null /* description */);
1349 mUpdateTaskThumbnailWhenHidden = false;
1350 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001351 mWindowContainerController.setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001352 final ArrayList<ActivityContainer> containers = mChildContainers;
1353 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
1354 final ActivityContainer container = containers.get(containerNdx);
1355 container.setVisible(visible);
1356 }
1357 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1358 }
1359
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001360 void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
1361 mWindowContainerController.notifyAppResumed(wasStopped, allowSavedSurface);
1362 }
1363
1364 void notifyUnknownVisibilityLaunched() {
1365 mWindowContainerController.notifyUnknownVisibilityLaunched();
1366 }
1367
Jorim Jaggi241ae102016-11-02 21:57:33 -07001368 /**
1369 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1370 * might have on the visibility
1371 *
1372 * @see {@link ActivityStack#checkKeyguardVisibility}
1373 */
1374 boolean shouldBeVisibleIgnoringKeyguard(boolean behindTranslucentActivity,
1375 boolean stackVisibleBehind, ActivityRecord visibleBehind,
1376 boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001377 if (!okToShowLocked()) {
1378 return false;
1379 }
1380
1381 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1382 // but must be drawn initially for the animation as though they were visible.
1383 final boolean activityVisibleBehind =
1384 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == this;
1385
1386 boolean isVisible =
1387 !behindFullscreenActivity || mLaunchTaskBehind || activityVisibleBehind;
1388
1389 if (service.mSupportsLeanbackOnly && isVisible && isRecentsActivity()) {
1390 // On devices that support leanback only (Android TV), Recents activity can only be
1391 // visible if the home stack is the focused stack or we are in split-screen mode.
1392 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1393 || mStackSupervisor.isFocusedStack(getStack());
1394 }
1395
1396 return isVisible;
1397 }
1398
1399 void makeVisibleIfNeeded(ActivityRecord starting) {
1400 // This activity is not currently visible, but is running. Tell it to become visible.
1401 if (state == ActivityState.RESUMED || this == starting) {
1402 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1403 "Not making visible, r=" + this + " state=" + state + " starting=" + starting);
1404 return;
1405 }
1406
1407 // If this activity is paused, tell it to now show its window.
1408 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1409 "Making visible and scheduling visibility: " + this);
1410 final ActivityStack stack = getStack();
1411 try {
1412 if (stack.mTranslucentActivityWaiting != null) {
1413 updateOptionsLocked(returningOptions);
1414 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1415 }
1416 setVisible(true);
1417 sleeping = false;
1418 app.pendingUiClean = true;
1419 app.thread.scheduleWindowVisibility(appToken, true /* showWindow */);
1420 // The activity may be waiting for stop, but that is no longer appropriate for it.
1421 mStackSupervisor.mStoppingActivities.remove(this);
1422 mStackSupervisor.mGoingToSleepActivities.remove(this);
1423 } catch (Exception e) {
1424 // Just skip on any failure; we'll make it visible when it next restarts.
1425 Slog.w(TAG, "Exception thrown making visibile: " + intent.getComponent(), e);
1426 }
1427 handleAlreadyVisible();
1428 }
1429
1430 boolean handleAlreadyVisible() {
1431 stopFreezingScreenLocked(false);
1432 try {
1433 if (returningOptions != null) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001434 app.thread.scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001435 }
1436 } catch(RemoteException e) {
1437 }
1438 return state == ActivityState.RESUMED;
1439 }
1440
1441 static void activityResumedLocked(IBinder token) {
1442 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1443 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1444 if (r != null) {
1445 r.icicle = null;
1446 r.haveState = false;
1447 }
1448 }
1449
1450 /**
1451 * Once we know that we have asked an application to put an activity in the resumed state
1452 * (either by launching it or explicitly telling it), this function updates the rest of our
1453 * state to match that fact.
1454 */
1455 void completeResumeLocked() {
1456 final boolean wasVisible = visible;
1457 visible = true;
1458 if (!wasVisible) {
1459 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1460 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1461 }
1462 idle = false;
1463 results = null;
1464 newIntents = null;
1465 stopped = false;
1466
1467 if (isHomeActivity()) {
1468 ProcessRecord app = task.mActivities.get(0).app;
1469 if (app != null && app != service.mHomeProcess) {
1470 service.mHomeProcess = app;
1471 }
1472 }
1473
1474 if (nowVisible) {
1475 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1476 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001477 }
1478
1479 // Schedule an idle timeout in case the app doesn't do it for us.
1480 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1481
1482 mStackSupervisor.reportResumedActivityLocked(this);
1483
1484 resumeKeyDispatchingLocked();
1485 final ActivityStack stack = getStack();
1486 stack.mNoAnimActivities.clear();
1487
1488 // Mark the point when the activity is resuming
1489 // TODO: To be more accurate, the mark should be before the onCreate,
1490 // not after the onResume. But for subsequent starts, onResume is fine.
1491 if (app != null) {
1492 cpuTimeAtResume = service.mProcessCpuTracker.getCpuTimeForPid(app.pid);
1493 } else {
1494 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1495 }
1496
1497 returningOptions = null;
1498
1499 if (stack.getVisibleBehindActivity() == this) {
1500 // When resuming an activity, require it to call requestVisibleBehind() again.
1501 stack.setVisibleBehindActivity(null /* ActivityRecord */);
1502 }
1503 mStackSupervisor.checkReadyForSleepLocked();
1504 }
1505
1506 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1507 CharSequence description) {
1508 final ActivityStack stack = getStack();
1509 if (state != ActivityState.STOPPING) {
1510 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
1511 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1512 return;
1513 }
1514 if (newPersistentState != null) {
1515 persistentState = newPersistentState;
1516 service.notifyTaskPersisterLocked(task, false);
1517 }
1518 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001519
Andrii Kulian21713ac2016-10-12 22:05:05 -07001520 if (newIcicle != null) {
1521 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1522 // the state.
1523 icicle = newIcicle;
1524 haveState = true;
1525 launchCount = 0;
1526 updateThumbnailLocked(null /* newThumbnail */, description);
1527 }
1528 if (!stopped) {
1529 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
1530 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1531 stopped = true;
1532 state = ActivityState.STOPPED;
1533
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001534 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001535
1536 if (stack.getVisibleBehindActivity() == this) {
1537 mStackSupervisor.requestVisibleBehindLocked(this, false /* visible */);
1538 }
1539 if (finishing) {
1540 clearOptionsLocked();
1541 } else {
1542 if (deferRelaunchUntilPaused) {
1543 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
1544 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1545 } else {
1546 mStackSupervisor.updatePreviousProcessLocked(this);
1547 }
1548 }
1549 }
1550 }
1551
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001552 void startLaunchTickingLocked() {
1553 if (ActivityManagerService.IS_USER_BUILD) {
1554 return;
1555 }
1556 if (launchTickTime == 0) {
1557 launchTickTime = SystemClock.uptimeMillis();
1558 continueLaunchTickingLocked();
1559 }
1560 }
1561
1562 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001563 if (launchTickTime == 0) {
1564 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001565 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001566
Andrii Kulian02b7a832016-10-06 23:11:56 -07001567 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001568 if (stack == null) {
1569 return false;
1570 }
1571
1572 Message msg = stack.mHandler.obtainMessage(ActivityStack.LAUNCH_TICK_MSG, this);
1573 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1574 stack.mHandler.sendMessageDelayed(msg, ActivityStack.LAUNCH_TICK);
1575 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001576 }
1577
1578 void finishLaunchTickingLocked() {
1579 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001580 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001581 if (stack != null) {
1582 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1583 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001584 }
1585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 // IApplicationToken
1587
1588 public boolean mayFreezeScreenLocked(ProcessRecord app) {
1589 // Only freeze the screen if this activity is currently attached to
1590 // an application, and that application is not blocked or unresponding.
1591 // In any other case, we can't count on getting the screen unfrozen,
1592 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -08001593 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
1597 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001598 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 }
1600 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 public void stopFreezingScreenLocked(boolean force) {
1603 if (force || frozenBeforeDestroy) {
1604 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001605 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
1607 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001608
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001609 public void reportFullyDrawnLocked() {
1610 final long curTime = SystemClock.uptimeMillis();
1611 if (displayStartTime != 0) {
1612 reportLaunchTimeLocked(curTime);
1613 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001614 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001615 if (fullyDrawnStartTime != 0 && stack != null) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001616 final long thisTime = curTime - fullyDrawnStartTime;
1617 final long totalTime = stack.mFullyDrawnStartTime != 0
1618 ? (curTime - stack.mFullyDrawnStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001619 if (SHOW_ACTIVITY_START_TIME) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001620 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001621 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME,
1622 userId, System.identityHashCode(this), shortComponentName,
1623 thisTime, totalTime);
1624 StringBuilder sb = service.mStringBuilder;
1625 sb.setLength(0);
1626 sb.append("Fully drawn ");
1627 sb.append(shortComponentName);
1628 sb.append(": ");
1629 TimeUtils.formatDuration(thisTime, sb);
1630 if (thisTime != totalTime) {
1631 sb.append(" (total ");
1632 TimeUtils.formatDuration(totalTime, sb);
1633 sb.append(")");
1634 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001635 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001636 }
1637 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001638 //service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001639 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001640 stack.mFullyDrawnStartTime = 0;
1641 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001642 fullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001643 }
1644
1645 private void reportLaunchTimeLocked(final long curTime) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001646 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001647 if (stack == null) {
1648 return;
1649 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001650 final long thisTime = curTime - displayStartTime;
1651 final long totalTime = stack.mLaunchStartTime != 0
1652 ? (curTime - stack.mLaunchStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001653 if (SHOW_ACTIVITY_START_TIME) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001654 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001655 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME,
1656 userId, System.identityHashCode(this), shortComponentName,
1657 thisTime, totalTime);
1658 StringBuilder sb = service.mStringBuilder;
1659 sb.setLength(0);
1660 sb.append("Displayed ");
1661 sb.append(shortComponentName);
1662 sb.append(": ");
1663 TimeUtils.formatDuration(thisTime, sb);
1664 if (thisTime != totalTime) {
1665 sb.append(" (total ");
1666 TimeUtils.formatDuration(totalTime, sb);
1667 sb.append(")");
1668 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001669 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001670 }
1671 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
1672 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001673 //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001674 }
1675 displayStartTime = 0;
1676 stack.mLaunchStartTime = 0;
1677 }
1678
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001679 @Override
1680 public void onWindowsDrawn() {
1681 synchronized (service) {
1682 mStackSupervisor.mActivityMetricsLogger.notifyWindowsDrawn();
1683 if (displayStartTime != 0) {
1684 reportLaunchTimeLocked(SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001686 mStackSupervisor.sendWaitingVisibleReportLocked(this);
1687 startTime = 0;
1688 finishLaunchTickingLocked();
1689 if (task != null) {
1690 task.hasBeenVisible = true;
1691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
1693 }
1694
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001695 @Override
1696 public void onWindowsVisible() {
1697 synchronized (service) {
1698 mStackSupervisor.reportActivityVisibleLocked(this);
1699 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
1700 if (!nowVisible) {
1701 nowVisible = true;
1702 lastVisibleTime = SystemClock.uptimeMillis();
1703 if (!idle) {
1704 // Instead of doing the full stop routine here, let's just hide any activities
1705 // we now can, and let them stop when the normal idle happens.
Winson Chung4dabf232017-01-25 13:25:22 -08001706 mStackSupervisor.processStoppingActivitiesLocked(null /* idleActivity */,
1707 false /* remove */, true /* processPausingActivities */);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001708 } else {
1709 // If this activity was already idle, then we now need to make sure we perform
1710 // the full stop of any activities that are waiting to do so. This is because
1711 // we won't do that while they are still waiting for this one to become visible.
1712 final int size = mStackSupervisor.mWaitingVisibleActivities.size();
1713 if (size > 0) {
1714 for (int i = 0; i < size; i++) {
1715 ActivityRecord r = mStackSupervisor.mWaitingVisibleActivities.get(i);
1716 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
1717 }
1718 mStackSupervisor.mWaitingVisibleActivities.clear();
1719 mStackSupervisor.scheduleIdleLocked();
1720 }
1721 }
1722 service.scheduleAppGcsLocked();
1723 }
1724 }
1725 }
1726
1727 @Override
1728 public void onWindowsGone() {
1729 synchronized (service) {
1730 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
1731 nowVisible = false;
1732 }
1733 }
1734
1735 @Override
1736 public boolean keyDispatchingTimedOut(String reason) {
1737 ActivityRecord anrActivity;
1738 ProcessRecord anrApp;
1739 synchronized (service) {
1740 anrActivity = getWaitingHistoryRecordLocked();
1741 anrApp = app;
1742 }
1743 return service.inputDispatchingTimedOut(anrApp, anrActivity, this, false, reason);
1744 }
1745
1746 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001747 // First find the real culprit... if this activity is waiting for
1748 // another activity to start or has stopped, then the key dispatching
1749 // timeout should not be caused by this.
Wale Ogunwale16a62792015-04-01 11:50:31 -07001750 if (mStackSupervisor.mWaitingVisibleActivities.contains(this) || stopped) {
Craig Mautner8f2adcb2014-04-07 22:21:33 +00001751 final ActivityStack stack = mStackSupervisor.getFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001752 // Try to use the one which is closest to top.
1753 ActivityRecord r = stack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001755 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001757 if (r != null) {
1758 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001761 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763
Chong Zhang87761972016-08-22 13:53:24 -07001764 /** Checks whether the activity should be shown for current user. */
1765 public boolean okToShowLocked() {
1766 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
1767 || (mStackSupervisor.isCurrentProfileLocked(userId)
1768 && !service.mUserController.isUserStoppingOrShuttingDownLocked(userId));
1769 }
1770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 /**
1772 * This method will return true if the activity is either visible, is becoming visible, is
1773 * currently pausing, or is resumed.
1774 */
1775 public boolean isInterestingToUserLocked() {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001776 return visible || nowVisible || state == ActivityState.PAUSING ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 state == ActivityState.RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001778 }
1779
Wale Ogunwale3e997362016-09-06 10:37:56 -07001780 void setSleeping(boolean _sleeping) {
1781 setSleeping(_sleeping, false);
1782 }
1783
1784 void setSleeping(boolean _sleeping, boolean force) {
1785 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001786 return;
1787 }
1788 if (app != null && app.thread != null) {
1789 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001790 app.thread.scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001791 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
1792 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001793 }
1794 sleeping = _sleeping;
1795 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001796 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001797 }
1798 }
1799 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08001800
Craig Mautnerd2328952013-03-05 12:46:26 -08001801 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001802 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08001803 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001804 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001805 }
1806 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001807 final int activityNdx = task.mActivities.indexOf(r);
1808 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001809 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001810 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001811 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08001812 }
1813
1814 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001815 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001816 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001817 }
1818
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001819 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001820 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1821 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001822 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08001823 }
1824 return null;
1825 }
1826
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001827 /**
1828 * @return display id to which this record is attached, -1 if not attached.
1829 */
1830 int getDisplayId() {
1831 final ActivityStack stack = getStack();
1832 if (stack == null) {
1833 return -1;
1834 }
1835 return stack.mDisplayId;
1836 }
1837
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001838 final boolean isDestroyable() {
1839 if (finishing || app == null || state == ActivityState.DESTROYING
1840 || state == ActivityState.DESTROYED) {
1841 // This would be redundant.
1842 return false;
1843 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001844 final ActivityStack stack = getStack();
1845 if (stack == null || this == stack.mResumedActivity || this == stack.mPausingActivity
1846 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001847 // We're not ready for this kind of thing.
1848 return false;
1849 }
1850 if (visible) {
1851 // The user would notice this!
1852 return false;
1853 }
1854 return true;
1855 }
1856
Winson Chung3bad5cc02014-08-19 17:44:32 -07001857 private static String createImageFilename(long createTime, int taskId) {
1858 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001859 TaskPersister.IMAGE_EXTENSION;
1860 }
1861
Craig Mautner648f69b2014-09-18 14:16:26 -07001862 void setTaskDescription(TaskDescription _taskDescription) {
1863 Bitmap icon;
1864 if (_taskDescription.getIconFilename() == null &&
1865 (icon = _taskDescription.getIcon()) != null) {
1866 final String iconFilename = createImageFilename(createTime, task.taskId);
Winson Chungc8408b82017-01-25 17:58:56 -08001867 final File iconFile = new File(TaskPersister.getUserImagesDir(task.userId),
1868 iconFilename);
Suprabh Shukla23593142015-11-03 17:31:15 -08001869 final String iconFilePath = iconFile.getAbsolutePath();
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001870 service.mRecentTasks.saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08001871 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07001872 }
1873 taskDescription = _taskDescription;
1874 }
1875
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001876 void setVoiceSessionLocked(IVoiceInteractionSession session) {
1877 voiceSession = session;
1878 pendingVoiceInteractionStart = false;
1879 }
1880
1881 void clearVoiceSessionLocked() {
1882 voiceSession = null;
1883 pendingVoiceInteractionStart = false;
1884 }
1885
Jorim Jaggi02886a82016-12-06 09:10:06 -08001886 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch) {
Jorim Jaggi70176432017-01-18 12:52:13 +01001887 if (mWindowContainerController == null) {
1888 return;
1889 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001890 final CompatibilityInfo compatInfo =
1891 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001892 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
1893 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
Jorim Jaggi02886a82016-12-06 09:10:06 -08001894 prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning());
Wale Ogunwale3b232392016-05-13 15:37:13 -07001895 if (shown) {
1896 mStartingWindowState = STARTING_WINDOW_SHOWN;
1897 }
1898 }
1899
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001900 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
1901 if (state == ActivityState.INITIALIZING
1902 && mStartingWindowState == STARTING_WINDOW_SHOWN
1903 && behindFullscreenActivity) {
1904 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
1905 mStartingWindowState = STARTING_WINDOW_REMOVED;
1906 mWindowContainerController.removeStartingWindow();
1907 }
1908 }
1909
1910 int getRequestedOrientation() {
1911 return mWindowContainerController.getOrientation();
1912 }
1913
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001914 void setRequestedOrientation(int requestedOrientation) {
1915 if (task != null && (!task.mFullscreen || !task.getStack().mFullscreen)) {
1916 // Fixed screen orientation isn't supported when activities aren't in full screen mode.
1917 return;
1918 }
1919
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001920 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001921 final Configuration displayConfig =
1922 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
1923
1924 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
1925 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001926 if (config != null) {
1927 frozenBeforeDestroy = true;
1928 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
1929 false /* deferResume */, displayId)) {
1930 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1931 }
1932 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001933 service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
1934 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001935 }
1936
Bryce Leea163b762017-01-24 11:05:01 -08001937 /**
1938 * Set the last reported global configuration to the client. Should be called whenever a new
1939 * global configuration is sent to the client for this activity.
1940 */
1941 void setLastReportedGlobalConfiguration(@NonNull Configuration config) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001942 mLastReportedConfiguration.setTo(config);
1943 }
1944
Bryce Leea163b762017-01-24 11:05:01 -08001945 /**
1946 * Set the last reported merged override configuration to the client. Should be called whenever
1947 * a new merged configuration is sent to the client for this activity.
1948 */
1949 void setLastReportedMergedOverrideConfiguration(@NonNull Configuration config) {
1950 mLastReportedOverrideConfiguration.setTo(config);
1951 }
1952
Andrii Kulian21713ac2016-10-12 22:05:05 -07001953 /** Call when override config was sent to the Window Manager to update internal records. */
1954 void onOverrideConfigurationSent() {
1955 mLastReportedOverrideConfiguration.setTo(task.getMergedOverrideConfiguration());
1956 }
1957
1958 /**
1959 * Make sure the given activity matches the current configuration. Returns false if the activity
1960 * had to be destroyed. Returns true if the configuration is the same, or the activity will
1961 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
1962 * correct configuration and all other bookkeeping is handled.
1963 */
1964 boolean ensureActivityConfigurationLocked(int globalChanges, boolean preserveWindow) {
1965 final ActivityStack stack = getStack();
1966 if (stack.mConfigWillChange) {
1967 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1968 "Skipping config check (will change): " + this);
1969 return true;
1970 }
1971
1972 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1973 "Ensuring correct configuration: " + this);
1974
Winson Chungbdc646f2017-02-13 12:12:22 -08001975 // Short circuit: if the two full configurations are equal (the common case), then there is
1976 // nothing to do. We test the full configuration instead of the global and merged override
1977 // configurations because there are cases (like moving a task to the pinned stack) where
1978 // the combine configurations are equal, but would otherwise differ in the override config
1979 mTmpConfig1.setTo(mLastReportedConfiguration);
1980 mTmpConfig1.updateFrom(mLastReportedOverrideConfiguration);
1981 if (task.getConfiguration().equals(mTmpConfig1) && !forceNewConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001982 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1983 "Configuration unchanged in " + this);
1984 return true;
1985 }
1986
1987 // We don't worry about activities that are finishing.
1988 if (finishing) {
1989 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1990 "Configuration doesn't matter in finishing " + this);
1991 stopFreezingScreenLocked(false);
1992 return true;
1993 }
1994
1995 // Okay we now are going to make this activity have the new config.
1996 // But then we need to figure out how it needs to deal with that.
Winson Chungbdc646f2017-02-13 12:12:22 -08001997 final Configuration newGlobalConfig = service.getGlobalConfiguration();
1998 final Configuration newTaskMergedOverrideConfig = task.getMergedOverrideConfiguration();
1999 mTmpConfig1.setTo(mLastReportedConfiguration);
2000 mTmpConfig2.setTo(mLastReportedOverrideConfiguration);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002001 mLastReportedConfiguration.setTo(newGlobalConfig);
2002 mLastReportedOverrideConfiguration.setTo(newTaskMergedOverrideConfig);
2003
2004 int taskChanges = getTaskConfigurationChanges(this, newTaskMergedOverrideConfig,
Winson Chungbdc646f2017-02-13 12:12:22 -08002005 mTmpConfig2);
2006 final int changes = mTmpConfig1.diff(newGlobalConfig) | taskChanges;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002007 if (changes == 0 && !forceNewConfig) {
2008 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2009 "Configuration no differences in " + this);
2010 // There are no significant differences, so we won't relaunch but should still deliver
2011 // the new configuration to the client process.
2012 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
2013 return true;
2014 }
2015
2016 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2017 "Configuration changes for " + this + " ; taskChanges="
2018 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
2019 + Configuration.configurationDiffToString(changes));
2020
2021 // If the activity isn't currently running, just leave the new configuration and it will
2022 // pick that up next time it starts.
2023 if (app == null || app.thread == null) {
2024 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2025 "Configuration doesn't matter not running " + this);
2026 stopFreezingScreenLocked(false);
2027 forceNewConfig = false;
2028 return true;
2029 }
2030
2031 // Figure out how to handle the changes between the configurations.
2032 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2033 "Checking to restart " + info.name + ": changed=0x"
2034 + Integer.toHexString(changes) + ", handles=0x"
2035 + Integer.toHexString(info.getRealConfigChanged())
2036 + ", newGlobalConfig=" + newGlobalConfig
2037 + ", newTaskMergedOverrideConfig=" + newTaskMergedOverrideConfig);
2038
Zak Cohen90e7116742017-01-29 12:59:23 -08002039 if (shouldRelaunchLocked(changes, newGlobalConfig, newTaskMergedOverrideConfig)
2040 || forceNewConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002041 // Aha, the activity isn't handling the change, so DIE DIE DIE.
2042 configChangeFlags |= changes;
2043 startFreezingScreenLocked(app, globalChanges);
2044 forceNewConfig = false;
2045 preserveWindow &= isResizeOnlyChange(changes);
2046 if (app == null || app.thread == null) {
2047 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2048 "Config is destroying non-running " + this);
2049 stack.destroyActivityLocked(this, true, "config");
2050 } else if (state == ActivityState.PAUSING) {
2051 // A little annoying: we are waiting for this activity to finish pausing. Let's not
2052 // do anything now, but just flag that it needs to be restarted when done pausing.
2053 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2054 "Config is skipping already pausing " + this);
2055 deferRelaunchUntilPaused = true;
2056 preserveWindowOnDeferredRelaunch = preserveWindow;
2057 return true;
2058 } else if (state == ActivityState.RESUMED) {
2059 // Try to optimize this case: the configuration is changing and we need to restart
2060 // the top, resumed activity. Instead of doing the normal handshaking, just say
2061 // "restart!".
2062 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2063 "Config is relaunching resumed " + this);
2064
2065 if (DEBUG_STATES && !visible) {
2066 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
2067 + " called by " + Debug.getCallers(4));
2068 }
2069
2070 relaunchActivityLocked(true /* andResume */, preserveWindow);
2071 } else {
2072 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2073 "Config is relaunching non-resumed " + this);
2074 relaunchActivityLocked(false /* andResume */, preserveWindow);
2075 }
2076
2077 // All done... tell the caller we weren't able to keep this activity around.
2078 return false;
2079 }
2080
2081 // Default case: the activity can handle this new configuration, so hand it over.
2082 // NOTE: We only forward the task override configuration as the system level configuration
2083 // changes is always sent to all processes when they happen so it can just use whatever
2084 // system level configuration it last got.
2085 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
2086 stopFreezingScreenLocked(false);
2087
2088 return true;
2089 }
2090
Zak Cohen90e7116742017-01-29 12:59:23 -08002091 /**
2092 * When assessing a configuration change, decide if the changes flags and the new configurations
2093 * should cause the Activity to relaunch.
2094 */
2095 private boolean shouldRelaunchLocked(int changes, Configuration newGlobalConfig,
2096 Configuration newTaskMergedOverrideConfig) {
2097 int configChanged = info.getRealConfigChanged();
2098
2099 // Override for apps targeting pre-O sdks
2100 // If a device is in VR mode, and we're transitioning into VR ui mode, add ignore ui mode
2101 // to the config change.
2102 // For O and later, apps will be required to add configChanges="uimode" to their manifest.
2103 if (appInfo.targetSdkVersion < O
2104 && requestedVrComponent != null
2105 && (isInVrUiMode(newGlobalConfig) || isInVrUiMode(newTaskMergedOverrideConfig))) {
2106 configChanged |= CONFIG_UI_MODE;
2107 }
2108
2109 return (changes&(~configChanged)) != 0;
2110 }
2111
Andrii Kulian21713ac2016-10-12 22:05:05 -07002112 private static int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
2113 Configuration oldTaskOverride) {
2114 // If we went from full-screen to non-full-screen, make sure to use the correct
2115 // configuration task diff, so the diff stays as small as possible.
2116 if (Configuration.EMPTY.equals(oldTaskOverride)
2117 && !Configuration.EMPTY.equals(taskConfig)) {
2118 oldTaskOverride = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
2119 }
2120
2121 // Conversely, do the same when going the other direction.
2122 if (Configuration.EMPTY.equals(taskConfig)
2123 && !Configuration.EMPTY.equals(oldTaskOverride)) {
2124 taskConfig = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
2125 }
2126
2127 // Determine what has changed. May be nothing, if this is a config that has come back from
2128 // the app after going idle. In that case we just want to leave the official config object
2129 // now in the activity and do nothing else.
2130 int taskChanges = oldTaskOverride.diff(taskConfig, true /* skipUndefined */);
2131 // We don't want to use size changes if they don't cross boundaries that are important to
2132 // the app.
2133 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
2134 final boolean crosses = record.crossesHorizontalSizeThreshold(
2135 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
2136 || record.crossesVerticalSizeThreshold(
2137 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
2138 if (!crosses) {
2139 taskChanges &= ~CONFIG_SCREEN_SIZE;
2140 }
2141 }
2142 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
2143 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
2144 final int newSmallest = taskConfig.smallestScreenWidthDp;
2145 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
2146 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
2147 }
2148 }
2149 return taskChanges;
2150 }
2151
2152 private static boolean isResizeOnlyChange(int change) {
2153 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2154 | CONFIG_SCREEN_LAYOUT)) == 0;
2155 }
2156
2157 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
2158 if (service.mSuppressResizeConfigChanges && preserveWindow) {
2159 configChangeFlags = 0;
2160 return;
2161 }
2162
2163 List<ResultInfo> pendingResults = null;
2164 List<ReferrerIntent> pendingNewIntents = null;
2165 if (andResume) {
2166 pendingResults = results;
2167 pendingNewIntents = newIntents;
2168 }
2169 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2170 "Relaunching: " + this + " with results=" + pendingResults
2171 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2172 + " preserveWindow=" + preserveWindow);
2173 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
2174 : EventLogTags.AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
2175 task.taskId, shortComponentName);
2176
2177 startFreezingScreenLocked(app, 0);
2178
2179 mStackSupervisor.removeChildActivityContainers(this);
2180
2181 try {
2182 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2183 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2184 + " callers=" + Debug.getCallers(6));
2185 forceNewConfig = false;
2186 mStackSupervisor.activityRelaunchingLocked(this);
2187 app.thread.scheduleRelaunchActivity(appToken, pendingResults, pendingNewIntents,
2188 configChangeFlags, !andResume,
2189 new Configuration(service.getGlobalConfiguration()),
2190 new Configuration(task.getMergedOverrideConfiguration()), preserveWindow);
2191 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
2192 // pass in 'andResume' if this activity is currently resumed, which implies we aren't
2193 // sleeping.
2194 } catch (RemoteException e) {
2195 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2196 }
2197
2198 if (andResume) {
2199 if (DEBUG_STATES) {
2200 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2201 }
2202 results = null;
2203 newIntents = null;
2204 service.showUnsupportedZoomDialogIfNeededLocked(this);
2205 service.showAskCompatModeDialogLocked(this);
2206 } else {
2207 service.mHandler.removeMessages(ActivityStack.PAUSE_TIMEOUT_MSG, this);
2208 state = ActivityState.PAUSED;
2209 // if the app is relaunched when it's stopped, and we're not resuming,
2210 // put it back into stopped state.
2211 if (stopped) {
Winson Chung4dabf232017-01-25 13:25:22 -08002212 getStack().addToStopping(this, true /* scheduleIdle */, false /* idleDelayed */);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002213 }
2214 }
2215
2216 configChangeFlags = 0;
2217 deferRelaunchUntilPaused = false;
2218 preserveWindowOnDeferredRelaunch = false;
2219 }
2220
Jorim Jaggi02886a82016-12-06 09:10:06 -08002221 boolean isProcessRunning() {
2222 ProcessRecord proc = app;
2223 if (proc == null) {
2224 proc = service.mProcessNames.get(processName, info.applicationInfo.uid);
2225 }
2226 return proc != null && proc.thread != null;
2227 }
2228
Craig Mautner21d24a22014-04-23 11:45:37 -07002229 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2230 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2231 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2232 if (launchedFromPackage != null) {
2233 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2234 }
2235 if (resolvedType != null) {
2236 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2237 }
2238 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2239 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002240
Craig Mautner21d24a22014-04-23 11:45:37 -07002241 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002242 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002243 }
2244
2245 out.startTag(null, TAG_INTENT);
2246 intent.saveToXml(out);
2247 out.endTag(null, TAG_INTENT);
2248
2249 if (isPersistable() && persistentState != null) {
2250 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2251 persistentState.saveToXml(out);
2252 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2253 }
2254 }
2255
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002256 static ActivityRecord restoreFromXml(XmlPullParser in,
2257 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002258 Intent intent = null;
2259 PersistableBundle persistentState = null;
2260 int launchedFromUid = 0;
2261 String launchedFromPackage = null;
2262 String resolvedType = null;
2263 boolean componentSpecified = false;
2264 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002265 long createTime = -1;
2266 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002267 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002268
2269 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2270 final String attrName = in.getAttributeName(attrNdx);
2271 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002272 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002273 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002274 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002275 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002276 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002277 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002278 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2279 launchedFromPackage = attrValue;
2280 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2281 resolvedType = attrValue;
2282 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002283 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002284 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002285 userId = Integer.parseInt(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07002286 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
2287 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002288 } else {
2289 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2290 }
2291 }
2292
2293 int event;
2294 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07002295 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002296 if (event == XmlPullParser.START_TAG) {
2297 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002298 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002299 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002300 if (TAG_INTENT.equals(name)) {
2301 intent = Intent.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002302 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002303 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002304 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2305 persistentState = PersistableBundle.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002306 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002307 "ActivityRecord: persistentState=" + persistentState);
2308 } else {
2309 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2310 XmlUtils.skipCurrentTag(in);
2311 }
2312 }
2313 }
2314
2315 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002316 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002317 }
2318
2319 final ActivityManagerService service = stackSupervisor.mService;
2320 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Jeff Hao1b012d32014-08-20 10:35:34 -07002321 userId);
Craig Mautnere0129b32014-05-25 16:41:09 -07002322 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002323 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2324 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002325 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002326 final ActivityRecord r = new ActivityRecord(service, null /* caller */,
2327 0 /* launchedFromPid */, launchedFromUid, launchedFromPackage, intent, resolvedType,
2328 aInfo, service.getConfiguration(), null /* resultTo */, null /* resultWho */,
2329 0 /* reqCode */, componentSpecified, false /* rootVoiceInteraction */,
2330 stackSupervisor, null /* container */, null /* options */, null /* sourceRecord */);
Craig Mautner21d24a22014-04-23 11:45:37 -07002331
2332 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002333 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002334 r.createTime = createTime;
2335
2336 return r;
2337 }
2338
2339 private static String activityTypeToString(int type) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002340 switch (type) {
2341 case APPLICATION_ACTIVITY_TYPE: return "APPLICATION_ACTIVITY_TYPE";
2342 case HOME_ACTIVITY_TYPE: return "HOME_ACTIVITY_TYPE";
2343 case RECENTS_ACTIVITY_TYPE: return "RECENTS_ACTIVITY_TYPE";
Winson Chung83471632016-12-13 11:02:12 -08002344 case ASSISTANT_ACTIVITY_TYPE: return "ASSISTANT_ACTIVITY_TYPE";
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002345 default: return Integer.toString(type);
2346 }
2347 }
2348
Zak Cohen90e7116742017-01-29 12:59:23 -08002349 private static boolean isInVrUiMode(Configuration config) {
2350 return (config.uiMode & Configuration.UI_MODE_TYPE_MASK) == UI_MODE_TYPE_VR_HEADSET;
2351 }
2352
Craig Mautnerf81b90872013-02-26 13:02:43 -08002353 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002355 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002356 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002357 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002358 }
2359 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002360 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002361 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002362 sb.append(" u");
2363 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002364 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002365 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002366 stringName = sb.toString();
2367 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
2369}