blob: 0ec4b18ff28a24b5e13d9ef3d009fc596fdc9159 [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;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100102import android.os.SystemProperties;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700103import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700104import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700105import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100106import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700107import android.os.WorkSource;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100108import android.os.storage.StorageManager;
Svetoslav7008b512015-06-24 18:47:07 -0700109import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400110import android.provider.Settings;
111import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700112import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700113import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700114import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700115import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700116import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800117import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118
Craig Mautner4a1cb222013-12-04 16:14:06 -0800119import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800120import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800121import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800122import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800123import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700124import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700125import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800126import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700127import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400128import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700129import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400130import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800131import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700132import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700133import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700134
justinzhang5286d3f2014-05-12 17:06:01 -0400135
Craig Mautner8d341ef2013-03-26 09:03:27 -0700136import java.io.FileDescriptor;
137import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700138import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700139import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700140import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700141import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700142import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700143
Craig Mautner4a1cb222013-12-04 16:14:06 -0800144public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800145 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700146 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700147 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700148 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700149 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700150 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700151 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700152 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700153 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
154 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700155 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700156 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700157 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
158 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700159 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700160 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800161
Craig Mautnerf3333272013-04-22 10:55:53 -0700162 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700163 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700164
Craig Mautner0eea92c2013-05-16 13:35:39 -0700165 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700166 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700167
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700168 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700169 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700170
Craig Mautner05d29032013-05-03 13:40:13 -0700171 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
172 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
173 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700174 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700175 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800176 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
177 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
178 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700179 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400180 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
181 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700182 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700183 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700184 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800185
Wale Ogunwale040b4702015-08-06 18:10:50 -0700186 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700187
Jason Monk62515be2014-05-21 16:06:19 -0400188 private static final String LOCK_TASK_TAG = "Lock-to-App";
189
Wale Ogunwale040b4702015-08-06 18:10:50 -0700190 // Used to indicate if an object (e.g. stack) that we are trying to get
191 // should be created if it doesn't exist already.
192 private static final boolean CREATE_IF_NEEDED = true;
193
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700194 // Used to indicate that windows of activities should be preserved during the resize.
195 static final boolean PRESERVE_WINDOWS = true;
196
Wale Ogunwale040b4702015-08-06 18:10:50 -0700197 // Used to indicate if an object (e.g. task) should be moved/created
198 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700199 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700200
201 // Used to indicate that an objects (e.g. task) removal from its container
202 // (e.g. stack) is due to it moving to another container.
203 static final boolean MOVING = true;
204
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700205 // Force the focus to change to the stack we are moving a task to..
206 static final boolean FORCE_FOCUS = true;
207
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700208 // Restore task from the saved recents if it can't be found in any live stack.
209 static final boolean RESTORE_FROM_RECENTS = true;
210
Svetoslav7008b512015-06-24 18:47:07 -0700211 // Activity actions an app cannot start if it uses a permission which is not granted.
212 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
213 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700214
Svetoslav7008b512015-06-24 18:47:07 -0700215 static {
216 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
217 Manifest.permission.CAMERA);
218 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
219 Manifest.permission.CAMERA);
220 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
221 Manifest.permission.CALL_PHONE);
222 }
223
Svet Ganov99b60432015-06-27 13:15:22 -0700224 /** Action restriction: launching the activity is not restricted. */
225 private static final int ACTIVITY_RESTRICTION_NONE = 0;
226 /** Action restriction: launching the activity is restricted by a permission. */
227 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
228 /** Action restriction: launching the activity is restricted by an app op. */
229 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700230
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700231 // The height/width divide used when fitting a task within a bounds with method
232 // {@link #fitWithinBounds}.
233 // We always want the task to to be visible in the bounds without affecting its size when
234 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
235 // the input bounds right or bottom side minus the width or height divided by this value.
236 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
237
justinzhang5286d3f2014-05-12 17:06:01 -0400238 /** Status Bar Service **/
239 private IBinder mToken = new Binder();
240 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400241 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400242
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700243 // For debugging to make sure the caller when acquiring/releasing our
244 // wake lock is the system process.
245 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700246
Craig Mautner27084302013-03-25 08:05:25 -0700247 final ActivityManagerService mService;
248
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800249 private final RecentTasks mRecentTasks;
250
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700251 final ActivityStackSupervisorHandler mHandler;
252
253 /** Short cut */
254 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800255 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700256
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700257 /** Counter for next free stack ID to use for dynamic activity stacks. */
258 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700259
260 /** Task identifier that activities are currently being started in. Incremented each time a
261 * new task is created. */
262 private int mCurTaskId = 0;
263
Craig Mautner2420ead2013-04-01 17:13:20 -0700264 /** The current user */
265 private int mCurrentUser;
266
Craig Mautnere0a38842013-12-16 16:14:02 -0800267 /** The stack containing the launcher app. Assumed to always be attached to
268 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700269 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700270
Craig Mautnere0a38842013-12-16 16:14:02 -0800271 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700272 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700273
Craig Mautner4a1cb222013-12-04 16:14:06 -0800274 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
275 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800276 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800277 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700278
279 /** List of activities that are waiting for a new activity to become visible before completing
280 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800281 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700282
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700283 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800284 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700285
286 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800287 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700288
Craig Mautnerde4ef022013-04-07 19:01:33 -0700289 /** List of activities that are ready to be stopped, but waiting for the next activity to
290 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800291 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700292
Craig Mautnerf3333272013-04-22 10:55:53 -0700293 /** List of activities that are ready to be finished, but waiting for the previous activity to
294 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800295 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700296
Craig Mautner0eea92c2013-05-16 13:35:39 -0700297 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800298 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700299
Craig Mautnerf3333272013-04-22 10:55:53 -0700300 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700301 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700302
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700303 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700304 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700305
Craig Mautnerde4ef022013-04-07 19:01:33 -0700306 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
307 * is being brought in front of us. */
308 boolean mUserLeaving = false;
309
Craig Mautner0eea92c2013-05-16 13:35:39 -0700310 /** Set when we have taken too long waiting to go to sleep. */
311 boolean mSleepTimeout = false;
312
313 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700314 * We don't want to allow the device to go to sleep while in the process
315 * of launching an activity. This is primarily to allow alarm intent
316 * receivers to launch an activity and get that to run before the device
317 * goes back to sleep.
318 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700319 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700320
321 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700322 * Set when the system is going to sleep, until we have
323 * successfully paused the current activity and released our wake lock.
324 * At that point the system is allowed to actually sleep.
325 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700326 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700327
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700328 /** Stack id of the front stack when user switched, indexed by userId. */
329 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700330
Craig Mautner4504de52013-12-20 09:06:56 -0800331 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800332 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700333 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800334
335 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700336 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800337
Jeff Brownca9bc702014-02-11 14:32:56 -0800338 InputManagerInternal mInputManagerInternal;
339
Craig Mautner15df08a2015-04-01 12:17:18 -0700340 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
341 * may be finished until there is only one entry left. If this is empty the system is not
342 * in lockTask mode. */
343 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000344 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700345 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
346 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000347 */
348 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400349 /**
350 * Notifies the user when entering/exiting lock-task.
351 */
352 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800353
Craig Mautner15df08a2015-04-01 12:17:18 -0700354 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700355
Craig Mautner42d04db2014-11-06 12:13:23 -0800356 /** Used to keep resumeTopActivityLocked() from being entered recursively */
357 boolean inResumeTopActivity;
358
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700359 // 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 -0700360 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700361 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700362
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700363 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
364 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
365
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700366 // The default minimal size that will be used if the activity doesn't specify its minimal size.
367 // It will be calculated when the default display gets added.
368 private int mDefaultMinimalSizeOfResizeableTask = -1;
369
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700370 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
371 private boolean mTaskLayersChanged = true;
372
Craig Mautneree36c772014-07-16 14:56:05 -0700373 /**
374 * Description of a request to start a new activity, which has been held
375 * due to app switches being disabled.
376 */
377 static class PendingActivityLaunch {
378 final ActivityRecord r;
379 final ActivityRecord sourceRecord;
380 final int startFlags;
381 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700382 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700383
384 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700385 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700386 r = _r;
387 sourceRecord = _sourceRecord;
388 startFlags = _startFlags;
389 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700390 callerApp = _callerApp;
391 }
392
393 void sendErrorResult(String message) {
394 try {
395 if (callerApp.thread != null) {
396 callerApp.thread.scheduleCrash(message);
397 }
398 } catch (RemoteException e) {
399 Slog.e(TAG, "Exception scheduling crash of failed "
400 + "activity launcher sourceRecord=" + sourceRecord, e);
401 }
Craig Mautneree36c772014-07-16 14:56:05 -0700402 }
403 }
404
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800405 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700406 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800407 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700408 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
409 }
410
411 /**
412 * At the time when the constructor runs, the power manager has not yet been
413 * initialized. So we initialize our wakelocks afterwards.
414 */
415 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700417 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700418 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700419 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700420 }
421
justinzhang5286d3f2014-05-12 17:06:01 -0400422 // This function returns a IStatusBarService. The value is from ServiceManager.
423 // getService and is cached.
424 private IStatusBarService getStatusBarService() {
425 synchronized (mService) {
426 if (mStatusBarService == null) {
427 mStatusBarService = IStatusBarService.Stub.asInterface(
428 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
429 if (mStatusBarService == null) {
430 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
431 }
432 }
433 return mStatusBarService;
434 }
435 }
436
Jason Monk35c62a42014-06-17 10:24:47 -0400437 private IDevicePolicyManager getDevicePolicyManager() {
438 synchronized (mService) {
439 if (mDevicePolicyManager == null) {
440 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
441 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
442 if (mDevicePolicyManager == null) {
443 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
444 }
445 }
446 return mDevicePolicyManager;
447 }
448 }
449
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700450 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800451 synchronized (mService) {
452 mWindowManager = wm;
453
454 mDisplayManager =
455 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
456 mDisplayManager.registerDisplayListener(this, null);
457
458 Display[] displays = mDisplayManager.getDisplays();
459 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
460 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800461 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700462 if (activityDisplay.mDisplay == null) {
463 throw new IllegalStateException("Default Display does not exist");
464 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800465 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700466 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800467 }
468
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700469 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800470 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800471
472 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800473 }
Craig Mautner27084302013-03-25 08:05:25 -0700474 }
475
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200476 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700477 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200478 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700479 }
480
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700481 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800482 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700483 }
484
Craig Mautnerde4ef022013-04-07 19:01:33 -0700485 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800486 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700487 }
488
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700489 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700490 if (stack == null) {
491 return false;
492 }
493
Craig Mautnerdf88d732014-01-27 09:21:32 -0800494 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
495 if (parent != null) {
496 stack = parent.task.stack;
497 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800498 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700499 }
500
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700501 /** The top most stack. */
502 boolean isFrontStack(ActivityStack stack) {
503 if (stack == null) {
504 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800505 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700506
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700507 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
508 if (parent != null) {
509 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800510 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700511 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
512 }
513
514 void setFocusStack(String reason, ActivityStack focusedStack) {
515 mLastFocusedStack = mFocusedStack;
516 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800517
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700518 EventLogTags.writeAmFocusedStack(
519 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
520 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800521
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800522 if (mService.mBooting || !mService.mBooted) {
523 final ActivityRecord r = topRunningActivityLocked();
524 if (r != null && r.idle) {
525 checkFinishBootingLocked();
526 }
527 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700528 }
529
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700530 void moveHomeStackToFront(String reason) {
531 mHomeStack.moveToFront(reason);
532 }
533
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700534 /** Returns true if the focus activity was adjusted to the home stack top activity. */
535 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700536 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
537 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700538 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700539 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700540
Craig Mautner84984fa2014-06-19 11:19:20 -0700541 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700542
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700543 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700544 if (top == null) {
545 return false;
546 }
547 mService.setFocusedActivityLocked(top, reason);
548 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700549 }
550
Craig Mautner299f9602015-01-26 09:47:33 -0800551 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700552 if (!mService.mBooting && !mService.mBooted) {
553 // Not ready yet!
554 return false;
555 }
556
Craig Mautner84984fa2014-06-19 11:19:20 -0700557 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
558 mWindowManager.showRecentApps();
559 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700560 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700561
Craig Mautner84984fa2014-06-19 11:19:20 -0700562 if (prev != null) {
563 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
564 }
565
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700566 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
567 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800568 // Only resume home activity if isn't finishing.
569 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800570 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700571 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700572 }
Craig Mautner299f9602015-01-26 09:47:33 -0800573 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700574 }
575
Craig Mautner8d341ef2013-03-26 09:03:27 -0700576 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700577 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700578 }
579
580 /**
581 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
582 * @param id Id of the task we would like returned.
583 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
584 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700585 * @param stackId The stack to restore the task to (default launch stack will be used if
586 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700587 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700588 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800589 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800590 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800591 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
593 ActivityStack stack = stacks.get(stackNdx);
594 TaskRecord task = stack.taskForIdLocked(id);
595 if (task != null) {
596 return task;
597 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700598 }
599 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800600
601 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700602 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800603 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800604 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700605 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800606 return null;
607 }
608
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700609 if (!restoreFromRecents) {
610 return task;
611 }
612
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700613 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700614 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
615 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800616 return null;
617 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700618 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800619 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700620 }
621
Craig Mautner6170f732013-04-02 13:05:23 -0700622 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800623 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800625 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800626 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
627 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
628 if (r != null) {
629 return r;
630 }
Craig Mautner6170f732013-04-02 13:05:23 -0700631 }
632 }
633 return null;
634 }
635
Craig Mautneref73ee12014-04-23 11:45:37 -0700636 void setNextTaskId(int taskId) {
637 if (taskId > mCurTaskId) {
638 mCurTaskId = taskId;
639 }
640 }
641
Craig Mautner8d341ef2013-03-26 09:03:27 -0700642 int getNextTaskId() {
643 do {
644 mCurTaskId++;
645 if (mCurTaskId <= 0) {
646 mCurTaskId = 1;
647 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700648 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700649 return mCurTaskId;
650 }
651
Craig Mautnerde4ef022013-04-07 19:01:33 -0700652 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800653 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700654 if (stack == null) {
655 return null;
656 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700657 ActivityRecord resumedActivity = stack.mResumedActivity;
658 if (resumedActivity == null || resumedActivity.app == null) {
659 resumedActivity = stack.mPausingActivity;
660 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700661 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700662 }
663 }
664 return resumedActivity;
665 }
666
Dianne Hackbornff072722014-09-24 10:56:28 -0700667 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700668 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800669 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800670 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
671 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
673 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700674 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 continue;
676 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700677 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 if (hr != null) {
679 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
680 && processName.equals(hr.processName)) {
681 try {
George Mount2c92c972014-03-20 09:38:23 -0700682 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 didSomething = true;
684 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700685 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 Slog.w(TAG, "Exception in new application when starting activity "
687 + hr.intent.getComponent().flattenToShortString(), e);
688 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700689 }
Craig Mautner20e72272013-04-01 13:45:53 -0700690 }
Craig Mautner20e72272013-04-01 13:45:53 -0700691 }
692 }
693 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700694 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700695 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700696 }
Craig Mautner20e72272013-04-01 13:45:53 -0700697 return didSomething;
698 }
699
700 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800701 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
702 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
704 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700705 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 continue;
707 }
708 final ActivityRecord resumedActivity = stack.mResumedActivity;
709 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700710 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800711 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 return false;
713 }
Craig Mautner20e72272013-04-01 13:45:53 -0700714 }
715 }
716 return true;
717 }
718
Craig Mautnerde4ef022013-04-07 19:01:33 -0700719 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800720 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
721 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800722 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
723 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700724 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800725 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700726 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 return false;
728 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700729 }
730 }
731 }
732 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700733 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800734 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
735 mLastFocusedStack + " to=" + mFocusedStack);
736 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700737 return true;
738 }
739
740 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800741 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800742 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
743 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800744 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
745 final ActivityStack stack = stacks.get(stackNdx);
746 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800747 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700748 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800749 return false;
750 }
751 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800752 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700753 }
754 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800755 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700756 }
757
Craig Mautner2acc3892013-09-23 10:28:14 -0700758 /**
759 * Pause all activities in either all of the stacks or just the back stacks.
760 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700761 * @return true if any activity was paused as a result of this call.
762 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700763 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700764 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800765 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
766 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
768 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700769 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700770 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800771 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700772 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
773 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700775 }
776 }
777 return someActivityPaused;
778 }
779
Craig Mautnerde4ef022013-04-07 19:01:33 -0700780 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700781 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800782 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
783 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800784 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
785 final ActivityStack stack = stacks.get(stackNdx);
786 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700787 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800788 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700789 Slog.d(TAG_STATES,
790 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800791 pausing = false;
792 } else {
793 return false;
794 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700795 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700796 }
797 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700798 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700799 }
800
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700801 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
802 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500803 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800804 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
805 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
806 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
807 final ActivityStack stack = stacks.get(stackNdx);
808 if (stack.mResumedActivity != null &&
809 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700810 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800811 }
812 }
813 }
814 }
815
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700816 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700817 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700818 }
819
820 void sendWaitingVisibleReportLocked(ActivityRecord r) {
821 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700822 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700823 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700824 if (w.who == null) {
825 changed = true;
826 w.timeout = false;
827 if (r != null) {
828 w.who = new ComponentName(r.info.packageName, r.info.name);
829 }
830 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
831 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700832 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700833 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700834 if (changed) {
835 mService.notifyAll();
836 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700837 }
838
839 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
840 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700841 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700842 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700843 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700844 if (w.who == null) {
845 changed = true;
846 w.timeout = timeout;
847 if (r != null) {
848 w.who = new ComponentName(r.info.packageName, r.info.name);
849 }
850 w.thisTime = thisTime;
851 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700852 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700853 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700854 if (changed) {
855 mService.notifyAll();
856 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700857 }
858
Craig Mautner29219d92013-04-16 20:19:12 -0700859 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800860 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700861 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700862 if (r != null) {
863 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700864 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700865
Craig Mautner4a1cb222013-12-04 16:14:06 -0800866 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800867 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
869 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700870 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700871 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700872 if (r != null) {
873 return r;
874 }
875 }
876 }
877 return null;
878 }
879
Dianne Hackborn09233282014-04-30 11:33:59 -0700880 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700881 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800882 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
883 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800884 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800886 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800887 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
888 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700889 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800890 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700891 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700892 }
893 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700894
895 // The lists are already sorted from most recent to oldest. Just pull the most recent off
896 // each list and add it to list. Stop when all lists are empty or maxNum reached.
897 while (maxNum > 0) {
898 long mostRecentActiveTime = Long.MIN_VALUE;
899 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800900 final int numTaskLists = runningTaskLists.size();
901 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
902 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700903 if (!stackTaskList.isEmpty()) {
904 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
905 if (lastActiveTime > mostRecentActiveTime) {
906 mostRecentActiveTime = lastActiveTime;
907 selectedStackList = stackTaskList;
908 }
909 }
910 }
911 if (selectedStackList != null) {
912 list.add(selectedStackList.remove(0));
913 --maxNum;
914 } else {
915 break;
916 }
917 }
Craig Mautner20e72272013-04-01 13:45:53 -0700918 }
919
Craig Mautner23ac33b2013-04-01 16:26:35 -0700920 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700921 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922 // Collect information about the target of the Intent.
923 ActivityInfo aInfo;
924 try {
925 ResolveInfo rInfo =
926 AppGlobals.getPackageManager().resolveIntent(
927 intent, resolvedType,
928 PackageManager.MATCH_DEFAULT_ONLY
929 | ActivityManagerService.STOCK_PM_FLAGS, userId);
930 aInfo = rInfo != null ? rInfo.activityInfo : null;
931 } catch (RemoteException e) {
932 aInfo = null;
933 }
934
935 if (aInfo != null) {
936 // Store the found target back into the intent, because now that
937 // we have it we never want to do this again. For example, if the
938 // user navigates back to this point in the history, we should
939 // always restart the exact same activity.
940 intent.setComponent(new ComponentName(
941 aInfo.applicationInfo.packageName, aInfo.name));
942
943 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700944 if (!aInfo.processName.equals("system")) {
945 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700946 mService.setDebugApp(aInfo.processName, true, false);
947 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700948
Man Caocfa78b22015-06-11 20:14:34 -0700949 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
950 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
951 }
952
953 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700954 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700955 }
956 }
957 }
958 return aInfo;
959 }
960
Craig Mautner299f9602015-01-26 09:47:33 -0800961 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
962 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700963 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
964 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
965 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
966 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700967 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
968 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700969 null /* outActivity */, null /* container */, null /* inTask */);
970 if (inResumeTopActivity) {
971 // If we are in resume section already, home activity will be initialized, but not
972 // resumed (to avoid recursive resume) and will stay that way until something pokes it
973 // again. We need to schedule another resume.
974 scheduleResumeTopActivities();
975 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700976 }
977
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700979 String callingPackage, Intent intent, String resolvedType,
980 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700981 IBinder resultTo, String resultWho, int requestCode, int startFlags,
982 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Chong Zhang280d3322015-11-03 17:27:26 -0800983 Bundle bOptions, boolean ignoreTargetSecurity, int userId,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700984 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985 // Refuse possible leaked file descriptors
986 if (intent != null && intent.hasFileDescriptors()) {
987 throw new IllegalArgumentException("File descriptors passed in Intent");
988 }
989 boolean componentSpecified = intent.getComponent() != null;
990
991 // Don't modify the client's object!
992 intent = new Intent(intent);
993
994 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700995 ActivityInfo aInfo =
996 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700997
Chong Zhang280d3322015-11-03 17:27:26 -0800998 ActivityOptions options = ActivityOptions.fromBundle(bOptions);
Craig Mautnere0a38842013-12-16 16:14:02 -0800999 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001000 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001001 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001002 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001003 // Cannot start a child activity if the parent is not resumed.
1004 return ActivityManager.START_CANCELED;
1005 }
Dianne Hackborn95465202014-09-15 16:21:55 -07001006 final int realCallingPid = Binder.getCallingPid();
1007 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -07001008 int callingPid;
1009 if (callingUid >= 0) {
1010 callingPid = -1;
1011 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001012 callingPid = realCallingPid;
1013 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001014 } else {
1015 callingPid = callingUid = -1;
1016 }
1017
Craig Mautnere0a38842013-12-16 16:14:02 -08001018 final ActivityStack stack;
1019 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001020 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001021 } else {
1022 stack = container.mStack;
1023 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001024 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001025 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001026 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001027
1028 final long origId = Binder.clearCallingIdentity();
1029
1030 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001031 (aInfo.applicationInfo.privateFlags
1032 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001033 // This may be a heavy-weight process! Check to see if we already
1034 // have another, different heavy-weight process running.
1035 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1036 if (mService.mHeavyWeightProcess != null &&
1037 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1038 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001039 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001040 if (caller != null) {
1041 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1042 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001043 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001044 } else {
1045 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001046 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001047 + intent.toString());
1048 ActivityOptions.abort(options);
1049 return ActivityManager.START_PERMISSION_DENIED;
1050 }
1051 }
1052
1053 IIntentSender target = mService.getIntentSenderLocked(
1054 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001055 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001056 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1057 | PendingIntent.FLAG_ONE_SHOT, null);
1058
1059 Intent newIntent = new Intent();
1060 if (requestCode >= 0) {
1061 // Caller is requesting a result.
1062 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1063 }
1064 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1065 new IntentSender(target));
1066 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1067 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1068 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1069 hist.packageName);
1070 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1071 hist.task.taskId);
1072 }
1073 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1074 aInfo.packageName);
1075 newIntent.setFlags(intent.getFlags());
1076 newIntent.setClassName("android",
1077 HeavyWeightSwitcherActivity.class.getName());
1078 intent = newIntent;
1079 resolvedType = null;
1080 caller = null;
1081 callingUid = Binder.getCallingUid();
1082 callingPid = Binder.getCallingPid();
1083 componentSpecified = true;
1084 try {
1085 ResolveInfo rInfo =
1086 AppGlobals.getPackageManager().resolveIntent(
1087 intent, null,
1088 PackageManager.MATCH_DEFAULT_ONLY
1089 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1090 aInfo = rInfo != null ? rInfo.activityInfo : null;
1091 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1092 } catch (RemoteException e) {
1093 aInfo = null;
1094 }
1095 }
1096 }
1097 }
1098
Dianne Hackborn91097de2014-04-04 18:02:06 -07001099 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1100 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001101 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001102 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001103 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001104
Craig Mautner85c11a82014-07-17 12:38:18 -07001105 Binder.restoreCallingIdentity(origId);
1106
Craig Mautnerde4ef022013-04-07 19:01:33 -07001107 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001108 // If the caller also wants to switch to a new configuration,
1109 // do so now. This allows a clean switch, as we are waiting
1110 // for the current activity to pause (so we will not destroy
1111 // it), and have not yet started the next activity.
1112 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1113 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001114 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001115 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001116 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001117 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001118 }
1119
Craig Mautner23ac33b2013-04-01 16:26:35 -07001120 if (outResult != null) {
1121 outResult.result = res;
1122 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001124 do {
1125 try {
1126 mService.wait();
1127 } catch (InterruptedException e) {
1128 }
1129 } while (!outResult.timeout && outResult.who == null);
1130 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001131 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001132 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001133 outResult.timeout = false;
1134 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1135 outResult.totalTime = 0;
1136 outResult.thisTime = 0;
1137 } else {
1138 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001139 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001140 do {
1141 try {
1142 mService.wait();
1143 } catch (InterruptedException e) {
1144 }
1145 } while (!outResult.timeout && outResult.who == null);
1146 }
1147 }
1148 }
1149
1150 return res;
1151 }
1152 }
1153
1154 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1155 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
Chong Zhang280d3322015-11-03 17:27:26 -08001156 Bundle bOptions, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001157 if (intents == null) {
1158 throw new NullPointerException("intents is null");
1159 }
1160 if (resolvedTypes == null) {
1161 throw new NullPointerException("resolvedTypes is null");
1162 }
1163 if (intents.length != resolvedTypes.length) {
1164 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1165 }
1166
Craig Mautner23ac33b2013-04-01 16:26:35 -07001167
1168 int callingPid;
1169 if (callingUid >= 0) {
1170 callingPid = -1;
1171 } else if (caller == null) {
1172 callingPid = Binder.getCallingPid();
1173 callingUid = Binder.getCallingUid();
1174 } else {
1175 callingPid = callingUid = -1;
1176 }
1177 final long origId = Binder.clearCallingIdentity();
1178 try {
1179 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001180 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001181 for (int i=0; i<intents.length; i++) {
1182 Intent intent = intents[i];
1183 if (intent == null) {
1184 continue;
1185 }
1186
1187 // Refuse possible leaked file descriptors
1188 if (intent != null && intent.hasFileDescriptors()) {
1189 throw new IllegalArgumentException("File descriptors passed in Intent");
1190 }
1191
1192 boolean componentSpecified = intent.getComponent() != null;
1193
1194 // Don't modify the client's object!
1195 intent = new Intent(intent);
1196
1197 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001198 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001199 // TODO: New, check if this is correct
1200 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1201
1202 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001203 (aInfo.applicationInfo.privateFlags
1204 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001205 throw new IllegalArgumentException(
1206 "FLAG_CANT_SAVE_STATE not supported here");
1207 }
1208
Chong Zhang280d3322015-11-03 17:27:26 -08001209 ActivityOptions options = ActivityOptions.fromBundle(
1210 i == intents.length - 1 ? bOptions : null);
Craig Mautner6170f732013-04-02 13:05:23 -07001211 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001212 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1213 callingPackage, callingPid, callingUid,
Chong Zhang280d3322015-11-03 17:27:26 -08001214 0, options, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001215 if (res < 0) {
1216 return res;
1217 }
1218
1219 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1220 }
1221 }
1222 } finally {
1223 Binder.restoreCallingIdentity(origId);
1224 }
1225
1226 return ActivityManager.START_SUCCESS;
1227 }
1228
Craig Mautner2420ead2013-04-01 17:13:20 -07001229 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001230 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001231 throws RemoteException {
1232
Craig Mautner2568c3a2015-03-26 14:22:34 -07001233 if (andResume) {
1234 r.startFreezingScreenLocked(app, 0);
1235 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001236
Craig Mautner2568c3a2015-03-26 14:22:34 -07001237 // schedule launch ticks to collect information about slow apps.
1238 r.startLaunchTickingLocked();
1239 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001240
1241 // Have the window manager re-evaluate the orientation of
1242 // the screen based on the new activity order. Note that
1243 // as a result of this, it can call back into the activity
1244 // manager with a new orientation. We don't care about that,
1245 // because the activity is not currently running so we are
1246 // just restarting it anyway.
1247 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001248 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 mService.mConfiguration,
1250 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001251 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001252 }
1253
1254 r.app = app;
1255 app.waitingToKill = null;
1256 r.launchCount++;
1257 r.lastLaunchTime = SystemClock.uptimeMillis();
1258
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001259 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001260
1261 int idx = app.activities.indexOf(r);
1262 if (idx < 0) {
1263 app.activities.add(r);
1264 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001265 mService.updateLruProcessLocked(app, true, null);
1266 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001267
Craig Mautner15df08a2015-04-01 12:17:18 -07001268 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001269 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1270 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001271 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001272 }
1273
1274 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 try {
1276 if (app.thread == null) {
1277 throw new RemoteException();
1278 }
1279 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001280 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001281 if (andResume) {
1282 results = r.results;
1283 newIntents = r.newIntents;
1284 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001285 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1286 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1287 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 if (andResume) {
1289 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1290 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001291 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001292 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001293 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001294 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001295 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001297 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001298 r.sleeping = false;
1299 r.forceNewConfig = false;
1300 mService.showAskCompatModeDialogLocked(r);
1301 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001302 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001303 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1304 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1305 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001306 final String profileFile = mService.mProfileFile;
1307 if (profileFile != null) {
1308 ParcelFileDescriptor profileFd = mService.mProfileFd;
1309 if (profileFd != null) {
1310 try {
1311 profileFd = profileFd.dup();
1312 } catch (IOException e) {
1313 if (profileFd != null) {
1314 try {
1315 profileFd.close();
1316 } catch (IOException o) {
1317 }
1318 profileFd = null;
1319 }
1320 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001321 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001322
1323 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1324 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001325 }
1326 }
1327 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001328
Craig Mautner2568c3a2015-03-26 14:22:34 -07001329 if (andResume) {
1330 app.hasShownUi = true;
1331 app.pendingUiClean = true;
1332 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001333 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001334 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001335 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001336 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001337 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001338 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001339
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001340 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001341 // This may be a heavy-weight process! Note that the package
1342 // manager will ensure that only activity can run in the main
1343 // process of the .apk, which is the only thing that will be
1344 // considered heavy-weight.
1345 if (app.processName.equals(app.info.packageName)) {
1346 if (mService.mHeavyWeightProcess != null
1347 && mService.mHeavyWeightProcess != app) {
1348 Slog.w(TAG, "Starting new heavy weight process " + app
1349 + " when already running "
1350 + mService.mHeavyWeightProcess);
1351 }
1352 mService.mHeavyWeightProcess = app;
1353 Message msg = mService.mHandler.obtainMessage(
1354 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1355 msg.obj = r;
1356 mService.mHandler.sendMessage(msg);
1357 }
1358 }
1359
1360 } catch (RemoteException e) {
1361 if (r.launchFailed) {
1362 // This is the second time we failed -- finish activity
1363 // and give up.
1364 Slog.e(TAG, "Second failure launching "
1365 + r.intent.getComponent().flattenToShortString()
1366 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001367 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001368 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1369 "2nd-crash", false);
1370 return false;
1371 }
1372
1373 // This is the first time we failed -- restart process and
1374 // retry.
1375 app.activities.remove(r);
1376 throw e;
1377 }
1378
1379 r.launchFailed = false;
1380 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001381 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001382 }
1383
1384 if (andResume) {
1385 // As part of the process of launching, ActivityThread also performs
1386 // a resume.
1387 stack.minimalResumeActivityLocked(r);
1388 } else {
1389 // This activity is not starting in the resumed state... which
1390 // should look like we asked it to pause+stop (but remain visible),
1391 // and it has done so and reported back the current icicle and
1392 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001393 if (DEBUG_STATES) Slog.v(TAG_STATES,
1394 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001395 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001396 r.stopped = true;
1397 }
1398
1399 // Launch the new version setup screen if needed. We do this -after-
1400 // launching the initial activity (that is, home), so that it can have
1401 // a chance to initialize itself while in the background, making the
1402 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001403 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001404 mService.startSetupActivityLocked();
1405 }
1406
Dianne Hackborn465fa392014-09-14 14:21:18 -07001407 // Update any services we are bound to that might care about whether
1408 // their client may have activities.
1409 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1410
Craig Mautner2420ead2013-04-01 17:13:20 -07001411 return true;
1412 }
1413
Craig Mautnere79d42682013-04-01 19:01:53 -07001414 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001415 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001416 // Is this activity's application already running?
1417 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001418 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001419
1420 r.task.stack.setLaunchTime(r);
1421
1422 if (app != null && app.thread != null) {
1423 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001424 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1425 || !"android".equals(r.info.packageName)) {
1426 // Don't add this if it is a platform component that is marked
1427 // to run in multiple processes, because this is actually
1428 // part of the framework so doesn't make sense to track as a
1429 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001430 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1431 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001432 }
George Mount2c92c972014-03-20 09:38:23 -07001433 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001434 return;
1435 } catch (RemoteException e) {
1436 Slog.w(TAG, "Exception when starting activity "
1437 + r.intent.getComponent().flattenToShortString(), e);
1438 }
1439
1440 // If a dead object exception was thrown -- fall through to
1441 // restart the application.
1442 }
1443
1444 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001445 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001446 }
1447
Craig Mautner6170f732013-04-02 13:05:23 -07001448 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001449 Intent intent, String resolvedType, ActivityInfo aInfo,
1450 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1451 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001452 int callingPid, int callingUid, String callingPackage,
Chong Zhang280d3322015-11-03 17:27:26 -08001453 int realCallingPid, int realCallingUid, int startFlags, ActivityOptions options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001454 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1455 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001456 int err = ActivityManager.START_SUCCESS;
1457
1458 ProcessRecord callerApp = null;
1459 if (caller != null) {
1460 callerApp = mService.getRecordForAppLocked(caller);
1461 if (callerApp != null) {
1462 callingPid = callerApp.pid;
1463 callingUid = callerApp.info.uid;
1464 } else {
1465 Slog.w(TAG, "Unable to find app for caller " + caller
1466 + " (pid=" + callingPid + ") when starting: "
1467 + intent.toString());
1468 err = ActivityManager.START_PERMISSION_DENIED;
1469 }
1470 }
1471
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001472 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1473
Craig Mautner6170f732013-04-02 13:05:23 -07001474 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001475 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001476 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001477 + " on display " + (container == null ? (mFocusedStack == null ?
1478 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1479 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1480 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001481 }
1482
1483 ActivityRecord sourceRecord = null;
1484 ActivityRecord resultRecord = null;
1485 if (resultTo != null) {
1486 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001487 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1488 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001489 if (sourceRecord != null) {
1490 if (requestCode >= 0 && !sourceRecord.finishing) {
1491 resultRecord = sourceRecord;
1492 }
1493 }
1494 }
Craig Mautner6170f732013-04-02 13:05:23 -07001495
Dianne Hackborn91097de2014-04-04 18:02:06 -07001496 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001497
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001498 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001499 // Transfer the result target from the source activity to the new
1500 // one being started, including any failures.
1501 if (requestCode >= 0) {
1502 ActivityOptions.abort(options);
1503 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1504 }
1505 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001506 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1507 resultRecord = null;
1508 }
Craig Mautner6170f732013-04-02 13:05:23 -07001509 resultWho = sourceRecord.resultWho;
1510 requestCode = sourceRecord.requestCode;
1511 sourceRecord.resultTo = null;
1512 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001513 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001514 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001515 if (sourceRecord.launchedFromUid == callingUid) {
1516 // The new activity is being launched from the same uid as the previous
1517 // activity in the flow, and asking to forward its result back to the
1518 // previous. In this case the activity is serving as a trampoline between
1519 // the two, so we also want to update its launchedFromPackage to be the
1520 // same as the previous activity. Note that this is safe, since we know
1521 // these two packages come from the same uid; the caller could just as
1522 // well have supplied that same package name itself. This specifially
1523 // deals with the case of an intent picker/chooser being launched in the app
1524 // flow to redirect to an activity picked by the user, where we want the final
1525 // activity to consider it to have been launched by the previous app activity.
1526 callingPackage = sourceRecord.launchedFromPackage;
1527 }
Craig Mautner6170f732013-04-02 13:05:23 -07001528 }
1529
1530 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1531 // We couldn't find a class that can handle the given Intent.
1532 // That's the end of that!
1533 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1534 }
1535
1536 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1537 // We couldn't find the specific class specified in the Intent.
1538 // Also the end of the line.
1539 err = ActivityManager.START_CLASS_NOT_FOUND;
1540 }
1541
Dianne Hackborn91097de2014-04-04 18:02:06 -07001542 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1543 && sourceRecord.task.voiceSession != null) {
1544 // If this activity is being launched as part of a voice session, we need
1545 // to ensure that it is safe to do so. If the upcoming activity will also
1546 // be part of the voice session, we can only launch it if it has explicitly
1547 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001548 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001549 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1550 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001551 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001552 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1553 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001554 Slog.w(TAG,
1555 "Activity being started in current voice task does not support voice: "
1556 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001557 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1558 }
1559 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001560 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001561 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1562 }
1563 }
1564 }
1565
1566 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1567 // If the caller is starting a new voice session, just make sure the target
1568 // is actually allowing it to run this way.
1569 try {
1570 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1571 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001572 Slog.w(TAG,
1573 "Activity being started in new voice task does not support: "
1574 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001575 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1576 }
1577 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001578 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001579 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1580 }
1581 }
1582
louis_changcd5d1982014-08-01 10:09:08 +08001583 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1584
Craig Mautner6170f732013-04-02 13:05:23 -07001585 if (err != ActivityManager.START_SUCCESS) {
1586 if (resultRecord != null) {
1587 resultStack.sendActivityResultLocked(-1,
1588 resultRecord, resultWho, requestCode,
1589 Activity.RESULT_CANCELED, null);
1590 }
Craig Mautner6170f732013-04-02 13:05:23 -07001591 ActivityOptions.abort(options);
1592 return err;
1593 }
1594
Svetoslav7008b512015-06-24 18:47:07 -07001595 boolean abort = false;
1596
Svet Ganov99b60432015-06-27 13:15:22 -07001597 final int startAnyPerm = mService.checkPermission(
1598 START_ANY_ACTIVITY, callingPid, callingUid);
1599
1600 if (startAnyPerm != PERMISSION_GRANTED) {
1601 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001602 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001603 final int actionRestriction = getActionRestrictionForCallingPackage(
1604 intent.getAction(), callingPackage, callingPid, callingUid);
1605
1606 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1607 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1608 if (resultRecord != null) {
1609 resultStack.sendActivityResultLocked(-1,
1610 resultRecord, resultWho, requestCode,
1611 Activity.RESULT_CANCELED, null);
1612 }
1613 String msg;
1614 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1615 msg = "Permission Denial: starting " + intent.toString()
1616 + " from " + callerApp + " (pid=" + callingPid
1617 + ", uid=" + callingUid + ")" + " with revoked permission "
1618 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1619 } else if (!aInfo.exported) {
1620 msg = "Permission Denial: starting " + intent.toString()
1621 + " from " + callerApp + " (pid=" + callingPid
1622 + ", uid=" + callingUid + ")"
1623 + " not exported from uid " + aInfo.applicationInfo.uid;
1624 } else {
1625 msg = "Permission Denial: starting " + intent.toString()
1626 + " from " + callerApp + " (pid=" + callingPid
1627 + ", uid=" + callingUid + ")"
1628 + " requires " + aInfo.permission;
1629 }
1630 Slog.w(TAG, msg);
1631 throw new SecurityException(msg);
1632 }
1633
1634 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1635 String message = "Appop Denial: starting " + intent.toString()
1636 + " from " + callerApp + " (pid=" + callingPid
1637 + ", uid=" + callingUid + ")"
1638 + " requires " + AppOpsManager.permissionToOp(
1639 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1640 Slog.w(TAG, message);
1641 abort = true;
1642 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1643 String message = "Appop Denial: starting " + intent.toString()
1644 + " from " + callerApp + " (pid=" + callingPid
1645 + ", uid=" + callingUid + ")"
1646 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1647 Slog.w(TAG, message);
1648 abort = true;
1649 }
Svetoslav7008b512015-06-24 18:47:07 -07001650 }
1651
1652 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001653 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001654
Craig Mautner6170f732013-04-02 13:05:23 -07001655 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001656 try {
1657 // The Intent we give to the watcher has the extra data
1658 // stripped off, since it can contain private information.
1659 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001660 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001661 aInfo.applicationInfo.packageName);
1662 } catch (RemoteException e) {
1663 mService.mController = null;
1664 }
Ben Gruver5e207332013-04-03 17:41:37 -07001665 }
Craig Mautner6170f732013-04-02 13:05:23 -07001666
Clara Bayarrif7fea162015-10-22 16:09:52 +01001667 UserInfo user = getUserInfo(userId);
1668 // TODO: Timeout for work challenge
1669 if (user.isManagedProfile()
1670 && mService.mContext.getSystemService(StorageManager.class)
1671 .isPerUserEncryptionEnabled()) {
1672 KeyguardManager km = (KeyguardManager) mService.mContext
1673 .getSystemService(Context.KEYGUARD_SERVICE);
1674
1675 IIntentSender target = mService.getIntentSenderLocked(
1676 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1677 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1678 new String[]{ resolvedType },
1679 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1680 | PendingIntent.FLAG_IMMUTABLE, null);
1681 int flags = intent.getFlags();
1682 intent = km.createConfirmDeviceCredentialIntent(null, null);
1683 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1684 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1685 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1686 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1687 intent.putExtra(Intent.EXTRA_USER_ID, userId);
1688 intent.setFlags(flags);
1689
1690 resolvedType = null;
1691 callingUid = realCallingUid;
1692 callingPid = realCallingPid;
1693
1694 UserInfo parent = UserManager.get(mService.mContext).getProfileParent(userId);
1695 aInfo = resolveActivity(intent, null, PackageManager.MATCH_DEFAULT_ONLY
1696 | ActivityManagerService.STOCK_PM_FLAGS, null, parent.id);
1697 }
1698
Ben Gruver5e207332013-04-03 17:41:37 -07001699 if (abort) {
1700 if (resultRecord != null) {
1701 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001702 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001703 }
Ben Gruver5e207332013-04-03 17:41:37 -07001704 // We pretend to the caller that it was really started, but
1705 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001706 ActivityOptions.abort(options);
1707 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001708 }
1709
1710 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001711 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001712 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001713 if (outActivity != null) {
1714 outActivity[0] = r;
1715 }
1716
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001717 if (r.appTimeTracker == null && sourceRecord != null) {
1718 // If the caller didn't specify an explicit time tracker, we want to continue
1719 // tracking under any it has.
1720 r.appTimeTracker = sourceRecord.appTimeTracker;
1721 }
1722
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001723 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001724 if (voiceSession == null && (stack.mResumedActivity == null
1725 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001726 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1727 realCallingPid, realCallingUid, "Activity start")) {
Chong Zhang280d3322015-11-03 17:27:26 -08001728 PendingActivityLaunch pal = new PendingActivityLaunch(r,
Robert Carr13997f52015-10-23 13:13:39 -07001729 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001730 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001731 ActivityOptions.abort(options);
1732 return ActivityManager.START_SWITCHES_CANCELED;
1733 }
1734 }
1735
1736 if (mService.mDidAppSwitch) {
1737 // This is the second allowed switch since we stopped switches,
1738 // so now just generally allow switches. Use case: user presses
1739 // home (switches disabled, switch to home, mDidAppSwitch now true);
1740 // user taps a home icon (coming from home so allowed, we hit here
1741 // and now allow anyone to switch again).
1742 mService.mAppSwitchesAllowedTime = 0;
1743 } else {
1744 mService.mDidAppSwitch = true;
1745 }
1746
Craig Mautneree36c772014-07-16 14:56:05 -07001747 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001748
Dianne Hackborn91097de2014-04-04 18:02:06 -07001749 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001750 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001751
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001752 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001753 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001754 // activity start, but we are not actually doing an activity
1755 // switch... just dismiss the keyguard now, because we
1756 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001757 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001758 }
1759 return err;
1760 }
1761
Clara Bayarrif7fea162015-10-22 16:09:52 +01001762 private UserInfo getUserInfo(int userId) {
1763 final long identity = Binder.clearCallingIdentity();
1764 try {
1765 return UserManager.get(mService.mContext).getUserInfo(userId);
1766 } finally {
1767 Binder.restoreCallingIdentity(identity);
1768 }
1769 }
1770
Svet Ganov99b60432015-06-27 13:15:22 -07001771 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001772 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001773 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1774 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001775 == PackageManager.PERMISSION_DENIED) {
1776 return ACTIVITY_RESTRICTION_PERMISSION;
1777 }
1778
Christopher Tateff7add02015-08-17 10:23:22 -07001779 if (activityInfo.permission == null) {
1780 return ACTIVITY_RESTRICTION_NONE;
1781 }
1782
Svet Ganov99b60432015-06-27 13:15:22 -07001783 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1784 if (opCode == AppOpsManager.OP_NONE) {
1785 return ACTIVITY_RESTRICTION_NONE;
1786 }
1787
1788 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1789 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001790 if (!ignoreTargetSecurity) {
1791 return ACTIVITY_RESTRICTION_APPOP;
1792 }
Svet Ganov99b60432015-06-27 13:15:22 -07001793 }
1794
1795 return ACTIVITY_RESTRICTION_NONE;
1796 }
1797
Svetoslav7008b512015-06-24 18:47:07 -07001798 private int getActionRestrictionForCallingPackage(String action,
1799 String callingPackage, int callingPid, int callingUid) {
1800 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001801 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001802 }
1803
1804 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1805 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001806 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001807 }
1808
1809 final PackageInfo packageInfo;
1810 try {
1811 packageInfo = mService.mContext.getPackageManager()
1812 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1813 } catch (PackageManager.NameNotFoundException e) {
1814 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001815 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001816 }
1817
1818 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001819 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001820 }
1821
1822 if (mService.checkPermission(permission, callingPid, callingUid) ==
1823 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001824 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001825 }
1826
1827 final int opCode = AppOpsManager.permissionToOpCode(permission);
1828 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001829 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001830 }
1831
1832 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1833 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001834 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001835 }
1836
Svet Ganov99b60432015-06-27 13:15:22 -07001837 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001838 }
1839
Chong Zhang0fa656b2015-08-31 15:17:21 -07001840 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001841 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001842
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001843 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1844 return mHomeStack;
1845 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001846
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001847 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001848
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001849 if (task != null && task.stack != null) {
1850 stack = task.stack;
1851 if (stack.isOnHomeDisplay()) {
1852 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001853 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001854 "computeStackFocus: Setting " + "focused stack to r=" + r
1855 + " task=" + task);
1856 } else {
1857 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1858 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001859 }
1860 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001861 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001862 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001863
1864 final ActivityContainer container = r.mInitialActivityContainer;
1865 if (container != null) {
1866 // The first time put it on the desired stack, after this put on task stack.
1867 r.mInitialActivityContainer = null;
1868 return container.mStack;
1869 }
1870
1871 // The fullscreen stack can contain any task regardless of if the task is resizeable
1872 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1873 // If the freeform stack has focus, and the activity to be launched is resizeable,
1874 // we can also put it in the focused stack.
1875 final boolean canUseFocusedStack =
1876 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1877 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1878 if (canUseFocusedStack
1879 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1880 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1881 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1882 return mFocusedStack;
1883 }
1884
1885 // We first try to put the task in the first dynamic stack.
1886 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1887 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1888 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001889 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001890 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1891 "computeStackFocus: Setting focused stack=" + stack);
1892 return stack;
1893 }
1894 }
1895
1896 // If there is no suitable dynamic stack then we figure out which static stack to use.
1897 final int stackId = task != null ? task.getLaunchStackId() :
1898 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1899 FULLSCREEN_WORKSPACE_STACK_ID;
1900 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1901 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1902 + r + " stackId=" + stack.mStackId);
1903 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001904 }
1905
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001906 boolean setFocusedStack(ActivityRecord r, String reason) {
1907 if (r == null) {
1908 // Not sure what you are trying to do, but it is not going to work...
1909 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001910 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001911 final TaskRecord task = r.task;
1912 if (task == null || task.stack == null) {
1913 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1914 return false;
1915 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001916 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001917 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001918 }
1919
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001920 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001921 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Chong Zhang280d3322015-11-03 17:27:26 -08001922 boolean doResume, ActivityOptions options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001923 final Intent intent = r.intent;
1924 final int callingUid = r.launchedFromUid;
1925
Chong Zhang0fa656b2015-08-31 15:17:21 -07001926 boolean overrideBounds = false;
1927 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001928 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
Chong Zhang280d3322015-11-03 17:27:26 -08001929 if (options.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001930 overrideBounds = true;
Chong Zhang280d3322015-11-03 17:27:26 -08001931 newBounds = options.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001932 }
1933 }
1934
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001935 // In some flows in to this function, we retrieve the task record and hold on to it
1936 // without a lock before calling back in to here... so the task at this point may
1937 // not actually be in recents. Check for that, and if it isn't in recents just
1938 // consider it invalid.
1939 if (inTask != null && !inTask.inRecents) {
1940 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1941 inTask = null;
1942 }
1943
Craig Mautnerad400af2014-08-13 16:41:36 -07001944 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001945 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1946 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947
Craig Mautnera228ae92014-07-09 05:44:55 -07001948 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001949 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001950 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001951 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1952 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1953 "\"singleInstance\" or \"singleTask\"");
1954 launchFlags &=
1955 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1956 } else {
1957 switch (r.info.documentLaunchMode) {
1958 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1959 break;
1960 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1961 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1962 break;
1963 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1964 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1965 break;
1966 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1967 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1968 break;
1969 }
1970 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001971
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001972 final boolean launchTaskBehind = r.mLaunchTaskBehind
1973 && !launchSingleTask && !launchSingleInstance
1974 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001975
Wale Ogunwale7d701172015-03-11 15:36:30 -07001976 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1977 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001978 // For whatever reason this activity is being launched into a new
1979 // task... yet the caller has requested a result back. Well, that
1980 // is pretty messed up, so instead immediately send back a cancel
1981 // and let the new task continue launched as normal without a
1982 // dependency on its originator.
1983 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1984 r.resultTo.task.stack.sendActivityResultLocked(-1,
1985 r.resultTo, r.resultWho, r.requestCode,
1986 Activity.RESULT_CANCELED, null);
1987 r.resultTo = null;
1988 }
1989
1990 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1991 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1992 }
1993
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001994 // If we are actually going to launch in to a new task, there are some cases where
1995 // we further want to do multiple task.
1996 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1997 if (launchTaskBehind
1998 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1999 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
2000 }
2001 }
2002
Craig Mautner8849a5e2013-04-02 16:41:03 -07002003 // We'll invoke onUserLeaving before onPause only if the launching
2004 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07002005 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002006 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2007 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002008
2009 // If the caller has asked not to resume at this point, we make note
2010 // of this in the record so that we can skip it when trying to find
2011 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07002012 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002013 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002014 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002015 }
2016
Craig Mautnera254cd72014-05-25 16:47:39 -07002017 ActivityRecord notTop =
2018 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002019
2020 // If the onlyIfNeeded flag is set, then we can do this if the activity
2021 // being launched is the same as the one making the call... or, as
2022 // a special case, if we do not know the caller then we count the
2023 // current top activity as the caller.
2024 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2025 ActivityRecord checkedCaller = sourceRecord;
2026 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002027 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002028 }
2029 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2030 // Caller is not the same as launcher, so always needed.
2031 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
2032 }
2033 }
2034
Craig Mautner8849a5e2013-04-02 16:41:03 -07002035 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002036 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002037
2038 // If the caller is not coming from another activity, but has given us an
2039 // explicit task into which they would like us to launch the new activity,
2040 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002041 if (sourceRecord == null && inTask != null && inTask.stack != null) {
2042 final Intent baseIntent = inTask.getBaseIntent();
2043 final ActivityRecord root = inTask.getRootActivity();
2044 if (baseIntent == null) {
2045 ActivityOptions.abort(options);
2046 throw new IllegalArgumentException("Launching into task without base intent: "
2047 + inTask);
2048 }
2049
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002050 // If this task is empty, then we are adding the first activity -- it
2051 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002052 if (launchSingleInstance || launchSingleTask) {
2053 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2054 ActivityOptions.abort(options);
2055 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2056 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002057 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002058 if (root != null) {
2059 ActivityOptions.abort(options);
2060 throw new IllegalArgumentException("Caller with inTask " + inTask
2061 + " has root " + root + " but target is singleInstance/Task");
2062 }
2063 }
2064
2065 // If task is empty, then adopt the interesting intent launch flags in to the
2066 // activity being started.
2067 if (root == null) {
2068 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2069 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2070 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2071 launchFlags = (launchFlags&~flagsOfInterest)
2072 | (baseIntent.getFlags()&flagsOfInterest);
2073 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002074 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002075 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002076
Dianne Hackborn962d5352014-08-29 16:27:40 -07002077 // If the task is not empty and the caller is asking to start it as the root
2078 // of a new task, then we don't actually want to start this on the task. We
2079 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002080 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002081 addingToTask = false;
2082
2083 } else {
2084 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002085 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002086
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002087 reuseTask = inTask;
2088 } else {
2089 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002090 // Launch ResolverActivity in the source task, so that it stays in the task
2091 // bounds when in freeform workspace.
2092 // Also put noDisplay activities in the source task. These by itself can
2093 // be placed in any task/stack, however it could launch other activities
2094 // like ResolverActivity, and we want those to stay in the original task.
2095 if (r.isResolverActivity() || r.noDisplay) {
2096 addingToTask = true;
2097 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002098 }
2099
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002100 if (inTask == null) {
2101 if (sourceRecord == null) {
2102 // This activity is not being started from another... in this
2103 // case we -always- start a new task.
2104 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2105 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2106 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2107 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2108 }
2109 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2110 // The original activity who is starting us is running as a single
2111 // instance... this new activity it is starting must go on its
2112 // own task.
2113 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2114 } else if (launchSingleInstance || launchSingleTask) {
2115 // The activity being started is a single instance... it always
2116 // gets launched into its own task.
2117 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2118 }
2119 }
2120
2121 ActivityInfo newTaskInfo = null;
2122 Intent newTaskIntent = null;
2123 ActivityStack sourceStack;
2124 if (sourceRecord != null) {
2125 if (sourceRecord.finishing) {
2126 // If the source is finishing, we can't further count it as our source. This
2127 // is because the task it is associated with may now be empty and on its way out,
2128 // so we don't want to blindly throw it in to that task. Instead we will take
2129 // the NEW_TASK flow and try to find a task for it. But save the task information
2130 // so it can be used when creating the new task.
2131 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2132 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2133 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2134 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2135 newTaskInfo = sourceRecord.info;
2136 newTaskIntent = sourceRecord.task.intent;
2137 }
2138 sourceRecord = null;
2139 sourceStack = null;
2140 } else {
2141 sourceStack = sourceRecord.task.stack;
2142 }
2143 } else {
2144 sourceStack = null;
2145 }
2146
2147 boolean movedHome = false;
2148 ActivityStack targetStack;
2149
2150 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002151 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002152
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002153 // We may want to try to place the new activity in to an existing task. We always
2154 // do this if the target activity is singleTask or singleInstance; we will also do
2155 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2156 // us to still place it in a new task: multi task, always doc mode, or being asked to
2157 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002158 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2159 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002160 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002161 // If bring to front is requested, and no result is requested and we have not
2162 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163 // we can find a task that was started with this same
2164 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002165 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002166 // See if there is a task to bring to the front. If this is
2167 // a SINGLE_INSTANCE activity, there can be one and only one
2168 // instance of it in the history, and it is always in its own
2169 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002170 ActivityRecord intentActivity = !launchSingleInstance ?
2171 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002173 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2174 // but still needs to be a lock task mode violation since the task gets
2175 // cleared out and the device would otherwise leave the locked task.
2176 if (isLockTaskModeViolation(intentActivity.task,
2177 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2178 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002179 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002180 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002181 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2182 }
Craig Mautner29219d92013-04-16 20:19:12 -07002183 if (r.task == null) {
2184 r.task = intentActivity.task;
2185 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186 if (intentActivity.task.intent == null) {
2187 // This task was started because of movement of
2188 // the activity based on affinity... now that we
2189 // are actually launching it, we can assign the
2190 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002191 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002192 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002193 targetStack = intentActivity.task.stack;
2194 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002195 // If the target task is not in the front, then we need
2196 // to bring it to the front... except... well, with
2197 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2198 // to have the same behavior as if a new instance was
2199 // being started, which means not bringing it to the front
2200 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002201 final ActivityStack focusStack = getFocusedStack();
2202 ActivityRecord curTop = (focusStack == null)
2203 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002204 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002205 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002206 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002207 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002208 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2209 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002210 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002211 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002212 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2213 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002214 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002215 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002216 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002217 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002218 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002219 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2220 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002221 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002222 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002223 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002224 options = null;
2225 }
2226 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002227 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002228 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002229 + " from " + intentActivity);
2230 targetStack.moveToFront("intentActivityFound");
2231 }
2232
Craig Mautner8849a5e2013-04-02 16:41:03 -07002233 // If the caller has requested that the target task be
2234 // reset, then do so.
2235 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2236 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2237 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002238 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002239 // We don't need to start a new activity, and
2240 // the client said not to do anything if that
2241 // is the case, so this is it! And for paranoia, make
2242 // sure we have correctly resumed the top activity.
2243 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002244 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002245
2246 // Make sure to notify Keyguard as well if we are not running an app
2247 // transition later.
2248 if (!movedToFront) {
2249 notifyActivityDrawnForKeyguard();
2250 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002251 } else {
2252 ActivityOptions.abort(options);
2253 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002254 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002255 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2256 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002257 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002258 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002259 // The caller has requested to completely replace any
2260 // existing task with its new activity. Well that should
2261 // not be too hard...
2262 reuseTask = intentActivity.task;
2263 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002264 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002265 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002266 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002267 // In this situation we want to remove all activities
2268 // from the task up to the one being started. In most
2269 // cases this means we are resetting the task to its
2270 // initial state.
2271 ActivityRecord top =
2272 intentActivity.task.performClearTaskLocked(r, launchFlags);
2273 if (top != null) {
2274 if (top.frontOfTask) {
2275 // Activity aliases may mean we use different
2276 // intents for the top activity, so make sure
2277 // the task now has the identity of the new
2278 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002279 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002280 }
2281 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2282 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002283 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002284 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002285 // A special case: we need to start the activity because it is not
2286 // currently running, and the caller has asked to clear the current
2287 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002288 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002289 // Now pretend like this activity is being started by the top of its
2290 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002291 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002292 TaskRecord task = sourceRecord.task;
2293 if (task != null && task.stack == null) {
2294 // Target stack got cleared when we all activities were removed
2295 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002296 targetStack = computeStackFocus(
2297 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002298 targetStack.addTask(
2299 task, !launchTaskBehind /* toTop */, false /* moving */);
2300 }
2301
Craig Mautner8849a5e2013-04-02 16:41:03 -07002302 }
2303 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2304 // In this case the top activity on the task is the
2305 // same as the one being launched, so we take that
2306 // as a request to bring the task to the foreground.
2307 // If the top activity in the task is the root
2308 // activity, deliver this new intent to it if it
2309 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002310 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002311 && intentActivity.realActivity.equals(r.realActivity)) {
2312 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2313 intentActivity.task);
2314 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002315 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002316 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002317 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2318 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002319 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2320 // In this case we are launching the root activity
2321 // of the task, but with a different intent. We
2322 // should start a new instance on top.
2323 addingToTask = true;
2324 sourceRecord = intentActivity;
2325 }
2326 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2327 // In this case an activity is being launched in to an
2328 // existing task, without resetting that task. This
2329 // is typically the situation of launching an activity
2330 // from a notification or shortcut. We want to place
2331 // the new activity on top of the current task.
2332 addingToTask = true;
2333 sourceRecord = intentActivity;
2334 } else if (!intentActivity.task.rootWasReset) {
2335 // In this case we are launching in to an existing task
2336 // that has not yet been started from its front door.
2337 // The current task has been brought to the front.
2338 // Ideally, we'd probably like to place this new task
2339 // at the bottom of its stack, but that's a little hard
2340 // to do with the current organization of the code so
2341 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002342 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002343 }
2344 if (!addingToTask && reuseTask == null) {
2345 // We didn't do anything... but it was needed (a.k.a., client
2346 // don't use that intent!) And for paranoia, make
2347 // sure we have correctly resumed the top activity.
2348 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002349 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002350 if (!movedToFront) {
2351 // Make sure to notify Keyguard as well if we are not running an app
2352 // transition later.
2353 notifyActivityDrawnForKeyguard();
2354 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002355 } else {
2356 ActivityOptions.abort(options);
2357 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002358 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002359 return ActivityManager.START_TASK_TO_FRONT;
2360 }
2361 }
2362 }
2363 }
2364
2365 //String uri = r.intent.toURI();
2366 //Intent intent2 = new Intent(uri);
2367 //Slog.i(TAG, "Given intent: " + r.intent);
2368 //Slog.i(TAG, "URI is: " + uri);
2369 //Slog.i(TAG, "To intent: " + intent2);
2370
2371 if (r.packageName != null) {
2372 // If the activity being launched is the same as the one currently
2373 // at the top, then we need to check if it should only be launched
2374 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002375 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002376 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002377 if (top != null && r.resultTo == null) {
2378 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2379 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002380 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002381 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002382 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2383 top.task);
2384 // For paranoia, make sure we have correctly
2385 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002386 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002387 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002388 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002389 }
2390 ActivityOptions.abort(options);
2391 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2392 // We don't need to start a new activity, and
2393 // the client said not to do anything if that
2394 // is the case, so this is it!
2395 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2396 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002397 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002398 return ActivityManager.START_DELIVERED_TO_TOP;
2399 }
2400 }
2401 }
2402 }
2403
2404 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002405 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002406 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2407 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002408 }
2409 ActivityOptions.abort(options);
2410 return ActivityManager.START_CLASS_NOT_FOUND;
2411 }
2412
2413 boolean newTask = false;
2414 boolean keepCurTransition = false;
2415
Craig Mautnerbb742462014-07-07 15:28:55 -07002416 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002417 sourceRecord.task : null;
2418
Craig Mautner8849a5e2013-04-02 16:41:03 -07002419 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002420 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002421 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002422 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002423 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002424 if (doResume) {
2425 targetStack.moveToFront("startingNewTask");
2426 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002427
Craig Mautner8849a5e2013-04-02 16:41:03 -07002428 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002429 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2430 newTaskInfo != null ? newTaskInfo : r.info,
2431 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002432 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2433 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002434 if (overrideBounds) {
2435 r.task.updateOverrideConfiguration(newBounds);
2436 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002437 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2438 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002439 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002440 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002441 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002442 if (isLockTaskModeViolation(r.task)) {
2443 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2444 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2445 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002446 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002447 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002448 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2449 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002450 // Caller wants to appear on home activity, so before starting
2451 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002452 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002453 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002454 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002455 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002456 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002457 if (isLockTaskModeViolation(sourceTask)) {
2458 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2459 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2460 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002461 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002462 if (doResume) {
2463 targetStack.moveToFront("sourceStackToFront");
2464 }
Craig Mautner737fae22014-10-15 12:52:10 -07002465 final TaskRecord topTask = targetStack.topTask();
2466 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002467 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002468 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002469 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002470 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002471 // In this case, we are adding the activity to an existing
2472 // task, but the caller has asked to clear that task if the
2473 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002474 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002475 keepCurTransition = true;
2476 if (top != null) {
2477 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002478 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002479 // For paranoia, make sure we have correctly
2480 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002481 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002482 if (doResume) {
2483 targetStack.resumeTopActivityLocked(null);
2484 }
2485 ActivityOptions.abort(options);
2486 return ActivityManager.START_DELIVERED_TO_TOP;
2487 }
2488 } else if (!addingToTask &&
2489 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2490 // In this case, we are launching an activity in our own task
2491 // that may already be running somewhere in the history, and
2492 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002493 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002494 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002495 final TaskRecord task = top.task;
2496 task.moveActivityToFrontLocked(top);
2497 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002498 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002499 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002500 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002501 if (doResume) {
2502 targetStack.resumeTopActivityLocked(null);
2503 }
2504 return ActivityManager.START_DELIVERED_TO_TOP;
2505 }
2506 }
2507 // An existing activity is starting this new activity, so we want
2508 // to keep the new one in the same task as the one that is starting
2509 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002510 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002511 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002512 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002513
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002514 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002515 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002516 // task it has provided to us.
2517 if (isLockTaskModeViolation(inTask)) {
2518 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2519 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2520 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002521 if (overrideBounds) {
2522 inTask.updateOverrideConfiguration(newBounds);
2523 int stackId = inTask.getLaunchStackId();
2524 if (stackId != inTask.stack.mStackId) {
2525 moveTaskToStackUncheckedLocked(
2526 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2527 }
2528 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002529 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002530 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2531 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002532
2533 // Check whether we should actually launch the new activity in to the task,
2534 // or just reuse the current activity on top.
2535 ActivityRecord top = inTask.getTopActivity();
2536 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2537 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2538 || launchSingleTop || launchSingleTask) {
2539 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2540 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2541 // We don't need to start a new activity, and
2542 // the client said not to do anything if that
2543 // is the case, so this is it!
2544 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2545 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002546 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002547 return ActivityManager.START_DELIVERED_TO_TOP;
2548 }
2549 }
2550
Dianne Hackborn962d5352014-08-29 16:27:40 -07002551 if (!addingToTask) {
2552 // We don't actually want to have this activity added to the task, so just
2553 // stop here but still tell the caller that we consumed the intent.
2554 ActivityOptions.abort(options);
2555 return ActivityManager.START_TASK_TO_FRONT;
2556 }
2557
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002558 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002559 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002560 + " in explicit task " + r.task);
2561
Craig Mautner8849a5e2013-04-02 16:41:03 -07002562 } else {
2563 // This not being started from an existing activity, and not part
2564 // of a new task... just put it in the top task, though these days
2565 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002566 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002567 if (doResume) {
2568 targetStack.moveToFront("addingToTopTask");
2569 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002570 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002571 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002572 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002573 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002574 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002575 + " in new guessed " + r.task);
2576 }
2577
2578 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002579 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002580
Craig Mautner76e2a762014-06-24 09:05:43 -07002581 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2582 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2583 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002584 if (newTask) {
2585 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2586 }
2587 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002588 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002589 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002590 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002591 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002592 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002593 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002594 return ActivityManager.START_SUCCESS;
2595 }
2596
Craig Mautneree36c772014-07-16 14:56:05 -07002597 final void doPendingActivityLaunchesLocked(boolean doResume) {
2598 while (!mPendingActivityLaunches.isEmpty()) {
2599 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002600
2601 try {
2602 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002603 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002604 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002605 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2606 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002607 }
Craig Mautneree36c772014-07-16 14:56:05 -07002608 }
2609 }
2610
Craig Mautner7f13ed32014-07-28 14:00:35 -07002611 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002612 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2613 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002614 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002615 mPendingActivityLaunches.remove(palNdx);
2616 }
2617 }
2618 }
2619
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002620 void setLaunchSource(int uid) {
2621 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2622 }
2623
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002624 void acquireLaunchWakelock() {
2625 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2626 throw new IllegalStateException("Calling must be system uid");
2627 }
2628 mLaunchingActivity.acquire();
2629 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2630 // To be safe, don't allow the wake lock to be held for too long.
2631 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2632 }
2633 }
2634
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002635 /**
2636 * Called when the frontmost task is idle.
2637 * @return the state of mService.mBooting before this was called.
2638 */
2639 private boolean checkFinishBootingLocked() {
2640 final boolean booting = mService.mBooting;
2641 boolean enableScreen = false;
2642 mService.mBooting = false;
2643 if (!mService.mBooted) {
2644 mService.mBooted = true;
2645 enableScreen = true;
2646 }
2647 if (booting || enableScreen) {
2648 mService.postFinishBooting(booting, enableScreen);
2649 }
2650 return booting;
2651 }
2652
Craig Mautnerf3333272013-04-22 10:55:53 -07002653 // Checked.
2654 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2655 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002656 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002657
Craig Mautnerf3333272013-04-22 10:55:53 -07002658 ArrayList<ActivityRecord> stops = null;
2659 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002660 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002661 int NS = 0;
2662 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002663 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002664 boolean activityRemoved = false;
2665
Wale Ogunwale7d701172015-03-11 15:36:30 -07002666 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002667 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002668 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2669 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002670 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2671 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002672 if (fromTimeout) {
2673 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002674 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002675
2676 // This is a hack to semi-deal with a race condition
2677 // in the client where it can be constructed with a
2678 // newer configuration from when we asked it to launch.
2679 // We'll update with whatever configuration it now says
2680 // it used to launch.
2681 if (config != null) {
2682 r.configuration = config;
2683 }
2684
2685 // We are now idle. If someone is waiting for a thumbnail from
2686 // us, we can now deliver.
2687 r.idle = true;
2688
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002689 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002690 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002691 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002692 }
2693 }
2694
2695 if (allResumedActivitiesIdle()) {
2696 if (r != null) {
2697 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002698 }
2699
2700 if (mLaunchingActivity.isHeld()) {
2701 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2702 if (VALIDATE_WAKE_LOCK_CALLER &&
2703 Binder.getCallingUid() != Process.myUid()) {
2704 throw new IllegalStateException("Calling must be system uid");
2705 }
2706 mLaunchingActivity.release();
2707 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002708 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002709 }
2710
2711 // Atomically retrieve all of the other things to do.
2712 stops = processStoppingActivitiesLocked(true);
2713 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002714 if ((NF = mFinishingActivities.size()) > 0) {
2715 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002716 mFinishingActivities.clear();
2717 }
2718
Craig Mautnerf3333272013-04-22 10:55:53 -07002719 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002720 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002721 mStartingUsers.clear();
2722 }
2723
Craig Mautnerf3333272013-04-22 10:55:53 -07002724 // Stop any activities that are scheduled to do so but have been
2725 // waiting for the next one to start.
2726 for (int i = 0; i < NS; i++) {
2727 r = stops.get(i);
2728 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002729 if (stack != null) {
2730 if (r.finishing) {
2731 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2732 } else {
2733 stack.stopActivityLocked(r);
2734 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002735 }
2736 }
2737
2738 // Finish any activities that are scheduled to do so but have been
2739 // waiting for the next one to start.
2740 for (int i = 0; i < NF; i++) {
2741 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002742 final ActivityStack stack = r.task.stack;
2743 if (stack != null) {
2744 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2745 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002746 }
2747
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002748 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002749 // Complete user switch
2750 if (startingUsers != null) {
2751 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002752 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002753 }
2754 }
2755 // Complete starting up of background users
2756 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002757 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002758 mStartingBackgroundUsers.clear();
2759 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002760 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002761 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002762 }
2763 }
2764
2765 mService.trimApplications();
2766 //dump();
2767 //mWindowManager.dump();
2768
Craig Mautnerf3333272013-04-22 10:55:53 -07002769 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002770 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002771 }
2772
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002773 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002774 }
2775
Craig Mautner8e569572013-10-11 17:36:59 -07002776 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002777 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002778 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2779 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002780 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2781 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2782 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002783 }
Craig Mautner19091252013-10-05 00:03:53 -07002784 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002785 }
2786
2787 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002788 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2789 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002790 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2791 stacks.get(stackNdx).closeSystemDialogsLocked();
2792 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002793 }
2794 }
2795
Craig Mautner93529a42013-10-04 15:03:13 -07002796 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002797 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002798 }
2799
Craig Mautner8d341ef2013-03-26 09:03:27 -07002800 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002801 * Update the last used stack id for non-current user (current user's last
2802 * used stack is the focused stack)
2803 */
2804 void updateUserStackLocked(int userId, ActivityStack stack) {
2805 if (userId != mCurrentUser) {
2806 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2807 }
2808 }
2809
2810 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002811 * @return true if some activity was finished (or would have finished if doit were true).
2812 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002813 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2814 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002815 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002816 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2817 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002818 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002819 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002820 if (stack.finishDisabledPackageActivitiesLocked(
2821 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002822 didSomething = true;
2823 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002824 }
2825 }
2826 return didSomething;
2827 }
2828
Dianne Hackborna413dc02013-07-12 12:02:55 -07002829 void updatePreviousProcessLocked(ActivityRecord r) {
2830 // Now that this process has stopped, we may want to consider
2831 // it to be the previous app to try to keep around in case
2832 // the user wants to return to it.
2833
2834 // First, found out what is currently the foreground app, so that
2835 // we don't blow away the previous app if this activity is being
2836 // hosted by the process that is actually still the foreground.
2837 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002838 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2839 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002840 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2841 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002842 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002843 if (stack.mResumedActivity != null) {
2844 fgApp = stack.mResumedActivity.app;
2845 } else if (stack.mPausingActivity != null) {
2846 fgApp = stack.mPausingActivity.app;
2847 }
2848 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002849 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002850 }
2851 }
2852
2853 // Now set this one as the previous process, only if that really
2854 // makes sense to.
2855 if (r.app != null && fgApp != null && r.app != fgApp
2856 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002857 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002858 mService.mPreviousProcess = r.app;
2859 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2860 }
2861 }
2862
Craig Mautner05d29032013-05-03 13:40:13 -07002863 boolean resumeTopActivitiesLocked() {
2864 return resumeTopActivitiesLocked(null, null, null);
2865 }
2866
Chong Zhang280d3322015-11-03 17:27:26 -08002867 boolean resumeTopActivitiesLocked(
2868 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Craig Mautner05d29032013-05-03 13:40:13 -07002869 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002870 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002871 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002872 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002873 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002874 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002875 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2876 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002877
Craig Mautnere0a38842013-12-16 16:14:02 -08002878 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2879 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002880 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2881 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002882 if (stack == targetStack) {
2883 // Already started above.
2884 continue;
2885 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002886 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002887 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002888 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002889 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002890 }
Craig Mautner05d29032013-05-03 13:40:13 -07002891 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002892 }
2893
Todd Kennedy539db512014-12-15 09:57:55 -08002894 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002895 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2896 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002897 final int numStacks = stacks.size();
2898 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2899 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002900 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002901 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002902 }
2903 }
2904
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002905 void finishVoiceTask(IVoiceInteractionSession session) {
2906 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2907 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2908 final int numStacks = stacks.size();
2909 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2910 final ActivityStack stack = stacks.get(stackNdx);
2911 stack.finishVoiceTask(session);
2912 }
2913 }
2914 }
2915
Chong Zhang280d3322015-11-03 17:27:26 -08002916 void findTaskToMoveToFrontLocked(
2917 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002918 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2919 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002920 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002921 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2922 // Caller wants the home activity moved with it. To accomplish this,
2923 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002924 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002925 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002926 if (task.stack == null) {
2927 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2928 + task + " to front. Stack is null");
2929 return;
2930 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002931
Chong Zhang0fa656b2015-08-31 15:17:21 -07002932 if (task.mResizeable && options != null) {
Chong Zhang280d3322015-11-03 17:27:26 -08002933 if (options.hasBounds()) {
2934 Rect bounds = options.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002935 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08002936 final int stackId = task.getLaunchStackId();
2937 if (stackId != task.stack.mStackId) {
2938 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2939 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2940 // still need moveTaskToFrontLocked() below for any transition settings.
2941 }
2942 // WM resizeTask must be done after the task is moved to the correct stack,
2943 // because Task's setBounds() also updates dim layer's bounds, but that has
2944 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07002945 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2946 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002947 }
2948 }
2949
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002950 final ActivityRecord r = task.getTopActivity();
2951 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2952 r == null ? null : r.appTimeTracker, reason);
2953
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002954 if (DEBUG_STACK) Slog.d(TAG_STACK,
2955 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002956 }
2957
Craig Mautner967212c2013-04-13 21:10:58 -07002958 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002959 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002960 }
2961
2962 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002963 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2964 if (activityContainer != null) {
2965 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002966 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002967 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002968 return null;
2969 }
2970 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002971 }
2972
Craig Mautner967212c2013-04-13 21:10:58 -07002973 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002974 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002975 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2976 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002977 }
2978 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002979 }
2980
Craig Mautner4a1cb222013-12-04 16:14:06 -08002981 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002982 ActivityRecord homeActivity = getHomeActivity();
2983 if (homeActivity != null) {
2984 return homeActivity.appToken;
2985 }
2986 return null;
2987 }
2988
2989 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002990 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002991 }
2992
2993 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002994 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2995 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2996 final TaskRecord task = tasks.get(taskNdx);
2997 if (task.isHomeTask()) {
2998 final ArrayList<ActivityRecord> activities = task.mActivities;
2999 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3000 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003001 if (r.isHomeActivity()
3002 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003003 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003004 }
3005 }
3006 }
3007 }
3008 return null;
3009 }
3010
Todd Kennedyca4d8422015-01-15 15:19:22 -08003011 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003012 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003013 ActivityContainer activityContainer =
3014 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003015 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003016 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3017 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003018 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003019 return activityContainer;
3020 }
3021
Craig Mautner34b73df2014-01-12 21:11:08 -08003022 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003023 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
3024 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
3025 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003026 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
3027 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003028 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08003029 }
3030 }
3031
Craig Mautner95da1082014-02-24 17:54:35 -08003032 void deleteActivityContainer(IActivityContainer container) {
3033 ActivityContainer activityContainer = (ActivityContainer)container;
3034 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003035 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3036 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08003037 final int stackId = activityContainer.mStackId;
3038 mActivityContainers.remove(stackId);
3039 mWindowManager.removeStack(stackId);
3040 }
3041 }
3042
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003043 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
3044 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003045 final ActivityStack stack = getStack(stackId);
3046 if (stack == null) {
3047 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
3048 return;
3049 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003050
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003051 if (!allowResizeInDockedMode
3052 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3053 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3054 // stack size changing so things don't get out of sync.
3055 return;
3056 }
3057
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003058 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003059 mWindowManager.deferSurfaceLayout();
3060 try {
3061 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003062
Jorim Jaggic4025202015-10-22 16:43:34 +02003063 mTmpBounds.clear();
3064 mTmpConfigs.clear();
3065 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3066 for (int i = tasks.size() - 1; i >= 0; i--) {
3067 TaskRecord task = tasks.get(i);
3068 if (task.mResizeable) {
3069 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3070 // For freeform stack we don't adjust the size of the tasks to match that
3071 // of the stack, but we do try to make sure the tasks are still contained
3072 // with the bounds of the stack.
3073 tempRect2.set(task.mBounds);
3074 fitWithinBounds(tempRect2, bounds);
3075 task.updateOverrideConfiguration(tempRect2);
3076 } else {
3077 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003078 }
3079 }
3080
Jorim Jaggic4025202015-10-22 16:43:34 +02003081 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3082 mTmpBounds.put(task.taskId, task.mBounds);
3083 }
3084 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3085 if (stack.mStackId == DOCKED_STACK_ID) {
3086 // Dock stack funness...Yay!
3087 if (stack.mFullscreen) {
3088 // The dock stack went fullscreen which is kinda like dismissing it.
3089 // In this case we make all other static stacks fullscreen and move all
3090 // docked stack tasks to the fullscreen stack.
3091 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003092 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003093 resizeStackLocked(i, null, preserveWindows, true);
3094 }
3095 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003096
Jorim Jaggic4025202015-10-22 16:43:34 +02003097 final int count = tasks.size();
3098 for (int i = 0; i < count; i++) {
3099 moveTaskToStackLocked(tasks.get(i).taskId,
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003100 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack");
Jorim Jaggic4025202015-10-22 16:43:34 +02003101 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003102
Jorim Jaggic4025202015-10-22 16:43:34 +02003103 // stack shouldn't contain anymore activities, so nothing to resume.
3104 r = null;
3105 } else {
3106 // Docked stacks occupy a dedicated region on screen so the size of all other
3107 // static stacks need to be adjusted so they don't overlap with the docked stack.
3108 // We get the bounds to use from window manager which has been adjusted for any
3109 // screen controls and is also the same for all stacks.
3110 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3111
3112 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003113 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003114 ActivityStack otherStack = getStack(i);
3115 if (otherStack != null) {
3116 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3117 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003118 }
3119 }
3120 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003121 // Since we are resizing the stack, all other operations should strive to preserve
3122 // windows.
3123 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003124 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003125 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003126
Jorim Jaggic4025202015-10-22 16:43:34 +02003127 if (r != null) {
3128 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3129 // And we need to make sure at this point that all other activities
3130 // are made visible with the correct configuration.
3131 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3132 if (!updated) {
3133 resumeTopActivitiesLocked(stack, null, null);
3134 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003135 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003136 } finally {
3137 mWindowManager.continueSurfaceLayout();
3138 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003139 }
3140 }
3141
Chong Zhang6de2ae82015-09-30 18:25:21 -07003142 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003143 if (!task.mResizeable) {
3144 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3145 return;
3146 }
3147
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003148 adjustForMinimalTaskDimensions(task, bounds);
3149
Chong Zhang87b21722015-09-21 15:39:51 -07003150 // If this is a forced resize, let it go through even if the bounds is not changing,
3151 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003152 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003153 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003154 // Nothing to do here...
3155 return;
3156 }
3157
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003158 if (!mWindowManager.isValidTaskId(task.taskId)) {
3159 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003160 // All we can do for now is update the bounds so it can be used when the task is
3161 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003162 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003163 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3164 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003165 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003166 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003167 return;
3168 }
3169
Chong Zhang6de2ae82015-09-30 18:25:21 -07003170 // Do not move the task to another stack here.
3171 // This method assumes that the task is already placed in the right stack.
3172 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003173
Chong Zhang6de2ae82015-09-30 18:25:21 -07003174 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003175
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003176 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003177 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003178 // way and the activity was kept the way it was. If it's false, it means the activity had
3179 // to be relaunched due to configuration change.
3180 boolean kept = true;
3181 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003182 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003183 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003184 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003185 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003186 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003187 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003188 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003189 resumeTopActivitiesLocked(stack, null, null);
3190 }
3191 }
3192 }
Chong Zhang87b21722015-09-21 15:39:51 -07003193 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003194
3195 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003196 }
3197
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003198 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3199 if (bounds == null) {
3200 return;
3201 }
3202 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3203 : task.mMinimalSize;
3204 final boolean adjustWidth = minimalSize > bounds.width();
3205 final boolean adjustHeight = minimalSize > bounds.height();
3206 if (!(adjustWidth || adjustHeight)) {
3207 return;
3208 }
3209 Rect taskBounds = task.mBounds;
3210 if (adjustWidth) {
3211 if (taskBounds != null && bounds.right == taskBounds.right) {
3212 bounds.left = bounds.right - minimalSize;
3213 } else {
3214 // Either left bounds match, or neither match, or the previous bounds were
3215 // fullscreen and we default to keeping left.
3216 bounds.right = bounds.left + minimalSize;
3217 }
3218 }
3219 if (adjustHeight) {
3220 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3221 bounds.top = bounds.bottom - minimalSize;
3222 } else {
3223 // Either top bounds match, or neither match, or the previous bounds were
3224 // fullscreen and we default to keeping top.
3225 bounds.bottom = bounds.top + minimalSize;
3226 }
3227 }
3228 }
3229
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003230 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003231 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3232 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003233 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003234 }
3235
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003236 ActivityContainer activityContainer = new ActivityContainer(stackId);
3237 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003238 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003239 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003240 }
3241
3242 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003243 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003244 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3245 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003246 break;
3247 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003248 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003249 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003250 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003251 }
3252
Chong Zhang5dcb2752015-08-18 13:50:26 -07003253 /**
3254 * Restores a recent task to a stack
3255 * @param task The recent task to be restored.
3256 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003257 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003258 * @return true if the task has been restored successfully.
3259 */
3260 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3261 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003262 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003263 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003264 if (task.stack != null) {
3265 // Task has already been restored once. See if we need to do anything more
3266 if (task.stack.mStackId == stackId) {
3267 // Nothing else to do since it is already restored in the right stack.
3268 return true;
3269 }
3270 // Remove current stack association, so we can re-associate the task with the
3271 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003272 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003273 }
3274
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003275 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003276 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003277
3278 if (stack == null) {
3279 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003280 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3281 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003282 return false;
3283 }
3284
3285 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003286 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3287 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003288 final ArrayList<ActivityRecord> activities = task.mActivities;
3289 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003290 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003291 }
3292 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003293 }
3294
Wale Ogunwale040b4702015-08-06 18:10:50 -07003295 /**
3296 * Moves the specified task record to the input stack id.
3297 * WARNING: This method performs an unchecked/raw move of the task and
3298 * can leave the system in an unstable state if used incorrectly.
3299 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3300 * to a stack.
3301 * @param task Task to move.
3302 * @param stackId Id of stack to move task to.
3303 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003304 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003305 * @param reason Reason the task is been moved.
3306 * @return The stack the task was moved to.
3307 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003308 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003309 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3310 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003311 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003312 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3313
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003314 final boolean resizeable = task.mResizeable;
3315 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3316 // if a docked stack is created below which will lead to the stack we are moving from and
3317 // its resizeable tasks being resized.
3318 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003319 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003320 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003321 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3322 if (task.stack != null) {
3323 task.stack.removeTask(task, reason, MOVING);
3324 }
3325 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003326
3327 // If the task had focus before (or we're requested to move focus),
3328 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003329 stack.setFocusAndResumeStateIfNeeded(
3330 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003331
Wale Ogunwale040b4702015-08-06 18:10:50 -07003332 return stack;
3333 }
3334
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003335 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
3336 String reason) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003337 final TaskRecord task = anyTaskForIdLocked(taskId);
3338 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003339 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003340 return;
3341 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003342
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003343 if (task.stack != null && task.stack.mStackId == stackId) {
3344 // You are already in the right stack silly...
3345 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
3346 return;
3347 }
3348
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003349 final ActivityRecord topActivity = task.getTopActivity();
3350 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003351 // We are about to relaunch the activity because its configuration changed due to
3352 // being maximized, i.e. size change. The activity will first remove the old window
3353 // and then add a new one. This call will tell window manager about this, so it can
3354 // preserve the old window until the new one is drawn. This prevents having a gap
3355 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003356 // entrance of the new window to be properly animated.
Wale Ogunwale3cd48042015-11-16 13:01:41 -08003357 mWindowManager.setReplacingWindow(topActivity.appToken);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003358 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08003359 final ActivityStack stack = moveTaskToStackUncheckedLocked(
3360 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003361
3362 // Make sure the task has the appropriate bounds/size for the stack it is in.
3363 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003364 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003365 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3366 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003367 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3368 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003369 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003370 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003371 }
3372
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003373 // The task might have already been running and its visibility needs to be synchronized with
3374 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003375 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003376 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003377 }
3378
Wale Ogunwale079a0042015-10-24 11:44:07 -07003379 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3380 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3381 if (stack == null) {
3382 throw new IllegalArgumentException(
3383 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3384 }
3385
3386 final ActivityRecord r = stack.topRunningActivityLocked();
3387 if (r == null) {
3388 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3389 + " in stack=" + stack);
3390 return false;
3391 }
3392
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003393 if (!r.info.supportsPip) {
3394 Slog.w(TAG,
3395 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003396 + " r=" + r);
3397 return false;
3398 }
3399
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003400 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003401 if (task.mActivities.size() == 1) {
3402 // There is only one activity in the task. So, we can just move the task over to the
3403 // pinned stack without re-parenting the activity in a different task.
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003404 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS,
3405 "moveTopActivityToPinnedStack");
Wale Ogunwale079a0042015-10-24 11:44:07 -07003406 } else {
3407 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3408 pinnedStack.moveActivityToStack(r);
3409 }
3410
3411 resizeStackLocked(PINNED_STACK_ID, bounds, PRESERVE_WINDOWS, true);
3412
3413 // The task might have already been running and its visibility needs to be synchronized with
3414 // the visibility of the stack / windows.
3415 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3416 resumeTopActivitiesLocked();
3417 return true;
3418 }
3419
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003420 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3421 final TaskRecord task = anyTaskForIdLocked(taskId);
3422 if (task == null) {
3423 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3424 return;
3425 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08003426 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
3427
3428 task.updateOverrideConfigurationForStack(stack);
3429
3430 mWindowManager.positionTaskInStack(
3431 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003432 final boolean stackChanged = task.stack != null && task.stack != stack;
3433 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003434 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003435 }
3436 stack.positionTask(task, position, stackChanged);
3437 // The task might have already been running and its visibility needs to be synchronized with
3438 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003439 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003440 resumeTopActivitiesLocked();
3441 }
3442
Craig Mautnerac6f8432013-07-17 13:24:59 -07003443 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003444 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003445 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3446 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003447 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3448 final ActivityStack stack = stacks.get(stackNdx);
3449 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003450 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003451 continue;
3452 }
3453 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003454 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3455 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003456 continue;
3457 }
3458 final ActivityRecord ar = stack.findTaskLocked(r);
3459 if (ar != null) {
3460 return ar;
3461 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003462 }
3463 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003464 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003465 return null;
3466 }
3467
3468 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003469 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3470 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3472 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3473 if (ar != null) {
3474 return ar;
3475 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003476 }
3477 }
3478 return null;
3479 }
3480
Craig Mautner8d341ef2013-03-26 09:03:27 -07003481 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003482 scheduleSleepTimeout();
3483 if (!mGoingToSleep.isHeld()) {
3484 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003485 if (mLaunchingActivity.isHeld()) {
3486 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3487 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003488 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003489 mLaunchingActivity.release();
3490 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003491 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003492 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003493 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003494 }
3495
3496 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003497 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003498
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003499 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003500 final long endTime = System.currentTimeMillis() + timeout;
3501 while (true) {
3502 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003503 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3504 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003505 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3506 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3507 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003508 }
3509 if (cantShutdown) {
3510 long timeRemaining = endTime - System.currentTimeMillis();
3511 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003512 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003513 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003514 } catch (InterruptedException e) {
3515 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003516 } else {
3517 Slog.w(TAG, "Activity manager shutdown timed out");
3518 timedout = true;
3519 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003520 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003521 } else {
3522 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003523 }
3524 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003525
3526 // Force checkReadyForSleep to complete.
3527 mSleepTimeout = true;
3528 checkReadyForSleepLocked();
3529
Craig Mautner8d341ef2013-03-26 09:03:27 -07003530 return timedout;
3531 }
3532
3533 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003534 removeSleepTimeouts();
3535 if (mGoingToSleep.isHeld()) {
3536 mGoingToSleep.release();
3537 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003538 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3539 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003540 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3541 final ActivityStack stack = stacks.get(stackNdx);
3542 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003543 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003544 resumeTopActivitiesLocked();
3545 }
Craig Mautner5314a402013-09-26 12:40:16 -07003546 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003547 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003548 mGoingToSleepActivities.clear();
3549 }
3550
3551 void activitySleptLocked(ActivityRecord r) {
3552 mGoingToSleepActivities.remove(r);
3553 checkReadyForSleepLocked();
3554 }
3555
3556 void checkReadyForSleepLocked() {
3557 if (!mService.isSleepingOrShuttingDown()) {
3558 // Do not care.
3559 return;
3560 }
3561
3562 if (!mSleepTimeout) {
3563 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003564 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3565 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003566 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3567 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3568 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003569 }
3570
3571 if (mStoppingActivities.size() > 0) {
3572 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003573 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003574 + mStoppingActivities.size() + " activities");
3575 scheduleIdleLocked();
3576 dontSleep = true;
3577 }
3578
3579 if (mGoingToSleepActivities.size() > 0) {
3580 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003581 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003582 + mGoingToSleepActivities.size() + " activities");
3583 dontSleep = true;
3584 }
3585
3586 if (dontSleep) {
3587 return;
3588 }
3589 }
3590
Craig Mautnere0a38842013-12-16 16:14:02 -08003591 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3592 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003593 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3594 stacks.get(stackNdx).goToSleep();
3595 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003596 }
3597
3598 removeSleepTimeouts();
3599
3600 if (mGoingToSleep.isHeld()) {
3601 mGoingToSleep.release();
3602 }
3603 if (mService.mShuttingDown) {
3604 mService.notifyAll();
3605 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003606 }
3607
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003608 boolean reportResumedActivityLocked(ActivityRecord r) {
3609 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003610 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003611 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003612 }
3613 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003614 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003615 mWindowManager.executeAppTransition();
3616 return true;
3617 }
3618 return false;
3619 }
3620
Craig Mautner8d341ef2013-03-26 09:03:27 -07003621 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003622 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3623 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003624 int stackNdx = stacks.size() - 1;
3625 while (stackNdx >= 0) {
3626 stacks.get(stackNdx).handleAppCrashLocked(app);
3627 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003628 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003629 }
3630 }
3631
Jose Lima4b6c6692014-08-12 17:41:12 -07003632 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003633 final ActivityStack stack = r.task.stack;
3634 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003635 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3636 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003637 return false;
3638 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003639 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003640 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3641 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003642
3643 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003644 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003645 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003646 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003647 return true;
3648 }
3649
3650 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003651 if (visible && top.fullscreen) {
3652 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003653 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3654 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3655 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3656 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003657 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003658 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3659 // Only the activity set as currently visible behind should actively reset its
3660 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003661 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3662 "requestVisibleBehind: returning visible=" + visible
3663 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3664 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003665 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003666 }
3667
Jose Lima4b6c6692014-08-12 17:41:12 -07003668 stack.setVisibleBehindActivity(visible ? r : null);
3669 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003670 // Make the activity immediately above r opaque.
3671 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3672 if (next != null) {
3673 mService.convertFromTranslucent(next.appToken);
3674 }
3675 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003676 if (top.app != null && top.app.thread != null) {
3677 // Notify the top app of the change.
3678 try {
3679 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3680 } catch (RemoteException e) {
3681 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003682 }
3683 return true;
3684 }
3685
Craig Mautnerbb742462014-07-07 15:28:55 -07003686 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3687 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3688 r.mLaunchTaskBehind = false;
3689 final TaskRecord task = r.task;
Winsonc809cbb2015-11-02 12:06:15 -08003690 task.setLastThumbnailLocked(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003691 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003692 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003693 mWindowManager.setAppVisibility(r.appToken, false);
3694 }
3695
3696 void scheduleLaunchTaskBehindComplete(IBinder token) {
3697 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3698 }
3699
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003700 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3701 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003702 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3704 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003705 final int topStackNdx = stacks.size() - 1;
3706 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3707 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003708 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003709 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003710 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003711 }
3712
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003713 void invalidateTaskLayers() {
3714 mTaskLayersChanged = true;
3715 }
3716
3717 void rankTaskLayersIfNeeded() {
3718 if (!mTaskLayersChanged) {
3719 return;
3720 }
3721 mTaskLayersChanged = false;
3722 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3723 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3724 int baseLayer = 0;
3725 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3726 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3727 }
3728 }
3729 }
3730
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003731 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3732 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3733 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3734 final int topStackNdx = stacks.size() - 1;
3735 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3736 final ActivityStack stack = stacks.get(stackNdx);
3737 stack.clearOtherAppTimeTrackers(except);
3738 }
3739 }
3740 }
3741
Craig Mautner8d341ef2013-03-26 09:03:27 -07003742 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003743 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3744 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003745 final int numStacks = stacks.size();
3746 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3747 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003748 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003749 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003750 }
3751 }
3752
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003753 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3754 // Examine all activities currently running in the process.
3755 TaskRecord firstTask = null;
3756 // Tasks is non-null only if two or more tasks are found.
3757 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003758 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3759 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003760 ActivityRecord r = app.activities.get(i);
3761 // First, if we find an activity that is in the process of being destroyed,
3762 // then we just aren't going to do anything for now; we want things to settle
3763 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003764 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003765 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003766 return;
3767 }
3768 // Don't consider any activies that are currently not in a state where they
3769 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003770 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3771 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003772 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003773 continue;
3774 }
3775 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003776 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003777 + " from " + r);
3778 if (firstTask == null) {
3779 firstTask = r.task;
3780 } else if (firstTask != r.task) {
3781 if (tasks == null) {
3782 tasks = new ArraySet<>();
3783 tasks.add(firstTask);
3784 }
3785 tasks.add(r.task);
3786 }
3787 }
3788 }
3789 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003790 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003791 return;
3792 }
3793 // If we have activities in multiple tasks that are in a position to be destroyed,
3794 // let's iterate through the tasks and release the oldest one.
3795 final int numDisplays = mActivityDisplays.size();
3796 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3797 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3798 // Step through all stacks starting from behind, to hit the oldest things first.
3799 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3800 final ActivityStack stack = stacks.get(stackNdx);
3801 // Try to release activities in this stack; if we manage to, we are done.
3802 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3803 return;
3804 }
3805 }
3806 }
3807 }
3808
Amith Yamasani37a40c22015-06-17 13:25:42 -07003809 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003810 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003811 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003812 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003813
Craig Mautner858d8a62013-04-23 17:08:34 -07003814 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003815 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3816 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003817 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003818 final ActivityStack stack = stacks.get(stackNdx);
3819 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003820 TaskRecord task = stack.topTask();
3821 if (task != null) {
3822 mWindowManager.moveTaskToTop(task.taskId);
3823 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003824 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003825 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003826
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003827 ActivityStack stack = getStack(restoreStackId);
3828 if (stack == null) {
3829 stack = mHomeStack;
3830 }
3831 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003832 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003833 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003834 } else {
3835 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003836 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003837 }
Craig Mautner93529a42013-10-04 15:03:13 -07003838 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003839 }
3840
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003841 /**
3842 * Add background users to send boot completed events to.
3843 * @param userId The user being started in the background
3844 * @param uss The state object for the user.
3845 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003846 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003847 mStartingBackgroundUsers.add(uss);
3848 }
3849
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003850 /** Checks whether the userid is a profile of the current user. */
3851 boolean isCurrentProfileLocked(int userId) {
3852 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003853 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003854 }
3855
Chong Zhang45c25ce2015-08-10 22:18:26 -07003856 /** Checks whether the activity should be shown for current user. */
3857 boolean okToShowLocked(ActivityRecord r) {
3858 return r != null && (isCurrentProfileLocked(r.userId)
3859 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3860 }
3861
Craig Mautnerde4ef022013-04-07 19:01:33 -07003862 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003863 ArrayList<ActivityRecord> stops = null;
3864
3865 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003866 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3867 ActivityRecord s = mStoppingActivities.get(activityNdx);
3868 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003869 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003870 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3871 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003872 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003873 if (s.finishing) {
3874 // If this activity is finishing, it is sitting on top of
3875 // everyone else but we now know it is no longer needed...
3876 // so get rid of it. Otherwise, we need to go through the
3877 // normal flow and hide it once we determine that it is
3878 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003879 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003880 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003881 }
3882 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003883 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003884 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003885 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003886 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003887 }
3888 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003889 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003890 }
3891 }
3892
3893 return stops;
3894 }
3895
Craig Mautnercf910b02013-04-23 11:23:27 -07003896 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003897 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3898 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3899 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3900 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003901 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003902 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003903 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003904 if (r == null) Slog.e(TAG,
3905 "validateTop...: null top activity, stack=" + stack);
3906 else {
3907 final ActivityRecord pausing = stack.mPausingActivity;
3908 if (pausing != null && pausing == r) Slog.e(TAG,
3909 "validateTop...: top stack has pausing activity r=" + r
3910 + " state=" + state);
3911 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3912 "validateTop...: activity in front not resumed r=" + r
3913 + " state=" + state);
3914 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003915 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003916 final ActivityRecord resumed = stack.mResumedActivity;
3917 if (resumed != null && resumed == r) Slog.e(TAG,
3918 "validateTop...: back stack has resumed activity r=" + r
3919 + " state=" + state);
3920 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3921 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003922 }
3923 }
3924 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003925 }
3926
Craig Mautnere0570202015-05-13 13:06:11 -07003927 private String lockTaskModeToString() {
3928 switch (mLockTaskModeState) {
3929 case LOCK_TASK_MODE_LOCKED:
3930 return "LOCKED";
3931 case LOCK_TASK_MODE_PINNED:
3932 return "PINNED";
3933 case LOCK_TASK_MODE_NONE:
3934 return "NONE";
3935 default: return "unknown=" + mLockTaskModeState;
3936 }
3937 }
3938
Craig Mautner27084302013-03-25 08:05:25 -07003939 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003940 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003941 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003942 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3943 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3944 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003945 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003946 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3947 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3948 if (packages.size() > 0) {
3949 pw.println(" mLockTaskPackages (userId:packages)=");
3950 for (int i = 0; i < packages.size(); ++i) {
3951 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3952 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3953 }
3954 }
3955 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003956 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003957
Craig Mautner20e72272013-04-01 13:45:53 -07003958 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003959 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003960 }
3961
Dianne Hackborn390517b2013-05-30 15:03:32 -07003962 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3963 boolean needSep, String prefix) {
3964 if (activity != null) {
3965 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3966 if (needSep) {
3967 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003968 }
3969 pw.print(prefix);
3970 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003971 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003972 }
3973 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003974 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003975 }
3976
Craig Mautner8d341ef2013-03-26 09:03:27 -07003977 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3978 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003979 boolean printed = false;
3980 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003981 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3982 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003983 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003984 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003985 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003986 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003987 final ActivityStack stack = stacks.get(stackNdx);
3988 StringBuilder stackHeader = new StringBuilder(128);
3989 stackHeader.append(" Stack #");
3990 stackHeader.append(stack.mStackId);
3991 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003992 stackHeader.append("\n");
3993 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3994 stackHeader.append("\n");
3995 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003996 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3997 needSep, stackHeader.toString());
3998 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3999 !dumpAll, false, dumpPackage, true,
4000 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004001
Craig Mautner4a1cb222013-12-04 16:14:06 -08004002 needSep = printed;
4003 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4004 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004005 if (pr) {
4006 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004008 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
4010 " mResumedActivity: ");
4011 if (pr) {
4012 printed = true;
4013 needSep = false;
4014 }
4015 if (dumpAll) {
4016 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4017 " mLastPausedActivity: ");
4018 if (pr) {
4019 printed = true;
4020 needSep = true;
4021 }
4022 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4023 needSep, " mLastNoHistoryActivity: ");
4024 }
4025 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004026 }
4027 }
4028
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004029 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4030 false, dumpPackage, true, " Activities waiting to finish:", null);
4031 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4032 false, dumpPackage, true, " Activities waiting to stop:", null);
4033 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
4034 false, dumpPackage, true, " Activities waiting for another to become visible:",
4035 null);
4036 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4037 false, dumpPackage, true, " Activities waiting to sleep:", null);
4038 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4039 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004040
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004041 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004042 }
4043
Dianne Hackborn390517b2013-05-30 15:03:32 -07004044 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004045 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004046 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004047 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004048 String innerPrefix = null;
4049 String[] args = null;
4050 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004051 for (int i=list.size()-1; i>=0; i--) {
4052 final ActivityRecord r = list.get(i);
4053 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4054 continue;
4055 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004056 if (innerPrefix == null) {
4057 innerPrefix = prefix + " ";
4058 args = new String[0];
4059 }
4060 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004061 final boolean full = !brief && (complete || !r.isInHistory());
4062 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004063 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004064 needNL = false;
4065 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004066 if (header1 != null) {
4067 pw.println(header1);
4068 header1 = null;
4069 }
4070 if (header2 != null) {
4071 pw.println(header2);
4072 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004073 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004074 if (lastTask != r.task) {
4075 lastTask = r.task;
4076 pw.print(prefix);
4077 pw.print(full ? "* " : " ");
4078 pw.println(lastTask);
4079 if (full) {
4080 lastTask.dump(pw, prefix + " ");
4081 } else if (complete) {
4082 // Complete + brief == give a summary. Isn't that obvious?!?
4083 if (lastTask.intent != null) {
4084 pw.print(prefix); pw.print(" ");
4085 pw.println(lastTask.intent.toInsecureStringWithClip());
4086 }
4087 }
4088 }
4089 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4090 pw.print(" #"); pw.print(i); pw.print(": ");
4091 pw.println(r);
4092 if (full) {
4093 r.dump(pw, innerPrefix);
4094 } else if (complete) {
4095 // Complete + brief == give a summary. Isn't that obvious?!?
4096 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4097 if (r.app != null) {
4098 pw.print(innerPrefix); pw.println(r.app);
4099 }
4100 }
4101 if (client && r.app != null && r.app.thread != null) {
4102 // flush anything that is already in the PrintWriter since the thread is going
4103 // to write to the file descriptor directly
4104 pw.flush();
4105 try {
4106 TransferPipe tp = new TransferPipe();
4107 try {
4108 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4109 r.appToken, innerPrefix, args);
4110 // Short timeout, since blocking here can
4111 // deadlock with the application.
4112 tp.go(fd, 2000);
4113 } finally {
4114 tp.kill();
4115 }
4116 } catch (IOException e) {
4117 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4118 } catch (RemoteException e) {
4119 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4120 }
4121 needNL = true;
4122 }
4123 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004124 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004125 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004126
Craig Mautnerf3333272013-04-22 10:55:53 -07004127 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004128 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4129 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004130 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4131 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004132 }
4133
4134 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004135 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004136 }
4137
4138 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004139 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4140 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004141 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4142 }
4143
Craig Mautner05d29032013-05-03 13:40:13 -07004144 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004145 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4146 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4147 }
Craig Mautner05d29032013-05-03 13:40:13 -07004148 }
4149
Craig Mautner0eea92c2013-05-16 13:35:39 -07004150 void removeSleepTimeouts() {
4151 mSleepTimeout = false;
4152 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4153 }
4154
4155 final void scheduleSleepTimeout() {
4156 removeSleepTimeouts();
4157 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4158 }
4159
Craig Mautner4a1cb222013-12-04 16:14:06 -08004160 @Override
4161 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004162 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004163 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4164 }
4165
4166 @Override
4167 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004168 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004169 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4170 }
4171
4172 @Override
4173 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004174 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004175 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4176 }
4177
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004178 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004179 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004180 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004181 newDisplay = mActivityDisplays.get(displayId) == null;
4182 if (newDisplay) {
4183 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004184 if (activityDisplay.mDisplay == null) {
4185 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4186 return;
4187 }
Craig Mautner4504de52013-12-20 09:06:56 -08004188 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004189 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004190 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004191 }
Craig Mautner4504de52013-12-20 09:06:56 -08004192 if (newDisplay) {
4193 mWindowManager.onDisplayAdded(displayId);
4194 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004195 }
4196
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004197 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4198 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4199 return;
4200 }
4201 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4202 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4203 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4204 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4205 }
4206
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004207 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004208 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004209 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4210 if (activityDisplay != null) {
4211 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004212 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004213 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004214 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004215 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216 }
4217 }
4218 mWindowManager.onDisplayRemoved(displayId);
4219 }
4220
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004221 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004222 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004223 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4224 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004225 // TODO: Update the bounds.
4226 }
4227 }
4228 mWindowManager.onDisplayChanged(displayId);
4229 }
4230
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004231 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004232 StackInfo info = new StackInfo();
4233 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4234 info.displayId = Display.DEFAULT_DISPLAY;
4235 info.stackId = stack.mStackId;
4236
4237 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4238 final int numTasks = tasks.size();
4239 int[] taskIds = new int[numTasks];
4240 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004241 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004242 for (int i = 0; i < numTasks; ++i) {
4243 final TaskRecord task = tasks.get(i);
4244 taskIds[i] = task.taskId;
4245 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4246 : task.realActivity != null ? task.realActivity.flattenToString()
4247 : task.getTopActivity() != null ? task.getTopActivity().packageName
4248 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004249 taskBounds[i] = new Rect();
4250 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004251 }
4252 info.taskIds = taskIds;
4253 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004254 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004255 return info;
4256 }
4257
4258 StackInfo getStackInfoLocked(int stackId) {
4259 ActivityStack stack = getStack(stackId);
4260 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004261 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004262 }
4263 return null;
4264 }
4265
4266 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004267 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004268 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4269 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004270 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004271 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004272 }
4273 }
4274 return list;
4275 }
4276
Craig Mautner15df08a2015-04-01 12:17:18 -07004277 TaskRecord getLockedTaskLocked() {
4278 final int top = mLockTaskModeTasks.size() - 1;
4279 if (top >= 0) {
4280 return mLockTaskModeTasks.get(top);
4281 }
4282 return null;
4283 }
4284
4285 boolean isLockedTask(TaskRecord task) {
4286 return mLockTaskModeTasks.contains(task);
4287 }
4288
4289 boolean isLastLockedTask(TaskRecord task) {
4290 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4291 }
4292
4293 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004294 if (!mLockTaskModeTasks.remove(task)) {
4295 return;
4296 }
4297 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4298 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004299 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004300 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4301 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004302 final Message lockTaskMsg = Message.obtain();
4303 lockTaskMsg.arg1 = task.userId;
4304 lockTaskMsg.what = LOCK_TASK_END_MSG;
4305 mHandler.sendMessage(lockTaskMsg);
4306 }
4307 }
4308
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004309 void showLockTaskToast() {
4310 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004311 }
4312
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004313 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4314 if (mLockTaskModeTasks.contains(task)) {
4315 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4316 }
4317 }
4318
Craig Mautner432f64e2015-05-20 14:59:57 -07004319 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4320 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004321 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004322 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004323 final TaskRecord lockedTask = getLockedTaskLocked();
4324 if (lockedTask != null) {
4325 removeLockedTaskLocked(lockedTask);
4326 if (!mLockTaskModeTasks.isEmpty()) {
4327 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004328 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4329 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004330 lockedTask.performClearTaskLocked();
4331 resumeTopActivitiesLocked();
4332 return;
4333 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004334 }
Craig Mautnere0570202015-05-13 13:06:11 -07004335 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4336 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004337 return;
4338 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004339
4340 // Should have already been checked, but do it again.
4341 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004342 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4343 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004344 return;
4345 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004346 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004347 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004348 return;
4349 }
4350
4351 if (mLockTaskModeTasks.isEmpty()) {
4352 // First locktask.
4353 final Message lockTaskMsg = Message.obtain();
4354 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4355 lockTaskMsg.arg1 = task.userId;
4356 lockTaskMsg.what = LOCK_TASK_START_MSG;
4357 lockTaskMsg.arg2 = lockTaskModeState;
4358 mHandler.sendMessage(lockTaskMsg);
4359 }
4360 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004361 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4362 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004363 mLockTaskModeTasks.remove(task);
4364 mLockTaskModeTasks.add(task);
4365
4366 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004367 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004368 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004369
4370 if (andResume) {
4371 findTaskToMoveToFrontLocked(task, 0, null, reason);
4372 resumeTopActivitiesLocked();
4373 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004374 }
4375
4376 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004377 return isLockTaskModeViolation(task, false);
4378 }
4379
4380 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4381 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004382 return false;
4383 }
4384 final int lockTaskAuth = task.mLockTaskAuth;
4385 switch (lockTaskAuth) {
4386 case LOCK_TASK_AUTH_DONT_LOCK:
4387 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004388 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004389 case LOCK_TASK_AUTH_LAUNCHABLE:
4390 case LOCK_TASK_AUTH_WHITELISTED:
4391 return false;
4392 case LOCK_TASK_AUTH_PINNABLE:
4393 // Pinnable tasks can't be launched on top of locktask tasks.
4394 return !mLockTaskModeTasks.isEmpty();
4395 default:
4396 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4397 return true;
4398 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004399 }
4400
Craig Mautner15df08a2015-04-01 12:17:18 -07004401 void onLockTaskPackagesUpdatedLocked() {
4402 boolean didSomething = false;
4403 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4404 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004405 final boolean wasWhitelisted =
4406 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4407 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004408 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004409 final boolean isWhitelisted =
4410 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4411 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4412 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004413 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004414 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4415 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004416 removeLockedTaskLocked(lockedTask);
4417 lockedTask.performClearTaskLocked();
4418 didSomething = true;
4419 }
4420 }
4421 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4422 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4423 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4424 final ActivityStack stack = stacks.get(stackNdx);
4425 stack.onLockTaskPackagesUpdatedLocked();
4426 }
4427 }
Craig Mautnere0570202015-05-13 13:06:11 -07004428 final ActivityRecord r = topRunningActivityLocked();
4429 final TaskRecord task = r != null ? r.task : null;
4430 if (mLockTaskModeTasks.isEmpty() && task != null
4431 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4432 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004433 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4434 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4435 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4436 false);
4437 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004438 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004439 if (didSomething) {
4440 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004441 }
4442 }
4443
Benjamin Franz43261142015-02-11 15:59:44 +00004444 int getLockTaskModeState() {
4445 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004446 }
4447
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004448 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004449
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004450 public ActivityStackSupervisorHandler(Looper looper) {
4451 super(looper);
4452 }
4453
Craig Mautnerf3333272013-04-22 10:55:53 -07004454 void activityIdleInternal(ActivityRecord r) {
4455 synchronized (mService) {
4456 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4457 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004458 }
4459
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004460 @Override
4461 public void handleMessage(Message msg) {
4462 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004463 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004464 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4465 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004466 if (mService.mDidDexOpt) {
4467 mService.mDidDexOpt = false;
4468 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4469 nmsg.obj = msg.obj;
4470 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4471 return;
4472 }
4473 // We don't at this point know if the activity is fullscreen,
4474 // so we need to be conservative and assume it isn't.
4475 activityIdleInternal((ActivityRecord)msg.obj);
4476 } break;
4477 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004478 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004479 activityIdleInternal((ActivityRecord)msg.obj);
4480 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004481 case RESUME_TOP_ACTIVITY_MSG: {
4482 synchronized (mService) {
4483 resumeTopActivitiesLocked();
4484 }
4485 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004486 case SLEEP_TIMEOUT_MSG: {
4487 synchronized (mService) {
4488 if (mService.isSleepingOrShuttingDown()) {
4489 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4490 mSleepTimeout = true;
4491 checkReadyForSleepLocked();
4492 }
4493 }
4494 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004495 case LAUNCH_TIMEOUT_MSG: {
4496 if (mService.mDidDexOpt) {
4497 mService.mDidDexOpt = false;
4498 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4499 return;
4500 }
4501 synchronized (mService) {
4502 if (mLaunchingActivity.isHeld()) {
4503 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4504 if (VALIDATE_WAKE_LOCK_CALLER
4505 && Binder.getCallingUid() != Process.myUid()) {
4506 throw new IllegalStateException("Calling must be system uid");
4507 }
4508 mLaunchingActivity.release();
4509 }
4510 }
4511 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004512 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004513 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004514 } break;
4515 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004516 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004517 } break;
4518 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004519 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004520 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004521 case CONTAINER_CALLBACK_VISIBILITY: {
4522 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004523 final IActivityContainerCallback callback = container.mCallback;
4524 if (callback != null) {
4525 try {
4526 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4527 } catch (RemoteException e) {
4528 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004529 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004530 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004531 case LOCK_TASK_START_MSG: {
4532 // When lock task starts, we disable the status bars.
4533 try {
Jason Monk62515be2014-05-21 16:06:19 -04004534 if (mLockTaskNotify == null) {
4535 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004536 }
Jason Monk62515be2014-05-21 16:06:19 -04004537 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004538 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004539 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004540 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004541 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004542 flags = StatusBarManager.DISABLE_MASK
4543 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004544 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004545 flags = StatusBarManager.DISABLE_MASK
4546 & (~StatusBarManager.DISABLE_BACK)
4547 & (~StatusBarManager.DISABLE_HOME)
4548 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004549 }
4550 getStatusBarService().disable(flags, mToken,
4551 mService.mContext.getPackageName());
4552 }
4553 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004554 if (getDevicePolicyManager() != null) {
4555 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004556 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004557 }
justinzhang5286d3f2014-05-12 17:06:01 -04004558 } catch (RemoteException ex) {
4559 throw new RuntimeException(ex);
4560 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004561 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004562 case LOCK_TASK_END_MSG: {
4563 // When lock task ends, we enable the status bars.
4564 try {
Jason Monk62515be2014-05-21 16:06:19 -04004565 if (getStatusBarService() != null) {
4566 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4567 mService.mContext.getPackageName());
4568 }
4569 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004570 if (getDevicePolicyManager() != null) {
4571 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4572 msg.arg1);
4573 }
Jason Monk62515be2014-05-21 16:06:19 -04004574 if (mLockTaskNotify == null) {
4575 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4576 }
4577 mLockTaskNotify.show(false);
4578 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004579 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004580 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004581 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004582 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004583 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004584 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004585 new LockPatternUtils(mService.mContext)
4586 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004587 }
4588 } catch (SettingNotFoundException e) {
4589 // No setting, don't lock.
4590 }
justinzhang5286d3f2014-05-12 17:06:01 -04004591 } catch (RemoteException ex) {
4592 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004593 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004594 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004595 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004596 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004597 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4598 if (mLockTaskNotify == null) {
4599 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4600 }
4601 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4602 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004603 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4604 final ActivityContainer container = (ActivityContainer) msg.obj;
4605 final IActivityContainerCallback callback = container.mCallback;
4606 if (callback != null) {
4607 try {
4608 callback.onAllActivitiesComplete(container.asBinder());
4609 } catch (RemoteException e) {
4610 }
4611 }
4612 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004613 case LAUNCH_TASK_BEHIND_COMPLETE: {
4614 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004615 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004616 if (r != null) {
4617 handleLaunchTaskBehindCompleteLocked(r);
4618 }
4619 }
4620 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004621 }
4622 }
4623 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004624
Ying Wangb081a592014-04-22 15:20:16 -07004625 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004626 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004627 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004628 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004629 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004630 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004631 ActivityRecord mParentActivity = null;
4632 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004633
Craig Mautnere3a00d72014-04-16 08:31:19 -07004634 boolean mVisible = true;
4635
Craig Mautner4a1cb222013-12-04 16:14:06 -08004636 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004637 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004638
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004639 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4640 final static int CONTAINER_STATE_NO_SURFACE = 1;
4641 final static int CONTAINER_STATE_FINISHING = 2;
4642 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4643
4644 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004645 synchronized (mService) {
4646 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004647 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004648 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004649 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004650 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004651 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004652
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004653 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004654 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004655 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004656 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004657 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004658 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004659 }
4660
4661 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004662 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004663 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004664 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4665 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004666 return;
4667 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004668 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004669 }
4670 }
4671
4672 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004673 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004674 synchronized (mService) {
4675 if (mActivityDisplay != null) {
4676 return mActivityDisplay.mDisplayId;
4677 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004678 }
4679 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004680 }
4681
Jeff Brownca9bc702014-02-11 14:32:56 -08004682 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004683 public int getStackId() {
4684 synchronized (mService) {
4685 return mStackId;
4686 }
4687 }
4688
4689 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004690 public boolean injectEvent(InputEvent event) {
4691 final long origId = Binder.clearCallingIdentity();
4692 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004693 synchronized (mService) {
4694 if (mActivityDisplay != null) {
4695 return mInputManagerInternal.injectInputEvent(event,
4696 mActivityDisplay.mDisplayId,
4697 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4698 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004699 }
4700 return false;
4701 } finally {
4702 Binder.restoreCallingIdentity(origId);
4703 }
4704 }
4705
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004706 @Override
4707 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004708 synchronized (mService) {
4709 if (mContainerState == CONTAINER_STATE_FINISHING) {
4710 return;
4711 }
4712 mContainerState = CONTAINER_STATE_FINISHING;
4713
Craig Mautnerd163e752014-06-13 17:18:47 -07004714 long origId = Binder.clearCallingIdentity();
4715 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004716 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004717 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004718 } finally {
4719 Binder.restoreCallingIdentity(origId);
4720 }
4721 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004722 }
4723
Craig Mautner60257702014-09-17 15:02:33 -07004724 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004725 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004726 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004727 if (mActivityDisplay != null) {
4728 mActivityDisplay.detachActivitiesLocked(mStack);
4729 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004730 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004731 }
4732 }
4733
4734 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004735 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004736 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004737 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004738 Binder.getCallingUid(), mCurrentUser, false,
4739 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004740
Craig Mautnere0a38842013-12-16 16:14:02 -08004741 // TODO: Switch to user app stacks here.
4742 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004743 final Uri data = intent.getData();
4744 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4745 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004746 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004747 checkEmbeddedAllowedInner(userId, intent, mimeType);
4748
4749 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4750 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004751 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004752 }
4753
4754 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004755 public final int startActivityIntentSender(IIntentSender intentSender)
4756 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004757 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4758
4759 if (!(intentSender instanceof PendingIntentRecord)) {
4760 throw new IllegalArgumentException("Bad PendingIntent object");
4761 }
4762
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004763 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004764 Binder.getCallingUid(), mCurrentUser, false,
4765 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004766
4767 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4768 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4769 pendingIntent.key.requestResolvedType);
4770
4771 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4772 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4773 }
4774
4775 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004776 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004777 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004778 throw new SecurityException(
4779 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4780 }
4781 }
4782
Craig Mautnerdf88d732014-01-27 09:21:32 -08004783 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004784 public IBinder asBinder() {
4785 return this;
4786 }
4787
Craig Mautner4504de52013-12-20 09:06:56 -08004788 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004789 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004790 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004791 }
4792
Craig Mautner4a1cb222013-12-04 16:14:06 -08004793 ActivityStackSupervisor getOuter() {
4794 return ActivityStackSupervisor.this;
4795 }
4796
Craig Mautnerd163e752014-06-13 17:18:47 -07004797 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004798 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004799 }
4800
Craig Mautner6985bad2014-04-21 15:22:06 -07004801 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004802 void setVisible(boolean visible) {
4803 if (mVisible != visible) {
4804 mVisible = visible;
4805 if (mCallback != null) {
4806 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4807 0 /* unused */, this).sendToTarget();
4808 }
4809 }
4810 }
4811
Craig Mautner6985bad2014-04-21 15:22:06 -07004812 void setDrawn() {
4813 }
4814
Craig Mautner1b4bf852014-05-26 15:06:32 -07004815 // You can always start a new task on a regular ActivityStack.
4816 boolean isEligibleForNewTasks() {
4817 return true;
4818 }
4819
Craig Mautnerd163e752014-06-13 17:18:47 -07004820 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004821 detachLocked();
4822 deleteActivityContainer(this);
4823 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004824 }
4825
Craig Mautner34b73df2014-01-12 21:11:08 -08004826 @Override
4827 public String toString() {
4828 return mIdString + (mActivityDisplay == null ? "N" : "A");
4829 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004830 }
4831
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004832 private class VirtualActivityContainer extends ActivityContainer {
4833 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004834 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004835
4836 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4837 super(getNextStackId());
4838 mParentActivity = parent;
4839 mCallback = callback;
4840 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004841 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004842 }
4843
4844 @Override
4845 public void setSurface(Surface surface, int width, int height, int density) {
4846 super.setSurface(surface, width, height, density);
4847
4848 synchronized (mService) {
4849 final long origId = Binder.clearCallingIdentity();
4850 try {
4851 setSurfaceLocked(surface, width, height, density);
4852 } finally {
4853 Binder.restoreCallingIdentity(origId);
4854 }
4855 }
4856 }
4857
4858 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4859 if (mContainerState == CONTAINER_STATE_FINISHING) {
4860 return;
4861 }
4862 VirtualActivityDisplay virtualActivityDisplay =
4863 (VirtualActivityDisplay) mActivityDisplay;
4864 if (virtualActivityDisplay == null) {
4865 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004866 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004867 mActivityDisplay = virtualActivityDisplay;
4868 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004869 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004870 }
4871
4872 if (mSurface != null) {
4873 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004874 }
4875
Craig Mautner6985bad2014-04-21 15:22:06 -07004876 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004877 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004878 mStack.resumeTopActivityLocked(null);
4879 } else {
4880 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004881 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004882 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004883 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004884 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004885 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004886
Craig Mautnerd163e752014-06-13 17:18:47 -07004887 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004888
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004889 if (DEBUG_STACK) Slog.d(TAG_STACK,
4890 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004891 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004892
Craig Mautner6985bad2014-04-21 15:22:06 -07004893 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004894 boolean isAttachedLocked() {
4895 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004896 }
4897
4898 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004899 void setDrawn() {
4900 synchronized (mService) {
4901 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004902 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004903 }
4904 }
4905
Craig Mautner1b4bf852014-05-26 15:06:32 -07004906 // Never start a new task on an ActivityView if it isn't explicitly specified.
4907 @Override
4908 boolean isEligibleForNewTasks() {
4909 return false;
4910 }
4911
Craig Mautnerd163e752014-06-13 17:18:47 -07004912 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004913 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004914 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4915 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4916 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4917 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4918 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004919 }
4920 }
4921
Craig Mautner4a1cb222013-12-04 16:14:06 -08004922 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4923 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004924 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004925 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004926 int mDisplayId;
4927 Display mDisplay;
4928 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004929
Craig Mautner4a1cb222013-12-04 16:14:06 -08004930 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4931 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004932 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004933
Jose Lima4b6c6692014-08-12 17:41:12 -07004934 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004935
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004936 ActivityDisplay() {
4937 }
Craig Mautner4504de52013-12-20 09:06:56 -08004938
Craig Mautner1a70a162014-09-13 12:09:31 -07004939 // After instantiation, check that mDisplay is not null before using this. The alternative
4940 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004941 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004942 final Display display = mDisplayManager.getDisplay(displayId);
4943 if (display == null) {
4944 return;
4945 }
4946 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004947 }
4948
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004949 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004950 mDisplay = display;
4951 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004952 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004953 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004954
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004955 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004956 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004957 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4958 + " onTop=" + onTop);
4959 if (onTop) {
4960 mStacks.add(stack);
4961 } else {
4962 mStacks.add(0, stack);
4963 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004964 }
4965
Craig Mautnere0a38842013-12-16 16:14:02 -08004966 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004967 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004968 + " from displayId=" + mDisplayId);
4969 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004970 }
4971
Jose Lima4b6c6692014-08-12 17:41:12 -07004972 void setVisibleBehindActivity(ActivityRecord r) {
4973 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004974 }
4975
Jose Lima4b6c6692014-08-12 17:41:12 -07004976 boolean hasVisibleBehindActivity() {
4977 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004978 }
4979
Craig Mautner34b73df2014-01-12 21:11:08 -08004980 @Override
4981 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004982 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4983 }
4984 }
4985
4986 class VirtualActivityDisplay extends ActivityDisplay {
4987 VirtualDisplay mVirtualDisplay;
4988
Craig Mautner6985bad2014-04-21 15:22:06 -07004989 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004990 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004991 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4992 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4993 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4994 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004995
4996 init(mVirtualDisplay.getDisplay());
4997
4998 mWindowManager.handleDisplayAdded(mDisplayId);
4999 }
5000
5001 void setSurface(Surface surface) {
5002 if (mVirtualDisplay != null) {
5003 mVirtualDisplay.setSurface(surface);
5004 }
5005 }
5006
5007 @Override
5008 void detachActivitiesLocked(ActivityStack stack) {
5009 super.detachActivitiesLocked(stack);
5010 if (mVirtualDisplay != null) {
5011 mVirtualDisplay.release();
5012 mVirtualDisplay = null;
5013 }
5014 }
5015
5016 @Override
5017 public String toString() {
5018 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005019 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005020 }
Jose Lima58e66d62014-05-27 20:00:27 -07005021
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07005022 /**
5023 * Adjust bounds to stay within stack bounds.
5024 *
5025 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
5026 * that keep them unchanged, but be contained within the stack bounds.
5027 *
5028 * @param bounds Bounds to be adjusted.
5029 * @param stackBounds Bounds within which the other bounds should remain.
5030 */
5031 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
5032 if (stackBounds == null || stackBounds.contains(bounds)) {
5033 return;
5034 }
5035
5036 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
5037 final int maxRight = stackBounds.right
5038 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
5039 int horizontalDiff = stackBounds.left - bounds.left;
5040 if ((horizontalDiff < 0 && bounds.left >= maxRight)
5041 || (bounds.left + horizontalDiff >= maxRight)) {
5042 horizontalDiff = maxRight - bounds.left;
5043 }
5044 bounds.left += horizontalDiff;
5045 bounds.right += horizontalDiff;
5046 }
5047
5048 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
5049 final int maxBottom = stackBounds.bottom
5050 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
5051 int verticalDiff = stackBounds.top - bounds.top;
5052 if ((verticalDiff < 0 && bounds.top >= maxBottom)
5053 || (bounds.top + verticalDiff >= maxBottom)) {
5054 verticalDiff = maxBottom - bounds.top;
5055 }
5056 bounds.top += verticalDiff;
5057 bounds.bottom += verticalDiff;
5058 }
5059 }
5060
Craig Mautner27084302013-03-25 08:05:25 -07005061}