blob: 0e4ab96cccb49afee1933cfb21bafd2f525b5057 [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;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080024import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070025import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
26import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
27import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
28import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080029import static android.content.pm.ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
30import static android.content.pm.ActivityInfo.FLAG_IMMERSIVE;
31import static android.content.pm.ActivityInfo.FLAG_MULTIPROCESS;
Chong Zhang87761972016-08-22 13:53:24 -070032import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale6cae7652015-12-26 07:36:26 -080033import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080034import static android.content.pm.ActivityInfo.FLAG_STATE_NOT_NEEDED;
35import static android.content.pm.ActivityInfo.LAUNCH_MULTIPLE;
36import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070037import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Jorim Jaggicd13d332016-04-27 15:40:20 -070038import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
Wale Ogunwaled26176f2016-01-25 20:04:04 -080039import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE;
Wale Ogunwale72a73e32016-10-13 12:16:39 -070040import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Wale Ogunwaledf241e92016-10-13 15:14:21 -070041import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080042import static android.os.Build.VERSION_CODES.HONEYCOMB;
43import static android.os.Process.SYSTEM_UID;
Wale Ogunwaleeb915f22016-10-14 07:18:22 -070044import static android.view.Display.DEFAULT_DISPLAY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080045import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
Andrii Kulian21713ac2016-10-12 22:05:05 -070046import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SCREENSHOTS;
48import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080049import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
50import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070051import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
52import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
53import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
54import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080055import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
57import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070058import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080059import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
60import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Andrii Kulian21713ac2016-10-12 22:05:05 -070061import static com.android.server.am.ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS;
Wale Ogunwale18795a22014-12-03 11:38:33 -080062import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
63
Andrii Kulian21713ac2016-10-12 22:05:05 -070064import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -070065import android.app.ActivityManager.TaskDescription;
Winson Chunga29eb982016-12-14 12:01:27 -080066import android.app.ActivityManagerInternal.PictureInPictureArguments;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070067import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080068import android.app.PendingIntent;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080069import android.app.ResultInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.ComponentName;
71import android.content.Intent;
72import android.content.pm.ActivityInfo;
73import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -070074import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.content.res.Configuration;
76import android.graphics.Bitmap;
Dianne Hackbornd367ca82012-05-07 15:49:39 -070077import android.graphics.Rect;
Dianne Hackborn247fe742011-01-08 17:25:57 -080078import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -070080import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -080081import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080083import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -070085import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.SystemClock;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080087import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070088import android.os.UserHandle;
Amith Yamasani0af6fa72016-01-17 15:36:19 -080089import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.EventLog;
91import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -070093import android.util.TimeUtils;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -070094import android.view.AppTransitionAnimationSpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.IApplicationToken;
Jorim Jaggife762342016-10-13 14:33:27 +020096import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -070097
Filip Gruszczynski3d026712015-12-16 13:46:38 -080098import com.android.internal.app.ResolverActivity;
99import com.android.internal.content.ReferrerIntent;
100import com.android.internal.util.XmlUtils;
101import com.android.server.AttributeCache;
102import com.android.server.am.ActivityStack.ActivityState;
103import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800104import com.android.server.wm.AppWindowContainerController;
105import com.android.server.wm.AppWindowContainerListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
Suprabh Shukla23593142015-11-03 17:31:15 -0800107import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -0700108import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import java.io.PrintWriter;
110import java.lang.ref.WeakReference;
111import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800112import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700114import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700115import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800117import org.xmlpull.v1.XmlPullParser;
118import org.xmlpull.v1.XmlPullParserException;
119import org.xmlpull.v1.XmlSerializer;
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121/**
122 * An entry in the history stack, representing an activity.
123 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800124final class ActivityRecord implements AppWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800125 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_AM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700126 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
127 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
128 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700129 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700130 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
131 private static final String TAG_THUMBNAILS = TAG + POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700132 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800133
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700134 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700135 private static final String RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700136
Craig Mautner21d24a22014-04-23 11:45:37 -0700137 private static final String ATTR_ID = "id";
138 private static final String TAG_INTENT = "intent";
139 private static final String ATTR_USERID = "user_id";
140 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
141 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700142 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700143 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
144 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700145 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 final ActivityManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800148 final IApplicationToken.Stub appToken; // window manager token
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800149 AppWindowContainerController mWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 final ActivityInfo info; // all about me
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700151 final ApplicationInfo appInfo; // information about activity's app
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700152 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800153 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700154 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 final Intent intent; // the original intent that generated us
156 final ComponentName realActivity; // the intent component, or target of an alias.
157 final String shortComponentName; // the short component name of the intent
158 final String resolvedType; // as per original caller;
159 final String packageName; // the package implementing intent's component
160 final String processName; // process where this component wants to run
161 final String taskAffinity; // as per ActivityInfo.taskAffinity
162 final boolean stateNotNeeded; // As per ActivityInfo.flags
Craig Mautner4addfc52013-06-25 08:05:45 -0700163 boolean fullscreen; // covers the full screen?
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800164 final boolean noDisplay; // activity is not displayed?
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800165 private final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700166 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700167
168 static final int APPLICATION_ACTIVITY_TYPE = 0;
169 static final int HOME_ACTIVITY_TYPE = 1;
170 static final int RECENTS_ACTIVITY_TYPE = 2;
Craig Mautner2c1faed2013-07-23 12:56:02 -0700171 int mActivityType;
Craig Mautner86d67a42013-05-14 10:34:38 -0700172
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800173 private CharSequence nonLocalizedLabel; // the label information from the package mgr.
174 private int labelRes; // the label information from the package mgr.
175 private int icon; // resource identifier of activity's icon.
176 private int logo; // resource identifier of activity's logo.
177 private int theme; // resource identifier of activity's theme.
178 private int realTheme; // actual theme resource we will use, never 0.
179 private int windowFlags; // custom window flags for preview window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 TaskRecord task; // the task this is in.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800181 private long createTime = System.currentTimeMillis();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700182 long displayStartTime; // when we started launching this activity
183 long fullyDrawnStartTime; // when we started launching this activity
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800184 private long startTime; // last time this activity was started
Dianne Hackborn50685602011-12-01 12:23:37 -0800185 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700186 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700187 long pauseTime; // last time we started pausing the activity
188 long launchTickTime; // base time for launch tick messages
Andrii Kulian21713ac2016-10-12 22:05:05 -0700189 private Configuration mLastReportedConfiguration; // configuration activity was last running in
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700190 // Overridden configuration by the activity task
Andrii Kulian1779e612016-10-12 21:58:25 -0700191 // WARNING: Reference points to {@link TaskRecord#getMergedOverrideConfig}, so its internal
192 // state should never be altered directly.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700193 private Configuration mLastReportedOverrideConfiguration;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700194 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700195 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 final String resultWho; // additional identifier for use by resultTo.
197 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800198 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800200 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700201 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700202 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700203 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -0700205 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700206 ProcessRecord app; // if non-null, hosting application
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700207 ActivityState state; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700209 PersistableBundle persistentState; // last persistently saved activity state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 boolean frontOfTask; // is this the root activity of its task?
211 boolean launchFailed; // set if a launched failed, to abort on 2nd try
212 boolean haveState; // have we gotten the last activity state?
213 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700214 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800216 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
217 // completed
218 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 int configChangeFlags; // which config values have changed
220 boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 int launchMode; // the launch mode activity attribute.
222 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700223 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
224 // might hide this activity?
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800225 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 boolean nowVisible; // is this activity's window visible?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 boolean idle; // has the activity gone idle?
228 boolean hasBeenLaunched;// has this activity ever been launched?
229 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400230 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400231 boolean forceNewConfig; // force re-create with new config next time
Winson Chungb5c41b72016-12-07 15:00:47 -0800232 boolean supportsPipOnMoveToBackground; // Supports automatically entering picture-in-picture
Winson Chunga29eb982016-12-14 12:01:27 -0800233 // when this activity is hidden. This flag is requested by the activity.
Winson Chungb5c41b72016-12-07 15:00:47 -0800234 private boolean enterPipOnMoveToBackground; // Flag to enter picture in picture when this
Winson Chunga29eb982016-12-14 12:01:27 -0800235 // activity is made invisible. This flag is set specifically when another task is being
236 // launched or moved to the front which may cause this activity to try and enter PiP
237 // when it is next made invisible.
238 PictureInPictureArguments pictureInPictureArgs = new PictureInPictureArguments(); // The PiP
239 // arguments used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800240 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800241 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800242 ComponentName requestedVrComponent; // the requested component for handling VR mode.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800243 ArrayList<ActivityContainer> mChildContainers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700245 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700246
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700247 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700248 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800249
250 static final int STARTING_WINDOW_NOT_SHOWN = 0;
251 static final int STARTING_WINDOW_SHOWN = 1;
252 static final int STARTING_WINDOW_REMOVED = 2;
253 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700254 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800255
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800256 boolean mUpdateTaskThumbnailWhenHidden;
Craig Mautnere0a38842013-12-16 16:14:02 -0800257 ActivityContainer mInitialActivityContainer;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700258
Craig Mautner21d24a22014-04-23 11:45:37 -0700259 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700260 boolean mLaunchTaskBehind; // this activity is actively being launched with
261 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700262
Filip Gruszczynski23493322015-07-29 17:02:59 -0700263 // These configurations are collected from application's resources based on size-sensitive
264 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
265 // and drawable-sw400dp will be added to both as 400.
266 private int[] mVerticalSizeConfigurations;
267 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700268 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700269
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800270 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
271 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
272
Robert Carrd2265122016-08-05 10:25:21 -0700273 // A hint to override the window specified rotation animation, or -1
274 // to use the window specified value. We use this so that
275 // we can select the right animation in the cases of starting
276 // windows, where the app hasn't had time to set a value
277 // on the window.
278 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700279
Andrii Kulian21713ac2016-10-12 22:05:05 -0700280 /**
281 * Temp configs used in {@link #ensureActivityConfigurationLocked(int, boolean)}
282 */
283 private final Configuration mTmpGlobalConfig = new Configuration();
284 private final Configuration mTmpTaskConfig = new Configuration();
285
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800286 private static String startingWindowStateToString(int state) {
287 switch (state) {
288 case STARTING_WINDOW_NOT_SHOWN:
289 return "STARTING_WINDOW_NOT_SHOWN";
290 case STARTING_WINDOW_SHOWN:
291 return "STARTING_WINDOW_SHOWN";
292 case STARTING_WINDOW_REMOVED:
293 return "STARTING_WINDOW_REMOVED";
294 default:
295 return "unknown state=" + state;
296 }
297 }
298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700300 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700301 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
302 pw.print(" processName="); pw.println(processName);
303 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700304 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800305 pw.print(" userId="); pw.println(userId);
306 pw.print(prefix); pw.print("app="); pw.println(app);
307 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700308 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
309 pw.print(" task="); pw.println(task);
310 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700311 pw.print(prefix); pw.print("realActivity=");
312 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700313 if (appInfo != null) {
314 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
315 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
316 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
317 }
318 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800319 if (appInfo.splitSourceDirs != null) {
320 pw.print(prefix); pw.print("splitDir=");
321 pw.println(Arrays.toString(appInfo.splitSourceDirs));
322 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800323 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700324 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
325 pw.print(" componentSpecified="); pw.print(componentSpecified);
Craig Mautner86d67a42013-05-14 10:34:38 -0700326 pw.print(" mActivityType="); pw.println(mActivityType);
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700327 if (rootVoiceInteraction) {
328 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
329 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800330 pw.print(prefix); pw.print("compat="); pw.print(compat);
331 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
332 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
333 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Andrii Kulian1779e612016-10-12 21:58:25 -0700334 pw.print(prefix); pw.print("mLastReportedConfiguration=");
335 pw.println(mLastReportedConfiguration);
336 pw.print(prefix); pw.print("mLastReportedOverrideConfiguration=");
337 pw.println(mLastReportedOverrideConfiguration);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700338 if (resultTo != null || resultWho != null) {
339 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
340 pw.print(" resultWho="); pw.print(resultWho);
341 pw.print(" resultCode="); pw.println(requestCode);
342 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700343 if (taskDescription != null) {
344 final String iconFilename = taskDescription.getIconFilename();
345 if (iconFilename != null || taskDescription.getLabel() != null ||
346 taskDescription.getPrimaryColor() != 0) {
347 pw.print(prefix); pw.print("taskDescription:");
348 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
349 pw.print(" label=\""); pw.print(taskDescription.getLabel());
350 pw.print("\"");
351 pw.print(" color=");
352 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
353 }
354 if (iconFilename == null && taskDescription.getIcon() != null) {
355 pw.print(prefix); pw.println("taskDescription contains Bitmap");
356 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700357 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700358 if (results != null) {
359 pw.print(prefix); pw.print("results="); pw.println(results);
360 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700361 if (pendingResults != null && pendingResults.size() > 0) {
362 pw.print(prefix); pw.println("Pending Results:");
363 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
364 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
365 pw.print(prefix); pw.print(" - ");
366 if (pir == null) {
367 pw.println("null");
368 } else {
369 pw.println(pir);
370 pir.dump(pw, prefix + " ");
371 }
372 }
373 }
374 if (newIntents != null && newIntents.size() > 0) {
375 pw.print(prefix); pw.println("Pending New Intents:");
376 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800377 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700378 pw.print(prefix); pw.print(" - ");
379 if (intent == null) {
380 pw.println("null");
381 } else {
382 pw.println(intent.toShortString(false, true, false, true));
383 }
384 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700385 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700386 if (pendingOptions != null) {
387 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
388 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700389 if (appTimeTracker != null) {
390 appTimeTracker.dumpWithHeader(pw, prefix, false);
391 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700392 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700393 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700394 }
395 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800396 pw.print(" launchCount="); pw.print(launchCount);
397 pw.print(" lastLaunchTime=");
398 if (lastLaunchTime == 0) pw.print("0");
399 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
400 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700401 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700402 pw.print(" icicle="); pw.println(icicle);
403 pw.print(prefix); pw.print("state="); pw.print(state);
404 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700405 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700406 pw.print(" finishing="); pw.println(finishing);
407 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
408 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700409 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800410 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800411 pw.print(" idle="); pw.print(idle);
412 pw.print(" mStartingWindowState=");
413 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800414 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
415 pw.print(" noDisplay="); pw.print(noDisplay);
416 pw.print(" immersive="); pw.print(immersive);
417 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800418 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400419 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700420 pw.print(prefix); pw.print("mActivityType=");
421 pw.println(activityTypeToString(mActivityType));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800422 if (requestedVrComponent != null) {
423 pw.print(prefix);
424 pw.print("requestedVrComponent=");
425 pw.println(requestedVrComponent);
426 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700427 if (displayStartTime != 0 || startTime != 0) {
428 pw.print(prefix); pw.print("displayStartTime=");
429 if (displayStartTime == 0) pw.print("0");
430 else TimeUtils.formatDuration(displayStartTime, now, pw);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700431 pw.print(" startTime=");
432 if (startTime == 0) pw.print("0");
433 else TimeUtils.formatDuration(startTime, now, pw);
434 pw.println();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700435 }
Craig Mautner8c14c152015-01-15 17:32:07 -0800436 final boolean waitingVisible = mStackSupervisor.mWaitingVisibleActivities.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800437 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700438 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800439 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700440 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700441 if (lastVisibleTime == 0) pw.print("0");
442 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
443 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700444 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800445 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
446 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700447 pw.print(" configChangeFlags=");
448 pw.println(Integer.toHexString(configChangeFlags));
449 }
450 if (connections != null) {
451 pw.print(prefix); pw.print("connections="); pw.println(connections);
452 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800453 if (info != null) {
454 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
455 }
Winson Chungb5c41b72016-12-07 15:00:47 -0800456 if (supportsPipOnMoveToBackground) {
Winson Chunga29eb982016-12-14 12:01:27 -0800457 pw.println(prefix + "supportsPipOnMoveToBackground=1");
458 pw.println(prefix + "enterPipOnMoveToBackground=" +
459 (enterPipOnMoveToBackground ? 1 : 0));
460 pictureInPictureArgs.dump(pw, prefix);
Winson Chungb5c41b72016-12-07 15:00:47 -0800461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463
Andrii Kulian21713ac2016-10-12 22:05:05 -0700464 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700465 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
466 }
467
Andrii Kulian21713ac2016-10-12 22:05:05 -0700468 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700469 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
470 }
471
Andrii Kulian21713ac2016-10-12 22:05:05 -0700472 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700473 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
474 }
475
Filip Gruszczynski23493322015-07-29 17:02:59 -0700476 /**
477 * The purpose of this method is to decide whether the activity needs to be relaunched upon
478 * changing its size. In most cases the activities don't need to be relaunched, if the resize
479 * is small, all the activity content has to do is relayout itself within new bounds. There are
480 * cases however, where the activity's content would be completely changed in the new size and
481 * the full relaunch is required.
482 *
483 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
484 * required. These thresholds are collected from the application resource qualifiers. For
485 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
486 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
487 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
488 * of the threshold.
489 */
490 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
491 int secondDp) {
492 if (thresholds == null) {
493 return false;
494 }
495 for (int i = thresholds.length - 1; i >= 0; i--) {
496 final int threshold = thresholds[i];
497 if ((firstDp < threshold && secondDp >= threshold)
498 || (firstDp >= threshold && secondDp < threshold)) {
499 return true;
500 }
501 }
502 return false;
503 }
504
Andrii Kulian21713ac2016-10-12 22:05:05 -0700505 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700506 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700507 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
508 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700509 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700510 }
511
Andrii Kulian21713ac2016-10-12 22:05:05 -0700512 private void scheduleConfigurationChanged(Configuration config, boolean reportToActivity) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800513 if (app == null || app.thread == null) {
514 return;
515 }
516 try {
517 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + " " +
Adam Lesinskibad43fc2016-07-19 13:35:01 -0700518 "reportToActivity=" + reportToActivity + " and config: " + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700519
Andrii Kulian1779e612016-10-12 21:58:25 -0700520 app.thread.scheduleActivityConfigurationChanged(appToken, new Configuration(config),
521 reportToActivity);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800522 } catch (RemoteException e) {
523 // If process died, whatever.
524 }
525 }
526
Andrii Kulian933076d2016-03-29 17:04:42 -0700527 void scheduleMultiWindowModeChanged() {
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 {
532 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Andrii Kulian933076d2016-03-29 17:04:42 -0700533 app.thread.scheduleMultiWindowModeChanged(appToken, !task.mFullscreen);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800534 } catch (Exception e) {
535 // If process died, I don't care.
536 }
537 }
538
Andrii Kulian933076d2016-03-29 17:04:42 -0700539 void schedulePictureInPictureModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700540 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800541 return;
542 }
543 try {
Andrii Kulian933076d2016-03-29 17:04:42 -0700544 app.thread.schedulePictureInPictureModeChanged(
Andrii Kulian02b7a832016-10-06 23:11:56 -0700545 appToken, task.getStackId() == PINNED_STACK_ID);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800546 } catch (Exception e) {
547 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800548 }
549 }
550
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800551 boolean isFreeform() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700552 return task != null && task.getStackId() == FREEFORM_WORKSPACE_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800553 }
554
Dianne Hackbornbe707852011-11-11 14:32:10 -0800555 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700556 private final WeakReference<ActivityRecord> weakActivity;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800557
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800558 Token(ActivityRecord activity) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800559 weakActivity = new WeakReference<>(activity);
Wale Ogunwale7d701172015-03-11 15:36:30 -0700560 }
561
Andrii Kulian21713ac2016-10-12 22:05:05 -0700562 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700563 if (token == null) {
564 return null;
565 }
566 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700567 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700568 return null;
569 }
570 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800571 }
572
Craig Mautnerde4ef022013-04-07 19:01:33 -0700573 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800574 public String toString() {
575 StringBuilder sb = new StringBuilder(128);
576 sb.append("Token{");
577 sb.append(Integer.toHexString(System.identityHashCode(this)));
578 sb.append(' ');
579 sb.append(weakActivity.get());
580 sb.append('}');
581 return sb.toString();
582 }
583 }
584
Wale Ogunwale7d701172015-03-11 15:36:30 -0700585 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800586 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700587 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800588 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800589 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800590 return null;
591 }
592 }
593
Chong Zhang85ee6542015-10-02 13:36:38 -0700594 boolean isResolverActivity() {
595 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700596 }
597
Craig Mautnerd2328952013-03-05 12:46:26 -0800598 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800599 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700601 ActivityRecord _resultTo, String _resultWho, int _reqCode,
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700602 boolean _componentSpecified, boolean _rootVoiceInteraction,
603 ActivityStackSupervisor supervisor,
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700604 ActivityContainer container, ActivityOptions options, ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 service = _service;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800606 appToken = new Token(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 info = aInfo;
608 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800609 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700610 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 intent = _intent;
612 shortComponentName = _intent.getComponent().flattenToShortString();
613 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800614 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700615 rootVoiceInteraction = _rootVoiceInteraction;
Andrii Kulian1779e612016-10-12 21:58:25 -0700616 mLastReportedConfiguration = new Configuration(_configuration);
617 mLastReportedOverrideConfiguration = new Configuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 resultTo = _resultTo;
619 resultWho = _resultWho;
620 requestCode = _reqCode;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700621 state = ActivityState.INITIALIZING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 frontOfTask = false;
623 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700625 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800627 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 keysPaused = false;
629 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700630 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 idle = false;
633 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700634 mStackSupervisor = supervisor;
Craig Mautnere0a38842013-12-16 16:14:02 -0800635 mInitialActivityContainer = container;
Robert Carr0f5d7532016-10-17 16:39:17 -0700636
637 mRotationAnimationHint = aInfo.rotationAnimation;
638
Craig Mautner233ceee2014-05-09 17:05:11 -0700639 if (options != null) {
Chong Zhang280d3322015-11-03 17:27:26 -0800640 pendingOptions = options;
Craig Mautnerbb742462014-07-07 15:28:55 -0700641 mLaunchTaskBehind = pendingOptions.getLaunchTaskBehind();
Robert Carr0f5d7532016-10-17 16:39:17 -0700642
643 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
644 // Only override manifest supplied option if set.
645 if (rotationAnimation >= 0) {
646 mRotationAnimationHint = rotationAnimation;
647 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700648 PendingIntent usageReport = pendingOptions.getUsageTimeReport();
649 if (usageReport != null) {
650 appTimeTracker = new AppTimeTracker(usageReport);
651 }
Craig Mautner233ceee2014-05-09 17:05:11 -0700652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800654 // This starts out true, since the initial state of an activity is that we have everything,
655 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700656 haveState = true;
657
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800658 // If the class name in the intent doesn't match that of the target, this is
659 // probably an alias. We have to create a new ComponentName object to keep track
660 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
661 if (aInfo.targetActivity == null
662 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
663 && (aInfo.launchMode == LAUNCH_MULTIPLE
664 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
665 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800667 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800669 taskAffinity = aInfo.taskAffinity;
670 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
671 appInfo = aInfo.applicationInfo;
672 nonLocalizedLabel = aInfo.nonLocalizedLabel;
673 labelRes = aInfo.labelRes;
674 if (nonLocalizedLabel == null && labelRes == 0) {
675 ApplicationInfo app = aInfo.applicationInfo;
676 nonLocalizedLabel = app.nonLocalizedLabel;
677 labelRes = app.labelRes;
678 }
679 icon = aInfo.getIconResource();
680 logo = aInfo.getLogoResource();
681 theme = aInfo.getThemeResource();
682 realTheme = theme;
683 if (realTheme == 0) {
684 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
685 ? android.R.style.Theme : android.R.style.Theme_Holo;
686 }
687 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
688 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
689 }
690 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
691 && (aInfo.applicationInfo.uid == SYSTEM_UID
692 || aInfo.applicationInfo.uid == _caller.info.uid)) {
693 processName = _caller.processName;
694 } else {
695 processName = aInfo.processName;
696 }
697
698 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
699 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
700 }
701
702 packageName = aInfo.applicationInfo.packageName;
703 launchMode = aInfo.launchMode;
704
705 AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
706 realTheme, com.android.internal.R.styleable.Window, userId);
707 final boolean translucent = ent != null && (ent.array.getBoolean(
708 com.android.internal.R.styleable.Window_windowIsTranslucent, false)
709 || (!ent.array.hasValue(
710 com.android.internal.R.styleable.Window_windowIsTranslucent)
711 && ent.array.getBoolean(
712 com.android.internal.R.styleable.Window_windowSwipeToDismiss,
713 false)));
714 fullscreen = ent != null && !ent.array.getBoolean(
715 com.android.internal.R.styleable.Window_windowIsFloating, false) && !translucent;
716 noDisplay = ent != null && ent.array.getBoolean(
717 com.android.internal.R.styleable.Window_windowNoDisplay, false);
718
719 setActivityType(_componentSpecified, _launchedFromUid, _intent, sourceRecord);
720
721 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
722
723 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
724 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
725 }
726
727 void createWindowContainer() {
728 if (mWindowContainerController != null) {
729 throw new IllegalArgumentException("Window container=" + mWindowContainerController
730 + " already created for r=" + this);
731
732 }
733
734 inHistory = true;
735
736 task.updateOverrideConfigurationFromLaunchBounds();
737
738 mWindowContainerController = new AppWindowContainerController(appToken, this, task.taskId,
739 Integer.MAX_VALUE /* add on top */, info.screenOrientation, fullscreen,
740 (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
741 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
742 appInfo.targetSdkVersion, mRotationAnimationHint,
743 ActivityManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L);
744
745 task.addActivityToTop(this);
746
747 onOverrideConfigurationSent();
748 }
749
750 void removeWindowContainer() {
751 mWindowContainerController.removeContainer(getDisplayId());
752 }
753
754 // TODO: Remove once task record is converted to use controller in which case we can use
755 // positionChildAt()
756 void positionWindowContainerAt(int index) {
757 mWindowContainerController.positionAt(task.taskId, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700760 private boolean isHomeIntent(Intent intent) {
761 return Intent.ACTION_MAIN.equals(intent.getAction())
762 && intent.hasCategory(Intent.CATEGORY_HOME)
763 && intent.getCategories().size() == 1
764 && intent.getData() == null
765 && intent.getType() == null;
766 }
767
Chong Zhangad24f962016-08-25 12:12:33 -0700768 static boolean isMainIntent(Intent intent) {
769 return Intent.ACTION_MAIN.equals(intent.getAction())
770 && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
771 && intent.getCategories().size() == 1
772 && intent.getData() == null
773 && intent.getType() == null;
774 }
775
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700776 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
777 if (uid == Process.myUid() || uid == 0) {
778 // System process can launch home activity.
779 return true;
780 }
781 // Resolver activity can launch home activity.
782 return sourceRecord != null && sourceRecord.isResolverActivity();
783 }
784
785 private void setActivityType(boolean componentSpecified,
786 int launchedFromUid, Intent intent, ActivityRecord sourceRecord) {
787 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
788 && isHomeIntent(intent) && !isResolverActivity()) {
789 // This sure looks like a home activity!
790 mActivityType = HOME_ACTIVITY_TYPE;
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700791
792 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
793 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
794 // We only allow home activities to be resizeable if they explicitly requested it.
795 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
796 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700797 } else if (realActivity.getClassName().contains(RECENTS_PACKAGE_NAME)) {
798 mActivityType = RECENTS_ACTIVITY_TYPE;
799 } else {
800 mActivityType = APPLICATION_ACTIVITY_TYPE;
801 }
802 }
803
Craig Mautnera228ae92014-07-09 05:44:55 -0700804 void setTask(TaskRecord newTask, TaskRecord taskToAffiliateWith) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700805 if (task != null && task.removeActivity(this) && task != newTask
806 && task.getStack() != null) {
807 task.getStack().removeTask(task, "setTask");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700808 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700809 task = newTask;
Craig Mautnera228ae92014-07-09 05:44:55 -0700810 setTaskToAffiliateWith(taskToAffiliateWith);
811 }
812
813 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
814 if (taskToAffiliateWith != null &&
815 launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE &&
816 launchMode != ActivityInfo.LAUNCH_SINGLE_TASK) {
817 task.setTaskToAffiliateWith(taskToAffiliateWith);
818 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700819 }
820
Andrii Kulian02b7a832016-10-06 23:11:56 -0700821 /**
Winson Chungb5c41b72016-12-07 15:00:47 -0800822 * If this activity has requested that it auto-enter picture-in-picture and we can actually do
823 * this, then mark it to enter picture in picture at that point.
824 */
825 void setEnterPipOnMoveToBackground(boolean enterPipOnInvisible) {
826 if (supportsPipOnMoveToBackground) {
827 enterPipOnMoveToBackground = enterPipOnInvisible;
828 }
829 }
830
831 /**
832 * @return whether to enter PiP when this activity is made invisible.
833 */
834 public boolean shouldEnterPictureInPictureOnInvisible() {
835 return enterPipOnMoveToBackground;
836 }
837
838 /**
Andrii Kulian02b7a832016-10-06 23:11:56 -0700839 * @return Stack value from current task, null if there is no task.
840 */
841 ActivityStack getStack() {
842 return task != null ? task.getStack() : null;
843 }
844
Craig Mautner5eda9b32013-07-02 11:58:16 -0700845 boolean changeWindowTranslucency(boolean toOpaque) {
846 if (fullscreen == toOpaque) {
847 return false;
848 }
Craig Mautner4addfc52013-06-25 08:05:45 -0700849
Craig Mautner5eda9b32013-07-02 11:58:16 -0700850 // Keep track of the number of fullscreen activities in this task.
851 task.numFullscreen += toOpaque ? +1 : -1;
852
853 fullscreen = toOpaque;
854 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -0700855 }
856
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700857 void takeFromHistory() {
858 if (inHistory) {
859 inHistory = false;
860 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800861 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700862 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700863 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700864 }
865 }
866
867 boolean isInHistory() {
868 return inHistory;
869 }
870
Wale Ogunwale7d701172015-03-11 15:36:30 -0700871 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700872 final ActivityStack stack = getStack();
873 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700874 }
875
Craig Mautner86d67a42013-05-14 10:34:38 -0700876 boolean isHomeActivity() {
877 return mActivityType == HOME_ACTIVITY_TYPE;
878 }
879
880 boolean isRecentsActivity() {
881 return mActivityType == RECENTS_ACTIVITY_TYPE;
882 }
883
884 boolean isApplicationActivity() {
885 return mActivityType == APPLICATION_ACTIVITY_TYPE;
886 }
887
Craig Mautner21d24a22014-04-23 11:45:37 -0700888 boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700889 return (info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY ||
890 info.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS) &&
891 (intent == null ||
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800892 (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -0700893 }
894
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800895 boolean isFocusable() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700896 return StackId.canReceiveKeys(task.getStackId()) || isAlwaysFocusable();
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800897 }
898
899 boolean isResizeable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700900 return ActivityInfo.isResizeableMode(info.resizeMode);
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800901 }
902
Wale Ogunwale513346d2016-01-27 10:55:01 -0800903 boolean isResizeableOrForced() {
904 return !isHomeActivity() && (isResizeable() || service.mForceResizableActivities);
905 }
906
Jorim Jaggicd13d332016-04-27 15:40:20 -0700907 boolean isNonResizableOrForced() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700908 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -0700909 && info.resizeMode != RESIZE_MODE_RESIZEABLE_AND_PIPABLE
910 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -0700911 }
912
Winson Chunge6308042016-10-31 09:24:01 -0700913 /**
914 * @return whether this activity's resize mode supports PIP.
915 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800916 boolean supportsPictureInPicture() {
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800917 return !isHomeActivity() && info.resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE;
918 }
919
Winson Chunge6308042016-10-31 09:24:01 -0700920 /**
Winson Chungb5c41b72016-12-07 15:00:47 -0800921 * @return whether this activity is currently allowed to enter PIP, if
922 * {@param checkActivityVisibility} is set, then the current activity visibility is taken into
923 * account.
Winson Chunge6308042016-10-31 09:24:01 -0700924 */
Winson Chungb5c41b72016-12-07 15:00:47 -0800925 boolean canEnterPictureInPicture(boolean checkActivityVisibility) {
926 if (!checkActivityVisibility) {
927 return supportsPictureInPicture();
928 }
929
Winson Chung7e3233a2016-12-12 14:27:21 -0800930 if (supportsPictureInPicture()) {
Winson Chunge6308042016-10-31 09:24:01 -0700931 switch (state) {
932 case RESUMED:
933 case PAUSING:
934 case PAUSED:
935 return true;
936 }
937 }
938 return false;
939 }
940
Wale Ogunwale513346d2016-01-27 10:55:01 -0800941 boolean canGoInDockedStack() {
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700942 return !isHomeActivity() && isResizeableOrForced();
Wale Ogunwale513346d2016-01-27 10:55:01 -0800943 }
944
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800945 boolean isAlwaysFocusable() {
946 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800947 }
948
Jorim Jaggife762342016-10-13 14:33:27 +0200949 /**
950 * @return true if the activity contains windows that have
951 * {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set
952 */
953 boolean hasShowWhenLockedWindows() {
954 return service.mWindowManager.containsShowWhenLockedWindow(appToken);
955 }
956
957 /**
958 * @return true if the activity contains windows that have
959 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
960 */
961 boolean hasDismissKeyguardWindows() {
962 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
963 }
964
Wale Ogunwale7d701172015-03-11 15:36:30 -0700965 void makeFinishingLocked() {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800966 if (!finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700967 final ActivityStack stack = getStack();
968 if (stack != null && this == stack.getVisibleBehindActivity()) {
Jose Lima34ff4922014-08-18 15:19:41 -0700969 // A finishing activity should not remain as visible in the background
970 mStackSupervisor.requestVisibleBehindLocked(this, false);
971 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800972 finishing = true;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700973 if (stopped) {
974 clearOptionsLocked();
975 }
Yorke Leebd54c2a2016-10-25 13:49:23 -0700976
977 if (service != null) {
978 service.mTaskChangeNotificationController.notifyTaskStackChanged();
979 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800980 }
981 }
982
Dianne Hackborn7e269642010-08-25 19:50:20 -0700983 UriPermissionOwner getUriPermissionsLocked() {
984 if (uriPermissions == null) {
985 uriPermissions = new UriPermissionOwner(service, this);
986 }
987 return uriPermissions;
988 }
989
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700990 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 int requestCode, int resultCode,
992 Intent resultData) {
993 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -0500994 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800996 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 }
998 results.add(r);
999 }
1000
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001001 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 int requestCode) {
1003 if (results != null) {
1004 for (int i=results.size()-1; i>=0; i--) {
1005 ActivityResult r = (ActivityResult)results.get(i);
1006 if (r.mFrom != from) continue;
1007 if (r.mResultWho == null) {
1008 if (resultWho != null) continue;
1009 } else {
1010 if (!r.mResultWho.equals(resultWho)) continue;
1011 }
1012 if (r.mRequestCode != requestCode) continue;
1013
1014 results.remove(i);
1015 }
1016 }
1017 }
1018
Andrii Kulian21713ac2016-10-12 22:05:05 -07001019 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001021 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023 newIntents.add(intent);
1024 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001025
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 /**
1027 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1028 * method will be called at the proper time.
1029 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001030 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001031 // The activity now gets access to the data associated with this Intent.
1032 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001033 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001034 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001035 boolean unsent = true;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001036 final ActivityStack stack = getStack();
Wale Ogunwale826c7062016-09-13 08:25:54 -07001037 final boolean isTopActivityInStack =
1038 stack != null && stack.topRunningActivityLocked() == this;
1039 final boolean isTopActivityWhileSleeping =
1040 service.isSleepingLocked() && isTopActivityInStack;
Wale Ogunwale826c7062016-09-13 08:25:54 -07001041
1042 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001043 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1044 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001045 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001046 if ((state == ActivityState.RESUMED || state == ActivityState.PAUSED
1047 || isTopActivityWhileSleeping) && app != null && app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001048 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001049 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1050 ar.add(rintent);
Wale Ogunwale826c7062016-09-13 08:25:54 -07001051 app.thread.scheduleNewIntent(
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001052 ar, appToken, state == ActivityState.PAUSED /* andPause */);
Craig Mautner86d67a42013-05-14 10:34:38 -07001053 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001054 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001055 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001056 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001057 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001058 }
1059 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001060 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001061 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001062 }
1063 }
1064
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001065 void updateOptionsLocked(ActivityOptions options) {
1066 if (options != null) {
1067 if (pendingOptions != null) {
1068 pendingOptions.abort();
1069 }
1070 pendingOptions = options;
1071 }
1072 }
1073
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001074 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001075 if (pendingOptions != null
1076 && pendingOptions.getAnimationType() != ActivityOptions.ANIM_SCENE_TRANSITION) {
Michael Jurka21385cd2012-05-03 10:57:31 -07001077 final int animationType = pendingOptions.getAnimationType();
1078 switch (animationType) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001079 case ActivityOptions.ANIM_CUSTOM:
1080 service.mWindowManager.overridePendingAppTransition(
1081 pendingOptions.getPackageName(),
1082 pendingOptions.getCustomEnterResId(),
Dianne Hackborn84375872012-06-01 19:03:50 -07001083 pendingOptions.getCustomExitResId(),
1084 pendingOptions.getOnAnimationStartListener());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001085 break;
Chet Haase10e23ab2015-02-11 15:08:38 -08001086 case ActivityOptions.ANIM_CLIP_REVEAL:
1087 service.mWindowManager.overridePendingAppTransitionClipReveal(
1088 pendingOptions.getStartX(), pendingOptions.getStartY(),
1089 pendingOptions.getWidth(), pendingOptions.getHeight());
1090 if (intent.getSourceBounds() == null) {
1091 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1092 pendingOptions.getStartY(),
1093 pendingOptions.getStartX()+pendingOptions.getWidth(),
1094 pendingOptions.getStartY()+pendingOptions.getHeight()));
1095 }
1096 break;
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001097 case ActivityOptions.ANIM_SCALE_UP:
1098 service.mWindowManager.overridePendingAppTransitionScaleUp(
1099 pendingOptions.getStartX(), pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001100 pendingOptions.getWidth(), pendingOptions.getHeight());
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001101 if (intent.getSourceBounds() == null) {
1102 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1103 pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001104 pendingOptions.getStartX()+pendingOptions.getWidth(),
1105 pendingOptions.getStartY()+pendingOptions.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001106 }
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001107 break;
Michael Jurka832cb222012-04-13 09:32:47 -07001108 case ActivityOptions.ANIM_THUMBNAIL_SCALE_UP:
1109 case ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN:
1110 boolean scaleUp = (animationType == ActivityOptions.ANIM_THUMBNAIL_SCALE_UP);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001111 service.mWindowManager.overridePendingAppTransitionThumb(
1112 pendingOptions.getThumbnail(),
1113 pendingOptions.getStartX(), pendingOptions.getStartY(),
Michael Jurka21385cd2012-05-03 10:57:31 -07001114 pendingOptions.getOnAnimationStartListener(),
Michael Jurka832cb222012-04-13 09:32:47 -07001115 scaleUp);
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001116 if (intent.getSourceBounds() == null) {
1117 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1118 pendingOptions.getStartY(),
1119 pendingOptions.getStartX()
1120 + pendingOptions.getThumbnail().getWidth(),
1121 pendingOptions.getStartY()
1122 + pendingOptions.getThumbnail().getHeight()));
1123 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001124 break;
Winson Chunga4ccb862014-08-22 15:26:27 -07001125 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP:
1126 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN:
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001127 final AppTransitionAnimationSpec[] specs = pendingOptions.getAnimSpecs();
1128 if (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN
1129 && specs != null) {
1130 service.mWindowManager.overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -07001131 specs, pendingOptions.getOnAnimationStartListener(),
1132 pendingOptions.getAnimationFinishedListener(), false);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001133 } else {
1134 service.mWindowManager.overridePendingAppTransitionAspectScaledThumb(
1135 pendingOptions.getThumbnail(),
1136 pendingOptions.getStartX(), pendingOptions.getStartY(),
1137 pendingOptions.getWidth(), pendingOptions.getHeight(),
1138 pendingOptions.getOnAnimationStartListener(),
1139 (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP));
1140 if (intent.getSourceBounds() == null) {
1141 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1142 pendingOptions.getStartY(),
1143 pendingOptions.getStartX() + pendingOptions.getWidth(),
1144 pendingOptions.getStartY() + pendingOptions.getHeight()));
1145 }
Winson Chunga4ccb862014-08-22 15:26:27 -07001146 }
1147 break;
Craig Mautner233ceee2014-05-09 17:05:11 -07001148 default:
1149 Slog.e(TAG, "applyOptionsLocked: Unknown animationType=" + animationType);
1150 break;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001151 }
1152 pendingOptions = null;
1153 }
1154 }
1155
Adam Powellcfbe9be2013-11-06 14:58:58 -08001156 ActivityOptions getOptionsForTargetActivityLocked() {
1157 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1158 }
1159
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001160 void clearOptionsLocked() {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001161 if (pendingOptions != null) {
1162 pendingOptions.abort();
1163 pendingOptions = null;
1164 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001165 }
1166
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001167 ActivityOptions takeOptionsLocked() {
1168 ActivityOptions opts = pendingOptions;
1169 pendingOptions = null;
1170 return opts;
1171 }
1172
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001173 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001174 if (uriPermissions != null) {
1175 uriPermissions.removeUriPermissionsLocked();
1176 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001177 }
1178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179
1180 void pauseKeyDispatchingLocked() {
1181 if (!keysPaused) {
1182 keysPaused = true;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001183 mWindowContainerController.pauseKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185 }
1186
1187 void resumeKeyDispatchingLocked() {
1188 if (keysPaused) {
1189 keysPaused = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001190 mWindowContainerController.resumeKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
1193
Winson Chung740c3ac2014-11-12 16:14:38 -08001194 void updateThumbnailLocked(Bitmap newThumbnail, CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001195 if (newThumbnail != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001196 if (DEBUG_THUMBNAILS) Slog.i(TAG_THUMBNAILS,
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001197 "Setting thumbnail of " + this + " to " + newThumbnail);
Winsonc809cbb2015-11-02 12:06:15 -08001198 boolean thumbnailUpdated = task.setLastThumbnailLocked(newThumbnail);
Winson Chung096f36b2014-08-20 15:39:01 -07001199 if (thumbnailUpdated && isPersistable()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001200 service.notifyTaskPersisterLocked(task, false);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001201 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001202 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001203 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001204 }
1205
Andrii Kulian21713ac2016-10-12 22:05:05 -07001206 final Bitmap screenshotActivityLocked() {
1207 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivityLocked: " + this);
1208 if (noDisplay) {
1209 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
1210 return null;
1211 }
1212
1213 final ActivityStack stack = getStack();
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001214 if (stack.isHomeOrRecentsStack()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001215 // This is an optimization -- since we never show Home or Recents within Recents itself,
1216 // we can just go ahead and skip taking the screenshot if this is the home stack.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001217 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, stack.getStackId() == HOME_STACK_ID ?
1218 "\tHome stack" : "\tRecents stack");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001219 return null;
1220 }
1221
1222 int w = service.mThumbnailWidth;
1223 int h = service.mThumbnailHeight;
1224
1225 if (w <= 0) {
1226 Slog.e(TAG, "\tInvalid thumbnail dimensions: " + w + "x" + h);
1227 return null;
1228 }
1229
1230 if (stack.mStackId == DOCKED_STACK_ID && mStackSupervisor.mIsDockMinimized) {
1231 // When the docked stack is minimized its app windows are cropped significantly so any
1232 // screenshot taken will not display the apps contain. So, we avoid taking a screenshot
1233 // in that case.
1234 if (DEBUG_SCREENSHOTS) Slog.e(TAG, "\tIn minimized docked stack");
1235 return null;
1236 }
1237
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001238 float scale = 0;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001239 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
1240
1241 // When this flag is set, we currently take the fullscreen screenshot of the activity but
1242 // scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to use within
1243 // SystemUI while keeping memory usage low.
1244 if (TAKE_FULLSCREEN_SCREENSHOTS) {
1245 w = h = -1;
1246 scale = service.mFullscreenThumbnailScale;
1247 }
1248
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001249 return mWindowContainerController.screenshotApplications(getDisplayId(), w, h, scale);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001250 }
1251
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001252 void setVisibility(boolean visible) {
1253 mWindowContainerController.setVisibility(visible);
1254 }
1255
1256 // TODO: Look into merging with #setVisibility()
Andrii Kulian21713ac2016-10-12 22:05:05 -07001257 void setVisible(boolean newVisible) {
1258 visible = newVisible;
1259 if (!visible && mUpdateTaskThumbnailWhenHidden) {
1260 updateThumbnailLocked(screenshotActivityLocked(), null /* description */);
1261 mUpdateTaskThumbnailWhenHidden = false;
1262 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001263 mWindowContainerController.setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001264 final ArrayList<ActivityContainer> containers = mChildContainers;
1265 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
1266 final ActivityContainer container = containers.get(containerNdx);
1267 container.setVisible(visible);
1268 }
1269 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1270 }
1271
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001272 void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
1273 mWindowContainerController.notifyAppResumed(wasStopped, allowSavedSurface);
1274 }
1275
1276 void notifyUnknownVisibilityLaunched() {
1277 mWindowContainerController.notifyUnknownVisibilityLaunched();
1278 }
1279
Jorim Jaggi241ae102016-11-02 21:57:33 -07001280 /**
1281 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1282 * might have on the visibility
1283 *
1284 * @see {@link ActivityStack#checkKeyguardVisibility}
1285 */
1286 boolean shouldBeVisibleIgnoringKeyguard(boolean behindTranslucentActivity,
1287 boolean stackVisibleBehind, ActivityRecord visibleBehind,
1288 boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001289 if (!okToShowLocked()) {
1290 return false;
1291 }
1292
1293 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1294 // but must be drawn initially for the animation as though they were visible.
1295 final boolean activityVisibleBehind =
1296 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == this;
1297
1298 boolean isVisible =
1299 !behindFullscreenActivity || mLaunchTaskBehind || activityVisibleBehind;
1300
1301 if (service.mSupportsLeanbackOnly && isVisible && isRecentsActivity()) {
1302 // On devices that support leanback only (Android TV), Recents activity can only be
1303 // visible if the home stack is the focused stack or we are in split-screen mode.
1304 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1305 || mStackSupervisor.isFocusedStack(getStack());
1306 }
1307
1308 return isVisible;
1309 }
1310
1311 void makeVisibleIfNeeded(ActivityRecord starting) {
1312 // This activity is not currently visible, but is running. Tell it to become visible.
1313 if (state == ActivityState.RESUMED || this == starting) {
1314 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1315 "Not making visible, r=" + this + " state=" + state + " starting=" + starting);
1316 return;
1317 }
1318
1319 // If this activity is paused, tell it to now show its window.
1320 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1321 "Making visible and scheduling visibility: " + this);
1322 final ActivityStack stack = getStack();
1323 try {
1324 if (stack.mTranslucentActivityWaiting != null) {
1325 updateOptionsLocked(returningOptions);
1326 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1327 }
1328 setVisible(true);
1329 sleeping = false;
1330 app.pendingUiClean = true;
1331 app.thread.scheduleWindowVisibility(appToken, true /* showWindow */);
1332 // The activity may be waiting for stop, but that is no longer appropriate for it.
1333 mStackSupervisor.mStoppingActivities.remove(this);
1334 mStackSupervisor.mGoingToSleepActivities.remove(this);
1335 } catch (Exception e) {
1336 // Just skip on any failure; we'll make it visible when it next restarts.
1337 Slog.w(TAG, "Exception thrown making visibile: " + intent.getComponent(), e);
1338 }
1339 handleAlreadyVisible();
1340 }
1341
1342 boolean handleAlreadyVisible() {
1343 stopFreezingScreenLocked(false);
1344 try {
1345 if (returningOptions != null) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001346 app.thread.scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001347 }
1348 } catch(RemoteException e) {
1349 }
1350 return state == ActivityState.RESUMED;
1351 }
1352
1353 static void activityResumedLocked(IBinder token) {
1354 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1355 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1356 if (r != null) {
1357 r.icicle = null;
1358 r.haveState = false;
1359 }
1360 }
1361
1362 /**
1363 * Once we know that we have asked an application to put an activity in the resumed state
1364 * (either by launching it or explicitly telling it), this function updates the rest of our
1365 * state to match that fact.
1366 */
1367 void completeResumeLocked() {
1368 final boolean wasVisible = visible;
1369 visible = true;
1370 if (!wasVisible) {
1371 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1372 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1373 }
1374 idle = false;
1375 results = null;
1376 newIntents = null;
1377 stopped = false;
1378
1379 if (isHomeActivity()) {
1380 ProcessRecord app = task.mActivities.get(0).app;
1381 if (app != null && app != service.mHomeProcess) {
1382 service.mHomeProcess = app;
1383 }
1384 }
1385
1386 if (nowVisible) {
1387 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1388 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001389 }
1390
1391 // Schedule an idle timeout in case the app doesn't do it for us.
1392 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1393
1394 mStackSupervisor.reportResumedActivityLocked(this);
1395
1396 resumeKeyDispatchingLocked();
1397 final ActivityStack stack = getStack();
1398 stack.mNoAnimActivities.clear();
1399
1400 // Mark the point when the activity is resuming
1401 // TODO: To be more accurate, the mark should be before the onCreate,
1402 // not after the onResume. But for subsequent starts, onResume is fine.
1403 if (app != null) {
1404 cpuTimeAtResume = service.mProcessCpuTracker.getCpuTimeForPid(app.pid);
1405 } else {
1406 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1407 }
1408
1409 returningOptions = null;
1410
1411 if (stack.getVisibleBehindActivity() == this) {
1412 // When resuming an activity, require it to call requestVisibleBehind() again.
1413 stack.setVisibleBehindActivity(null /* ActivityRecord */);
1414 }
1415 mStackSupervisor.checkReadyForSleepLocked();
1416 }
1417
1418 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1419 CharSequence description) {
1420 final ActivityStack stack = getStack();
1421 if (state != ActivityState.STOPPING) {
1422 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
1423 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1424 return;
1425 }
1426 if (newPersistentState != null) {
1427 persistentState = newPersistentState;
1428 service.notifyTaskPersisterLocked(task, false);
1429 }
1430 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001431
Andrii Kulian21713ac2016-10-12 22:05:05 -07001432 if (newIcicle != null) {
1433 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1434 // the state.
1435 icicle = newIcicle;
1436 haveState = true;
1437 launchCount = 0;
1438 updateThumbnailLocked(null /* newThumbnail */, description);
1439 }
1440 if (!stopped) {
1441 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
1442 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1443 stopped = true;
1444 state = ActivityState.STOPPED;
1445
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001446 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001447
1448 if (stack.getVisibleBehindActivity() == this) {
1449 mStackSupervisor.requestVisibleBehindLocked(this, false /* visible */);
1450 }
1451 if (finishing) {
1452 clearOptionsLocked();
1453 } else {
1454 if (deferRelaunchUntilPaused) {
1455 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
1456 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1457 } else {
1458 mStackSupervisor.updatePreviousProcessLocked(this);
1459 }
1460 }
1461 }
1462 }
1463
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001464 void startLaunchTickingLocked() {
1465 if (ActivityManagerService.IS_USER_BUILD) {
1466 return;
1467 }
1468 if (launchTickTime == 0) {
1469 launchTickTime = SystemClock.uptimeMillis();
1470 continueLaunchTickingLocked();
1471 }
1472 }
1473
1474 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001475 if (launchTickTime == 0) {
1476 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001477 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001478
Andrii Kulian02b7a832016-10-06 23:11:56 -07001479 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001480 if (stack == null) {
1481 return false;
1482 }
1483
1484 Message msg = stack.mHandler.obtainMessage(ActivityStack.LAUNCH_TICK_MSG, this);
1485 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1486 stack.mHandler.sendMessageDelayed(msg, ActivityStack.LAUNCH_TICK);
1487 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001488 }
1489
1490 void finishLaunchTickingLocked() {
1491 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001492 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001493 if (stack != null) {
1494 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1495 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001496 }
1497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 // IApplicationToken
1499
1500 public boolean mayFreezeScreenLocked(ProcessRecord app) {
1501 // Only freeze the screen if this activity is currently attached to
1502 // an application, and that application is not blocked or unresponding.
1503 // In any other case, we can't count on getting the screen unfrozen,
1504 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -08001505 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
1509 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001510 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 public void stopFreezingScreenLocked(boolean force) {
1515 if (force || frozenBeforeDestroy) {
1516 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001517 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
1519 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001520
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001521 public void reportFullyDrawnLocked() {
1522 final long curTime = SystemClock.uptimeMillis();
1523 if (displayStartTime != 0) {
1524 reportLaunchTimeLocked(curTime);
1525 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001526 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001527 if (fullyDrawnStartTime != 0 && stack != null) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001528 final long thisTime = curTime - fullyDrawnStartTime;
1529 final long totalTime = stack.mFullyDrawnStartTime != 0
1530 ? (curTime - stack.mFullyDrawnStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001531 if (SHOW_ACTIVITY_START_TIME) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001532 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001533 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME,
1534 userId, System.identityHashCode(this), shortComponentName,
1535 thisTime, totalTime);
1536 StringBuilder sb = service.mStringBuilder;
1537 sb.setLength(0);
1538 sb.append("Fully drawn ");
1539 sb.append(shortComponentName);
1540 sb.append(": ");
1541 TimeUtils.formatDuration(thisTime, sb);
1542 if (thisTime != totalTime) {
1543 sb.append(" (total ");
1544 TimeUtils.formatDuration(totalTime, sb);
1545 sb.append(")");
1546 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001547 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001548 }
1549 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001550 //service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001551 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001552 stack.mFullyDrawnStartTime = 0;
1553 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001554 fullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001555 }
1556
1557 private void reportLaunchTimeLocked(final long curTime) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001558 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001559 if (stack == null) {
1560 return;
1561 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001562 final long thisTime = curTime - displayStartTime;
1563 final long totalTime = stack.mLaunchStartTime != 0
1564 ? (curTime - stack.mLaunchStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001565 if (SHOW_ACTIVITY_START_TIME) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001566 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001567 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME,
1568 userId, System.identityHashCode(this), shortComponentName,
1569 thisTime, totalTime);
1570 StringBuilder sb = service.mStringBuilder;
1571 sb.setLength(0);
1572 sb.append("Displayed ");
1573 sb.append(shortComponentName);
1574 sb.append(": ");
1575 TimeUtils.formatDuration(thisTime, sb);
1576 if (thisTime != totalTime) {
1577 sb.append(" (total ");
1578 TimeUtils.formatDuration(totalTime, sb);
1579 sb.append(")");
1580 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001581 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001582 }
1583 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
1584 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001585 //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001586 }
1587 displayStartTime = 0;
1588 stack.mLaunchStartTime = 0;
1589 }
1590
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001591 @Override
1592 public void onWindowsDrawn() {
1593 synchronized (service) {
1594 mStackSupervisor.mActivityMetricsLogger.notifyWindowsDrawn();
1595 if (displayStartTime != 0) {
1596 reportLaunchTimeLocked(SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001598 mStackSupervisor.sendWaitingVisibleReportLocked(this);
1599 startTime = 0;
1600 finishLaunchTickingLocked();
1601 if (task != null) {
1602 task.hasBeenVisible = true;
1603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
1605 }
1606
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001607 @Override
1608 public void onWindowsVisible() {
1609 synchronized (service) {
1610 mStackSupervisor.reportActivityVisibleLocked(this);
1611 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
1612 if (!nowVisible) {
1613 nowVisible = true;
1614 lastVisibleTime = SystemClock.uptimeMillis();
1615 if (!idle) {
1616 // Instead of doing the full stop routine here, let's just hide any activities
1617 // we now can, and let them stop when the normal idle happens.
1618 mStackSupervisor.processStoppingActivitiesLocked(false);
1619 } else {
1620 // If this activity was already idle, then we now need to make sure we perform
1621 // the full stop of any activities that are waiting to do so. This is because
1622 // we won't do that while they are still waiting for this one to become visible.
1623 final int size = mStackSupervisor.mWaitingVisibleActivities.size();
1624 if (size > 0) {
1625 for (int i = 0; i < size; i++) {
1626 ActivityRecord r = mStackSupervisor.mWaitingVisibleActivities.get(i);
1627 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
1628 }
1629 mStackSupervisor.mWaitingVisibleActivities.clear();
1630 mStackSupervisor.scheduleIdleLocked();
1631 }
1632 }
1633 service.scheduleAppGcsLocked();
1634 }
1635 }
1636 }
1637
1638 @Override
1639 public void onWindowsGone() {
1640 synchronized (service) {
1641 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
1642 nowVisible = false;
1643 }
1644 }
1645
1646 @Override
1647 public boolean keyDispatchingTimedOut(String reason) {
1648 ActivityRecord anrActivity;
1649 ProcessRecord anrApp;
1650 synchronized (service) {
1651 anrActivity = getWaitingHistoryRecordLocked();
1652 anrApp = app;
1653 }
1654 return service.inputDispatchingTimedOut(anrApp, anrActivity, this, false, reason);
1655 }
1656
1657 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001658 // First find the real culprit... if this activity is waiting for
1659 // another activity to start or has stopped, then the key dispatching
1660 // timeout should not be caused by this.
Wale Ogunwale16a62792015-04-01 11:50:31 -07001661 if (mStackSupervisor.mWaitingVisibleActivities.contains(this) || stopped) {
Craig Mautner8f2adcb2014-04-07 22:21:33 +00001662 final ActivityStack stack = mStackSupervisor.getFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001663 // Try to use the one which is closest to top.
1664 ActivityRecord r = stack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001666 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001668 if (r != null) {
1669 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 }
1671 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001672 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
1674
Chong Zhang87761972016-08-22 13:53:24 -07001675 /** Checks whether the activity should be shown for current user. */
1676 public boolean okToShowLocked() {
1677 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
1678 || (mStackSupervisor.isCurrentProfileLocked(userId)
1679 && !service.mUserController.isUserStoppingOrShuttingDownLocked(userId));
1680 }
1681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 /**
1683 * This method will return true if the activity is either visible, is becoming visible, is
1684 * currently pausing, or is resumed.
1685 */
1686 public boolean isInterestingToUserLocked() {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001687 return visible || nowVisible || state == ActivityState.PAUSING ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 state == ActivityState.RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001689 }
1690
Wale Ogunwale3e997362016-09-06 10:37:56 -07001691 void setSleeping(boolean _sleeping) {
1692 setSleeping(_sleeping, false);
1693 }
1694
1695 void setSleeping(boolean _sleeping, boolean force) {
1696 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001697 return;
1698 }
1699 if (app != null && app.thread != null) {
1700 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001701 app.thread.scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001702 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
1703 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001704 }
1705 sleeping = _sleeping;
1706 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001707 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001708 }
1709 }
1710 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08001711
Craig Mautnerd2328952013-03-05 12:46:26 -08001712 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001713 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08001714 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001715 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001716 }
1717 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001718 final int activityNdx = task.mActivities.indexOf(r);
1719 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001720 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001721 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001722 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08001723 }
1724
1725 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001726 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001727 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001728 }
1729
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001730 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001731 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1732 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001733 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08001734 }
1735 return null;
1736 }
1737
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001738 /**
1739 * @return display id to which this record is attached, -1 if not attached.
1740 */
1741 int getDisplayId() {
1742 final ActivityStack stack = getStack();
1743 if (stack == null) {
1744 return -1;
1745 }
1746 return stack.mDisplayId;
1747 }
1748
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001749 final boolean isDestroyable() {
1750 if (finishing || app == null || state == ActivityState.DESTROYING
1751 || state == ActivityState.DESTROYED) {
1752 // This would be redundant.
1753 return false;
1754 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001755 final ActivityStack stack = getStack();
1756 if (stack == null || this == stack.mResumedActivity || this == stack.mPausingActivity
1757 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001758 // We're not ready for this kind of thing.
1759 return false;
1760 }
1761 if (visible) {
1762 // The user would notice this!
1763 return false;
1764 }
1765 return true;
1766 }
1767
Winson Chung3bad5cc02014-08-19 17:44:32 -07001768 private static String createImageFilename(long createTime, int taskId) {
1769 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001770 TaskPersister.IMAGE_EXTENSION;
1771 }
1772
Craig Mautner648f69b2014-09-18 14:16:26 -07001773 void setTaskDescription(TaskDescription _taskDescription) {
1774 Bitmap icon;
1775 if (_taskDescription.getIconFilename() == null &&
1776 (icon = _taskDescription.getIcon()) != null) {
1777 final String iconFilename = createImageFilename(createTime, task.taskId);
Suprabh Shukla23593142015-11-03 17:31:15 -08001778 final File iconFile = new File(TaskPersister.getUserImagesDir(userId), iconFilename);
1779 final String iconFilePath = iconFile.getAbsolutePath();
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001780 service.mRecentTasks.saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08001781 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07001782 }
1783 taskDescription = _taskDescription;
1784 }
1785
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001786 void setVoiceSessionLocked(IVoiceInteractionSession session) {
1787 voiceSession = session;
1788 pendingVoiceInteractionStart = false;
1789 }
1790
1791 void clearVoiceSessionLocked() {
1792 voiceSession = null;
1793 pendingVoiceInteractionStart = false;
1794 }
1795
Wale Ogunwale3b232392016-05-13 15:37:13 -07001796 void showStartingWindow(ActivityRecord prev, boolean createIfNeeded) {
1797 final CompatibilityInfo compatInfo =
1798 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001799 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
1800 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
1801 prev != null ? prev.appToken : null, createIfNeeded);
Wale Ogunwale3b232392016-05-13 15:37:13 -07001802 if (shown) {
1803 mStartingWindowState = STARTING_WINDOW_SHOWN;
1804 }
1805 }
1806
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001807 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
1808 if (state == ActivityState.INITIALIZING
1809 && mStartingWindowState == STARTING_WINDOW_SHOWN
1810 && behindFullscreenActivity) {
1811 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
1812 mStartingWindowState = STARTING_WINDOW_REMOVED;
1813 mWindowContainerController.removeStartingWindow();
1814 }
1815 }
1816
1817 int getRequestedOrientation() {
1818 return mWindowContainerController.getOrientation();
1819 }
1820
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001821 void setRequestedOrientation(int requestedOrientation) {
1822 if (task != null && (!task.mFullscreen || !task.getStack().mFullscreen)) {
1823 // Fixed screen orientation isn't supported when activities aren't in full screen mode.
1824 return;
1825 }
1826
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001827 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001828 final Configuration displayConfig =
1829 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
1830
1831 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
1832 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001833 if (config != null) {
1834 frozenBeforeDestroy = true;
1835 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
1836 false /* deferResume */, displayId)) {
1837 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1838 }
1839 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001840 service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
1841 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001842 }
1843
Andrii Kulian21713ac2016-10-12 22:05:05 -07001844 // TODO: now used only in one place to address race-condition. Remove when that will be fixed.
1845 void setLastReportedConfiguration(@NonNull Configuration config) {
1846 mLastReportedConfiguration.setTo(config);
1847 }
1848
1849 /** Call when override config was sent to the Window Manager to update internal records. */
1850 void onOverrideConfigurationSent() {
1851 mLastReportedOverrideConfiguration.setTo(task.getMergedOverrideConfiguration());
1852 }
1853
1854 /**
1855 * Make sure the given activity matches the current configuration. Returns false if the activity
1856 * had to be destroyed. Returns true if the configuration is the same, or the activity will
1857 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
1858 * correct configuration and all other bookkeeping is handled.
1859 */
1860 boolean ensureActivityConfigurationLocked(int globalChanges, boolean preserveWindow) {
1861 final ActivityStack stack = getStack();
1862 if (stack.mConfigWillChange) {
1863 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1864 "Skipping config check (will change): " + this);
1865 return true;
1866 }
1867
1868 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1869 "Ensuring correct configuration: " + this);
1870
1871 // Short circuit: if the two configurations are equal (the common case), then there is
1872 // nothing to do.
1873 final Configuration newGlobalConfig = service.getGlobalConfiguration();
1874 final Configuration newTaskMergedOverrideConfig = task.getMergedOverrideConfiguration();
1875 if (mLastReportedConfiguration.equals(newGlobalConfig)
1876 && mLastReportedOverrideConfiguration.equals(newTaskMergedOverrideConfig)
1877 && !forceNewConfig) {
1878 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1879 "Configuration unchanged in " + this);
1880 return true;
1881 }
1882
1883 // We don't worry about activities that are finishing.
1884 if (finishing) {
1885 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1886 "Configuration doesn't matter in finishing " + this);
1887 stopFreezingScreenLocked(false);
1888 return true;
1889 }
1890
1891 // Okay we now are going to make this activity have the new config.
1892 // But then we need to figure out how it needs to deal with that.
1893 mTmpGlobalConfig.setTo(mLastReportedConfiguration);
1894 mTmpTaskConfig.setTo(mLastReportedOverrideConfiguration);
1895 mLastReportedConfiguration.setTo(newGlobalConfig);
1896 mLastReportedOverrideConfiguration.setTo(newTaskMergedOverrideConfig);
1897
1898 int taskChanges = getTaskConfigurationChanges(this, newTaskMergedOverrideConfig,
1899 mTmpTaskConfig);
1900 final int changes = mTmpGlobalConfig.diff(newGlobalConfig) | taskChanges;
1901 if (changes == 0 && !forceNewConfig) {
1902 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1903 "Configuration no differences in " + this);
1904 // There are no significant differences, so we won't relaunch but should still deliver
1905 // the new configuration to the client process.
1906 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
1907 return true;
1908 }
1909
1910 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1911 "Configuration changes for " + this + " ; taskChanges="
1912 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
1913 + Configuration.configurationDiffToString(changes));
1914
1915 // If the activity isn't currently running, just leave the new configuration and it will
1916 // pick that up next time it starts.
1917 if (app == null || app.thread == null) {
1918 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1919 "Configuration doesn't matter not running " + this);
1920 stopFreezingScreenLocked(false);
1921 forceNewConfig = false;
1922 return true;
1923 }
1924
1925 // Figure out how to handle the changes between the configurations.
1926 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1927 "Checking to restart " + info.name + ": changed=0x"
1928 + Integer.toHexString(changes) + ", handles=0x"
1929 + Integer.toHexString(info.getRealConfigChanged())
1930 + ", newGlobalConfig=" + newGlobalConfig
1931 + ", newTaskMergedOverrideConfig=" + newTaskMergedOverrideConfig);
1932
1933 if ((changes&(~info.getRealConfigChanged())) != 0 || forceNewConfig) {
1934 // Aha, the activity isn't handling the change, so DIE DIE DIE.
1935 configChangeFlags |= changes;
1936 startFreezingScreenLocked(app, globalChanges);
1937 forceNewConfig = false;
1938 preserveWindow &= isResizeOnlyChange(changes);
1939 if (app == null || app.thread == null) {
1940 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1941 "Config is destroying non-running " + this);
1942 stack.destroyActivityLocked(this, true, "config");
1943 } else if (state == ActivityState.PAUSING) {
1944 // A little annoying: we are waiting for this activity to finish pausing. Let's not
1945 // do anything now, but just flag that it needs to be restarted when done pausing.
1946 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1947 "Config is skipping already pausing " + this);
1948 deferRelaunchUntilPaused = true;
1949 preserveWindowOnDeferredRelaunch = preserveWindow;
1950 return true;
1951 } else if (state == ActivityState.RESUMED) {
1952 // Try to optimize this case: the configuration is changing and we need to restart
1953 // the top, resumed activity. Instead of doing the normal handshaking, just say
1954 // "restart!".
1955 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1956 "Config is relaunching resumed " + this);
1957
1958 if (DEBUG_STATES && !visible) {
1959 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
1960 + " called by " + Debug.getCallers(4));
1961 }
1962
1963 relaunchActivityLocked(true /* andResume */, preserveWindow);
1964 } else {
1965 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1966 "Config is relaunching non-resumed " + this);
1967 relaunchActivityLocked(false /* andResume */, preserveWindow);
1968 }
1969
1970 // All done... tell the caller we weren't able to keep this activity around.
1971 return false;
1972 }
1973
1974 // Default case: the activity can handle this new configuration, so hand it over.
1975 // NOTE: We only forward the task override configuration as the system level configuration
1976 // changes is always sent to all processes when they happen so it can just use whatever
1977 // system level configuration it last got.
1978 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
1979 stopFreezingScreenLocked(false);
1980
1981 return true;
1982 }
1983
1984 private static int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
1985 Configuration oldTaskOverride) {
1986 // If we went from full-screen to non-full-screen, make sure to use the correct
1987 // configuration task diff, so the diff stays as small as possible.
1988 if (Configuration.EMPTY.equals(oldTaskOverride)
1989 && !Configuration.EMPTY.equals(taskConfig)) {
1990 oldTaskOverride = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
1991 }
1992
1993 // Conversely, do the same when going the other direction.
1994 if (Configuration.EMPTY.equals(taskConfig)
1995 && !Configuration.EMPTY.equals(oldTaskOverride)) {
1996 taskConfig = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
1997 }
1998
1999 // Determine what has changed. May be nothing, if this is a config that has come back from
2000 // the app after going idle. In that case we just want to leave the official config object
2001 // now in the activity and do nothing else.
2002 int taskChanges = oldTaskOverride.diff(taskConfig, true /* skipUndefined */);
2003 // We don't want to use size changes if they don't cross boundaries that are important to
2004 // the app.
2005 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
2006 final boolean crosses = record.crossesHorizontalSizeThreshold(
2007 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
2008 || record.crossesVerticalSizeThreshold(
2009 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
2010 if (!crosses) {
2011 taskChanges &= ~CONFIG_SCREEN_SIZE;
2012 }
2013 }
2014 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
2015 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
2016 final int newSmallest = taskConfig.smallestScreenWidthDp;
2017 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
2018 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
2019 }
2020 }
2021 return taskChanges;
2022 }
2023
2024 private static boolean isResizeOnlyChange(int change) {
2025 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2026 | CONFIG_SCREEN_LAYOUT)) == 0;
2027 }
2028
2029 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
2030 if (service.mSuppressResizeConfigChanges && preserveWindow) {
2031 configChangeFlags = 0;
2032 return;
2033 }
2034
2035 List<ResultInfo> pendingResults = null;
2036 List<ReferrerIntent> pendingNewIntents = null;
2037 if (andResume) {
2038 pendingResults = results;
2039 pendingNewIntents = newIntents;
2040 }
2041 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2042 "Relaunching: " + this + " with results=" + pendingResults
2043 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2044 + " preserveWindow=" + preserveWindow);
2045 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
2046 : EventLogTags.AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
2047 task.taskId, shortComponentName);
2048
2049 startFreezingScreenLocked(app, 0);
2050
2051 mStackSupervisor.removeChildActivityContainers(this);
2052
2053 try {
2054 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2055 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2056 + " callers=" + Debug.getCallers(6));
2057 forceNewConfig = false;
2058 mStackSupervisor.activityRelaunchingLocked(this);
2059 app.thread.scheduleRelaunchActivity(appToken, pendingResults, pendingNewIntents,
2060 configChangeFlags, !andResume,
2061 new Configuration(service.getGlobalConfiguration()),
2062 new Configuration(task.getMergedOverrideConfiguration()), preserveWindow);
2063 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
2064 // pass in 'andResume' if this activity is currently resumed, which implies we aren't
2065 // sleeping.
2066 } catch (RemoteException e) {
2067 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2068 }
2069
2070 if (andResume) {
2071 if (DEBUG_STATES) {
2072 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2073 }
2074 results = null;
2075 newIntents = null;
2076 service.showUnsupportedZoomDialogIfNeededLocked(this);
2077 service.showAskCompatModeDialogLocked(this);
2078 } else {
2079 service.mHandler.removeMessages(ActivityStack.PAUSE_TIMEOUT_MSG, this);
2080 state = ActivityState.PAUSED;
2081 // if the app is relaunched when it's stopped, and we're not resuming,
2082 // put it back into stopped state.
2083 if (stopped) {
2084 getStack().addToStopping(this, true /* immediate */);
2085 }
2086 }
2087
2088 configChangeFlags = 0;
2089 deferRelaunchUntilPaused = false;
2090 preserveWindowOnDeferredRelaunch = false;
2091 }
2092
Craig Mautner21d24a22014-04-23 11:45:37 -07002093 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2094 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2095 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2096 if (launchedFromPackage != null) {
2097 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2098 }
2099 if (resolvedType != null) {
2100 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2101 }
2102 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2103 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002104
Craig Mautner21d24a22014-04-23 11:45:37 -07002105 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002106 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002107 }
2108
2109 out.startTag(null, TAG_INTENT);
2110 intent.saveToXml(out);
2111 out.endTag(null, TAG_INTENT);
2112
2113 if (isPersistable() && persistentState != null) {
2114 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2115 persistentState.saveToXml(out);
2116 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2117 }
2118 }
2119
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002120 static ActivityRecord restoreFromXml(XmlPullParser in,
2121 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002122 Intent intent = null;
2123 PersistableBundle persistentState = null;
2124 int launchedFromUid = 0;
2125 String launchedFromPackage = null;
2126 String resolvedType = null;
2127 boolean componentSpecified = false;
2128 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002129 long createTime = -1;
2130 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002131 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002132
2133 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2134 final String attrName = in.getAttributeName(attrNdx);
2135 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002136 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002137 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002138 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002139 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002140 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002141 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002142 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2143 launchedFromPackage = attrValue;
2144 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2145 resolvedType = attrValue;
2146 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002147 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002148 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002149 userId = Integer.parseInt(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07002150 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
2151 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002152 } else {
2153 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2154 }
2155 }
2156
2157 int event;
2158 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07002159 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002160 if (event == XmlPullParser.START_TAG) {
2161 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002162 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002163 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002164 if (TAG_INTENT.equals(name)) {
2165 intent = Intent.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002166 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002167 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002168 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2169 persistentState = PersistableBundle.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002170 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002171 "ActivityRecord: persistentState=" + persistentState);
2172 } else {
2173 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2174 XmlUtils.skipCurrentTag(in);
2175 }
2176 }
2177 }
2178
2179 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002180 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002181 }
2182
2183 final ActivityManagerService service = stackSupervisor.mService;
2184 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Jeff Hao1b012d32014-08-20 10:35:34 -07002185 userId);
Craig Mautnere0129b32014-05-25 16:41:09 -07002186 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002187 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2188 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002189 }
Craig Mautner21d24a22014-04-23 11:45:37 -07002190 final ActivityRecord r = new ActivityRecord(service, /*caller*/null, launchedFromUid,
2191 launchedFromPackage, intent, resolvedType, aInfo, service.getConfiguration(),
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07002192 null, null, 0, componentSpecified, false, stackSupervisor, null, null, null);
Craig Mautner21d24a22014-04-23 11:45:37 -07002193
2194 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002195 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002196 r.createTime = createTime;
2197
2198 return r;
2199 }
2200
2201 private static String activityTypeToString(int type) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002202 switch (type) {
2203 case APPLICATION_ACTIVITY_TYPE: return "APPLICATION_ACTIVITY_TYPE";
2204 case HOME_ACTIVITY_TYPE: return "HOME_ACTIVITY_TYPE";
2205 case RECENTS_ACTIVITY_TYPE: return "RECENTS_ACTIVITY_TYPE";
2206 default: return Integer.toString(type);
2207 }
2208 }
2209
Craig Mautnerf81b90872013-02-26 13:02:43 -08002210 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002212 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002213 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002214 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002215 }
2216 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002217 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002218 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002219 sb.append(" u");
2220 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002221 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002222 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002223 stringName = sb.toString();
2224 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 }
2226}