blob: 8f1aa0dea2f8c91f111cbbb99676dd7ff6546e4a [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;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080026import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale22e25262016-02-01 10:32:02 -080027import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Winson Chung59fda9e2017-01-20 16:14:51 -080028import static android.app.AppOpsManager.MODE_ALLOWED;
Winson Chungf4ac0632017-03-17 12:34:12 -070029import static android.app.AppOpsManager.OP_PICTURE_IN_PICTURE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080030import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070031import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
32import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
33import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
34import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Zak Cohen90e7116742017-01-29 12:59:23 -080035import static android.content.pm.ActivityInfo.CONFIG_UI_MODE;
Jorim Jaggi02886a82016-12-06 09:10:06 -080036import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080037import static android.content.pm.ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
38import static android.content.pm.ActivityInfo.FLAG_IMMERSIVE;
39import static android.content.pm.ActivityInfo.FLAG_MULTIPROCESS;
Chong Zhang87761972016-08-22 13:53:24 -070040import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080041import static android.content.pm.ActivityInfo.FLAG_STATE_NOT_NEEDED;
42import static android.content.pm.ActivityInfo.LAUNCH_MULTIPLE;
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -080043import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
44import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080045import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070046import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggicd13d332016-04-27 15:40:20 -070047import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwale72a73e32016-10-13 12:16:39 -070048import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070049import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -070050import static android.content.res.Configuration.EMPTY;
Zak Cohen90e7116742017-01-29 12:59:23 -080051import static android.content.res.Configuration.UI_MODE_TYPE_VR_HEADSET;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080052import static android.os.Build.VERSION_CODES.HONEYCOMB;
Zak Cohen90e7116742017-01-29 12:59:23 -080053import static android.os.Build.VERSION_CODES.O;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080054import static android.os.Process.SYSTEM_UID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080055import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
Andrii Kulian21713ac2016-10-12 22:05:05 -070056import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
57import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SCREENSHOTS;
58import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080059import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
60import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070061import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
62import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
63import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
64import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080065import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
66import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
67import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070068import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080069import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
70import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Andrii Kulian21713ac2016-10-12 22:05:05 -070071import static com.android.server.am.ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS;
Wale Ogunwale18795a22014-12-03 11:38:33 -080072import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
73
Andrii Kulian21713ac2016-10-12 22:05:05 -070074import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -070075import android.app.ActivityManager.TaskDescription;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070076import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080077import android.app.PendingIntent;
Winson Chungc2baac02017-01-11 13:34:47 -080078import android.app.PictureInPictureArgs;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080079import android.app.ResultInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.content.ComponentName;
81import android.content.Intent;
82import android.content.pm.ActivityInfo;
83import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -070084import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.content.res.Configuration;
86import android.graphics.Bitmap;
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -070087import android.graphics.Point;
Dianne Hackbornd367ca82012-05-07 15:49:39 -070088import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -070090import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -080091import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080093import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -070095import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.SystemClock;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080097import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070098import android.os.UserHandle;
Amith Yamasani0af6fa72016-01-17 15:36:19 -080099import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.util.EventLog;
101import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700102import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700103import android.util.TimeUtils;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700104import android.view.AppTransitionAnimationSpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.IApplicationToken;
Jorim Jaggife762342016-10-13 14:33:27 +0200106import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700107
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800108import com.android.internal.app.ResolverActivity;
109import com.android.internal.content.ReferrerIntent;
110import com.android.internal.util.XmlUtils;
111import com.android.server.AttributeCache;
112import com.android.server.am.ActivityStack.ActivityState;
113import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800114import com.android.server.wm.AppWindowContainerController;
115import com.android.server.wm.AppWindowContainerListener;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800116import com.android.server.wm.TaskWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Jorim Jaggi02886a82016-12-06 09:10:06 -0800118import org.xmlpull.v1.XmlPullParser;
119import org.xmlpull.v1.XmlPullParserException;
120import org.xmlpull.v1.XmlSerializer;
121
Suprabh Shukla23593142015-11-03 17:31:15 -0800122import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -0700123import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import java.io.PrintWriter;
125import java.lang.ref.WeakReference;
126import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800127import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700129import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700130import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132/**
133 * An entry in the history stack, representing an activity.
134 */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700135final class ActivityRecord extends ConfigurationContainer implements AppWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800136 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_AM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700137 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
138 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
139 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700140 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700141 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
142 private static final String TAG_THUMBNAILS = TAG + POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700143 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800144
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700145 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700146 private static final String RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700147
Craig Mautner21d24a22014-04-23 11:45:37 -0700148 private static final String ATTR_ID = "id";
149 private static final String TAG_INTENT = "intent";
150 private static final String ATTR_USERID = "user_id";
151 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
152 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700153 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700154 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
155 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700156 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 final ActivityManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800159 final IApplicationToken.Stub appToken; // window manager token
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800160 AppWindowContainerController mWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 final ActivityInfo info; // all about me
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700162 final ApplicationInfo appInfo; // information about activity's app
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800163 final int launchedFromPid; // always the pid who started the activity.
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700164 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800165 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700166 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final Intent intent; // the original intent that generated us
168 final ComponentName realActivity; // the intent component, or target of an alias.
169 final String shortComponentName; // the short component name of the intent
170 final String resolvedType; // as per original caller;
171 final String packageName; // the package implementing intent's component
172 final String processName; // process where this component wants to run
173 final String taskAffinity; // as per ActivityInfo.taskAffinity
174 final boolean stateNotNeeded; // As per ActivityInfo.flags
Craig Mautner4addfc52013-06-25 08:05:45 -0700175 boolean fullscreen; // covers the full screen?
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800176 final boolean noDisplay; // activity is not displayed?
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800177 private final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700178 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700179
180 static final int APPLICATION_ACTIVITY_TYPE = 0;
181 static final int HOME_ACTIVITY_TYPE = 1;
182 static final int RECENTS_ACTIVITY_TYPE = 2;
Winson Chung83471632016-12-13 11:02:12 -0800183 static final int ASSISTANT_ACTIVITY_TYPE = 3;
Craig Mautner2c1faed2013-07-23 12:56:02 -0700184 int mActivityType;
Craig Mautner86d67a42013-05-14 10:34:38 -0700185
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800186 private CharSequence nonLocalizedLabel; // the label information from the package mgr.
187 private int labelRes; // the label information from the package mgr.
188 private int icon; // resource identifier of activity's icon.
189 private int logo; // resource identifier of activity's logo.
190 private int theme; // resource identifier of activity's theme.
191 private int realTheme; // actual theme resource we will use, never 0.
192 private int windowFlags; // custom window flags for preview window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 TaskRecord task; // the task this is in.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800194 private long createTime = System.currentTimeMillis();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700195 long displayStartTime; // when we started launching this activity
196 long fullyDrawnStartTime; // when we started launching this activity
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800197 private long startTime; // last time this activity was started
Dianne Hackborn50685602011-12-01 12:23:37 -0800198 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700199 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700200 long pauseTime; // last time we started pausing the activity
201 long launchTickTime; // base time for launch tick messages
Andrii Kulian21713ac2016-10-12 22:05:05 -0700202 private Configuration mLastReportedConfiguration; // configuration activity was last running in
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700203 // Overridden configuration by the activity task
Andrii Kulian1779e612016-10-12 21:58:25 -0700204 // WARNING: Reference points to {@link TaskRecord#getMergedOverrideConfig}, so its internal
205 // state should never be altered directly.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700206 private Configuration mLastReportedOverrideConfiguration;
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800207 private int mLastReportedDisplayId;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700208 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700209 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 final String resultWho; // additional identifier for use by resultTo.
211 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800212 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800214 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700215 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700216 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700217 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -0700219 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700220 ProcessRecord app; // if non-null, hosting application
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700221 ActivityState state; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700223 PersistableBundle persistentState; // last persistently saved activity state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 boolean frontOfTask; // is this the root activity of its task?
225 boolean launchFailed; // set if a launched failed, to abort on 2nd try
226 boolean haveState; // have we gotten the last activity state?
227 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700228 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800230 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
231 // completed
232 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 int configChangeFlags; // which config values have changed
234 boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 int launchMode; // the launch mode activity attribute.
236 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700237 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
238 // might hide this activity?
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800239 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 boolean nowVisible; // is this activity's window visible?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 boolean idle; // has the activity gone idle?
242 boolean hasBeenLaunched;// has this activity ever been launched?
243 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400244 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400245 boolean forceNewConfig; // force re-create with new config next time
Winson Chungc2baac02017-01-11 13:34:47 -0800246 boolean supportsPictureInPictureWhilePausing; // This flag is set by the system to indicate
247 // that the activity can enter picture in picture while pausing (ie. only when another
248 // task is brought to front or started)
249 PictureInPictureArgs pictureInPictureArgs = new PictureInPictureArgs(); // The PiP
Winson Chunga29eb982016-12-14 12:01:27 -0800250 // arguments used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800251 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800252 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800253 ComponentName requestedVrComponent; // the requested component for handling VR mode.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800254 ArrayList<ActivityContainer> mChildContainers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700256 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700257
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700258 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700259 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800260
261 static final int STARTING_WINDOW_NOT_SHOWN = 0;
262 static final int STARTING_WINDOW_SHOWN = 1;
263 static final int STARTING_WINDOW_REMOVED = 2;
264 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700265 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800266
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800267 boolean mUpdateTaskThumbnailWhenHidden;
Craig Mautnere0a38842013-12-16 16:14:02 -0800268 ActivityContainer mInitialActivityContainer;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700269
Craig Mautner21d24a22014-04-23 11:45:37 -0700270 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700271 boolean mLaunchTaskBehind; // this activity is actively being launched with
272 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700273
Filip Gruszczynski23493322015-07-29 17:02:59 -0700274 // These configurations are collected from application's resources based on size-sensitive
275 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
276 // and drawable-sw400dp will be added to both as 400.
277 private int[] mVerticalSizeConfigurations;
278 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700279 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700280
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800281 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
282 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
283
Robert Carrd2265122016-08-05 10:25:21 -0700284 // A hint to override the window specified rotation animation, or -1
285 // to use the window specified value. We use this so that
286 // we can select the right animation in the cases of starting
287 // windows, where the app hasn't had time to set a value
288 // on the window.
289 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700290
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700291 // The bounds of this activity. Mainly used for aspect-ratio compatibility.
292 // TODO(b/36505427): Every level on ConfigurationContainer now has bounds information, which
293 // directly affects the configuration. We should probably move this into that class and have it
294 // handle calculating override configuration from the bounds.
295 private final Rect mBounds = new Rect();
296
Andrii Kulian21713ac2016-10-12 22:05:05 -0700297 /**
298 * Temp configs used in {@link #ensureActivityConfigurationLocked(int, boolean)}
299 */
Winson Chungbdc646f2017-02-13 12:12:22 -0800300 private final Configuration mTmpConfig1 = new Configuration();
301 private final Configuration mTmpConfig2 = new Configuration();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700302 private final Point mTmpPoint = new Point();
303 private final Rect mTmpBounds = new Rect();
Andrii Kulian21713ac2016-10-12 22:05:05 -0700304
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800305 private static String startingWindowStateToString(int state) {
306 switch (state) {
307 case STARTING_WINDOW_NOT_SHOWN:
308 return "STARTING_WINDOW_NOT_SHOWN";
309 case STARTING_WINDOW_SHOWN:
310 return "STARTING_WINDOW_SHOWN";
311 case STARTING_WINDOW_REMOVED:
312 return "STARTING_WINDOW_REMOVED";
313 default:
314 return "unknown state=" + state;
315 }
316 }
317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700319 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700320 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
321 pw.print(" processName="); pw.println(processName);
322 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700323 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800324 pw.print(" userId="); pw.println(userId);
325 pw.print(prefix); pw.print("app="); pw.println(app);
326 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700327 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
328 pw.print(" task="); pw.println(task);
329 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700330 pw.print(prefix); pw.print("realActivity=");
331 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700332 if (appInfo != null) {
333 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
334 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
335 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
336 }
337 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800338 if (appInfo.splitSourceDirs != null) {
339 pw.print(prefix); pw.print("splitDir=");
340 pw.println(Arrays.toString(appInfo.splitSourceDirs));
341 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800342 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700343 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
344 pw.print(" componentSpecified="); pw.print(componentSpecified);
Craig Mautner86d67a42013-05-14 10:34:38 -0700345 pw.print(" mActivityType="); pw.println(mActivityType);
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700346 if (rootVoiceInteraction) {
347 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
348 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800349 pw.print(prefix); pw.print("compat="); pw.print(compat);
350 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
351 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
352 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Andrii Kulian1779e612016-10-12 21:58:25 -0700353 pw.print(prefix); pw.print("mLastReportedConfiguration=");
354 pw.println(mLastReportedConfiguration);
355 pw.print(prefix); pw.print("mLastReportedOverrideConfiguration=");
356 pw.println(mLastReportedOverrideConfiguration);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700357 pw.print(prefix); pw.print("CurrentConfiguration="); pw.println(getConfiguration());
358 if (!getOverrideConfiguration().equals(EMPTY)) {
359 pw.println(prefix + "OverrideConfiguration=" + getOverrideConfiguration());
360 }
361 if (!mBounds.isEmpty()) {
362 pw.println(prefix + "mBounds=" + mBounds);
363 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700364 if (resultTo != null || resultWho != null) {
365 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
366 pw.print(" resultWho="); pw.print(resultWho);
367 pw.print(" resultCode="); pw.println(requestCode);
368 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700369 if (taskDescription != null) {
370 final String iconFilename = taskDescription.getIconFilename();
371 if (iconFilename != null || taskDescription.getLabel() != null ||
372 taskDescription.getPrimaryColor() != 0) {
373 pw.print(prefix); pw.print("taskDescription:");
374 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
375 pw.print(" label=\""); pw.print(taskDescription.getLabel());
376 pw.print("\"");
377 pw.print(" color=");
378 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
379 }
380 if (iconFilename == null && taskDescription.getIcon() != null) {
381 pw.print(prefix); pw.println("taskDescription contains Bitmap");
382 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700383 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700384 if (results != null) {
385 pw.print(prefix); pw.print("results="); pw.println(results);
386 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700387 if (pendingResults != null && pendingResults.size() > 0) {
388 pw.print(prefix); pw.println("Pending Results:");
389 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
390 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
391 pw.print(prefix); pw.print(" - ");
392 if (pir == null) {
393 pw.println("null");
394 } else {
395 pw.println(pir);
396 pir.dump(pw, prefix + " ");
397 }
398 }
399 }
400 if (newIntents != null && newIntents.size() > 0) {
401 pw.print(prefix); pw.println("Pending New Intents:");
402 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800403 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700404 pw.print(prefix); pw.print(" - ");
405 if (intent == null) {
406 pw.println("null");
407 } else {
408 pw.println(intent.toShortString(false, true, false, true));
409 }
410 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700411 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700412 if (pendingOptions != null) {
413 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
414 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700415 if (appTimeTracker != null) {
416 appTimeTracker.dumpWithHeader(pw, prefix, false);
417 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700418 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700419 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700420 }
421 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800422 pw.print(" launchCount="); pw.print(launchCount);
423 pw.print(" lastLaunchTime=");
424 if (lastLaunchTime == 0) pw.print("0");
425 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
426 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700427 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700428 pw.print(" icicle="); pw.println(icicle);
429 pw.print(prefix); pw.print("state="); pw.print(state);
430 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700431 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700432 pw.print(" finishing="); pw.println(finishing);
433 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
434 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700435 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800436 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800437 pw.print(" idle="); pw.print(idle);
438 pw.print(" mStartingWindowState=");
439 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800440 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
441 pw.print(" noDisplay="); pw.print(noDisplay);
442 pw.print(" immersive="); pw.print(immersive);
443 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800444 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400445 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700446 pw.print(prefix); pw.print("mActivityType=");
447 pw.println(activityTypeToString(mActivityType));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800448 if (requestedVrComponent != null) {
449 pw.print(prefix);
450 pw.print("requestedVrComponent=");
451 pw.println(requestedVrComponent);
452 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700453 if (displayStartTime != 0 || startTime != 0) {
454 pw.print(prefix); pw.print("displayStartTime=");
455 if (displayStartTime == 0) pw.print("0");
456 else TimeUtils.formatDuration(displayStartTime, now, pw);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700457 pw.print(" startTime=");
458 if (startTime == 0) pw.print("0");
459 else TimeUtils.formatDuration(startTime, now, pw);
460 pw.println();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700461 }
Craig Mautner8c14c152015-01-15 17:32:07 -0800462 final boolean waitingVisible = mStackSupervisor.mWaitingVisibleActivities.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800463 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700464 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800465 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700466 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700467 if (lastVisibleTime == 0) pw.print("0");
468 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
469 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700470 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800471 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
472 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700473 pw.print(" configChangeFlags=");
474 pw.println(Integer.toHexString(configChangeFlags));
475 }
476 if (connections != null) {
477 pw.print(prefix); pw.print("connections="); pw.println(connections);
478 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800479 if (info != null) {
480 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700481 if (info.supportsPictureInPicture()) {
482 pw.println(prefix + "supportsPictureInPicture=" + info.supportsPictureInPicture());
483 pw.println(prefix + "supportsPictureInPictureWhilePausing: "
484 + supportsPictureInPictureWhilePausing);
485 }
486 if (info.maxAspectRatio != 0) {
487 pw.println(prefix + "maxAspectRatio=" + info.maxAspectRatio);
488 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
491
Andrii Kulian21713ac2016-10-12 22:05:05 -0700492 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700493 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
494 }
495
Andrii Kulian21713ac2016-10-12 22:05:05 -0700496 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700497 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
498 }
499
Andrii Kulian21713ac2016-10-12 22:05:05 -0700500 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700501 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
502 }
503
Filip Gruszczynski23493322015-07-29 17:02:59 -0700504 /**
505 * The purpose of this method is to decide whether the activity needs to be relaunched upon
506 * changing its size. In most cases the activities don't need to be relaunched, if the resize
507 * is small, all the activity content has to do is relayout itself within new bounds. There are
508 * cases however, where the activity's content would be completely changed in the new size and
509 * the full relaunch is required.
510 *
511 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
512 * required. These thresholds are collected from the application resource qualifiers. For
513 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
514 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
515 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
516 * of the threshold.
517 */
518 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
519 int secondDp) {
520 if (thresholds == null) {
521 return false;
522 }
523 for (int i = thresholds.length - 1; i >= 0; i--) {
524 final int threshold = thresholds[i];
525 if ((firstDp < threshold && secondDp >= threshold)
526 || (firstDp >= threshold && secondDp < threshold)) {
527 return true;
528 }
529 }
530 return false;
531 }
532
Andrii Kulian21713ac2016-10-12 22:05:05 -0700533 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700534 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700535 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
536 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700537 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700538 }
539
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800540 private void scheduleActivityMovedToDisplay(int displayId, Configuration config) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800541 if (app == null || app.thread == null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800542 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.w(TAG,
543 "Can't report activity moved to display - client not running, activityRecord="
544 + this + ", displayId=" + displayId);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800545 return;
546 }
547 try {
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800548 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
549 "Reporting activity moved to display" + ", activityRecord=" + this
550 + ", displayId=" + displayId + ", config=" + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700551
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800552 app.thread.scheduleActivityMovedToDisplay(appToken, displayId,
553 new Configuration(config));
554 } catch (RemoteException e) {
555 // If process died, whatever.
556 }
557 }
558
559 private void scheduleConfigurationChanged(Configuration config) {
560 if (app == null || app.thread == null) {
561 if (DEBUG_CONFIGURATION) Slog.w(TAG,
562 "Can't report activity configuration update - client not running"
563 + ", activityRecord=" + this);
564 return;
565 }
566 try {
567 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + ", config: "
568 + config);
569
570 app.thread.scheduleActivityConfigurationChanged(appToken, new Configuration(config));
Wale Ogunwale22e25262016-02-01 10:32:02 -0800571 } catch (RemoteException e) {
572 // If process died, whatever.
573 }
574 }
575
Andrii Kulian933076d2016-03-29 17:04:42 -0700576 void scheduleMultiWindowModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700577 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800578 return;
579 }
580 try {
581 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Andrii Kulian933076d2016-03-29 17:04:42 -0700582 app.thread.scheduleMultiWindowModeChanged(appToken, !task.mFullscreen);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800583 } catch (Exception e) {
584 // If process died, I don't care.
585 }
586 }
587
Andrii Kulian933076d2016-03-29 17:04:42 -0700588 void schedulePictureInPictureModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700589 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800590 return;
591 }
592 try {
Andrii Kulian933076d2016-03-29 17:04:42 -0700593 app.thread.schedulePictureInPictureModeChanged(
Andrii Kulian02b7a832016-10-06 23:11:56 -0700594 appToken, task.getStackId() == PINNED_STACK_ID);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800595 } catch (Exception e) {
596 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800597 }
598 }
599
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800600 boolean isFreeform() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700601 return task != null && task.getStackId() == FREEFORM_WORKSPACE_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800602 }
603
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700604 @Override
605 protected int getChildCount() {
606 // {@link ActivityRecord} is a leaf node and has no children.
607 return 0;
608 }
609
610 @Override
611 protected ConfigurationContainer getChildAt(int index) {
612 return null;
613 }
614
615 @Override
616 protected ConfigurationContainer getParent() {
617 return task;
618 }
619
Dianne Hackbornbe707852011-11-11 14:32:10 -0800620 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700621 private final WeakReference<ActivityRecord> weakActivity;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800622
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800623 Token(ActivityRecord activity) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800624 weakActivity = new WeakReference<>(activity);
Wale Ogunwale7d701172015-03-11 15:36:30 -0700625 }
626
Andrii Kulian21713ac2016-10-12 22:05:05 -0700627 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700628 if (token == null) {
629 return null;
630 }
631 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700632 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700633 return null;
634 }
635 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800636 }
637
Craig Mautnerde4ef022013-04-07 19:01:33 -0700638 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800639 public String toString() {
640 StringBuilder sb = new StringBuilder(128);
641 sb.append("Token{");
642 sb.append(Integer.toHexString(System.identityHashCode(this)));
643 sb.append(' ');
644 sb.append(weakActivity.get());
645 sb.append('}');
646 return sb.toString();
647 }
648 }
649
Wale Ogunwale7d701172015-03-11 15:36:30 -0700650 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800651 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700652 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800653 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800654 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800655 return null;
656 }
657 }
658
Chong Zhang85ee6542015-10-02 13:36:38 -0700659 boolean isResolverActivity() {
660 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700661 }
662
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800663 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller, int _launchedFromPid,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800664 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700666 ActivityRecord _resultTo, String _resultWho, int _reqCode,
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700667 boolean _componentSpecified, boolean _rootVoiceInteraction,
668 ActivityStackSupervisor supervisor,
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700669 ActivityContainer container, ActivityOptions options, ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 service = _service;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800671 appToken = new Token(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 info = aInfo;
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800673 launchedFromPid = _launchedFromPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800675 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700676 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 intent = _intent;
678 shortComponentName = _intent.getComponent().flattenToShortString();
679 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800680 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700681 rootVoiceInteraction = _rootVoiceInteraction;
Andrii Kulian1779e612016-10-12 21:58:25 -0700682 mLastReportedConfiguration = new Configuration(_configuration);
683 mLastReportedOverrideConfiguration = new Configuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 resultTo = _resultTo;
685 resultWho = _resultWho;
686 requestCode = _reqCode;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700687 state = ActivityState.INITIALIZING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 frontOfTask = false;
689 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700691 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800693 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 keysPaused = false;
695 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700696 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 idle = false;
699 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700700 mStackSupervisor = supervisor;
Craig Mautnere0a38842013-12-16 16:14:02 -0800701 mInitialActivityContainer = container;
Robert Carr0f5d7532016-10-17 16:39:17 -0700702
703 mRotationAnimationHint = aInfo.rotationAnimation;
704
Craig Mautner233ceee2014-05-09 17:05:11 -0700705 if (options != null) {
Chong Zhang280d3322015-11-03 17:27:26 -0800706 pendingOptions = options;
Craig Mautnerbb742462014-07-07 15:28:55 -0700707 mLaunchTaskBehind = pendingOptions.getLaunchTaskBehind();
Robert Carr0f5d7532016-10-17 16:39:17 -0700708
709 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
710 // Only override manifest supplied option if set.
711 if (rotationAnimation >= 0) {
712 mRotationAnimationHint = rotationAnimation;
713 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700714 PendingIntent usageReport = pendingOptions.getUsageTimeReport();
715 if (usageReport != null) {
716 appTimeTracker = new AppTimeTracker(usageReport);
717 }
Craig Mautner233ceee2014-05-09 17:05:11 -0700718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800720 // This starts out true, since the initial state of an activity is that we have everything,
721 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700722 haveState = true;
723
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800724 // If the class name in the intent doesn't match that of the target, this is
725 // probably an alias. We have to create a new ComponentName object to keep track
726 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
727 if (aInfo.targetActivity == null
728 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
729 && (aInfo.launchMode == LAUNCH_MULTIPLE
730 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
731 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800733 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800735 taskAffinity = aInfo.taskAffinity;
736 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
737 appInfo = aInfo.applicationInfo;
738 nonLocalizedLabel = aInfo.nonLocalizedLabel;
739 labelRes = aInfo.labelRes;
740 if (nonLocalizedLabel == null && labelRes == 0) {
741 ApplicationInfo app = aInfo.applicationInfo;
742 nonLocalizedLabel = app.nonLocalizedLabel;
743 labelRes = app.labelRes;
744 }
745 icon = aInfo.getIconResource();
746 logo = aInfo.getLogoResource();
747 theme = aInfo.getThemeResource();
748 realTheme = theme;
749 if (realTheme == 0) {
750 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
751 ? android.R.style.Theme : android.R.style.Theme_Holo;
752 }
753 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
754 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
755 }
756 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
757 && (aInfo.applicationInfo.uid == SYSTEM_UID
758 || aInfo.applicationInfo.uid == _caller.info.uid)) {
759 processName = _caller.processName;
760 } else {
761 processName = aInfo.processName;
762 }
763
764 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
765 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
766 }
767
768 packageName = aInfo.applicationInfo.packageName;
769 launchMode = aInfo.launchMode;
770
771 AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
772 realTheme, com.android.internal.R.styleable.Window, userId);
773 final boolean translucent = ent != null && (ent.array.getBoolean(
774 com.android.internal.R.styleable.Window_windowIsTranslucent, false)
775 || (!ent.array.hasValue(
776 com.android.internal.R.styleable.Window_windowIsTranslucent)
777 && ent.array.getBoolean(
778 com.android.internal.R.styleable.Window_windowSwipeToDismiss,
779 false)));
780 fullscreen = ent != null && !ent.array.getBoolean(
781 com.android.internal.R.styleable.Window_windowIsFloating, false) && !translucent;
782 noDisplay = ent != null && ent.array.getBoolean(
783 com.android.internal.R.styleable.Window_windowNoDisplay, false);
784
Winson Chung83471632016-12-13 11:02:12 -0800785 setActivityType(_componentSpecified, _launchedFromUid, _intent, options, sourceRecord);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800786
787 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
788
789 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
790 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
791 }
792
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800793 AppWindowContainerController getWindowContainerController() {
794 return mWindowContainerController;
795 }
796
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800797 void createWindowContainer() {
798 if (mWindowContainerController != null) {
799 throw new IllegalArgumentException("Window container=" + mWindowContainerController
800 + " already created for r=" + this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800801 }
802
803 inHistory = true;
804
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800805 final TaskWindowContainerController taskController = task.getWindowContainerController();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800806
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700807 // TODO(b/36505427): Maybe this call should be moved inside updateOverrideConfiguration()
808 task.updateOverrideConfigurationFromLaunchBounds();
809 // Make sure override configuration is up-to-date before using to create window controller.
810 updateOverrideConfiguration();
811
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800812 mWindowContainerController = new AppWindowContainerController(taskController, appToken,
813 this, Integer.MAX_VALUE /* add on top */, info.screenOrientation, fullscreen,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800814 (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
815 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
816 appInfo.targetSdkVersion, mRotationAnimationHint,
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700817 ActivityManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L,
818 getOverrideConfiguration(), mBounds);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800819
820 task.addActivityToTop(this);
821
822 onOverrideConfigurationSent();
823 }
824
825 void removeWindowContainer() {
Wale Ogunwalecc367f42017-02-01 08:12:14 -0800826 // Resume key dispatching if it is currently paused before we remove the container.
827 resumeKeyDispatchingLocked();
828
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800829 mWindowContainerController.removeContainer(getDisplayId());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800830 mWindowContainerController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 }
832
Winson Chung30480042017-01-26 10:55:34 -0800833 /**
834 * Reparents this activity into {@param newTask} at the provided {@param position}. The caller
835 * should ensure that the {@param newTask} is not already the parent of this activity.
836 */
837 void reparent(TaskRecord newTask, int position, String reason) {
838 final TaskRecord prevTask = task;
839 if (prevTask == newTask) {
840 throw new IllegalArgumentException(reason + ": task=" + newTask
841 + " is already the parent of r=" + this);
842 }
843
Winson Chung74666102017-02-22 17:49:24 -0800844 // TODO: Ensure that we do not directly reparent activities across stacks, as that may leave
845 // the stacks in strange states. For now, we should use Task.reparent() to ensure that
846 // the stack is left in an OK state.
847 if (prevTask != null && newTask != null && prevTask.getStack() != newTask.getStack()) {
848 throw new IllegalArgumentException(reason + ": task=" + newTask
849 + " is in a different stack (" + newTask.getStackId() + ") than the parent of"
850 + " r=" + this + " (" + prevTask.getStackId() + ")");
851 }
852
Winson Chung30480042017-01-26 10:55:34 -0800853 // Must reparent first in window manager
854 mWindowContainerController.reparent(newTask.getWindowContainerController(), position);
855
856 // Remove the activity from the old task and add it to the new task
857 prevTask.removeActivity(this);
Bryce Lee0f9bde82017-02-22 16:39:06 -0800858
Winson Chung30480042017-01-26 10:55:34 -0800859 newTask.addActivityAtIndex(position, this);
860 }
861
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700862 private boolean isHomeIntent(Intent intent) {
863 return Intent.ACTION_MAIN.equals(intent.getAction())
864 && intent.hasCategory(Intent.CATEGORY_HOME)
865 && intent.getCategories().size() == 1
866 && intent.getData() == null
867 && intent.getType() == null;
868 }
869
Chong Zhangad24f962016-08-25 12:12:33 -0700870 static boolean isMainIntent(Intent intent) {
871 return Intent.ACTION_MAIN.equals(intent.getAction())
872 && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
873 && intent.getCategories().size() == 1
874 && intent.getData() == null
875 && intent.getType() == null;
876 }
877
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700878 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
879 if (uid == Process.myUid() || uid == 0) {
880 // System process can launch home activity.
881 return true;
882 }
883 // Resolver activity can launch home activity.
884 return sourceRecord != null && sourceRecord.isResolverActivity();
885 }
886
Winson Chung83471632016-12-13 11:02:12 -0800887 /**
888 * @return whether the given package name can launch an assist activity.
889 */
890 private boolean canLaunchAssistActivity(String packageName) {
891 if (service.mAssistUtils == null) {
892 return false;
893 }
894
895 final ComponentName assistComponent = service.mAssistUtils.getActiveServiceComponentName();
896 if (assistComponent != null) {
897 return assistComponent.getPackageName().equals(packageName);
898 }
899 return false;
900 }
901
902 private void setActivityType(boolean componentSpecified, int launchedFromUid, Intent intent,
903 ActivityOptions options, ActivityRecord sourceRecord) {
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700904 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
905 && isHomeIntent(intent) && !isResolverActivity()) {
906 // This sure looks like a home activity!
907 mActivityType = HOME_ACTIVITY_TYPE;
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700908
909 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
910 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
911 // We only allow home activities to be resizeable if they explicitly requested it.
912 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
913 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700914 } else if (realActivity.getClassName().contains(RECENTS_PACKAGE_NAME)) {
915 mActivityType = RECENTS_ACTIVITY_TYPE;
Winson Chung83471632016-12-13 11:02:12 -0800916 } else if (options != null && options.getLaunchStackId() == ASSISTANT_STACK_ID
917 && canLaunchAssistActivity(launchedFromPackage)) {
918 mActivityType = ASSISTANT_ACTIVITY_TYPE;
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700919 } else {
920 mActivityType = APPLICATION_ACTIVITY_TYPE;
921 }
922 }
923
Craig Mautnera228ae92014-07-09 05:44:55 -0700924 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
Wale Ogunwalea0cd15e2017-02-01 15:33:08 -0800925 if (launchMode != LAUNCH_SINGLE_INSTANCE && launchMode != LAUNCH_SINGLE_TASK) {
Craig Mautnera228ae92014-07-09 05:44:55 -0700926 task.setTaskToAffiliateWith(taskToAffiliateWith);
927 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700928 }
929
Andrii Kulian02b7a832016-10-06 23:11:56 -0700930 /**
931 * @return Stack value from current task, null if there is no task.
932 */
Winson Chung55893332017-02-17 17:13:10 -0800933 <T extends ActivityStack> T getStack() {
934 return task != null ? (T) task.getStack() : null;
Andrii Kulian02b7a832016-10-06 23:11:56 -0700935 }
936
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800937 private int getStackId() {
938 return getStack() != null ? getStack().mStackId : INVALID_STACK_ID;
939 }
940
Craig Mautner5eda9b32013-07-02 11:58:16 -0700941 boolean changeWindowTranslucency(boolean toOpaque) {
942 if (fullscreen == toOpaque) {
943 return false;
944 }
Craig Mautner4addfc52013-06-25 08:05:45 -0700945
Craig Mautner5eda9b32013-07-02 11:58:16 -0700946 // Keep track of the number of fullscreen activities in this task.
947 task.numFullscreen += toOpaque ? +1 : -1;
948
949 fullscreen = toOpaque;
950 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -0700951 }
952
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700953 void takeFromHistory() {
954 if (inHistory) {
955 inHistory = false;
956 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800957 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700958 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700959 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700960 }
961 }
962
963 boolean isInHistory() {
964 return inHistory;
965 }
966
Wale Ogunwale7d701172015-03-11 15:36:30 -0700967 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700968 final ActivityStack stack = getStack();
969 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700970 }
971
Craig Mautner86d67a42013-05-14 10:34:38 -0700972 boolean isHomeActivity() {
973 return mActivityType == HOME_ACTIVITY_TYPE;
974 }
975
976 boolean isRecentsActivity() {
977 return mActivityType == RECENTS_ACTIVITY_TYPE;
978 }
979
Winson Chung83471632016-12-13 11:02:12 -0800980 boolean isAssistantActivity() {
981 return mActivityType == ASSISTANT_ACTIVITY_TYPE;
982 }
983
Craig Mautner86d67a42013-05-14 10:34:38 -0700984 boolean isApplicationActivity() {
985 return mActivityType == APPLICATION_ACTIVITY_TYPE;
986 }
987
Craig Mautner21d24a22014-04-23 11:45:37 -0700988 boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700989 return (info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY ||
990 info.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS) &&
991 (intent == null ||
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800992 (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -0700993 }
994
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800995 boolean isFocusable() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700996 return StackId.canReceiveKeys(task.getStackId()) || isAlwaysFocusable();
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800997 }
998
999 boolean isResizeable() {
Winson Chungd3395382016-12-13 11:49:09 -08001000 return ActivityInfo.isResizeableMode(info.resizeMode) || info.supportsPictureInPicture();
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001001 }
1002
Winson Chungd3395382016-12-13 11:49:09 -08001003 /**
1004 * @return whether this activity is non-resizeable or forced to be resizeable
1005 */
1006 boolean isNonResizableOrForcedResizable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -07001007 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -07001008 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -07001009 }
1010
Winson Chunge6308042016-10-31 09:24:01 -07001011 /**
Winson Chungd3395382016-12-13 11:49:09 -08001012 * @return whether this activity supports PiP multi-window and can be put in the pinned stack.
Winson Chunge6308042016-10-31 09:24:01 -07001013 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001014 boolean supportsPictureInPicture() {
Winson Chungd3395382016-12-13 11:49:09 -08001015 return service.mSupportsPictureInPicture && !isHomeActivity()
1016 && info.supportsPictureInPicture();
1017 }
1018
1019 /**
1020 * @return whether this activity supports split-screen multi-window and can be put in the docked
1021 * stack.
1022 */
1023 boolean supportsSplitScreen() {
1024 // An activity can not be docked even if it is considered resizeable because it only
1025 // supports picture-in-picture mode but has a non-resizeable resizeMode
1026 return service.mSupportsSplitScreenMultiWindow && supportsResizeableMultiWindow();
1027 }
1028
1029 /**
1030 * @return whether this activity supports freeform multi-window and can be put in the freeform
1031 * stack.
1032 */
1033 boolean supportsFreeform() {
1034 return service.mSupportsFreeformWindowManagement && supportsResizeableMultiWindow();
1035 }
1036
1037 /**
1038 * @return whether this activity supports non-PiP multi-window.
1039 */
1040 private boolean supportsResizeableMultiWindow() {
1041 return service.mSupportsMultiWindow && !isHomeActivity()
1042 && (ActivityInfo.isResizeableMode(info.resizeMode)
1043 || service.mForceResizableActivities);
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001044 }
1045
Winson Chunge6308042016-10-31 09:24:01 -07001046 /**
Winson Chungc2baac02017-01-11 13:34:47 -08001047 * @return whether this activity is currently allowed to enter PIP, throwing an exception if
Winson Chung4dabf232017-01-25 13:25:22 -08001048 * the activity is not currently visible and {@param noThrow} is not set.
Winson Chunge6308042016-10-31 09:24:01 -07001049 */
Winson Chung4dabf232017-01-25 13:25:22 -08001050 boolean checkEnterPictureInPictureState(String caller, boolean noThrow) {
Winson Chungf4ac0632017-03-17 12:34:12 -07001051 // Check app-ops and see if PiP is supported for this package
1052 if (!checkEnterPictureInPictureAppOpsState()) {
1053 return false;
1054 }
1055
Winson Chungbb348802017-01-30 12:01:45 -08001056 boolean isCurrentAppLocked = mStackSupervisor.getLockTaskModeState() != LOCK_TASK_MODE_NONE;
Winson Chungc2baac02017-01-11 13:34:47 -08001057 boolean isKeyguardLocked = service.isKeyguardLocked();
1058 boolean hasPinnedStack = mStackSupervisor.getStack(PINNED_STACK_ID) != null;
Winson Chungbb348802017-01-30 12:01:45 -08001059 // Don't return early if !isNotLocked, since we want to throw an exception if the activity
1060 // is in an incorrect state
Winson Chunge581ebf2017-02-21 08:25:03 -08001061 boolean isNotLockedOrOnKeyguard = !isKeyguardLocked && !isCurrentAppLocked;
Winson Chungc2baac02017-01-11 13:34:47 -08001062 switch (state) {
1063 case RESUMED:
Winson Chunge581ebf2017-02-21 08:25:03 -08001064 // When visible, allow entering PiP if the app is not locked. If it is over the
1065 // keyguard, then we will prompt to unlock in the caller before entering PiP.
1066 return !isCurrentAppLocked;
Winson Chungc2baac02017-01-11 13:34:47 -08001067 case PAUSING:
1068 case PAUSED:
Winson Chungbb348802017-01-30 12:01:45 -08001069 // When pausing, then only allow enter PiP as in the resume state, and in addition,
1070 // require that there is not an existing PiP activity and that the current system
1071 // state supports entering PiP
Winson Chunge581ebf2017-02-21 08:25:03 -08001072 return isNotLockedOrOnKeyguard && !hasPinnedStack
Winson Chungf4ac0632017-03-17 12:34:12 -07001073 && supportsPictureInPictureWhilePausing;
Winson Chungc2baac02017-01-11 13:34:47 -08001074 case STOPPING:
1075 // When stopping in a valid state, then only allow enter PiP as in the pause state.
1076 // Otherwise, fall through to throw an exception if the caller is trying to enter
1077 // PiP in an invalid stopping state.
1078 if (supportsPictureInPictureWhilePausing) {
Winson Chungf4ac0632017-03-17 12:34:12 -07001079 return isNotLockedOrOnKeyguard && !hasPinnedStack;
Winson Chungc2baac02017-01-11 13:34:47 -08001080 }
1081 default:
Winson Chung4dabf232017-01-25 13:25:22 -08001082 if (noThrow) {
1083 return false;
1084 } else {
1085 throw new IllegalStateException(caller
1086 + ": Current activity is not visible (state=" + state.name() + ") "
1087 + "r=" + this);
1088 }
Winson Chungb5c41b72016-12-07 15:00:47 -08001089 }
Winson Chunge6308042016-10-31 09:24:01 -07001090 }
1091
Winson Chung59fda9e2017-01-20 16:14:51 -08001092 /**
Winson Chungf4ac0632017-03-17 12:34:12 -07001093 * @return Whether AppOps allows this package to enter picture-in-picture.
Winson Chung59fda9e2017-01-20 16:14:51 -08001094 */
Winson Chungf4ac0632017-03-17 12:34:12 -07001095 private boolean checkEnterPictureInPictureAppOpsState() {
Winson Chung59fda9e2017-01-20 16:14:51 -08001096 try {
Winson Chungf4ac0632017-03-17 12:34:12 -07001097 return service.getAppOpsService().checkOperation(OP_PICTURE_IN_PICTURE,
Winson Chung59fda9e2017-01-20 16:14:51 -08001098 appInfo.uid, packageName) == MODE_ALLOWED;
1099 } catch (RemoteException e) {
1100 // Local call
1101 }
1102 return false;
1103 }
1104
Wale Ogunwale6cae7652015-12-26 07:36:26 -08001105 boolean isAlwaysFocusable() {
1106 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001107 }
1108
Jorim Jaggife762342016-10-13 14:33:27 +02001109 /**
1110 * @return true if the activity contains windows that have
1111 * {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set
1112 */
1113 boolean hasShowWhenLockedWindows() {
1114 return service.mWindowManager.containsShowWhenLockedWindow(appToken);
1115 }
1116
1117 /**
1118 * @return true if the activity contains windows that have
1119 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
1120 */
1121 boolean hasDismissKeyguardWindows() {
1122 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
1123 }
1124
Wale Ogunwale7d701172015-03-11 15:36:30 -07001125 void makeFinishingLocked() {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001126 if (!finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001127 final ActivityStack stack = getStack();
1128 if (stack != null && this == stack.getVisibleBehindActivity()) {
Jose Lima34ff4922014-08-18 15:19:41 -07001129 // A finishing activity should not remain as visible in the background
1130 mStackSupervisor.requestVisibleBehindLocked(this, false);
1131 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001132 finishing = true;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001133 if (stopped) {
1134 clearOptionsLocked();
1135 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001136
1137 if (service != null) {
1138 service.mTaskChangeNotificationController.notifyTaskStackChanged();
1139 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001140 }
1141 }
1142
Dianne Hackborn7e269642010-08-25 19:50:20 -07001143 UriPermissionOwner getUriPermissionsLocked() {
1144 if (uriPermissions == null) {
1145 uriPermissions = new UriPermissionOwner(service, this);
1146 }
1147 return uriPermissions;
1148 }
1149
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001150 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 int requestCode, int resultCode,
1152 Intent resultData) {
1153 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -05001154 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001156 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158 results.add(r);
1159 }
1160
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001161 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 int requestCode) {
1163 if (results != null) {
1164 for (int i=results.size()-1; i>=0; i--) {
1165 ActivityResult r = (ActivityResult)results.get(i);
1166 if (r.mFrom != from) continue;
1167 if (r.mResultWho == null) {
1168 if (resultWho != null) continue;
1169 } else {
1170 if (!r.mResultWho.equals(resultWho)) continue;
1171 }
1172 if (r.mRequestCode != requestCode) continue;
1173
1174 results.remove(i);
1175 }
1176 }
1177 }
1178
Andrii Kulian21713ac2016-10-12 22:05:05 -07001179 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001181 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 newIntents.add(intent);
1184 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001185
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001186 /**
1187 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1188 * method will be called at the proper time.
1189 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001190 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001191 // The activity now gets access to the data associated with this Intent.
1192 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001193 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001194 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001195 boolean unsent = true;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001196 final ActivityStack stack = getStack();
Wale Ogunwale826c7062016-09-13 08:25:54 -07001197 final boolean isTopActivityInStack =
1198 stack != null && stack.topRunningActivityLocked() == this;
1199 final boolean isTopActivityWhileSleeping =
1200 service.isSleepingLocked() && isTopActivityInStack;
Wale Ogunwale826c7062016-09-13 08:25:54 -07001201
1202 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001203 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1204 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001205 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001206 if ((state == ActivityState.RESUMED || state == ActivityState.PAUSED
1207 || isTopActivityWhileSleeping) && app != null && app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001208 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001209 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1210 ar.add(rintent);
Wale Ogunwale826c7062016-09-13 08:25:54 -07001211 app.thread.scheduleNewIntent(
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001212 ar, appToken, state == ActivityState.PAUSED /* andPause */);
Craig Mautner86d67a42013-05-14 10:34:38 -07001213 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001214 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001215 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001216 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001217 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001218 }
1219 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001220 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001221 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001222 }
1223 }
1224
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001225 void updateOptionsLocked(ActivityOptions options) {
1226 if (options != null) {
1227 if (pendingOptions != null) {
1228 pendingOptions.abort();
1229 }
1230 pendingOptions = options;
1231 }
1232 }
1233
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001234 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001235 if (pendingOptions != null
1236 && pendingOptions.getAnimationType() != ActivityOptions.ANIM_SCENE_TRANSITION) {
Michael Jurka21385cd2012-05-03 10:57:31 -07001237 final int animationType = pendingOptions.getAnimationType();
1238 switch (animationType) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001239 case ActivityOptions.ANIM_CUSTOM:
1240 service.mWindowManager.overridePendingAppTransition(
1241 pendingOptions.getPackageName(),
1242 pendingOptions.getCustomEnterResId(),
Dianne Hackborn84375872012-06-01 19:03:50 -07001243 pendingOptions.getCustomExitResId(),
1244 pendingOptions.getOnAnimationStartListener());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001245 break;
Chet Haase10e23ab2015-02-11 15:08:38 -08001246 case ActivityOptions.ANIM_CLIP_REVEAL:
1247 service.mWindowManager.overridePendingAppTransitionClipReveal(
1248 pendingOptions.getStartX(), pendingOptions.getStartY(),
1249 pendingOptions.getWidth(), pendingOptions.getHeight());
1250 if (intent.getSourceBounds() == null) {
1251 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1252 pendingOptions.getStartY(),
1253 pendingOptions.getStartX()+pendingOptions.getWidth(),
1254 pendingOptions.getStartY()+pendingOptions.getHeight()));
1255 }
1256 break;
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001257 case ActivityOptions.ANIM_SCALE_UP:
1258 service.mWindowManager.overridePendingAppTransitionScaleUp(
1259 pendingOptions.getStartX(), pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001260 pendingOptions.getWidth(), pendingOptions.getHeight());
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001261 if (intent.getSourceBounds() == null) {
1262 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1263 pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001264 pendingOptions.getStartX()+pendingOptions.getWidth(),
1265 pendingOptions.getStartY()+pendingOptions.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001266 }
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001267 break;
Michael Jurka832cb222012-04-13 09:32:47 -07001268 case ActivityOptions.ANIM_THUMBNAIL_SCALE_UP:
1269 case ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN:
1270 boolean scaleUp = (animationType == ActivityOptions.ANIM_THUMBNAIL_SCALE_UP);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001271 service.mWindowManager.overridePendingAppTransitionThumb(
1272 pendingOptions.getThumbnail(),
1273 pendingOptions.getStartX(), pendingOptions.getStartY(),
Michael Jurka21385cd2012-05-03 10:57:31 -07001274 pendingOptions.getOnAnimationStartListener(),
Michael Jurka832cb222012-04-13 09:32:47 -07001275 scaleUp);
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001276 if (intent.getSourceBounds() == null) {
1277 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1278 pendingOptions.getStartY(),
1279 pendingOptions.getStartX()
1280 + pendingOptions.getThumbnail().getWidth(),
1281 pendingOptions.getStartY()
1282 + pendingOptions.getThumbnail().getHeight()));
1283 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001284 break;
Winson Chunga4ccb862014-08-22 15:26:27 -07001285 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP:
1286 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN:
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001287 final AppTransitionAnimationSpec[] specs = pendingOptions.getAnimSpecs();
1288 if (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN
1289 && specs != null) {
1290 service.mWindowManager.overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -07001291 specs, pendingOptions.getOnAnimationStartListener(),
1292 pendingOptions.getAnimationFinishedListener(), false);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001293 } else {
1294 service.mWindowManager.overridePendingAppTransitionAspectScaledThumb(
1295 pendingOptions.getThumbnail(),
1296 pendingOptions.getStartX(), pendingOptions.getStartY(),
1297 pendingOptions.getWidth(), pendingOptions.getHeight(),
1298 pendingOptions.getOnAnimationStartListener(),
1299 (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP));
1300 if (intent.getSourceBounds() == null) {
1301 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1302 pendingOptions.getStartY(),
1303 pendingOptions.getStartX() + pendingOptions.getWidth(),
1304 pendingOptions.getStartY() + pendingOptions.getHeight()));
1305 }
Winson Chunga4ccb862014-08-22 15:26:27 -07001306 }
1307 break;
Craig Mautner233ceee2014-05-09 17:05:11 -07001308 default:
1309 Slog.e(TAG, "applyOptionsLocked: Unknown animationType=" + animationType);
1310 break;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001311 }
1312 pendingOptions = null;
1313 }
1314 }
1315
Adam Powellcfbe9be2013-11-06 14:58:58 -08001316 ActivityOptions getOptionsForTargetActivityLocked() {
1317 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1318 }
1319
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001320 void clearOptionsLocked() {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001321 if (pendingOptions != null) {
1322 pendingOptions.abort();
1323 pendingOptions = null;
1324 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001325 }
1326
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001327 ActivityOptions takeOptionsLocked() {
1328 ActivityOptions opts = pendingOptions;
1329 pendingOptions = null;
1330 return opts;
1331 }
1332
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001333 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001334 if (uriPermissions != null) {
1335 uriPermissions.removeUriPermissionsLocked();
1336 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001337 }
1338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339
1340 void pauseKeyDispatchingLocked() {
1341 if (!keysPaused) {
1342 keysPaused = true;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001343 mWindowContainerController.pauseKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 }
1345 }
1346
1347 void resumeKeyDispatchingLocked() {
1348 if (keysPaused) {
1349 keysPaused = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001350 mWindowContainerController.resumeKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 }
1352 }
1353
Winson Chung740c3ac2014-11-12 16:14:38 -08001354 void updateThumbnailLocked(Bitmap newThumbnail, CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001355 if (newThumbnail != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001356 if (DEBUG_THUMBNAILS) Slog.i(TAG_THUMBNAILS,
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001357 "Setting thumbnail of " + this + " to " + newThumbnail);
Winsonc809cbb2015-11-02 12:06:15 -08001358 boolean thumbnailUpdated = task.setLastThumbnailLocked(newThumbnail);
Winson Chung096f36b2014-08-20 15:39:01 -07001359 if (thumbnailUpdated && isPersistable()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001360 service.notifyTaskPersisterLocked(task, false);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001361 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001362 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001363 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001364 }
1365
Andrii Kulian21713ac2016-10-12 22:05:05 -07001366 final Bitmap screenshotActivityLocked() {
1367 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivityLocked: " + this);
Jorim Jaggi02886a82016-12-06 09:10:06 -08001368
1369 if (ENABLE_TASK_SNAPSHOTS) {
1370 // No need to screenshot if snapshots are enabled.
1371 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS,
1372 "\tSnapshots are enabled, abort taking screenshot");
1373 return null;
1374 }
1375
Andrii Kulian21713ac2016-10-12 22:05:05 -07001376 if (noDisplay) {
1377 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
1378 return null;
1379 }
1380
1381 final ActivityStack stack = getStack();
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001382 if (stack.isHomeOrRecentsStack()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001383 // This is an optimization -- since we never show Home or Recents within Recents itself,
1384 // we can just go ahead and skip taking the screenshot if this is the home stack.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001385 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, stack.getStackId() == HOME_STACK_ID ?
1386 "\tHome stack" : "\tRecents stack");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001387 return null;
1388 }
1389
1390 int w = service.mThumbnailWidth;
1391 int h = service.mThumbnailHeight;
1392
1393 if (w <= 0) {
1394 Slog.e(TAG, "\tInvalid thumbnail dimensions: " + w + "x" + h);
1395 return null;
1396 }
1397
1398 if (stack.mStackId == DOCKED_STACK_ID && mStackSupervisor.mIsDockMinimized) {
1399 // When the docked stack is minimized its app windows are cropped significantly so any
1400 // screenshot taken will not display the apps contain. So, we avoid taking a screenshot
1401 // in that case.
1402 if (DEBUG_SCREENSHOTS) Slog.e(TAG, "\tIn minimized docked stack");
1403 return null;
1404 }
1405
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001406 float scale = 0;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001407 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
1408
1409 // When this flag is set, we currently take the fullscreen screenshot of the activity but
1410 // scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to use within
1411 // SystemUI while keeping memory usage low.
1412 if (TAKE_FULLSCREEN_SCREENSHOTS) {
1413 w = h = -1;
1414 scale = service.mFullscreenThumbnailScale;
1415 }
1416
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001417 return mWindowContainerController.screenshotApplications(getDisplayId(), w, h, scale);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001418 }
1419
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001420 void setVisibility(boolean visible) {
1421 mWindowContainerController.setVisibility(visible);
1422 }
1423
1424 // TODO: Look into merging with #setVisibility()
Andrii Kulian21713ac2016-10-12 22:05:05 -07001425 void setVisible(boolean newVisible) {
1426 visible = newVisible;
1427 if (!visible && mUpdateTaskThumbnailWhenHidden) {
1428 updateThumbnailLocked(screenshotActivityLocked(), null /* description */);
1429 mUpdateTaskThumbnailWhenHidden = false;
1430 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001431 mWindowContainerController.setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001432 final ArrayList<ActivityContainer> containers = mChildContainers;
1433 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
1434 final ActivityContainer container = containers.get(containerNdx);
1435 container.setVisible(visible);
1436 }
1437 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1438 }
1439
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001440 void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
1441 mWindowContainerController.notifyAppResumed(wasStopped, allowSavedSurface);
1442 }
1443
1444 void notifyUnknownVisibilityLaunched() {
1445 mWindowContainerController.notifyUnknownVisibilityLaunched();
1446 }
1447
Jorim Jaggi241ae102016-11-02 21:57:33 -07001448 /**
1449 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1450 * might have on the visibility
1451 *
1452 * @see {@link ActivityStack#checkKeyguardVisibility}
1453 */
1454 boolean shouldBeVisibleIgnoringKeyguard(boolean behindTranslucentActivity,
1455 boolean stackVisibleBehind, ActivityRecord visibleBehind,
1456 boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001457 if (!okToShowLocked()) {
1458 return false;
1459 }
1460
1461 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1462 // but must be drawn initially for the animation as though they were visible.
1463 final boolean activityVisibleBehind =
1464 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == this;
1465
1466 boolean isVisible =
1467 !behindFullscreenActivity || mLaunchTaskBehind || activityVisibleBehind;
1468
1469 if (service.mSupportsLeanbackOnly && isVisible && isRecentsActivity()) {
1470 // On devices that support leanback only (Android TV), Recents activity can only be
1471 // visible if the home stack is the focused stack or we are in split-screen mode.
1472 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1473 || mStackSupervisor.isFocusedStack(getStack());
1474 }
1475
1476 return isVisible;
1477 }
1478
1479 void makeVisibleIfNeeded(ActivityRecord starting) {
1480 // This activity is not currently visible, but is running. Tell it to become visible.
1481 if (state == ActivityState.RESUMED || this == starting) {
1482 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1483 "Not making visible, r=" + this + " state=" + state + " starting=" + starting);
1484 return;
1485 }
1486
1487 // If this activity is paused, tell it to now show its window.
1488 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1489 "Making visible and scheduling visibility: " + this);
1490 final ActivityStack stack = getStack();
1491 try {
1492 if (stack.mTranslucentActivityWaiting != null) {
1493 updateOptionsLocked(returningOptions);
1494 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1495 }
1496 setVisible(true);
1497 sleeping = false;
1498 app.pendingUiClean = true;
1499 app.thread.scheduleWindowVisibility(appToken, true /* showWindow */);
1500 // The activity may be waiting for stop, but that is no longer appropriate for it.
1501 mStackSupervisor.mStoppingActivities.remove(this);
1502 mStackSupervisor.mGoingToSleepActivities.remove(this);
1503 } catch (Exception e) {
1504 // Just skip on any failure; we'll make it visible when it next restarts.
1505 Slog.w(TAG, "Exception thrown making visibile: " + intent.getComponent(), e);
1506 }
1507 handleAlreadyVisible();
1508 }
1509
1510 boolean handleAlreadyVisible() {
1511 stopFreezingScreenLocked(false);
1512 try {
1513 if (returningOptions != null) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001514 app.thread.scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001515 }
1516 } catch(RemoteException e) {
1517 }
1518 return state == ActivityState.RESUMED;
1519 }
1520
1521 static void activityResumedLocked(IBinder token) {
1522 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1523 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1524 if (r != null) {
1525 r.icicle = null;
1526 r.haveState = false;
1527 }
1528 }
1529
1530 /**
1531 * Once we know that we have asked an application to put an activity in the resumed state
1532 * (either by launching it or explicitly telling it), this function updates the rest of our
1533 * state to match that fact.
1534 */
1535 void completeResumeLocked() {
1536 final boolean wasVisible = visible;
1537 visible = true;
1538 if (!wasVisible) {
1539 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1540 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1541 }
1542 idle = false;
1543 results = null;
1544 newIntents = null;
1545 stopped = false;
1546
1547 if (isHomeActivity()) {
1548 ProcessRecord app = task.mActivities.get(0).app;
1549 if (app != null && app != service.mHomeProcess) {
1550 service.mHomeProcess = app;
1551 }
1552 }
1553
1554 if (nowVisible) {
1555 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1556 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001557 }
1558
1559 // Schedule an idle timeout in case the app doesn't do it for us.
1560 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1561
1562 mStackSupervisor.reportResumedActivityLocked(this);
1563
1564 resumeKeyDispatchingLocked();
1565 final ActivityStack stack = getStack();
1566 stack.mNoAnimActivities.clear();
1567
1568 // Mark the point when the activity is resuming
1569 // TODO: To be more accurate, the mark should be before the onCreate,
1570 // not after the onResume. But for subsequent starts, onResume is fine.
1571 if (app != null) {
1572 cpuTimeAtResume = service.mProcessCpuTracker.getCpuTimeForPid(app.pid);
1573 } else {
1574 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1575 }
1576
1577 returningOptions = null;
1578
1579 if (stack.getVisibleBehindActivity() == this) {
1580 // When resuming an activity, require it to call requestVisibleBehind() again.
1581 stack.setVisibleBehindActivity(null /* ActivityRecord */);
1582 }
1583 mStackSupervisor.checkReadyForSleepLocked();
1584 }
1585
1586 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1587 CharSequence description) {
1588 final ActivityStack stack = getStack();
1589 if (state != ActivityState.STOPPING) {
1590 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
1591 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1592 return;
1593 }
1594 if (newPersistentState != null) {
1595 persistentState = newPersistentState;
1596 service.notifyTaskPersisterLocked(task, false);
1597 }
1598 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001599
Andrii Kulian21713ac2016-10-12 22:05:05 -07001600 if (newIcicle != null) {
1601 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1602 // the state.
1603 icicle = newIcicle;
1604 haveState = true;
1605 launchCount = 0;
1606 updateThumbnailLocked(null /* newThumbnail */, description);
1607 }
1608 if (!stopped) {
1609 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
1610 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1611 stopped = true;
1612 state = ActivityState.STOPPED;
1613
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001614 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001615
1616 if (stack.getVisibleBehindActivity() == this) {
1617 mStackSupervisor.requestVisibleBehindLocked(this, false /* visible */);
1618 }
1619 if (finishing) {
1620 clearOptionsLocked();
1621 } else {
1622 if (deferRelaunchUntilPaused) {
1623 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
1624 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1625 } else {
1626 mStackSupervisor.updatePreviousProcessLocked(this);
1627 }
1628 }
1629 }
1630 }
1631
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001632 void startLaunchTickingLocked() {
1633 if (ActivityManagerService.IS_USER_BUILD) {
1634 return;
1635 }
1636 if (launchTickTime == 0) {
1637 launchTickTime = SystemClock.uptimeMillis();
1638 continueLaunchTickingLocked();
1639 }
1640 }
1641
1642 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001643 if (launchTickTime == 0) {
1644 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001645 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001646
Andrii Kulian02b7a832016-10-06 23:11:56 -07001647 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001648 if (stack == null) {
1649 return false;
1650 }
1651
1652 Message msg = stack.mHandler.obtainMessage(ActivityStack.LAUNCH_TICK_MSG, this);
1653 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1654 stack.mHandler.sendMessageDelayed(msg, ActivityStack.LAUNCH_TICK);
1655 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001656 }
1657
1658 void finishLaunchTickingLocked() {
1659 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001660 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001661 if (stack != null) {
1662 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1663 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001664 }
1665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 // IApplicationToken
1667
1668 public boolean mayFreezeScreenLocked(ProcessRecord app) {
1669 // Only freeze the screen if this activity is currently attached to
1670 // an application, and that application is not blocked or unresponding.
1671 // In any other case, we can't count on getting the screen unfrozen,
1672 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -08001673 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
1677 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001678 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
1680 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 public void stopFreezingScreenLocked(boolean force) {
1683 if (force || frozenBeforeDestroy) {
1684 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001685 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
1687 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001688
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001689 public void reportFullyDrawnLocked() {
1690 final long curTime = SystemClock.uptimeMillis();
1691 if (displayStartTime != 0) {
1692 reportLaunchTimeLocked(curTime);
1693 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001694 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001695 if (fullyDrawnStartTime != 0 && stack != null) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001696 final long thisTime = curTime - fullyDrawnStartTime;
1697 final long totalTime = stack.mFullyDrawnStartTime != 0
1698 ? (curTime - stack.mFullyDrawnStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001699 if (SHOW_ACTIVITY_START_TIME) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001700 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001701 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME,
1702 userId, System.identityHashCode(this), shortComponentName,
1703 thisTime, totalTime);
1704 StringBuilder sb = service.mStringBuilder;
1705 sb.setLength(0);
1706 sb.append("Fully drawn ");
1707 sb.append(shortComponentName);
1708 sb.append(": ");
1709 TimeUtils.formatDuration(thisTime, sb);
1710 if (thisTime != totalTime) {
1711 sb.append(" (total ");
1712 TimeUtils.formatDuration(totalTime, sb);
1713 sb.append(")");
1714 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001715 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001716 }
1717 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001718 //service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001719 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001720 stack.mFullyDrawnStartTime = 0;
1721 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001722 fullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001723 }
1724
1725 private void reportLaunchTimeLocked(final long curTime) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001726 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001727 if (stack == null) {
1728 return;
1729 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001730 final long thisTime = curTime - displayStartTime;
1731 final long totalTime = stack.mLaunchStartTime != 0
1732 ? (curTime - stack.mLaunchStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001733 if (SHOW_ACTIVITY_START_TIME) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001734 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001735 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME,
1736 userId, System.identityHashCode(this), shortComponentName,
1737 thisTime, totalTime);
1738 StringBuilder sb = service.mStringBuilder;
1739 sb.setLength(0);
1740 sb.append("Displayed ");
1741 sb.append(shortComponentName);
1742 sb.append(": ");
1743 TimeUtils.formatDuration(thisTime, sb);
1744 if (thisTime != totalTime) {
1745 sb.append(" (total ");
1746 TimeUtils.formatDuration(totalTime, sb);
1747 sb.append(")");
1748 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001749 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001750 }
1751 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
1752 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001753 //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001754 }
1755 displayStartTime = 0;
1756 stack.mLaunchStartTime = 0;
1757 }
1758
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001759 @Override
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001760 public void onStartingWindowDrawn() {
1761 synchronized (service) {
1762 mStackSupervisor.mActivityMetricsLogger.notifyStartingWindowDrawn(getStackId());
1763 }
1764 }
1765
1766 @Override
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001767 public void onWindowsDrawn() {
1768 synchronized (service) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001769 mStackSupervisor.mActivityMetricsLogger.notifyWindowsDrawn(getStackId());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001770 if (displayStartTime != 0) {
1771 reportLaunchTimeLocked(SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001773 mStackSupervisor.sendWaitingVisibleReportLocked(this);
1774 startTime = 0;
1775 finishLaunchTickingLocked();
1776 if (task != null) {
1777 task.hasBeenVisible = true;
1778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
1780 }
1781
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001782 @Override
1783 public void onWindowsVisible() {
1784 synchronized (service) {
1785 mStackSupervisor.reportActivityVisibleLocked(this);
1786 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
1787 if (!nowVisible) {
1788 nowVisible = true;
1789 lastVisibleTime = SystemClock.uptimeMillis();
1790 if (!idle) {
1791 // Instead of doing the full stop routine here, let's just hide any activities
1792 // we now can, and let them stop when the normal idle happens.
Winson Chung4dabf232017-01-25 13:25:22 -08001793 mStackSupervisor.processStoppingActivitiesLocked(null /* idleActivity */,
1794 false /* remove */, true /* processPausingActivities */);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001795 } else {
1796 // If this activity was already idle, then we now need to make sure we perform
1797 // the full stop of any activities that are waiting to do so. This is because
1798 // we won't do that while they are still waiting for this one to become visible.
1799 final int size = mStackSupervisor.mWaitingVisibleActivities.size();
1800 if (size > 0) {
1801 for (int i = 0; i < size; i++) {
1802 ActivityRecord r = mStackSupervisor.mWaitingVisibleActivities.get(i);
1803 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
1804 }
1805 mStackSupervisor.mWaitingVisibleActivities.clear();
1806 mStackSupervisor.scheduleIdleLocked();
1807 }
1808 }
1809 service.scheduleAppGcsLocked();
1810 }
1811 }
1812 }
1813
1814 @Override
1815 public void onWindowsGone() {
1816 synchronized (service) {
1817 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
1818 nowVisible = false;
1819 }
1820 }
1821
1822 @Override
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07001823 public boolean keyDispatchingTimedOut(String reason, int windowPid) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001824 ActivityRecord anrActivity;
1825 ProcessRecord anrApp;
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07001826 boolean windowFromSameProcessAsActivity;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001827 synchronized (service) {
1828 anrActivity = getWaitingHistoryRecordLocked();
1829 anrApp = app;
Brian Carlstrom7b0f2e82017-03-31 00:24:18 -07001830 windowFromSameProcessAsActivity =
1831 app == null || app.pid == windowPid || windowPid == -1;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001832 }
Wale Ogunwale7402ddf2017-03-29 12:58:24 -07001833 if (windowFromSameProcessAsActivity) {
1834 return service.inputDispatchingTimedOut(anrApp, anrActivity, this, false, reason);
1835 } else {
1836 // In this case another process added windows using this activity token. So, we call the
1837 // generic service input dispatch timed out method so that the right process is blamed.
1838 return service.inputDispatchingTimedOut(windowPid, false /* aboveSystem */, reason) < 0;
1839 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001840 }
1841
1842 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001843 // First find the real culprit... if this activity is waiting for
1844 // another activity to start or has stopped, then the key dispatching
1845 // timeout should not be caused by this.
Wale Ogunwale16a62792015-04-01 11:50:31 -07001846 if (mStackSupervisor.mWaitingVisibleActivities.contains(this) || stopped) {
Craig Mautner8f2adcb2014-04-07 22:21:33 +00001847 final ActivityStack stack = mStackSupervisor.getFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001848 // Try to use the one which is closest to top.
1849 ActivityRecord r = stack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001851 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001853 if (r != null) {
1854 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 }
1856 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001857 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
1859
Chong Zhang87761972016-08-22 13:53:24 -07001860 /** Checks whether the activity should be shown for current user. */
1861 public boolean okToShowLocked() {
1862 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
1863 || (mStackSupervisor.isCurrentProfileLocked(userId)
1864 && !service.mUserController.isUserStoppingOrShuttingDownLocked(userId));
1865 }
1866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 /**
1868 * This method will return true if the activity is either visible, is becoming visible, is
1869 * currently pausing, or is resumed.
1870 */
1871 public boolean isInterestingToUserLocked() {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001872 return visible || nowVisible || state == ActivityState.PAUSING ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 state == ActivityState.RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001874 }
1875
Wale Ogunwale3e997362016-09-06 10:37:56 -07001876 void setSleeping(boolean _sleeping) {
1877 setSleeping(_sleeping, false);
1878 }
1879
1880 void setSleeping(boolean _sleeping, boolean force) {
1881 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001882 return;
1883 }
1884 if (app != null && app.thread != null) {
1885 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001886 app.thread.scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001887 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
1888 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001889 }
1890 sleeping = _sleeping;
1891 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001892 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001893 }
1894 }
1895 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08001896
Craig Mautnerd2328952013-03-05 12:46:26 -08001897 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001898 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08001899 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001900 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001901 }
1902 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001903 final int activityNdx = task.mActivities.indexOf(r);
1904 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001905 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001906 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001907 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08001908 }
1909
1910 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001911 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001912 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001913 }
1914
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001915 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001916 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1917 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001918 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08001919 }
1920 return null;
1921 }
1922
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001923 /**
1924 * @return display id to which this record is attached, -1 if not attached.
1925 */
1926 int getDisplayId() {
1927 final ActivityStack stack = getStack();
1928 if (stack == null) {
1929 return -1;
1930 }
1931 return stack.mDisplayId;
1932 }
1933
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001934 final boolean isDestroyable() {
1935 if (finishing || app == null || state == ActivityState.DESTROYING
1936 || state == ActivityState.DESTROYED) {
1937 // This would be redundant.
1938 return false;
1939 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001940 final ActivityStack stack = getStack();
1941 if (stack == null || this == stack.mResumedActivity || this == stack.mPausingActivity
1942 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001943 // We're not ready for this kind of thing.
1944 return false;
1945 }
1946 if (visible) {
1947 // The user would notice this!
1948 return false;
1949 }
1950 return true;
1951 }
1952
Winson Chung3bad5cc02014-08-19 17:44:32 -07001953 private static String createImageFilename(long createTime, int taskId) {
1954 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001955 TaskPersister.IMAGE_EXTENSION;
1956 }
1957
Craig Mautner648f69b2014-09-18 14:16:26 -07001958 void setTaskDescription(TaskDescription _taskDescription) {
1959 Bitmap icon;
1960 if (_taskDescription.getIconFilename() == null &&
1961 (icon = _taskDescription.getIcon()) != null) {
1962 final String iconFilename = createImageFilename(createTime, task.taskId);
Winson Chungc8408b82017-01-25 17:58:56 -08001963 final File iconFile = new File(TaskPersister.getUserImagesDir(task.userId),
1964 iconFilename);
Suprabh Shukla23593142015-11-03 17:31:15 -08001965 final String iconFilePath = iconFile.getAbsolutePath();
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001966 service.mRecentTasks.saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08001967 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07001968 }
1969 taskDescription = _taskDescription;
1970 }
1971
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001972 void setVoiceSessionLocked(IVoiceInteractionSession session) {
1973 voiceSession = session;
1974 pendingVoiceInteractionStart = false;
1975 }
1976
1977 void clearVoiceSessionLocked() {
1978 voiceSession = null;
1979 pendingVoiceInteractionStart = false;
1980 }
1981
Jorim Jaggi02886a82016-12-06 09:10:06 -08001982 void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch) {
Jorim Jaggi70176432017-01-18 12:52:13 +01001983 if (mWindowContainerController == null) {
1984 return;
1985 }
Wale Ogunwale3b232392016-05-13 15:37:13 -07001986 final CompatibilityInfo compatInfo =
1987 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001988 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
1989 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
Jorim Jaggi02886a82016-12-06 09:10:06 -08001990 prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning());
Wale Ogunwale3b232392016-05-13 15:37:13 -07001991 if (shown) {
1992 mStartingWindowState = STARTING_WINDOW_SHOWN;
1993 }
1994 }
1995
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001996 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
1997 if (state == ActivityState.INITIALIZING
1998 && mStartingWindowState == STARTING_WINDOW_SHOWN
1999 && behindFullscreenActivity) {
2000 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
2001 mStartingWindowState = STARTING_WINDOW_REMOVED;
2002 mWindowContainerController.removeStartingWindow();
2003 }
2004 }
2005
2006 int getRequestedOrientation() {
2007 return mWindowContainerController.getOrientation();
2008 }
2009
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002010 void setRequestedOrientation(int requestedOrientation) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002011 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08002012 final Configuration displayConfig =
2013 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
2014
2015 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
2016 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002017 if (config != null) {
2018 frozenBeforeDestroy = true;
2019 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
2020 false /* deferResume */, displayId)) {
2021 mStackSupervisor.resumeFocusedStackTopActivityLocked();
2022 }
2023 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07002024 service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
2025 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07002026 }
2027
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01002028 void setDisablePreviewScreenshots(boolean disable) {
2029 mWindowContainerController.setDisablePreviewScreenshots(disable);
2030 }
2031
Bryce Leea163b762017-01-24 11:05:01 -08002032 /**
2033 * Set the last reported global configuration to the client. Should be called whenever a new
2034 * global configuration is sent to the client for this activity.
2035 */
2036 void setLastReportedGlobalConfiguration(@NonNull Configuration config) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002037 mLastReportedConfiguration.setTo(config);
2038 }
2039
Bryce Leea163b762017-01-24 11:05:01 -08002040 /**
2041 * Set the last reported merged override configuration to the client. Should be called whenever
2042 * a new merged configuration is sent to the client for this activity.
2043 */
2044 void setLastReportedMergedOverrideConfiguration(@NonNull Configuration config) {
2045 mLastReportedOverrideConfiguration.setTo(config);
2046 }
2047
Andrii Kulian21713ac2016-10-12 22:05:05 -07002048 /** Call when override config was sent to the Window Manager to update internal records. */
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002049 // TODO(b/36505427): Why do we set last reported based on sending the config to WM? Seems like
2050 // we should only set this when we actually report to the activity which is what the method
2051 // setLastReportedMergedOverrideConfiguration() does. Investigate if this is really needed.
Andrii Kulian21713ac2016-10-12 22:05:05 -07002052 void onOverrideConfigurationSent() {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002053 mLastReportedOverrideConfiguration.setTo(getMergedOverrideConfiguration());
2054 }
2055
2056 @Override
2057 void onOverrideConfigurationChanged(Configuration overrideConfiguration) {
2058 super.onOverrideConfigurationChanged(overrideConfiguration);
2059 if (mWindowContainerController != null) {
2060 mWindowContainerController.onOverrideConfigurationChanged(
2061 overrideConfiguration, mBounds);
2062 // TODO(b/36505427): Can we consolidate the call points of onOverrideConfigurationSent()
2063 // to just use this method instead?
2064 onOverrideConfigurationSent();
2065 }
2066 }
2067
2068 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
2069 private boolean updateOverrideConfiguration() {
2070 computeBounds(mTmpBounds);
2071 if (mTmpBounds.equals(mBounds)) {
2072 return false;
2073 }
2074 mBounds.set(mTmpBounds);
2075 // Bounds changed...update configuration to match.
2076 mTmpConfig1.unset();
2077 task.computeOverrideConfiguration(mTmpConfig1, mBounds, null /* insetBounds */,
2078 false /* overrideWidth */, false /* overrideHeight */);
2079 onOverrideConfigurationChanged(mTmpConfig1);
2080 return true;
2081 }
2082
2083 /** Computes the override configuration for this activity */
2084 // TODO(b/36505427): Consider moving this method and similar ones to ConfigurationContainer.
2085 private void computeBounds(Rect outBounds) {
2086 outBounds.setEmpty();
2087 final float maxAspectRatio = info.maxAspectRatio;
2088 final ActivityStack stack = getStack();
2089 if ((task != null && !task.mFullscreen) || maxAspectRatio == 0 || stack == null) {
2090 // We don't set override configuration if that activity task isn't fullscreen. I.e. the
2091 // activity is in multi-window mode. Or, there isn't a max aspect ratio specified for
2092 // the activity.
2093 return;
2094 }
2095
2096 stack.getDisplaySize(mTmpPoint);
2097 int maxActivityWidth = mTmpPoint.x;
2098 int maxActivityHeight = mTmpPoint.y;
2099 if (mTmpPoint.x < mTmpPoint.y) {
2100 // Width is the shorter side, so we use that to figure-out what the max. height should
2101 // be given the aspect ratio.
2102 maxActivityHeight = (int) ((maxActivityWidth * maxAspectRatio) + 0.5f);
2103 } else {
2104 // Height is the shorter side, so we use that to figure-out what the max. width should
2105 // be given the aspect ratio.
2106 maxActivityWidth = (int) ((maxActivityHeight * maxAspectRatio) + 0.5f);
2107 }
2108
2109 if (mTmpPoint.x <= maxActivityWidth && mTmpPoint.y <= maxActivityHeight) {
2110 // The display matches or is less than the activity aspect ratio, so nothing else to do.
2111 return;
2112 }
2113
2114 // Compute configuration based on max supported width and height.
2115 outBounds.set(0, 0, maxActivityWidth, maxActivityHeight);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002116 }
2117
2118 /**
2119 * Make sure the given activity matches the current configuration. Returns false if the activity
2120 * had to be destroyed. Returns true if the configuration is the same, or the activity will
2121 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
2122 * correct configuration and all other bookkeeping is handled.
2123 */
2124 boolean ensureActivityConfigurationLocked(int globalChanges, boolean preserveWindow) {
2125 final ActivityStack stack = getStack();
2126 if (stack.mConfigWillChange) {
2127 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2128 "Skipping config check (will change): " + this);
2129 return true;
2130 }
2131
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002132 // We don't worry about activities that are finishing.
2133 if (finishing) {
2134 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2135 "Configuration doesn't matter in finishing " + this);
2136 stopFreezingScreenLocked(false);
2137 return true;
2138 }
2139
Andrii Kulian21713ac2016-10-12 22:05:05 -07002140 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2141 "Ensuring correct configuration: " + this);
2142
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002143 final int newDisplayId = getDisplayId();
2144 final boolean displayChanged = mLastReportedDisplayId != newDisplayId;
2145 if (displayChanged) {
2146 mLastReportedDisplayId = newDisplayId;
2147 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002148 // TODO(b/36505427): Is there a better place to do this?
2149 updateOverrideConfiguration();
2150
Winson Chungbdc646f2017-02-13 12:12:22 -08002151 // Short circuit: if the two full configurations are equal (the common case), then there is
2152 // nothing to do. We test the full configuration instead of the global and merged override
2153 // configurations because there are cases (like moving a task to the pinned stack) where
2154 // the combine configurations are equal, but would otherwise differ in the override config
2155 mTmpConfig1.setTo(mLastReportedConfiguration);
2156 mTmpConfig1.updateFrom(mLastReportedOverrideConfiguration);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002157 if (getConfiguration().equals(mTmpConfig1) && !forceNewConfig && !displayChanged) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002158 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002159 "Configuration & display unchanged in " + this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002160 return true;
2161 }
2162
2163 // Okay we now are going to make this activity have the new config.
2164 // But then we need to figure out how it needs to deal with that.
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002165
2166 // Find changes between last reported merged configuration and the current one. This is used
2167 // to decide whether to relaunch an activity or just report a configuration change.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002168 final int changes = getConfigurationChanges(mTmpConfig1);
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002169
2170 // Update last reported values.
Winson Chungbdc646f2017-02-13 12:12:22 -08002171 final Configuration newGlobalConfig = service.getGlobalConfiguration();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002172 final Configuration newMergedOverrideConfig = getMergedOverrideConfiguration();
Winson Chungbdc646f2017-02-13 12:12:22 -08002173 mTmpConfig1.setTo(mLastReportedConfiguration);
2174 mTmpConfig2.setTo(mLastReportedOverrideConfiguration);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002175 mLastReportedConfiguration.setTo(newGlobalConfig);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002176 mLastReportedOverrideConfiguration.setTo(newMergedOverrideConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002177
Andrii Kulian21713ac2016-10-12 22:05:05 -07002178 if (changes == 0 && !forceNewConfig) {
2179 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2180 "Configuration no differences in " + this);
2181 // There are no significant differences, so we won't relaunch but should still deliver
2182 // the new configuration to the client process.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002183 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002184 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002185 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002186 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002187 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002188 return true;
2189 }
2190
2191 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002192 "Configuration changes for " + this + ", allChanges="
Andrii Kulian21713ac2016-10-12 22:05:05 -07002193 + Configuration.configurationDiffToString(changes));
2194
2195 // If the activity isn't currently running, just leave the new configuration and it will
2196 // pick that up next time it starts.
2197 if (app == null || app.thread == null) {
2198 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2199 "Configuration doesn't matter not running " + this);
2200 stopFreezingScreenLocked(false);
2201 forceNewConfig = false;
2202 return true;
2203 }
2204
2205 // Figure out how to handle the changes between the configurations.
2206 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2207 "Checking to restart " + info.name + ": changed=0x"
2208 + Integer.toHexString(changes) + ", handles=0x"
2209 + Integer.toHexString(info.getRealConfigChanged())
2210 + ", newGlobalConfig=" + newGlobalConfig
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002211 + ", newMergedOverrideConfig=" + newMergedOverrideConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002212
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002213 if (shouldRelaunchLocked(changes, newGlobalConfig, newMergedOverrideConfig)
Zak Cohen90e7116742017-01-29 12:59:23 -08002214 || forceNewConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002215 // Aha, the activity isn't handling the change, so DIE DIE DIE.
2216 configChangeFlags |= changes;
2217 startFreezingScreenLocked(app, globalChanges);
2218 forceNewConfig = false;
2219 preserveWindow &= isResizeOnlyChange(changes);
2220 if (app == null || app.thread == null) {
2221 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2222 "Config is destroying non-running " + this);
2223 stack.destroyActivityLocked(this, true, "config");
2224 } else if (state == ActivityState.PAUSING) {
2225 // A little annoying: we are waiting for this activity to finish pausing. Let's not
2226 // do anything now, but just flag that it needs to be restarted when done pausing.
2227 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2228 "Config is skipping already pausing " + this);
2229 deferRelaunchUntilPaused = true;
2230 preserveWindowOnDeferredRelaunch = preserveWindow;
2231 return true;
2232 } else if (state == ActivityState.RESUMED) {
2233 // Try to optimize this case: the configuration is changing and we need to restart
2234 // the top, resumed activity. Instead of doing the normal handshaking, just say
2235 // "restart!".
2236 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2237 "Config is relaunching resumed " + this);
2238
2239 if (DEBUG_STATES && !visible) {
2240 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
2241 + " called by " + Debug.getCallers(4));
2242 }
2243
2244 relaunchActivityLocked(true /* andResume */, preserveWindow);
2245 } else {
2246 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
2247 "Config is relaunching non-resumed " + this);
2248 relaunchActivityLocked(false /* andResume */, preserveWindow);
2249 }
2250
2251 // All done... tell the caller we weren't able to keep this activity around.
2252 return false;
2253 }
2254
2255 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002256 // NOTE: We only forward the override configuration as the system level configuration
Andrii Kulian21713ac2016-10-12 22:05:05 -07002257 // changes is always sent to all processes when they happen so it can just use whatever
2258 // system level configuration it last got.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002259 if (displayChanged) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002260 scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002261 } else {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002262 scheduleConfigurationChanged(newMergedOverrideConfig);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08002263 }
Andrii Kulian21713ac2016-10-12 22:05:05 -07002264 stopFreezingScreenLocked(false);
2265
2266 return true;
2267 }
2268
Zak Cohen90e7116742017-01-29 12:59:23 -08002269 /**
2270 * When assessing a configuration change, decide if the changes flags and the new configurations
2271 * should cause the Activity to relaunch.
2272 */
2273 private boolean shouldRelaunchLocked(int changes, Configuration newGlobalConfig,
2274 Configuration newTaskMergedOverrideConfig) {
2275 int configChanged = info.getRealConfigChanged();
2276
2277 // Override for apps targeting pre-O sdks
2278 // If a device is in VR mode, and we're transitioning into VR ui mode, add ignore ui mode
2279 // to the config change.
2280 // For O and later, apps will be required to add configChanges="uimode" to their manifest.
2281 if (appInfo.targetSdkVersion < O
2282 && requestedVrComponent != null
2283 && (isInVrUiMode(newGlobalConfig) || isInVrUiMode(newTaskMergedOverrideConfig))) {
2284 configChanged |= CONFIG_UI_MODE;
2285 }
2286
2287 return (changes&(~configChanged)) != 0;
2288 }
2289
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002290 private int getConfigurationChanges(Configuration lastReportedConfig) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07002291 // Determine what has changed. May be nothing, if this is a config that has come back from
2292 // the app after going idle. In that case we just want to leave the official config object
2293 // now in the activity and do nothing else.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002294 final Configuration currentConfig = getConfiguration();
2295 int changes = lastReportedConfig.diff(currentConfig);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002296 // We don't want to use size changes if they don't cross boundaries that are important to
2297 // the app.
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002298 if ((changes & CONFIG_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002299 final boolean crosses = crossesHorizontalSizeThreshold(lastReportedConfig.screenWidthDp,
2300 currentConfig.screenWidthDp)
2301 || crossesVerticalSizeThreshold(lastReportedConfig.screenHeightDp,
2302 currentConfig.screenHeightDp);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002303 if (!crosses) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002304 changes &= ~CONFIG_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002305 }
2306 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002307 if ((changes & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Andrii Kulianb43be0a2017-03-02 17:29:40 -08002308 final int oldSmallest = lastReportedConfig.smallestScreenWidthDp;
2309 final int newSmallest = currentConfig.smallestScreenWidthDp;
2310 if (!crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002311 changes &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002312 }
2313 }
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002314 return changes;
Andrii Kulian21713ac2016-10-12 22:05:05 -07002315 }
2316
2317 private static boolean isResizeOnlyChange(int change) {
2318 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2319 | CONFIG_SCREEN_LAYOUT)) == 0;
2320 }
2321
2322 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
2323 if (service.mSuppressResizeConfigChanges && preserveWindow) {
2324 configChangeFlags = 0;
2325 return;
2326 }
2327
2328 List<ResultInfo> pendingResults = null;
2329 List<ReferrerIntent> pendingNewIntents = null;
2330 if (andResume) {
2331 pendingResults = results;
2332 pendingNewIntents = newIntents;
2333 }
2334 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2335 "Relaunching: " + this + " with results=" + pendingResults
2336 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2337 + " preserveWindow=" + preserveWindow);
2338 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
2339 : EventLogTags.AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
2340 task.taskId, shortComponentName);
2341
2342 startFreezingScreenLocked(app, 0);
2343
2344 mStackSupervisor.removeChildActivityContainers(this);
2345
2346 try {
2347 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2348 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2349 + " callers=" + Debug.getCallers(6));
2350 forceNewConfig = false;
2351 mStackSupervisor.activityRelaunchingLocked(this);
2352 app.thread.scheduleRelaunchActivity(appToken, pendingResults, pendingNewIntents,
2353 configChangeFlags, !andResume,
2354 new Configuration(service.getGlobalConfiguration()),
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07002355 new Configuration(getMergedOverrideConfiguration()), preserveWindow);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002356 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
2357 // pass in 'andResume' if this activity is currently resumed, which implies we aren't
2358 // sleeping.
2359 } catch (RemoteException e) {
2360 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2361 }
2362
2363 if (andResume) {
2364 if (DEBUG_STATES) {
2365 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2366 }
2367 results = null;
2368 newIntents = null;
2369 service.showUnsupportedZoomDialogIfNeededLocked(this);
2370 service.showAskCompatModeDialogLocked(this);
2371 } else {
2372 service.mHandler.removeMessages(ActivityStack.PAUSE_TIMEOUT_MSG, this);
2373 state = ActivityState.PAUSED;
2374 // if the app is relaunched when it's stopped, and we're not resuming,
2375 // put it back into stopped state.
2376 if (stopped) {
Winson Chung4dabf232017-01-25 13:25:22 -08002377 getStack().addToStopping(this, true /* scheduleIdle */, false /* idleDelayed */);
Andrii Kulian21713ac2016-10-12 22:05:05 -07002378 }
2379 }
2380
2381 configChangeFlags = 0;
2382 deferRelaunchUntilPaused = false;
2383 preserveWindowOnDeferredRelaunch = false;
2384 }
2385
Jorim Jaggi02886a82016-12-06 09:10:06 -08002386 boolean isProcessRunning() {
2387 ProcessRecord proc = app;
2388 if (proc == null) {
2389 proc = service.mProcessNames.get(processName, info.applicationInfo.uid);
2390 }
2391 return proc != null && proc.thread != null;
2392 }
2393
Craig Mautner21d24a22014-04-23 11:45:37 -07002394 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2395 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2396 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2397 if (launchedFromPackage != null) {
2398 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2399 }
2400 if (resolvedType != null) {
2401 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2402 }
2403 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2404 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002405
Craig Mautner21d24a22014-04-23 11:45:37 -07002406 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002407 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002408 }
2409
2410 out.startTag(null, TAG_INTENT);
2411 intent.saveToXml(out);
2412 out.endTag(null, TAG_INTENT);
2413
2414 if (isPersistable() && persistentState != null) {
2415 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2416 persistentState.saveToXml(out);
2417 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2418 }
2419 }
2420
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002421 static ActivityRecord restoreFromXml(XmlPullParser in,
2422 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002423 Intent intent = null;
2424 PersistableBundle persistentState = null;
2425 int launchedFromUid = 0;
2426 String launchedFromPackage = null;
2427 String resolvedType = null;
2428 boolean componentSpecified = false;
2429 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002430 long createTime = -1;
2431 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002432 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002433
2434 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2435 final String attrName = in.getAttributeName(attrNdx);
2436 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002437 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002438 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002439 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002440 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002441 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002442 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002443 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2444 launchedFromPackage = attrValue;
2445 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2446 resolvedType = attrValue;
2447 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002448 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002449 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002450 userId = Integer.parseInt(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07002451 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
2452 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002453 } else {
2454 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2455 }
2456 }
2457
2458 int event;
2459 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07002460 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002461 if (event == XmlPullParser.START_TAG) {
2462 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002463 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002464 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002465 if (TAG_INTENT.equals(name)) {
2466 intent = Intent.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002467 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002468 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002469 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2470 persistentState = PersistableBundle.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002471 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002472 "ActivityRecord: persistentState=" + persistentState);
2473 } else {
2474 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2475 XmlUtils.skipCurrentTag(in);
2476 }
2477 }
2478 }
2479
2480 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002481 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002482 }
2483
2484 final ActivityManagerService service = stackSupervisor.mService;
2485 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Jeff Hao1b012d32014-08-20 10:35:34 -07002486 userId);
Craig Mautnere0129b32014-05-25 16:41:09 -07002487 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002488 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2489 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002490 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002491 final ActivityRecord r = new ActivityRecord(service, null /* caller */,
2492 0 /* launchedFromPid */, launchedFromUid, launchedFromPackage, intent, resolvedType,
2493 aInfo, service.getConfiguration(), null /* resultTo */, null /* resultWho */,
2494 0 /* reqCode */, componentSpecified, false /* rootVoiceInteraction */,
2495 stackSupervisor, null /* container */, null /* options */, null /* sourceRecord */);
Craig Mautner21d24a22014-04-23 11:45:37 -07002496
2497 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002498 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002499 r.createTime = createTime;
2500
2501 return r;
2502 }
2503
2504 private static String activityTypeToString(int type) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002505 switch (type) {
2506 case APPLICATION_ACTIVITY_TYPE: return "APPLICATION_ACTIVITY_TYPE";
2507 case HOME_ACTIVITY_TYPE: return "HOME_ACTIVITY_TYPE";
2508 case RECENTS_ACTIVITY_TYPE: return "RECENTS_ACTIVITY_TYPE";
Winson Chung83471632016-12-13 11:02:12 -08002509 case ASSISTANT_ACTIVITY_TYPE: return "ASSISTANT_ACTIVITY_TYPE";
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002510 default: return Integer.toString(type);
2511 }
2512 }
2513
Zak Cohen90e7116742017-01-29 12:59:23 -08002514 private static boolean isInVrUiMode(Configuration config) {
2515 return (config.uiMode & Configuration.UI_MODE_TYPE_MASK) == UI_MODE_TYPE_VR_HEADSET;
2516 }
2517
David Stevens82ea6cb2017-03-03 16:18:50 -08002518 int getUid() {
2519 return info.applicationInfo.uid;
2520 }
2521
Craig Mautnerf81b90872013-02-26 13:02:43 -08002522 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002524 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002525 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002526 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002527 }
2528 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002529 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002530 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002531 sb.append(" u");
2532 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002533 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002534 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002535 stringName = sb.toString();
2536 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 }
2538}