blob: 2963ad1d54f614cf6b8f9154f131a55cac745d40 [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;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080044import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_CONFIGURATION;
Andrii Kulian21713ac2016-10-12 22:05:05 -070045import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SAVED_STATE;
46import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SCREENSHOTS;
47import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_STATES;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080048import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_SWITCH;
49import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070050import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_VISIBILITY;
51import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_CONFIGURATION;
52import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SAVED_STATE;
53import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SCREENSHOTS;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080054import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_STATES;
55import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SWITCH;
56import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -070057import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_VISIBILITY;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080058import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
59import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Andrii Kulian21713ac2016-10-12 22:05:05 -070060import static com.android.server.am.ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS;
Wale Ogunwale18795a22014-12-03 11:38:33 -080061import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
62
Andrii Kulian21713ac2016-10-12 22:05:05 -070063import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -070064import android.app.ActivityManager.TaskDescription;
Winson Chunga29eb982016-12-14 12:01:27 -080065import android.app.ActivityManagerInternal.PictureInPictureArguments;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070066import android.app.ActivityOptions;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080067import android.app.PendingIntent;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080068import android.app.ResultInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.ComponentName;
70import android.content.Intent;
71import android.content.pm.ActivityInfo;
72import android.content.pm.ApplicationInfo;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -070073import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.content.res.Configuration;
75import android.graphics.Bitmap;
Dianne Hackbornd367ca82012-05-07 15:49:39 -070076import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.os.Bundle;
Andrii Kulian21713ac2016-10-12 22:05:05 -070078import android.os.Debug;
Dianne Hackbornbe707852011-11-11 14:32:10 -080079import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.os.Message;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080081import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Process;
Dianne Hackborn39792d22010-08-19 18:01:52 -070083import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.SystemClock;
Filip Gruszczynski3d026712015-12-16 13:46:38 -080085import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070086import android.os.UserHandle;
Amith Yamasani0af6fa72016-01-17 15:36:19 -080087import android.service.voice.IVoiceInteractionSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.util.EventLog;
89import android.util.Log;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070090import android.util.Slog;
Dianne Hackborn0dad3642010-09-09 21:25:35 -070091import android.util.TimeUtils;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -070092import android.view.AppTransitionAnimationSpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.view.IApplicationToken;
Jorim Jaggife762342016-10-13 14:33:27 +020094import android.view.WindowManager.LayoutParams;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -070095
Filip Gruszczynski3d026712015-12-16 13:46:38 -080096import com.android.internal.app.ResolverActivity;
97import com.android.internal.content.ReferrerIntent;
98import com.android.internal.util.XmlUtils;
99import com.android.server.AttributeCache;
100import com.android.server.am.ActivityStack.ActivityState;
101import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800102import com.android.server.wm.AppWindowContainerController;
103import com.android.server.wm.AppWindowContainerListener;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800104import com.android.server.wm.TaskWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Suprabh Shukla23593142015-11-03 17:31:15 -0800106import java.io.File;
Craig Mautner21d24a22014-04-23 11:45:37 -0700107import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import java.io.PrintWriter;
109import java.lang.ref.WeakReference;
110import java.util.ArrayList;
Todd Kennedy39bfee52016-02-24 10:28:21 -0800111import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.util.HashSet;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700113import java.util.List;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700114import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800116import org.xmlpull.v1.XmlPullParser;
117import org.xmlpull.v1.XmlPullParserException;
118import org.xmlpull.v1.XmlSerializer;
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120/**
121 * An entry in the history stack, representing an activity.
122 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800123final class ActivityRecord implements AppWindowContainerListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800124 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityRecord" : TAG_AM;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700125 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
126 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
127 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700128 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700129 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
130 private static final String TAG_THUMBNAILS = TAG + POSTFIX_THUMBNAILS;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700131 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800132
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700133 private static final boolean SHOW_ACTIVITY_START_TIME = true;
Andrii Kulian21713ac2016-10-12 22:05:05 -0700134 private static final String RECENTS_PACKAGE_NAME = "com.android.systemui.recents";
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700135
Craig Mautner21d24a22014-04-23 11:45:37 -0700136 private static final String ATTR_ID = "id";
137 private static final String TAG_INTENT = "intent";
138 private static final String ATTR_USERID = "user_id";
139 private static final String TAG_PERSISTABLEBUNDLE = "persistable_bundle";
140 private static final String ATTR_LAUNCHEDFROMUID = "launched_from_uid";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700141 private static final String ATTR_LAUNCHEDFROMPACKAGE = "launched_from_package";
Craig Mautner21d24a22014-04-23 11:45:37 -0700142 private static final String ATTR_RESOLVEDTYPE = "resolved_type";
143 private static final String ATTR_COMPONENTSPECIFIED = "component_specified";
Dianne Hackborn337abb32014-09-24 12:44:29 -0700144 static final String ACTIVITY_ICON_SUFFIX = "_activity_icon_";
Craig Mautner21d24a22014-04-23 11:45:37 -0700145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 final ActivityManagerService service; // owner
Dianne Hackbornbe707852011-11-11 14:32:10 -0800147 final IApplicationToken.Stub appToken; // window manager token
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800148 AppWindowContainerController mWindowContainerController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 final ActivityInfo info; // all about me
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700150 final ApplicationInfo appInfo; // information about activity's app
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700151 final int launchedFromUid; // always the uid who started the activity.
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800152 final String launchedFromPackage; // always the package who started the activity.
Amith Yamasani742a6712011-05-04 14:49:28 -0700153 final int userId; // Which user is this running for?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 final Intent intent; // the original intent that generated us
155 final ComponentName realActivity; // the intent component, or target of an alias.
156 final String shortComponentName; // the short component name of the intent
157 final String resolvedType; // as per original caller;
158 final String packageName; // the package implementing intent's component
159 final String processName; // process where this component wants to run
160 final String taskAffinity; // as per ActivityInfo.taskAffinity
161 final boolean stateNotNeeded; // As per ActivityInfo.flags
Craig Mautner4addfc52013-06-25 08:05:45 -0700162 boolean fullscreen; // covers the full screen?
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800163 final boolean noDisplay; // activity is not displayed?
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800164 private final boolean componentSpecified; // did caller specify an explicit component?
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700165 final boolean rootVoiceInteraction; // was this the root activity of a voice interaction?
Craig Mautner86d67a42013-05-14 10:34:38 -0700166
167 static final int APPLICATION_ACTIVITY_TYPE = 0;
168 static final int HOME_ACTIVITY_TYPE = 1;
169 static final int RECENTS_ACTIVITY_TYPE = 2;
Craig Mautner2c1faed2013-07-23 12:56:02 -0700170 int mActivityType;
Craig Mautner86d67a42013-05-14 10:34:38 -0700171
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800172 private CharSequence nonLocalizedLabel; // the label information from the package mgr.
173 private int labelRes; // the label information from the package mgr.
174 private int icon; // resource identifier of activity's icon.
175 private int logo; // resource identifier of activity's logo.
176 private int theme; // resource identifier of activity's theme.
177 private int realTheme; // actual theme resource we will use, never 0.
178 private int windowFlags; // custom window flags for preview window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 TaskRecord task; // the task this is in.
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800180 private long createTime = System.currentTimeMillis();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700181 long displayStartTime; // when we started launching this activity
182 long fullyDrawnStartTime; // when we started launching this activity
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800183 private long startTime; // last time this activity was started
Dianne Hackborn50685602011-12-01 12:23:37 -0800184 long lastVisibleTime; // last time this activity became visible
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700185 long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700186 long pauseTime; // last time we started pausing the activity
187 long launchTickTime; // base time for launch tick messages
Andrii Kulian21713ac2016-10-12 22:05:05 -0700188 private Configuration mLastReportedConfiguration; // configuration activity was last running in
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700189 // Overridden configuration by the activity task
Andrii Kulian1779e612016-10-12 21:58:25 -0700190 // WARNING: Reference points to {@link TaskRecord#getMergedOverrideConfig}, so its internal
191 // state should never be altered directly.
Andrii Kulian21713ac2016-10-12 22:05:05 -0700192 private Configuration mLastReportedOverrideConfiguration;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700193 CompatibilityInfo compat;// last used compatibility mode
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 ActivityRecord resultTo; // who started this entry, so will get our reply
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 final String resultWho; // additional identifier for use by resultTo.
196 final int requestCode; // code given by requester (resultTo)
Craig Mautner05d6272ba2013-02-11 09:39:27 -0800197 ArrayList<ResultInfo> results; // pending ActivityResult objs we have received
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 HashSet<WeakReference<PendingIntentRecord>> pendingResults; // all pending intents for this act
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800199 ArrayList<ReferrerIntent> newIntents; // any pending new intents for single-top mode
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700200 ActivityOptions pendingOptions; // most recently given options
George Mount6ba042b2014-07-28 11:12:28 -0700201 ActivityOptions returningOptions; // options that are coming back via convertToTranslucent
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700202 AppTimeTracker appTimeTracker; // set if we are tracking the time in this app/task/activity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 HashSet<ConnectionRecord> connections; // All ConnectionRecord we hold
Dianne Hackborn7e269642010-08-25 19:50:20 -0700204 UriPermissionOwner uriPermissions; // current special URI access perms.
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700205 ProcessRecord app; // if non-null, hosting application
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700206 ActivityState state; // current state we are in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 Bundle icicle; // last saved activity state
Craig Mautnera0026042014-04-23 11:45:37 -0700208 PersistableBundle persistentState; // last persistently saved activity state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 boolean frontOfTask; // is this the root activity of its task?
210 boolean launchFailed; // set if a launched failed, to abort on 2nd try
211 boolean haveState; // have we gotten the last activity state?
212 boolean stopped; // is activity pause finished?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700213 boolean delayedResume; // not yet resumed because of stopped app switches?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 boolean finishing; // activity in pending finish list?
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800215 boolean deferRelaunchUntilPaused; // relaunch of activity is being deferred until pause is
216 // completed
217 boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 int configChangeFlags; // which config values have changed
219 boolean keysPaused; // has key dispatching been paused for it?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 int launchMode; // the launch mode activity attribute.
221 boolean visible; // does this activity's window need to be shown?
Jorim Jaggi241ae102016-11-02 21:57:33 -0700222 boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
223 // might hide this activity?
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800224 boolean sleeping; // have we told the activity to sleep?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 boolean nowVisible; // is this activity's window visible?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 boolean idle; // has the activity gone idle?
227 boolean hasBeenLaunched;// has this activity ever been launched?
228 boolean frozenBeforeDestroy;// has been frozen but not yet destroyed.
Daniel Sandler69a48172010-06-23 16:29:36 -0400229 boolean immersive; // immersive mode (don't interrupt if possible)
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400230 boolean forceNewConfig; // force re-create with new config next time
Winson Chungb5c41b72016-12-07 15:00:47 -0800231 boolean supportsPipOnMoveToBackground; // Supports automatically entering picture-in-picture
Winson Chunga29eb982016-12-14 12:01:27 -0800232 // when this activity is hidden. This flag is requested by the activity.
Winson Chungb5c41b72016-12-07 15:00:47 -0800233 private boolean enterPipOnMoveToBackground; // Flag to enter picture in picture when this
Winson Chunga29eb982016-12-14 12:01:27 -0800234 // activity is made invisible. This flag is set specifically when another task is being
235 // launched or moved to the front which may cause this activity to try and enter PiP
236 // when it is next made invisible.
237 PictureInPictureArguments pictureInPictureArgs = new PictureInPictureArguments(); // The PiP
238 // arguments used when deferring the entering of picture-in-picture.
Dianne Hackborn07981492013-01-28 11:36:23 -0800239 int launchCount; // count of launches since last state
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800240 long lastLaunchTime; // time of last launch of this activity
Ruben Brunke24b9a62016-02-16 21:38:24 -0800241 ComponentName requestedVrComponent; // the requested component for handling VR mode.
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800242 ArrayList<ActivityContainer> mChildContainers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700244 String stringName; // for caching of toString().
Craig Mautnerde4ef022013-04-07 19:01:33 -0700245
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700246 private boolean inHistory; // are we in the history stack?
Craig Mautnerde4ef022013-04-07 19:01:33 -0700247 final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800248
249 static final int STARTING_WINDOW_NOT_SHOWN = 0;
250 static final int STARTING_WINDOW_SHOWN = 1;
251 static final int STARTING_WINDOW_REMOVED = 2;
252 int mStartingWindowState = STARTING_WINDOW_NOT_SHOWN;
Wale Ogunwale3b232392016-05-13 15:37:13 -0700253 boolean mTaskOverlay = false; // Task is always on-top of other activities in the task.
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800254
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800255 boolean mUpdateTaskThumbnailWhenHidden;
Craig Mautnere0a38842013-12-16 16:14:02 -0800256 ActivityContainer mInitialActivityContainer;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700257
Craig Mautner21d24a22014-04-23 11:45:37 -0700258 TaskDescription taskDescription; // the recents information for this activity
Craig Mautnerbb742462014-07-07 15:28:55 -0700259 boolean mLaunchTaskBehind; // this activity is actively being launched with
260 // ActivityOptions.setLaunchTaskBehind, will be cleared once launch is completed.
Craig Mautner2fbd7542014-03-21 09:34:07 -0700261
Filip Gruszczynski23493322015-07-29 17:02:59 -0700262 // These configurations are collected from application's resources based on size-sensitive
263 // qualifiers. For example, layout-w800dp will be added to mHorizontalSizeConfigurations as 800
264 // and drawable-sw400dp will be added to both as 400.
265 private int[] mVerticalSizeConfigurations;
266 private int[] mHorizontalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700267 private int[] mSmallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700268
Amith Yamasani0af6fa72016-01-17 15:36:19 -0800269 boolean pendingVoiceInteractionStart; // Waiting for activity-invoked voice session
270 IVoiceInteractionSession voiceSession; // Voice interaction session for this activity
271
Robert Carrd2265122016-08-05 10:25:21 -0700272 // A hint to override the window specified rotation animation, or -1
273 // to use the window specified value. We use this so that
274 // we can select the right animation in the cases of starting
275 // windows, where the app hasn't had time to set a value
276 // on the window.
277 int mRotationAnimationHint = -1;
Robert Carrfd10cd12016-06-29 16:41:50 -0700278
Andrii Kulian21713ac2016-10-12 22:05:05 -0700279 /**
280 * Temp configs used in {@link #ensureActivityConfigurationLocked(int, boolean)}
281 */
282 private final Configuration mTmpGlobalConfig = new Configuration();
283 private final Configuration mTmpTaskConfig = new Configuration();
284
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800285 private static String startingWindowStateToString(int state) {
286 switch (state) {
287 case STARTING_WINDOW_NOT_SHOWN:
288 return "STARTING_WINDOW_NOT_SHOWN";
289 case STARTING_WINDOW_SHOWN:
290 return "STARTING_WINDOW_SHOWN";
291 case STARTING_WINDOW_REMOVED:
292 return "STARTING_WINDOW_REMOVED";
293 default:
294 return "unknown state=" + state;
295 }
296 }
297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700299 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700300 pw.print(prefix); pw.print("packageName="); pw.print(packageName);
301 pw.print(" processName="); pw.println(processName);
302 pw.print(prefix); pw.print("launchedFromUid="); pw.print(launchedFromUid);
Craig Mautnere11f2b72013-04-01 12:37:17 -0700303 pw.print(" launchedFromPackage="); pw.print(launchedFromPackage);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800304 pw.print(" userId="); pw.println(userId);
305 pw.print(prefix); pw.print("app="); pw.println(app);
306 pw.print(prefix); pw.println(intent.toInsecureStringWithClip());
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700307 pw.print(prefix); pw.print("frontOfTask="); pw.print(frontOfTask);
308 pw.print(" task="); pw.println(task);
309 pw.print(prefix); pw.print("taskAffinity="); pw.println(taskAffinity);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700310 pw.print(prefix); pw.print("realActivity=");
311 pw.println(realActivity.flattenToShortString());
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700312 if (appInfo != null) {
313 pw.print(prefix); pw.print("baseDir="); pw.println(appInfo.sourceDir);
314 if (!Objects.equals(appInfo.sourceDir, appInfo.publicSourceDir)) {
315 pw.print(prefix); pw.print("resDir="); pw.println(appInfo.publicSourceDir);
316 }
317 pw.print(prefix); pw.print("dataDir="); pw.println(appInfo.dataDir);
Todd Kennedy39bfee52016-02-24 10:28:21 -0800318 if (appInfo.splitSourceDirs != null) {
319 pw.print(prefix); pw.print("splitDir=");
320 pw.println(Arrays.toString(appInfo.splitSourceDirs));
321 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800322 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700323 pw.print(prefix); pw.print("stateNotNeeded="); pw.print(stateNotNeeded);
324 pw.print(" componentSpecified="); pw.print(componentSpecified);
Craig Mautner86d67a42013-05-14 10:34:38 -0700325 pw.print(" mActivityType="); pw.println(mActivityType);
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700326 if (rootVoiceInteraction) {
327 pw.print(prefix); pw.print("rootVoiceInteraction="); pw.println(rootVoiceInteraction);
328 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800329 pw.print(prefix); pw.print("compat="); pw.print(compat);
330 pw.print(" labelRes=0x"); pw.print(Integer.toHexString(labelRes));
331 pw.print(" icon=0x"); pw.print(Integer.toHexString(icon));
332 pw.print(" theme=0x"); pw.println(Integer.toHexString(theme));
Andrii Kulian1779e612016-10-12 21:58:25 -0700333 pw.print(prefix); pw.print("mLastReportedConfiguration=");
334 pw.println(mLastReportedConfiguration);
335 pw.print(prefix); pw.print("mLastReportedOverrideConfiguration=");
336 pw.println(mLastReportedOverrideConfiguration);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700337 if (resultTo != null || resultWho != null) {
338 pw.print(prefix); pw.print("resultTo="); pw.print(resultTo);
339 pw.print(" resultWho="); pw.print(resultWho);
340 pw.print(" resultCode="); pw.println(requestCode);
341 }
Craig Mautner29c58ca2014-10-14 16:17:06 -0700342 if (taskDescription != null) {
343 final String iconFilename = taskDescription.getIconFilename();
344 if (iconFilename != null || taskDescription.getLabel() != null ||
345 taskDescription.getPrimaryColor() != 0) {
346 pw.print(prefix); pw.print("taskDescription:");
347 pw.print(" iconFilename="); pw.print(taskDescription.getIconFilename());
348 pw.print(" label=\""); pw.print(taskDescription.getLabel());
349 pw.print("\"");
350 pw.print(" color=");
351 pw.println(Integer.toHexString(taskDescription.getPrimaryColor()));
352 }
353 if (iconFilename == null && taskDescription.getIcon() != null) {
354 pw.print(prefix); pw.println("taskDescription contains Bitmap");
355 }
Craig Mautner648f69b2014-09-18 14:16:26 -0700356 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700357 if (results != null) {
358 pw.print(prefix); pw.print("results="); pw.println(results);
359 }
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700360 if (pendingResults != null && pendingResults.size() > 0) {
361 pw.print(prefix); pw.println("Pending Results:");
362 for (WeakReference<PendingIntentRecord> wpir : pendingResults) {
363 PendingIntentRecord pir = wpir != null ? wpir.get() : null;
364 pw.print(prefix); pw.print(" - ");
365 if (pir == null) {
366 pw.println("null");
367 } else {
368 pw.println(pir);
369 pir.dump(pw, prefix + " ");
370 }
371 }
372 }
373 if (newIntents != null && newIntents.size() > 0) {
374 pw.print(prefix); pw.println("Pending New Intents:");
375 for (int i=0; i<newIntents.size(); i++) {
Craig Mautnerd2328952013-03-05 12:46:26 -0800376 Intent intent = newIntents.get(i);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700377 pw.print(prefix); pw.print(" - ");
378 if (intent == null) {
379 pw.println("null");
380 } else {
381 pw.println(intent.toShortString(false, true, false, true));
382 }
383 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700384 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700385 if (pendingOptions != null) {
386 pw.print(prefix); pw.print("pendingOptions="); pw.println(pendingOptions);
387 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700388 if (appTimeTracker != null) {
389 appTimeTracker.dumpWithHeader(pw, prefix, false);
390 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700391 if (uriPermissions != null) {
Jeff Sharkey846318a2014-04-04 12:12:41 -0700392 uriPermissions.dump(pw, prefix);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700393 }
394 pw.print(prefix); pw.print("launchFailed="); pw.print(launchFailed);
Dianne Hackborn07981492013-01-28 11:36:23 -0800395 pw.print(" launchCount="); pw.print(launchCount);
396 pw.print(" lastLaunchTime=");
397 if (lastLaunchTime == 0) pw.print("0");
398 else TimeUtils.formatDuration(lastLaunchTime, now, pw);
399 pw.println();
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700400 pw.print(prefix); pw.print("haveState="); pw.print(haveState);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700401 pw.print(" icicle="); pw.println(icicle);
402 pw.print(prefix); pw.print("state="); pw.print(state);
403 pw.print(" stopped="); pw.print(stopped);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700404 pw.print(" delayedResume="); pw.print(delayedResume);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700405 pw.print(" finishing="); pw.println(finishing);
406 pw.print(prefix); pw.print("keysPaused="); pw.print(keysPaused);
407 pw.print(" inHistory="); pw.print(inHistory);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700408 pw.print(" visible="); pw.print(visible);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800409 pw.print(" sleeping="); pw.print(sleeping);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -0800410 pw.print(" idle="); pw.print(idle);
411 pw.print(" mStartingWindowState=");
412 pw.println(startingWindowStateToString(mStartingWindowState));
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800413 pw.print(prefix); pw.print("fullscreen="); pw.print(fullscreen);
414 pw.print(" noDisplay="); pw.print(noDisplay);
415 pw.print(" immersive="); pw.print(immersive);
416 pw.print(" launchMode="); pw.println(launchMode);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800417 pw.print(prefix); pw.print("frozenBeforeDestroy="); pw.print(frozenBeforeDestroy);
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400418 pw.print(" forceNewConfig="); pw.println(forceNewConfig);
Craig Mautnerae7ecab2013-09-18 11:48:14 -0700419 pw.print(prefix); pw.print("mActivityType=");
420 pw.println(activityTypeToString(mActivityType));
Ruben Brunke24b9a62016-02-16 21:38:24 -0800421 if (requestedVrComponent != null) {
422 pw.print(prefix);
423 pw.print("requestedVrComponent=");
424 pw.println(requestedVrComponent);
425 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700426 if (displayStartTime != 0 || startTime != 0) {
427 pw.print(prefix); pw.print("displayStartTime=");
428 if (displayStartTime == 0) pw.print("0");
429 else TimeUtils.formatDuration(displayStartTime, now, pw);
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700430 pw.print(" startTime=");
431 if (startTime == 0) pw.print("0");
432 else TimeUtils.formatDuration(startTime, now, pw);
433 pw.println();
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700434 }
Craig Mautner8c14c152015-01-15 17:32:07 -0800435 final boolean waitingVisible = mStackSupervisor.mWaitingVisibleActivities.contains(this);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800436 if (lastVisibleTime != 0 || waitingVisible || nowVisible) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700437 pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible);
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800438 pw.print(" nowVisible="); pw.print(nowVisible);
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700439 pw.print(" lastVisibleTime=");
Dianne Hackbornf530ac32012-06-21 14:17:48 -0700440 if (lastVisibleTime == 0) pw.print("0");
441 else TimeUtils.formatDuration(lastVisibleTime, now, pw);
442 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700443 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800444 if (deferRelaunchUntilPaused || configChangeFlags != 0) {
445 pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700446 pw.print(" configChangeFlags=");
447 pw.println(Integer.toHexString(configChangeFlags));
448 }
449 if (connections != null) {
450 pw.print(prefix); pw.print("connections="); pw.println(connections);
451 }
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800452 if (info != null) {
453 pw.println(prefix + "resizeMode=" + ActivityInfo.resizeModeToString(info.resizeMode));
454 }
Winson Chungb5c41b72016-12-07 15:00:47 -0800455 if (supportsPipOnMoveToBackground) {
Winson Chunga29eb982016-12-14 12:01:27 -0800456 pw.println(prefix + "supportsPipOnMoveToBackground=1");
457 pw.println(prefix + "enterPipOnMoveToBackground=" +
458 (enterPipOnMoveToBackground ? 1 : 0));
459 pictureInPictureArgs.dump(pw, prefix);
Winson Chungb5c41b72016-12-07 15:00:47 -0800460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462
Andrii Kulian21713ac2016-10-12 22:05:05 -0700463 private boolean crossesHorizontalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700464 return crossesSizeThreshold(mHorizontalSizeConfigurations, firstDp, secondDp);
465 }
466
Andrii Kulian21713ac2016-10-12 22:05:05 -0700467 private boolean crossesVerticalSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700468 return crossesSizeThreshold(mVerticalSizeConfigurations, firstDp, secondDp);
469 }
470
Andrii Kulian21713ac2016-10-12 22:05:05 -0700471 private boolean crossesSmallestSizeThreshold(int firstDp, int secondDp) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700472 return crossesSizeThreshold(mSmallestSizeConfigurations, firstDp, secondDp);
473 }
474
Filip Gruszczynski23493322015-07-29 17:02:59 -0700475 /**
476 * The purpose of this method is to decide whether the activity needs to be relaunched upon
477 * changing its size. In most cases the activities don't need to be relaunched, if the resize
478 * is small, all the activity content has to do is relayout itself within new bounds. There are
479 * cases however, where the activity's content would be completely changed in the new size and
480 * the full relaunch is required.
481 *
482 * The activity will report to us vertical and horizontal thresholds after which a relaunch is
483 * required. These thresholds are collected from the application resource qualifiers. For
484 * example, if application has layout-w600dp resource directory, then it needs a relaunch when
485 * we resize from width of 650dp to 550dp, as it crosses the 600dp threshold. However, if
486 * it resizes width from 620dp to 700dp, it won't be relaunched as it stays on the same side
487 * of the threshold.
488 */
489 private static boolean crossesSizeThreshold(int[] thresholds, int firstDp,
490 int secondDp) {
491 if (thresholds == null) {
492 return false;
493 }
494 for (int i = thresholds.length - 1; i >= 0; i--) {
495 final int threshold = thresholds[i];
496 if ((firstDp < threshold && secondDp >= threshold)
497 || (firstDp >= threshold && secondDp < threshold)) {
498 return true;
499 }
500 }
501 return false;
502 }
503
Andrii Kulian21713ac2016-10-12 22:05:05 -0700504 void setSizeConfigurations(int[] horizontalSizeConfiguration,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700505 int[] verticalSizeConfigurations, int[] smallestSizeConfigurations) {
Filip Gruszczynski23493322015-07-29 17:02:59 -0700506 mHorizontalSizeConfigurations = horizontalSizeConfiguration;
507 mVerticalSizeConfigurations = verticalSizeConfigurations;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700508 mSmallestSizeConfigurations = smallestSizeConfigurations;
Filip Gruszczynski23493322015-07-29 17:02:59 -0700509 }
510
Andrii Kulian21713ac2016-10-12 22:05:05 -0700511 private void scheduleConfigurationChanged(Configuration config, boolean reportToActivity) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800512 if (app == null || app.thread == null) {
513 return;
514 }
515 try {
516 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + this + " " +
Adam Lesinskibad43fc2016-07-19 13:35:01 -0700517 "reportToActivity=" + reportToActivity + " and config: " + config);
Chong Zhang6be533e2016-06-17 16:24:21 -0700518
Andrii Kulian1779e612016-10-12 21:58:25 -0700519 app.thread.scheduleActivityConfigurationChanged(appToken, new Configuration(config),
520 reportToActivity);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800521 } catch (RemoteException e) {
522 // If process died, whatever.
523 }
524 }
525
Andrii Kulian933076d2016-03-29 17:04:42 -0700526 void scheduleMultiWindowModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700527 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800528 return;
529 }
530 try {
531 // An activity is considered to be in multi-window mode if its task isn't fullscreen.
Andrii Kulian933076d2016-03-29 17:04:42 -0700532 app.thread.scheduleMultiWindowModeChanged(appToken, !task.mFullscreen);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800533 } catch (Exception e) {
534 // If process died, I don't care.
535 }
536 }
537
Andrii Kulian933076d2016-03-29 17:04:42 -0700538 void schedulePictureInPictureModeChanged() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700539 if (task == null || task.getStack() == null || app == null || app.thread == null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -0800540 return;
541 }
542 try {
Andrii Kulian933076d2016-03-29 17:04:42 -0700543 app.thread.schedulePictureInPictureModeChanged(
Andrii Kulian02b7a832016-10-06 23:11:56 -0700544 appToken, task.getStackId() == PINNED_STACK_ID);
Wale Ogunwale22e25262016-02-01 10:32:02 -0800545 } catch (Exception e) {
546 // If process died, no one cares.
Filip Gruszczynskica664812015-12-04 12:43:36 -0800547 }
548 }
549
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800550 boolean isFreeform() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700551 return task != null && task.getStackId() == FREEFORM_WORKSPACE_STACK_ID;
Filip Gruszczynski3d026712015-12-16 13:46:38 -0800552 }
553
Dianne Hackbornbe707852011-11-11 14:32:10 -0800554 static class Token extends IApplicationToken.Stub {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700555 private final WeakReference<ActivityRecord> weakActivity;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800556
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800557 Token(ActivityRecord activity) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800558 weakActivity = new WeakReference<>(activity);
Wale Ogunwale7d701172015-03-11 15:36:30 -0700559 }
560
Andrii Kulian21713ac2016-10-12 22:05:05 -0700561 private static ActivityRecord tokenToActivityRecordLocked(Token token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700562 if (token == null) {
563 return null;
564 }
565 ActivityRecord r = token.weakActivity.get();
Andrii Kulian02b7a832016-10-06 23:11:56 -0700566 if (r == null || r.getStack() == null) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700567 return null;
568 }
569 return r;
Dianne Hackbornbe707852011-11-11 14:32:10 -0800570 }
571
Craig Mautnerde4ef022013-04-07 19:01:33 -0700572 @Override
Dianne Hackbornbe707852011-11-11 14:32:10 -0800573 public String toString() {
574 StringBuilder sb = new StringBuilder(128);
575 sb.append("Token{");
576 sb.append(Integer.toHexString(System.identityHashCode(this)));
577 sb.append(' ');
578 sb.append(weakActivity.get());
579 sb.append('}');
580 return sb.toString();
581 }
582 }
583
Wale Ogunwale7d701172015-03-11 15:36:30 -0700584 static ActivityRecord forTokenLocked(IBinder token) {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800585 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700586 return Token.tokenToActivityRecordLocked((Token)token);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800587 } catch (ClassCastException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800588 Slog.w(TAG, "Bad activity token: " + token, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800589 return null;
590 }
591 }
592
Chong Zhang85ee6542015-10-02 13:36:38 -0700593 boolean isResolverActivity() {
594 return ResolverActivity.class.getName().equals(realActivity.getClassName());
Craig Mautnerac6f8432013-07-17 13:24:59 -0700595 }
596
Craig Mautnerd2328952013-03-05 12:46:26 -0800597 ActivityRecord(ActivityManagerService _service, ProcessRecord _caller,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800598 int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 ActivityInfo aInfo, Configuration _configuration,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700600 ActivityRecord _resultTo, String _resultWho, int _reqCode,
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700601 boolean _componentSpecified, boolean _rootVoiceInteraction,
602 ActivityStackSupervisor supervisor,
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700603 ActivityContainer container, ActivityOptions options, ActivityRecord sourceRecord) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 service = _service;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800605 appToken = new Token(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 info = aInfo;
607 launchedFromUid = _launchedFromUid;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800608 launchedFromPackage = _launchedFromPackage;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700609 userId = UserHandle.getUserId(aInfo.applicationInfo.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 intent = _intent;
611 shortComponentName = _intent.getComponent().flattenToShortString();
612 resolvedType = _resolvedType;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800613 componentSpecified = _componentSpecified;
Dianne Hackbornfb81d092015-08-03 17:14:46 -0700614 rootVoiceInteraction = _rootVoiceInteraction;
Andrii Kulian1779e612016-10-12 21:58:25 -0700615 mLastReportedConfiguration = new Configuration(_configuration);
616 mLastReportedOverrideConfiguration = new Configuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 resultTo = _resultTo;
618 resultWho = _resultWho;
619 requestCode = _reqCode;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700620 state = ActivityState.INITIALIZING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 frontOfTask = false;
622 launchFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 stopped = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700624 delayedResume = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 finishing = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -0800626 deferRelaunchUntilPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 keysPaused = false;
628 inHistory = false;
Chong Zhanga48ef662015-08-18 19:21:47 -0700629 visible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 nowVisible = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 idle = false;
632 hasBeenLaunched = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700633 mStackSupervisor = supervisor;
Craig Mautnere0a38842013-12-16 16:14:02 -0800634 mInitialActivityContainer = container;
Robert Carr0f5d7532016-10-17 16:39:17 -0700635
636 mRotationAnimationHint = aInfo.rotationAnimation;
637
Craig Mautner233ceee2014-05-09 17:05:11 -0700638 if (options != null) {
Chong Zhang280d3322015-11-03 17:27:26 -0800639 pendingOptions = options;
Craig Mautnerbb742462014-07-07 15:28:55 -0700640 mLaunchTaskBehind = pendingOptions.getLaunchTaskBehind();
Robert Carr0f5d7532016-10-17 16:39:17 -0700641
642 final int rotationAnimation = pendingOptions.getRotationAnimationHint();
643 // Only override manifest supplied option if set.
644 if (rotationAnimation >= 0) {
645 mRotationAnimationHint = rotationAnimation;
646 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -0700647 PendingIntent usageReport = pendingOptions.getUsageTimeReport();
648 if (usageReport != null) {
649 appTimeTracker = new AppTimeTracker(usageReport);
650 }
Craig Mautner233ceee2014-05-09 17:05:11 -0700651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800653 // This starts out true, since the initial state of an activity is that we have everything,
654 // and we shouldn't never consider it lacking in state to be removed if it dies.
Dianne Hackborn2d1b3782012-09-09 17:49:39 -0700655 haveState = true;
656
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800657 // If the class name in the intent doesn't match that of the target, this is
658 // probably an alias. We have to create a new ComponentName object to keep track
659 // of the real activity name, so that FLAG_ACTIVITY_CLEAR_TOP is handled properly.
660 if (aInfo.targetActivity == null
661 || (aInfo.targetActivity.equals(_intent.getComponent().getClassName())
662 && (aInfo.launchMode == LAUNCH_MULTIPLE
663 || aInfo.launchMode == LAUNCH_SINGLE_TOP))) {
664 realActivity = _intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 } else {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800666 realActivity = new ComponentName(aInfo.packageName, aInfo.targetActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800668 taskAffinity = aInfo.taskAffinity;
669 stateNotNeeded = (aInfo.flags & FLAG_STATE_NOT_NEEDED) != 0;
670 appInfo = aInfo.applicationInfo;
671 nonLocalizedLabel = aInfo.nonLocalizedLabel;
672 labelRes = aInfo.labelRes;
673 if (nonLocalizedLabel == null && labelRes == 0) {
674 ApplicationInfo app = aInfo.applicationInfo;
675 nonLocalizedLabel = app.nonLocalizedLabel;
676 labelRes = app.labelRes;
677 }
678 icon = aInfo.getIconResource();
679 logo = aInfo.getLogoResource();
680 theme = aInfo.getThemeResource();
681 realTheme = theme;
682 if (realTheme == 0) {
683 realTheme = aInfo.applicationInfo.targetSdkVersion < HONEYCOMB
684 ? android.R.style.Theme : android.R.style.Theme_Holo;
685 }
686 if ((aInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
687 windowFlags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
688 }
689 if ((aInfo.flags & FLAG_MULTIPROCESS) != 0 && _caller != null
690 && (aInfo.applicationInfo.uid == SYSTEM_UID
691 || aInfo.applicationInfo.uid == _caller.info.uid)) {
692 processName = _caller.processName;
693 } else {
694 processName = aInfo.processName;
695 }
696
697 if ((aInfo.flags & FLAG_EXCLUDE_FROM_RECENTS) != 0) {
698 intent.addFlags(FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
699 }
700
701 packageName = aInfo.applicationInfo.packageName;
702 launchMode = aInfo.launchMode;
703
704 AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
705 realTheme, com.android.internal.R.styleable.Window, userId);
706 final boolean translucent = ent != null && (ent.array.getBoolean(
707 com.android.internal.R.styleable.Window_windowIsTranslucent, false)
708 || (!ent.array.hasValue(
709 com.android.internal.R.styleable.Window_windowIsTranslucent)
710 && ent.array.getBoolean(
711 com.android.internal.R.styleable.Window_windowSwipeToDismiss,
712 false)));
713 fullscreen = ent != null && !ent.array.getBoolean(
714 com.android.internal.R.styleable.Window_windowIsFloating, false) && !translucent;
715 noDisplay = ent != null && ent.array.getBoolean(
716 com.android.internal.R.styleable.Window_windowNoDisplay, false);
717
718 setActivityType(_componentSpecified, _launchedFromUid, _intent, sourceRecord);
719
720 immersive = (aInfo.flags & FLAG_IMMERSIVE) != 0;
721
722 requestedVrComponent = (aInfo.requestedVrComponent == null) ?
723 null : ComponentName.unflattenFromString(aInfo.requestedVrComponent);
724 }
725
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800726 AppWindowContainerController getWindowContainerController() {
727 return mWindowContainerController;
728 }
729
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800730 void createWindowContainer() {
731 if (mWindowContainerController != null) {
732 throw new IllegalArgumentException("Window container=" + mWindowContainerController
733 + " already created for r=" + this);
734
735 }
736
737 inHistory = true;
738
739 task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800740 final TaskWindowContainerController taskController = task.getWindowContainerController();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800741
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800742 mWindowContainerController = new AppWindowContainerController(taskController, appToken,
743 this, Integer.MAX_VALUE /* add on top */, info.screenOrientation, fullscreen,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800744 (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, info.configChanges,
745 task.voiceSession != null, mLaunchTaskBehind, isAlwaysFocusable(),
746 appInfo.targetSdkVersion, mRotationAnimationHint,
747 ActivityManagerService.getInputDispatchingTimeoutLocked(this) * 1000000L);
748
749 task.addActivityToTop(this);
750
751 onOverrideConfigurationSent();
752 }
753
754 void removeWindowContainer() {
755 mWindowContainerController.removeContainer(getDisplayId());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800756 mWindowContainerController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700759 private boolean isHomeIntent(Intent intent) {
760 return Intent.ACTION_MAIN.equals(intent.getAction())
761 && intent.hasCategory(Intent.CATEGORY_HOME)
762 && intent.getCategories().size() == 1
763 && intent.getData() == null
764 && intent.getType() == null;
765 }
766
Chong Zhangad24f962016-08-25 12:12:33 -0700767 static boolean isMainIntent(Intent intent) {
768 return Intent.ACTION_MAIN.equals(intent.getAction())
769 && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
770 && intent.getCategories().size() == 1
771 && intent.getData() == null
772 && intent.getType() == null;
773 }
774
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700775 private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
776 if (uid == Process.myUid() || uid == 0) {
777 // System process can launch home activity.
778 return true;
779 }
780 // Resolver activity can launch home activity.
781 return sourceRecord != null && sourceRecord.isResolverActivity();
782 }
783
784 private void setActivityType(boolean componentSpecified,
785 int launchedFromUid, Intent intent, ActivityRecord sourceRecord) {
786 if ((!componentSpecified || canLaunchHomeActivity(launchedFromUid, sourceRecord))
787 && isHomeIntent(intent) && !isResolverActivity()) {
788 // This sure looks like a home activity!
789 mActivityType = HOME_ACTIVITY_TYPE;
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700790
791 if (info.resizeMode == RESIZE_MODE_FORCE_RESIZEABLE
792 || info.resizeMode == RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION) {
793 // We only allow home activities to be resizeable if they explicitly requested it.
794 info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
795 }
Wale Ogunwale1affbbc2016-05-01 09:03:52 -0700796 } else if (realActivity.getClassName().contains(RECENTS_PACKAGE_NAME)) {
797 mActivityType = RECENTS_ACTIVITY_TYPE;
798 } else {
799 mActivityType = APPLICATION_ACTIVITY_TYPE;
800 }
801 }
802
Craig Mautnera228ae92014-07-09 05:44:55 -0700803 void setTask(TaskRecord newTask, TaskRecord taskToAffiliateWith) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700804 if (task != null && task.removeActivity(this) && task != newTask
805 && task.getStack() != null) {
806 task.getStack().removeTask(task, "setTask");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700807 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700808 task = newTask;
Craig Mautnera228ae92014-07-09 05:44:55 -0700809 setTaskToAffiliateWith(taskToAffiliateWith);
810 }
811
812 void setTaskToAffiliateWith(TaskRecord taskToAffiliateWith) {
813 if (taskToAffiliateWith != null &&
814 launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE &&
815 launchMode != ActivityInfo.LAUNCH_SINGLE_TASK) {
816 task.setTaskToAffiliateWith(taskToAffiliateWith);
817 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700818 }
819
Andrii Kulian02b7a832016-10-06 23:11:56 -0700820 /**
Winson Chungb5c41b72016-12-07 15:00:47 -0800821 * If this activity has requested that it auto-enter picture-in-picture and we can actually do
822 * this, then mark it to enter picture in picture at that point.
823 */
824 void setEnterPipOnMoveToBackground(boolean enterPipOnInvisible) {
825 if (supportsPipOnMoveToBackground) {
826 enterPipOnMoveToBackground = enterPipOnInvisible;
827 }
828 }
829
830 /**
831 * @return whether to enter PiP when this activity is made invisible.
832 */
833 public boolean shouldEnterPictureInPictureOnInvisible() {
834 return enterPipOnMoveToBackground;
835 }
836
837 /**
Andrii Kulian02b7a832016-10-06 23:11:56 -0700838 * @return Stack value from current task, null if there is no task.
839 */
840 ActivityStack getStack() {
841 return task != null ? task.getStack() : null;
842 }
843
Craig Mautner5eda9b32013-07-02 11:58:16 -0700844 boolean changeWindowTranslucency(boolean toOpaque) {
845 if (fullscreen == toOpaque) {
846 return false;
847 }
Craig Mautner4addfc52013-06-25 08:05:45 -0700848
Craig Mautner5eda9b32013-07-02 11:58:16 -0700849 // Keep track of the number of fullscreen activities in this task.
850 task.numFullscreen += toOpaque ? +1 : -1;
851
852 fullscreen = toOpaque;
853 return true;
Craig Mautner4addfc52013-06-25 08:05:45 -0700854 }
855
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700856 void takeFromHistory() {
857 if (inHistory) {
858 inHistory = false;
859 if (task != null && !finishing) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800860 task = null;
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700861 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700862 clearOptionsLocked();
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700863 }
864 }
865
866 boolean isInHistory() {
867 return inHistory;
868 }
869
Wale Ogunwale7d701172015-03-11 15:36:30 -0700870 boolean isInStackLocked() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700871 final ActivityStack stack = getStack();
872 return stack != null && stack.isInStackLocked(this) != null;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700873 }
874
Craig Mautner86d67a42013-05-14 10:34:38 -0700875 boolean isHomeActivity() {
876 return mActivityType == HOME_ACTIVITY_TYPE;
877 }
878
879 boolean isRecentsActivity() {
880 return mActivityType == RECENTS_ACTIVITY_TYPE;
881 }
882
883 boolean isApplicationActivity() {
884 return mActivityType == APPLICATION_ACTIVITY_TYPE;
885 }
886
Craig Mautner21d24a22014-04-23 11:45:37 -0700887 boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700888 return (info.persistableMode == ActivityInfo.PERSIST_ROOT_ONLY ||
889 info.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS) &&
890 (intent == null ||
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800891 (intent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0);
Craig Mautner21d24a22014-04-23 11:45:37 -0700892 }
893
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800894 boolean isFocusable() {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700895 return StackId.canReceiveKeys(task.getStackId()) || isAlwaysFocusable();
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800896 }
897
898 boolean isResizeable() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700899 return ActivityInfo.isResizeableMode(info.resizeMode);
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800900 }
901
Wale Ogunwale513346d2016-01-27 10:55:01 -0800902 boolean isResizeableOrForced() {
903 return !isHomeActivity() && (isResizeable() || service.mForceResizableActivities);
904 }
905
Jorim Jaggicd13d332016-04-27 15:40:20 -0700906 boolean isNonResizableOrForced() {
Wale Ogunwaledf241e92016-10-13 15:14:21 -0700907 return info.resizeMode != RESIZE_MODE_RESIZEABLE
Wale Ogunwale72a73e32016-10-13 12:16:39 -0700908 && info.resizeMode != RESIZE_MODE_RESIZEABLE_AND_PIPABLE
909 && info.resizeMode != RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION;
Jorim Jaggicd13d332016-04-27 15:40:20 -0700910 }
911
Winson Chunge6308042016-10-31 09:24:01 -0700912 /**
913 * @return whether this activity's resize mode supports PIP.
914 */
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800915 boolean supportsPictureInPicture() {
Wale Ogunwaled26176f2016-01-25 20:04:04 -0800916 return !isHomeActivity() && info.resizeMode == RESIZE_MODE_RESIZEABLE_AND_PIPABLE;
917 }
918
Winson Chunge6308042016-10-31 09:24:01 -0700919 /**
Winson Chungb5c41b72016-12-07 15:00:47 -0800920 * @return whether this activity is currently allowed to enter PIP, if
921 * {@param checkActivityVisibility} is set, then the current activity visibility is taken into
922 * account.
Winson Chunge6308042016-10-31 09:24:01 -0700923 */
Winson Chungb5c41b72016-12-07 15:00:47 -0800924 boolean canEnterPictureInPicture(boolean checkActivityVisibility) {
925 if (!checkActivityVisibility) {
926 return supportsPictureInPicture();
927 }
928
Winson Chung7e3233a2016-12-12 14:27:21 -0800929 if (supportsPictureInPicture()) {
Winson Chunge6308042016-10-31 09:24:01 -0700930 switch (state) {
931 case RESUMED:
932 case PAUSING:
933 case PAUSED:
934 return true;
935 }
936 }
937 return false;
938 }
939
Wale Ogunwale513346d2016-01-27 10:55:01 -0800940 boolean canGoInDockedStack() {
Wale Ogunwale3eadad72016-10-13 09:16:59 -0700941 return !isHomeActivity() && isResizeableOrForced();
Wale Ogunwale513346d2016-01-27 10:55:01 -0800942 }
943
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800944 boolean isAlwaysFocusable() {
945 return (info.flags & FLAG_ALWAYS_FOCUSABLE) != 0;
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800946 }
947
Jorim Jaggife762342016-10-13 14:33:27 +0200948 /**
949 * @return true if the activity contains windows that have
950 * {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} set
951 */
952 boolean hasShowWhenLockedWindows() {
953 return service.mWindowManager.containsShowWhenLockedWindow(appToken);
954 }
955
956 /**
957 * @return true if the activity contains windows that have
958 * {@link LayoutParams#FLAG_DISMISS_KEYGUARD} set
959 */
960 boolean hasDismissKeyguardWindows() {
961 return service.mWindowManager.containsDismissKeyguardWindow(appToken);
962 }
963
Wale Ogunwale7d701172015-03-11 15:36:30 -0700964 void makeFinishingLocked() {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800965 if (!finishing) {
Andrii Kulian02b7a832016-10-06 23:11:56 -0700966 final ActivityStack stack = getStack();
967 if (stack != null && this == stack.getVisibleBehindActivity()) {
Jose Lima34ff4922014-08-18 15:19:41 -0700968 // A finishing activity should not remain as visible in the background
969 mStackSupervisor.requestVisibleBehindLocked(this, false);
970 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800971 finishing = true;
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -0700972 if (stopped) {
973 clearOptionsLocked();
974 }
Yorke Leebd54c2a2016-10-25 13:49:23 -0700975
976 if (service != null) {
977 service.mTaskChangeNotificationController.notifyTaskStackChanged();
978 }
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800979 }
980 }
981
Dianne Hackborn7e269642010-08-25 19:50:20 -0700982 UriPermissionOwner getUriPermissionsLocked() {
983 if (uriPermissions == null) {
984 uriPermissions = new UriPermissionOwner(service, this);
985 }
986 return uriPermissions;
987 }
988
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700989 void addResultLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 int requestCode, int resultCode,
991 Intent resultData) {
992 ActivityResult r = new ActivityResult(from, resultWho,
John Spurlock8a985d22014-02-25 09:40:05 -0500993 requestCode, resultCode, resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (results == null) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800995 results = new ArrayList<ResultInfo>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
997 results.add(r);
998 }
999
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001000 void removeResultsLocked(ActivityRecord from, String resultWho,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 int requestCode) {
1002 if (results != null) {
1003 for (int i=results.size()-1; i>=0; i--) {
1004 ActivityResult r = (ActivityResult)results.get(i);
1005 if (r.mFrom != from) continue;
1006 if (r.mResultWho == null) {
1007 if (resultWho != null) continue;
1008 } else {
1009 if (!r.mResultWho.equals(resultWho)) continue;
1010 }
1011 if (r.mRequestCode != requestCode) continue;
1012
1013 results.remove(i);
1014 }
1015 }
1016 }
1017
Andrii Kulian21713ac2016-10-12 22:05:05 -07001018 private void addNewIntentLocked(ReferrerIntent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 if (newIntents == null) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001020 newIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022 newIntents.add(intent);
1023 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001024
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001025 /**
1026 * Deliver a new Intent to an existing activity, so that its onNewIntent()
1027 * method will be called at the proper time.
1028 */
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001029 final void deliverNewIntentLocked(int callingUid, Intent intent, String referrer) {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001030 // The activity now gets access to the data associated with this Intent.
1031 service.grantUriPermissionFromIntentLocked(callingUid, packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001032 intent, getUriPermissionsLocked(), userId);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001033 final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
Craig Mautner86d67a42013-05-14 10:34:38 -07001034 boolean unsent = true;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001035 final ActivityStack stack = getStack();
Wale Ogunwale826c7062016-09-13 08:25:54 -07001036 final boolean isTopActivityInStack =
1037 stack != null && stack.topRunningActivityLocked() == this;
1038 final boolean isTopActivityWhileSleeping =
1039 service.isSleepingLocked() && isTopActivityInStack;
Wale Ogunwale826c7062016-09-13 08:25:54 -07001040
1041 // We want to immediately deliver the intent to the activity if:
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001042 // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
1043 // the user to see the visual effects caused by the intent delivery now.
Wale Ogunwale826c7062016-09-13 08:25:54 -07001044 // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001045 if ((state == ActivityState.RESUMED || state == ActivityState.PAUSED
1046 || isTopActivityWhileSleeping) && app != null && app.thread != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001047 try {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001048 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
1049 ar.add(rintent);
Wale Ogunwale826c7062016-09-13 08:25:54 -07001050 app.thread.scheduleNewIntent(
Wale Ogunwale03f7e9e2016-09-22 09:04:09 -07001051 ar, appToken, state == ActivityState.PAUSED /* andPause */);
Craig Mautner86d67a42013-05-14 10:34:38 -07001052 unsent = false;
Dianne Hackborn39792d22010-08-19 18:01:52 -07001053 } catch (RemoteException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001054 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn39792d22010-08-19 18:01:52 -07001055 } catch (NullPointerException e) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001056 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001057 }
1058 }
Craig Mautner86d67a42013-05-14 10:34:38 -07001059 if (unsent) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001060 addNewIntentLocked(rintent);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001061 }
1062 }
1063
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001064 void updateOptionsLocked(ActivityOptions options) {
1065 if (options != null) {
1066 if (pendingOptions != null) {
1067 pendingOptions.abort();
1068 }
1069 pendingOptions = options;
1070 }
1071 }
1072
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001073 void applyOptionsLocked() {
George Mount2c92c972014-03-20 09:38:23 -07001074 if (pendingOptions != null
1075 && pendingOptions.getAnimationType() != ActivityOptions.ANIM_SCENE_TRANSITION) {
Michael Jurka21385cd2012-05-03 10:57:31 -07001076 final int animationType = pendingOptions.getAnimationType();
1077 switch (animationType) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001078 case ActivityOptions.ANIM_CUSTOM:
1079 service.mWindowManager.overridePendingAppTransition(
1080 pendingOptions.getPackageName(),
1081 pendingOptions.getCustomEnterResId(),
Dianne Hackborn84375872012-06-01 19:03:50 -07001082 pendingOptions.getCustomExitResId(),
1083 pendingOptions.getOnAnimationStartListener());
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001084 break;
Chet Haase10e23ab2015-02-11 15:08:38 -08001085 case ActivityOptions.ANIM_CLIP_REVEAL:
1086 service.mWindowManager.overridePendingAppTransitionClipReveal(
1087 pendingOptions.getStartX(), pendingOptions.getStartY(),
1088 pendingOptions.getWidth(), pendingOptions.getHeight());
1089 if (intent.getSourceBounds() == null) {
1090 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1091 pendingOptions.getStartY(),
1092 pendingOptions.getStartX()+pendingOptions.getWidth(),
1093 pendingOptions.getStartY()+pendingOptions.getHeight()));
1094 }
1095 break;
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001096 case ActivityOptions.ANIM_SCALE_UP:
1097 service.mWindowManager.overridePendingAppTransitionScaleUp(
1098 pendingOptions.getStartX(), pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001099 pendingOptions.getWidth(), pendingOptions.getHeight());
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001100 if (intent.getSourceBounds() == null) {
1101 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1102 pendingOptions.getStartY(),
Winson Chung2e7f3bd2014-09-05 13:17:22 +02001103 pendingOptions.getStartX()+pendingOptions.getWidth(),
1104 pendingOptions.getStartY()+pendingOptions.getHeight()));
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001105 }
Dianne Hackborneabfb3a2012-04-16 16:28:22 -07001106 break;
Michael Jurka832cb222012-04-13 09:32:47 -07001107 case ActivityOptions.ANIM_THUMBNAIL_SCALE_UP:
1108 case ActivityOptions.ANIM_THUMBNAIL_SCALE_DOWN:
1109 boolean scaleUp = (animationType == ActivityOptions.ANIM_THUMBNAIL_SCALE_UP);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001110 service.mWindowManager.overridePendingAppTransitionThumb(
1111 pendingOptions.getThumbnail(),
1112 pendingOptions.getStartX(), pendingOptions.getStartY(),
Michael Jurka21385cd2012-05-03 10:57:31 -07001113 pendingOptions.getOnAnimationStartListener(),
Michael Jurka832cb222012-04-13 09:32:47 -07001114 scaleUp);
Dianne Hackbornd367ca82012-05-07 15:49:39 -07001115 if (intent.getSourceBounds() == null) {
1116 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1117 pendingOptions.getStartY(),
1118 pendingOptions.getStartX()
1119 + pendingOptions.getThumbnail().getWidth(),
1120 pendingOptions.getStartY()
1121 + pendingOptions.getThumbnail().getHeight()));
1122 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001123 break;
Winson Chunga4ccb862014-08-22 15:26:27 -07001124 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP:
1125 case ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN:
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001126 final AppTransitionAnimationSpec[] specs = pendingOptions.getAnimSpecs();
1127 if (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_DOWN
1128 && specs != null) {
1129 service.mWindowManager.overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -07001130 specs, pendingOptions.getOnAnimationStartListener(),
1131 pendingOptions.getAnimationFinishedListener(), false);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07001132 } else {
1133 service.mWindowManager.overridePendingAppTransitionAspectScaledThumb(
1134 pendingOptions.getThumbnail(),
1135 pendingOptions.getStartX(), pendingOptions.getStartY(),
1136 pendingOptions.getWidth(), pendingOptions.getHeight(),
1137 pendingOptions.getOnAnimationStartListener(),
1138 (animationType == ActivityOptions.ANIM_THUMBNAIL_ASPECT_SCALE_UP));
1139 if (intent.getSourceBounds() == null) {
1140 intent.setSourceBounds(new Rect(pendingOptions.getStartX(),
1141 pendingOptions.getStartY(),
1142 pendingOptions.getStartX() + pendingOptions.getWidth(),
1143 pendingOptions.getStartY() + pendingOptions.getHeight()));
1144 }
Winson Chunga4ccb862014-08-22 15:26:27 -07001145 }
1146 break;
Craig Mautner233ceee2014-05-09 17:05:11 -07001147 default:
1148 Slog.e(TAG, "applyOptionsLocked: Unknown animationType=" + animationType);
1149 break;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001150 }
1151 pendingOptions = null;
1152 }
1153 }
1154
Adam Powellcfbe9be2013-11-06 14:58:58 -08001155 ActivityOptions getOptionsForTargetActivityLocked() {
1156 return pendingOptions != null ? pendingOptions.forTargetActivity() : null;
1157 }
1158
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001159 void clearOptionsLocked() {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07001160 if (pendingOptions != null) {
1161 pendingOptions.abort();
1162 pendingOptions = null;
1163 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001164 }
1165
Dianne Hackborn9622ca42012-10-23 18:56:33 -07001166 ActivityOptions takeOptionsLocked() {
1167 ActivityOptions opts = pendingOptions;
1168 pendingOptions = null;
1169 return opts;
1170 }
1171
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001172 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -07001173 if (uriPermissions != null) {
1174 uriPermissions.removeUriPermissionsLocked();
1175 uriPermissions = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001176 }
1177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178
1179 void pauseKeyDispatchingLocked() {
1180 if (!keysPaused) {
1181 keysPaused = true;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001182 mWindowContainerController.pauseKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
1184 }
1185
1186 void resumeKeyDispatchingLocked() {
1187 if (keysPaused) {
1188 keysPaused = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001189 mWindowContainerController.resumeKeyDispatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191 }
1192
Winson Chung740c3ac2014-11-12 16:14:38 -08001193 void updateThumbnailLocked(Bitmap newThumbnail, CharSequence description) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001194 if (newThumbnail != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001195 if (DEBUG_THUMBNAILS) Slog.i(TAG_THUMBNAILS,
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001196 "Setting thumbnail of " + this + " to " + newThumbnail);
Winsonc809cbb2015-11-02 12:06:15 -08001197 boolean thumbnailUpdated = task.setLastThumbnailLocked(newThumbnail);
Winson Chung096f36b2014-08-20 15:39:01 -07001198 if (thumbnailUpdated && isPersistable()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001199 service.notifyTaskPersisterLocked(task, false);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001200 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001201 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001202 task.lastDescription = description;
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001203 }
1204
Andrii Kulian21713ac2016-10-12 22:05:05 -07001205 final Bitmap screenshotActivityLocked() {
1206 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivityLocked: " + this);
1207 if (noDisplay) {
1208 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
1209 return null;
1210 }
1211
1212 final ActivityStack stack = getStack();
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001213 if (stack.isHomeOrRecentsStack()) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001214 // This is an optimization -- since we never show Home or Recents within Recents itself,
1215 // we can just go ahead and skip taking the screenshot if this is the home stack.
Matthew Ngae1ff4f2016-11-10 15:49:14 -08001216 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, stack.getStackId() == HOME_STACK_ID ?
1217 "\tHome stack" : "\tRecents stack");
Andrii Kulian21713ac2016-10-12 22:05:05 -07001218 return null;
1219 }
1220
1221 int w = service.mThumbnailWidth;
1222 int h = service.mThumbnailHeight;
1223
1224 if (w <= 0) {
1225 Slog.e(TAG, "\tInvalid thumbnail dimensions: " + w + "x" + h);
1226 return null;
1227 }
1228
1229 if (stack.mStackId == DOCKED_STACK_ID && mStackSupervisor.mIsDockMinimized) {
1230 // When the docked stack is minimized its app windows are cropped significantly so any
1231 // screenshot taken will not display the apps contain. So, we avoid taking a screenshot
1232 // in that case.
1233 if (DEBUG_SCREENSHOTS) Slog.e(TAG, "\tIn minimized docked stack");
1234 return null;
1235 }
1236
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001237 float scale = 0;
Andrii Kulian21713ac2016-10-12 22:05:05 -07001238 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
1239
1240 // When this flag is set, we currently take the fullscreen screenshot of the activity but
1241 // scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to use within
1242 // SystemUI while keeping memory usage low.
1243 if (TAKE_FULLSCREEN_SCREENSHOTS) {
1244 w = h = -1;
1245 scale = service.mFullscreenThumbnailScale;
1246 }
1247
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001248 return mWindowContainerController.screenshotApplications(getDisplayId(), w, h, scale);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001249 }
1250
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001251 void setVisibility(boolean visible) {
1252 mWindowContainerController.setVisibility(visible);
1253 }
1254
1255 // TODO: Look into merging with #setVisibility()
Andrii Kulian21713ac2016-10-12 22:05:05 -07001256 void setVisible(boolean newVisible) {
1257 visible = newVisible;
1258 if (!visible && mUpdateTaskThumbnailWhenHidden) {
1259 updateThumbnailLocked(screenshotActivityLocked(), null /* description */);
1260 mUpdateTaskThumbnailWhenHidden = false;
1261 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001262 mWindowContainerController.setVisibility(visible);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001263 final ArrayList<ActivityContainer> containers = mChildContainers;
1264 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
1265 final ActivityContainer container = containers.get(containerNdx);
1266 container.setVisible(visible);
1267 }
1268 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1269 }
1270
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001271 void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
1272 mWindowContainerController.notifyAppResumed(wasStopped, allowSavedSurface);
1273 }
1274
1275 void notifyUnknownVisibilityLaunched() {
1276 mWindowContainerController.notifyUnknownVisibilityLaunched();
1277 }
1278
Jorim Jaggi241ae102016-11-02 21:57:33 -07001279 /**
1280 * @return true if the input activity should be made visible, ignoring any effect Keyguard
1281 * might have on the visibility
1282 *
1283 * @see {@link ActivityStack#checkKeyguardVisibility}
1284 */
1285 boolean shouldBeVisibleIgnoringKeyguard(boolean behindTranslucentActivity,
1286 boolean stackVisibleBehind, ActivityRecord visibleBehind,
1287 boolean behindFullscreenActivity) {
Andrii Kulian21713ac2016-10-12 22:05:05 -07001288 if (!okToShowLocked()) {
1289 return false;
1290 }
1291
1292 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1293 // but must be drawn initially for the animation as though they were visible.
1294 final boolean activityVisibleBehind =
1295 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == this;
1296
1297 boolean isVisible =
1298 !behindFullscreenActivity || mLaunchTaskBehind || activityVisibleBehind;
1299
1300 if (service.mSupportsLeanbackOnly && isVisible && isRecentsActivity()) {
1301 // On devices that support leanback only (Android TV), Recents activity can only be
1302 // visible if the home stack is the focused stack or we are in split-screen mode.
1303 isVisible = mStackSupervisor.getStack(DOCKED_STACK_ID) != null
1304 || mStackSupervisor.isFocusedStack(getStack());
1305 }
1306
1307 return isVisible;
1308 }
1309
1310 void makeVisibleIfNeeded(ActivityRecord starting) {
1311 // This activity is not currently visible, but is running. Tell it to become visible.
1312 if (state == ActivityState.RESUMED || this == starting) {
1313 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1314 "Not making visible, r=" + this + " state=" + state + " starting=" + starting);
1315 return;
1316 }
1317
1318 // If this activity is paused, tell it to now show its window.
1319 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1320 "Making visible and scheduling visibility: " + this);
1321 final ActivityStack stack = getStack();
1322 try {
1323 if (stack.mTranslucentActivityWaiting != null) {
1324 updateOptionsLocked(returningOptions);
1325 stack.mUndrawnActivitiesBelowTopTranslucent.add(this);
1326 }
1327 setVisible(true);
1328 sleeping = false;
1329 app.pendingUiClean = true;
1330 app.thread.scheduleWindowVisibility(appToken, true /* showWindow */);
1331 // The activity may be waiting for stop, but that is no longer appropriate for it.
1332 mStackSupervisor.mStoppingActivities.remove(this);
1333 mStackSupervisor.mGoingToSleepActivities.remove(this);
1334 } catch (Exception e) {
1335 // Just skip on any failure; we'll make it visible when it next restarts.
1336 Slog.w(TAG, "Exception thrown making visibile: " + intent.getComponent(), e);
1337 }
1338 handleAlreadyVisible();
1339 }
1340
1341 boolean handleAlreadyVisible() {
1342 stopFreezingScreenLocked(false);
1343 try {
1344 if (returningOptions != null) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001345 app.thread.scheduleOnNewActivityOptions(appToken, returningOptions.toBundle());
Andrii Kulian21713ac2016-10-12 22:05:05 -07001346 }
1347 } catch(RemoteException e) {
1348 }
1349 return state == ActivityState.RESUMED;
1350 }
1351
1352 static void activityResumedLocked(IBinder token) {
1353 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1354 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1355 if (r != null) {
1356 r.icicle = null;
1357 r.haveState = false;
1358 }
1359 }
1360
1361 /**
1362 * Once we know that we have asked an application to put an activity in the resumed state
1363 * (either by launching it or explicitly telling it), this function updates the rest of our
1364 * state to match that fact.
1365 */
1366 void completeResumeLocked() {
1367 final boolean wasVisible = visible;
1368 visible = true;
1369 if (!wasVisible) {
1370 // Visibility has changed, so take a note of it so we call the TaskStackChangedListener
1371 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
1372 }
1373 idle = false;
1374 results = null;
1375 newIntents = null;
1376 stopped = false;
1377
1378 if (isHomeActivity()) {
1379 ProcessRecord app = task.mActivities.get(0).app;
1380 if (app != null && app != service.mHomeProcess) {
1381 service.mHomeProcess = app;
1382 }
1383 }
1384
1385 if (nowVisible) {
1386 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
1387 mStackSupervisor.reportActivityVisibleLocked(this);
Andrii Kulian21713ac2016-10-12 22:05:05 -07001388 }
1389
1390 // Schedule an idle timeout in case the app doesn't do it for us.
1391 mStackSupervisor.scheduleIdleTimeoutLocked(this);
1392
1393 mStackSupervisor.reportResumedActivityLocked(this);
1394
1395 resumeKeyDispatchingLocked();
1396 final ActivityStack stack = getStack();
1397 stack.mNoAnimActivities.clear();
1398
1399 // Mark the point when the activity is resuming
1400 // TODO: To be more accurate, the mark should be before the onCreate,
1401 // not after the onResume. But for subsequent starts, onResume is fine.
1402 if (app != null) {
1403 cpuTimeAtResume = service.mProcessCpuTracker.getCpuTimeForPid(app.pid);
1404 } else {
1405 cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1406 }
1407
1408 returningOptions = null;
1409
1410 if (stack.getVisibleBehindActivity() == this) {
1411 // When resuming an activity, require it to call requestVisibleBehind() again.
1412 stack.setVisibleBehindActivity(null /* ActivityRecord */);
1413 }
1414 mStackSupervisor.checkReadyForSleepLocked();
1415 }
1416
1417 final void activityStoppedLocked(Bundle newIcicle, PersistableBundle newPersistentState,
1418 CharSequence description) {
1419 final ActivityStack stack = getStack();
1420 if (state != ActivityState.STOPPING) {
1421 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + this);
1422 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1423 return;
1424 }
1425 if (newPersistentState != null) {
1426 persistentState = newPersistentState;
1427 service.notifyTaskPersisterLocked(task, false);
1428 }
1429 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + this + ": " + icicle);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001430
Andrii Kulian21713ac2016-10-12 22:05:05 -07001431 if (newIcicle != null) {
1432 // If icicle is null, this is happening due to a timeout, so we haven't really saved
1433 // the state.
1434 icicle = newIcicle;
1435 haveState = true;
1436 launchCount = 0;
1437 updateThumbnailLocked(null /* newThumbnail */, description);
1438 }
1439 if (!stopped) {
1440 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + this + " (stop complete)");
1441 stack.mHandler.removeMessages(ActivityStack.STOP_TIMEOUT_MSG, this);
1442 stopped = true;
1443 state = ActivityState.STOPPED;
1444
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001445 mWindowContainerController.notifyAppStopped();
Andrii Kulian21713ac2016-10-12 22:05:05 -07001446
1447 if (stack.getVisibleBehindActivity() == this) {
1448 mStackSupervisor.requestVisibleBehindLocked(this, false /* visible */);
1449 }
1450 if (finishing) {
1451 clearOptionsLocked();
1452 } else {
1453 if (deferRelaunchUntilPaused) {
1454 stack.destroyActivityLocked(this, true /* removeFromApp */, "stop-config");
1455 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1456 } else {
1457 mStackSupervisor.updatePreviousProcessLocked(this);
1458 }
1459 }
1460 }
1461 }
1462
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001463 void startLaunchTickingLocked() {
1464 if (ActivityManagerService.IS_USER_BUILD) {
1465 return;
1466 }
1467 if (launchTickTime == 0) {
1468 launchTickTime = SystemClock.uptimeMillis();
1469 continueLaunchTickingLocked();
1470 }
1471 }
1472
1473 boolean continueLaunchTickingLocked() {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001474 if (launchTickTime == 0) {
1475 return false;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001476 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001477
Andrii Kulian02b7a832016-10-06 23:11:56 -07001478 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001479 if (stack == null) {
1480 return false;
1481 }
1482
1483 Message msg = stack.mHandler.obtainMessage(ActivityStack.LAUNCH_TICK_MSG, this);
1484 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1485 stack.mHandler.sendMessageDelayed(msg, ActivityStack.LAUNCH_TICK);
1486 return true;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001487 }
1488
1489 void finishLaunchTickingLocked() {
1490 launchTickTime = 0;
Andrii Kulian02b7a832016-10-06 23:11:56 -07001491 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001492 if (stack != null) {
1493 stack.mHandler.removeMessages(ActivityStack.LAUNCH_TICK_MSG);
1494 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001495 }
1496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 // IApplicationToken
1498
1499 public boolean mayFreezeScreenLocked(ProcessRecord app) {
1500 // Only freeze the screen if this activity is currently attached to
1501 // an application, and that application is not blocked or unresponding.
1502 // In any other case, we can't count on getting the screen unfrozen,
1503 // so it is best to leave as-is.
Dianne Hackborn5f4d6432010-12-21 20:40:11 -08001504 return app != null && !app.crashing && !app.notResponding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 public void startFreezingScreenLocked(ProcessRecord app, int configChanges) {
1508 if (mayFreezeScreenLocked(app)) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001509 mWindowContainerController.startFreezingScreen(configChanges);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
1511 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 public void stopFreezingScreenLocked(boolean force) {
1514 if (force || frozenBeforeDestroy) {
1515 frozenBeforeDestroy = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001516 mWindowContainerController.stopFreezingScreen(force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
1518 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001519
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001520 public void reportFullyDrawnLocked() {
1521 final long curTime = SystemClock.uptimeMillis();
1522 if (displayStartTime != 0) {
1523 reportLaunchTimeLocked(curTime);
1524 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001525 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001526 if (fullyDrawnStartTime != 0 && stack != null) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001527 final long thisTime = curTime - fullyDrawnStartTime;
1528 final long totalTime = stack.mFullyDrawnStartTime != 0
1529 ? (curTime - stack.mFullyDrawnStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001530 if (SHOW_ACTIVITY_START_TIME) {
Dianne Hackborncee04b52013-07-03 17:01:28 -07001531 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001532 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_FULLY_DRAWN_TIME,
1533 userId, System.identityHashCode(this), shortComponentName,
1534 thisTime, totalTime);
1535 StringBuilder sb = service.mStringBuilder;
1536 sb.setLength(0);
1537 sb.append("Fully drawn ");
1538 sb.append(shortComponentName);
1539 sb.append(": ");
1540 TimeUtils.formatDuration(thisTime, sb);
1541 if (thisTime != totalTime) {
1542 sb.append(" (total ");
1543 TimeUtils.formatDuration(totalTime, sb);
1544 sb.append(")");
1545 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001546 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001547 }
1548 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001549 //service.mUsageStatsService.noteFullyDrawnTime(realActivity, (int) totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001550 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001551 stack.mFullyDrawnStartTime = 0;
1552 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07001553 fullyDrawnStartTime = 0;
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001554 }
1555
1556 private void reportLaunchTimeLocked(final long curTime) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001557 final ActivityStack stack = getStack();
Wale Ogunwale7d701172015-03-11 15:36:30 -07001558 if (stack == null) {
1559 return;
1560 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001561 final long thisTime = curTime - displayStartTime;
1562 final long totalTime = stack.mLaunchStartTime != 0
1563 ? (curTime - stack.mLaunchStartTime) : thisTime;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001564 if (SHOW_ACTIVITY_START_TIME) {
Narayan Kamath7829c812015-06-08 17:39:43 +01001565 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001566 EventLog.writeEvent(EventLogTags.AM_ACTIVITY_LAUNCH_TIME,
1567 userId, System.identityHashCode(this), shortComponentName,
1568 thisTime, totalTime);
1569 StringBuilder sb = service.mStringBuilder;
1570 sb.setLength(0);
1571 sb.append("Displayed ");
1572 sb.append(shortComponentName);
1573 sb.append(": ");
1574 TimeUtils.formatDuration(thisTime, sb);
1575 if (thisTime != totalTime) {
1576 sb.append(" (total ");
1577 TimeUtils.formatDuration(totalTime, sb);
1578 sb.append(")");
1579 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001580 Log.i(TAG, sb.toString());
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001581 }
1582 mStackSupervisor.reportActivityLaunchedLocked(false, this, thisTime, totalTime);
1583 if (totalTime > 0) {
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001584 //service.mUsageStatsService.noteLaunchTime(realActivity, (int)totalTime);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001585 }
1586 displayStartTime = 0;
1587 stack.mLaunchStartTime = 0;
1588 }
1589
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001590 @Override
1591 public void onWindowsDrawn() {
1592 synchronized (service) {
1593 mStackSupervisor.mActivityMetricsLogger.notifyWindowsDrawn();
1594 if (displayStartTime != 0) {
1595 reportLaunchTimeLocked(SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001597 mStackSupervisor.sendWaitingVisibleReportLocked(this);
1598 startTime = 0;
1599 finishLaunchTickingLocked();
1600 if (task != null) {
1601 task.hasBeenVisible = true;
1602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
1604 }
1605
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001606 @Override
1607 public void onWindowsVisible() {
1608 synchronized (service) {
1609 mStackSupervisor.reportActivityVisibleLocked(this);
1610 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsVisibleLocked(): " + this);
1611 if (!nowVisible) {
1612 nowVisible = true;
1613 lastVisibleTime = SystemClock.uptimeMillis();
1614 if (!idle) {
1615 // Instead of doing the full stop routine here, let's just hide any activities
1616 // we now can, and let them stop when the normal idle happens.
1617 mStackSupervisor.processStoppingActivitiesLocked(false);
1618 } else {
1619 // If this activity was already idle, then we now need to make sure we perform
1620 // the full stop of any activities that are waiting to do so. This is because
1621 // we won't do that while they are still waiting for this one to become visible.
1622 final int size = mStackSupervisor.mWaitingVisibleActivities.size();
1623 if (size > 0) {
1624 for (int i = 0; i < size; i++) {
1625 ActivityRecord r = mStackSupervisor.mWaitingVisibleActivities.get(i);
1626 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "Was waiting for visible: " + r);
1627 }
1628 mStackSupervisor.mWaitingVisibleActivities.clear();
1629 mStackSupervisor.scheduleIdleLocked();
1630 }
1631 }
1632 service.scheduleAppGcsLocked();
1633 }
1634 }
1635 }
1636
1637 @Override
1638 public void onWindowsGone() {
1639 synchronized (service) {
1640 if (DEBUG_SWITCH) Log.v(TAG_SWITCH, "windowsGone(): " + this);
1641 nowVisible = false;
1642 }
1643 }
1644
1645 @Override
1646 public boolean keyDispatchingTimedOut(String reason) {
1647 ActivityRecord anrActivity;
1648 ProcessRecord anrApp;
1649 synchronized (service) {
1650 anrActivity = getWaitingHistoryRecordLocked();
1651 anrApp = app;
1652 }
1653 return service.inputDispatchingTimedOut(anrApp, anrActivity, this, false, reason);
1654 }
1655
1656 private ActivityRecord getWaitingHistoryRecordLocked() {
riddle_hsudb46d6b2015-04-01 18:58:07 +08001657 // First find the real culprit... if this activity is waiting for
1658 // another activity to start or has stopped, then the key dispatching
1659 // timeout should not be caused by this.
Wale Ogunwale16a62792015-04-01 11:50:31 -07001660 if (mStackSupervisor.mWaitingVisibleActivities.contains(this) || stopped) {
Craig Mautner8f2adcb2014-04-07 22:21:33 +00001661 final ActivityStack stack = mStackSupervisor.getFocusedStack();
riddle_hsudb46d6b2015-04-01 18:58:07 +08001662 // Try to use the one which is closest to top.
1663 ActivityRecord r = stack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 if (r == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001665 r = stack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001667 if (r != null) {
1668 return r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
1670 }
riddle_hsudb46d6b2015-04-01 18:58:07 +08001671 return this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673
Chong Zhang87761972016-08-22 13:53:24 -07001674 /** Checks whether the activity should be shown for current user. */
1675 public boolean okToShowLocked() {
1676 return (info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0
1677 || (mStackSupervisor.isCurrentProfileLocked(userId)
1678 && !service.mUserController.isUserStoppingOrShuttingDownLocked(userId));
1679 }
1680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 /**
1682 * This method will return true if the activity is either visible, is becoming visible, is
1683 * currently pausing, or is resumed.
1684 */
1685 public boolean isInterestingToUserLocked() {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001686 return visible || nowVisible || state == ActivityState.PAUSING ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 state == ActivityState.RESUMED;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001688 }
1689
Wale Ogunwale3e997362016-09-06 10:37:56 -07001690 void setSleeping(boolean _sleeping) {
1691 setSleeping(_sleeping, false);
1692 }
1693
1694 void setSleeping(boolean _sleeping, boolean force) {
1695 if (!force && sleeping == _sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001696 return;
1697 }
1698 if (app != null && app.thread != null) {
1699 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08001700 app.thread.scheduleSleeping(appToken, _sleeping);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001701 if (_sleeping && !mStackSupervisor.mGoingToSleepActivities.contains(this)) {
1702 mStackSupervisor.mGoingToSleepActivities.add(this);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001703 }
1704 sleeping = _sleeping;
1705 } catch (RemoteException e) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001706 Slog.w(TAG, "Exception thrown when sleeping: " + intent.getComponent(), e);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001707 }
1708 }
1709 }
Craig Mautnerf81b90872013-02-26 13:02:43 -08001710
Craig Mautnerd2328952013-03-05 12:46:26 -08001711 static int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001712 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08001713 if (r == null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001714 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001715 }
1716 final TaskRecord task = r.task;
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001717 final int activityNdx = task.mActivities.indexOf(r);
1718 if (activityNdx < 0 || (onlyRoot && activityNdx > task.findEffectiveRootIndex())) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08001719 return INVALID_TASK_ID;
Craig Mautnerd2328952013-03-05 12:46:26 -08001720 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07001721 return task.taskId;
Craig Mautnerd2328952013-03-05 12:46:26 -08001722 }
1723
1724 static ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07001725 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Andrii Kulian02b7a832016-10-06 23:11:56 -07001726 return (r != null) ? r.getStack().isInStackLocked(r) : null;
Craig Mautnerd2328952013-03-05 12:46:26 -08001727 }
1728
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001729 static ActivityStack getStackLocked(IBinder token) {
Craig Mautnerd2328952013-03-05 12:46:26 -08001730 final ActivityRecord r = ActivityRecord.isInStackLocked(token);
1731 if (r != null) {
Andrii Kulian02b7a832016-10-06 23:11:56 -07001732 return r.getStack();
Craig Mautnerd2328952013-03-05 12:46:26 -08001733 }
1734 return null;
1735 }
1736
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001737 /**
1738 * @return display id to which this record is attached, -1 if not attached.
1739 */
1740 int getDisplayId() {
1741 final ActivityStack stack = getStack();
1742 if (stack == null) {
1743 return -1;
1744 }
1745 return stack.mDisplayId;
1746 }
1747
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001748 final boolean isDestroyable() {
1749 if (finishing || app == null || state == ActivityState.DESTROYING
1750 || state == ActivityState.DESTROYED) {
1751 // This would be redundant.
1752 return false;
1753 }
Andrii Kulian02b7a832016-10-06 23:11:56 -07001754 final ActivityStack stack = getStack();
1755 if (stack == null || this == stack.mResumedActivity || this == stack.mPausingActivity
1756 || !haveState || !stopped) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001757 // We're not ready for this kind of thing.
1758 return false;
1759 }
1760 if (visible) {
1761 // The user would notice this!
1762 return false;
1763 }
1764 return true;
1765 }
1766
Winson Chung3bad5cc02014-08-19 17:44:32 -07001767 private static String createImageFilename(long createTime, int taskId) {
1768 return String.valueOf(taskId) + ACTIVITY_ICON_SUFFIX + createTime +
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001769 TaskPersister.IMAGE_EXTENSION;
1770 }
1771
Craig Mautner648f69b2014-09-18 14:16:26 -07001772 void setTaskDescription(TaskDescription _taskDescription) {
1773 Bitmap icon;
1774 if (_taskDescription.getIconFilename() == null &&
1775 (icon = _taskDescription.getIcon()) != null) {
1776 final String iconFilename = createImageFilename(createTime, task.taskId);
Suprabh Shukla23593142015-11-03 17:31:15 -08001777 final File iconFile = new File(TaskPersister.getUserImagesDir(userId), iconFilename);
1778 final String iconFilePath = iconFile.getAbsolutePath();
Suprabh Shukla09a88f52015-12-02 14:36:31 -08001779 service.mRecentTasks.saveImage(icon, iconFilePath);
Suprabh Shukla23593142015-11-03 17:31:15 -08001780 _taskDescription.setIconFilename(iconFilePath);
Craig Mautner648f69b2014-09-18 14:16:26 -07001781 }
1782 taskDescription = _taskDescription;
1783 }
1784
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001785 void setVoiceSessionLocked(IVoiceInteractionSession session) {
1786 voiceSession = session;
1787 pendingVoiceInteractionStart = false;
1788 }
1789
1790 void clearVoiceSessionLocked() {
1791 voiceSession = null;
1792 pendingVoiceInteractionStart = false;
1793 }
1794
Wale Ogunwale3b232392016-05-13 15:37:13 -07001795 void showStartingWindow(ActivityRecord prev, boolean createIfNeeded) {
1796 final CompatibilityInfo compatInfo =
1797 service.compatibilityInfoForPackageLocked(info.applicationInfo);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001798 final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
1799 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
1800 prev != null ? prev.appToken : null, createIfNeeded);
Wale Ogunwale3b232392016-05-13 15:37:13 -07001801 if (shown) {
1802 mStartingWindowState = STARTING_WINDOW_SHOWN;
1803 }
1804 }
1805
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001806 void removeOrphanedStartingWindow(boolean behindFullscreenActivity) {
1807 if (state == ActivityState.INITIALIZING
1808 && mStartingWindowState == STARTING_WINDOW_SHOWN
1809 && behindFullscreenActivity) {
1810 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY, "Found orphaned starting window " + this);
1811 mStartingWindowState = STARTING_WINDOW_REMOVED;
1812 mWindowContainerController.removeStartingWindow();
1813 }
1814 }
1815
1816 int getRequestedOrientation() {
1817 return mWindowContainerController.getOrientation();
1818 }
1819
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001820 void setRequestedOrientation(int requestedOrientation) {
1821 if (task != null && (!task.mFullscreen || !task.getStack().mFullscreen)) {
1822 // Fixed screen orientation isn't supported when activities aren't in full screen mode.
1823 return;
1824 }
1825
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001826 final int displayId = getDisplayId();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001827 final Configuration displayConfig =
1828 mStackSupervisor.getDisplayOverrideConfiguration(displayId);
1829
1830 final Configuration config = mWindowContainerController.setOrientation(requestedOrientation,
1831 displayId, displayConfig, mayFreezeScreenLocked(app));
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001832 if (config != null) {
1833 frozenBeforeDestroy = true;
1834 if (!service.updateDisplayOverrideConfigurationLocked(config, this,
1835 false /* deferResume */, displayId)) {
1836 mStackSupervisor.resumeFocusedStackTopActivityLocked();
1837 }
1838 }
Yorke Leebd54c2a2016-10-25 13:49:23 -07001839 service.mTaskChangeNotificationController.notifyActivityRequestedOrientationChanged(
1840 task.taskId, requestedOrientation);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001841 }
1842
Andrii Kulian21713ac2016-10-12 22:05:05 -07001843 // TODO: now used only in one place to address race-condition. Remove when that will be fixed.
1844 void setLastReportedConfiguration(@NonNull Configuration config) {
1845 mLastReportedConfiguration.setTo(config);
1846 }
1847
1848 /** Call when override config was sent to the Window Manager to update internal records. */
1849 void onOverrideConfigurationSent() {
1850 mLastReportedOverrideConfiguration.setTo(task.getMergedOverrideConfiguration());
1851 }
1852
1853 /**
1854 * Make sure the given activity matches the current configuration. Returns false if the activity
1855 * had to be destroyed. Returns true if the configuration is the same, or the activity will
1856 * remain running as-is for whatever reason. Ensures the HistoryRecord is updated with the
1857 * correct configuration and all other bookkeeping is handled.
1858 */
1859 boolean ensureActivityConfigurationLocked(int globalChanges, boolean preserveWindow) {
1860 final ActivityStack stack = getStack();
1861 if (stack.mConfigWillChange) {
1862 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1863 "Skipping config check (will change): " + this);
1864 return true;
1865 }
1866
1867 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1868 "Ensuring correct configuration: " + this);
1869
1870 // Short circuit: if the two configurations are equal (the common case), then there is
1871 // nothing to do.
1872 final Configuration newGlobalConfig = service.getGlobalConfiguration();
1873 final Configuration newTaskMergedOverrideConfig = task.getMergedOverrideConfiguration();
1874 if (mLastReportedConfiguration.equals(newGlobalConfig)
1875 && mLastReportedOverrideConfiguration.equals(newTaskMergedOverrideConfig)
1876 && !forceNewConfig) {
1877 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1878 "Configuration unchanged in " + this);
1879 return true;
1880 }
1881
1882 // We don't worry about activities that are finishing.
1883 if (finishing) {
1884 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1885 "Configuration doesn't matter in finishing " + this);
1886 stopFreezingScreenLocked(false);
1887 return true;
1888 }
1889
1890 // Okay we now are going to make this activity have the new config.
1891 // But then we need to figure out how it needs to deal with that.
1892 mTmpGlobalConfig.setTo(mLastReportedConfiguration);
1893 mTmpTaskConfig.setTo(mLastReportedOverrideConfiguration);
1894 mLastReportedConfiguration.setTo(newGlobalConfig);
1895 mLastReportedOverrideConfiguration.setTo(newTaskMergedOverrideConfig);
1896
1897 int taskChanges = getTaskConfigurationChanges(this, newTaskMergedOverrideConfig,
1898 mTmpTaskConfig);
1899 final int changes = mTmpGlobalConfig.diff(newGlobalConfig) | taskChanges;
1900 if (changes == 0 && !forceNewConfig) {
1901 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1902 "Configuration no differences in " + this);
1903 // There are no significant differences, so we won't relaunch but should still deliver
1904 // the new configuration to the client process.
1905 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
1906 return true;
1907 }
1908
1909 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1910 "Configuration changes for " + this + " ; taskChanges="
1911 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
1912 + Configuration.configurationDiffToString(changes));
1913
1914 // If the activity isn't currently running, just leave the new configuration and it will
1915 // pick that up next time it starts.
1916 if (app == null || app.thread == null) {
1917 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1918 "Configuration doesn't matter not running " + this);
1919 stopFreezingScreenLocked(false);
1920 forceNewConfig = false;
1921 return true;
1922 }
1923
1924 // Figure out how to handle the changes between the configurations.
1925 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1926 "Checking to restart " + info.name + ": changed=0x"
1927 + Integer.toHexString(changes) + ", handles=0x"
1928 + Integer.toHexString(info.getRealConfigChanged())
1929 + ", newGlobalConfig=" + newGlobalConfig
1930 + ", newTaskMergedOverrideConfig=" + newTaskMergedOverrideConfig);
1931
1932 if ((changes&(~info.getRealConfigChanged())) != 0 || forceNewConfig) {
1933 // Aha, the activity isn't handling the change, so DIE DIE DIE.
1934 configChangeFlags |= changes;
1935 startFreezingScreenLocked(app, globalChanges);
1936 forceNewConfig = false;
1937 preserveWindow &= isResizeOnlyChange(changes);
1938 if (app == null || app.thread == null) {
1939 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1940 "Config is destroying non-running " + this);
1941 stack.destroyActivityLocked(this, true, "config");
1942 } else if (state == ActivityState.PAUSING) {
1943 // A little annoying: we are waiting for this activity to finish pausing. Let's not
1944 // do anything now, but just flag that it needs to be restarted when done pausing.
1945 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1946 "Config is skipping already pausing " + this);
1947 deferRelaunchUntilPaused = true;
1948 preserveWindowOnDeferredRelaunch = preserveWindow;
1949 return true;
1950 } else if (state == ActivityState.RESUMED) {
1951 // Try to optimize this case: the configuration is changing and we need to restart
1952 // the top, resumed activity. Instead of doing the normal handshaking, just say
1953 // "restart!".
1954 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1955 "Config is relaunching resumed " + this);
1956
1957 if (DEBUG_STATES && !visible) {
1958 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + this
1959 + " called by " + Debug.getCallers(4));
1960 }
1961
1962 relaunchActivityLocked(true /* andResume */, preserveWindow);
1963 } else {
1964 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
1965 "Config is relaunching non-resumed " + this);
1966 relaunchActivityLocked(false /* andResume */, preserveWindow);
1967 }
1968
1969 // All done... tell the caller we weren't able to keep this activity around.
1970 return false;
1971 }
1972
1973 // Default case: the activity can handle this new configuration, so hand it over.
1974 // NOTE: We only forward the task override configuration as the system level configuration
1975 // changes is always sent to all processes when they happen so it can just use whatever
1976 // system level configuration it last got.
1977 scheduleConfigurationChanged(newTaskMergedOverrideConfig, true);
1978 stopFreezingScreenLocked(false);
1979
1980 return true;
1981 }
1982
1983 private static int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
1984 Configuration oldTaskOverride) {
1985 // If we went from full-screen to non-full-screen, make sure to use the correct
1986 // configuration task diff, so the diff stays as small as possible.
1987 if (Configuration.EMPTY.equals(oldTaskOverride)
1988 && !Configuration.EMPTY.equals(taskConfig)) {
1989 oldTaskOverride = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
1990 }
1991
1992 // Conversely, do the same when going the other direction.
1993 if (Configuration.EMPTY.equals(taskConfig)
1994 && !Configuration.EMPTY.equals(oldTaskOverride)) {
1995 taskConfig = record.task.extractOverrideConfig(record.mLastReportedConfiguration);
1996 }
1997
1998 // Determine what has changed. May be nothing, if this is a config that has come back from
1999 // the app after going idle. In that case we just want to leave the official config object
2000 // now in the activity and do nothing else.
2001 int taskChanges = oldTaskOverride.diff(taskConfig, true /* skipUndefined */);
2002 // We don't want to use size changes if they don't cross boundaries that are important to
2003 // the app.
2004 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
2005 final boolean crosses = record.crossesHorizontalSizeThreshold(
2006 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
2007 || record.crossesVerticalSizeThreshold(
2008 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
2009 if (!crosses) {
2010 taskChanges &= ~CONFIG_SCREEN_SIZE;
2011 }
2012 }
2013 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
2014 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
2015 final int newSmallest = taskConfig.smallestScreenWidthDp;
2016 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
2017 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
2018 }
2019 }
2020 return taskChanges;
2021 }
2022
2023 private static boolean isResizeOnlyChange(int change) {
2024 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
2025 | CONFIG_SCREEN_LAYOUT)) == 0;
2026 }
2027
2028 void relaunchActivityLocked(boolean andResume, boolean preserveWindow) {
2029 if (service.mSuppressResizeConfigChanges && preserveWindow) {
2030 configChangeFlags = 0;
2031 return;
2032 }
2033
2034 List<ResultInfo> pendingResults = null;
2035 List<ReferrerIntent> pendingNewIntents = null;
2036 if (andResume) {
2037 pendingResults = results;
2038 pendingNewIntents = newIntents;
2039 }
2040 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2041 "Relaunching: " + this + " with results=" + pendingResults
2042 + " newIntents=" + pendingNewIntents + " andResume=" + andResume
2043 + " preserveWindow=" + preserveWindow);
2044 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
2045 : EventLogTags.AM_RELAUNCH_ACTIVITY, userId, System.identityHashCode(this),
2046 task.taskId, shortComponentName);
2047
2048 startFreezingScreenLocked(app, 0);
2049
2050 mStackSupervisor.removeChildActivityContainers(this);
2051
2052 try {
2053 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
2054 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + this
2055 + " callers=" + Debug.getCallers(6));
2056 forceNewConfig = false;
2057 mStackSupervisor.activityRelaunchingLocked(this);
2058 app.thread.scheduleRelaunchActivity(appToken, pendingResults, pendingNewIntents,
2059 configChangeFlags, !andResume,
2060 new Configuration(service.getGlobalConfiguration()),
2061 new Configuration(task.getMergedOverrideConfiguration()), preserveWindow);
2062 // Note: don't need to call pauseIfSleepingLocked() here, because the caller will only
2063 // pass in 'andResume' if this activity is currently resumed, which implies we aren't
2064 // sleeping.
2065 } catch (RemoteException e) {
2066 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
2067 }
2068
2069 if (andResume) {
2070 if (DEBUG_STATES) {
2071 Slog.d(TAG_STATES, "Resumed after relaunch " + this);
2072 }
2073 results = null;
2074 newIntents = null;
2075 service.showUnsupportedZoomDialogIfNeededLocked(this);
2076 service.showAskCompatModeDialogLocked(this);
2077 } else {
2078 service.mHandler.removeMessages(ActivityStack.PAUSE_TIMEOUT_MSG, this);
2079 state = ActivityState.PAUSED;
2080 // if the app is relaunched when it's stopped, and we're not resuming,
2081 // put it back into stopped state.
2082 if (stopped) {
2083 getStack().addToStopping(this, true /* immediate */);
2084 }
2085 }
2086
2087 configChangeFlags = 0;
2088 deferRelaunchUntilPaused = false;
2089 preserveWindowOnDeferredRelaunch = false;
2090 }
2091
Craig Mautner21d24a22014-04-23 11:45:37 -07002092 void saveToXml(XmlSerializer out) throws IOException, XmlPullParserException {
2093 out.attribute(null, ATTR_ID, String.valueOf(createTime));
2094 out.attribute(null, ATTR_LAUNCHEDFROMUID, String.valueOf(launchedFromUid));
2095 if (launchedFromPackage != null) {
2096 out.attribute(null, ATTR_LAUNCHEDFROMPACKAGE, launchedFromPackage);
2097 }
2098 if (resolvedType != null) {
2099 out.attribute(null, ATTR_RESOLVEDTYPE, resolvedType);
2100 }
2101 out.attribute(null, ATTR_COMPONENTSPECIFIED, String.valueOf(componentSpecified));
2102 out.attribute(null, ATTR_USERID, String.valueOf(userId));
Winson Chung2cb86c72014-06-25 12:03:30 -07002103
Craig Mautner21d24a22014-04-23 11:45:37 -07002104 if (taskDescription != null) {
Craig Mautner648f69b2014-09-18 14:16:26 -07002105 taskDescription.saveToXml(out);
Craig Mautner21d24a22014-04-23 11:45:37 -07002106 }
2107
2108 out.startTag(null, TAG_INTENT);
2109 intent.saveToXml(out);
2110 out.endTag(null, TAG_INTENT);
2111
2112 if (isPersistable() && persistentState != null) {
2113 out.startTag(null, TAG_PERSISTABLEBUNDLE);
2114 persistentState.saveToXml(out);
2115 out.endTag(null, TAG_PERSISTABLEBUNDLE);
2116 }
2117 }
2118
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002119 static ActivityRecord restoreFromXml(XmlPullParser in,
2120 ActivityStackSupervisor stackSupervisor) throws IOException, XmlPullParserException {
Craig Mautner21d24a22014-04-23 11:45:37 -07002121 Intent intent = null;
2122 PersistableBundle persistentState = null;
2123 int launchedFromUid = 0;
2124 String launchedFromPackage = null;
2125 String resolvedType = null;
2126 boolean componentSpecified = false;
2127 int userId = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -07002128 long createTime = -1;
2129 final int outerDepth = in.getDepth();
Winson Chung2cb86c72014-06-25 12:03:30 -07002130 TaskDescription taskDescription = new TaskDescription();
Craig Mautner21d24a22014-04-23 11:45:37 -07002131
2132 for (int attrNdx = in.getAttributeCount() - 1; attrNdx >= 0; --attrNdx) {
2133 final String attrName = in.getAttributeName(attrNdx);
2134 final String attrValue = in.getAttributeValue(attrNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002135 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Wale Ogunwale18795a22014-12-03 11:38:33 -08002136 "ActivityRecord: attribute name=" + attrName + " value=" + attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002137 if (ATTR_ID.equals(attrName)) {
Tobias Thierer28532d02016-04-21 14:52:10 +01002138 createTime = Long.parseLong(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002139 } else if (ATTR_LAUNCHEDFROMUID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002140 launchedFromUid = Integer.parseInt(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002141 } else if (ATTR_LAUNCHEDFROMPACKAGE.equals(attrName)) {
2142 launchedFromPackage = attrValue;
2143 } else if (ATTR_RESOLVEDTYPE.equals(attrName)) {
2144 resolvedType = attrValue;
2145 } else if (ATTR_COMPONENTSPECIFIED.equals(attrName)) {
Tobias Thiererb0800dc2016-04-21 17:51:41 +01002146 componentSpecified = Boolean.parseBoolean(attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002147 } else if (ATTR_USERID.equals(attrName)) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002148 userId = Integer.parseInt(attrValue);
Craig Mautner648f69b2014-09-18 14:16:26 -07002149 } else if (attrName.startsWith(TaskDescription.ATTR_TASKDESCRIPTION_PREFIX)) {
2150 taskDescription.restoreFromXml(attrName, attrValue);
Craig Mautner21d24a22014-04-23 11:45:37 -07002151 } else {
2152 Log.d(TAG, "Unknown ActivityRecord attribute=" + attrName);
2153 }
2154 }
2155
2156 int event;
2157 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
Ben Kwa8814cf42015-07-08 10:54:56 -07002158 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
Craig Mautner21d24a22014-04-23 11:45:37 -07002159 if (event == XmlPullParser.START_TAG) {
2160 final String name = in.getName();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002161 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002162 Slog.d(TaskPersister.TAG, "ActivityRecord: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -07002163 if (TAG_INTENT.equals(name)) {
2164 intent = Intent.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002165 if (TaskPersister.DEBUG)
Wale Ogunwale18795a22014-12-03 11:38:33 -08002166 Slog.d(TaskPersister.TAG, "ActivityRecord: intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002167 } else if (TAG_PERSISTABLEBUNDLE.equals(name)) {
2168 persistentState = PersistableBundle.restoreFromXml(in);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -07002169 if (TaskPersister.DEBUG) Slog.d(TaskPersister.TAG,
Craig Mautner21d24a22014-04-23 11:45:37 -07002170 "ActivityRecord: persistentState=" + persistentState);
2171 } else {
2172 Slog.w(TAG, "restoreActivity: unexpected name=" + name);
2173 XmlUtils.skipCurrentTag(in);
2174 }
2175 }
2176 }
2177
2178 if (intent == null) {
Craig Mautnere0129b32014-05-25 16:41:09 -07002179 throw new XmlPullParserException("restoreActivity error intent=" + intent);
Craig Mautner21d24a22014-04-23 11:45:37 -07002180 }
2181
2182 final ActivityManagerService service = stackSupervisor.mService;
2183 final ActivityInfo aInfo = stackSupervisor.resolveActivity(intent, resolvedType, 0, null,
Jeff Hao1b012d32014-08-20 10:35:34 -07002184 userId);
Craig Mautnere0129b32014-05-25 16:41:09 -07002185 if (aInfo == null) {
Craig Mautner77b04262014-06-27 15:22:12 -07002186 throw new XmlPullParserException("restoreActivity resolver error. Intent=" + intent +
2187 " resolvedType=" + resolvedType);
Craig Mautnere0129b32014-05-25 16:41:09 -07002188 }
Craig Mautner21d24a22014-04-23 11:45:37 -07002189 final ActivityRecord r = new ActivityRecord(service, /*caller*/null, launchedFromUid,
2190 launchedFromPackage, intent, resolvedType, aInfo, service.getConfiguration(),
Wale Ogunwale1affbbc2016-05-01 09:03:52 -07002191 null, null, 0, componentSpecified, false, stackSupervisor, null, null, null);
Craig Mautner21d24a22014-04-23 11:45:37 -07002192
2193 r.persistentState = persistentState;
Winson Chung2cb86c72014-06-25 12:03:30 -07002194 r.taskDescription = taskDescription;
Craig Mautner21d24a22014-04-23 11:45:37 -07002195 r.createTime = createTime;
2196
2197 return r;
2198 }
2199
2200 private static String activityTypeToString(int type) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07002201 switch (type) {
2202 case APPLICATION_ACTIVITY_TYPE: return "APPLICATION_ACTIVITY_TYPE";
2203 case HOME_ACTIVITY_TYPE: return "HOME_ACTIVITY_TYPE";
2204 case RECENTS_ACTIVITY_TYPE: return "RECENTS_ACTIVITY_TYPE";
2205 default: return Integer.toString(type);
2206 }
2207 }
2208
Craig Mautnerf81b90872013-02-26 13:02:43 -08002209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002211 if (stringName != null) {
Wale Ogunwale18795a22014-12-03 11:38:33 -08002212 return stringName + " t" + (task == null ? INVALID_TASK_ID : task.taskId) +
Craig Mautnerf3333272013-04-22 10:55:53 -07002213 (finishing ? " f}" : "}");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002214 }
2215 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002216 sb.append("ActivityRecord{");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002217 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002218 sb.append(" u");
2219 sb.append(userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002220 sb.append(' ');
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002221 sb.append(intent.getComponent().flattenToShortString());
Craig Mautnerf81b90872013-02-26 13:02:43 -08002222 stringName = sb.toString();
2223 return toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225}