blob: ef197002f0a721779ed6dc3671576074f0c8a157 [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
Wale Ogunwale4cea0f52015-12-25 06:30:31 -080019import static android.app.ActivityManager.StackId;
Wale Ogunwaleeb915f22016-10-14 07:18:22 -070020import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080021import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -080022import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwale22e25262016-02-01 10:32:02 -080023import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Andrii Kulian21713ac2016-10-12 22:05:05 -070024import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
25import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
26import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
27import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Chong Zhang87761972016-08-22 13:53:24 -070028import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale6cae7652015-12-26 07:36:26 -080029import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070030import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggicd13d332016-04-27 15:40:20 -070031import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwaled26176f2016-01-25 20:04:04 -080032import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE;
Wale Ogunwale72a73e32016-10-13 12:16:39 -070033import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070034import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwaleeb915f22016-10-14 07:18:22 -070035import static android.view.Display.DEFAULT_DISPLAY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080036import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
Andrii Kulian21713ac2016-10-12 22:05:05 -070037import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
38import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SCREENSHOTS;
39import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080040import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
41import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070042import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
43import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
44import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
45import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080046import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
47import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
48import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070049import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080050import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
51import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Andrii Kulian21713ac2016-10-12 22:05:05 -070052import static com.android.server.am.ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS;
Wale Ogunwale18795a22014-12-03 11:38:33 -080053import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
54
Andrii Kulian21713ac2016-10-12 22:05:05 -070055import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -070056import android.app.ActivityManager.TaskDescription;
Winson Chunga29eb982016-12-14 12:01:27 -080057import android.app.ActivityManagerInternal.PictureInPictureArguments;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070058import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080059import android.app.PendingIntent;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080060import android.app.ResultInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.ComponentName;
62import android.content.Intent;
63import android.content.pm.ActivityInfo;
64import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -070065import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.res.Configuration;
67import android.graphics.Bitmap;
Dianne Hackbornd367ca82012-05-07 15:49:39 -070068import android.graphics.Rect;
Dianne Hackborn247fe742011-01-08 17:25:57 -080069import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -070071import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -080072import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080074import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -070076import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.os.SystemClock;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080078import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070079import android.os.UserHandle;
Amith Yamasani0af6fa72016-01-17 15:36:19 -080080import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.util.EventLog;
82import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -070084import android.util.TimeUtils;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -070085import android.view.AppTransitionAnimationSpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.IApplicationToken;
Jorim Jaggife762342016-10-13 14:33:27 +020087import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -070088
Filip Gruszczynski3d026712015-12-16 13:46:38 -080089import com.android.internal.app.ResolverActivity;
90import com.android.internal.content.ReferrerIntent;
91import com.android.internal.util.XmlUtils;
92import com.android.server.AttributeCache;
93import com.android.server.am.ActivityStack.ActivityState;
94import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Suprabh Shukla23593142015-11-03 17:31:15 -080096import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -070097import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.io.PrintWriter;
99import java.lang.ref.WeakReference;
100import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800101import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700103import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700104import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800106import org.xmlpull.v1.XmlPullParser;
107import org.xmlpull.v1.XmlPullParserException;
108import org.xmlpull.v1.XmlSerializer;
109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110/**
111 * An entry in the history stack, representing an activity.
112 */
Dianne Hackbornbe707852011-11-11 14:32:10 -0800113final class ActivityRecord {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800114 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_AM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700115 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
116 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
117 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700118 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700119 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
120 private static final String TAG_THUMBNAILS = TAG + POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700121 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800122
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700123 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700124 private static final String RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700125
Craig Mautner21d24a22014-04-23 11:45:37 -0700126 private static final String ATTR_ID = "id";
127 private static final String TAG_INTENT = "intent";
128 private static final String ATTR_USERID = "user_id";
129 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
130 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700131 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700132 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
133 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700134 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 final ActivityManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800137 final IApplicationToken.Stub appToken; // window manager token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 final ActivityInfo info; // all about me
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700139 final ApplicationInfo appInfo; // information about activity's app
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700140 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800141 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700142 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 final Intent intent; // the original intent that generated us
144 final ComponentName realActivity; // the intent component, or target of an alias.
145 final String shortComponentName; // the short component name of the intent
146 final String resolvedType; // as per original caller;
147 final String packageName; // the package implementing intent's component
148 final String processName; // process where this component wants to run
149 final String taskAffinity; // as per ActivityInfo.taskAffinity
150 final boolean stateNotNeeded; // As per ActivityInfo.flags
Craig Mautner4addfc52013-06-25 08:05:45 -0700151 boolean fullscreen; // covers the full screen?
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800152 final boolean noDisplay; // activity is not displayed?
Ruben Brunke24b9a62016-02-16 21:38:24 -0800153 final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700154 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700155
156 static final int APPLICATION_ACTIVITY_TYPE = 0;
157 static final int HOME_ACTIVITY_TYPE = 1;
158 static final int RECENTS_ACTIVITY_TYPE = 2;
Craig Mautner2c1faed2013-07-23 12:56:02 -0700159 int mActivityType;
Craig Mautner86d67a42013-05-14 10:34:38 -0700160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 CharSequence nonLocalizedLabel; // the label information from the package mgr.
162 int labelRes; // the label information from the package mgr.
163 int icon; // resource identifier of activity's icon.
Adam Powell04fe6eb2013-05-31 14:39:48 -0700164 int logo; // resource identifier of activity's logo.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 int theme; // resource identifier of activity's theme.
Dianne Hackborn247fe742011-01-08 17:25:57 -0800166 int realTheme; // actual theme resource we will use, never 0.
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800167 int windowFlags; // custom window flags for preview window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 TaskRecord task; // the task this is in.
Craig Mautner21d24a22014-04-23 11:45:37 -0700169 long createTime = System.currentTimeMillis();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700170 long displayStartTime; // when we started launching this activity
171 long fullyDrawnStartTime; // when we started launching this activity
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700172 long startTime; // last time this activity was started
Dianne Hackborn50685602011-12-01 12:23:37 -0800173 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700174 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700175 long pauseTime; // last time we started pausing the activity
176 long launchTickTime; // base time for launch tick messages
Andrii Kulian21713ac2016-10-12 22:05:05 -0700177 private Configuration mLastReportedConfiguration; // configuration activity was last running in
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700178 // Overridden configuration by the activity task
Andrii Kulian1779e612016-10-12 21:58:25 -0700179 // WARNING: Reference points to {@link TaskRecord#getMergedOverrideConfig}, so its internal
180 // state should never be altered directly.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700181 private Configuration mLastReportedOverrideConfiguration;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700182 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700183 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final String resultWho; // additional identifier for use by resultTo.
185 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800186 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800188 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700189 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700190 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700191 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -0700193 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700194 ProcessRecord app; // if non-null, hosting application
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700195 ActivityState state; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700197 PersistableBundle persistentState; // last persistently saved activity state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 boolean frontOfTask; // is this the root activity of its task?
199 boolean launchFailed; // set if a launched failed, to abort on 2nd try
200 boolean haveState; // have we gotten the last activity state?
201 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700202 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800204 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
205 // completed
206 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 int configChangeFlags; // which config values have changed
208 boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 int launchMode; // the launch mode activity attribute.
210 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700211 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
212 // might hide this activity?
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800213 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 boolean nowVisible; // is this activity's window visible?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 boolean idle; // has the activity gone idle?
216 boolean hasBeenLaunched;// has this activity ever been launched?
217 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400218 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400219 boolean forceNewConfig; // force re-create with new config next time
Winson Chungb5c41b72016-12-07 15:00:47 -0800220 boolean supportsPipOnMoveToBackground; // Supports automatically entering picture-in-picture
Winson Chunga29eb982016-12-14 12:01:27 -0800221 // when this activity is hidden. This flag is requested by the activity.
Winson Chungb5c41b72016-12-07 15:00:47 -0800222 private boolean enterPipOnMoveToBackground; // Flag to enter picture in picture when this
Winson Chunga29eb982016-12-14 12:01:27 -0800223 // activity is made invisible. This flag is set specifically when another task is being
224 // launched or moved to the front which may cause this activity to try and enter PiP
225 // when it is next made invisible.
226 PictureInPictureArguments pictureInPictureArgs = new PictureInPictureArguments(); // The PiP
227 // arguments used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800228 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800229 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800230 ComponentName requestedVrComponent; // the requested component for handling VR mode.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800231 ArrayList<ActivityContainer> mChildContainers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700233 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700234
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700235 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700236 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800237
238 static final int STARTING_WINDOW_NOT_SHOWN = 0;
239 static final int STARTING_WINDOW_SHOWN = 1;
240 static final int STARTING_WINDOW_REMOVED = 2;
241 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700242 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800243
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800244 boolean mUpdateTaskThumbnailWhenHidden;
Craig Mautnere0a38842013-12-16 16:14:02 -0800245 ActivityContainer mInitialActivityContainer;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700246
Craig Mautner21d24a22014-04-23 11:45:37 -0700247 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700248 boolean mLaunchTaskBehind; // this activity is actively being launched with
249 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700250
Filip Gruszczynski23493322015-07-29 17:02:59 -0700251 // These configurations are collected from application's resources based on size-sensitive
252 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
253 // and drawable-sw400dp will be added to both as 400.
254 private int[] mVerticalSizeConfigurations;
255 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700256 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700257
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800258 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
259 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
260
Robert Carrd2265122016-08-05 10:25:21 -0700261 // A hint to override the window specified rotation animation, or -1
262 // to use the window specified value. We use this so that
263 // we can select the right animation in the cases of starting
264 // windows, where the app hasn't had time to set a value
265 // on the window.
266 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700267
Andrii Kulian21713ac2016-10-12 22:05:05 -0700268 /**
269 * Temp configs used in {@link #ensureActivityConfigurationLocked(int, boolean)}
270 */
271 private final Configuration mTmpGlobalConfig = new Configuration();
272 private final Configuration mTmpTaskConfig = new Configuration();
273
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800274 private static String startingWindowStateToString(int state) {
275 switch (state) {
276 case STARTING_WINDOW_NOT_SHOWN:
277 return "STARTING_WINDOW_NOT_SHOWN";
278 case STARTING_WINDOW_SHOWN:
279 return "STARTING_WINDOW_SHOWN";
280 case STARTING_WINDOW_REMOVED:
281 return "STARTING_WINDOW_REMOVED";
282 default:
283 return "unknown state=" + state;
284 }
285 }
286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700288 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700289 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
290 pw.print(" processName="); pw.println(processName);
291 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700292 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800293 pw.print(" userId="); pw.println(userId);
294 pw.print(prefix); pw.print("app="); pw.println(app);
295 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700296 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
297 pw.print(" task="); pw.println(task);
298 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700299 pw.print(prefix); pw.print("realActivity=");
300 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700301 if (appInfo != null) {
302 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
303 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
304 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
305 }
306 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800307 if (appInfo.splitSourceDirs != null) {
308 pw.print(prefix); pw.print("splitDir=");
309 pw.println(Arrays.toString(appInfo.splitSourceDirs));
310 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800311 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700312 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
313 pw.print(" componentSpecified="); pw.print(componentSpecified);
Craig Mautner86d67a42013-05-14 10:34:38 -0700314 pw.print(" mActivityType="); pw.println(mActivityType);
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700315 if (rootVoiceInteraction) {
316 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
317 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800318 pw.print(prefix); pw.print("compat="); pw.print(compat);
319 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
320 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
321 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Andrii Kulian1779e612016-10-12 21:58:25 -0700322 pw.print(prefix); pw.print("mLastReportedConfiguration=");
323 pw.println(mLastReportedConfiguration);
324 pw.print(prefix); pw.print("mLastReportedOverrideConfiguration=");
325 pw.println(mLastReportedOverrideConfiguration);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700326 if (resultTo != null || resultWho != null) {
327 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
328 pw.print(" resultWho="); pw.print(resultWho);
329 pw.print(" resultCode="); pw.println(requestCode);
330 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700331 if (taskDescription != null) {
332 final String iconFilename = taskDescription.getIconFilename();
333 if (iconFilename != null || taskDescription.getLabel() != null ||
334 taskDescription.getPrimaryColor() != 0) {
335 pw.print(prefix); pw.print("taskDescription:");
336 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
337 pw.print(" label=\""); pw.print(taskDescription.getLabel());
338 pw.print("\"");
339 pw.print(" color=");
340 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
341 }
342 if (iconFilename == null && taskDescription.getIcon() != null) {
343 pw.print(prefix); pw.println("taskDescription contains Bitmap");
344 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700345 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700346 if (results != null) {
347 pw.print(prefix); pw.print("results="); pw.println(results);
348 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700349 if (pendingResults != null && pendingResults.size() > 0) {
350 pw.print(prefix); pw.println("Pending Results:");
351 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
352 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
353 pw.print(prefix); pw.print(" - ");
354 if (pir == null) {
355 pw.println("null");
356 } else {
357 pw.println(pir);
358 pir.dump(pw, prefix + " ");
359 }
360 }
361 }
362 if (newIntents != null && newIntents.size() > 0) {
363 pw.print(prefix); pw.println("Pending New Intents:");
364 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800365 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700366 pw.print(prefix); pw.print(" - ");
367 if (intent == null) {
368 pw.println("null");
369 } else {
370 pw.println(intent.toShortString(false, true, false, true));
371 }
372 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700373 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700374 if (pendingOptions != null) {
375 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
376 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700377 if (appTimeTracker != null) {
378 appTimeTracker.dumpWithHeader(pw, prefix, false);
379 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700380 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700381 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700382 }
383 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800384 pw.print(" launchCount="); pw.print(launchCount);
385 pw.print(" lastLaunchTime=");
386 if (lastLaunchTime == 0) pw.print("0");
387 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
388 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700389 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700390 pw.print(" icicle="); pw.println(icicle);
391 pw.print(prefix); pw.print("state="); pw.print(state);
392 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700393 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700394 pw.print(" finishing="); pw.println(finishing);
395 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
396 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700397 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800398 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800399 pw.print(" idle="); pw.print(idle);
400 pw.print(" mStartingWindowState=");
401 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800402 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
403 pw.print(" noDisplay="); pw.print(noDisplay);
404 pw.print(" immersive="); pw.print(immersive);
405 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800406 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400407 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700408 pw.print(prefix); pw.print("mActivityType=");
409 pw.println(activityTypeToString(mActivityType));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800410 if (requestedVrComponent != null) {
411 pw.print(prefix);
412 pw.print("requestedVrComponent=");
413 pw.println(requestedVrComponent);
414 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700415 if (displayStartTime != 0 || startTime != 0) {
416 pw.print(prefix); pw.print("displayStartTime=");
417 if (displayStartTime == 0) pw.print("0");
418 else TimeUtils.formatDuration(displayStartTime, now, pw);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700419 pw.print(" startTime=");
420 if (startTime == 0) pw.print("0");
421 else TimeUtils.formatDuration(startTime, now, pw);
422 pw.println();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700423 }
Craig Mautner8c14c152015-01-15 17:32:07 -0800424 final boolean waitingVisible = mStackSupervisor.mWaitingVisibleActivities.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800425 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700426 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800427 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700428 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700429 if (lastVisibleTime == 0) pw.print("0");
430 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
431 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700432 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800433 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
434 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700435 pw.print(" configChangeFlags=");
436 pw.println(Integer.toHexString(configChangeFlags));
437 }
438 if (connections != null) {
439 pw.print(prefix); pw.print("connections="); pw.println(connections);
440 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800441 if (info != null) {
442 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
443 }
Winson Chungb5c41b72016-12-07 15:00:47 -0800444 if (supportsPipOnMoveToBackground) {
Winson Chunga29eb982016-12-14 12:01:27 -0800445 pw.println(prefix + "supportsPipOnMoveToBackground=1");
446 pw.println(prefix + "enterPipOnMoveToBackground=" +
447 (enterPipOnMoveToBackground ? 1 : 0));
448 pictureInPictureArgs.dump(pw, prefix);
Winson Chungb5c41b72016-12-07 15:00:47 -0800449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451
Andrii Kulian21713ac2016-10-12 22:05:05 -0700452 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700453 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
454 }
455
Andrii Kulian21713ac2016-10-12 22:05:05 -0700456 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700457 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
458 }
459
Andrii Kulian21713ac2016-10-12 22:05:05 -0700460 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700461 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
462 }
463
Filip Gruszczynski23493322015-07-29 17:02:59 -0700464 /**
465 * The purpose of this method is to decide whether the activity needs to be relaunched upon
466 * changing its size. In most cases the activities don't need to be relaunched, if the resize
467 * is small, all the activity content has to do is relayout itself within new bounds. There are
468 * cases however, where the activity's content would be completely changed in the new size and
469 * the full relaunch is required.
470 *
471 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
472 * required. These thresholds are collected from the application resource qualifiers. For
473 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
474 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
475 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
476 * of the threshold.
477 */
478 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
479 int secondDp) {
480 if (thresholds == null) {
481 return false;
482 }
483 for (int i = thresholds.length - 1; i >= 0; i--) {
484 final int threshold = thresholds[i];
485 if ((firstDp < threshold && secondDp >= threshold)
486 || (firstDp >= threshold && secondDp < threshold)) {
487 return true;
488 }
489 }
490 return false;
491 }
492
Andrii Kulian21713ac2016-10-12 22:05:05 -0700493 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700494 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700495 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
496 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700497 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700498 }
499
Andrii Kulian21713ac2016-10-12 22:05:05 -0700500 private void scheduleConfigurationChanged(Configuration config, boolean reportToActivity) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800501 if (app == null || app.thread == null) {
502 return;
503 }
504 try {
505 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + " " +
Adam Lesinskibad43fc2016-07-19 13:35:01 -0700506 "reportToActivity=" + reportToActivity + " and config: " + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700507
Andrii Kulian1779e612016-10-12 21:58:25 -0700508 app.thread.scheduleActivityConfigurationChanged(appToken, new Configuration(config),
509 reportToActivity);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800510 } catch (RemoteException e) {
511 // If process died, whatever.
512 }
513 }
514
Andrii Kulian933076d2016-03-29 17:04:42 -0700515 void scheduleMultiWindowModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700516 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800517 return;
518 }
519 try {
520 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Andrii Kulian933076d2016-03-29 17:04:42 -0700521 app.thread.scheduleMultiWindowModeChanged(appToken, !task.mFullscreen);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800522 } catch (Exception e) {
523 // If process died, I don't care.
524 }
525 }
526
Andrii Kulian933076d2016-03-29 17:04:42 -0700527 void schedulePictureInPictureModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700528 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800529 return;
530 }
531 try {
Andrii Kulian933076d2016-03-29 17:04:42 -0700532 app.thread.schedulePictureInPictureModeChanged(
Andrii Kulian02b7a832016-10-06 23:11:56 -0700533 appToken, task.getStackId() == PINNED_STACK_ID);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800534 } catch (Exception e) {
535 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800536 }
537 }
538
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800539 boolean isFreeform() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700540 return task != null && task.getStackId() == FREEFORM_WORKSPACE_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800541 }
542
Dianne Hackbornbe707852011-11-11 14:32:10 -0800543 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700544 private final WeakReference<ActivityRecord> weakActivity;
545 private final ActivityManagerService mService;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800546
Wale Ogunwale7d701172015-03-11 15:36:30 -0700547 Token(ActivityRecord activity, ActivityManagerService service) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800548 weakActivity = new WeakReference<>(activity);
Wale Ogunwale7d701172015-03-11 15:36:30 -0700549 mService = service;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800550 }
551
Wale Ogunwale7d701172015-03-11 15:36:30 -0700552 @Override
553 public void windowsDrawn() {
554 synchronized (mService) {
555 ActivityRecord r = tokenToActivityRecordLocked(this);
556 if (r != null) {
557 r.windowsDrawnLocked();
558 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800559 }
560 }
561
Wale Ogunwale7d701172015-03-11 15:36:30 -0700562 @Override
563 public void windowsVisible() {
564 synchronized (mService) {
565 ActivityRecord r = tokenToActivityRecordLocked(this);
566 if (r != null) {
567 r.windowsVisibleLocked();
568 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800569 }
570 }
571
Wale Ogunwale7d701172015-03-11 15:36:30 -0700572 @Override
573 public void windowsGone() {
574 synchronized (mService) {
575 ActivityRecord r = tokenToActivityRecordLocked(this);
576 if (r != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700577 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + r);
Wale Ogunwale7d701172015-03-11 15:36:30 -0700578 r.nowVisible = false;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700579 }
Dianne Hackbornbe707852011-11-11 14:32:10 -0800580 }
581 }
582
Wale Ogunwale7d701172015-03-11 15:36:30 -0700583 @Override
584 public boolean keyDispatchingTimedOut(String reason) {
585 ActivityRecord r;
586 ActivityRecord anrActivity;
587 ProcessRecord anrApp;
588 synchronized (mService) {
589 r = tokenToActivityRecordLocked(this);
590 if (r == null) {
591 return false;
592 }
593 anrActivity = r.getWaitingHistoryRecordLocked();
Andrii Kulian21713ac2016-10-12 22:05:05 -0700594 anrApp = r.app;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700595 }
596 return mService.inputDispatchingTimedOut(anrApp, anrActivity, r, false, reason);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800597 }
598
Wale Ogunwale7d701172015-03-11 15:36:30 -0700599 @Override
600 public long getKeyDispatchingTimeout() {
601 synchronized (mService) {
602 ActivityRecord r = tokenToActivityRecordLocked(this);
603 if (r == null) {
604 return 0;
605 }
606 r = r.getWaitingHistoryRecordLocked();
607 return ActivityManagerService.getInputDispatchingTimeoutLocked(r);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800608 }
Wale Ogunwale7d701172015-03-11 15:36:30 -0700609 }
610
Andrii Kulian21713ac2016-10-12 22:05:05 -0700611 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700612 if (token == null) {
613 return null;
614 }
615 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700616 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700617 return null;
618 }
619 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800620 }
621
Craig Mautnerde4ef022013-04-07 19:01:33 -0700622 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800623 public String toString() {
624 StringBuilder sb = new StringBuilder(128);
625 sb.append("Token{");
626 sb.append(Integer.toHexString(System.identityHashCode(this)));
627 sb.append(' ');
628 sb.append(weakActivity.get());
629 sb.append('}');
630 return sb.toString();
631 }
632 }
633
Wale Ogunwale7d701172015-03-11 15:36:30 -0700634 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800635 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700636 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800637 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800638 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800639 return null;
640 }
641 }
642
Chong Zhang85ee6542015-10-02 13:36:38 -0700643 boolean isResolverActivity() {
644 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700645 }
646
Craig Mautnerd2328952013-03-05 12:46:26 -0800647 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800648 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700650 ActivityRecord _resultTo, String _resultWho, int _reqCode,
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700651 boolean _componentSpecified, boolean _rootVoiceInteraction,
652 ActivityStackSupervisor supervisor,
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700653 ActivityContainer container, ActivityOptions options, ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 service = _service;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700655 appToken = new Token(this, service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 info = aInfo;
657 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800658 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700659 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 intent = _intent;
661 shortComponentName = _intent.getComponent().flattenToShortString();
662 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800663 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700664 rootVoiceInteraction = _rootVoiceInteraction;
Andrii Kulian1779e612016-10-12 21:58:25 -0700665 mLastReportedConfiguration = new Configuration(_configuration);
666 mLastReportedOverrideConfiguration = new Configuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 resultTo = _resultTo;
668 resultWho = _resultWho;
669 requestCode = _reqCode;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700670 state = ActivityState.INITIALIZING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 frontOfTask = false;
672 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700674 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800676 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 keysPaused = false;
678 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700679 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 idle = false;
682 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700683 mStackSupervisor = supervisor;
Craig Mautnere0a38842013-12-16 16:14:02 -0800684 mInitialActivityContainer = container;
Robert Carr0f5d7532016-10-17 16:39:17 -0700685
686 mRotationAnimationHint = aInfo.rotationAnimation;
687
Craig Mautner233ceee2014-05-09 17:05:11 -0700688 if (options != null) {
Chong Zhang280d3322015-11-03 17:27:26 -0800689 pendingOptions = options;
Craig Mautnerbb742462014-07-07 15:28:55 -0700690 mLaunchTaskBehind = pendingOptions.getLaunchTaskBehind();
Robert Carr0f5d7532016-10-17 16:39:17 -0700691
692 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
693 // Only override manifest supplied option if set.
694 if (rotationAnimation >= 0) {
695 mRotationAnimationHint = rotationAnimation;
696 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700697 PendingIntent usageReport = pendingOptions.getUsageTimeReport();
698 if (usageReport != null) {
699 appTimeTracker = new AppTimeTracker(usageReport);
700 }
Craig Mautner233ceee2014-05-09 17:05:11 -0700701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700703 // This starts out true, since the initial state of an activity
704 // is that we have everything, and we shouldn't never consider it
705 // lacking in state to be removed if it dies.
706 haveState = true;
707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 if (aInfo != null) {
Chong Zhang6bdc7e12016-02-01 14:33:53 -0800709 // If the class name in the intent doesn't match that of the target, this is
710 // probably an alias. We have to create a new ComponentName object to keep track
711 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 if (aInfo.targetActivity == null
Chong Zhang6bdc7e12016-02-01 14:33:53 -0800713 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
714 && (aInfo.launchMode == ActivityInfo.LAUNCH_MULTIPLE
715 || aInfo.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 realActivity = _intent.getComponent();
717 } else {
Chong Zhang6bdc7e12016-02-01 14:33:53 -0800718 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720 taskAffinity = aInfo.taskAffinity;
721 stateNotNeeded = (aInfo.flags&
722 ActivityInfo.FLAG_STATE_NOT_NEEDED) != 0;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700723 appInfo = aInfo.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 nonLocalizedLabel = aInfo.nonLocalizedLabel;
725 labelRes = aInfo.labelRes;
726 if (nonLocalizedLabel == null && labelRes == 0) {
727 ApplicationInfo app = aInfo.applicationInfo;
728 nonLocalizedLabel = app.nonLocalizedLabel;
729 labelRes = app.labelRes;
730 }
731 icon = aInfo.getIconResource();
Adam Powell04fe6eb2013-05-31 14:39:48 -0700732 logo = aInfo.getLogoResource();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 theme = aInfo.getThemeResource();
Dianne Hackborn247fe742011-01-08 17:25:57 -0800734 realTheme = theme;
735 if (realTheme == 0) {
736 realTheme = aInfo.applicationInfo.targetSdkVersion
737 < Build.VERSION_CODES.HONEYCOMB
738 ? android.R.style.Theme
739 : android.R.style.Theme_Holo;
740 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800741 if ((aInfo.flags&ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
Jorim Jaggife762342016-10-13 14:33:27 +0200742 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 if ((aInfo.flags&ActivityInfo.FLAG_MULTIPROCESS) != 0
745 && _caller != null
746 && (aInfo.applicationInfo.uid == Process.SYSTEM_UID
747 || aInfo.applicationInfo.uid == _caller.info.uid)) {
748 processName = _caller.processName;
749 } else {
750 processName = aInfo.processName;
751 }
752
753 if (intent != null && (aInfo.flags & ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS) != 0) {
754 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
755 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 packageName = aInfo.applicationInfo.packageName;
758 launchMode = aInfo.launchMode;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
Amith Yamasani4befbec2013-07-10 16:18:01 -0700761 realTheme, com.android.internal.R.styleable.Window, userId);
Dianne Hackbornb795d7c2015-07-28 17:40:06 -0700762 final boolean translucent = ent != null && (ent.array.getBoolean(
Filip Gruszczynski23958c62015-02-23 18:05:27 -0800763 com.android.internal.R.styleable.Window_windowIsTranslucent, false)
764 || (!ent.array.hasValue(
765 com.android.internal.R.styleable.Window_windowIsTranslucent)
766 && ent.array.getBoolean(
767 com.android.internal.R.styleable.Window_windowSwipeToDismiss,
Dianne Hackbornb795d7c2015-07-28 17:40:06 -0700768 false)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 fullscreen = ent != null && !ent.array.getBoolean(
770 com.android.internal.R.styleable.Window_windowIsFloating, false)
Filip Gruszczynski23958c62015-02-23 18:05:27 -0800771 && !translucent;
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800772 noDisplay = ent != null && ent.array.getBoolean(
773 com.android.internal.R.styleable.Window_windowNoDisplay, false);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700774
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700775 setActivityType(_componentSpecified, _launchedFromUid, _intent, sourceRecord);
Daniel Sandler69a48172010-06-23 16:29:36 -0400776
777 immersive = (aInfo.flags & ActivityInfo.FLAG_IMMERSIVE) != 0;
Ruben Brunkf56c9f42016-04-22 18:20:55 -0700778
779 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
780 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 } else {
782 realActivity = null;
783 taskAffinity = null;
784 stateNotNeeded = false;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700785 appInfo = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 processName = null;
787 packageName = null;
788 fullscreen = true;
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800789 noDisplay = false;
Craig Mautner86d67a42013-05-14 10:34:38 -0700790 mActivityType = APPLICATION_ACTIVITY_TYPE;
Daniel Sandler69a48172010-06-23 16:29:36 -0400791 immersive = false;
Ruben Brunkf56c9f42016-04-22 18:20:55 -0700792 requestedVrComponent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
794 }
795
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700796 private boolean isHomeIntent(Intent intent) {
797 return Intent.ACTION_MAIN.equals(intent.getAction())
798 && intent.hasCategory(Intent.CATEGORY_HOME)
799 && intent.getCategories().size() == 1
800 && intent.getData() == null
801 && intent.getType() == null;
802 }
803
Chong Zhangad24f962016-08-25 12:12:33 -0700804 static boolean isMainIntent(Intent intent) {
805 return Intent.ACTION_MAIN.equals(intent.getAction())
806 && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
807 && intent.getCategories().size() == 1
808 && intent.getData() == null
809 && intent.getType() == null;
810 }
811
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700812 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
813 if (uid == Process.myUid() || uid == 0) {
814 // System process can launch home activity.
815 return true;
816 }
817 // Resolver activity can launch home activity.
818 return sourceRecord != null && sourceRecord.isResolverActivity();
819 }
820
821 private void setActivityType(boolean componentSpecified,
822 int launchedFromUid, Intent intent, ActivityRecord sourceRecord) {
823 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
824 && isHomeIntent(intent) && !isResolverActivity()) {
825 // This sure looks like a home activity!
826 mActivityType = HOME_ACTIVITY_TYPE;
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700827
828 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
829 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
830 // We only allow home activities to be resizeable if they explicitly requested it.
831 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
832 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700833 } else if (realActivity.getClassName().contains(RECENTS_PACKAGE_NAME)) {
834 mActivityType = RECENTS_ACTIVITY_TYPE;
835 } else {
836 mActivityType = APPLICATION_ACTIVITY_TYPE;
837 }
838 }
839
Craig Mautnera228ae92014-07-09 05:44:55 -0700840 void setTask(TaskRecord newTask, TaskRecord taskToAffiliateWith) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700841 if (task != null && task.removeActivity(this) && task != newTask
842 && task.getStack() != null) {
843 task.getStack().removeTask(task, "setTask");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700844 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700845 task = newTask;
Craig Mautnera228ae92014-07-09 05:44:55 -0700846 setTaskToAffiliateWith(taskToAffiliateWith);
847 }
848
849 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
850 if (taskToAffiliateWith != null &&
851 launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE &&
852 launchMode != ActivityInfo.LAUNCH_SINGLE_TASK) {
853 task.setTaskToAffiliateWith(taskToAffiliateWith);
854 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700855 }
856
Andrii Kulian02b7a832016-10-06 23:11:56 -0700857 /**
Winson Chungb5c41b72016-12-07 15:00:47 -0800858 * If this activity has requested that it auto-enter picture-in-picture and we can actually do
859 * this, then mark it to enter picture in picture at that point.
860 */
861 void setEnterPipOnMoveToBackground(boolean enterPipOnInvisible) {
862 if (supportsPipOnMoveToBackground) {
863 enterPipOnMoveToBackground = enterPipOnInvisible;
864 }
865 }
866
867 /**
868 * @return whether to enter PiP when this activity is made invisible.
869 */
870 public boolean shouldEnterPictureInPictureOnInvisible() {
871 return enterPipOnMoveToBackground;
872 }
873
874 /**
Andrii Kulian02b7a832016-10-06 23:11:56 -0700875 * @return Stack value from current task, null if there is no task.
876 */
877 ActivityStack getStack() {
878 return task != null ? task.getStack() : null;
879 }
880
Craig Mautner5eda9b32013-07-02 11:58:16 -0700881 boolean changeWindowTranslucency(boolean toOpaque) {
882 if (fullscreen == toOpaque) {
883 return false;
884 }
Craig Mautner4addfc52013-06-25 08:05:45 -0700885
Craig Mautner5eda9b32013-07-02 11:58:16 -0700886 // Keep track of the number of fullscreen activities in this task.
887 task.numFullscreen += toOpaque ? +1 : -1;
888
889 fullscreen = toOpaque;
890 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -0700891 }
892
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700893 void putInHistory() {
894 if (!inHistory) {
895 inHistory = true;
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700896 }
897 }
898
899 void takeFromHistory() {
900 if (inHistory) {
901 inHistory = false;
902 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800903 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700904 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700905 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700906 }
907 }
908
909 boolean isInHistory() {
910 return inHistory;
911 }
912
Wale Ogunwale7d701172015-03-11 15:36:30 -0700913 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700914 final ActivityStack stack = getStack();
915 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700916 }
917
Craig Mautner86d67a42013-05-14 10:34:38 -0700918 boolean isHomeActivity() {
919 return mActivityType == HOME_ACTIVITY_TYPE;
920 }
921
922 boolean isRecentsActivity() {
923 return mActivityType == RECENTS_ACTIVITY_TYPE;
924 }
925
926 boolean isApplicationActivity() {
927 return mActivityType == APPLICATION_ACTIVITY_TYPE;
928 }
929
Craig Mautner21d24a22014-04-23 11:45:37 -0700930 boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700931 return (info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY ||
932 info.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS) &&
933 (intent == null ||
934 (intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -0700935 }
936
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800937 boolean isFocusable() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700938 return StackId.canReceiveKeys(task.getStackId()) || isAlwaysFocusable();
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800939 }
940
941 boolean isResizeable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700942 return ActivityInfo.isResizeableMode(info.resizeMode);
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800943 }
944
Wale Ogunwale513346d2016-01-27 10:55:01 -0800945 boolean isResizeableOrForced() {
946 return !isHomeActivity() && (isResizeable() || service.mForceResizableActivities);
947 }
948
Jorim Jaggicd13d332016-04-27 15:40:20 -0700949 boolean isNonResizableOrForced() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700950 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -0700951 && info.resizeMode != RESIZE_MODE_RESIZEABLE_AND_PIPABLE
952 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -0700953 }
954
Winson Chunge6308042016-10-31 09:24:01 -0700955 /**
956 * @return whether this activity's resize mode supports PIP.
957 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800958 boolean supportsPictureInPicture() {
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800959 return !isHomeActivity() && info.resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE;
960 }
961
Winson Chunge6308042016-10-31 09:24:01 -0700962 /**
Winson Chungb5c41b72016-12-07 15:00:47 -0800963 * @return whether this activity is currently allowed to enter PIP, if
964 * {@param checkActivityVisibility} is set, then the current activity visibility is taken into
965 * account.
Winson Chunge6308042016-10-31 09:24:01 -0700966 */
Winson Chungb5c41b72016-12-07 15:00:47 -0800967 boolean canEnterPictureInPicture(boolean checkActivityVisibility) {
968 if (!checkActivityVisibility) {
969 return supportsPictureInPicture();
970 }
971
Winson Chung7e3233a2016-12-12 14:27:21 -0800972 if (supportsPictureInPicture()) {
Winson Chunge6308042016-10-31 09:24:01 -0700973 switch (state) {
974 case RESUMED:
975 case PAUSING:
976 case PAUSED:
977 return true;
978 }
979 }
980 return false;
981 }
982
Wale Ogunwale513346d2016-01-27 10:55:01 -0800983 boolean canGoInDockedStack() {
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700984 return !isHomeActivity() && isResizeableOrForced();
Wale Ogunwale513346d2016-01-27 10:55:01 -0800985 }
986
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800987 boolean isAlwaysFocusable() {
988 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800989 }
990
Jorim Jaggife762342016-10-13 14:33:27 +0200991 /**
992 * @return true if the activity contains windows that have
993 * {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set
994 */
995 boolean hasShowWhenLockedWindows() {
996 return service.mWindowManager.containsShowWhenLockedWindow(appToken);
997 }
998
999 /**
1000 * @return true if the activity contains windows that have
1001 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
1002 */
1003 boolean hasDismissKeyguardWindows() {
1004 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
1005 }
1006
Wale Ogunwale7d701172015-03-11 15:36:30 -07001007 void makeFinishingLocked() {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001008 if (!finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001009 final ActivityStack stack = getStack();
1010 if (stack != null && this == stack.getVisibleBehindActivity()) {
Jose Lima34ff4922014-08-18 15:19:41 -07001011 // A finishing activity should not remain as visible in the background
1012 mStackSupervisor.requestVisibleBehindLocked(this, false);
1013 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001014 finishing = true;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001015 if (stopped) {
1016 clearOptionsLocked();
1017 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001018
1019 if (service != null) {
1020 service.mTaskChangeNotificationController.notifyTaskStackChanged();
1021 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08001022 }
1023 }
1024
Dianne Hackborn7e269642010-08-25 19:50:20 -07001025 UriPermissionOwner getUriPermissionsLocked() {
1026 if (uriPermissions == null) {
1027 uriPermissions = new UriPermissionOwner(service, this);
1028 }
1029 return uriPermissions;
1030 }
1031
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001032 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 int requestCode, int resultCode,
1034 Intent resultData) {
1035 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -05001036 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -08001038 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040 results.add(r);
1041 }
1042
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001043 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 int requestCode) {
1045 if (results != null) {
1046 for (int i=results.size()-1; i>=0; i--) {
1047 ActivityResult r = (ActivityResult)results.get(i);
1048 if (r.mFrom != from) continue;
1049 if (r.mResultWho == null) {
1050 if (resultWho != null) continue;
1051 } else {
1052 if (!r.mResultWho.equals(resultWho)) continue;
1053 }
1054 if (r.mRequestCode != requestCode) continue;
1055
1056 results.remove(i);
1057 }
1058 }
1059 }
1060
Andrii Kulian21713ac2016-10-12 22:05:05 -07001061 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001063 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 }
1065 newIntents.add(intent);
1066 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001067
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001068 /**
1069 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1070 * method will be called at the proper time.
1071 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001072 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001073 // The activity now gets access to the data associated with this Intent.
1074 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001075 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001076 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001077 boolean unsent = true;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001078 final ActivityStack stack = getStack();
Wale Ogunwale826c7062016-09-13 08:25:54 -07001079 final boolean isTopActivityInStack =
1080 stack != null && stack.topRunningActivityLocked() == this;
1081 final boolean isTopActivityWhileSleeping =
1082 service.isSleepingLocked() && isTopActivityInStack;
Wale Ogunwale826c7062016-09-13 08:25:54 -07001083
1084 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001085 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1086 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001087 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001088 if ((state == ActivityState.RESUMED || state == ActivityState.PAUSED
1089 || isTopActivityWhileSleeping) && app != null && app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001091 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1092 ar.add(rintent);
Wale Ogunwale826c7062016-09-13 08:25:54 -07001093 app.thread.scheduleNewIntent(
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001094 ar, appToken, state == ActivityState.PAUSED /* andPause */);
Craig Mautner86d67a42013-05-14 10:34:38 -07001095 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001096 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001097 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001098 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001099 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001100 }
1101 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001102 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001103 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001104 }
1105 }
1106
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001107 void updateOptionsLocked(ActivityOptions options) {
1108 if (options != null) {
1109 if (pendingOptions != null) {
1110 pendingOptions.abort();
1111 }
1112 pendingOptions = options;
1113 }
1114 }
1115
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001116 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001117 if (pendingOptions != null
1118 && pendingOptions.getAnimationType() != ActivityOptions.ANIM_SCENE_TRANSITION) {
Michael Jurka21385cd2012-05-03 10:57:31 -07001119 final int animationType = pendingOptions.getAnimationType();
1120 switch (animationType) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001121 case ActivityOptions.ANIM_CUSTOM:
1122 service.mWindowManager.overridePendingAppTransition(
1123 pendingOptions.getPackageName(),
1124 pendingOptions.getCustomEnterResId(),
Dianne Hackborn84375872012-06-01 19:03:50 -07001125 pendingOptions.getCustomExitResId(),
1126 pendingOptions.getOnAnimationStartListener());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001127 break;
Chet Haase10e23ab2015-02-11 15:08:38 -08001128 case ActivityOptions.ANIM_CLIP_REVEAL:
1129 service.mWindowManager.overridePendingAppTransitionClipReveal(
1130 pendingOptions.getStartX(), pendingOptions.getStartY(),
1131 pendingOptions.getWidth(), pendingOptions.getHeight());
1132 if (intent.getSourceBounds() == null) {
1133 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1134 pendingOptions.getStartY(),
1135 pendingOptions.getStartX()+pendingOptions.getWidth(),
1136 pendingOptions.getStartY()+pendingOptions.getHeight()));
1137 }
1138 break;
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001139 case ActivityOptions.ANIM_SCALE_UP:
1140 service.mWindowManager.overridePendingAppTransitionScaleUp(
1141 pendingOptions.getStartX(), pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001142 pendingOptions.getWidth(), pendingOptions.getHeight());
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001143 if (intent.getSourceBounds() == null) {
1144 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1145 pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001146 pendingOptions.getStartX()+pendingOptions.getWidth(),
1147 pendingOptions.getStartY()+pendingOptions.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001148 }
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001149 break;
Michael Jurka832cb222012-04-13 09:32:47 -07001150 case ActivityOptions.ANIM_THUMBNAIL_SCALE_UP:
1151 case ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN:
1152 boolean scaleUp = (animationType == ActivityOptions.ANIM_THUMBNAIL_SCALE_UP);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001153 service.mWindowManager.overridePendingAppTransitionThumb(
1154 pendingOptions.getThumbnail(),
1155 pendingOptions.getStartX(), pendingOptions.getStartY(),
Michael Jurka21385cd2012-05-03 10:57:31 -07001156 pendingOptions.getOnAnimationStartListener(),
Michael Jurka832cb222012-04-13 09:32:47 -07001157 scaleUp);
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001158 if (intent.getSourceBounds() == null) {
1159 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1160 pendingOptions.getStartY(),
1161 pendingOptions.getStartX()
1162 + pendingOptions.getThumbnail().getWidth(),
1163 pendingOptions.getStartY()
1164 + pendingOptions.getThumbnail().getHeight()));
1165 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001166 break;
Winson Chunga4ccb862014-08-22 15:26:27 -07001167 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP:
1168 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN:
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001169 final AppTransitionAnimationSpec[] specs = pendingOptions.getAnimSpecs();
1170 if (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN
1171 && specs != null) {
1172 service.mWindowManager.overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -07001173 specs, pendingOptions.getOnAnimationStartListener(),
1174 pendingOptions.getAnimationFinishedListener(), false);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001175 } else {
1176 service.mWindowManager.overridePendingAppTransitionAspectScaledThumb(
1177 pendingOptions.getThumbnail(),
1178 pendingOptions.getStartX(), pendingOptions.getStartY(),
1179 pendingOptions.getWidth(), pendingOptions.getHeight(),
1180 pendingOptions.getOnAnimationStartListener(),
1181 (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP));
1182 if (intent.getSourceBounds() == null) {
1183 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1184 pendingOptions.getStartY(),
1185 pendingOptions.getStartX() + pendingOptions.getWidth(),
1186 pendingOptions.getStartY() + pendingOptions.getHeight()));
1187 }
Winson Chunga4ccb862014-08-22 15:26:27 -07001188 }
1189 break;
Craig Mautner233ceee2014-05-09 17:05:11 -07001190 default:
1191 Slog.e(TAG, "applyOptionsLocked: Unknown animationType=" + animationType);
1192 break;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001193 }
1194 pendingOptions = null;
1195 }
1196 }
1197
Adam Powellcfbe9be2013-11-06 14:58:58 -08001198 ActivityOptions getOptionsForTargetActivityLocked() {
1199 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1200 }
1201
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001202 void clearOptionsLocked() {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001203 if (pendingOptions != null) {
1204 pendingOptions.abort();
1205 pendingOptions = null;
1206 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001207 }
1208
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001209 ActivityOptions takeOptionsLocked() {
1210 ActivityOptions opts = pendingOptions;
1211 pendingOptions = null;
1212 return opts;
1213 }
1214
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001215 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001216 if (uriPermissions != null) {
1217 uriPermissions.removeUriPermissionsLocked();
1218 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001219 }
1220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221
1222 void pauseKeyDispatchingLocked() {
1223 if (!keysPaused) {
1224 keysPaused = true;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001225 service.mWindowManager.pauseKeyDispatching(appToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227 }
1228
1229 void resumeKeyDispatchingLocked() {
1230 if (keysPaused) {
1231 keysPaused = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001232 service.mWindowManager.resumeKeyDispatching(appToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
1234 }
1235
Winson Chung740c3ac2014-11-12 16:14:38 -08001236 void updateThumbnailLocked(Bitmap newThumbnail, CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001237 if (newThumbnail != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001238 if (DEBUG_THUMBNAILS) Slog.i(TAG_THUMBNAILS,
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001239 "Setting thumbnail of " + this + " to " + newThumbnail);
Winsonc809cbb2015-11-02 12:06:15 -08001240 boolean thumbnailUpdated = task.setLastThumbnailLocked(newThumbnail);
Winson Chung096f36b2014-08-20 15:39:01 -07001241 if (thumbnailUpdated && isPersistable()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001242 service.notifyTaskPersisterLocked(task, false);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001243 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001244 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001245 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001246 }
1247
Andrii Kulian21713ac2016-10-12 22:05:05 -07001248 final Bitmap screenshotActivityLocked() {
1249 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivityLocked: " + this);
1250 if (noDisplay) {
1251 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
1252 return null;
1253 }
1254
1255 final ActivityStack stack = getStack();
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001256 if (stack.isHomeOrRecentsStack()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001257 // This is an optimization -- since we never show Home or Recents within Recents itself,
1258 // we can just go ahead and skip taking the screenshot if this is the home stack.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001259 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, stack.getStackId() == HOME_STACK_ID ?
1260 "\tHome stack" : "\tRecents stack");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001261 return null;
1262 }
1263
1264 int w = service.mThumbnailWidth;
1265 int h = service.mThumbnailHeight;
1266
1267 if (w <= 0) {
1268 Slog.e(TAG, "\tInvalid thumbnail dimensions: " + w + "x" + h);
1269 return null;
1270 }
1271
1272 if (stack.mStackId == DOCKED_STACK_ID && mStackSupervisor.mIsDockMinimized) {
1273 // When the docked stack is minimized its app windows are cropped significantly so any
1274 // screenshot taken will not display the apps contain. So, we avoid taking a screenshot
1275 // in that case.
1276 if (DEBUG_SCREENSHOTS) Slog.e(TAG, "\tIn minimized docked stack");
1277 return null;
1278 }
1279
1280 final float scale;
1281 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
1282
1283 // When this flag is set, we currently take the fullscreen screenshot of the activity but
1284 // scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to use within
1285 // SystemUI while keeping memory usage low.
1286 if (TAKE_FULLSCREEN_SCREENSHOTS) {
1287 w = h = -1;
1288 scale = service.mFullscreenThumbnailScale;
1289 }
1290
1291 return service.mWindowManager.screenshotApplications(appToken, DEFAULT_DISPLAY, w, h,
1292 scale);
1293 }
1294
1295 void setVisible(boolean newVisible) {
1296 visible = newVisible;
1297 if (!visible && mUpdateTaskThumbnailWhenHidden) {
1298 updateThumbnailLocked(screenshotActivityLocked(), null /* description */);
1299 mUpdateTaskThumbnailWhenHidden = false;
1300 }
1301 service.mWindowManager.setAppVisibility(appToken, visible);
1302 final ArrayList<ActivityContainer> containers = mChildContainers;
1303 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
1304 final ActivityContainer container = containers.get(containerNdx);
1305 container.setVisible(visible);
1306 }
1307 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1308 }
1309
Jorim Jaggi241ae102016-11-02 21:57:33 -07001310 /**
1311 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1312 * might have on the visibility
1313 *
1314 * @see {@link ActivityStack#checkKeyguardVisibility}
1315 */
1316 boolean shouldBeVisibleIgnoringKeyguard(boolean behindTranslucentActivity,
1317 boolean stackVisibleBehind, ActivityRecord visibleBehind,
1318 boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001319 if (!okToShowLocked()) {
1320 return false;
1321 }
1322
1323 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1324 // but must be drawn initially for the animation as though they were visible.
1325 final boolean activityVisibleBehind =
1326 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == this;
1327
1328 boolean isVisible =
1329 !behindFullscreenActivity || mLaunchTaskBehind || activityVisibleBehind;
1330
1331 if (service.mSupportsLeanbackOnly && isVisible && isRecentsActivity()) {
1332 // On devices that support leanback only (Android TV), Recents activity can only be
1333 // visible if the home stack is the focused stack or we are in split-screen mode.
1334 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1335 || mStackSupervisor.isFocusedStack(getStack());
1336 }
1337
1338 return isVisible;
1339 }
1340
1341 void makeVisibleIfNeeded(ActivityRecord starting) {
1342 // This activity is not currently visible, but is running. Tell it to become visible.
1343 if (state == ActivityState.RESUMED || this == starting) {
1344 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1345 "Not making visible, r=" + this + " state=" + state + " starting=" + starting);
1346 return;
1347 }
1348
1349 // If this activity is paused, tell it to now show its window.
1350 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1351 "Making visible and scheduling visibility: " + this);
1352 final ActivityStack stack = getStack();
1353 try {
1354 if (stack.mTranslucentActivityWaiting != null) {
1355 updateOptionsLocked(returningOptions);
1356 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1357 }
1358 setVisible(true);
1359 sleeping = false;
1360 app.pendingUiClean = true;
1361 app.thread.scheduleWindowVisibility(appToken, true /* showWindow */);
1362 // The activity may be waiting for stop, but that is no longer appropriate for it.
1363 mStackSupervisor.mStoppingActivities.remove(this);
1364 mStackSupervisor.mGoingToSleepActivities.remove(this);
1365 } catch (Exception e) {
1366 // Just skip on any failure; we'll make it visible when it next restarts.
1367 Slog.w(TAG, "Exception thrown making visibile: " + intent.getComponent(), e);
1368 }
1369 handleAlreadyVisible();
1370 }
1371
1372 boolean handleAlreadyVisible() {
1373 stopFreezingScreenLocked(false);
1374 try {
1375 if (returningOptions != null) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001376 app.thread.scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001377 }
1378 } catch(RemoteException e) {
1379 }
1380 return state == ActivityState.RESUMED;
1381 }
1382
1383 static void activityResumedLocked(IBinder token) {
1384 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1385 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1386 if (r != null) {
1387 r.icicle = null;
1388 r.haveState = false;
1389 }
1390 }
1391
1392 /**
1393 * Once we know that we have asked an application to put an activity in the resumed state
1394 * (either by launching it or explicitly telling it), this function updates the rest of our
1395 * state to match that fact.
1396 */
1397 void completeResumeLocked() {
1398 final boolean wasVisible = visible;
1399 visible = true;
1400 if (!wasVisible) {
1401 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1402 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1403 }
1404 idle = false;
1405 results = null;
1406 newIntents = null;
1407 stopped = false;
1408
1409 if (isHomeActivity()) {
1410 ProcessRecord app = task.mActivities.get(0).app;
1411 if (app != null && app != service.mHomeProcess) {
1412 service.mHomeProcess = app;
1413 }
1414 }
1415
1416 if (nowVisible) {
1417 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1418 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001419 }
1420
1421 // Schedule an idle timeout in case the app doesn't do it for us.
1422 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1423
1424 mStackSupervisor.reportResumedActivityLocked(this);
1425
1426 resumeKeyDispatchingLocked();
1427 final ActivityStack stack = getStack();
1428 stack.mNoAnimActivities.clear();
1429
1430 // Mark the point when the activity is resuming
1431 // TODO: To be more accurate, the mark should be before the onCreate,
1432 // not after the onResume. But for subsequent starts, onResume is fine.
1433 if (app != null) {
1434 cpuTimeAtResume = service.mProcessCpuTracker.getCpuTimeForPid(app.pid);
1435 } else {
1436 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1437 }
1438
1439 returningOptions = null;
1440
1441 if (stack.getVisibleBehindActivity() == this) {
1442 // When resuming an activity, require it to call requestVisibleBehind() again.
1443 stack.setVisibleBehindActivity(null /* ActivityRecord */);
1444 }
1445 mStackSupervisor.checkReadyForSleepLocked();
1446 }
1447
1448 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1449 CharSequence description) {
1450 final ActivityStack stack = getStack();
1451 if (state != ActivityState.STOPPING) {
1452 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
1453 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1454 return;
1455 }
1456 if (newPersistentState != null) {
1457 persistentState = newPersistentState;
1458 service.notifyTaskPersisterLocked(task, false);
1459 }
1460 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
1461 if (newIcicle != null) {
1462 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1463 // the state.
1464 icicle = newIcicle;
1465 haveState = true;
1466 launchCount = 0;
1467 updateThumbnailLocked(null /* newThumbnail */, description);
1468 }
1469 if (!stopped) {
1470 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
1471 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1472 stopped = true;
1473 state = ActivityState.STOPPED;
1474
1475 service.mWindowManager.notifyAppStopped(appToken);
1476
1477 if (stack.getVisibleBehindActivity() == this) {
1478 mStackSupervisor.requestVisibleBehindLocked(this, false /* visible */);
1479 }
1480 if (finishing) {
1481 clearOptionsLocked();
1482 } else {
1483 if (deferRelaunchUntilPaused) {
1484 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
1485 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1486 } else {
1487 mStackSupervisor.updatePreviousProcessLocked(this);
1488 }
1489 }
1490 }
1491 }
1492
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001493 void startLaunchTickingLocked() {
1494 if (ActivityManagerService.IS_USER_BUILD) {
1495 return;
1496 }
1497 if (launchTickTime == 0) {
1498 launchTickTime = SystemClock.uptimeMillis();
1499 continueLaunchTickingLocked();
1500 }
1501 }
1502
1503 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001504 if (launchTickTime == 0) {
1505 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001506 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001507
Andrii Kulian02b7a832016-10-06 23:11:56 -07001508 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001509 if (stack == null) {
1510 return false;
1511 }
1512
1513 Message msg = stack.mHandler.obtainMessage(ActivityStack.LAUNCH_TICK_MSG, this);
1514 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1515 stack.mHandler.sendMessageDelayed(msg, ActivityStack.LAUNCH_TICK);
1516 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001517 }
1518
1519 void finishLaunchTickingLocked() {
1520 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001521 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001522 if (stack != null) {
1523 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1524 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001525 }
1526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 // IApplicationToken
1528
1529 public boolean mayFreezeScreenLocked(ProcessRecord app) {
1530 // Only freeze the screen if this activity is currently attached to
1531 // an application, and that application is not blocked or unresponding.
1532 // In any other case, we can't count on getting the screen unfrozen,
1533 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -08001534 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
1538 if (mayFreezeScreenLocked(app)) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001539 service.mWindowManager.startAppFreezingScreen(appToken, configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
1541 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 public void stopFreezingScreenLocked(boolean force) {
1544 if (force || frozenBeforeDestroy) {
1545 frozenBeforeDestroy = false;
Dianne Hackbornbe707852011-11-11 14:32:10 -08001546 service.mWindowManager.stopAppFreezingScreen(appToken, force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001549
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001550 public void reportFullyDrawnLocked() {
1551 final long curTime = SystemClock.uptimeMillis();
1552 if (displayStartTime != 0) {
1553 reportLaunchTimeLocked(curTime);
1554 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001555 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001556 if (fullyDrawnStartTime != 0 && stack != null) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001557 final long thisTime = curTime - fullyDrawnStartTime;
1558 final long totalTime = stack.mFullyDrawnStartTime != 0
1559 ? (curTime - stack.mFullyDrawnStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001560 if (SHOW_ACTIVITY_START_TIME) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001561 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001562 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME,
1563 userId, System.identityHashCode(this), shortComponentName,
1564 thisTime, totalTime);
1565 StringBuilder sb = service.mStringBuilder;
1566 sb.setLength(0);
1567 sb.append("Fully drawn ");
1568 sb.append(shortComponentName);
1569 sb.append(": ");
1570 TimeUtils.formatDuration(thisTime, sb);
1571 if (thisTime != totalTime) {
1572 sb.append(" (total ");
1573 TimeUtils.formatDuration(totalTime, sb);
1574 sb.append(")");
1575 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001576 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001577 }
1578 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001579 //service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001580 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001581 stack.mFullyDrawnStartTime = 0;
1582 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001583 fullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001584 }
1585
1586 private void reportLaunchTimeLocked(final long curTime) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001587 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001588 if (stack == null) {
1589 return;
1590 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001591 final long thisTime = curTime - displayStartTime;
1592 final long totalTime = stack.mLaunchStartTime != 0
1593 ? (curTime - stack.mLaunchStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001594 if (SHOW_ACTIVITY_START_TIME) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001595 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001596 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME,
1597 userId, System.identityHashCode(this), shortComponentName,
1598 thisTime, totalTime);
1599 StringBuilder sb = service.mStringBuilder;
1600 sb.setLength(0);
1601 sb.append("Displayed ");
1602 sb.append(shortComponentName);
1603 sb.append(": ");
1604 TimeUtils.formatDuration(thisTime, sb);
1605 if (thisTime != totalTime) {
1606 sb.append(" (total ");
1607 TimeUtils.formatDuration(totalTime, sb);
1608 sb.append(")");
1609 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001610 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001611 }
1612 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
1613 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001614 //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001615 }
1616 displayStartTime = 0;
1617 stack.mLaunchStartTime = 0;
1618 }
1619
Wale Ogunwale7d701172015-03-11 15:36:30 -07001620 void windowsDrawnLocked() {
Jorim Jaggi275561a2016-02-23 10:11:02 -05001621 mStackSupervisor.mActivityMetricsLogger.notifyWindowsDrawn();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001622 if (displayStartTime != 0) {
Chong Zhang5022da32016-06-21 16:31:37 -07001623 reportLaunchTimeLocked(SystemClock.uptimeMillis());
Wale Ogunwale7d701172015-03-11 15:36:30 -07001624 }
1625 mStackSupervisor.sendWaitingVisibleReportLocked(this);
1626 startTime = 0;
1627 finishLaunchTickingLocked();
1628 if (task != null) {
1629 task.hasBeenVisible = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001630 }
1631 }
1632
Wale Ogunwale7d701172015-03-11 15:36:30 -07001633 void windowsVisibleLocked() {
1634 mStackSupervisor.reportActivityVisibleLocked(this);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001635 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
Wale Ogunwale7d701172015-03-11 15:36:30 -07001636 if (!nowVisible) {
1637 nowVisible = true;
1638 lastVisibleTime = SystemClock.uptimeMillis();
1639 if (!idle) {
1640 // Instead of doing the full stop routine here, let's just hide any activities
1641 // we now can, and let them stop when the normal idle happens.
1642 mStackSupervisor.processStoppingActivitiesLocked(false);
1643 } else {
1644 // If this activity was already idle, then we now need to make sure we perform
1645 // the full stop of any activities that are waiting to do so. This is because
1646 // we won't do that while they are still waiting for this one to become visible.
1647 final int size = mStackSupervisor.mWaitingVisibleActivities.size();
1648 if (size > 0) {
1649 for (int i = 0; i < size; i++) {
1650 ActivityRecord r = mStackSupervisor.mWaitingVisibleActivities.get(i);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001651 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001653 mStackSupervisor.mWaitingVisibleActivities.clear();
1654 mStackSupervisor.scheduleIdleLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001657 service.scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
1659 }
1660
Wale Ogunwale7d701172015-03-11 15:36:30 -07001661 ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001662 // First find the real culprit... if this activity is waiting for
1663 // another activity to start or has stopped, then the key dispatching
1664 // timeout should not be caused by this.
Wale Ogunwale16a62792015-04-01 11:50:31 -07001665 if (mStackSupervisor.mWaitingVisibleActivities.contains(this) || stopped) {
Craig Mautner8f2adcb2014-04-07 22:21:33 +00001666 final ActivityStack stack = mStackSupervisor.getFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001667 // Try to use the one which is closest to top.
1668 ActivityRecord r = stack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001670 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001672 if (r != null) {
1673 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
1675 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001676 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678
Chong Zhang87761972016-08-22 13:53:24 -07001679 /** Checks whether the activity should be shown for current user. */
1680 public boolean okToShowLocked() {
1681 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
1682 || (mStackSupervisor.isCurrentProfileLocked(userId)
1683 && !service.mUserController.isUserStoppingOrShuttingDownLocked(userId));
1684 }
1685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 /**
1687 * This method will return true if the activity is either visible, is becoming visible, is
1688 * currently pausing, or is resumed.
1689 */
1690 public boolean isInterestingToUserLocked() {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001691 return visible || nowVisible || state == ActivityState.PAUSING ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 state == ActivityState.RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001693 }
1694
Wale Ogunwale3e997362016-09-06 10:37:56 -07001695 void setSleeping(boolean _sleeping) {
1696 setSleeping(_sleeping, false);
1697 }
1698
1699 void setSleeping(boolean _sleeping, boolean force) {
1700 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001701 return;
1702 }
1703 if (app != null && app.thread != null) {
1704 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001705 app.thread.scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001706 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
1707 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001708 }
1709 sleeping = _sleeping;
1710 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001711 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001712 }
1713 }
1714 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08001715
Craig Mautnerd2328952013-03-05 12:46:26 -08001716 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001717 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08001718 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001719 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001720 }
1721 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001722 final int activityNdx = task.mActivities.indexOf(r);
1723 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001724 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001725 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001726 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08001727 }
1728
1729 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001730 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001731 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001732 }
1733
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001734 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001735 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1736 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001737 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08001738 }
1739 return null;
1740 }
1741
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001742 /**
1743 * @return display id to which this record is attached, -1 if not attached.
1744 */
1745 int getDisplayId() {
1746 final ActivityStack stack = getStack();
1747 if (stack == null) {
1748 return -1;
1749 }
1750 return stack.mDisplayId;
1751 }
1752
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001753 final boolean isDestroyable() {
1754 if (finishing || app == null || state == ActivityState.DESTROYING
1755 || state == ActivityState.DESTROYED) {
1756 // This would be redundant.
1757 return false;
1758 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001759 final ActivityStack stack = getStack();
1760 if (stack == null || this == stack.mResumedActivity || this == stack.mPausingActivity
1761 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001762 // We're not ready for this kind of thing.
1763 return false;
1764 }
1765 if (visible) {
1766 // The user would notice this!
1767 return false;
1768 }
1769 return true;
1770 }
1771
Winson Chung3bad5cc02014-08-19 17:44:32 -07001772 private static String createImageFilename(long createTime, int taskId) {
1773 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001774 TaskPersister.IMAGE_EXTENSION;
1775 }
1776
Craig Mautner648f69b2014-09-18 14:16:26 -07001777 void setTaskDescription(TaskDescription _taskDescription) {
1778 Bitmap icon;
1779 if (_taskDescription.getIconFilename() == null &&
1780 (icon = _taskDescription.getIcon()) != null) {
1781 final String iconFilename = createImageFilename(createTime, task.taskId);
Suprabh Shukla23593142015-11-03 17:31:15 -08001782 final File iconFile = new File(TaskPersister.getUserImagesDir(userId), iconFilename);
1783 final String iconFilePath = iconFile.getAbsolutePath();
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001784 service.mRecentTasks.saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08001785 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07001786 }
1787 taskDescription = _taskDescription;
1788 }
1789
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001790 void setVoiceSessionLocked(IVoiceInteractionSession session) {
1791 voiceSession = session;
1792 pendingVoiceInteractionStart = false;
1793 }
1794
1795 void clearVoiceSessionLocked() {
1796 voiceSession = null;
1797 pendingVoiceInteractionStart = false;
1798 }
1799
Wale Ogunwale3b232392016-05-13 15:37:13 -07001800 void showStartingWindow(ActivityRecord prev, boolean createIfNeeded) {
1801 final CompatibilityInfo compatInfo =
1802 service.compatibilityInfoForPackageLocked(info.applicationInfo);
1803 final boolean shown = service.mWindowManager.setAppStartingWindow(
1804 appToken, packageName, theme, compatInfo, nonLocalizedLabel, labelRes, icon,
1805 logo, windowFlags, prev != null ? prev.appToken : null, createIfNeeded);
1806 if (shown) {
1807 mStartingWindowState = STARTING_WINDOW_SHOWN;
1808 }
1809 }
1810
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001811 void setRequestedOrientation(int requestedOrientation) {
1812 if (task != null && (!task.mFullscreen || !task.getStack().mFullscreen)) {
1813 // Fixed screen orientation isn't supported when activities aren't in full screen mode.
1814 return;
1815 }
1816
1817 service.mWindowManager.setAppOrientation(appToken, requestedOrientation);
1818 final int displayId = getDisplayId();
1819 final Configuration config = service.mWindowManager.updateOrientationFromAppTokens(
1820 mStackSupervisor.getDisplayOverrideConfiguration(displayId),
1821 mayFreezeScreenLocked(app) ? appToken : null, displayId);
1822 if (config != null) {
1823 frozenBeforeDestroy = true;
1824 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
1825 false /* deferResume */, displayId)) {
1826 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1827 }
1828 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001829 service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
1830 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001831 }
1832
Andrii Kulian21713ac2016-10-12 22:05:05 -07001833 // TODO: now used only in one place to address race-condition. Remove when that will be fixed.
1834 void setLastReportedConfiguration(@NonNull Configuration config) {
1835 mLastReportedConfiguration.setTo(config);
1836 }
1837
1838 /** Call when override config was sent to the Window Manager to update internal records. */
1839 void onOverrideConfigurationSent() {
1840 mLastReportedOverrideConfiguration.setTo(task.getMergedOverrideConfiguration());
1841 }
1842
1843 /**
1844 * Make sure the given activity matches the current configuration. Returns false if the activity
1845 * had to be destroyed. Returns true if the configuration is the same, or the activity will
1846 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
1847 * correct configuration and all other bookkeeping is handled.
1848 */
1849 boolean ensureActivityConfigurationLocked(int globalChanges, boolean preserveWindow) {
1850 final ActivityStack stack = getStack();
1851 if (stack.mConfigWillChange) {
1852 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1853 "Skipping config check (will change): " + this);
1854 return true;
1855 }
1856
1857 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1858 "Ensuring correct configuration: " + this);
1859
1860 // Short circuit: if the two configurations are equal (the common case), then there is
1861 // nothing to do.
1862 final Configuration newGlobalConfig = service.getGlobalConfiguration();
1863 final Configuration newTaskMergedOverrideConfig = task.getMergedOverrideConfiguration();
1864 if (mLastReportedConfiguration.equals(newGlobalConfig)
1865 && mLastReportedOverrideConfiguration.equals(newTaskMergedOverrideConfig)
1866 && !forceNewConfig) {
1867 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1868 "Configuration unchanged in " + this);
1869 return true;
1870 }
1871
1872 // We don't worry about activities that are finishing.
1873 if (finishing) {
1874 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1875 "Configuration doesn't matter in finishing " + this);
1876 stopFreezingScreenLocked(false);
1877 return true;
1878 }
1879
1880 // Okay we now are going to make this activity have the new config.
1881 // But then we need to figure out how it needs to deal with that.
1882 mTmpGlobalConfig.setTo(mLastReportedConfiguration);
1883 mTmpTaskConfig.setTo(mLastReportedOverrideConfiguration);
1884 mLastReportedConfiguration.setTo(newGlobalConfig);
1885 mLastReportedOverrideConfiguration.setTo(newTaskMergedOverrideConfig);
1886
1887 int taskChanges = getTaskConfigurationChanges(this, newTaskMergedOverrideConfig,
1888 mTmpTaskConfig);
1889 final int changes = mTmpGlobalConfig.diff(newGlobalConfig) | taskChanges;
1890 if (changes == 0 && !forceNewConfig) {
1891 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1892 "Configuration no differences in " + this);
1893 // There are no significant differences, so we won't relaunch but should still deliver
1894 // the new configuration to the client process.
1895 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
1896 return true;
1897 }
1898
1899 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1900 "Configuration changes for " + this + " ; taskChanges="
1901 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
1902 + Configuration.configurationDiffToString(changes));
1903
1904 // If the activity isn't currently running, just leave the new configuration and it will
1905 // pick that up next time it starts.
1906 if (app == null || app.thread == null) {
1907 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1908 "Configuration doesn't matter not running " + this);
1909 stopFreezingScreenLocked(false);
1910 forceNewConfig = false;
1911 return true;
1912 }
1913
1914 // Figure out how to handle the changes between the configurations.
1915 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1916 "Checking to restart " + info.name + ": changed=0x"
1917 + Integer.toHexString(changes) + ", handles=0x"
1918 + Integer.toHexString(info.getRealConfigChanged())
1919 + ", newGlobalConfig=" + newGlobalConfig
1920 + ", newTaskMergedOverrideConfig=" + newTaskMergedOverrideConfig);
1921
1922 if ((changes&(~info.getRealConfigChanged())) != 0 || forceNewConfig) {
1923 // Aha, the activity isn't handling the change, so DIE DIE DIE.
1924 configChangeFlags |= changes;
1925 startFreezingScreenLocked(app, globalChanges);
1926 forceNewConfig = false;
1927 preserveWindow &= isResizeOnlyChange(changes);
1928 if (app == null || app.thread == null) {
1929 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1930 "Config is destroying non-running " + this);
1931 stack.destroyActivityLocked(this, true, "config");
1932 } else if (state == ActivityState.PAUSING) {
1933 // A little annoying: we are waiting for this activity to finish pausing. Let's not
1934 // do anything now, but just flag that it needs to be restarted when done pausing.
1935 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1936 "Config is skipping already pausing " + this);
1937 deferRelaunchUntilPaused = true;
1938 preserveWindowOnDeferredRelaunch = preserveWindow;
1939 return true;
1940 } else if (state == ActivityState.RESUMED) {
1941 // Try to optimize this case: the configuration is changing and we need to restart
1942 // the top, resumed activity. Instead of doing the normal handshaking, just say
1943 // "restart!".
1944 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1945 "Config is relaunching resumed " + this);
1946
1947 if (DEBUG_STATES && !visible) {
1948 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
1949 + " called by " + Debug.getCallers(4));
1950 }
1951
1952 relaunchActivityLocked(true /* andResume */, preserveWindow);
1953 } else {
1954 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1955 "Config is relaunching non-resumed " + this);
1956 relaunchActivityLocked(false /* andResume */, preserveWindow);
1957 }
1958
1959 // All done... tell the caller we weren't able to keep this activity around.
1960 return false;
1961 }
1962
1963 // Default case: the activity can handle this new configuration, so hand it over.
1964 // NOTE: We only forward the task override configuration as the system level configuration
1965 // changes is always sent to all processes when they happen so it can just use whatever
1966 // system level configuration it last got.
1967 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
1968 stopFreezingScreenLocked(false);
1969
1970 return true;
1971 }
1972
1973 private static int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
1974 Configuration oldTaskOverride) {
1975 // If we went from full-screen to non-full-screen, make sure to use the correct
1976 // configuration task diff, so the diff stays as small as possible.
1977 if (Configuration.EMPTY.equals(oldTaskOverride)
1978 && !Configuration.EMPTY.equals(taskConfig)) {
1979 oldTaskOverride = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
1980 }
1981
1982 // Conversely, do the same when going the other direction.
1983 if (Configuration.EMPTY.equals(taskConfig)
1984 && !Configuration.EMPTY.equals(oldTaskOverride)) {
1985 taskConfig = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
1986 }
1987
1988 // Determine what has changed. May be nothing, if this is a config that has come back from
1989 // the app after going idle. In that case we just want to leave the official config object
1990 // now in the activity and do nothing else.
1991 int taskChanges = oldTaskOverride.diff(taskConfig, true /* skipUndefined */);
1992 // We don't want to use size changes if they don't cross boundaries that are important to
1993 // the app.
1994 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
1995 final boolean crosses = record.crossesHorizontalSizeThreshold(
1996 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
1997 || record.crossesVerticalSizeThreshold(
1998 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
1999 if (!crosses) {
2000 taskChanges &= ~CONFIG_SCREEN_SIZE;
2001 }
2002 }
2003 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
2004 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
2005 final int newSmallest = taskConfig.smallestScreenWidthDp;
2006 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
2007 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
2008 }
2009 }
2010 return taskChanges;
2011 }
2012
2013 private static boolean isResizeOnlyChange(int change) {
2014 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2015 | CONFIG_SCREEN_LAYOUT)) == 0;
2016 }
2017
2018 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
2019 if (service.mSuppressResizeConfigChanges && preserveWindow) {
2020 configChangeFlags = 0;
2021 return;
2022 }
2023
2024 List<ResultInfo> pendingResults = null;
2025 List<ReferrerIntent> pendingNewIntents = null;
2026 if (andResume) {
2027 pendingResults = results;
2028 pendingNewIntents = newIntents;
2029 }
2030 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2031 "Relaunching: " + this + " with results=" + pendingResults
2032 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2033 + " preserveWindow=" + preserveWindow);
2034 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
2035 : EventLogTags.AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
2036 task.taskId, shortComponentName);
2037
2038 startFreezingScreenLocked(app, 0);
2039
2040 mStackSupervisor.removeChildActivityContainers(this);
2041
2042 try {
2043 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2044 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2045 + " callers=" + Debug.getCallers(6));
2046 forceNewConfig = false;
2047 mStackSupervisor.activityRelaunchingLocked(this);
2048 app.thread.scheduleRelaunchActivity(appToken, pendingResults, pendingNewIntents,
2049 configChangeFlags, !andResume,
2050 new Configuration(service.getGlobalConfiguration()),
2051 new Configuration(task.getMergedOverrideConfiguration()), preserveWindow);
2052 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
2053 // pass in 'andResume' if this activity is currently resumed, which implies we aren't
2054 // sleeping.
2055 } catch (RemoteException e) {
2056 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2057 }
2058
2059 if (andResume) {
2060 if (DEBUG_STATES) {
2061 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2062 }
2063 results = null;
2064 newIntents = null;
2065 service.showUnsupportedZoomDialogIfNeededLocked(this);
2066 service.showAskCompatModeDialogLocked(this);
2067 } else {
2068 service.mHandler.removeMessages(ActivityStack.PAUSE_TIMEOUT_MSG, this);
2069 state = ActivityState.PAUSED;
2070 // if the app is relaunched when it's stopped, and we're not resuming,
2071 // put it back into stopped state.
2072 if (stopped) {
2073 getStack().addToStopping(this, true /* immediate */);
2074 }
2075 }
2076
2077 configChangeFlags = 0;
2078 deferRelaunchUntilPaused = false;
2079 preserveWindowOnDeferredRelaunch = false;
2080 }
2081
Craig Mautner21d24a22014-04-23 11:45:37 -07002082 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2083 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2084 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2085 if (launchedFromPackage != null) {
2086 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2087 }
2088 if (resolvedType != null) {
2089 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2090 }
2091 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2092 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002093
Craig Mautner21d24a22014-04-23 11:45:37 -07002094 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002095 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002096 }
2097
2098 out.startTag(null, TAG_INTENT);
2099 intent.saveToXml(out);
2100 out.endTag(null, TAG_INTENT);
2101
2102 if (isPersistable() && persistentState != null) {
2103 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2104 persistentState.saveToXml(out);
2105 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2106 }
2107 }
2108
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002109 static ActivityRecord restoreFromXml(XmlPullParser in,
2110 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002111 Intent intent = null;
2112 PersistableBundle persistentState = null;
2113 int launchedFromUid = 0;
2114 String launchedFromPackage = null;
2115 String resolvedType = null;
2116 boolean componentSpecified = false;
2117 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002118 long createTime = -1;
2119 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002120 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002121
2122 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2123 final String attrName = in.getAttributeName(attrNdx);
2124 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002125 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002126 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002127 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002128 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002129 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002130 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002131 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2132 launchedFromPackage = attrValue;
2133 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2134 resolvedType = attrValue;
2135 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002136 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002137 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002138 userId = Integer.parseInt(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07002139 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
2140 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002141 } else {
2142 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2143 }
2144 }
2145
2146 int event;
2147 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07002148 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002149 if (event == XmlPullParser.START_TAG) {
2150 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002151 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002152 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002153 if (TAG_INTENT.equals(name)) {
2154 intent = Intent.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002155 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002156 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002157 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2158 persistentState = PersistableBundle.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002159 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002160 "ActivityRecord: persistentState=" + persistentState);
2161 } else {
2162 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2163 XmlUtils.skipCurrentTag(in);
2164 }
2165 }
2166 }
2167
2168 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002169 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002170 }
2171
2172 final ActivityManagerService service = stackSupervisor.mService;
2173 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Jeff Hao1b012d32014-08-20 10:35:34 -07002174 userId);
Craig Mautnere0129b32014-05-25 16:41:09 -07002175 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002176 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2177 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002178 }
Craig Mautner21d24a22014-04-23 11:45:37 -07002179 final ActivityRecord r = new ActivityRecord(service, /*caller*/null, launchedFromUid,
2180 launchedFromPackage, intent, resolvedType, aInfo, service.getConfiguration(),
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07002181 null, null, 0, componentSpecified, false, stackSupervisor, null, null, null);
Craig Mautner21d24a22014-04-23 11:45:37 -07002182
2183 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002184 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002185 r.createTime = createTime;
2186
2187 return r;
2188 }
2189
2190 private static String activityTypeToString(int type) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002191 switch (type) {
2192 case APPLICATION_ACTIVITY_TYPE: return "APPLICATION_ACTIVITY_TYPE";
2193 case HOME_ACTIVITY_TYPE: return "HOME_ACTIVITY_TYPE";
2194 case RECENTS_ACTIVITY_TYPE: return "RECENTS_ACTIVITY_TYPE";
2195 default: return Integer.toString(type);
2196 }
2197 }
2198
Craig Mautnerf81b90872013-02-26 13:02:43 -08002199 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002201 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002202 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002203 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002204 }
2205 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002206 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002207 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002208 sb.append(" u");
2209 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002210 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002211 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002212 stringName = sb.toString();
2213 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 }
2215}