blob: 79aa85fa5c6385f4debb95d0907495fd9ebb7576 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 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
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Wale Ogunwale868a5e12015-08-02 16:19:20 -070020import static android.app.ActivityManager.*;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
22import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
23import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
24import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
25import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
26import static android.app.ActivityManager.StackId.HOME_STACK_ID;
27import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
28import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070030import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
31import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070032import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
33import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070034import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070035import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070036import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080037import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070038import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070039import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
40import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
41import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070042import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070043import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
44import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010045import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070046import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
47import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070048
Svetoslav7008b512015-06-24 18:47:07 -070049import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070050import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070052import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080053import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.app.ActivityOptions;
55import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070056import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080057import android.app.IActivityContainer;
58import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070059import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.app.IApplicationThread;
Clara Bayarrif7fea162015-10-22 16:09:52 +010061import android.app.KeyguardManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070063import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070064import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070066import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040067import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040068import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070069import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070070import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070071import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070072import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070073import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070074import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070076import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.content.pm.PackageManager;
78import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010079import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080081import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080082import android.hardware.display.DisplayManager;
83import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080084import android.hardware.display.DisplayManagerGlobal;
85import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080086import android.hardware.input.InputManager;
87import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080088import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070090import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070091import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070092import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070093import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070094import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070095import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070096import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070097import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070098import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070099import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400100import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700101import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700102import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700103import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700104import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100105import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700106import android.os.WorkSource;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100107import android.os.storage.StorageManager;
Svetoslav7008b512015-06-24 18:47:07 -0700108import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400109import android.provider.Settings;
110import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700111import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700112import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700113import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700114import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700115import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800116import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700117
Craig Mautner4a1cb222013-12-04 16:14:06 -0800118import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800119import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800120import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800121import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800122import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700123import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700124import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800125import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700126import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400127import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700128import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400129import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800130import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700131import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700132import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700133
justinzhang5286d3f2014-05-12 17:06:01 -0400134
Craig Mautner8d341ef2013-03-26 09:03:27 -0700135import java.io.FileDescriptor;
136import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700137import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700138import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700139import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700140import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700141import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700142
Craig Mautner4a1cb222013-12-04 16:14:06 -0800143public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800144 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700145 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700146 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700147 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700149 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700150 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700151 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700152 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
153 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700154 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700155 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700156 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
157 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700158 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700159 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800160
Craig Mautnerf3333272013-04-22 10:55:53 -0700161 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700162 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700163
Craig Mautner0eea92c2013-05-16 13:35:39 -0700164 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700165 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700166
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700167 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700168 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700169
Craig Mautner05d29032013-05-03 13:40:13 -0700170 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
171 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
172 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700173 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700174 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800175 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
176 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
177 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700178 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400179 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
180 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700181 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700182 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700183 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800184
Wale Ogunwale040b4702015-08-06 18:10:50 -0700185 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700186
Jason Monk62515be2014-05-21 16:06:19 -0400187 private static final String LOCK_TASK_TAG = "Lock-to-App";
188
Wale Ogunwale040b4702015-08-06 18:10:50 -0700189 // Used to indicate if an object (e.g. stack) that we are trying to get
190 // should be created if it doesn't exist already.
191 private static final boolean CREATE_IF_NEEDED = true;
192
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700193 // Used to indicate that windows of activities should be preserved during the resize.
194 static final boolean PRESERVE_WINDOWS = true;
195
Wale Ogunwale040b4702015-08-06 18:10:50 -0700196 // Used to indicate if an object (e.g. task) should be moved/created
197 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700198 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700199
200 // Used to indicate that an objects (e.g. task) removal from its container
201 // (e.g. stack) is due to it moving to another container.
202 static final boolean MOVING = true;
203
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700204 // Force the focus to change to the stack we are moving a task to..
205 static final boolean FORCE_FOCUS = true;
206
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700207 // Restore task from the saved recents if it can't be found in any live stack.
208 static final boolean RESTORE_FROM_RECENTS = true;
209
Svetoslav7008b512015-06-24 18:47:07 -0700210 // Activity actions an app cannot start if it uses a permission which is not granted.
211 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
212 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700213
Svetoslav7008b512015-06-24 18:47:07 -0700214 static {
215 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
216 Manifest.permission.CAMERA);
217 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
218 Manifest.permission.CAMERA);
219 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
220 Manifest.permission.CALL_PHONE);
221 }
222
Svet Ganov99b60432015-06-27 13:15:22 -0700223 /** Action restriction: launching the activity is not restricted. */
224 private static final int ACTIVITY_RESTRICTION_NONE = 0;
225 /** Action restriction: launching the activity is restricted by a permission. */
226 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
227 /** Action restriction: launching the activity is restricted by an app op. */
228 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700229
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700230 // The height/width divide used when fitting a task within a bounds with method
231 // {@link #fitWithinBounds}.
232 // We always want the task to to be visible in the bounds without affecting its size when
233 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
234 // the input bounds right or bottom side minus the width or height divided by this value.
235 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
236
justinzhang5286d3f2014-05-12 17:06:01 -0400237 /** Status Bar Service **/
238 private IBinder mToken = new Binder();
239 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400240 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400241
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700242 // For debugging to make sure the caller when acquiring/releasing our
243 // wake lock is the system process.
244 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700245
Craig Mautner27084302013-03-25 08:05:25 -0700246 final ActivityManagerService mService;
247
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800248 private final RecentTasks mRecentTasks;
249
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700250 final ActivityStackSupervisorHandler mHandler;
251
252 /** Short cut */
253 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800254 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700255
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700256 /** Counter for next free stack ID to use for dynamic activity stacks. */
257 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700258
259 /** Task identifier that activities are currently being started in. Incremented each time a
260 * new task is created. */
261 private int mCurTaskId = 0;
262
Craig Mautner2420ead2013-04-01 17:13:20 -0700263 /** The current user */
264 private int mCurrentUser;
265
Craig Mautnere0a38842013-12-16 16:14:02 -0800266 /** The stack containing the launcher app. Assumed to always be attached to
267 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700268 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700269
Craig Mautnere0a38842013-12-16 16:14:02 -0800270 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700271 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700272
Craig Mautner4a1cb222013-12-04 16:14:06 -0800273 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
274 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800275 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800276 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700277
278 /** List of activities that are waiting for a new activity to become visible before completing
279 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800280 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700281
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700282 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800283 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700284
285 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800286 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700287
Craig Mautnerde4ef022013-04-07 19:01:33 -0700288 /** List of activities that are ready to be stopped, but waiting for the next activity to
289 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800290 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700291
Craig Mautnerf3333272013-04-22 10:55:53 -0700292 /** List of activities that are ready to be finished, but waiting for the previous activity to
293 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800294 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700295
Craig Mautner0eea92c2013-05-16 13:35:39 -0700296 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800297 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700298
Craig Mautnerf3333272013-04-22 10:55:53 -0700299 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700300 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700301
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700302 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700303 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700304
Craig Mautnerde4ef022013-04-07 19:01:33 -0700305 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
306 * is being brought in front of us. */
307 boolean mUserLeaving = false;
308
Craig Mautner0eea92c2013-05-16 13:35:39 -0700309 /** Set when we have taken too long waiting to go to sleep. */
310 boolean mSleepTimeout = false;
311
312 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700313 * We don't want to allow the device to go to sleep while in the process
314 * of launching an activity. This is primarily to allow alarm intent
315 * receivers to launch an activity and get that to run before the device
316 * goes back to sleep.
317 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700318 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700319
320 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700321 * Set when the system is going to sleep, until we have
322 * successfully paused the current activity and released our wake lock.
323 * At that point the system is allowed to actually sleep.
324 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700325 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700326
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700327 /** Stack id of the front stack when user switched, indexed by userId. */
328 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700329
Craig Mautner4504de52013-12-20 09:06:56 -0800330 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800331 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700332 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800333
334 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700335 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800336
Jeff Brownca9bc702014-02-11 14:32:56 -0800337 InputManagerInternal mInputManagerInternal;
338
Craig Mautner15df08a2015-04-01 12:17:18 -0700339 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
340 * may be finished until there is only one entry left. If this is empty the system is not
341 * in lockTask mode. */
342 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000343 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700344 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
345 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000346 */
347 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400348 /**
349 * Notifies the user when entering/exiting lock-task.
350 */
351 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800352
Craig Mautner15df08a2015-04-01 12:17:18 -0700353 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700354
Craig Mautner42d04db2014-11-06 12:13:23 -0800355 /** Used to keep resumeTopActivityLocked() from being entered recursively */
356 boolean inResumeTopActivity;
357
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700358 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700359 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700360 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700361
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700362 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
363 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
364
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700365 // The default minimal size that will be used if the activity doesn't specify its minimal size.
366 // It will be calculated when the default display gets added.
367 private int mDefaultMinimalSizeOfResizeableTask = -1;
368
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700369 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
370 private boolean mTaskLayersChanged = true;
371
Craig Mautneree36c772014-07-16 14:56:05 -0700372 /**
373 * Description of a request to start a new activity, which has been held
374 * due to app switches being disabled.
375 */
376 static class PendingActivityLaunch {
377 final ActivityRecord r;
378 final ActivityRecord sourceRecord;
379 final int startFlags;
380 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700381 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700382
383 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700384 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700385 r = _r;
386 sourceRecord = _sourceRecord;
387 startFlags = _startFlags;
388 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700389 callerApp = _callerApp;
390 }
391
392 void sendErrorResult(String message) {
393 try {
394 if (callerApp.thread != null) {
395 callerApp.thread.scheduleCrash(message);
396 }
397 } catch (RemoteException e) {
398 Slog.e(TAG, "Exception scheduling crash of failed "
399 + "activity launcher sourceRecord=" + sourceRecord, e);
400 }
Craig Mautneree36c772014-07-16 14:56:05 -0700401 }
402 }
403
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800404 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700405 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800406 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700407 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
408 }
409
410 /**
411 * At the time when the constructor runs, the power manager has not yet been
412 * initialized. So we initialize our wakelocks afterwards.
413 */
414 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800415 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700416 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700417 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700418 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700419 }
420
justinzhang5286d3f2014-05-12 17:06:01 -0400421 // This function returns a IStatusBarService. The value is from ServiceManager.
422 // getService and is cached.
423 private IStatusBarService getStatusBarService() {
424 synchronized (mService) {
425 if (mStatusBarService == null) {
426 mStatusBarService = IStatusBarService.Stub.asInterface(
427 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
428 if (mStatusBarService == null) {
429 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
430 }
431 }
432 return mStatusBarService;
433 }
434 }
435
Jason Monk35c62a42014-06-17 10:24:47 -0400436 private IDevicePolicyManager getDevicePolicyManager() {
437 synchronized (mService) {
438 if (mDevicePolicyManager == null) {
439 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
440 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
441 if (mDevicePolicyManager == null) {
442 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
443 }
444 }
445 return mDevicePolicyManager;
446 }
447 }
448
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700449 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800450 synchronized (mService) {
451 mWindowManager = wm;
452
453 mDisplayManager =
454 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
455 mDisplayManager.registerDisplayListener(this, null);
456
457 Display[] displays = mDisplayManager.getDisplays();
458 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
459 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800460 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700461 if (activityDisplay.mDisplay == null) {
462 throw new IllegalStateException("Default Display does not exist");
463 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800464 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700465 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800466 }
467
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700468 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800469 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800470
471 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800472 }
Craig Mautner27084302013-03-25 08:05:25 -0700473 }
474
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200475 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700476 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200477 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700478 }
479
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700480 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800481 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700482 }
483
Craig Mautnerde4ef022013-04-07 19:01:33 -0700484 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800485 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700486 }
487
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700488 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700489 if (stack == null) {
490 return false;
491 }
492
Craig Mautnerdf88d732014-01-27 09:21:32 -0800493 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
494 if (parent != null) {
495 stack = parent.task.stack;
496 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800497 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700498 }
499
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700500 /** The top most stack. */
501 boolean isFrontStack(ActivityStack stack) {
502 if (stack == null) {
503 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800504 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700505
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700506 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
507 if (parent != null) {
508 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800509 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700510 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
511 }
512
513 void setFocusStack(String reason, ActivityStack focusedStack) {
514 mLastFocusedStack = mFocusedStack;
515 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800516
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700517 EventLogTags.writeAmFocusedStack(
518 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
519 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800520
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800521 if (mService.mBooting || !mService.mBooted) {
522 final ActivityRecord r = topRunningActivityLocked();
523 if (r != null && r.idle) {
524 checkFinishBootingLocked();
525 }
526 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700527 }
528
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700529 void moveHomeStackToFront(String reason) {
530 mHomeStack.moveToFront(reason);
531 }
532
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700533 /** Returns true if the focus activity was adjusted to the home stack top activity. */
534 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700535 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
536 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700537 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700538 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700539
Craig Mautner84984fa2014-06-19 11:19:20 -0700540 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700541
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700542 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700543 if (top == null) {
544 return false;
545 }
546 mService.setFocusedActivityLocked(top, reason);
547 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700548 }
549
Craig Mautner299f9602015-01-26 09:47:33 -0800550 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700551 if (!mService.mBooting && !mService.mBooted) {
552 // Not ready yet!
553 return false;
554 }
555
Craig Mautner84984fa2014-06-19 11:19:20 -0700556 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
557 mWindowManager.showRecentApps();
558 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700559 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700560
Craig Mautner84984fa2014-06-19 11:19:20 -0700561 if (prev != null) {
562 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
563 }
564
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700565 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
566 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800567 // Only resume home activity if isn't finishing.
568 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800569 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700570 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700571 }
Craig Mautner299f9602015-01-26 09:47:33 -0800572 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700573 }
574
Craig Mautner8d341ef2013-03-26 09:03:27 -0700575 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700576 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700577 }
578
579 /**
580 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
581 * @param id Id of the task we would like returned.
582 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
583 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700584 * @param stackId The stack to restore the task to (default launch stack will be used if
585 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700586 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700587 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800588 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800589 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800590 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
592 ActivityStack stack = stacks.get(stackNdx);
593 TaskRecord task = stack.taskForIdLocked(id);
594 if (task != null) {
595 return task;
596 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700597 }
598 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800599
600 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700601 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800602 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800603 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700604 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800605 return null;
606 }
607
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700608 if (!restoreFromRecents) {
609 return task;
610 }
611
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700612 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700613 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
614 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800615 return null;
616 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700617 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800618 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700619 }
620
Craig Mautner6170f732013-04-02 13:05:23 -0700621 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800622 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800623 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800624 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800625 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
626 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
627 if (r != null) {
628 return r;
629 }
Craig Mautner6170f732013-04-02 13:05:23 -0700630 }
631 }
632 return null;
633 }
634
Craig Mautneref73ee12014-04-23 11:45:37 -0700635 void setNextTaskId(int taskId) {
636 if (taskId > mCurTaskId) {
637 mCurTaskId = taskId;
638 }
639 }
640
Craig Mautner8d341ef2013-03-26 09:03:27 -0700641 int getNextTaskId() {
642 do {
643 mCurTaskId++;
644 if (mCurTaskId <= 0) {
645 mCurTaskId = 1;
646 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700647 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700648 return mCurTaskId;
649 }
650
Craig Mautnerde4ef022013-04-07 19:01:33 -0700651 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800652 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700653 if (stack == null) {
654 return null;
655 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700656 ActivityRecord resumedActivity = stack.mResumedActivity;
657 if (resumedActivity == null || resumedActivity.app == null) {
658 resumedActivity = stack.mPausingActivity;
659 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700660 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700661 }
662 }
663 return resumedActivity;
664 }
665
Dianne Hackbornff072722014-09-24 10:56:28 -0700666 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700667 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800669 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
670 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
672 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700673 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 continue;
675 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700676 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 if (hr != null) {
678 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
679 && processName.equals(hr.processName)) {
680 try {
George Mount2c92c972014-03-20 09:38:23 -0700681 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800682 didSomething = true;
683 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700684 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 Slog.w(TAG, "Exception in new application when starting activity "
686 + hr.intent.getComponent().flattenToShortString(), e);
687 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700688 }
Craig Mautner20e72272013-04-01 13:45:53 -0700689 }
Craig Mautner20e72272013-04-01 13:45:53 -0700690 }
691 }
692 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700693 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700694 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700695 }
Craig Mautner20e72272013-04-01 13:45:53 -0700696 return didSomething;
697 }
698
699 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800700 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
701 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800702 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
703 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700704 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 continue;
706 }
707 final ActivityRecord resumedActivity = stack.mResumedActivity;
708 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700709 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800710 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800711 return false;
712 }
Craig Mautner20e72272013-04-01 13:45:53 -0700713 }
714 }
715 return true;
716 }
717
Craig Mautnerde4ef022013-04-07 19:01:33 -0700718 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800719 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
720 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
722 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700723 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700725 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 return false;
727 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700728 }
729 }
730 }
731 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700732 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800733 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
734 mLastFocusedStack + " to=" + mFocusedStack);
735 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700736 return true;
737 }
738
739 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800740 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800741 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
742 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
744 final ActivityStack stack = stacks.get(stackNdx);
745 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800746 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700747 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800748 return false;
749 }
750 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800751 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700752 }
753 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800754 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700755 }
756
Craig Mautner2acc3892013-09-23 10:28:14 -0700757 /**
758 * Pause all activities in either all of the stacks or just the back stacks.
759 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700760 * @return true if any activity was paused as a result of this call.
761 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700762 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700763 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
765 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800766 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
767 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700768 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700769 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700771 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
772 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700774 }
775 }
776 return someActivityPaused;
777 }
778
Craig Mautnerde4ef022013-04-07 19:01:33 -0700779 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700780 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800781 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
782 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800783 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
784 final ActivityStack stack = stacks.get(stackNdx);
785 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700786 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700788 Slog.d(TAG_STATES,
789 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800790 pausing = false;
791 } else {
792 return false;
793 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700794 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700795 }
796 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700797 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700798 }
799
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700800 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
801 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500802 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800803 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
804 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
805 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
806 final ActivityStack stack = stacks.get(stackNdx);
807 if (stack.mResumedActivity != null &&
808 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700809 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800810 }
811 }
812 }
813 }
814
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700815 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700816 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700817 }
818
819 void sendWaitingVisibleReportLocked(ActivityRecord r) {
820 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700821 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700822 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700823 if (w.who == null) {
824 changed = true;
825 w.timeout = false;
826 if (r != null) {
827 w.who = new ComponentName(r.info.packageName, r.info.name);
828 }
829 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
830 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700831 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700832 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700833 if (changed) {
834 mService.notifyAll();
835 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700836 }
837
838 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
839 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700840 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700841 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700842 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700843 if (w.who == null) {
844 changed = true;
845 w.timeout = timeout;
846 if (r != null) {
847 w.who = new ComponentName(r.info.packageName, r.info.name);
848 }
849 w.thisTime = thisTime;
850 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700851 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700852 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700853 if (changed) {
854 mService.notifyAll();
855 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700856 }
857
Craig Mautner29219d92013-04-16 20:19:12 -0700858 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800859 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700860 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700861 if (r != null) {
862 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700863 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700864
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800866 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800867 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
868 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700869 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700870 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700871 if (r != null) {
872 return r;
873 }
874 }
875 }
876 return null;
877 }
878
Dianne Hackborn09233282014-04-30 11:33:59 -0700879 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700880 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800881 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
882 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800883 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800884 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800885 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800886 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
887 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700888 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800889 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700890 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700891 }
892 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700893
894 // The lists are already sorted from most recent to oldest. Just pull the most recent off
895 // each list and add it to list. Stop when all lists are empty or maxNum reached.
896 while (maxNum > 0) {
897 long mostRecentActiveTime = Long.MIN_VALUE;
898 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800899 final int numTaskLists = runningTaskLists.size();
900 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
901 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700902 if (!stackTaskList.isEmpty()) {
903 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
904 if (lastActiveTime > mostRecentActiveTime) {
905 mostRecentActiveTime = lastActiveTime;
906 selectedStackList = stackTaskList;
907 }
908 }
909 }
910 if (selectedStackList != null) {
911 list.add(selectedStackList.remove(0));
912 --maxNum;
913 } else {
914 break;
915 }
916 }
Craig Mautner20e72272013-04-01 13:45:53 -0700917 }
918
Craig Mautner23ac33b2013-04-01 16:26:35 -0700919 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700920 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700921 // Collect information about the target of the Intent.
922 ActivityInfo aInfo;
923 try {
924 ResolveInfo rInfo =
925 AppGlobals.getPackageManager().resolveIntent(
926 intent, resolvedType,
927 PackageManager.MATCH_DEFAULT_ONLY
928 | ActivityManagerService.STOCK_PM_FLAGS, userId);
929 aInfo = rInfo != null ? rInfo.activityInfo : null;
930 } catch (RemoteException e) {
931 aInfo = null;
932 }
933
934 if (aInfo != null) {
935 // Store the found target back into the intent, because now that
936 // we have it we never want to do this again. For example, if the
937 // user navigates back to this point in the history, we should
938 // always restart the exact same activity.
939 intent.setComponent(new ComponentName(
940 aInfo.applicationInfo.packageName, aInfo.name));
941
942 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700943 if (!aInfo.processName.equals("system")) {
944 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700945 mService.setDebugApp(aInfo.processName, true, false);
946 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700947
Man Caocfa78b22015-06-11 20:14:34 -0700948 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
949 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
950 }
951
952 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700953 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700954 }
955 }
956 }
957 return aInfo;
958 }
959
Craig Mautner299f9602015-01-26 09:47:33 -0800960 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
961 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700962 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
963 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
964 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
965 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700966 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
967 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700968 null /* outActivity */, null /* container */, null /* inTask */);
969 if (inResumeTopActivity) {
970 // If we are in resume section already, home activity will be initialized, but not
971 // resumed (to avoid recursive resume) and will stay that way until something pokes it
972 // again. We need to schedule another resume.
973 scheduleResumeTopActivities();
974 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700975 }
976
Craig Mautner23ac33b2013-04-01 16:26:35 -0700977 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700978 String callingPackage, Intent intent, String resolvedType,
979 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700980 IBinder resultTo, String resultWho, int requestCode, int startFlags,
981 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Chong Zhang280d3322015-11-03 17:27:26 -0800982 Bundle bOptions, boolean ignoreTargetSecurity, int userId,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700983 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700984 // Refuse possible leaked file descriptors
985 if (intent != null && intent.hasFileDescriptors()) {
986 throw new IllegalArgumentException("File descriptors passed in Intent");
987 }
988 boolean componentSpecified = intent.getComponent() != null;
989
990 // Don't modify the client's object!
991 intent = new Intent(intent);
992
993 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700994 ActivityInfo aInfo =
995 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700996
Chong Zhang280d3322015-11-03 17:27:26 -0800997 ActivityOptions options = ActivityOptions.fromBundle(bOptions);
Craig Mautnere0a38842013-12-16 16:14:02 -0800998 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700999 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001000 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001001 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001002 // Cannot start a child activity if the parent is not resumed.
1003 return ActivityManager.START_CANCELED;
1004 }
Dianne Hackborn95465202014-09-15 16:21:55 -07001005 final int realCallingPid = Binder.getCallingPid();
1006 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -07001007 int callingPid;
1008 if (callingUid >= 0) {
1009 callingPid = -1;
1010 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001011 callingPid = realCallingPid;
1012 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001013 } else {
1014 callingPid = callingUid = -1;
1015 }
1016
Craig Mautnere0a38842013-12-16 16:14:02 -08001017 final ActivityStack stack;
1018 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001019 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001020 } else {
1021 stack = container.mStack;
1022 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001023 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001024 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026
1027 final long origId = Binder.clearCallingIdentity();
1028
1029 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001030 (aInfo.applicationInfo.privateFlags
1031 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001032 // This may be a heavy-weight process! Check to see if we already
1033 // have another, different heavy-weight process running.
1034 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1035 if (mService.mHeavyWeightProcess != null &&
1036 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1037 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001038 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001039 if (caller != null) {
1040 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1041 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001042 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001043 } else {
1044 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001045 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001046 + intent.toString());
1047 ActivityOptions.abort(options);
1048 return ActivityManager.START_PERMISSION_DENIED;
1049 }
1050 }
1051
1052 IIntentSender target = mService.getIntentSenderLocked(
1053 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001054 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001055 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1056 | PendingIntent.FLAG_ONE_SHOT, null);
1057
1058 Intent newIntent = new Intent();
1059 if (requestCode >= 0) {
1060 // Caller is requesting a result.
1061 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1062 }
1063 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1064 new IntentSender(target));
1065 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1066 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1067 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1068 hist.packageName);
1069 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1070 hist.task.taskId);
1071 }
1072 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1073 aInfo.packageName);
1074 newIntent.setFlags(intent.getFlags());
1075 newIntent.setClassName("android",
1076 HeavyWeightSwitcherActivity.class.getName());
1077 intent = newIntent;
1078 resolvedType = null;
1079 caller = null;
1080 callingUid = Binder.getCallingUid();
1081 callingPid = Binder.getCallingPid();
1082 componentSpecified = true;
1083 try {
1084 ResolveInfo rInfo =
1085 AppGlobals.getPackageManager().resolveIntent(
1086 intent, null,
1087 PackageManager.MATCH_DEFAULT_ONLY
1088 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1089 aInfo = rInfo != null ? rInfo.activityInfo : null;
1090 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1091 } catch (RemoteException e) {
1092 aInfo = null;
1093 }
1094 }
1095 }
1096 }
1097
Dianne Hackborn91097de2014-04-04 18:02:06 -07001098 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1099 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001100 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001101 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001102 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001103
Craig Mautner85c11a82014-07-17 12:38:18 -07001104 Binder.restoreCallingIdentity(origId);
1105
Craig Mautnerde4ef022013-04-07 19:01:33 -07001106 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001107 // If the caller also wants to switch to a new configuration,
1108 // do so now. This allows a clean switch, as we are waiting
1109 // for the current activity to pause (so we will not destroy
1110 // it), and have not yet started the next activity.
1111 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1112 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001113 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001114 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001115 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001116 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001117 }
1118
Craig Mautner23ac33b2013-04-01 16:26:35 -07001119 if (outResult != null) {
1120 outResult.result = res;
1121 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001122 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001123 do {
1124 try {
1125 mService.wait();
1126 } catch (InterruptedException e) {
1127 }
1128 } while (!outResult.timeout && outResult.who == null);
1129 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001130 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001131 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001132 outResult.timeout = false;
1133 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1134 outResult.totalTime = 0;
1135 outResult.thisTime = 0;
1136 } else {
1137 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001138 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001139 do {
1140 try {
1141 mService.wait();
1142 } catch (InterruptedException e) {
1143 }
1144 } while (!outResult.timeout && outResult.who == null);
1145 }
1146 }
1147 }
1148
1149 return res;
1150 }
1151 }
1152
1153 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1154 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
Chong Zhang280d3322015-11-03 17:27:26 -08001155 Bundle bOptions, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001156 if (intents == null) {
1157 throw new NullPointerException("intents is null");
1158 }
1159 if (resolvedTypes == null) {
1160 throw new NullPointerException("resolvedTypes is null");
1161 }
1162 if (intents.length != resolvedTypes.length) {
1163 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1164 }
1165
Craig Mautner23ac33b2013-04-01 16:26:35 -07001166
1167 int callingPid;
1168 if (callingUid >= 0) {
1169 callingPid = -1;
1170 } else if (caller == null) {
1171 callingPid = Binder.getCallingPid();
1172 callingUid = Binder.getCallingUid();
1173 } else {
1174 callingPid = callingUid = -1;
1175 }
1176 final long origId = Binder.clearCallingIdentity();
1177 try {
1178 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001179 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001180 for (int i=0; i<intents.length; i++) {
1181 Intent intent = intents[i];
1182 if (intent == null) {
1183 continue;
1184 }
1185
1186 // Refuse possible leaked file descriptors
1187 if (intent != null && intent.hasFileDescriptors()) {
1188 throw new IllegalArgumentException("File descriptors passed in Intent");
1189 }
1190
1191 boolean componentSpecified = intent.getComponent() != null;
1192
1193 // Don't modify the client's object!
1194 intent = new Intent(intent);
1195
1196 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001197 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001198 // TODO: New, check if this is correct
1199 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1200
1201 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001202 (aInfo.applicationInfo.privateFlags
1203 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001204 throw new IllegalArgumentException(
1205 "FLAG_CANT_SAVE_STATE not supported here");
1206 }
1207
Chong Zhang280d3322015-11-03 17:27:26 -08001208 ActivityOptions options = ActivityOptions.fromBundle(
1209 i == intents.length - 1 ? bOptions : null);
Craig Mautner6170f732013-04-02 13:05:23 -07001210 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001211 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1212 callingPackage, callingPid, callingUid,
Chong Zhang280d3322015-11-03 17:27:26 -08001213 0, options, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001214 if (res < 0) {
1215 return res;
1216 }
1217
1218 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1219 }
1220 }
1221 } finally {
1222 Binder.restoreCallingIdentity(origId);
1223 }
1224
1225 return ActivityManager.START_SUCCESS;
1226 }
1227
Craig Mautner2420ead2013-04-01 17:13:20 -07001228 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001229 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001230 throws RemoteException {
1231
Craig Mautner2568c3a2015-03-26 14:22:34 -07001232 if (andResume) {
1233 r.startFreezingScreenLocked(app, 0);
1234 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001235
Craig Mautner2568c3a2015-03-26 14:22:34 -07001236 // schedule launch ticks to collect information about slow apps.
1237 r.startLaunchTickingLocked();
1238 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001239
1240 // Have the window manager re-evaluate the orientation of
1241 // the screen based on the new activity order. Note that
1242 // as a result of this, it can call back into the activity
1243 // manager with a new orientation. We don't care about that,
1244 // because the activity is not currently running so we are
1245 // just restarting it anyway.
1246 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001247 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001248 mService.mConfiguration,
1249 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001250 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 }
1252
1253 r.app = app;
1254 app.waitingToKill = null;
1255 r.launchCount++;
1256 r.lastLaunchTime = SystemClock.uptimeMillis();
1257
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001258 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001259
1260 int idx = app.activities.indexOf(r);
1261 if (idx < 0) {
1262 app.activities.add(r);
1263 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001264 mService.updateLruProcessLocked(app, true, null);
1265 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001266
Craig Mautner15df08a2015-04-01 12:17:18 -07001267 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001268 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1269 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001270 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001271 }
1272
1273 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001274 try {
1275 if (app.thread == null) {
1276 throw new RemoteException();
1277 }
1278 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001279 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 if (andResume) {
1281 results = r.results;
1282 newIntents = r.newIntents;
1283 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001284 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1285 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1286 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001287 if (andResume) {
1288 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1289 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001290 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001291 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001292 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001293 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001294 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001296 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001297 r.sleeping = false;
1298 r.forceNewConfig = false;
1299 mService.showAskCompatModeDialogLocked(r);
1300 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001301 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1303 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1304 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001305 final String profileFile = mService.mProfileFile;
1306 if (profileFile != null) {
1307 ParcelFileDescriptor profileFd = mService.mProfileFd;
1308 if (profileFd != null) {
1309 try {
1310 profileFd = profileFd.dup();
1311 } catch (IOException e) {
1312 if (profileFd != null) {
1313 try {
1314 profileFd.close();
1315 } catch (IOException o) {
1316 }
1317 profileFd = null;
1318 }
1319 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001320 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001321
1322 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1323 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 }
1325 }
1326 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001327
Craig Mautner2568c3a2015-03-26 14:22:34 -07001328 if (andResume) {
1329 app.hasShownUi = true;
1330 app.pendingUiClean = true;
1331 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001332 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001333 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001334 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001335 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001336 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001337 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001338
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001339 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001340 // This may be a heavy-weight process! Note that the package
1341 // manager will ensure that only activity can run in the main
1342 // process of the .apk, which is the only thing that will be
1343 // considered heavy-weight.
1344 if (app.processName.equals(app.info.packageName)) {
1345 if (mService.mHeavyWeightProcess != null
1346 && mService.mHeavyWeightProcess != app) {
1347 Slog.w(TAG, "Starting new heavy weight process " + app
1348 + " when already running "
1349 + mService.mHeavyWeightProcess);
1350 }
1351 mService.mHeavyWeightProcess = app;
1352 Message msg = mService.mHandler.obtainMessage(
1353 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1354 msg.obj = r;
1355 mService.mHandler.sendMessage(msg);
1356 }
1357 }
1358
1359 } catch (RemoteException e) {
1360 if (r.launchFailed) {
1361 // This is the second time we failed -- finish activity
1362 // and give up.
1363 Slog.e(TAG, "Second failure launching "
1364 + r.intent.getComponent().flattenToShortString()
1365 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001366 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001367 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1368 "2nd-crash", false);
1369 return false;
1370 }
1371
1372 // This is the first time we failed -- restart process and
1373 // retry.
1374 app.activities.remove(r);
1375 throw e;
1376 }
1377
1378 r.launchFailed = false;
1379 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001380 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001381 }
1382
1383 if (andResume) {
1384 // As part of the process of launching, ActivityThread also performs
1385 // a resume.
1386 stack.minimalResumeActivityLocked(r);
1387 } else {
1388 // This activity is not starting in the resumed state... which
1389 // should look like we asked it to pause+stop (but remain visible),
1390 // and it has done so and reported back the current icicle and
1391 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001392 if (DEBUG_STATES) Slog.v(TAG_STATES,
1393 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001394 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001395 r.stopped = true;
1396 }
1397
1398 // Launch the new version setup screen if needed. We do this -after-
1399 // launching the initial activity (that is, home), so that it can have
1400 // a chance to initialize itself while in the background, making the
1401 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001402 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001403 mService.startSetupActivityLocked();
1404 }
1405
Dianne Hackborn465fa392014-09-14 14:21:18 -07001406 // Update any services we are bound to that might care about whether
1407 // their client may have activities.
1408 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1409
Craig Mautner2420ead2013-04-01 17:13:20 -07001410 return true;
1411 }
1412
Craig Mautnere79d42682013-04-01 19:01:53 -07001413 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001414 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001415 // Is this activity's application already running?
1416 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001417 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001418
1419 r.task.stack.setLaunchTime(r);
1420
1421 if (app != null && app.thread != null) {
1422 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001423 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1424 || !"android".equals(r.info.packageName)) {
1425 // Don't add this if it is a platform component that is marked
1426 // to run in multiple processes, because this is actually
1427 // part of the framework so doesn't make sense to track as a
1428 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001429 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1430 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001431 }
George Mount2c92c972014-03-20 09:38:23 -07001432 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001433 return;
1434 } catch (RemoteException e) {
1435 Slog.w(TAG, "Exception when starting activity "
1436 + r.intent.getComponent().flattenToShortString(), e);
1437 }
1438
1439 // If a dead object exception was thrown -- fall through to
1440 // restart the application.
1441 }
1442
1443 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001444 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001445 }
1446
Craig Mautner6170f732013-04-02 13:05:23 -07001447 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001448 Intent intent, String resolvedType, ActivityInfo aInfo,
1449 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1450 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001451 int callingPid, int callingUid, String callingPackage,
Chong Zhang280d3322015-11-03 17:27:26 -08001452 int realCallingPid, int realCallingUid, int startFlags, ActivityOptions options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001453 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1454 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001455 int err = ActivityManager.START_SUCCESS;
1456
1457 ProcessRecord callerApp = null;
1458 if (caller != null) {
1459 callerApp = mService.getRecordForAppLocked(caller);
1460 if (callerApp != null) {
1461 callingPid = callerApp.pid;
1462 callingUid = callerApp.info.uid;
1463 } else {
1464 Slog.w(TAG, "Unable to find app for caller " + caller
1465 + " (pid=" + callingPid + ") when starting: "
1466 + intent.toString());
1467 err = ActivityManager.START_PERMISSION_DENIED;
1468 }
1469 }
1470
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001471 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1472
Craig Mautner6170f732013-04-02 13:05:23 -07001473 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001474 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001475 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001476 + " on display " + (container == null ? (mFocusedStack == null ?
1477 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1478 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1479 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001480 }
1481
1482 ActivityRecord sourceRecord = null;
1483 ActivityRecord resultRecord = null;
1484 if (resultTo != null) {
1485 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001486 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1487 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001488 if (sourceRecord != null) {
1489 if (requestCode >= 0 && !sourceRecord.finishing) {
1490 resultRecord = sourceRecord;
1491 }
1492 }
1493 }
Craig Mautner6170f732013-04-02 13:05:23 -07001494
Dianne Hackborn91097de2014-04-04 18:02:06 -07001495 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001496
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001497 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001498 // Transfer the result target from the source activity to the new
1499 // one being started, including any failures.
1500 if (requestCode >= 0) {
1501 ActivityOptions.abort(options);
1502 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1503 }
1504 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001505 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1506 resultRecord = null;
1507 }
Craig Mautner6170f732013-04-02 13:05:23 -07001508 resultWho = sourceRecord.resultWho;
1509 requestCode = sourceRecord.requestCode;
1510 sourceRecord.resultTo = null;
1511 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001512 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001513 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001514 if (sourceRecord.launchedFromUid == callingUid) {
1515 // The new activity is being launched from the same uid as the previous
1516 // activity in the flow, and asking to forward its result back to the
1517 // previous. In this case the activity is serving as a trampoline between
1518 // the two, so we also want to update its launchedFromPackage to be the
1519 // same as the previous activity. Note that this is safe, since we know
1520 // these two packages come from the same uid; the caller could just as
1521 // well have supplied that same package name itself. This specifially
1522 // deals with the case of an intent picker/chooser being launched in the app
1523 // flow to redirect to an activity picked by the user, where we want the final
1524 // activity to consider it to have been launched by the previous app activity.
1525 callingPackage = sourceRecord.launchedFromPackage;
1526 }
Craig Mautner6170f732013-04-02 13:05:23 -07001527 }
1528
1529 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1530 // We couldn't find a class that can handle the given Intent.
1531 // That's the end of that!
1532 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1533 }
1534
1535 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1536 // We couldn't find the specific class specified in the Intent.
1537 // Also the end of the line.
1538 err = ActivityManager.START_CLASS_NOT_FOUND;
1539 }
1540
Dianne Hackborn91097de2014-04-04 18:02:06 -07001541 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1542 && sourceRecord.task.voiceSession != null) {
1543 // If this activity is being launched as part of a voice session, we need
1544 // to ensure that it is safe to do so. If the upcoming activity will also
1545 // be part of the voice session, we can only launch it if it has explicitly
1546 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001547 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001548 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1549 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001550 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001551 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1552 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001553 Slog.w(TAG,
1554 "Activity being started in current voice task does not support voice: "
1555 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001556 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1557 }
1558 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001559 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001560 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1561 }
1562 }
1563 }
1564
1565 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1566 // If the caller is starting a new voice session, just make sure the target
1567 // is actually allowing it to run this way.
1568 try {
1569 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1570 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001571 Slog.w(TAG,
1572 "Activity being started in new voice task does not support: "
1573 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001574 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1575 }
1576 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001577 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001578 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1579 }
1580 }
1581
louis_changcd5d1982014-08-01 10:09:08 +08001582 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1583
Craig Mautner6170f732013-04-02 13:05:23 -07001584 if (err != ActivityManager.START_SUCCESS) {
1585 if (resultRecord != null) {
1586 resultStack.sendActivityResultLocked(-1,
1587 resultRecord, resultWho, requestCode,
1588 Activity.RESULT_CANCELED, null);
1589 }
Craig Mautner6170f732013-04-02 13:05:23 -07001590 ActivityOptions.abort(options);
1591 return err;
1592 }
1593
Svetoslav7008b512015-06-24 18:47:07 -07001594 boolean abort = false;
1595
Svet Ganov99b60432015-06-27 13:15:22 -07001596 final int startAnyPerm = mService.checkPermission(
1597 START_ANY_ACTIVITY, callingPid, callingUid);
1598
1599 if (startAnyPerm != PERMISSION_GRANTED) {
1600 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001601 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001602 final int actionRestriction = getActionRestrictionForCallingPackage(
1603 intent.getAction(), callingPackage, callingPid, callingUid);
1604
1605 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1606 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1607 if (resultRecord != null) {
1608 resultStack.sendActivityResultLocked(-1,
1609 resultRecord, resultWho, requestCode,
1610 Activity.RESULT_CANCELED, null);
1611 }
1612 String msg;
1613 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1614 msg = "Permission Denial: starting " + intent.toString()
1615 + " from " + callerApp + " (pid=" + callingPid
1616 + ", uid=" + callingUid + ")" + " with revoked permission "
1617 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1618 } else if (!aInfo.exported) {
1619 msg = "Permission Denial: starting " + intent.toString()
1620 + " from " + callerApp + " (pid=" + callingPid
1621 + ", uid=" + callingUid + ")"
1622 + " not exported from uid " + aInfo.applicationInfo.uid;
1623 } else {
1624 msg = "Permission Denial: starting " + intent.toString()
1625 + " from " + callerApp + " (pid=" + callingPid
1626 + ", uid=" + callingUid + ")"
1627 + " requires " + aInfo.permission;
1628 }
1629 Slog.w(TAG, msg);
1630 throw new SecurityException(msg);
1631 }
1632
1633 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1634 String message = "Appop Denial: starting " + intent.toString()
1635 + " from " + callerApp + " (pid=" + callingPid
1636 + ", uid=" + callingUid + ")"
1637 + " requires " + AppOpsManager.permissionToOp(
1638 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1639 Slog.w(TAG, message);
1640 abort = true;
1641 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1642 String message = "Appop Denial: starting " + intent.toString()
1643 + " from " + callerApp + " (pid=" + callingPid
1644 + ", uid=" + callingUid + ")"
1645 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1646 Slog.w(TAG, message);
1647 abort = true;
1648 }
Svetoslav7008b512015-06-24 18:47:07 -07001649 }
1650
1651 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001652 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001653
Craig Mautner6170f732013-04-02 13:05:23 -07001654 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001655 try {
1656 // The Intent we give to the watcher has the extra data
1657 // stripped off, since it can contain private information.
1658 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001659 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001660 aInfo.applicationInfo.packageName);
1661 } catch (RemoteException e) {
1662 mService.mController = null;
1663 }
Ben Gruver5e207332013-04-03 17:41:37 -07001664 }
Craig Mautner6170f732013-04-02 13:05:23 -07001665
Clara Bayarrif7fea162015-10-22 16:09:52 +01001666 UserInfo user = getUserInfo(userId);
1667 // TODO: Timeout for work challenge
1668 if (user.isManagedProfile()
1669 && mService.mContext.getSystemService(StorageManager.class)
1670 .isPerUserEncryptionEnabled()) {
1671 KeyguardManager km = (KeyguardManager) mService.mContext
1672 .getSystemService(Context.KEYGUARD_SERVICE);
1673
1674 IIntentSender target = mService.getIntentSenderLocked(
1675 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1676 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1677 new String[]{ resolvedType },
1678 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1679 | PendingIntent.FLAG_IMMUTABLE, null);
1680 int flags = intent.getFlags();
1681 intent = km.createConfirmDeviceCredentialIntent(null, null);
1682 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1683 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1684 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1685 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1686 intent.putExtra(Intent.EXTRA_USER_ID, userId);
1687 intent.setFlags(flags);
1688
1689 resolvedType = null;
1690 callingUid = realCallingUid;
1691 callingPid = realCallingPid;
1692
1693 UserInfo parent = UserManager.get(mService.mContext).getProfileParent(userId);
1694 aInfo = resolveActivity(intent, null, PackageManager.MATCH_DEFAULT_ONLY
1695 | ActivityManagerService.STOCK_PM_FLAGS, null, parent.id);
1696 }
1697
Ben Gruver5e207332013-04-03 17:41:37 -07001698 if (abort) {
1699 if (resultRecord != null) {
1700 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001701 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001702 }
Ben Gruver5e207332013-04-03 17:41:37 -07001703 // We pretend to the caller that it was really started, but
1704 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001705 ActivityOptions.abort(options);
1706 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001707 }
1708
1709 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001710 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001711 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001712 if (outActivity != null) {
1713 outActivity[0] = r;
1714 }
1715
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001716 if (r.appTimeTracker == null && sourceRecord != null) {
1717 // If the caller didn't specify an explicit time tracker, we want to continue
1718 // tracking under any it has.
1719 r.appTimeTracker = sourceRecord.appTimeTracker;
1720 }
1721
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001722 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001723 if (voiceSession == null && (stack.mResumedActivity == null
1724 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001725 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1726 realCallingPid, realCallingUid, "Activity start")) {
Chong Zhang280d3322015-11-03 17:27:26 -08001727 PendingActivityLaunch pal = new PendingActivityLaunch(r,
Robert Carr13997f52015-10-23 13:13:39 -07001728 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001729 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001730 ActivityOptions.abort(options);
1731 return ActivityManager.START_SWITCHES_CANCELED;
1732 }
1733 }
1734
1735 if (mService.mDidAppSwitch) {
1736 // This is the second allowed switch since we stopped switches,
1737 // so now just generally allow switches. Use case: user presses
1738 // home (switches disabled, switch to home, mDidAppSwitch now true);
1739 // user taps a home icon (coming from home so allowed, we hit here
1740 // and now allow anyone to switch again).
1741 mService.mAppSwitchesAllowedTime = 0;
1742 } else {
1743 mService.mDidAppSwitch = true;
1744 }
1745
Craig Mautneree36c772014-07-16 14:56:05 -07001746 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001747
Dianne Hackborn91097de2014-04-04 18:02:06 -07001748 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001749 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001750
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001751 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001752 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001753 // activity start, but we are not actually doing an activity
1754 // switch... just dismiss the keyguard now, because we
1755 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001756 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001757 }
1758 return err;
1759 }
1760
Clara Bayarrif7fea162015-10-22 16:09:52 +01001761 private UserInfo getUserInfo(int userId) {
1762 final long identity = Binder.clearCallingIdentity();
1763 try {
1764 return UserManager.get(mService.mContext).getUserInfo(userId);
1765 } finally {
1766 Binder.restoreCallingIdentity(identity);
1767 }
1768 }
1769
Svet Ganov99b60432015-06-27 13:15:22 -07001770 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001771 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001772 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1773 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001774 == PackageManager.PERMISSION_DENIED) {
1775 return ACTIVITY_RESTRICTION_PERMISSION;
1776 }
1777
Christopher Tateff7add02015-08-17 10:23:22 -07001778 if (activityInfo.permission == null) {
1779 return ACTIVITY_RESTRICTION_NONE;
1780 }
1781
Svet Ganov99b60432015-06-27 13:15:22 -07001782 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1783 if (opCode == AppOpsManager.OP_NONE) {
1784 return ACTIVITY_RESTRICTION_NONE;
1785 }
1786
1787 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1788 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001789 if (!ignoreTargetSecurity) {
1790 return ACTIVITY_RESTRICTION_APPOP;
1791 }
Svet Ganov99b60432015-06-27 13:15:22 -07001792 }
1793
1794 return ACTIVITY_RESTRICTION_NONE;
1795 }
1796
Svetoslav7008b512015-06-24 18:47:07 -07001797 private int getActionRestrictionForCallingPackage(String action,
1798 String callingPackage, int callingPid, int callingUid) {
1799 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001800 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001801 }
1802
1803 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1804 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001805 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001806 }
1807
1808 final PackageInfo packageInfo;
1809 try {
1810 packageInfo = mService.mContext.getPackageManager()
1811 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1812 } catch (PackageManager.NameNotFoundException e) {
1813 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001814 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001815 }
1816
1817 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001818 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001819 }
1820
1821 if (mService.checkPermission(permission, callingPid, callingUid) ==
1822 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001823 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001824 }
1825
1826 final int opCode = AppOpsManager.permissionToOpCode(permission);
1827 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001828 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001829 }
1830
1831 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1832 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001833 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001834 }
1835
Svet Ganov99b60432015-06-27 13:15:22 -07001836 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001837 }
1838
Chong Zhang0fa656b2015-08-31 15:17:21 -07001839 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001840 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001841
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001842 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1843 return mHomeStack;
1844 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001845
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001846 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001847
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001848 if (task != null && task.stack != null) {
1849 stack = task.stack;
1850 if (stack.isOnHomeDisplay()) {
1851 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001852 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001853 "computeStackFocus: Setting " + "focused stack to r=" + r
1854 + " task=" + task);
1855 } else {
1856 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1857 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001858 }
1859 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001860 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001861 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001862
1863 final ActivityContainer container = r.mInitialActivityContainer;
1864 if (container != null) {
1865 // The first time put it on the desired stack, after this put on task stack.
1866 r.mInitialActivityContainer = null;
1867 return container.mStack;
1868 }
1869
1870 // The fullscreen stack can contain any task regardless of if the task is resizeable
1871 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1872 // If the freeform stack has focus, and the activity to be launched is resizeable,
1873 // we can also put it in the focused stack.
1874 final boolean canUseFocusedStack =
1875 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1876 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1877 if (canUseFocusedStack
1878 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1879 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1880 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1881 return mFocusedStack;
1882 }
1883
1884 // We first try to put the task in the first dynamic stack.
1885 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1886 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1887 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001888 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001889 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1890 "computeStackFocus: Setting focused stack=" + stack);
1891 return stack;
1892 }
1893 }
1894
1895 // If there is no suitable dynamic stack then we figure out which static stack to use.
1896 final int stackId = task != null ? task.getLaunchStackId() :
1897 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1898 FULLSCREEN_WORKSPACE_STACK_ID;
1899 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1900 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1901 + r + " stackId=" + stack.mStackId);
1902 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001903 }
1904
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001905 boolean setFocusedStack(ActivityRecord r, String reason) {
1906 if (r == null) {
1907 // Not sure what you are trying to do, but it is not going to work...
1908 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001909 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001910 final TaskRecord task = r.task;
1911 if (task == null || task.stack == null) {
1912 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1913 return false;
1914 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001915 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001916 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001917 }
1918
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001919 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001920 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Chong Zhang280d3322015-11-03 17:27:26 -08001921 boolean doResume, ActivityOptions options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001922 final Intent intent = r.intent;
1923 final int callingUid = r.launchedFromUid;
1924
Chong Zhang0fa656b2015-08-31 15:17:21 -07001925 boolean overrideBounds = false;
1926 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001927 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001928 if (canUseActivityOptionsLaunchBounds(options)) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001929 overrideBounds = true;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08001930 newBounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001931 }
1932 }
1933
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001934 // In some flows in to this function, we retrieve the task record and hold on to it
1935 // without a lock before calling back in to here... so the task at this point may
1936 // not actually be in recents. Check for that, and if it isn't in recents just
1937 // consider it invalid.
1938 if (inTask != null && !inTask.inRecents) {
1939 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1940 inTask = null;
1941 }
1942
Craig Mautnerad400af2014-08-13 16:41:36 -07001943 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001944 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1945 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001946
Craig Mautnera228ae92014-07-09 05:44:55 -07001947 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001948 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001949 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001950 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1951 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1952 "\"singleInstance\" or \"singleTask\"");
1953 launchFlags &=
1954 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1955 } else {
1956 switch (r.info.documentLaunchMode) {
1957 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1958 break;
1959 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1960 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1961 break;
1962 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1963 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1964 break;
1965 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1966 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1967 break;
1968 }
1969 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001970
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001971 final boolean launchTaskBehind = r.mLaunchTaskBehind
1972 && !launchSingleTask && !launchSingleInstance
1973 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001974
Wale Ogunwale7d701172015-03-11 15:36:30 -07001975 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1976 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001977 // For whatever reason this activity is being launched into a new
1978 // task... yet the caller has requested a result back. Well, that
1979 // is pretty messed up, so instead immediately send back a cancel
1980 // and let the new task continue launched as normal without a
1981 // dependency on its originator.
1982 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1983 r.resultTo.task.stack.sendActivityResultLocked(-1,
1984 r.resultTo, r.resultWho, r.requestCode,
1985 Activity.RESULT_CANCELED, null);
1986 r.resultTo = null;
1987 }
1988
1989 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1990 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1991 }
1992
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001993 // If we are actually going to launch in to a new task, there are some cases where
1994 // we further want to do multiple task.
1995 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1996 if (launchTaskBehind
1997 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1998 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1999 }
2000 }
2001
Craig Mautner8849a5e2013-04-02 16:41:03 -07002002 // We'll invoke onUserLeaving before onPause only if the launching
2003 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07002004 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002005 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2006 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002007
2008 // If the caller has asked not to resume at this point, we make note
2009 // of this in the record so that we can skip it when trying to find
2010 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07002011 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002012 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002013 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002014 }
2015
Craig Mautnera254cd72014-05-25 16:47:39 -07002016 ActivityRecord notTop =
2017 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002018
2019 // If the onlyIfNeeded flag is set, then we can do this if the activity
2020 // being launched is the same as the one making the call... or, as
2021 // a special case, if we do not know the caller then we count the
2022 // current top activity as the caller.
2023 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2024 ActivityRecord checkedCaller = sourceRecord;
2025 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002026 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002027 }
2028 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2029 // Caller is not the same as launcher, so always needed.
2030 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
2031 }
2032 }
2033
Craig Mautner8849a5e2013-04-02 16:41:03 -07002034 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002035 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002036
2037 // If the caller is not coming from another activity, but has given us an
2038 // explicit task into which they would like us to launch the new activity,
2039 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002040 if (sourceRecord == null && inTask != null && inTask.stack != null) {
2041 final Intent baseIntent = inTask.getBaseIntent();
2042 final ActivityRecord root = inTask.getRootActivity();
2043 if (baseIntent == null) {
2044 ActivityOptions.abort(options);
2045 throw new IllegalArgumentException("Launching into task without base intent: "
2046 + inTask);
2047 }
2048
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002049 // If this task is empty, then we are adding the first activity -- it
2050 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002051 if (launchSingleInstance || launchSingleTask) {
2052 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2053 ActivityOptions.abort(options);
2054 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2055 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002056 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002057 if (root != null) {
2058 ActivityOptions.abort(options);
2059 throw new IllegalArgumentException("Caller with inTask " + inTask
2060 + " has root " + root + " but target is singleInstance/Task");
2061 }
2062 }
2063
2064 // If task is empty, then adopt the interesting intent launch flags in to the
2065 // activity being started.
2066 if (root == null) {
2067 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2068 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2069 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2070 launchFlags = (launchFlags&~flagsOfInterest)
2071 | (baseIntent.getFlags()&flagsOfInterest);
2072 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002073 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002074 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002075
Dianne Hackborn962d5352014-08-29 16:27:40 -07002076 // If the task is not empty and the caller is asking to start it as the root
2077 // of a new task, then we don't actually want to start this on the task. We
2078 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002079 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002080 addingToTask = false;
2081
2082 } else {
2083 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002084 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002085
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002086 reuseTask = inTask;
2087 } else {
2088 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002089 // Launch ResolverActivity in the source task, so that it stays in the task
2090 // bounds when in freeform workspace.
2091 // Also put noDisplay activities in the source task. These by itself can
2092 // be placed in any task/stack, however it could launch other activities
2093 // like ResolverActivity, and we want those to stay in the original task.
2094 if (r.isResolverActivity() || r.noDisplay) {
2095 addingToTask = true;
2096 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002097 }
2098
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002099 if (inTask == null) {
2100 if (sourceRecord == null) {
2101 // This activity is not being started from another... in this
2102 // case we -always- start a new task.
2103 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2104 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2105 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2106 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2107 }
2108 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2109 // The original activity who is starting us is running as a single
2110 // instance... this new activity it is starting must go on its
2111 // own task.
2112 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2113 } else if (launchSingleInstance || launchSingleTask) {
2114 // The activity being started is a single instance... it always
2115 // gets launched into its own task.
2116 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2117 }
2118 }
2119
2120 ActivityInfo newTaskInfo = null;
2121 Intent newTaskIntent = null;
2122 ActivityStack sourceStack;
2123 if (sourceRecord != null) {
2124 if (sourceRecord.finishing) {
2125 // If the source is finishing, we can't further count it as our source. This
2126 // is because the task it is associated with may now be empty and on its way out,
2127 // so we don't want to blindly throw it in to that task. Instead we will take
2128 // the NEW_TASK flow and try to find a task for it. But save the task information
2129 // so it can be used when creating the new task.
2130 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2131 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2132 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2133 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2134 newTaskInfo = sourceRecord.info;
2135 newTaskIntent = sourceRecord.task.intent;
2136 }
2137 sourceRecord = null;
2138 sourceStack = null;
2139 } else {
2140 sourceStack = sourceRecord.task.stack;
2141 }
2142 } else {
2143 sourceStack = null;
2144 }
2145
2146 boolean movedHome = false;
2147 ActivityStack targetStack;
2148
2149 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002150 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002151
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002152 // We may want to try to place the new activity in to an existing task. We always
2153 // do this if the target activity is singleTask or singleInstance; we will also do
2154 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2155 // us to still place it in a new task: multi task, always doc mode, or being asked to
2156 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002157 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2158 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002159 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002160 // If bring to front is requested, and no result is requested and we have not
2161 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002162 // we can find a task that was started with this same
2163 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002164 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002165 // See if there is a task to bring to the front. If this is
2166 // a SINGLE_INSTANCE activity, there can be one and only one
2167 // instance of it in the history, and it is always in its own
2168 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002169 ActivityRecord intentActivity = !launchSingleInstance ?
2170 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002171 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002172 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2173 // but still needs to be a lock task mode violation since the task gets
2174 // cleared out and the device would otherwise leave the locked task.
2175 if (isLockTaskModeViolation(intentActivity.task,
2176 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2177 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002178 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002179 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002180 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2181 }
Craig Mautner29219d92013-04-16 20:19:12 -07002182 if (r.task == null) {
2183 r.task = intentActivity.task;
2184 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002185 if (intentActivity.task.intent == null) {
2186 // This task was started because of movement of
2187 // the activity based on affinity... now that we
2188 // are actually launching it, we can assign the
2189 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002190 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002191 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002192 targetStack = intentActivity.task.stack;
2193 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002194 // If the target task is not in the front, then we need
2195 // to bring it to the front... except... well, with
2196 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2197 // to have the same behavior as if a new instance was
2198 // being started, which means not bringing it to the front
2199 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002200 final ActivityStack focusStack = getFocusedStack();
2201 ActivityRecord curTop = (focusStack == null)
2202 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002203 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002204 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002205 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002206 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002207 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2208 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002209 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002210 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002211 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2212 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002213 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002214 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002215 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002216 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002217 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002218 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2219 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002220 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002221 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002222 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002223 options = null;
2224 }
2225 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002226 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002227 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002228 + " from " + intentActivity);
2229 targetStack.moveToFront("intentActivityFound");
2230 }
2231
Craig Mautner8849a5e2013-04-02 16:41:03 -07002232 // If the caller has requested that the target task be
2233 // reset, then do so.
2234 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2235 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2236 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002237 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002238 // We don't need to start a new activity, and
2239 // the client said not to do anything if that
2240 // is the case, so this is it! And for paranoia, make
2241 // sure we have correctly resumed the top activity.
2242 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002243 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002244
2245 // Make sure to notify Keyguard as well if we are not running an app
2246 // transition later.
2247 if (!movedToFront) {
2248 notifyActivityDrawnForKeyguard();
2249 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002250 } else {
2251 ActivityOptions.abort(options);
2252 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002253 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002254 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2255 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002256 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002257 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002258 // The caller has requested to completely replace any
2259 // existing task with its new activity. Well that should
2260 // not be too hard...
2261 reuseTask = intentActivity.task;
2262 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002263 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002264 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002265 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002266 // In this situation we want to remove all activities
2267 // from the task up to the one being started. In most
2268 // cases this means we are resetting the task to its
2269 // initial state.
2270 ActivityRecord top =
2271 intentActivity.task.performClearTaskLocked(r, launchFlags);
2272 if (top != null) {
2273 if (top.frontOfTask) {
2274 // Activity aliases may mean we use different
2275 // intents for the top activity, so make sure
2276 // the task now has the identity of the new
2277 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002278 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002279 }
2280 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2281 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002282 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002283 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002284 // A special case: we need to start the activity because it is not
2285 // currently running, and the caller has asked to clear the current
2286 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002287 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002288 // Now pretend like this activity is being started by the top of its
2289 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002290 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002291 TaskRecord task = sourceRecord.task;
2292 if (task != null && task.stack == null) {
2293 // Target stack got cleared when we all activities were removed
2294 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002295 targetStack = computeStackFocus(
2296 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002297 targetStack.addTask(
2298 task, !launchTaskBehind /* toTop */, false /* moving */);
2299 }
2300
Craig Mautner8849a5e2013-04-02 16:41:03 -07002301 }
2302 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2303 // In this case the top activity on the task is the
2304 // same as the one being launched, so we take that
2305 // as a request to bring the task to the foreground.
2306 // If the top activity in the task is the root
2307 // activity, deliver this new intent to it if it
2308 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002309 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002310 && intentActivity.realActivity.equals(r.realActivity)) {
2311 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2312 intentActivity.task);
2313 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002314 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002315 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002316 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2317 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002318 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2319 // In this case we are launching the root activity
2320 // of the task, but with a different intent. We
2321 // should start a new instance on top.
2322 addingToTask = true;
2323 sourceRecord = intentActivity;
2324 }
2325 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2326 // In this case an activity is being launched in to an
2327 // existing task, without resetting that task. This
2328 // is typically the situation of launching an activity
2329 // from a notification or shortcut. We want to place
2330 // the new activity on top of the current task.
2331 addingToTask = true;
2332 sourceRecord = intentActivity;
2333 } else if (!intentActivity.task.rootWasReset) {
2334 // In this case we are launching in to an existing task
2335 // that has not yet been started from its front door.
2336 // The current task has been brought to the front.
2337 // Ideally, we'd probably like to place this new task
2338 // at the bottom of its stack, but that's a little hard
2339 // to do with the current organization of the code so
2340 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002341 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002342 }
2343 if (!addingToTask && reuseTask == null) {
2344 // We didn't do anything... but it was needed (a.k.a., client
2345 // don't use that intent!) And for paranoia, make
2346 // sure we have correctly resumed the top activity.
2347 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002348 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002349 if (!movedToFront) {
2350 // Make sure to notify Keyguard as well if we are not running an app
2351 // transition later.
2352 notifyActivityDrawnForKeyguard();
2353 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002354 } else {
2355 ActivityOptions.abort(options);
2356 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002357 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002358 return ActivityManager.START_TASK_TO_FRONT;
2359 }
2360 }
2361 }
2362 }
2363
2364 //String uri = r.intent.toURI();
2365 //Intent intent2 = new Intent(uri);
2366 //Slog.i(TAG, "Given intent: " + r.intent);
2367 //Slog.i(TAG, "URI is: " + uri);
2368 //Slog.i(TAG, "To intent: " + intent2);
2369
2370 if (r.packageName != null) {
2371 // If the activity being launched is the same as the one currently
2372 // at the top, then we need to check if it should only be launched
2373 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002374 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002375 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002376 if (top != null && r.resultTo == null) {
2377 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2378 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002379 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002380 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002381 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2382 top.task);
2383 // For paranoia, make sure we have correctly
2384 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002385 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002386 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002387 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002388 }
2389 ActivityOptions.abort(options);
2390 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2391 // We don't need to start a new activity, and
2392 // the client said not to do anything if that
2393 // is the case, so this is it!
2394 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2395 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002396 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002397 return ActivityManager.START_DELIVERED_TO_TOP;
2398 }
2399 }
2400 }
2401 }
2402
2403 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002404 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002405 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2406 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002407 }
2408 ActivityOptions.abort(options);
2409 return ActivityManager.START_CLASS_NOT_FOUND;
2410 }
2411
2412 boolean newTask = false;
2413 boolean keepCurTransition = false;
2414
Craig Mautnerbb742462014-07-07 15:28:55 -07002415 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002416 sourceRecord.task : null;
2417
Craig Mautner8849a5e2013-04-02 16:41:03 -07002418 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002419 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002420 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002421 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002422 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002423 if (doResume) {
2424 targetStack.moveToFront("startingNewTask");
2425 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002426
Craig Mautner8849a5e2013-04-02 16:41:03 -07002427 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002428 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2429 newTaskInfo != null ? newTaskInfo : r.info,
2430 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002431 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2432 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002433 if (overrideBounds) {
2434 r.task.updateOverrideConfiguration(newBounds);
2435 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002436 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2437 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002438 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002439 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002440 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002441 if (isLockTaskModeViolation(r.task)) {
2442 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2443 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2444 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002445 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002446 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002447 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2448 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002449 // Caller wants to appear on home activity, so before starting
2450 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002451 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002452 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002453 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002454 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002455 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002456 if (isLockTaskModeViolation(sourceTask)) {
2457 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2458 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2459 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002460 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002461 if (doResume) {
2462 targetStack.moveToFront("sourceStackToFront");
2463 }
Craig Mautner737fae22014-10-15 12:52:10 -07002464 final TaskRecord topTask = targetStack.topTask();
2465 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002466 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002467 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002468 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002469 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002470 // In this case, we are adding the activity to an existing
2471 // task, but the caller has asked to clear that task if the
2472 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002473 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002474 keepCurTransition = true;
2475 if (top != null) {
2476 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002477 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002478 // For paranoia, make sure we have correctly
2479 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002480 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002481 if (doResume) {
2482 targetStack.resumeTopActivityLocked(null);
2483 }
2484 ActivityOptions.abort(options);
2485 return ActivityManager.START_DELIVERED_TO_TOP;
2486 }
2487 } else if (!addingToTask &&
2488 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2489 // In this case, we are launching an activity in our own task
2490 // that may already be running somewhere in the history, and
2491 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002492 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002493 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002494 final TaskRecord task = top.task;
2495 task.moveActivityToFrontLocked(top);
2496 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002497 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002498 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002499 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002500 if (doResume) {
2501 targetStack.resumeTopActivityLocked(null);
2502 }
2503 return ActivityManager.START_DELIVERED_TO_TOP;
2504 }
2505 }
2506 // An existing activity is starting this new activity, so we want
2507 // to keep the new one in the same task as the one that is starting
2508 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002509 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002510 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002511 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002512
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002513 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002514 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002515 // task it has provided to us.
2516 if (isLockTaskModeViolation(inTask)) {
2517 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2518 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2519 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002520 if (overrideBounds) {
2521 inTask.updateOverrideConfiguration(newBounds);
2522 int stackId = inTask.getLaunchStackId();
2523 if (stackId != inTask.stack.mStackId) {
2524 moveTaskToStackUncheckedLocked(
2525 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2526 }
2527 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002528 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002529 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2530 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002531
2532 // Check whether we should actually launch the new activity in to the task,
2533 // or just reuse the current activity on top.
2534 ActivityRecord top = inTask.getTopActivity();
2535 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2536 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2537 || launchSingleTop || launchSingleTask) {
2538 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2539 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2540 // We don't need to start a new activity, and
2541 // the client said not to do anything if that
2542 // is the case, so this is it!
2543 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2544 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002545 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002546 return ActivityManager.START_DELIVERED_TO_TOP;
2547 }
2548 }
2549
Dianne Hackborn962d5352014-08-29 16:27:40 -07002550 if (!addingToTask) {
2551 // We don't actually want to have this activity added to the task, so just
2552 // stop here but still tell the caller that we consumed the intent.
2553 ActivityOptions.abort(options);
2554 return ActivityManager.START_TASK_TO_FRONT;
2555 }
2556
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002557 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002558 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002559 + " in explicit task " + r.task);
2560
Craig Mautner8849a5e2013-04-02 16:41:03 -07002561 } else {
2562 // This not being started from an existing activity, and not part
2563 // of a new task... just put it in the top task, though these days
2564 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002565 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002566 if (doResume) {
2567 targetStack.moveToFront("addingToTopTask");
2568 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002569 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002570 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002571 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002572 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002573 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002574 + " in new guessed " + r.task);
2575 }
2576
2577 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002578 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002579
Craig Mautner76e2a762014-06-24 09:05:43 -07002580 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2581 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2582 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002583 if (newTask) {
2584 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2585 }
2586 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002587 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002588 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002589 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002590 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002591 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002592 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002593 return ActivityManager.START_SUCCESS;
2594 }
2595
Craig Mautneree36c772014-07-16 14:56:05 -07002596 final void doPendingActivityLaunchesLocked(boolean doResume) {
2597 while (!mPendingActivityLaunches.isEmpty()) {
2598 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002599
2600 try {
2601 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002602 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002603 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002604 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2605 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002606 }
Craig Mautneree36c772014-07-16 14:56:05 -07002607 }
2608 }
2609
Craig Mautner7f13ed32014-07-28 14:00:35 -07002610 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002611 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2612 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002613 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002614 mPendingActivityLaunches.remove(palNdx);
2615 }
2616 }
2617 }
2618
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002619 void setLaunchSource(int uid) {
2620 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2621 }
2622
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002623 void acquireLaunchWakelock() {
2624 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2625 throw new IllegalStateException("Calling must be system uid");
2626 }
2627 mLaunchingActivity.acquire();
2628 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2629 // To be safe, don't allow the wake lock to be held for too long.
2630 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2631 }
2632 }
2633
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002634 /**
2635 * Called when the frontmost task is idle.
2636 * @return the state of mService.mBooting before this was called.
2637 */
2638 private boolean checkFinishBootingLocked() {
2639 final boolean booting = mService.mBooting;
2640 boolean enableScreen = false;
2641 mService.mBooting = false;
2642 if (!mService.mBooted) {
2643 mService.mBooted = true;
2644 enableScreen = true;
2645 }
2646 if (booting || enableScreen) {
2647 mService.postFinishBooting(booting, enableScreen);
2648 }
2649 return booting;
2650 }
2651
Craig Mautnerf3333272013-04-22 10:55:53 -07002652 // Checked.
2653 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2654 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002655 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002656
Craig Mautnerf3333272013-04-22 10:55:53 -07002657 ArrayList<ActivityRecord> stops = null;
2658 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002659 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002660 int NS = 0;
2661 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002662 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002663 boolean activityRemoved = false;
2664
Wale Ogunwale7d701172015-03-11 15:36:30 -07002665 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002666 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002667 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2668 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002669 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2670 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002671 if (fromTimeout) {
2672 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002673 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002674
2675 // This is a hack to semi-deal with a race condition
2676 // in the client where it can be constructed with a
2677 // newer configuration from when we asked it to launch.
2678 // We'll update with whatever configuration it now says
2679 // it used to launch.
2680 if (config != null) {
2681 r.configuration = config;
2682 }
2683
2684 // We are now idle. If someone is waiting for a thumbnail from
2685 // us, we can now deliver.
2686 r.idle = true;
2687
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002688 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002689 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002690 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002691 }
2692 }
2693
2694 if (allResumedActivitiesIdle()) {
2695 if (r != null) {
2696 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002697 }
2698
2699 if (mLaunchingActivity.isHeld()) {
2700 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2701 if (VALIDATE_WAKE_LOCK_CALLER &&
2702 Binder.getCallingUid() != Process.myUid()) {
2703 throw new IllegalStateException("Calling must be system uid");
2704 }
2705 mLaunchingActivity.release();
2706 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002707 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002708 }
2709
2710 // Atomically retrieve all of the other things to do.
2711 stops = processStoppingActivitiesLocked(true);
2712 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002713 if ((NF = mFinishingActivities.size()) > 0) {
2714 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002715 mFinishingActivities.clear();
2716 }
2717
Craig Mautnerf3333272013-04-22 10:55:53 -07002718 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002719 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002720 mStartingUsers.clear();
2721 }
2722
Craig Mautnerf3333272013-04-22 10:55:53 -07002723 // Stop any activities that are scheduled to do so but have been
2724 // waiting for the next one to start.
2725 for (int i = 0; i < NS; i++) {
2726 r = stops.get(i);
2727 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002728 if (stack != null) {
2729 if (r.finishing) {
2730 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2731 } else {
2732 stack.stopActivityLocked(r);
2733 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002734 }
2735 }
2736
2737 // Finish any activities that are scheduled to do so but have been
2738 // waiting for the next one to start.
2739 for (int i = 0; i < NF; i++) {
2740 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002741 final ActivityStack stack = r.task.stack;
2742 if (stack != null) {
2743 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2744 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002745 }
2746
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002747 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002748 // Complete user switch
2749 if (startingUsers != null) {
2750 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002751 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002752 }
2753 }
2754 // Complete starting up of background users
2755 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002756 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002757 mStartingBackgroundUsers.clear();
2758 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002759 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002760 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002761 }
2762 }
2763
2764 mService.trimApplications();
2765 //dump();
2766 //mWindowManager.dump();
2767
Craig Mautnerf3333272013-04-22 10:55:53 -07002768 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002769 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002770 }
2771
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002772 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002773 }
2774
Craig Mautner8e569572013-10-11 17:36:59 -07002775 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002776 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002777 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2778 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002779 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2780 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2781 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002782 }
Craig Mautner19091252013-10-05 00:03:53 -07002783 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002784 }
2785
2786 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002787 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2788 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002789 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2790 stacks.get(stackNdx).closeSystemDialogsLocked();
2791 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002792 }
2793 }
2794
Craig Mautner93529a42013-10-04 15:03:13 -07002795 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002796 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002797 }
2798
Craig Mautner8d341ef2013-03-26 09:03:27 -07002799 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002800 * Update the last used stack id for non-current user (current user's last
2801 * used stack is the focused stack)
2802 */
2803 void updateUserStackLocked(int userId, ActivityStack stack) {
2804 if (userId != mCurrentUser) {
2805 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2806 }
2807 }
2808
2809 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002810 * @return true if some activity was finished (or would have finished if doit were true).
2811 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002812 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2813 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002814 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002815 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2816 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002817 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002818 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002819 if (stack.finishDisabledPackageActivitiesLocked(
2820 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002821 didSomething = true;
2822 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002823 }
2824 }
2825 return didSomething;
2826 }
2827
Dianne Hackborna413dc02013-07-12 12:02:55 -07002828 void updatePreviousProcessLocked(ActivityRecord r) {
2829 // Now that this process has stopped, we may want to consider
2830 // it to be the previous app to try to keep around in case
2831 // the user wants to return to it.
2832
2833 // First, found out what is currently the foreground app, so that
2834 // we don't blow away the previous app if this activity is being
2835 // hosted by the process that is actually still the foreground.
2836 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002837 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2838 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2840 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002841 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002842 if (stack.mResumedActivity != null) {
2843 fgApp = stack.mResumedActivity.app;
2844 } else if (stack.mPausingActivity != null) {
2845 fgApp = stack.mPausingActivity.app;
2846 }
2847 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002848 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002849 }
2850 }
2851
2852 // Now set this one as the previous process, only if that really
2853 // makes sense to.
2854 if (r.app != null && fgApp != null && r.app != fgApp
2855 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002856 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002857 mService.mPreviousProcess = r.app;
2858 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2859 }
2860 }
2861
Craig Mautner05d29032013-05-03 13:40:13 -07002862 boolean resumeTopActivitiesLocked() {
2863 return resumeTopActivitiesLocked(null, null, null);
2864 }
2865
Chong Zhang280d3322015-11-03 17:27:26 -08002866 boolean resumeTopActivitiesLocked(
2867 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Craig Mautner05d29032013-05-03 13:40:13 -07002868 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002869 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002870 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002871 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002872 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002873 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002874 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2875 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002876
Craig Mautnere0a38842013-12-16 16:14:02 -08002877 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2878 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002879 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2880 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002881 if (stack == targetStack) {
2882 // Already started above.
2883 continue;
2884 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002885 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002886 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002887 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002888 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002889 }
Craig Mautner05d29032013-05-03 13:40:13 -07002890 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002891 }
2892
Todd Kennedy539db512014-12-15 09:57:55 -08002893 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002894 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2895 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002896 final int numStacks = stacks.size();
2897 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2898 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002899 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002900 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002901 }
2902 }
2903
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002904 void finishVoiceTask(IVoiceInteractionSession session) {
2905 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2906 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2907 final int numStacks = stacks.size();
2908 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2909 final ActivityStack stack = stacks.get(stackNdx);
2910 stack.finishVoiceTask(session);
2911 }
2912 }
2913 }
2914
Chong Zhang280d3322015-11-03 17:27:26 -08002915 void findTaskToMoveToFrontLocked(
2916 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002917 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2918 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002919 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002920 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2921 // Caller wants the home activity moved with it. To accomplish this,
2922 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002923 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002924 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002925 if (task.stack == null) {
2926 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2927 + task + " to front. Stack is null");
2928 return;
2929 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002930
Chong Zhang0fa656b2015-08-31 15:17:21 -07002931 if (task.mResizeable && options != null) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002932 if (canUseActivityOptionsLaunchBounds(options)) {
2933 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002934 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08002935 final int stackId = task.getLaunchStackId();
2936 if (stackId != task.stack.mStackId) {
2937 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2938 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2939 // still need moveTaskToFrontLocked() below for any transition settings.
2940 }
2941 // WM resizeTask must be done after the task is moved to the correct stack,
2942 // because Task's setBounds() also updates dim layer's bounds, but that has
2943 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07002944 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2945 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002946 }
2947 }
2948
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002949 final ActivityRecord r = task.getTopActivity();
2950 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2951 r == null ? null : r.appTimeTracker, reason);
2952
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002953 if (DEBUG_STACK) Slog.d(TAG_STACK,
2954 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002955 }
2956
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002957 private boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
2958 // We use the launch bounds in the activity options is the device supports freeform
2959 // window management.
2960 return options.hasLaunchBounds() && mService.mSupportsFreeformWindowManagement;
2961 }
2962
Craig Mautner967212c2013-04-13 21:10:58 -07002963 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002964 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002965 }
2966
2967 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002968 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2969 if (activityContainer != null) {
2970 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002971 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002972 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002973 return null;
2974 }
2975 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002976 }
2977
Craig Mautner967212c2013-04-13 21:10:58 -07002978 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002979 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002980 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2981 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002982 }
2983 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002984 }
2985
Craig Mautner4a1cb222013-12-04 16:14:06 -08002986 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002987 ActivityRecord homeActivity = getHomeActivity();
2988 if (homeActivity != null) {
2989 return homeActivity.appToken;
2990 }
2991 return null;
2992 }
2993
2994 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002995 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002996 }
2997
2998 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002999 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
3000 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
3001 final TaskRecord task = tasks.get(taskNdx);
3002 if (task.isHomeTask()) {
3003 final ArrayList<ActivityRecord> activities = task.mActivities;
3004 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3005 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003006 if (r.isHomeActivity()
3007 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003008 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003009 }
3010 }
3011 }
3012 }
3013 return null;
3014 }
3015
Todd Kennedyca4d8422015-01-15 15:19:22 -08003016 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003017 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003018 ActivityContainer activityContainer =
3019 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003020 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003021 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3022 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003023 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003024 return activityContainer;
3025 }
3026
Craig Mautner34b73df2014-01-12 21:11:08 -08003027 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003028 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
3029 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
3030 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003031 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
3032 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003033 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08003034 }
3035 }
3036
Craig Mautner95da1082014-02-24 17:54:35 -08003037 void deleteActivityContainer(IActivityContainer container) {
3038 ActivityContainer activityContainer = (ActivityContainer)container;
3039 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003040 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3041 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08003042 final int stackId = activityContainer.mStackId;
3043 mActivityContainers.remove(stackId);
3044 mWindowManager.removeStack(stackId);
3045 }
3046 }
3047
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003048 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
3049 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003050 final ActivityStack stack = getStack(stackId);
3051 if (stack == null) {
3052 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
3053 return;
3054 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003055
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003056 if (!allowResizeInDockedMode
3057 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3058 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3059 // stack size changing so things don't get out of sync.
3060 return;
3061 }
3062
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003063 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003064 mWindowManager.deferSurfaceLayout();
3065 try {
3066 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003067
Jorim Jaggic4025202015-10-22 16:43:34 +02003068 mTmpBounds.clear();
3069 mTmpConfigs.clear();
3070 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3071 for (int i = tasks.size() - 1; i >= 0; i--) {
3072 TaskRecord task = tasks.get(i);
3073 if (task.mResizeable) {
3074 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3075 // For freeform stack we don't adjust the size of the tasks to match that
3076 // of the stack, but we do try to make sure the tasks are still contained
3077 // with the bounds of the stack.
3078 tempRect2.set(task.mBounds);
3079 fitWithinBounds(tempRect2, bounds);
3080 task.updateOverrideConfiguration(tempRect2);
3081 } else {
3082 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003083 }
3084 }
3085
Jorim Jaggic4025202015-10-22 16:43:34 +02003086 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3087 mTmpBounds.put(task.taskId, task.mBounds);
3088 }
3089 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3090 if (stack.mStackId == DOCKED_STACK_ID) {
3091 // Dock stack funness...Yay!
3092 if (stack.mFullscreen) {
3093 // The dock stack went fullscreen which is kinda like dismissing it.
3094 // In this case we make all other static stacks fullscreen and move all
3095 // docked stack tasks to the fullscreen stack.
3096 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003097 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003098 resizeStackLocked(i, null, preserveWindows, true);
3099 }
3100 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003101
Jorim Jaggic4025202015-10-22 16:43:34 +02003102 final int count = tasks.size();
3103 for (int i = 0; i < count; i++) {
3104 moveTaskToStackLocked(tasks.get(i).taskId,
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003105 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack");
Jorim Jaggic4025202015-10-22 16:43:34 +02003106 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003107
Jorim Jaggic4025202015-10-22 16:43:34 +02003108 // stack shouldn't contain anymore activities, so nothing to resume.
3109 r = null;
3110 } else {
3111 // Docked stacks occupy a dedicated region on screen so the size of all other
3112 // static stacks need to be adjusted so they don't overlap with the docked stack.
3113 // We get the bounds to use from window manager which has been adjusted for any
3114 // screen controls and is also the same for all stacks.
3115 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3116
3117 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003118 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003119 ActivityStack otherStack = getStack(i);
3120 if (otherStack != null) {
3121 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3122 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003123 }
3124 }
3125 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003126 // Since we are resizing the stack, all other operations should strive to preserve
3127 // windows.
3128 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003129 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003130 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003131
Jorim Jaggic4025202015-10-22 16:43:34 +02003132 if (r != null) {
3133 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3134 // And we need to make sure at this point that all other activities
3135 // are made visible with the correct configuration.
3136 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3137 if (!updated) {
3138 resumeTopActivitiesLocked(stack, null, null);
3139 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003140 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003141 } finally {
3142 mWindowManager.continueSurfaceLayout();
3143 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003144 }
3145 }
3146
Chong Zhang6de2ae82015-09-30 18:25:21 -07003147 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003148 if (!task.mResizeable) {
3149 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3150 return;
3151 }
3152
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003153 adjustForMinimalTaskDimensions(task, bounds);
3154
Chong Zhang87b21722015-09-21 15:39:51 -07003155 // If this is a forced resize, let it go through even if the bounds is not changing,
3156 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003157 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003158 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003159 // Nothing to do here...
3160 return;
3161 }
3162
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003163 if (!mWindowManager.isValidTaskId(task.taskId)) {
3164 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003165 // All we can do for now is update the bounds so it can be used when the task is
3166 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003167 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003168 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3169 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003170 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003171 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003172 return;
3173 }
3174
Chong Zhang6de2ae82015-09-30 18:25:21 -07003175 // Do not move the task to another stack here.
3176 // This method assumes that the task is already placed in the right stack.
3177 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003178
Chong Zhang6de2ae82015-09-30 18:25:21 -07003179 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003180
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003181 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003182 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003183 // way and the activity was kept the way it was. If it's false, it means the activity had
3184 // to be relaunched due to configuration change.
3185 boolean kept = true;
3186 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003187 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003188 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003189 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003190 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003191 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003192 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003193 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003194 resumeTopActivitiesLocked(stack, null, null);
3195 }
3196 }
3197 }
Chong Zhang87b21722015-09-21 15:39:51 -07003198 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003199
3200 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003201 }
3202
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003203 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3204 if (bounds == null) {
3205 return;
3206 }
3207 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3208 : task.mMinimalSize;
3209 final boolean adjustWidth = minimalSize > bounds.width();
3210 final boolean adjustHeight = minimalSize > bounds.height();
3211 if (!(adjustWidth || adjustHeight)) {
3212 return;
3213 }
3214 Rect taskBounds = task.mBounds;
3215 if (adjustWidth) {
3216 if (taskBounds != null && bounds.right == taskBounds.right) {
3217 bounds.left = bounds.right - minimalSize;
3218 } else {
3219 // Either left bounds match, or neither match, or the previous bounds were
3220 // fullscreen and we default to keeping left.
3221 bounds.right = bounds.left + minimalSize;
3222 }
3223 }
3224 if (adjustHeight) {
3225 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3226 bounds.top = bounds.bottom - minimalSize;
3227 } else {
3228 // Either top bounds match, or neither match, or the previous bounds were
3229 // fullscreen and we default to keeping top.
3230 bounds.bottom = bounds.top + minimalSize;
3231 }
3232 }
3233 }
3234
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003235 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003236 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3237 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003238 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003239 }
3240
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003241 ActivityContainer activityContainer = new ActivityContainer(stackId);
3242 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003243 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003244 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003245 }
3246
3247 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003248 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003249 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3250 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003251 break;
3252 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003253 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003254 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003255 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003256 }
3257
Chong Zhang5dcb2752015-08-18 13:50:26 -07003258 /**
3259 * Restores a recent task to a stack
3260 * @param task The recent task to be restored.
3261 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003262 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003263 * @return true if the task has been restored successfully.
3264 */
3265 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3266 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003267 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003268 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003269 if (task.stack != null) {
3270 // Task has already been restored once. See if we need to do anything more
3271 if (task.stack.mStackId == stackId) {
3272 // Nothing else to do since it is already restored in the right stack.
3273 return true;
3274 }
3275 // Remove current stack association, so we can re-associate the task with the
3276 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003277 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003278 }
3279
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003280 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003281 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003282
3283 if (stack == null) {
3284 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003285 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3286 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003287 return false;
3288 }
3289
3290 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003291 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3292 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003293 final ArrayList<ActivityRecord> activities = task.mActivities;
3294 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003295 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003296 }
3297 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003298 }
3299
Wale Ogunwale040b4702015-08-06 18:10:50 -07003300 /**
3301 * Moves the specified task record to the input stack id.
3302 * WARNING: This method performs an unchecked/raw move of the task and
3303 * can leave the system in an unstable state if used incorrectly.
3304 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3305 * to a stack.
3306 * @param task Task to move.
3307 * @param stackId Id of stack to move task to.
3308 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003309 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003310 * @param reason Reason the task is been moved.
3311 * @return The stack the task was moved to.
3312 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003313 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003314 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3315 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003316 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003317 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3318
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003319 final boolean resizeable = task.mResizeable;
3320 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3321 // if a docked stack is created below which will lead to the stack we are moving from and
3322 // its resizeable tasks being resized.
3323 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003324 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003325 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003326 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3327 if (task.stack != null) {
3328 task.stack.removeTask(task, reason, MOVING);
3329 }
3330 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003331
3332 // If the task had focus before (or we're requested to move focus),
3333 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003334 stack.setFocusAndResumeStateIfNeeded(
3335 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003336
Wale Ogunwale040b4702015-08-06 18:10:50 -07003337 return stack;
3338 }
3339
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003340 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
3341 String reason) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003342 final TaskRecord task = anyTaskForIdLocked(taskId);
3343 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003344 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003345 return;
3346 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003347
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003348 if (task.stack != null && task.stack.mStackId == stackId) {
3349 // You are already in the right stack silly...
3350 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
3351 return;
3352 }
3353
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003354 final ActivityRecord topActivity = task.getTopActivity();
3355 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003356 // We are about to relaunch the activity because its configuration changed due to
3357 // being maximized, i.e. size change. The activity will first remove the old window
3358 // and then add a new one. This call will tell window manager about this, so it can
3359 // preserve the old window until the new one is drawn. This prevents having a gap
3360 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003361 // entrance of the new window to be properly animated.
Wale Ogunwale3cd48042015-11-16 13:01:41 -08003362 mWindowManager.setReplacingWindow(topActivity.appToken);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003363 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08003364 final ActivityStack stack = moveTaskToStackUncheckedLocked(
3365 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003366
3367 // Make sure the task has the appropriate bounds/size for the stack it is in.
3368 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003369 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003370 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3371 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003372 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3373 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003374 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003375 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003376 }
3377
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003378 // The task might have already been running and its visibility needs to be synchronized with
3379 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003380 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003381 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003382 }
3383
Wale Ogunwale079a0042015-10-24 11:44:07 -07003384 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3385 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3386 if (stack == null) {
3387 throw new IllegalArgumentException(
3388 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3389 }
3390
3391 final ActivityRecord r = stack.topRunningActivityLocked();
3392 if (r == null) {
3393 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3394 + " in stack=" + stack);
3395 return false;
3396 }
3397
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003398 if (!r.info.supportsPip) {
3399 Slog.w(TAG,
3400 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003401 + " r=" + r);
3402 return false;
3403 }
3404
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003405 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003406 if (task.mActivities.size() == 1) {
3407 // There is only one activity in the task. So, we can just move the task over to the
3408 // pinned stack without re-parenting the activity in a different task.
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003409 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS,
3410 "moveTopActivityToPinnedStack");
Wale Ogunwale079a0042015-10-24 11:44:07 -07003411 } else {
3412 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3413 pinnedStack.moveActivityToStack(r);
3414 }
3415
3416 resizeStackLocked(PINNED_STACK_ID, bounds, PRESERVE_WINDOWS, true);
3417
3418 // The task might have already been running and its visibility needs to be synchronized with
3419 // the visibility of the stack / windows.
3420 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3421 resumeTopActivitiesLocked();
3422 return true;
3423 }
3424
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003425 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3426 final TaskRecord task = anyTaskForIdLocked(taskId);
3427 if (task == null) {
3428 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3429 return;
3430 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08003431 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
3432
3433 task.updateOverrideConfigurationForStack(stack);
3434
3435 mWindowManager.positionTaskInStack(
3436 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003437 final boolean stackChanged = task.stack != null && task.stack != stack;
3438 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003439 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003440 }
3441 stack.positionTask(task, position, stackChanged);
3442 // The task might have already been running and its visibility needs to be synchronized with
3443 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003444 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003445 resumeTopActivitiesLocked();
3446 }
3447
Craig Mautnerac6f8432013-07-17 13:24:59 -07003448 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003449 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003450 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3451 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003452 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3453 final ActivityStack stack = stacks.get(stackNdx);
3454 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003455 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003456 continue;
3457 }
3458 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003459 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3460 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003461 continue;
3462 }
3463 final ActivityRecord ar = stack.findTaskLocked(r);
3464 if (ar != null) {
3465 return ar;
3466 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003467 }
3468 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003469 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003470 return null;
3471 }
3472
3473 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003474 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3475 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003476 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3477 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3478 if (ar != null) {
3479 return ar;
3480 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003481 }
3482 }
3483 return null;
3484 }
3485
Craig Mautner8d341ef2013-03-26 09:03:27 -07003486 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003487 scheduleSleepTimeout();
3488 if (!mGoingToSleep.isHeld()) {
3489 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003490 if (mLaunchingActivity.isHeld()) {
3491 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3492 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003493 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003494 mLaunchingActivity.release();
3495 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003496 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003497 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003498 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003499 }
3500
3501 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003502 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003503
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003504 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003505 final long endTime = System.currentTimeMillis() + timeout;
3506 while (true) {
3507 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003508 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3509 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003510 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3511 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3512 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003513 }
3514 if (cantShutdown) {
3515 long timeRemaining = endTime - System.currentTimeMillis();
3516 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003517 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003518 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003519 } catch (InterruptedException e) {
3520 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003521 } else {
3522 Slog.w(TAG, "Activity manager shutdown timed out");
3523 timedout = true;
3524 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003525 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003526 } else {
3527 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003528 }
3529 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003530
3531 // Force checkReadyForSleep to complete.
3532 mSleepTimeout = true;
3533 checkReadyForSleepLocked();
3534
Craig Mautner8d341ef2013-03-26 09:03:27 -07003535 return timedout;
3536 }
3537
3538 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003539 removeSleepTimeouts();
3540 if (mGoingToSleep.isHeld()) {
3541 mGoingToSleep.release();
3542 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003543 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3544 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003545 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3546 final ActivityStack stack = stacks.get(stackNdx);
3547 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003548 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003549 resumeTopActivitiesLocked();
3550 }
Craig Mautner5314a402013-09-26 12:40:16 -07003551 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003552 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003553 mGoingToSleepActivities.clear();
3554 }
3555
3556 void activitySleptLocked(ActivityRecord r) {
3557 mGoingToSleepActivities.remove(r);
3558 checkReadyForSleepLocked();
3559 }
3560
3561 void checkReadyForSleepLocked() {
3562 if (!mService.isSleepingOrShuttingDown()) {
3563 // Do not care.
3564 return;
3565 }
3566
3567 if (!mSleepTimeout) {
3568 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003569 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3570 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003571 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3572 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3573 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003574 }
3575
3576 if (mStoppingActivities.size() > 0) {
3577 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003578 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003579 + mStoppingActivities.size() + " activities");
3580 scheduleIdleLocked();
3581 dontSleep = true;
3582 }
3583
3584 if (mGoingToSleepActivities.size() > 0) {
3585 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003586 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003587 + mGoingToSleepActivities.size() + " activities");
3588 dontSleep = true;
3589 }
3590
3591 if (dontSleep) {
3592 return;
3593 }
3594 }
3595
Craig Mautnere0a38842013-12-16 16:14:02 -08003596 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3597 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003598 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3599 stacks.get(stackNdx).goToSleep();
3600 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003601 }
3602
3603 removeSleepTimeouts();
3604
3605 if (mGoingToSleep.isHeld()) {
3606 mGoingToSleep.release();
3607 }
3608 if (mService.mShuttingDown) {
3609 mService.notifyAll();
3610 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003611 }
3612
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003613 boolean reportResumedActivityLocked(ActivityRecord r) {
3614 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003615 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003616 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003617 }
3618 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003619 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003620 mWindowManager.executeAppTransition();
3621 return true;
3622 }
3623 return false;
3624 }
3625
Craig Mautner8d341ef2013-03-26 09:03:27 -07003626 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003627 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3628 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003629 int stackNdx = stacks.size() - 1;
3630 while (stackNdx >= 0) {
3631 stacks.get(stackNdx).handleAppCrashLocked(app);
3632 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003633 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003634 }
3635 }
3636
Jose Lima4b6c6692014-08-12 17:41:12 -07003637 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003638 final ActivityStack stack = r.task.stack;
3639 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003640 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3641 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003642 return false;
3643 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003644 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003645 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3646 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003647
3648 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003649 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003650 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003651 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003652 return true;
3653 }
3654
3655 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003656 if (visible && top.fullscreen) {
3657 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003658 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3659 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3660 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3661 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003662 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003663 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3664 // Only the activity set as currently visible behind should actively reset its
3665 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003666 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3667 "requestVisibleBehind: returning visible=" + visible
3668 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3669 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003670 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003671 }
3672
Jose Lima4b6c6692014-08-12 17:41:12 -07003673 stack.setVisibleBehindActivity(visible ? r : null);
3674 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003675 // Make the activity immediately above r opaque.
3676 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3677 if (next != null) {
3678 mService.convertFromTranslucent(next.appToken);
3679 }
3680 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003681 if (top.app != null && top.app.thread != null) {
3682 // Notify the top app of the change.
3683 try {
3684 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3685 } catch (RemoteException e) {
3686 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003687 }
3688 return true;
3689 }
3690
Craig Mautnerbb742462014-07-07 15:28:55 -07003691 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3692 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3693 r.mLaunchTaskBehind = false;
3694 final TaskRecord task = r.task;
Winsonc809cbb2015-11-02 12:06:15 -08003695 task.setLastThumbnailLocked(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003696 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003697 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003698 mWindowManager.setAppVisibility(r.appToken, false);
3699 }
3700
3701 void scheduleLaunchTaskBehindComplete(IBinder token) {
3702 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3703 }
3704
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003705 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3706 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003707 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003708 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3709 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003710 final int topStackNdx = stacks.size() - 1;
3711 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3712 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003713 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003714 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003715 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003716 }
3717
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003718 void invalidateTaskLayers() {
3719 mTaskLayersChanged = true;
3720 }
3721
3722 void rankTaskLayersIfNeeded() {
3723 if (!mTaskLayersChanged) {
3724 return;
3725 }
3726 mTaskLayersChanged = false;
3727 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3728 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3729 int baseLayer = 0;
3730 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3731 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3732 }
3733 }
3734 }
3735
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003736 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3739 final int topStackNdx = stacks.size() - 1;
3740 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3741 final ActivityStack stack = stacks.get(stackNdx);
3742 stack.clearOtherAppTimeTrackers(except);
3743 }
3744 }
3745 }
3746
Craig Mautner8d341ef2013-03-26 09:03:27 -07003747 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003748 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3749 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003750 final int numStacks = stacks.size();
3751 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3752 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003753 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003754 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003755 }
3756 }
3757
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003758 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3759 // Examine all activities currently running in the process.
3760 TaskRecord firstTask = null;
3761 // Tasks is non-null only if two or more tasks are found.
3762 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003763 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3764 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003765 ActivityRecord r = app.activities.get(i);
3766 // First, if we find an activity that is in the process of being destroyed,
3767 // then we just aren't going to do anything for now; we want things to settle
3768 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003769 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003770 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003771 return;
3772 }
3773 // Don't consider any activies that are currently not in a state where they
3774 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003775 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3776 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003777 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003778 continue;
3779 }
3780 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003781 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003782 + " from " + r);
3783 if (firstTask == null) {
3784 firstTask = r.task;
3785 } else if (firstTask != r.task) {
3786 if (tasks == null) {
3787 tasks = new ArraySet<>();
3788 tasks.add(firstTask);
3789 }
3790 tasks.add(r.task);
3791 }
3792 }
3793 }
3794 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003795 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003796 return;
3797 }
3798 // If we have activities in multiple tasks that are in a position to be destroyed,
3799 // let's iterate through the tasks and release the oldest one.
3800 final int numDisplays = mActivityDisplays.size();
3801 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3802 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3803 // Step through all stacks starting from behind, to hit the oldest things first.
3804 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3805 final ActivityStack stack = stacks.get(stackNdx);
3806 // Try to release activities in this stack; if we manage to, we are done.
3807 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3808 return;
3809 }
3810 }
3811 }
3812 }
3813
Amith Yamasani37a40c22015-06-17 13:25:42 -07003814 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003815 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003816 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003817 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003818
Craig Mautner858d8a62013-04-23 17:08:34 -07003819 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003820 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3821 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003822 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003823 final ActivityStack stack = stacks.get(stackNdx);
3824 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003825 TaskRecord task = stack.topTask();
3826 if (task != null) {
3827 mWindowManager.moveTaskToTop(task.taskId);
3828 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003829 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003830 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003831
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003832 ActivityStack stack = getStack(restoreStackId);
3833 if (stack == null) {
3834 stack = mHomeStack;
3835 }
3836 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003837 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003838 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003839 } else {
3840 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003841 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003842 }
Craig Mautner93529a42013-10-04 15:03:13 -07003843 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003844 }
3845
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003846 /**
3847 * Add background users to send boot completed events to.
3848 * @param userId The user being started in the background
3849 * @param uss The state object for the user.
3850 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003851 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003852 mStartingBackgroundUsers.add(uss);
3853 }
3854
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003855 /** Checks whether the userid is a profile of the current user. */
3856 boolean isCurrentProfileLocked(int userId) {
3857 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003858 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003859 }
3860
Chong Zhang45c25ce2015-08-10 22:18:26 -07003861 /** Checks whether the activity should be shown for current user. */
3862 boolean okToShowLocked(ActivityRecord r) {
3863 return r != null && (isCurrentProfileLocked(r.userId)
3864 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3865 }
3866
Craig Mautnerde4ef022013-04-07 19:01:33 -07003867 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003868 ArrayList<ActivityRecord> stops = null;
3869
3870 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003871 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3872 ActivityRecord s = mStoppingActivities.get(activityNdx);
3873 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003874 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003875 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3876 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003877 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003878 if (s.finishing) {
3879 // If this activity is finishing, it is sitting on top of
3880 // everyone else but we now know it is no longer needed...
3881 // so get rid of it. Otherwise, we need to go through the
3882 // normal flow and hide it once we determine that it is
3883 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003884 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003885 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003886 }
3887 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003888 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003889 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003890 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003891 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003892 }
3893 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003894 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003895 }
3896 }
3897
3898 return stops;
3899 }
3900
Craig Mautnercf910b02013-04-23 11:23:27 -07003901 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003902 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3903 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3904 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3905 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003906 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003907 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003908 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003909 if (r == null) Slog.e(TAG,
3910 "validateTop...: null top activity, stack=" + stack);
3911 else {
3912 final ActivityRecord pausing = stack.mPausingActivity;
3913 if (pausing != null && pausing == r) Slog.e(TAG,
3914 "validateTop...: top stack has pausing activity r=" + r
3915 + " state=" + state);
3916 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3917 "validateTop...: activity in front not resumed r=" + r
3918 + " state=" + state);
3919 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003920 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003921 final ActivityRecord resumed = stack.mResumedActivity;
3922 if (resumed != null && resumed == r) Slog.e(TAG,
3923 "validateTop...: back stack has resumed activity r=" + r
3924 + " state=" + state);
3925 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3926 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003927 }
3928 }
3929 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003930 }
3931
Craig Mautnere0570202015-05-13 13:06:11 -07003932 private String lockTaskModeToString() {
3933 switch (mLockTaskModeState) {
3934 case LOCK_TASK_MODE_LOCKED:
3935 return "LOCKED";
3936 case LOCK_TASK_MODE_PINNED:
3937 return "PINNED";
3938 case LOCK_TASK_MODE_NONE:
3939 return "NONE";
3940 default: return "unknown=" + mLockTaskModeState;
3941 }
3942 }
3943
Craig Mautner27084302013-03-25 08:05:25 -07003944 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003945 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003946 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003947 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3948 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3949 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003950 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003951 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3952 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3953 if (packages.size() > 0) {
3954 pw.println(" mLockTaskPackages (userId:packages)=");
3955 for (int i = 0; i < packages.size(); ++i) {
3956 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3957 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3958 }
3959 }
3960 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003961 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003962
Craig Mautner20e72272013-04-01 13:45:53 -07003963 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003964 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003965 }
3966
Dianne Hackborn390517b2013-05-30 15:03:32 -07003967 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3968 boolean needSep, String prefix) {
3969 if (activity != null) {
3970 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3971 if (needSep) {
3972 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003973 }
3974 pw.print(prefix);
3975 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003976 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003977 }
3978 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003979 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003980 }
3981
Craig Mautner8d341ef2013-03-26 09:03:27 -07003982 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3983 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003984 boolean printed = false;
3985 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003986 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3987 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003988 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003989 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003990 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003991 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003992 final ActivityStack stack = stacks.get(stackNdx);
3993 StringBuilder stackHeader = new StringBuilder(128);
3994 stackHeader.append(" Stack #");
3995 stackHeader.append(stack.mStackId);
3996 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003997 stackHeader.append("\n");
3998 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3999 stackHeader.append("\n");
4000 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004001 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4002 needSep, stackHeader.toString());
4003 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4004 !dumpAll, false, dumpPackage, true,
4005 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004006
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 needSep = printed;
4008 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4009 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004010 if (pr) {
4011 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004013 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004014 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
4015 " mResumedActivity: ");
4016 if (pr) {
4017 printed = true;
4018 needSep = false;
4019 }
4020 if (dumpAll) {
4021 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4022 " mLastPausedActivity: ");
4023 if (pr) {
4024 printed = true;
4025 needSep = true;
4026 }
4027 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4028 needSep, " mLastNoHistoryActivity: ");
4029 }
4030 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004031 }
4032 }
4033
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004034 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4035 false, dumpPackage, true, " Activities waiting to finish:", null);
4036 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4037 false, dumpPackage, true, " Activities waiting to stop:", null);
4038 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
4039 false, dumpPackage, true, " Activities waiting for another to become visible:",
4040 null);
4041 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4042 false, dumpPackage, true, " Activities waiting to sleep:", null);
4043 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4044 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004045
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004046 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004047 }
4048
Dianne Hackborn390517b2013-05-30 15:03:32 -07004049 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004050 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004051 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004052 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004053 String innerPrefix = null;
4054 String[] args = null;
4055 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004056 for (int i=list.size()-1; i>=0; i--) {
4057 final ActivityRecord r = list.get(i);
4058 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4059 continue;
4060 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004061 if (innerPrefix == null) {
4062 innerPrefix = prefix + " ";
4063 args = new String[0];
4064 }
4065 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004066 final boolean full = !brief && (complete || !r.isInHistory());
4067 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004068 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004069 needNL = false;
4070 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004071 if (header1 != null) {
4072 pw.println(header1);
4073 header1 = null;
4074 }
4075 if (header2 != null) {
4076 pw.println(header2);
4077 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004078 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004079 if (lastTask != r.task) {
4080 lastTask = r.task;
4081 pw.print(prefix);
4082 pw.print(full ? "* " : " ");
4083 pw.println(lastTask);
4084 if (full) {
4085 lastTask.dump(pw, prefix + " ");
4086 } else if (complete) {
4087 // Complete + brief == give a summary. Isn't that obvious?!?
4088 if (lastTask.intent != null) {
4089 pw.print(prefix); pw.print(" ");
4090 pw.println(lastTask.intent.toInsecureStringWithClip());
4091 }
4092 }
4093 }
4094 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4095 pw.print(" #"); pw.print(i); pw.print(": ");
4096 pw.println(r);
4097 if (full) {
4098 r.dump(pw, innerPrefix);
4099 } else if (complete) {
4100 // Complete + brief == give a summary. Isn't that obvious?!?
4101 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4102 if (r.app != null) {
4103 pw.print(innerPrefix); pw.println(r.app);
4104 }
4105 }
4106 if (client && r.app != null && r.app.thread != null) {
4107 // flush anything that is already in the PrintWriter since the thread is going
4108 // to write to the file descriptor directly
4109 pw.flush();
4110 try {
4111 TransferPipe tp = new TransferPipe();
4112 try {
4113 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4114 r.appToken, innerPrefix, args);
4115 // Short timeout, since blocking here can
4116 // deadlock with the application.
4117 tp.go(fd, 2000);
4118 } finally {
4119 tp.kill();
4120 }
4121 } catch (IOException e) {
4122 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4123 } catch (RemoteException e) {
4124 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4125 }
4126 needNL = true;
4127 }
4128 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004129 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004130 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004131
Craig Mautnerf3333272013-04-22 10:55:53 -07004132 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004133 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4134 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004135 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4136 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004137 }
4138
4139 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004140 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004141 }
4142
4143 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004144 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4145 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004146 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4147 }
4148
Craig Mautner05d29032013-05-03 13:40:13 -07004149 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004150 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4151 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4152 }
Craig Mautner05d29032013-05-03 13:40:13 -07004153 }
4154
Craig Mautner0eea92c2013-05-16 13:35:39 -07004155 void removeSleepTimeouts() {
4156 mSleepTimeout = false;
4157 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4158 }
4159
4160 final void scheduleSleepTimeout() {
4161 removeSleepTimeouts();
4162 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4163 }
4164
Craig Mautner4a1cb222013-12-04 16:14:06 -08004165 @Override
4166 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004167 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004168 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4169 }
4170
4171 @Override
4172 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004173 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004174 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4175 }
4176
4177 @Override
4178 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004179 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004180 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4181 }
4182
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004183 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004184 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004185 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004186 newDisplay = mActivityDisplays.get(displayId) == null;
4187 if (newDisplay) {
4188 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004189 if (activityDisplay.mDisplay == null) {
4190 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4191 return;
4192 }
Craig Mautner4504de52013-12-20 09:06:56 -08004193 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004194 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004195 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004196 }
Craig Mautner4504de52013-12-20 09:06:56 -08004197 if (newDisplay) {
4198 mWindowManager.onDisplayAdded(displayId);
4199 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004200 }
4201
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004202 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4203 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4204 return;
4205 }
4206 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4207 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4208 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4209 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4210 }
4211
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004212 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004213 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004214 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4215 if (activityDisplay != null) {
4216 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004217 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004218 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004219 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004220 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004221 }
4222 }
4223 mWindowManager.onDisplayRemoved(displayId);
4224 }
4225
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004226 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004227 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004228 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4229 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004230 // TODO: Update the bounds.
4231 }
4232 }
4233 mWindowManager.onDisplayChanged(displayId);
4234 }
4235
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004236 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004237 StackInfo info = new StackInfo();
4238 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4239 info.displayId = Display.DEFAULT_DISPLAY;
4240 info.stackId = stack.mStackId;
4241
4242 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4243 final int numTasks = tasks.size();
4244 int[] taskIds = new int[numTasks];
4245 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004246 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004247 for (int i = 0; i < numTasks; ++i) {
4248 final TaskRecord task = tasks.get(i);
4249 taskIds[i] = task.taskId;
4250 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4251 : task.realActivity != null ? task.realActivity.flattenToString()
4252 : task.getTopActivity() != null ? task.getTopActivity().packageName
4253 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004254 taskBounds[i] = new Rect();
4255 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004256 }
4257 info.taskIds = taskIds;
4258 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004259 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004260 return info;
4261 }
4262
4263 StackInfo getStackInfoLocked(int stackId) {
4264 ActivityStack stack = getStack(stackId);
4265 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004266 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004267 }
4268 return null;
4269 }
4270
4271 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004272 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004273 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4274 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004275 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004276 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004277 }
4278 }
4279 return list;
4280 }
4281
Craig Mautner15df08a2015-04-01 12:17:18 -07004282 TaskRecord getLockedTaskLocked() {
4283 final int top = mLockTaskModeTasks.size() - 1;
4284 if (top >= 0) {
4285 return mLockTaskModeTasks.get(top);
4286 }
4287 return null;
4288 }
4289
4290 boolean isLockedTask(TaskRecord task) {
4291 return mLockTaskModeTasks.contains(task);
4292 }
4293
4294 boolean isLastLockedTask(TaskRecord task) {
4295 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4296 }
4297
4298 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004299 if (!mLockTaskModeTasks.remove(task)) {
4300 return;
4301 }
4302 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4303 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004304 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004305 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4306 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004307 final Message lockTaskMsg = Message.obtain();
4308 lockTaskMsg.arg1 = task.userId;
4309 lockTaskMsg.what = LOCK_TASK_END_MSG;
4310 mHandler.sendMessage(lockTaskMsg);
4311 }
4312 }
4313
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004314 void showLockTaskToast() {
4315 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004316 }
4317
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004318 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4319 if (mLockTaskModeTasks.contains(task)) {
4320 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4321 }
4322 }
4323
Craig Mautner432f64e2015-05-20 14:59:57 -07004324 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4325 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004326 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004327 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004328 final TaskRecord lockedTask = getLockedTaskLocked();
4329 if (lockedTask != null) {
4330 removeLockedTaskLocked(lockedTask);
4331 if (!mLockTaskModeTasks.isEmpty()) {
4332 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004333 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4334 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004335 lockedTask.performClearTaskLocked();
4336 resumeTopActivitiesLocked();
4337 return;
4338 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004339 }
Craig Mautnere0570202015-05-13 13:06:11 -07004340 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4341 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004342 return;
4343 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004344
4345 // Should have already been checked, but do it again.
4346 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004347 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4348 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004349 return;
4350 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004351 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004352 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004353 return;
4354 }
4355
4356 if (mLockTaskModeTasks.isEmpty()) {
4357 // First locktask.
4358 final Message lockTaskMsg = Message.obtain();
4359 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4360 lockTaskMsg.arg1 = task.userId;
4361 lockTaskMsg.what = LOCK_TASK_START_MSG;
4362 lockTaskMsg.arg2 = lockTaskModeState;
4363 mHandler.sendMessage(lockTaskMsg);
4364 }
4365 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004366 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4367 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004368 mLockTaskModeTasks.remove(task);
4369 mLockTaskModeTasks.add(task);
4370
4371 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004372 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004373 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004374
4375 if (andResume) {
4376 findTaskToMoveToFrontLocked(task, 0, null, reason);
4377 resumeTopActivitiesLocked();
4378 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004379 }
4380
4381 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004382 return isLockTaskModeViolation(task, false);
4383 }
4384
4385 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4386 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004387 return false;
4388 }
4389 final int lockTaskAuth = task.mLockTaskAuth;
4390 switch (lockTaskAuth) {
4391 case LOCK_TASK_AUTH_DONT_LOCK:
4392 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004393 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004394 case LOCK_TASK_AUTH_LAUNCHABLE:
4395 case LOCK_TASK_AUTH_WHITELISTED:
4396 return false;
4397 case LOCK_TASK_AUTH_PINNABLE:
4398 // Pinnable tasks can't be launched on top of locktask tasks.
4399 return !mLockTaskModeTasks.isEmpty();
4400 default:
4401 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4402 return true;
4403 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004404 }
4405
Craig Mautner15df08a2015-04-01 12:17:18 -07004406 void onLockTaskPackagesUpdatedLocked() {
4407 boolean didSomething = false;
4408 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4409 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004410 final boolean wasWhitelisted =
4411 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4412 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004413 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004414 final boolean isWhitelisted =
4415 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4416 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4417 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004418 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004419 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4420 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004421 removeLockedTaskLocked(lockedTask);
4422 lockedTask.performClearTaskLocked();
4423 didSomething = true;
4424 }
4425 }
4426 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4427 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4428 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4429 final ActivityStack stack = stacks.get(stackNdx);
4430 stack.onLockTaskPackagesUpdatedLocked();
4431 }
4432 }
Craig Mautnere0570202015-05-13 13:06:11 -07004433 final ActivityRecord r = topRunningActivityLocked();
4434 final TaskRecord task = r != null ? r.task : null;
4435 if (mLockTaskModeTasks.isEmpty() && task != null
4436 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4437 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004438 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4439 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4440 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4441 false);
4442 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004443 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004444 if (didSomething) {
4445 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004446 }
4447 }
4448
Benjamin Franz43261142015-02-11 15:59:44 +00004449 int getLockTaskModeState() {
4450 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004451 }
4452
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004453 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004454
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004455 public ActivityStackSupervisorHandler(Looper looper) {
4456 super(looper);
4457 }
4458
Craig Mautnerf3333272013-04-22 10:55:53 -07004459 void activityIdleInternal(ActivityRecord r) {
4460 synchronized (mService) {
4461 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4462 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004463 }
4464
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004465 @Override
4466 public void handleMessage(Message msg) {
4467 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004468 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004469 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4470 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004471 if (mService.mDidDexOpt) {
4472 mService.mDidDexOpt = false;
4473 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4474 nmsg.obj = msg.obj;
4475 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4476 return;
4477 }
4478 // We don't at this point know if the activity is fullscreen,
4479 // so we need to be conservative and assume it isn't.
4480 activityIdleInternal((ActivityRecord)msg.obj);
4481 } break;
4482 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004483 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004484 activityIdleInternal((ActivityRecord)msg.obj);
4485 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004486 case RESUME_TOP_ACTIVITY_MSG: {
4487 synchronized (mService) {
4488 resumeTopActivitiesLocked();
4489 }
4490 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004491 case SLEEP_TIMEOUT_MSG: {
4492 synchronized (mService) {
4493 if (mService.isSleepingOrShuttingDown()) {
4494 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4495 mSleepTimeout = true;
4496 checkReadyForSleepLocked();
4497 }
4498 }
4499 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004500 case LAUNCH_TIMEOUT_MSG: {
4501 if (mService.mDidDexOpt) {
4502 mService.mDidDexOpt = false;
4503 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4504 return;
4505 }
4506 synchronized (mService) {
4507 if (mLaunchingActivity.isHeld()) {
4508 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4509 if (VALIDATE_WAKE_LOCK_CALLER
4510 && Binder.getCallingUid() != Process.myUid()) {
4511 throw new IllegalStateException("Calling must be system uid");
4512 }
4513 mLaunchingActivity.release();
4514 }
4515 }
4516 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004517 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004518 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004519 } break;
4520 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004521 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004522 } break;
4523 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004524 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004525 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004526 case CONTAINER_CALLBACK_VISIBILITY: {
4527 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004528 final IActivityContainerCallback callback = container.mCallback;
4529 if (callback != null) {
4530 try {
4531 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4532 } catch (RemoteException e) {
4533 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004534 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004535 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004536 case LOCK_TASK_START_MSG: {
4537 // When lock task starts, we disable the status bars.
4538 try {
Jason Monk62515be2014-05-21 16:06:19 -04004539 if (mLockTaskNotify == null) {
4540 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004541 }
Jason Monk62515be2014-05-21 16:06:19 -04004542 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004543 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004544 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004545 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004546 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004547 flags = StatusBarManager.DISABLE_MASK
4548 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004549 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004550 flags = StatusBarManager.DISABLE_MASK
4551 & (~StatusBarManager.DISABLE_BACK)
4552 & (~StatusBarManager.DISABLE_HOME)
4553 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004554 }
4555 getStatusBarService().disable(flags, mToken,
4556 mService.mContext.getPackageName());
4557 }
4558 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004559 if (getDevicePolicyManager() != null) {
4560 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004561 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004562 }
justinzhang5286d3f2014-05-12 17:06:01 -04004563 } catch (RemoteException ex) {
4564 throw new RuntimeException(ex);
4565 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004566 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004567 case LOCK_TASK_END_MSG: {
4568 // When lock task ends, we enable the status bars.
4569 try {
Jason Monk62515be2014-05-21 16:06:19 -04004570 if (getStatusBarService() != null) {
4571 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4572 mService.mContext.getPackageName());
4573 }
4574 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004575 if (getDevicePolicyManager() != null) {
4576 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4577 msg.arg1);
4578 }
Jason Monk62515be2014-05-21 16:06:19 -04004579 if (mLockTaskNotify == null) {
4580 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4581 }
4582 mLockTaskNotify.show(false);
4583 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004584 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004585 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004586 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004587 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004588 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004589 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004590 new LockPatternUtils(mService.mContext)
4591 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004592 }
4593 } catch (SettingNotFoundException e) {
4594 // No setting, don't lock.
4595 }
justinzhang5286d3f2014-05-12 17:06:01 -04004596 } catch (RemoteException ex) {
4597 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004598 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004599 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004600 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004601 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004602 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4603 if (mLockTaskNotify == null) {
4604 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4605 }
4606 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4607 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004608 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4609 final ActivityContainer container = (ActivityContainer) msg.obj;
4610 final IActivityContainerCallback callback = container.mCallback;
4611 if (callback != null) {
4612 try {
4613 callback.onAllActivitiesComplete(container.asBinder());
4614 } catch (RemoteException e) {
4615 }
4616 }
4617 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004618 case LAUNCH_TASK_BEHIND_COMPLETE: {
4619 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004620 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004621 if (r != null) {
4622 handleLaunchTaskBehindCompleteLocked(r);
4623 }
4624 }
4625 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004626 }
4627 }
4628 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004629
Ying Wangb081a592014-04-22 15:20:16 -07004630 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004631 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004632 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004633 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004634 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004635 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004636 ActivityRecord mParentActivity = null;
4637 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004638
Craig Mautnere3a00d72014-04-16 08:31:19 -07004639 boolean mVisible = true;
4640
Craig Mautner4a1cb222013-12-04 16:14:06 -08004641 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004642 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004643
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004644 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4645 final static int CONTAINER_STATE_NO_SURFACE = 1;
4646 final static int CONTAINER_STATE_FINISHING = 2;
4647 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4648
4649 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004650 synchronized (mService) {
4651 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004652 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004653 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004654 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004655 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004656 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004657
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004658 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004659 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004660 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004661 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004662 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004663 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004664 }
4665
4666 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004667 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004668 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004669 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4670 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004671 return;
4672 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004673 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004674 }
4675 }
4676
4677 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004678 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004679 synchronized (mService) {
4680 if (mActivityDisplay != null) {
4681 return mActivityDisplay.mDisplayId;
4682 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004683 }
4684 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004685 }
4686
Jeff Brownca9bc702014-02-11 14:32:56 -08004687 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004688 public int getStackId() {
4689 synchronized (mService) {
4690 return mStackId;
4691 }
4692 }
4693
4694 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004695 public boolean injectEvent(InputEvent event) {
4696 final long origId = Binder.clearCallingIdentity();
4697 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004698 synchronized (mService) {
4699 if (mActivityDisplay != null) {
4700 return mInputManagerInternal.injectInputEvent(event,
4701 mActivityDisplay.mDisplayId,
4702 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4703 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004704 }
4705 return false;
4706 } finally {
4707 Binder.restoreCallingIdentity(origId);
4708 }
4709 }
4710
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004711 @Override
4712 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004713 synchronized (mService) {
4714 if (mContainerState == CONTAINER_STATE_FINISHING) {
4715 return;
4716 }
4717 mContainerState = CONTAINER_STATE_FINISHING;
4718
Craig Mautnerd163e752014-06-13 17:18:47 -07004719 long origId = Binder.clearCallingIdentity();
4720 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004721 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004722 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004723 } finally {
4724 Binder.restoreCallingIdentity(origId);
4725 }
4726 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004727 }
4728
Craig Mautner60257702014-09-17 15:02:33 -07004729 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004730 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004731 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004732 if (mActivityDisplay != null) {
4733 mActivityDisplay.detachActivitiesLocked(mStack);
4734 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004735 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004736 }
4737 }
4738
4739 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004740 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004741 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004742 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004743 Binder.getCallingUid(), mCurrentUser, false,
4744 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004745
Craig Mautnere0a38842013-12-16 16:14:02 -08004746 // TODO: Switch to user app stacks here.
4747 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004748 final Uri data = intent.getData();
4749 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4750 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004751 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004752 checkEmbeddedAllowedInner(userId, intent, mimeType);
4753
4754 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4755 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004756 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004757 }
4758
4759 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004760 public final int startActivityIntentSender(IIntentSender intentSender)
4761 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004762 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4763
4764 if (!(intentSender instanceof PendingIntentRecord)) {
4765 throw new IllegalArgumentException("Bad PendingIntent object");
4766 }
4767
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004768 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004769 Binder.getCallingUid(), mCurrentUser, false,
4770 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004771
4772 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4773 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4774 pendingIntent.key.requestResolvedType);
4775
4776 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4777 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4778 }
4779
4780 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004781 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004782 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004783 throw new SecurityException(
4784 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4785 }
4786 }
4787
Craig Mautnerdf88d732014-01-27 09:21:32 -08004788 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004789 public IBinder asBinder() {
4790 return this;
4791 }
4792
Craig Mautner4504de52013-12-20 09:06:56 -08004793 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004794 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004795 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004796 }
4797
Craig Mautner4a1cb222013-12-04 16:14:06 -08004798 ActivityStackSupervisor getOuter() {
4799 return ActivityStackSupervisor.this;
4800 }
4801
Craig Mautnerd163e752014-06-13 17:18:47 -07004802 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004803 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004804 }
4805
Craig Mautner6985bad2014-04-21 15:22:06 -07004806 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004807 void setVisible(boolean visible) {
4808 if (mVisible != visible) {
4809 mVisible = visible;
4810 if (mCallback != null) {
4811 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4812 0 /* unused */, this).sendToTarget();
4813 }
4814 }
4815 }
4816
Craig Mautner6985bad2014-04-21 15:22:06 -07004817 void setDrawn() {
4818 }
4819
Craig Mautner1b4bf852014-05-26 15:06:32 -07004820 // You can always start a new task on a regular ActivityStack.
4821 boolean isEligibleForNewTasks() {
4822 return true;
4823 }
4824
Craig Mautnerd163e752014-06-13 17:18:47 -07004825 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004826 detachLocked();
4827 deleteActivityContainer(this);
4828 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004829 }
4830
Craig Mautner34b73df2014-01-12 21:11:08 -08004831 @Override
4832 public String toString() {
4833 return mIdString + (mActivityDisplay == null ? "N" : "A");
4834 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004835 }
4836
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004837 private class VirtualActivityContainer extends ActivityContainer {
4838 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004839 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004840
4841 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4842 super(getNextStackId());
4843 mParentActivity = parent;
4844 mCallback = callback;
4845 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004846 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004847 }
4848
4849 @Override
4850 public void setSurface(Surface surface, int width, int height, int density) {
4851 super.setSurface(surface, width, height, density);
4852
4853 synchronized (mService) {
4854 final long origId = Binder.clearCallingIdentity();
4855 try {
4856 setSurfaceLocked(surface, width, height, density);
4857 } finally {
4858 Binder.restoreCallingIdentity(origId);
4859 }
4860 }
4861 }
4862
4863 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4864 if (mContainerState == CONTAINER_STATE_FINISHING) {
4865 return;
4866 }
4867 VirtualActivityDisplay virtualActivityDisplay =
4868 (VirtualActivityDisplay) mActivityDisplay;
4869 if (virtualActivityDisplay == null) {
4870 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004871 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004872 mActivityDisplay = virtualActivityDisplay;
4873 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004874 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004875 }
4876
4877 if (mSurface != null) {
4878 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004879 }
4880
Craig Mautner6985bad2014-04-21 15:22:06 -07004881 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004882 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004883 mStack.resumeTopActivityLocked(null);
4884 } else {
4885 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004886 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004887 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004888 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004889 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004890 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004891
Craig Mautnerd163e752014-06-13 17:18:47 -07004892 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004893
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004894 if (DEBUG_STACK) Slog.d(TAG_STACK,
4895 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004896 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004897
Craig Mautner6985bad2014-04-21 15:22:06 -07004898 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004899 boolean isAttachedLocked() {
4900 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004901 }
4902
4903 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004904 void setDrawn() {
4905 synchronized (mService) {
4906 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004907 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004908 }
4909 }
4910
Craig Mautner1b4bf852014-05-26 15:06:32 -07004911 // Never start a new task on an ActivityView if it isn't explicitly specified.
4912 @Override
4913 boolean isEligibleForNewTasks() {
4914 return false;
4915 }
4916
Craig Mautnerd163e752014-06-13 17:18:47 -07004917 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004918 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004919 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4920 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4921 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4922 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4923 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004924 }
4925 }
4926
Craig Mautner4a1cb222013-12-04 16:14:06 -08004927 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4928 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004929 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004930 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004931 int mDisplayId;
4932 Display mDisplay;
4933 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004934
Craig Mautner4a1cb222013-12-04 16:14:06 -08004935 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4936 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004937 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004938
Jose Lima4b6c6692014-08-12 17:41:12 -07004939 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004940
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004941 ActivityDisplay() {
4942 }
Craig Mautner4504de52013-12-20 09:06:56 -08004943
Craig Mautner1a70a162014-09-13 12:09:31 -07004944 // After instantiation, check that mDisplay is not null before using this. The alternative
4945 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004946 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004947 final Display display = mDisplayManager.getDisplay(displayId);
4948 if (display == null) {
4949 return;
4950 }
4951 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004952 }
4953
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004954 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004955 mDisplay = display;
4956 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004957 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004958 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004959
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004960 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004961 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004962 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4963 + " onTop=" + onTop);
4964 if (onTop) {
4965 mStacks.add(stack);
4966 } else {
4967 mStacks.add(0, stack);
4968 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004969 }
4970
Craig Mautnere0a38842013-12-16 16:14:02 -08004971 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004972 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004973 + " from displayId=" + mDisplayId);
4974 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004975 }
4976
Jose Lima4b6c6692014-08-12 17:41:12 -07004977 void setVisibleBehindActivity(ActivityRecord r) {
4978 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004979 }
4980
Jose Lima4b6c6692014-08-12 17:41:12 -07004981 boolean hasVisibleBehindActivity() {
4982 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004983 }
4984
Craig Mautner34b73df2014-01-12 21:11:08 -08004985 @Override
4986 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004987 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4988 }
4989 }
4990
4991 class VirtualActivityDisplay extends ActivityDisplay {
4992 VirtualDisplay mVirtualDisplay;
4993
Craig Mautner6985bad2014-04-21 15:22:06 -07004994 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004995 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004996 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4997 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4998 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4999 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005000
5001 init(mVirtualDisplay.getDisplay());
5002
5003 mWindowManager.handleDisplayAdded(mDisplayId);
5004 }
5005
5006 void setSurface(Surface surface) {
5007 if (mVirtualDisplay != null) {
5008 mVirtualDisplay.setSurface(surface);
5009 }
5010 }
5011
5012 @Override
5013 void detachActivitiesLocked(ActivityStack stack) {
5014 super.detachActivitiesLocked(stack);
5015 if (mVirtualDisplay != null) {
5016 mVirtualDisplay.release();
5017 mVirtualDisplay = null;
5018 }
5019 }
5020
5021 @Override
5022 public String toString() {
5023 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005024 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005025 }
Jose Lima58e66d62014-05-27 20:00:27 -07005026
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07005027 /**
5028 * Adjust bounds to stay within stack bounds.
5029 *
5030 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
5031 * that keep them unchanged, but be contained within the stack bounds.
5032 *
5033 * @param bounds Bounds to be adjusted.
5034 * @param stackBounds Bounds within which the other bounds should remain.
5035 */
5036 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
5037 if (stackBounds == null || stackBounds.contains(bounds)) {
5038 return;
5039 }
5040
5041 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
5042 final int maxRight = stackBounds.right
5043 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
5044 int horizontalDiff = stackBounds.left - bounds.left;
5045 if ((horizontalDiff < 0 && bounds.left >= maxRight)
5046 || (bounds.left + horizontalDiff >= maxRight)) {
5047 horizontalDiff = maxRight - bounds.left;
5048 }
5049 bounds.left += horizontalDiff;
5050 bounds.right += horizontalDiff;
5051 }
5052
5053 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
5054 final int maxBottom = stackBounds.bottom
5055 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
5056 int verticalDiff = stackBounds.top - bounds.top;
5057 if ((verticalDiff < 0 && bounds.top >= maxBottom)
5058 || (bounds.top + verticalDiff >= maxBottom)) {
5059 verticalDiff = maxBottom - bounds.top;
5060 }
5061 bounds.top += verticalDiff;
5062 bounds.bottom += verticalDiff;
5063 }
5064 }
5065
Craig Mautner27084302013-03-25 08:05:25 -07005066}