blob: f6156139af6bba06db179cab0dc0e8ed27bc4e7d [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;
Filip Gruszczynskidc394902015-12-14 10:20:22 -080032import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautner29219d92013-04-16 20:19:12 -070033import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
34import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070035import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070036import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070037import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080038import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070039import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070040import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
41import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
42import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070043import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070044import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
45import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010046import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070047import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
48import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070049
Svetoslav7008b512015-06-24 18:47:07 -070050import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070051import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070052import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070053import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080054import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.app.ActivityOptions;
56import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070057import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080058import android.app.IActivityContainer;
59import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070060import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.app.IApplicationThread;
Clara Bayarrif7fea162015-10-22 16:09:52 +010062import android.app.KeyguardManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070064import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070065import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070066import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070067import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040068import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040069import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070071import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070073import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070074import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070075import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070077import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070078import android.content.pm.PackageManager;
79import android.content.pm.ResolveInfo;
Clara Bayarrif7fea162015-10-22 16:09:52 +010080import android.content.pm.UserInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080082import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080083import android.hardware.display.DisplayManager;
84import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080085import android.hardware.display.DisplayManagerGlobal;
86import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080087import android.hardware.input.InputManager;
88import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080089import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070090import android.os.Binder;
Svet Ganov9c165d72015-12-01 19:52:26 -080091import android.os.Build;
Craig Mautner8d341ef2013-03-26 09:03:27 -070092import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070093import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070094import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070095import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070096import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070097import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070098import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070099import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700100import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700101import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -0400102import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700104import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700105import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700106import android.os.UserHandle;
Clara Bayarrif7fea162015-10-22 16:09:52 +0100107import android.os.UserManager;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700108import android.os.WorkSource;
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;
Chong Zhangb15758a2015-11-17 12:12:03 -0800124
Craig Mautner23ac33b2013-04-01 16:26:35 -0700125import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700126import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800127import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700128import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400129import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700130import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400131import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800132import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700133import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700134import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700135
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
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800373 private final ActivityMetricsLogger mActivityMetricsLogger;
374
Craig Mautneree36c772014-07-16 14:56:05 -0700375 /**
376 * Description of a request to start a new activity, which has been held
377 * due to app switches being disabled.
378 */
379 static class PendingActivityLaunch {
380 final ActivityRecord r;
381 final ActivityRecord sourceRecord;
382 final int startFlags;
383 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700384 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700385
386 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700387 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700388 r = _r;
389 sourceRecord = _sourceRecord;
390 startFlags = _startFlags;
391 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700392 callerApp = _callerApp;
393 }
394
395 void sendErrorResult(String message) {
396 try {
397 if (callerApp.thread != null) {
398 callerApp.thread.scheduleCrash(message);
399 }
400 } catch (RemoteException e) {
401 Slog.e(TAG, "Exception scheduling crash of failed "
402 + "activity launcher sourceRecord=" + sourceRecord, e);
403 }
Craig Mautneree36c772014-07-16 14:56:05 -0700404 }
405 }
406
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800407 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700408 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800409 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700410 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800411 mActivityMetricsLogger = new ActivityMetricsLogger(this, mService.mContext);
Jeff Brown2c43c332014-06-12 22:38:59 -0700412 }
413
414 /**
415 * At the time when the constructor runs, the power manager has not yet been
416 * initialized. So we initialize our wakelocks afterwards.
417 */
418 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800419 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700420 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700421 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700422 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700423 }
424
justinzhang5286d3f2014-05-12 17:06:01 -0400425 // This function returns a IStatusBarService. The value is from ServiceManager.
426 // getService and is cached.
427 private IStatusBarService getStatusBarService() {
428 synchronized (mService) {
429 if (mStatusBarService == null) {
430 mStatusBarService = IStatusBarService.Stub.asInterface(
431 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
432 if (mStatusBarService == null) {
433 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
434 }
435 }
436 return mStatusBarService;
437 }
438 }
439
Jason Monk35c62a42014-06-17 10:24:47 -0400440 private IDevicePolicyManager getDevicePolicyManager() {
441 synchronized (mService) {
442 if (mDevicePolicyManager == null) {
443 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
444 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
445 if (mDevicePolicyManager == null) {
446 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
447 }
448 }
449 return mDevicePolicyManager;
450 }
451 }
452
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700453 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800454 synchronized (mService) {
455 mWindowManager = wm;
456
457 mDisplayManager =
458 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
459 mDisplayManager.registerDisplayListener(this, null);
460
461 Display[] displays = mDisplayManager.getDisplays();
462 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
463 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800464 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700465 if (activityDisplay.mDisplay == null) {
466 throw new IllegalStateException("Default Display does not exist");
467 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800468 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700469 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800470 }
471
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700472 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800473 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800474
475 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800476 }
Craig Mautner27084302013-03-25 08:05:25 -0700477 }
478
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200479 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700480 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200481 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700482 }
483
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700484 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800485 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700486 }
487
Craig Mautnerde4ef022013-04-07 19:01:33 -0700488 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800489 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700490 }
491
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700492 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700493 if (stack == null) {
494 return false;
495 }
496
Craig Mautnerdf88d732014-01-27 09:21:32 -0800497 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
498 if (parent != null) {
499 stack = parent.task.stack;
500 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800501 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700502 }
503
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700504 /** The top most stack. */
505 boolean isFrontStack(ActivityStack stack) {
506 if (stack == null) {
507 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700509
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700510 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
511 if (parent != null) {
512 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800513 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700514 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
515 }
516
517 void setFocusStack(String reason, ActivityStack focusedStack) {
518 mLastFocusedStack = mFocusedStack;
519 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800520
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700521 EventLogTags.writeAmFocusedStack(
522 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
523 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800524
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800525 if (mService.mBooting || !mService.mBooted) {
526 final ActivityRecord r = topRunningActivityLocked();
527 if (r != null && r.idle) {
528 checkFinishBootingLocked();
529 }
530 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700531 }
532
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700533 void moveHomeStackToFront(String reason) {
534 mHomeStack.moveToFront(reason);
535 }
536
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700537 /** Returns true if the focus activity was adjusted to the home stack top activity. */
538 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700539 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
540 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700541 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700542 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700543
Craig Mautner84984fa2014-06-19 11:19:20 -0700544 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700545
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700546 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700547 if (top == null) {
548 return false;
549 }
550 mService.setFocusedActivityLocked(top, reason);
551 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700552 }
553
Craig Mautner299f9602015-01-26 09:47:33 -0800554 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700555 if (!mService.mBooting && !mService.mBooted) {
556 // Not ready yet!
557 return false;
558 }
559
Craig Mautner84984fa2014-06-19 11:19:20 -0700560 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
561 mWindowManager.showRecentApps();
562 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700563 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700564
Craig Mautner84984fa2014-06-19 11:19:20 -0700565 if (prev != null) {
566 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
567 }
568
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700569 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
570 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800571 // Only resume home activity if isn't finishing.
572 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800573 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700574 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700575 }
Craig Mautner299f9602015-01-26 09:47:33 -0800576 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700577 }
578
Craig Mautner8d341ef2013-03-26 09:03:27 -0700579 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700580 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700581 }
582
583 /**
584 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
585 * @param id Id of the task we would like returned.
586 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
587 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700588 * @param stackId The stack to restore the task to (default launch stack will be used if
589 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700590 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700591 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800592 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800593 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800594 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
596 ActivityStack stack = stacks.get(stackNdx);
597 TaskRecord task = stack.taskForIdLocked(id);
598 if (task != null) {
599 return task;
600 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700601 }
602 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800603
604 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700605 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800606 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800607 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700608 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800609 return null;
610 }
611
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700612 if (!restoreFromRecents) {
613 return task;
614 }
615
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700616 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700617 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
618 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800619 return null;
620 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700621 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800622 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700623 }
624
Craig Mautner6170f732013-04-02 13:05:23 -0700625 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800626 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800627 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800628 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800629 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
630 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
631 if (r != null) {
632 return r;
633 }
Craig Mautner6170f732013-04-02 13:05:23 -0700634 }
635 }
636 return null;
637 }
638
Craig Mautneref73ee12014-04-23 11:45:37 -0700639 void setNextTaskId(int taskId) {
640 if (taskId > mCurTaskId) {
641 mCurTaskId = taskId;
642 }
643 }
644
Craig Mautner8d341ef2013-03-26 09:03:27 -0700645 int getNextTaskId() {
646 do {
647 mCurTaskId++;
648 if (mCurTaskId <= 0) {
649 mCurTaskId = 1;
650 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700651 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700652 return mCurTaskId;
653 }
654
Craig Mautnerde4ef022013-04-07 19:01:33 -0700655 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800656 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700657 if (stack == null) {
658 return null;
659 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700660 ActivityRecord resumedActivity = stack.mResumedActivity;
661 if (resumedActivity == null || resumedActivity.app == null) {
662 resumedActivity = stack.mPausingActivity;
663 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700664 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700665 }
666 }
667 return resumedActivity;
668 }
669
Dianne Hackbornff072722014-09-24 10:56:28 -0700670 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700671 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
674 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
676 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700677 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 continue;
679 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700680 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800681 if (hr != null) {
682 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
683 && processName.equals(hr.processName)) {
684 try {
George Mount2c92c972014-03-20 09:38:23 -0700685 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 didSomething = true;
687 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700688 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800689 Slog.w(TAG, "Exception in new application when starting activity "
690 + hr.intent.getComponent().flattenToShortString(), e);
691 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700692 }
Craig Mautner20e72272013-04-01 13:45:53 -0700693 }
Craig Mautner20e72272013-04-01 13:45:53 -0700694 }
695 }
696 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700697 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700698 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700699 }
Craig Mautner20e72272013-04-01 13:45:53 -0700700 return didSomething;
701 }
702
703 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800704 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
705 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
707 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700708 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800709 continue;
710 }
711 final ActivityRecord resumedActivity = stack.mResumedActivity;
712 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700713 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800714 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800715 return false;
716 }
Craig Mautner20e72272013-04-01 13:45:53 -0700717 }
718 }
719 return true;
720 }
721
Craig Mautnerde4ef022013-04-07 19:01:33 -0700722 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800723 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
724 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800725 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
726 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700727 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700729 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800730 return false;
731 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700732 }
733 }
734 }
735 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700736 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800737 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
738 mLastFocusedStack + " to=" + mFocusedStack);
739 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700740 return true;
741 }
742
743 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800744 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800745 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
746 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800747 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
748 final ActivityStack stack = stacks.get(stackNdx);
749 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800750 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700751 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800752 return false;
753 }
754 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700756 }
757 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800758 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700759 }
760
Craig Mautner2acc3892013-09-23 10:28:14 -0700761 /**
762 * Pause all activities in either all of the stacks or just the back stacks.
763 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700764 * @return true if any activity was paused as a result of this call.
765 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700766 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700767 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800768 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
769 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
771 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700772 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700773 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800774 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700775 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
776 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800777 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700778 }
779 }
780 return someActivityPaused;
781 }
782
Craig Mautnerde4ef022013-04-07 19:01:33 -0700783 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700784 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800785 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
786 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800787 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
788 final ActivityStack stack = stacks.get(stackNdx);
789 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700790 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800791 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700792 Slog.d(TAG_STATES,
793 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800794 pausing = false;
795 } else {
796 return false;
797 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700798 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700799 }
800 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700801 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700802 }
803
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700804 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
805 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500806 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
808 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
810 final ActivityStack stack = stacks.get(stackNdx);
811 if (stack.mResumedActivity != null &&
812 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700813 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800814 }
815 }
816 }
817 }
818
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700819 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700820 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700821 }
822
823 void sendWaitingVisibleReportLocked(ActivityRecord r) {
824 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700825 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700826 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700827 if (w.who == null) {
828 changed = true;
829 w.timeout = false;
830 if (r != null) {
831 w.who = new ComponentName(r.info.packageName, r.info.name);
832 }
833 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
834 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700835 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700836 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700837 if (changed) {
838 mService.notifyAll();
839 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700840 }
841
842 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
843 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700844 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700845 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700846 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700847 if (w.who == null) {
848 changed = true;
849 w.timeout = timeout;
850 if (r != null) {
851 w.who = new ComponentName(r.info.packageName, r.info.name);
852 }
853 w.thisTime = thisTime;
854 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700855 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700856 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700857 if (changed) {
858 mService.notifyAll();
859 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700860 }
861
Craig Mautner29219d92013-04-16 20:19:12 -0700862 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800863 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700864 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700865 if (r != null) {
866 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700867 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700868
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800870 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800871 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
872 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700873 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700874 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700875 if (r != null) {
876 return r;
877 }
878 }
879 }
880 return null;
881 }
882
Dianne Hackborn09233282014-04-30 11:33:59 -0700883 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700884 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
886 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800887 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800888 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800889 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800890 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
891 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700892 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800893 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700894 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700895 }
896 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700897
898 // The lists are already sorted from most recent to oldest. Just pull the most recent off
899 // each list and add it to list. Stop when all lists are empty or maxNum reached.
900 while (maxNum > 0) {
901 long mostRecentActiveTime = Long.MIN_VALUE;
902 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800903 final int numTaskLists = runningTaskLists.size();
904 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
905 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700906 if (!stackTaskList.isEmpty()) {
907 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
908 if (lastActiveTime > mostRecentActiveTime) {
909 mostRecentActiveTime = lastActiveTime;
910 selectedStackList = stackTaskList;
911 }
912 }
913 }
914 if (selectedStackList != null) {
915 list.add(selectedStackList.remove(0));
916 --maxNum;
917 } else {
918 break;
919 }
920 }
Craig Mautner20e72272013-04-01 13:45:53 -0700921 }
922
Todd Kennedy7440f172015-12-09 14:31:22 -0800923 ActivityInfo resolveActivity(Intent intent, ResolveInfo rInfo, int startFlags,
924 ProfilerInfo profilerInfo) {
925 final ActivityInfo aInfo = rInfo != null ? rInfo.activityInfo : null;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 if (aInfo != null) {
927 // Store the found target back into the intent, because now that
928 // we have it we never want to do this again. For example, if the
929 // user navigates back to this point in the history, we should
930 // always restart the exact same activity.
931 intent.setComponent(new ComponentName(
932 aInfo.applicationInfo.packageName, aInfo.name));
933
934 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700935 if (!aInfo.processName.equals("system")) {
936 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700937 mService.setDebugApp(aInfo.processName, true, false);
938 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700939
Man Caocfa78b22015-06-11 20:14:34 -0700940 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
941 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
942 }
943
944 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700945 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700946 }
947 }
948 }
949 return aInfo;
950 }
951
Todd Kennedy7440f172015-12-09 14:31:22 -0800952 ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId) {
953 try {
954 return AppGlobals.getPackageManager().resolveIntent(intent, resolvedType,
955 PackageManager.MATCH_DEFAULT_ONLY
956 | ActivityManagerService.STOCK_PM_FLAGS, userId);
957 } catch (RemoteException e) {
958 }
959 return null;
960 }
961
962 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
963 ProfilerInfo profilerInfo, int userId) {
964 final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
965 return resolveActivity(intent, rInfo, startFlags, profilerInfo);
966 }
967
Craig Mautner299f9602015-01-26 09:47:33 -0800968 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
969 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Todd Kennedy7440f172015-12-09 14:31:22 -0800970 startActivityLocked(null /*caller*/, intent, null /*ephemeralIntent*/,
971 null /*resolvedType*/, aInfo, null /*rInfo*/, null /*voiceSession*/,
972 null /*voiceInteractor*/, null /*resultTo*/, null /*resultWho*/,
973 0 /*requestCode*/, 0 /*callingPid*/, 0 /*callingUid*/, null /*callingPackage*/,
974 0 /*realCallingPid*/, 0 /*realCallingUid*/, 0 /*startFlags*/, null /*options*/,
975 false /*ignoreTargetSecurity*/, false /*componentSpecified*/, null /*outActivity*/,
976 null /*container*/, null /*inTask*/);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700977 if (inResumeTopActivity) {
978 // If we are in resume section already, home activity will be initialized, but not
979 // resumed (to avoid recursive resume) and will stay that way until something pokes it
980 // again. We need to schedule another resume.
981 scheduleResumeTopActivities();
982 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700983 }
984
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700986 String callingPackage, Intent intent, String resolvedType,
987 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700988 IBinder resultTo, String resultWho, int requestCode, int startFlags,
989 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Chong Zhang280d3322015-11-03 17:27:26 -0800990 Bundle bOptions, boolean ignoreTargetSecurity, int userId,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700991 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700992 // Refuse possible leaked file descriptors
993 if (intent != null && intent.hasFileDescriptors()) {
994 throw new IllegalArgumentException("File descriptors passed in Intent");
995 }
996 boolean componentSpecified = intent.getComponent() != null;
997
Todd Kennedy7440f172015-12-09 14:31:22 -0800998 // Save a copy in case ephemeral needs it
999 final Intent ephemeralIntent = new Intent(intent);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001000 // Don't modify the client's object!
1001 intent = new Intent(intent);
1002
Todd Kennedy7440f172015-12-09 14:31:22 -08001003 ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001004 // Collect information about the target of the Intent.
Todd Kennedy7440f172015-12-09 14:31:22 -08001005 ActivityInfo aInfo = resolveActivity(intent, rInfo, startFlags, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001006
Chong Zhang280d3322015-11-03 17:27:26 -08001007 ActivityOptions options = ActivityOptions.fromBundle(bOptions);
Craig Mautnere0a38842013-12-16 16:14:02 -08001008 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001009 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001010 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001011 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -08001012 // Cannot start a child activity if the parent is not resumed.
1013 return ActivityManager.START_CANCELED;
1014 }
Dianne Hackborn95465202014-09-15 16:21:55 -07001015 final int realCallingPid = Binder.getCallingPid();
1016 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 int callingPid;
1018 if (callingUid >= 0) {
1019 callingPid = -1;
1020 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001021 callingPid = realCallingPid;
1022 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001023 } else {
1024 callingPid = callingUid = -1;
1025 }
1026
Craig Mautnere0a38842013-12-16 16:14:02 -08001027 final ActivityStack stack;
1028 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001029 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001030 } else {
1031 stack = container.mStack;
1032 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001033 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001034 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001035 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001036
1037 final long origId = Binder.clearCallingIdentity();
1038
1039 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001040 (aInfo.applicationInfo.privateFlags
1041 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001042 // This may be a heavy-weight process! Check to see if we already
1043 // have another, different heavy-weight process running.
1044 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1045 if (mService.mHeavyWeightProcess != null &&
1046 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1047 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001048 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001049 if (caller != null) {
1050 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1051 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001052 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001053 } else {
1054 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001055 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001056 + intent.toString());
1057 ActivityOptions.abort(options);
1058 return ActivityManager.START_PERMISSION_DENIED;
1059 }
1060 }
1061
1062 IIntentSender target = mService.getIntentSenderLocked(
1063 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001064 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001065 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1066 | PendingIntent.FLAG_ONE_SHOT, null);
1067
1068 Intent newIntent = new Intent();
1069 if (requestCode >= 0) {
1070 // Caller is requesting a result.
1071 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1072 }
1073 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1074 new IntentSender(target));
1075 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1076 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1077 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1078 hist.packageName);
1079 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1080 hist.task.taskId);
1081 }
1082 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1083 aInfo.packageName);
1084 newIntent.setFlags(intent.getFlags());
1085 newIntent.setClassName("android",
1086 HeavyWeightSwitcherActivity.class.getName());
1087 intent = newIntent;
1088 resolvedType = null;
1089 caller = null;
1090 callingUid = Binder.getCallingUid();
1091 callingPid = Binder.getCallingPid();
1092 componentSpecified = true;
Todd Kennedy7440f172015-12-09 14:31:22 -08001093 rInfo = resolveIntent(intent, null /*resolvedType*/, userId);
1094 aInfo = rInfo != null ? rInfo.activityInfo : null;
1095 if (aInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001096 aInfo = mService.getActivityInfoForUser(aInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001097 }
1098 }
1099 }
1100 }
1101
Todd Kennedy7440f172015-12-09 14:31:22 -08001102 int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType,
1103 aInfo, rInfo, voiceSession, voiceInteractor,
1104 resultTo, resultWho, requestCode, callingPid,
1105 callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
1106 options, ignoreTargetSecurity, componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001107
Craig Mautner85c11a82014-07-17 12:38:18 -07001108 Binder.restoreCallingIdentity(origId);
1109
Craig Mautnerde4ef022013-04-07 19:01:33 -07001110 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001111 // If the caller also wants to switch to a new configuration,
1112 // do so now. This allows a clean switch, as we are waiting
1113 // for the current activity to pause (so we will not destroy
1114 // it), and have not yet started the next activity.
1115 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1116 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001117 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001118 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001119 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001120 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001121 }
1122
Craig Mautner23ac33b2013-04-01 16:26:35 -07001123 if (outResult != null) {
1124 outResult.result = res;
1125 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001126 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001127 do {
1128 try {
1129 mService.wait();
1130 } catch (InterruptedException e) {
1131 }
1132 } while (!outResult.timeout && outResult.who == null);
1133 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001134 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001135 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001136 outResult.timeout = false;
1137 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1138 outResult.totalTime = 0;
1139 outResult.thisTime = 0;
1140 } else {
1141 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001142 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001143 do {
1144 try {
1145 mService.wait();
1146 } catch (InterruptedException e) {
1147 }
1148 } while (!outResult.timeout && outResult.who == null);
1149 }
1150 }
1151 }
1152
1153 return res;
1154 }
1155 }
1156
1157 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1158 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
Chong Zhang280d3322015-11-03 17:27:26 -08001159 Bundle bOptions, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001160 if (intents == null) {
1161 throw new NullPointerException("intents is null");
1162 }
1163 if (resolvedTypes == null) {
1164 throw new NullPointerException("resolvedTypes is null");
1165 }
1166 if (intents.length != resolvedTypes.length) {
1167 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1168 }
1169
Craig Mautner23ac33b2013-04-01 16:26:35 -07001170
1171 int callingPid;
1172 if (callingUid >= 0) {
1173 callingPid = -1;
1174 } else if (caller == null) {
1175 callingPid = Binder.getCallingPid();
1176 callingUid = Binder.getCallingUid();
1177 } else {
1178 callingPid = callingUid = -1;
1179 }
1180 final long origId = Binder.clearCallingIdentity();
1181 try {
1182 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001183 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001184 for (int i=0; i<intents.length; i++) {
1185 Intent intent = intents[i];
1186 if (intent == null) {
1187 continue;
1188 }
1189
1190 // Refuse possible leaked file descriptors
1191 if (intent != null && intent.hasFileDescriptors()) {
1192 throw new IllegalArgumentException("File descriptors passed in Intent");
1193 }
1194
1195 boolean componentSpecified = intent.getComponent() != null;
1196
1197 // Don't modify the client's object!
1198 intent = new Intent(intent);
1199
1200 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001201 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001202 // TODO: New, check if this is correct
1203 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1204
1205 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001206 (aInfo.applicationInfo.privateFlags
1207 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001208 throw new IllegalArgumentException(
1209 "FLAG_CANT_SAVE_STATE not supported here");
1210 }
1211
Chong Zhang280d3322015-11-03 17:27:26 -08001212 ActivityOptions options = ActivityOptions.fromBundle(
1213 i == intents.length - 1 ? bOptions : null);
Todd Kennedy7440f172015-12-09 14:31:22 -08001214 int res = startActivityLocked(caller, intent, null /*ephemeralIntent*/,
1215 resolvedTypes[i], aInfo, null /*rInfo*/, null, null, resultTo, null, -1,
1216 callingPid, callingUid, callingPackage, callingPid, callingUid, 0,
1217 options, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001218 if (res < 0) {
1219 return res;
1220 }
1221
1222 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1223 }
1224 }
1225 } finally {
1226 Binder.restoreCallingIdentity(origId);
1227 }
1228
1229 return ActivityManager.START_SUCCESS;
1230 }
1231
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001233 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001234 throws RemoteException {
1235
Craig Mautner2568c3a2015-03-26 14:22:34 -07001236 if (andResume) {
1237 r.startFreezingScreenLocked(app, 0);
1238 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239
Craig Mautner2568c3a2015-03-26 14:22:34 -07001240 // schedule launch ticks to collect information about slow apps.
1241 r.startLaunchTickingLocked();
1242 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001243
1244 // Have the window manager re-evaluate the orientation of
1245 // the screen based on the new activity order. Note that
1246 // as a result of this, it can call back into the activity
1247 // manager with a new orientation. We don't care about that,
1248 // because the activity is not currently running so we are
1249 // just restarting it anyway.
1250 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001251 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001252 mService.mConfiguration,
1253 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001254 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 }
1256
1257 r.app = app;
1258 app.waitingToKill = null;
1259 r.launchCount++;
1260 r.lastLaunchTime = SystemClock.uptimeMillis();
1261
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001262 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001263
1264 int idx = app.activities.indexOf(r);
1265 if (idx < 0) {
1266 app.activities.add(r);
1267 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001268 mService.updateLruProcessLocked(app, true, null);
1269 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001270
Craig Mautner15df08a2015-04-01 12:17:18 -07001271 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001272 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1273 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001274 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001275 }
1276
1277 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 try {
1279 if (app.thread == null) {
1280 throw new RemoteException();
1281 }
1282 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001283 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 if (andResume) {
1285 results = r.results;
1286 newIntents = r.newIntents;
1287 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001288 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1289 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1290 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001291 if (andResume) {
1292 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1293 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001294 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001296 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001297 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001298 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001299 }
Nicolas Geoffray27c07372015-11-05 16:54:09 +00001300 mService.notifyPackageUse(r.intent.getComponent().getPackageName());
Craig Mautner2420ead2013-04-01 17:13:20 -07001301 r.sleeping = false;
1302 r.forceNewConfig = false;
1303 mService.showAskCompatModeDialogLocked(r);
1304 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001305 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001306 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1307 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1308 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001309 final String profileFile = mService.mProfileFile;
1310 if (profileFile != null) {
1311 ParcelFileDescriptor profileFd = mService.mProfileFd;
1312 if (profileFd != null) {
1313 try {
1314 profileFd = profileFd.dup();
1315 } catch (IOException e) {
1316 if (profileFd != null) {
1317 try {
1318 profileFd.close();
1319 } catch (IOException o) {
1320 }
1321 profileFd = null;
1322 }
1323 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001325
1326 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1327 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001328 }
1329 }
1330 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001331
Craig Mautner2568c3a2015-03-26 14:22:34 -07001332 if (andResume) {
1333 app.hasShownUi = true;
1334 app.pendingUiClean = true;
1335 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001336 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001337 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001338 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001339 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001340 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001341 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001342
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001343 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001344 // This may be a heavy-weight process! Note that the package
1345 // manager will ensure that only activity can run in the main
1346 // process of the .apk, which is the only thing that will be
1347 // considered heavy-weight.
1348 if (app.processName.equals(app.info.packageName)) {
1349 if (mService.mHeavyWeightProcess != null
1350 && mService.mHeavyWeightProcess != app) {
1351 Slog.w(TAG, "Starting new heavy weight process " + app
1352 + " when already running "
1353 + mService.mHeavyWeightProcess);
1354 }
1355 mService.mHeavyWeightProcess = app;
1356 Message msg = mService.mHandler.obtainMessage(
1357 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1358 msg.obj = r;
1359 mService.mHandler.sendMessage(msg);
1360 }
1361 }
1362
1363 } catch (RemoteException e) {
1364 if (r.launchFailed) {
1365 // This is the second time we failed -- finish activity
1366 // and give up.
1367 Slog.e(TAG, "Second failure launching "
1368 + r.intent.getComponent().flattenToShortString()
1369 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001370 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001371 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1372 "2nd-crash", false);
1373 return false;
1374 }
1375
1376 // This is the first time we failed -- restart process and
1377 // retry.
1378 app.activities.remove(r);
1379 throw e;
1380 }
1381
1382 r.launchFailed = false;
1383 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001384 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001385 }
1386
1387 if (andResume) {
1388 // As part of the process of launching, ActivityThread also performs
1389 // a resume.
1390 stack.minimalResumeActivityLocked(r);
1391 } else {
1392 // This activity is not starting in the resumed state... which
1393 // should look like we asked it to pause+stop (but remain visible),
1394 // and it has done so and reported back the current icicle and
1395 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001396 if (DEBUG_STATES) Slog.v(TAG_STATES,
1397 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001398 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001399 r.stopped = true;
1400 }
1401
1402 // Launch the new version setup screen if needed. We do this -after-
1403 // launching the initial activity (that is, home), so that it can have
1404 // a chance to initialize itself while in the background, making the
1405 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001406 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001407 mService.startSetupActivityLocked();
1408 }
1409
Dianne Hackborn465fa392014-09-14 14:21:18 -07001410 // Update any services we are bound to that might care about whether
1411 // their client may have activities.
1412 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1413
Craig Mautner2420ead2013-04-01 17:13:20 -07001414 return true;
1415 }
1416
Craig Mautnere79d42682013-04-01 19:01:53 -07001417 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001418 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001419 // Is this activity's application already running?
1420 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001421 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001422
1423 r.task.stack.setLaunchTime(r);
1424
1425 if (app != null && app.thread != null) {
1426 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001427 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1428 || !"android".equals(r.info.packageName)) {
1429 // Don't add this if it is a platform component that is marked
1430 // to run in multiple processes, because this is actually
1431 // part of the framework so doesn't make sense to track as a
1432 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001433 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1434 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001435 }
George Mount2c92c972014-03-20 09:38:23 -07001436 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001437 return;
1438 } catch (RemoteException e) {
1439 Slog.w(TAG, "Exception when starting activity "
1440 + r.intent.getComponent().flattenToShortString(), e);
1441 }
1442
1443 // If a dead object exception was thrown -- fall through to
1444 // restart the application.
1445 }
1446
1447 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001448 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001449 }
1450
Todd Kennedy7440f172015-12-09 14:31:22 -08001451 final int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
1452 String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001453 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Todd Kennedy7440f172015-12-09 14:31:22 -08001454 IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
1455 String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
1456 ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
1457 ActivityRecord[] outActivity, ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001458 int err = ActivityManager.START_SUCCESS;
1459
1460 ProcessRecord callerApp = null;
1461 if (caller != null) {
1462 callerApp = mService.getRecordForAppLocked(caller);
1463 if (callerApp != null) {
1464 callingPid = callerApp.pid;
1465 callingUid = callerApp.info.uid;
1466 } else {
1467 Slog.w(TAG, "Unable to find app for caller " + caller
1468 + " (pid=" + callingPid + ") when starting: "
1469 + intent.toString());
1470 err = ActivityManager.START_PERMISSION_DENIED;
1471 }
1472 }
1473
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001474 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1475
Craig Mautner6170f732013-04-02 13:05:23 -07001476 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001477 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001478 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001479 + " on display " + (container == null ? (mFocusedStack == null ?
1480 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1481 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1482 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001483 }
1484
1485 ActivityRecord sourceRecord = null;
1486 ActivityRecord resultRecord = null;
1487 if (resultTo != null) {
1488 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001489 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1490 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001491 if (sourceRecord != null) {
1492 if (requestCode >= 0 && !sourceRecord.finishing) {
1493 resultRecord = sourceRecord;
1494 }
1495 }
1496 }
Craig Mautner6170f732013-04-02 13:05:23 -07001497
Dianne Hackborn91097de2014-04-04 18:02:06 -07001498 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001499
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001500 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001501 // Transfer the result target from the source activity to the new
1502 // one being started, including any failures.
1503 if (requestCode >= 0) {
1504 ActivityOptions.abort(options);
1505 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1506 }
1507 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001508 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1509 resultRecord = null;
1510 }
Craig Mautner6170f732013-04-02 13:05:23 -07001511 resultWho = sourceRecord.resultWho;
1512 requestCode = sourceRecord.requestCode;
1513 sourceRecord.resultTo = null;
1514 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001515 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001516 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001517 if (sourceRecord.launchedFromUid == callingUid) {
1518 // The new activity is being launched from the same uid as the previous
1519 // activity in the flow, and asking to forward its result back to the
1520 // previous. In this case the activity is serving as a trampoline between
1521 // the two, so we also want to update its launchedFromPackage to be the
1522 // same as the previous activity. Note that this is safe, since we know
1523 // these two packages come from the same uid; the caller could just as
1524 // well have supplied that same package name itself. This specifially
1525 // deals with the case of an intent picker/chooser being launched in the app
1526 // flow to redirect to an activity picked by the user, where we want the final
1527 // activity to consider it to have been launched by the previous app activity.
1528 callingPackage = sourceRecord.launchedFromPackage;
1529 }
Craig Mautner6170f732013-04-02 13:05:23 -07001530 }
1531
1532 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1533 // We couldn't find a class that can handle the given Intent.
1534 // That's the end of that!
1535 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1536 }
1537
1538 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1539 // We couldn't find the specific class specified in the Intent.
1540 // Also the end of the line.
1541 err = ActivityManager.START_CLASS_NOT_FOUND;
1542 }
1543
Dianne Hackborn91097de2014-04-04 18:02:06 -07001544 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1545 && sourceRecord.task.voiceSession != null) {
1546 // If this activity is being launched as part of a voice session, we need
1547 // to ensure that it is safe to do so. If the upcoming activity will also
1548 // be part of the voice session, we can only launch it if it has explicitly
1549 // said it supports the VOICE category, or it is a part of the calling app.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001550 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001551 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1552 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001553 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001554 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1555 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001556 Slog.w(TAG,
1557 "Activity being started in current voice task does not support voice: "
1558 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001559 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1560 }
1561 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001562 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001563 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1564 }
1565 }
1566 }
1567
1568 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1569 // If the caller is starting a new voice session, just make sure the target
1570 // is actually allowing it to run this way.
1571 try {
1572 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1573 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001574 Slog.w(TAG,
1575 "Activity being started in new voice task does not support: "
1576 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001577 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1578 }
1579 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001580 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001581 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1582 }
1583 }
1584
louis_changcd5d1982014-08-01 10:09:08 +08001585 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1586
Craig Mautner6170f732013-04-02 13:05:23 -07001587 if (err != ActivityManager.START_SUCCESS) {
1588 if (resultRecord != null) {
1589 resultStack.sendActivityResultLocked(-1,
1590 resultRecord, resultWho, requestCode,
1591 Activity.RESULT_CANCELED, null);
1592 }
Craig Mautner6170f732013-04-02 13:05:23 -07001593 ActivityOptions.abort(options);
1594 return err;
1595 }
1596
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001597 boolean abort = !checkStartAnyActivityPermission(intent, aInfo, resultWho, requestCode,
1598 callingPid, callingUid, callingPackage, ignoreTargetSecurity, callerApp,
1599 resultRecord, resultStack);
Svetoslav7008b512015-06-24 18:47:07 -07001600 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001601 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001602
Craig Mautner6170f732013-04-02 13:05:23 -07001603 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001604 try {
1605 // The Intent we give to the watcher has the extra data
1606 // stripped off, since it can contain private information.
1607 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001608 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001609 aInfo.applicationInfo.packageName);
1610 } catch (RemoteException e) {
1611 mService.mController = null;
1612 }
Ben Gruver5e207332013-04-03 17:41:37 -07001613 }
Craig Mautner6170f732013-04-02 13:05:23 -07001614
Clara Bayarrif7fea162015-10-22 16:09:52 +01001615 UserInfo user = getUserInfo(userId);
Clara Bayarri56878a92015-10-29 15:43:55 +00001616 KeyguardManager km = (KeyguardManager) mService.mContext
1617 .getSystemService(Context.KEYGUARD_SERVICE);
1618 if (user.isManagedProfile()
Clara Bayarri10ad84a2015-12-01 17:38:05 +00001619 && LockPatternUtils.isSeparateWorkChallengeEnabled()
Clara Bayarri56878a92015-10-29 15:43:55 +00001620 && km.isDeviceLocked(userId)) {
Clara Bayarrif7fea162015-10-22 16:09:52 +01001621 IIntentSender target = mService.getIntentSenderLocked(
1622 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1623 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1624 new String[]{ resolvedType },
1625 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1626 | PendingIntent.FLAG_IMMUTABLE, null);
Todd Kennedy7440f172015-12-09 14:31:22 -08001627 final int flags = intent.getFlags();
1628 final Intent newIntent = km.createConfirmDeviceCredentialIntent(null, null, user.id);
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001629 if (newIntent != null) {
1630 intent = newIntent;
Todd Kennedy7440f172015-12-09 14:31:22 -08001631 intent.setFlags(flags
1632 | Intent.FLAG_ACTIVITY_NEW_TASK
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001633 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1634 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1635 intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
Clara Bayarrif7fea162015-10-22 16:09:52 +01001636
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001637 resolvedType = null;
1638 callingUid = realCallingUid;
1639 callingPid = realCallingPid;
Clara Bayarrif7fea162015-10-22 16:09:52 +01001640
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001641 UserInfo parent = UserManager.get(mService.mContext).getProfileParent(userId);
Todd Kennedy7440f172015-12-09 14:31:22 -08001642 rInfo = resolveIntent(intent, resolvedType, parent.id);
1643 aInfo = resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
Clara Bayarrib3987bd2015-11-18 16:39:34 -08001644 }
Clara Bayarrif7fea162015-10-22 16:09:52 +01001645 }
1646
Ben Gruver5e207332013-04-03 17:41:37 -07001647 if (abort) {
1648 if (resultRecord != null) {
1649 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001650 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001651 }
Ben Gruver5e207332013-04-03 17:41:37 -07001652 // We pretend to the caller that it was really started, but
1653 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001654 ActivityOptions.abort(options);
1655 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001656 }
1657
Svet Ganov9c165d72015-12-01 19:52:26 -08001658 // If permissions need a review before any of the app components can run, we
1659 // launch the review activity and pass a pending intent to start the activity
1660 // we are to launching now after the review is completed.
1661 if (Build.PERMISSIONS_REVIEW_REQUIRED && aInfo != null) {
1662 if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
1663 aInfo.packageName, userId)) {
1664 IIntentSender target = mService.getIntentSenderLocked(
1665 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1666 callingUid, userId, null, null, 0, new Intent[]{intent},
1667 new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT
1668 | PendingIntent.FLAG_ONE_SHOT, null);
1669
Todd Kennedy7440f172015-12-09 14:31:22 -08001670 final int flags = intent.getFlags();
Svet Ganov9c165d72015-12-01 19:52:26 -08001671 Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
Todd Kennedy7440f172015-12-09 14:31:22 -08001672 newIntent.setFlags(flags
1673 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Svet Ganov9c165d72015-12-01 19:52:26 -08001674 newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
1675 newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
1676 if (resultRecord != null) {
1677 newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);
1678 }
Svet Ganov9c165d72015-12-01 19:52:26 -08001679 intent = newIntent;
1680
1681 resolvedType = null;
1682 callingUid = realCallingUid;
1683 callingPid = realCallingPid;
1684
Todd Kennedy7440f172015-12-09 14:31:22 -08001685 rInfo = resolveIntent(intent, resolvedType, userId);
1686 aInfo = resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
Svet Ganov9c165d72015-12-01 19:52:26 -08001687
1688 if (DEBUG_PERMISSIONS_REVIEW) {
1689 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true,
1690 true, false) + "} from uid " + callingUid + " on display "
1691 + (container == null ? (mFocusedStack == null ?
1692 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1693 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1694 container.mActivityDisplay.mDisplayId)));
1695 }
1696 }
1697 }
1698
Todd Kennedy7440f172015-12-09 14:31:22 -08001699 // If we have an ephemeral app, abort the process of launching the resolved intent.
1700 // Instead, launch the ephemeral installer. Once the installer is finished, it
1701 // starts either the intent we resolved here [on install error] or the ephemeral
1702 // app [on install success].
1703 if (rInfo != null && rInfo.ephemeralResolveInfo != null) {
1704 // Create a pending intent to start the intent resolved here.
1705 final IIntentSender failureTarget = mService.getIntentSenderLocked(
1706 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1707 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
1708 new String[]{ resolvedType },
1709 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1710 | PendingIntent.FLAG_IMMUTABLE, null);
1711
1712 // Create a pending intent to start the ephemeral application; force it to be
1713 // directed to the ephemeral package.
1714 ephemeralIntent.setPackage(rInfo.ephemeralResolveInfo.getPackageName());
1715 final IIntentSender ephemeralTarget = mService.getIntentSenderLocked(
1716 ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
1717 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ ephemeralIntent },
1718 new String[]{ resolvedType },
1719 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
1720 | PendingIntent.FLAG_IMMUTABLE, null);
1721
1722 int flags = intent.getFlags();
1723 intent = new Intent();
1724 intent.setFlags(flags
1725 | Intent.FLAG_ACTIVITY_NEW_TASK
1726 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1727 intent.putExtra(Intent.EXTRA_PACKAGE_NAME,
1728 rInfo.ephemeralResolveInfo.getPackageName());
1729 intent.putExtra(Intent.EXTRA_EPHEMERAL_FAILURE, new IntentSender(failureTarget));
1730 intent.putExtra(Intent.EXTRA_EPHEMERAL_SUCCESS, new IntentSender(ephemeralTarget));
1731
1732 resolvedType = null;
1733 callingUid = realCallingUid;
1734 callingPid = realCallingPid;
1735
1736 rInfo = rInfo.ephemeralInstaller;
1737 aInfo = resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
1738 }
1739
Craig Mautner6170f732013-04-02 13:05:23 -07001740 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001741 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001742 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001743 if (outActivity != null) {
1744 outActivity[0] = r;
1745 }
1746
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001747 if (r.appTimeTracker == null && sourceRecord != null) {
1748 // If the caller didn't specify an explicit time tracker, we want to continue
1749 // tracking under any it has.
1750 r.appTimeTracker = sourceRecord.appTimeTracker;
1751 }
1752
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001753 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001754 if (voiceSession == null && (stack.mResumedActivity == null
1755 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001756 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1757 realCallingPid, realCallingUid, "Activity start")) {
Chong Zhang280d3322015-11-03 17:27:26 -08001758 PendingActivityLaunch pal = new PendingActivityLaunch(r,
Robert Carr13997f52015-10-23 13:13:39 -07001759 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001760 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001761 ActivityOptions.abort(options);
1762 return ActivityManager.START_SWITCHES_CANCELED;
1763 }
1764 }
1765
1766 if (mService.mDidAppSwitch) {
1767 // This is the second allowed switch since we stopped switches,
1768 // so now just generally allow switches. Use case: user presses
1769 // home (switches disabled, switch to home, mDidAppSwitch now true);
1770 // user taps a home icon (coming from home so allowed, we hit here
1771 // and now allow anyone to switch again).
1772 mService.mAppSwitchesAllowedTime = 0;
1773 } else {
1774 mService.mDidAppSwitch = true;
1775 }
1776
Craig Mautneree36c772014-07-16 14:56:05 -07001777 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001778
Dianne Hackborn91097de2014-04-04 18:02:06 -07001779 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001780 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001781
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001782 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001783 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001784 // activity start, but we are not actually doing an activity
1785 // switch... just dismiss the keyguard now, because we
1786 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001787 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001788 }
1789 return err;
1790 }
1791
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001792 private boolean checkStartAnyActivityPermission(Intent intent, ActivityInfo aInfo,
1793 String resultWho, int requestCode, int callingPid, int callingUid,
1794 String callingPackage, boolean ignoreTargetSecurity, ProcessRecord callerApp,
1795 ActivityRecord resultRecord, ActivityStack resultStack) {
1796 final int startAnyPerm = mService.checkPermission(START_ANY_ACTIVITY, callingPid,
1797 callingUid);
1798 if (startAnyPerm == PERMISSION_GRANTED) {
1799 return true;
1800 }
1801 final int componentRestriction = getComponentRestrictionForCallingPackage(
1802 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
1803 final int actionRestriction = getActionRestrictionForCallingPackage(
1804 intent.getAction(), callingPackage, callingPid, callingUid);
1805 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1806 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1807 if (resultRecord != null) {
1808 resultStack.sendActivityResultLocked(-1,
1809 resultRecord, resultWho, requestCode,
1810 Activity.RESULT_CANCELED, null);
1811 }
1812 final String msg;
1813 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1814 msg = "Permission Denial: starting " + intent.toString()
1815 + " from " + callerApp + " (pid=" + callingPid
1816 + ", uid=" + callingUid + ")" + " with revoked permission "
1817 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1818 } else if (!aInfo.exported) {
1819 msg = "Permission Denial: starting " + intent.toString()
1820 + " from " + callerApp + " (pid=" + callingPid
1821 + ", uid=" + callingUid + ")"
1822 + " not exported from uid " + aInfo.applicationInfo.uid;
1823 } else {
1824 msg = "Permission Denial: starting " + intent.toString()
1825 + " from " + callerApp + " (pid=" + callingPid
1826 + ", uid=" + callingUid + ")"
1827 + " requires " + aInfo.permission;
1828 }
1829 Slog.w(TAG, msg);
1830 throw new SecurityException(msg);
1831 }
1832
1833 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1834 final String message = "Appop Denial: starting " + intent.toString()
1835 + " from " + callerApp + " (pid=" + callingPid
1836 + ", uid=" + callingUid + ")"
1837 + " requires " + AppOpsManager.permissionToOp(
1838 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1839 Slog.w(TAG, message);
1840 return false;
1841 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1842 final String message = "Appop Denial: starting " + intent.toString()
1843 + " from " + callerApp + " (pid=" + callingPid
1844 + ", uid=" + callingUid + ")"
1845 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1846 Slog.w(TAG, message);
1847 return false;
1848 }
1849 return true;
1850 }
1851
Clara Bayarrif7fea162015-10-22 16:09:52 +01001852 private UserInfo getUserInfo(int userId) {
1853 final long identity = Binder.clearCallingIdentity();
1854 try {
1855 return UserManager.get(mService.mContext).getUserInfo(userId);
1856 } finally {
1857 Binder.restoreCallingIdentity(identity);
1858 }
1859 }
1860
Svet Ganov99b60432015-06-27 13:15:22 -07001861 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001862 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001863 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1864 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001865 == PackageManager.PERMISSION_DENIED) {
1866 return ACTIVITY_RESTRICTION_PERMISSION;
1867 }
1868
Christopher Tateff7add02015-08-17 10:23:22 -07001869 if (activityInfo.permission == null) {
1870 return ACTIVITY_RESTRICTION_NONE;
1871 }
1872
Svet Ganov99b60432015-06-27 13:15:22 -07001873 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1874 if (opCode == AppOpsManager.OP_NONE) {
1875 return ACTIVITY_RESTRICTION_NONE;
1876 }
1877
1878 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1879 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001880 if (!ignoreTargetSecurity) {
1881 return ACTIVITY_RESTRICTION_APPOP;
1882 }
Svet Ganov99b60432015-06-27 13:15:22 -07001883 }
1884
1885 return ACTIVITY_RESTRICTION_NONE;
1886 }
1887
Svetoslav7008b512015-06-24 18:47:07 -07001888 private int getActionRestrictionForCallingPackage(String action,
1889 String callingPackage, int callingPid, int callingUid) {
1890 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001891 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001892 }
1893
1894 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1895 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001896 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001897 }
1898
1899 final PackageInfo packageInfo;
1900 try {
1901 packageInfo = mService.mContext.getPackageManager()
1902 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1903 } catch (PackageManager.NameNotFoundException e) {
1904 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001905 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001906 }
1907
1908 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001909 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001910 }
1911
1912 if (mService.checkPermission(permission, callingPid, callingUid) ==
1913 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001914 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001915 }
1916
1917 final int opCode = AppOpsManager.permissionToOpCode(permission);
1918 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001919 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001920 }
1921
1922 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1923 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001924 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001925 }
1926
Svet Ganov99b60432015-06-27 13:15:22 -07001927 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001928 }
1929
Filip Gruszczynskidc394902015-12-14 10:20:22 -08001930 private ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001931 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001932
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001933 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1934 return mHomeStack;
1935 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001936
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001937 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001938
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001939 if (task != null && task.stack != null) {
1940 stack = task.stack;
1941 if (stack.isOnHomeDisplay()) {
1942 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001943 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001944 "computeStackFocus: Setting " + "focused stack to r=" + r
1945 + " task=" + task);
1946 } else {
1947 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1948 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001949 }
1950 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001951 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001952 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001953
1954 final ActivityContainer container = r.mInitialActivityContainer;
1955 if (container != null) {
1956 // The first time put it on the desired stack, after this put on task stack.
1957 r.mInitialActivityContainer = null;
1958 return container.mStack;
1959 }
1960
1961 // The fullscreen stack can contain any task regardless of if the task is resizeable
1962 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1963 // If the freeform stack has focus, and the activity to be launched is resizeable,
1964 // we can also put it in the focused stack.
1965 final boolean canUseFocusedStack =
1966 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1967 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1968 if (canUseFocusedStack
1969 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1970 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1971 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1972 return mFocusedStack;
1973 }
1974
1975 // We first try to put the task in the first dynamic stack.
1976 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1977 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1978 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001979 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001980 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1981 "computeStackFocus: Setting focused stack=" + stack);
1982 return stack;
1983 }
1984 }
1985
1986 // If there is no suitable dynamic stack then we figure out which static stack to use.
1987 final int stackId = task != null ? task.getLaunchStackId() :
1988 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1989 FULLSCREEN_WORKSPACE_STACK_ID;
1990 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1991 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1992 + r + " stackId=" + stack.mStackId);
1993 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001994 }
1995
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001996 boolean setFocusedStack(ActivityRecord r, String reason) {
1997 if (r == null) {
1998 // Not sure what you are trying to do, but it is not going to work...
1999 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07002000 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002001 final TaskRecord task = r.task;
2002 if (task == null || task.stack == null) {
2003 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
2004 return false;
2005 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07002006 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002007 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07002008 }
2009
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002010 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002011 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Chong Zhang280d3322015-11-03 17:27:26 -08002012 boolean doResume, ActivityOptions options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002013 final Intent intent = r.intent;
2014 final int callingUid = r.launchedFromUid;
2015
Chong Zhang0fa656b2015-08-31 15:17:21 -07002016 boolean overrideBounds = false;
2017 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07002018 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002019 if (canUseActivityOptionsLaunchBounds(options)) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07002020 overrideBounds = true;
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08002021 newBounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07002022 }
2023 }
2024
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002025 // In some flows in to this function, we retrieve the task record and hold on to it
2026 // without a lock before calling back in to here... so the task at this point may
2027 // not actually be in recents. Check for that, and if it isn't in recents just
2028 // consider it invalid.
2029 if (inTask != null && !inTask.inRecents) {
2030 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
2031 inTask = null;
2032 }
2033
Craig Mautnerad400af2014-08-13 16:41:36 -07002034 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07002035 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
2036 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002037
Craig Mautnera228ae92014-07-09 05:44:55 -07002038 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002039 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07002040 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002041 // We have a conflict between the Intent and the Activity manifest, manifest wins.
2042 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
2043 "\"singleInstance\" or \"singleTask\"");
2044 launchFlags &=
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002045 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | FLAG_ACTIVITY_MULTIPLE_TASK);
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002046 } else {
2047 switch (r.info.documentLaunchMode) {
2048 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
2049 break;
2050 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
2051 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2052 break;
2053 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
2054 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
2055 break;
2056 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002057 launchFlags &= ~FLAG_ACTIVITY_MULTIPLE_TASK;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002058 break;
2059 }
2060 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002061
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002062 final boolean launchTaskBehind = r.mLaunchTaskBehind
2063 && !launchSingleTask && !launchSingleInstance
2064 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002065
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002066 if (r.resultTo != null && (launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0
Wale Ogunwale7d701172015-03-11 15:36:30 -07002067 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002068 // For whatever reason this activity is being launched into a new
2069 // task... yet the caller has requested a result back. Well, that
2070 // is pretty messed up, so instead immediately send back a cancel
2071 // and let the new task continue launched as normal without a
2072 // dependency on its originator.
2073 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
2074 r.resultTo.task.stack.sendActivityResultLocked(-1,
2075 r.resultTo, r.resultWho, r.requestCode,
2076 Activity.RESULT_CANCELED, null);
2077 r.resultTo = null;
2078 }
2079
2080 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002081 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002082 }
2083
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002084 // If we are actually going to launch in to a new task, there are some cases where
2085 // we further want to do multiple task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002086 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002087 if (launchTaskBehind
2088 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002089 launchFlags |= FLAG_ACTIVITY_MULTIPLE_TASK;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002090 }
2091 }
2092
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 // We'll invoke onUserLeaving before onPause only if the launching
2094 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07002095 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002096 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
2097 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002098
2099 // If the caller has asked not to resume at this point, we make note
2100 // of this in the record so that we can skip it when trying to find
2101 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07002102 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002103 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002104 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002105 }
2106
Craig Mautnera254cd72014-05-25 16:47:39 -07002107 ActivityRecord notTop =
2108 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002109
2110 // If the onlyIfNeeded flag is set, then we can do this if the activity
2111 // being launched is the same as the one making the call... or, as
2112 // a special case, if we do not know the caller then we count the
2113 // current top activity as the caller.
2114 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2115 ActivityRecord checkedCaller = sourceRecord;
2116 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002117 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 }
2119 if (!checkedCaller.realActivity.equals(r.realActivity)) {
2120 // Caller is not the same as launcher, so always needed.
2121 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
2122 }
2123 }
2124
Craig Mautner8849a5e2013-04-02 16:41:03 -07002125 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002126 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002127
2128 // If the caller is not coming from another activity, but has given us an
2129 // explicit task into which they would like us to launch the new activity,
2130 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002131 if (sourceRecord == null && inTask != null && inTask.stack != null) {
2132 final Intent baseIntent = inTask.getBaseIntent();
2133 final ActivityRecord root = inTask.getRootActivity();
2134 if (baseIntent == null) {
2135 ActivityOptions.abort(options);
2136 throw new IllegalArgumentException("Launching into task without base intent: "
2137 + inTask);
2138 }
2139
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002140 // If this task is empty, then we are adding the first activity -- it
2141 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002142 if (launchSingleInstance || launchSingleTask) {
2143 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2144 ActivityOptions.abort(options);
2145 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2146 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002147 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002148 if (root != null) {
2149 ActivityOptions.abort(options);
2150 throw new IllegalArgumentException("Caller with inTask " + inTask
2151 + " has root " + root + " but target is singleInstance/Task");
2152 }
2153 }
2154
2155 // If task is empty, then adopt the interesting intent launch flags in to the
2156 // activity being started.
2157 if (root == null) {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002158 final int flagsOfInterest = FLAG_ACTIVITY_NEW_TASK
2159 | FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002160 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2161 launchFlags = (launchFlags&~flagsOfInterest)
2162 | (baseIntent.getFlags()&flagsOfInterest);
2163 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002164 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002165 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002166
Dianne Hackborn962d5352014-08-29 16:27:40 -07002167 // If the task is not empty and the caller is asking to start it as the root
2168 // of a new task, then we don't actually want to start this on the task. We
2169 // will bring the task to the front, and possibly give it a new intent.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002170 } else if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002171 addingToTask = false;
2172
2173 } else {
2174 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002175 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002176
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002177 reuseTask = inTask;
2178 } else {
2179 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002180 // Launch ResolverActivity in the source task, so that it stays in the task
2181 // bounds when in freeform workspace.
2182 // Also put noDisplay activities in the source task. These by itself can
2183 // be placed in any task/stack, however it could launch other activities
2184 // like ResolverActivity, and we want those to stay in the original task.
2185 if (r.isResolverActivity() || r.noDisplay) {
2186 addingToTask = true;
2187 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002188 }
2189
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002190 if (inTask == null) {
2191 if (sourceRecord == null) {
2192 // This activity is not being started from another... in this
2193 // case we -always- start a new task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002194 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002195 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2196 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002197 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002198 }
2199 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2200 // The original activity who is starting us is running as a single
2201 // instance... this new activity it is starting must go on its
2202 // own task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002203 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002204 } else if (launchSingleInstance || launchSingleTask) {
2205 // The activity being started is a single instance... it always
2206 // gets launched into its own task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002207 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002208 }
2209 }
2210
2211 ActivityInfo newTaskInfo = null;
2212 Intent newTaskIntent = null;
2213 ActivityStack sourceStack;
2214 if (sourceRecord != null) {
2215 if (sourceRecord.finishing) {
2216 // If the source is finishing, we can't further count it as our source. This
2217 // is because the task it is associated with may now be empty and on its way out,
2218 // so we don't want to blindly throw it in to that task. Instead we will take
2219 // the NEW_TASK flow and try to find a task for it. But save the task information
2220 // so it can be used when creating the new task.
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002221 if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002222 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2223 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002224 launchFlags |= FLAG_ACTIVITY_NEW_TASK;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002225 newTaskInfo = sourceRecord.info;
2226 newTaskIntent = sourceRecord.task.intent;
2227 }
2228 sourceRecord = null;
2229 sourceStack = null;
2230 } else {
2231 sourceStack = sourceRecord.task.stack;
2232 }
2233 } else {
2234 sourceStack = null;
2235 }
2236
2237 boolean movedHome = false;
2238 ActivityStack targetStack;
2239
2240 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002241 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002242
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002243 ActivityRecord intentActivity = getReusableIntentActivity(r, inTask, intent,
2244 launchSingleInstance, launchSingleTask, launchFlags);
2245 if (intentActivity != null) {
2246 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2247 // but still needs to be a lock task mode violation since the task gets
2248 // cleared out and the device would otherwise leave the locked task.
2249 if (isLockTaskModeViolation(intentActivity.task,
2250 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2251 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
2252 showLockTaskToast();
2253 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
2254 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2255 }
2256 if (r.task == null) {
2257 r.task = intentActivity.task;
2258 }
2259 if (intentActivity.task.intent == null) {
2260 // This task was started because of movement of the activity based on affinity...
2261 // Now that we are actually launching it, we can assign the base intent.
2262 intentActivity.task.setIntent(r);
2263 }
2264 targetStack = intentActivity.task.stack;
2265 targetStack.mLastPausedActivity = null;
2266 // If the target task is not in the front, then we need
2267 // to bring it to the front... except... well, with
2268 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2269 // to have the same behavior as if a new instance was
2270 // being started, which means not bringing it to the front
2271 // if the caller is not itself in the front.
2272 final ActivityStack focusStack = getFocusedStack();
2273 ActivityRecord curTop = (focusStack == null)
2274 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
2275 boolean movedToFront = false;
2276 if (curTop != null && (curTop.task != intentActivity.task ||
2277 curTop.task != focusStack.topTask())) {
2278 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
2279 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2280 sourceStack.topActivity().task == sourceRecord.task)) {
2281 // We really do want to push this one into the user's face, right now.
2282 if (launchTaskBehind && sourceRecord != null) {
2283 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
Craig Mautneraea74a52014-03-08 14:23:10 -08002284 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002285 movedHome = true;
2286 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
2287 options, r.appTimeTracker, "bringingFoundTaskToFront");
2288 movedToFront = true;
2289 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2290 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
2291 // Caller wants to appear on home activity.
2292 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautner29219d92013-04-16 20:19:12 -07002293 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002294 options = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002295 }
2296 }
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002297 if (!movedToFront && doResume) {
2298 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
2299 + " from " + intentActivity);
2300 targetStack.moveToFront("intentActivityFound");
2301 }
2302
2303 // If the caller has requested that the target task be
2304 // reset, then do so.
2305 if ((launchFlags & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2306 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2307 }
2308 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2309 // We don't need to start a new activity, and
2310 // the client said not to do anything if that
2311 // is the case, so this is it! And for paranoia, make
2312 // sure we have correctly resumed the top activity.
2313 if (doResume) {
2314 resumeTopActivitiesLocked(targetStack, null, options);
2315
2316 // Make sure to notify Keyguard as well if we are not running an app
2317 // transition later.
2318 if (!movedToFront) {
2319 notifyActivityDrawnForKeyguard();
2320 }
2321 } else {
2322 ActivityOptions.abort(options);
2323 }
2324 updateUserStackLocked(r.userId, targetStack);
2325 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2326 }
2327 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2328 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
2329 // The caller has requested to completely replace any
2330 // existing task with its new activity. Well that should
2331 // not be too hard...
2332 reuseTask = intentActivity.task;
2333 reuseTask.performClearTaskLocked();
2334 reuseTask.setIntent(r);
2335 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
2336 || launchSingleInstance || launchSingleTask) {
2337 // In this situation we want to remove all activities
2338 // from the task up to the one being started. In most
2339 // cases this means we are resetting the task to its
2340 // initial state.
2341 ActivityRecord top = intentActivity.task.performClearTaskLocked(r, launchFlags);
2342 if (top != null) {
2343 if (top.frontOfTask) {
2344 // Activity aliases may mean we use different
2345 // intents for the top activity, so make sure
2346 // the task now has the identity of the new
2347 // intent.
2348 top.task.setIntent(r);
2349 }
2350 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
2351 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
2352 } else {
2353 // A special case: we need to start the activity because it is not
2354 // currently running, and the caller has asked to clear the current
2355 // task to have this activity at the top.
2356 addingToTask = true;
2357 // Now pretend like this activity is being started by the top of its
2358 // task, so it is put in the right place.
2359 sourceRecord = intentActivity;
2360 TaskRecord task = sourceRecord.task;
2361 if (task != null && task.stack == null) {
2362 // Target stack got cleared when we all activities were removed
2363 // above. Go ahead and reset it.
2364 targetStack = computeStackFocus(
2365 sourceRecord, false /* newTask */, null /* bounds */);
2366 targetStack.addTask(task,
2367 !launchTaskBehind /* toTop */, "startActivityUnchecked");
2368 }
2369
2370 }
2371 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2372 // In this case the top activity on the task is the
2373 // same as the one being launched, so we take that
2374 // as a request to bring the task to the foreground.
2375 // If the top activity in the task is the root
2376 // activity, deliver this new intent to it if it
2377 // desires.
2378 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
2379 && intentActivity.realActivity.equals(r.realActivity)) {
2380 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2381 intentActivity.task);
2382 if (intentActivity.frontOfTask) {
2383 intentActivity.task.setIntent(r);
2384 }
2385 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2386 r.launchedFromPackage);
2387 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2388 // In this case we are launching the root activity
2389 // of the task, but with a different intent. We
2390 // should start a new instance on top.
2391 addingToTask = true;
2392 sourceRecord = intentActivity;
2393 }
2394 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2395 // In this case an activity is being launched in to an
2396 // existing task, without resetting that task. This
2397 // is typically the situation of launching an activity
2398 // from a notification or shortcut. We want to place
2399 // the new activity on top of the current task.
2400 addingToTask = true;
2401 sourceRecord = intentActivity;
2402 } else if (!intentActivity.task.rootWasReset) {
2403 // In this case we are launching in to an existing task
2404 // that has not yet been started from its front door.
2405 // The current task has been brought to the front.
2406 // Ideally, we'd probably like to place this new task
2407 // at the bottom of its stack, but that's a little hard
2408 // to do with the current organization of the code so
2409 // for now we'll just drop it.
2410 intentActivity.task.setIntent(r);
2411 }
2412 if (!addingToTask && reuseTask == null) {
2413 // We didn't do anything... but it was needed (a.k.a., client
2414 // don't use that intent!) And for paranoia, make
2415 // sure we have correctly resumed the top activity.
2416 if (doResume) {
2417 targetStack.resumeTopActivityLocked(null, options);
2418 if (!movedToFront) {
2419 // Make sure to notify Keyguard as well if we are not running an app
2420 // transition later.
2421 notifyActivityDrawnForKeyguard();
2422 }
2423 } else {
2424 ActivityOptions.abort(options);
2425 }
2426 updateUserStackLocked(r.userId, targetStack);
2427 return ActivityManager.START_TASK_TO_FRONT;
2428 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002429 }
2430
2431 //String uri = r.intent.toURI();
2432 //Intent intent2 = new Intent(uri);
2433 //Slog.i(TAG, "Given intent: " + r.intent);
2434 //Slog.i(TAG, "URI is: " + uri);
2435 //Slog.i(TAG, "To intent: " + intent2);
2436
2437 if (r.packageName != null) {
2438 // If the activity being launched is the same as the one currently
2439 // at the top, then we need to check if it should only be launched
2440 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002441 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002442 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002443 if (top != null && r.resultTo == null) {
2444 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2445 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002446 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002447 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002448 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2449 top.task);
2450 // For paranoia, make sure we have correctly
2451 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002452 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002453 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002454 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002455 }
2456 ActivityOptions.abort(options);
2457 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2458 // We don't need to start a new activity, and
2459 // the client said not to do anything if that
2460 // is the case, so this is it!
2461 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2462 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002463 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002464 return ActivityManager.START_DELIVERED_TO_TOP;
2465 }
2466 }
2467 }
2468 }
2469
2470 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002471 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002472 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2473 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002474 }
2475 ActivityOptions.abort(options);
2476 return ActivityManager.START_CLASS_NOT_FOUND;
2477 }
2478
2479 boolean newTask = false;
2480 boolean keepCurTransition = false;
2481
Craig Mautnerbb742462014-07-07 15:28:55 -07002482 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002483 sourceRecord.task : null;
2484
Craig Mautner8849a5e2013-04-02 16:41:03 -07002485 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002486 if (r.resultTo == null && inTask == null && !addingToTask
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002487 && (launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002488 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002489 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002490 if (doResume) {
2491 targetStack.moveToFront("startingNewTask");
2492 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002493
Craig Mautner8849a5e2013-04-02 16:41:03 -07002494 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002495 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2496 newTaskInfo != null ? newTaskInfo : r.info,
2497 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002498 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2499 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002500 if (overrideBounds) {
2501 r.task.updateOverrideConfiguration(newBounds);
2502 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002503 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2504 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002505 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002506 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002507 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002508 if (isLockTaskModeViolation(r.task)) {
2509 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2510 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2511 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002512 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002513 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002514 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2515 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002516 // Caller wants to appear on home activity, so before starting
2517 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002518 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002519 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002520 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002521 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002522 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002523 if (isLockTaskModeViolation(sourceTask)) {
2524 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2525 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2526 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002527 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002528 if (doResume) {
2529 targetStack.moveToFront("sourceStackToFront");
2530 }
Craig Mautner737fae22014-10-15 12:52:10 -07002531 final TaskRecord topTask = targetStack.topTask();
2532 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002533 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002534 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002535 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002536 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002537 // In this case, we are adding the activity to an existing
2538 // task, but the caller has asked to clear that task if the
2539 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002540 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002541 keepCurTransition = true;
2542 if (top != null) {
2543 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002544 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002545 // For paranoia, make sure we have correctly
2546 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002547 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002548 if (doResume) {
2549 targetStack.resumeTopActivityLocked(null);
2550 }
2551 ActivityOptions.abort(options);
2552 return ActivityManager.START_DELIVERED_TO_TOP;
2553 }
2554 } else if (!addingToTask &&
2555 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2556 // In this case, we are launching an activity in our own task
2557 // that may already be running somewhere in the history, and
2558 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002559 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002560 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002561 final TaskRecord task = top.task;
2562 task.moveActivityToFrontLocked(top);
2563 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002564 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002565 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002566 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002567 if (doResume) {
2568 targetStack.resumeTopActivityLocked(null);
2569 }
2570 return ActivityManager.START_DELIVERED_TO_TOP;
2571 }
2572 }
2573 // An existing activity is starting this new activity, so we want
2574 // to keep the new one in the same task as the one that is starting
2575 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002576 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002577 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002578 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002579
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002580 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002581 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002582 // task it has provided to us.
2583 if (isLockTaskModeViolation(inTask)) {
2584 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2585 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2586 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002587 if (overrideBounds) {
2588 inTask.updateOverrideConfiguration(newBounds);
2589 int stackId = inTask.getLaunchStackId();
2590 if (stackId != inTask.stack.mStackId) {
2591 moveTaskToStackUncheckedLocked(
2592 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2593 }
2594 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002595 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002596 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2597 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002598
2599 // Check whether we should actually launch the new activity in to the task,
2600 // or just reuse the current activity on top.
2601 ActivityRecord top = inTask.getTopActivity();
2602 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2603 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2604 || launchSingleTop || launchSingleTask) {
2605 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2606 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2607 // We don't need to start a new activity, and
2608 // the client said not to do anything if that
2609 // is the case, so this is it!
2610 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2611 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002612 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002613 return ActivityManager.START_DELIVERED_TO_TOP;
2614 }
2615 }
2616
Dianne Hackborn962d5352014-08-29 16:27:40 -07002617 if (!addingToTask) {
2618 // We don't actually want to have this activity added to the task, so just
2619 // stop here but still tell the caller that we consumed the intent.
2620 ActivityOptions.abort(options);
2621 return ActivityManager.START_TASK_TO_FRONT;
2622 }
2623
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002624 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002625 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002626 + " in explicit task " + r.task);
2627
Craig Mautner8849a5e2013-04-02 16:41:03 -07002628 } else {
2629 // This not being started from an existing activity, and not part
2630 // of a new task... just put it in the top task, though these days
2631 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002632 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002633 if (doResume) {
2634 targetStack.moveToFront("addingToTopTask");
2635 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002636 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002637 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002638 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002639 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002640 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002641 + " in new guessed " + r.task);
2642 }
2643
2644 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002645 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002646
Craig Mautner76e2a762014-06-24 09:05:43 -07002647 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2648 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2649 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002650 if (newTask) {
2651 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2652 }
2653 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002654 targetStack.mLastPausedActivity = null;
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002655 targetStack.startActivityLocked(r, newTask, keepCurTransition, options);
2656 if (doResume) {
2657 if (!launchTaskBehind) {
2658 mService.setFocusedActivityLocked(r, "startedActivity");
2659 }
2660 resumeTopActivitiesLocked(targetStack, r, options);
2661 } else {
2662 targetStack.addRecentActivityLocked(r);
Craig Mautnerbb742462014-07-07 15:28:55 -07002663 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002664 updateUserStackLocked(r.userId, targetStack);
Chong Zhangc806d902015-11-30 09:44:27 -08002665
2666 if (!r.task.mResizeable && isStackDockedInEffect(targetStack.mStackId)) {
2667 showNonResizeableDockToast(r.task.taskId);
2668 }
2669
Craig Mautner8849a5e2013-04-02 16:41:03 -07002670 return ActivityManager.START_SUCCESS;
2671 }
2672
Filip Gruszczynskidc394902015-12-14 10:20:22 -08002673 /**
2674 * Decide whether the new activity should be inserted into an existing task. Returns null if not
2675 * or an ActivityRecord with the task into which the new activity should be added.
2676 */
2677 private ActivityRecord getReusableIntentActivity(ActivityRecord r, TaskRecord inTask, Intent intent,
2678 boolean launchSingleInstance, boolean launchSingleTask, int launchFlags) {
2679 // We may want to try to place the new activity in to an existing task. We always
2680 // do this if the target activity is singleTask or singleInstance; we will also do
2681 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2682 // us to still place it in a new task: multi task, always doc mode, or being asked to
2683 // launch this as a new task behind the current one.
2684 boolean putIntoExistingTask = ((launchFlags & FLAG_ACTIVITY_NEW_TASK) != 0 &&
2685 (launchFlags & FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
2686 || launchSingleInstance || launchSingleTask;
2687 // If bring to front is requested, and no result is requested and we have not
2688 // been given an explicit task to launch in to, and
2689 // we can find a task that was started with this same
2690 // component, then instead of launching bring that one to the front.
2691 putIntoExistingTask &= inTask == null && r.resultTo == null;
2692 ActivityRecord intentActivity = null;
2693 if (putIntoExistingTask) {
2694 // See if there is a task to bring to the front. If this is
2695 // a SINGLE_INSTANCE activity, there can be one and only one
2696 // instance of it in the history, and it is always in its own
2697 // unique task, so we do a special search.
2698 intentActivity = launchSingleInstance ?
2699 findActivityLocked(intent, r.info) : findTaskLocked(r);
2700 }
2701 return intentActivity;
2702 }
2703
Craig Mautneree36c772014-07-16 14:56:05 -07002704 final void doPendingActivityLaunchesLocked(boolean doResume) {
2705 while (!mPendingActivityLaunches.isEmpty()) {
2706 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002707
2708 try {
2709 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002710 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002711 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002712 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2713 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002714 }
Craig Mautneree36c772014-07-16 14:56:05 -07002715 }
2716 }
2717
Craig Mautner7f13ed32014-07-28 14:00:35 -07002718 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002719 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2720 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002721 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002722 mPendingActivityLaunches.remove(palNdx);
2723 }
2724 }
2725 }
2726
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002727 void setLaunchSource(int uid) {
2728 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2729 }
2730
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002731 void acquireLaunchWakelock() {
2732 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2733 throw new IllegalStateException("Calling must be system uid");
2734 }
2735 mLaunchingActivity.acquire();
2736 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2737 // To be safe, don't allow the wake lock to be held for too long.
2738 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2739 }
2740 }
2741
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002742 /**
2743 * Called when the frontmost task is idle.
2744 * @return the state of mService.mBooting before this was called.
2745 */
2746 private boolean checkFinishBootingLocked() {
2747 final boolean booting = mService.mBooting;
2748 boolean enableScreen = false;
2749 mService.mBooting = false;
2750 if (!mService.mBooted) {
2751 mService.mBooted = true;
2752 enableScreen = true;
2753 }
2754 if (booting || enableScreen) {
2755 mService.postFinishBooting(booting, enableScreen);
2756 }
2757 return booting;
2758 }
2759
Craig Mautnerf3333272013-04-22 10:55:53 -07002760 // Checked.
2761 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2762 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002763 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002764
Craig Mautnerf3333272013-04-22 10:55:53 -07002765 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002766 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002767 int NS = 0;
2768 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002769 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002770 boolean activityRemoved = false;
2771
Wale Ogunwale7d701172015-03-11 15:36:30 -07002772 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002773 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002774 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2775 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002776 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2777 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002778 if (fromTimeout) {
2779 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002780 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002781
2782 // This is a hack to semi-deal with a race condition
2783 // in the client where it can be constructed with a
2784 // newer configuration from when we asked it to launch.
2785 // We'll update with whatever configuration it now says
2786 // it used to launch.
2787 if (config != null) {
2788 r.configuration = config;
2789 }
2790
2791 // We are now idle. If someone is waiting for a thumbnail from
2792 // us, we can now deliver.
2793 r.idle = true;
2794
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002795 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002796 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002797 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002798 }
2799 }
2800
2801 if (allResumedActivitiesIdle()) {
2802 if (r != null) {
2803 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002804 }
2805
2806 if (mLaunchingActivity.isHeld()) {
2807 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2808 if (VALIDATE_WAKE_LOCK_CALLER &&
2809 Binder.getCallingUid() != Process.myUid()) {
2810 throw new IllegalStateException("Calling must be system uid");
2811 }
2812 mLaunchingActivity.release();
2813 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002814 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002815 }
2816
2817 // Atomically retrieve all of the other things to do.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08002818 final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(true);
Craig Mautnerf3333272013-04-22 10:55:53 -07002819 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002820 if ((NF = mFinishingActivities.size()) > 0) {
2821 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002822 mFinishingActivities.clear();
2823 }
2824
Craig Mautnerf3333272013-04-22 10:55:53 -07002825 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002826 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002827 mStartingUsers.clear();
2828 }
2829
Craig Mautnerf3333272013-04-22 10:55:53 -07002830 // Stop any activities that are scheduled to do so but have been
2831 // waiting for the next one to start.
2832 for (int i = 0; i < NS; i++) {
2833 r = stops.get(i);
2834 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002835 if (stack != null) {
2836 if (r.finishing) {
2837 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2838 } else {
2839 stack.stopActivityLocked(r);
2840 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002841 }
2842 }
2843
2844 // Finish any activities that are scheduled to do so but have been
2845 // waiting for the next one to start.
2846 for (int i = 0; i < NF; i++) {
2847 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002848 final ActivityStack stack = r.task.stack;
2849 if (stack != null) {
2850 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2851 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002852 }
2853
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002854 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002855 // Complete user switch
2856 if (startingUsers != null) {
2857 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002858 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002859 }
2860 }
2861 // Complete starting up of background users
2862 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002863 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002864 mStartingBackgroundUsers.clear();
2865 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002866 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002867 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002868 }
2869 }
2870
2871 mService.trimApplications();
2872 //dump();
2873 //mWindowManager.dump();
2874
Craig Mautnerf3333272013-04-22 10:55:53 -07002875 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002876 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002877 }
2878
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002879 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002880 }
2881
Craig Mautner8e569572013-10-11 17:36:59 -07002882 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002883 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002884 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2885 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002886 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2887 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2888 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002889 }
Craig Mautner19091252013-10-05 00:03:53 -07002890 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002891 }
2892
2893 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002894 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2895 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002896 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2897 stacks.get(stackNdx).closeSystemDialogsLocked();
2898 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002899 }
2900 }
2901
Craig Mautner93529a42013-10-04 15:03:13 -07002902 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002903 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002904 }
2905
Craig Mautner8d341ef2013-03-26 09:03:27 -07002906 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002907 * Update the last used stack id for non-current user (current user's last
2908 * used stack is the focused stack)
2909 */
2910 void updateUserStackLocked(int userId, ActivityStack stack) {
2911 if (userId != mCurrentUser) {
2912 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2913 }
2914 }
2915
2916 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002917 * @return true if some activity was finished (or would have finished if doit were true).
2918 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002919 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2920 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002921 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002922 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2923 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002924 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002925 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002926 if (stack.finishDisabledPackageActivitiesLocked(
2927 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002928 didSomething = true;
2929 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002930 }
2931 }
2932 return didSomething;
2933 }
2934
Dianne Hackborna413dc02013-07-12 12:02:55 -07002935 void updatePreviousProcessLocked(ActivityRecord r) {
2936 // Now that this process has stopped, we may want to consider
2937 // it to be the previous app to try to keep around in case
2938 // the user wants to return to it.
2939
2940 // First, found out what is currently the foreground app, so that
2941 // we don't blow away the previous app if this activity is being
2942 // hosted by the process that is actually still the foreground.
2943 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002944 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2945 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2947 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002948 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002949 if (stack.mResumedActivity != null) {
2950 fgApp = stack.mResumedActivity.app;
2951 } else if (stack.mPausingActivity != null) {
2952 fgApp = stack.mPausingActivity.app;
2953 }
2954 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002955 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002956 }
2957 }
2958
2959 // Now set this one as the previous process, only if that really
2960 // makes sense to.
2961 if (r.app != null && fgApp != null && r.app != fgApp
2962 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002963 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002964 mService.mPreviousProcess = r.app;
2965 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2966 }
2967 }
2968
Craig Mautner05d29032013-05-03 13:40:13 -07002969 boolean resumeTopActivitiesLocked() {
2970 return resumeTopActivitiesLocked(null, null, null);
2971 }
2972
Chong Zhang280d3322015-11-03 17:27:26 -08002973 boolean resumeTopActivitiesLocked(
2974 ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
Craig Mautner05d29032013-05-03 13:40:13 -07002975 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002976 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002977 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002978 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002979 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002980 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002981 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2982 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002983
Craig Mautnere0a38842013-12-16 16:14:02 -08002984 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2985 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002986 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2987 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002988 if (stack == targetStack) {
2989 // Already started above.
2990 continue;
2991 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002992 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002993 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002994 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002995 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002996 }
Craig Mautner05d29032013-05-03 13:40:13 -07002997 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002998 }
2999
Todd Kennedy539db512014-12-15 09:57:55 -08003000 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003001 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3002 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003003 final int numStacks = stacks.size();
3004 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3005 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08003006 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003007 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003008 }
3009 }
3010
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003011 void finishVoiceTask(IVoiceInteractionSession session) {
3012 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3013 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3014 final int numStacks = stacks.size();
3015 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3016 final ActivityStack stack = stacks.get(stackNdx);
3017 stack.finishVoiceTask(session);
3018 }
3019 }
3020 }
3021
Chong Zhang280d3322015-11-03 17:27:26 -08003022 void findTaskToMoveToFrontLocked(
3023 TaskRecord task, int flags, ActivityOptions options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003024 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
3025 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003026 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003027 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
3028 // Caller wants the home activity moved with it. To accomplish this,
3029 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07003030 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08003031 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07003032 if (task.stack == null) {
3033 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
3034 + task + " to front. Stack is null");
3035 return;
3036 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07003037
Chong Zhang0fa656b2015-08-31 15:17:21 -07003038 if (task.mResizeable && options != null) {
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08003039 if (canUseActivityOptionsLaunchBounds(options)) {
3040 Rect bounds = options.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07003041 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08003042 final int stackId = task.getLaunchStackId();
3043 if (stackId != task.stack.mStackId) {
3044 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
3045 // moveTaskToStackUncheckedLocked() should already placed the task on top,
3046 // still need moveTaskToFrontLocked() below for any transition settings.
3047 }
3048 // WM resizeTask must be done after the task is moved to the correct stack,
3049 // because Task's setBounds() also updates dim layer's bounds, but that has
3050 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07003051 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
3052 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07003053 }
3054 }
3055
Chong Zhangdb20b5f2015-10-23 14:01:43 -07003056 final ActivityRecord r = task.getTopActivity();
3057 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
3058 r == null ? null : r.appTimeTracker, reason);
3059
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003060 if (DEBUG_STACK) Slog.d(TAG_STACK,
3061 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003062 }
3063
Wale Ogunwale7a8fa602015-11-18 15:56:57 -08003064 private boolean canUseActivityOptionsLaunchBounds(ActivityOptions options) {
3065 // We use the launch bounds in the activity options is the device supports freeform
3066 // window management.
3067 return options.hasLaunchBounds() && mService.mSupportsFreeformWindowManagement;
3068 }
3069
Craig Mautner967212c2013-04-13 21:10:58 -07003070 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003071 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003072 }
3073
3074 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003075 ActivityContainer activityContainer = mActivityContainers.get(stackId);
3076 if (activityContainer != null) {
3077 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003078 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07003079 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003080 return null;
3081 }
3082 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003083 }
3084
Craig Mautner967212c2013-04-13 21:10:58 -07003085 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003086 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003087 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3088 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003089 }
3090 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07003091 }
3092
Craig Mautner4a1cb222013-12-04 16:14:06 -08003093 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003094 ActivityRecord homeActivity = getHomeActivity();
3095 if (homeActivity != null) {
3096 return homeActivity.appToken;
3097 }
3098 return null;
3099 }
3100
3101 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07003102 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003103 }
3104
3105 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003106 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
3107 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
3108 final TaskRecord task = tasks.get(taskNdx);
3109 if (task.isHomeTask()) {
3110 final ArrayList<ActivityRecord> activities = task.mActivities;
3111 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3112 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07003113 if (r.isHomeActivity()
3114 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003115 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003116 }
3117 }
3118 }
3119 }
3120 return null;
3121 }
3122
Chong Zhangb15758a2015-11-17 12:12:03 -08003123 /**
3124 * Returns if a stack should be treated as if it's docked. Returns true if the stack is
3125 * the docked stack itself, or if it's side-by-side to the docked stack.
3126 */
3127 boolean isStackDockedInEffect(int stackId) {
3128 return stackId == DOCKED_STACK_ID ||
3129 (StackId.isResizeableByDockedStack(stackId) && getStack(DOCKED_STACK_ID) != null);
3130 }
3131
Todd Kennedyca4d8422015-01-15 15:19:22 -08003132 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08003133 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003134 ActivityContainer activityContainer =
3135 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003136 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003137 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3138 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003139 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003140 return activityContainer;
3141 }
3142
Craig Mautner34b73df2014-01-12 21:11:08 -08003143 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003144 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
3145 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
3146 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003147 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
3148 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003149 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08003150 }
3151 }
3152
Craig Mautner95da1082014-02-24 17:54:35 -08003153 void deleteActivityContainer(IActivityContainer container) {
3154 ActivityContainer activityContainer = (ActivityContainer)container;
3155 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003156 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
3157 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08003158 final int stackId = activityContainer.mStackId;
3159 mActivityContainers.remove(stackId);
3160 mWindowManager.removeStack(stackId);
3161 }
3162 }
3163
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003164 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
3165 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003166 final ActivityStack stack = getStack(stackId);
3167 if (stack == null) {
3168 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
3169 return;
3170 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003171
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003172 if (!allowResizeInDockedMode
3173 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3174 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3175 // stack size changing so things don't get out of sync.
3176 return;
3177 }
3178
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003179 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003180 mWindowManager.deferSurfaceLayout();
3181 try {
Wale Ogunwaleb4ec0a32015-12-14 10:31:43 -08003182
3183 if (bounds != null && mWindowManager.isFullscreenBounds(stackId, bounds)) {
3184 // The bounds passed in corresponds to the fullscreen bounds which we normally
3185 // represent with null. Go ahead and set it to null so that all tasks configuration
3186 // can have the right fullscreen state.
3187 bounds = null;
3188 }
3189
Jorim Jaggic4025202015-10-22 16:43:34 +02003190 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003191
Jorim Jaggic4025202015-10-22 16:43:34 +02003192 mTmpBounds.clear();
3193 mTmpConfigs.clear();
3194 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3195 for (int i = tasks.size() - 1; i >= 0; i--) {
3196 TaskRecord task = tasks.get(i);
3197 if (task.mResizeable) {
3198 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3199 // For freeform stack we don't adjust the size of the tasks to match that
3200 // of the stack, but we do try to make sure the tasks are still contained
3201 // with the bounds of the stack.
3202 tempRect2.set(task.mBounds);
3203 fitWithinBounds(tempRect2, bounds);
3204 task.updateOverrideConfiguration(tempRect2);
3205 } else {
3206 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003207 }
3208 }
3209
Jorim Jaggic4025202015-10-22 16:43:34 +02003210 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3211 mTmpBounds.put(task.taskId, task.mBounds);
3212 }
3213 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3214 if (stack.mStackId == DOCKED_STACK_ID) {
3215 // Dock stack funness...Yay!
3216 if (stack.mFullscreen) {
3217 // The dock stack went fullscreen which is kinda like dismissing it.
3218 // In this case we make all other static stacks fullscreen and move all
3219 // docked stack tasks to the fullscreen stack.
3220 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003221 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003222 resizeStackLocked(i, null, preserveWindows, true);
3223 }
3224 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003225
Jorim Jaggic4025202015-10-22 16:43:34 +02003226 final int count = tasks.size();
3227 for (int i = 0; i < count; i++) {
3228 moveTaskToStackLocked(tasks.get(i).taskId,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003229 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack",
Jorim Jaggi55387522015-11-24 18:21:10 -08003230 false /* animate */);
Jorim Jaggic4025202015-10-22 16:43:34 +02003231 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003232
Jorim Jaggic4025202015-10-22 16:43:34 +02003233 // stack shouldn't contain anymore activities, so nothing to resume.
3234 r = null;
3235 } else {
3236 // Docked stacks occupy a dedicated region on screen so the size of all other
3237 // static stacks need to be adjusted so they don't overlap with the docked stack.
3238 // We get the bounds to use from window manager which has been adjusted for any
3239 // screen controls and is also the same for all stacks.
3240 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3241
3242 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003243 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003244 ActivityStack otherStack = getStack(i);
3245 if (otherStack != null) {
3246 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3247 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003248 }
3249 }
3250 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003251 // Since we are resizing the stack, all other operations should strive to preserve
3252 // windows.
3253 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003254 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003255 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003256
Jorim Jaggic4025202015-10-22 16:43:34 +02003257 if (r != null) {
3258 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3259 // And we need to make sure at this point that all other activities
3260 // are made visible with the correct configuration.
3261 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3262 if (!updated) {
3263 resumeTopActivitiesLocked(stack, null, null);
3264 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003265 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003266 } finally {
3267 mWindowManager.continueSurfaceLayout();
3268 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003269 }
3270 }
3271
Chong Zhang6de2ae82015-09-30 18:25:21 -07003272 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003273 if (!task.mResizeable) {
3274 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3275 return;
3276 }
3277
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003278 adjustForMinimalTaskDimensions(task, bounds);
3279
Chong Zhang87b21722015-09-21 15:39:51 -07003280 // If this is a forced resize, let it go through even if the bounds is not changing,
3281 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003282 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003283 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003284 // Nothing to do here...
3285 return;
3286 }
3287
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003288 if (!mWindowManager.isValidTaskId(task.taskId)) {
3289 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003290 // All we can do for now is update the bounds so it can be used when the task is
3291 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003292 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003293 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3294 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003295 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003296 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003297 return;
3298 }
3299
Chong Zhang6de2ae82015-09-30 18:25:21 -07003300 // Do not move the task to another stack here.
3301 // This method assumes that the task is already placed in the right stack.
3302 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003303
Chong Zhang6de2ae82015-09-30 18:25:21 -07003304 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003305
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003306 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003307 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003308 // way and the activity was kept the way it was. If it's false, it means the activity had
3309 // to be relaunched due to configuration change.
3310 boolean kept = true;
3311 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003312 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003313 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003314 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003315 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003316 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003317 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003318 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003319 resumeTopActivitiesLocked(stack, null, null);
3320 }
3321 }
3322 }
Chong Zhang87b21722015-09-21 15:39:51 -07003323 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003324
3325 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003326 }
3327
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003328 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3329 if (bounds == null) {
3330 return;
3331 }
3332 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3333 : task.mMinimalSize;
3334 final boolean adjustWidth = minimalSize > bounds.width();
3335 final boolean adjustHeight = minimalSize > bounds.height();
3336 if (!(adjustWidth || adjustHeight)) {
3337 return;
3338 }
3339 Rect taskBounds = task.mBounds;
3340 if (adjustWidth) {
3341 if (taskBounds != null && bounds.right == taskBounds.right) {
3342 bounds.left = bounds.right - minimalSize;
3343 } else {
3344 // Either left bounds match, or neither match, or the previous bounds were
3345 // fullscreen and we default to keeping left.
3346 bounds.right = bounds.left + minimalSize;
3347 }
3348 }
3349 if (adjustHeight) {
3350 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3351 bounds.top = bounds.bottom - minimalSize;
3352 } else {
3353 // Either top bounds match, or neither match, or the previous bounds were
3354 // fullscreen and we default to keeping top.
3355 bounds.bottom = bounds.top + minimalSize;
3356 }
3357 }
3358 }
3359
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003360 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003361 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3362 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003363 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003364 }
3365
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003366 ActivityContainer activityContainer = new ActivityContainer(stackId);
3367 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003368 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003369 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003370 }
3371
3372 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003373 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003374 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3375 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003376 break;
3377 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003378 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003379 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003380 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003381 }
3382
Chong Zhang5dcb2752015-08-18 13:50:26 -07003383 /**
3384 * Restores a recent task to a stack
3385 * @param task The recent task to be restored.
3386 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003387 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003388 * @return true if the task has been restored successfully.
3389 */
3390 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3391 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003392 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003393 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003394 if (task.stack != null) {
3395 // Task has already been restored once. See if we need to do anything more
3396 if (task.stack.mStackId == stackId) {
3397 // Nothing else to do since it is already restored in the right stack.
3398 return true;
3399 }
3400 // Remove current stack association, so we can re-associate the task with the
3401 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003402 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003403 }
3404
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003405 final ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003406 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003407
3408 if (stack == null) {
3409 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003410 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3411 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003412 return false;
3413 }
3414
Wale Ogunwale5f986092015-12-04 15:35:38 -08003415 stack.addTask(task, false, "restoreRecentTask");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003416 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3417 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003418 final ArrayList<ActivityRecord> activities = task.mActivities;
3419 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003420 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003421 }
3422 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003423 }
3424
Wale Ogunwale040b4702015-08-06 18:10:50 -07003425 /**
3426 * Moves the specified task record to the input stack id.
3427 * WARNING: This method performs an unchecked/raw move of the task and
3428 * can leave the system in an unstable state if used incorrectly.
3429 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3430 * to a stack.
3431 * @param task Task to move.
3432 * @param stackId Id of stack to move task to.
3433 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003434 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003435 * @param reason Reason the task is been moved.
3436 * @return The stack the task was moved to.
3437 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003438 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003439 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3440 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003441 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003442 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3443
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003444 final boolean resizeable = task.mResizeable;
3445 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3446 // if a docked stack is created below which will lead to the stack we are moving from and
3447 // its resizeable tasks being resized.
3448 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003449 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003450 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003451 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003452 stack.addTask(task, toTop, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003453
3454 // If the task had focus before (or we're requested to move focus),
3455 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003456 stack.setFocusAndResumeStateIfNeeded(
3457 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003458
Wale Ogunwale040b4702015-08-06 18:10:50 -07003459 return stack;
3460 }
3461
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003462 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003463 String reason, boolean animate) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003464 final TaskRecord task = anyTaskForIdLocked(taskId);
3465 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003466 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003467 return;
3468 }
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003469
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003470 if (task.stack != null && task.stack.mStackId == stackId) {
3471 // You are already in the right stack silly...
3472 Slog.i(TAG, "moveTaskToStack: taskId=" + taskId + " already in stackId=" + stackId);
3473 return;
3474 }
3475
Wale Ogunwale6c5eb1c2015-11-10 07:52:22 -08003476 final ActivityRecord topActivity = task.getTopActivity();
3477 if (StackId.preserveWindowOnTaskMove(stackId) && topActivity != null) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003478 // We are about to relaunch the activity because its configuration changed due to
3479 // being maximized, i.e. size change. The activity will first remove the old window
3480 // and then add a new one. This call will tell window manager about this, so it can
3481 // preserve the old window until the new one is drawn. This prevents having a gap
3482 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003483 // entrance of the new window to be properly animated.
Jorim Jaggi030979c2015-11-20 15:14:43 -08003484 mWindowManager.setReplacingWindow(topActivity.appToken, animate);
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003485 }
Wale Ogunwale3cd48042015-11-16 13:01:41 -08003486 final ActivityStack stack = moveTaskToStackUncheckedLocked(
3487 task, stackId, toTop, forceFocus, "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003488
Jorim Jaggi55387522015-11-24 18:21:10 -08003489 if (!animate) {
3490 stack.mNoAnimActivities.add(topActivity);
3491 }
3492
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003493 // Make sure the task has the appropriate bounds/size for the stack it is in.
3494 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003495 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003496 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3497 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003498 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3499 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003500 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Wale Ogunwale70c65c82015-11-13 13:25:16 -08003501 resizeTaskLocked(task, stack.mBounds, RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003502 }
3503
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003504 // The task might have already been running and its visibility needs to be synchronized with
3505 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003506 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003507 resumeTopActivitiesLocked();
Chong Zhangb15758a2015-11-17 12:12:03 -08003508
3509 if (!task.mResizeable && isStackDockedInEffect(stackId)) {
Chong Zhangc806d902015-11-30 09:44:27 -08003510 showNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08003511 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003512 }
3513
Wale Ogunwale079a0042015-10-24 11:44:07 -07003514 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3515 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3516 if (stack == null) {
3517 throw new IllegalArgumentException(
3518 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3519 }
3520
3521 final ActivityRecord r = stack.topRunningActivityLocked();
3522 if (r == null) {
3523 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3524 + " in stack=" + stack);
3525 return false;
3526 }
3527
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003528 if (!r.info.supportsPip) {
3529 Slog.w(TAG,
3530 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Filip Gruszczynski77d94482015-12-11 13:59:52 -08003531 + " r=" + r);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003532 return false;
3533 }
3534
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003535 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003536 if (task.mActivities.size() == 1) {
3537 // There is only one activity in the task. So, we can just move the task over to the
3538 // pinned stack without re-parenting the activity in a different task.
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003539 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS,
Jorim Jaggi030979c2015-11-20 15:14:43 -08003540 "moveTopActivityToPinnedStack", true /* animate */);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003541 } else {
3542 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3543 pinnedStack.moveActivityToStack(r);
3544 }
3545
Filip Gruszczynskia21136c2015-11-24 14:43:31 -08003546 resizeStackLocked(PINNED_STACK_ID, bounds, !PRESERVE_WINDOWS, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07003547
3548 // The task might have already been running and its visibility needs to be synchronized with
3549 // the visibility of the stack / windows.
3550 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3551 resumeTopActivitiesLocked();
3552 return true;
3553 }
3554
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003555 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3556 final TaskRecord task = anyTaskForIdLocked(taskId);
3557 if (task == null) {
3558 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3559 return;
3560 }
Wale Ogunwale935e5022015-11-10 12:36:10 -08003561 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
3562
3563 task.updateOverrideConfigurationForStack(stack);
3564
3565 mWindowManager.positionTaskInStack(
3566 taskId, stackId, position, task.mBounds, task.mOverrideConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003567 stack.positionTask(task, position);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003568 // The task might have already been running and its visibility needs to be synchronized with
3569 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003570 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003571 resumeTopActivitiesLocked();
3572 }
3573
Craig Mautnerac6f8432013-07-17 13:24:59 -07003574 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003575 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003576 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3577 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003578 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3579 final ActivityStack stack = stacks.get(stackNdx);
3580 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003581 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003582 continue;
3583 }
3584 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003585 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3586 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003587 continue;
3588 }
3589 final ActivityRecord ar = stack.findTaskLocked(r);
3590 if (ar != null) {
3591 return ar;
3592 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003593 }
3594 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003595 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003596 return null;
3597 }
3598
3599 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003600 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3601 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003602 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3603 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3604 if (ar != null) {
3605 return ar;
3606 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003607 }
3608 }
3609 return null;
3610 }
3611
Craig Mautner8d341ef2013-03-26 09:03:27 -07003612 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003613 scheduleSleepTimeout();
3614 if (!mGoingToSleep.isHeld()) {
3615 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003616 if (mLaunchingActivity.isHeld()) {
3617 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3618 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003619 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003620 mLaunchingActivity.release();
3621 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003622 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003623 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003624 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003625 }
3626
3627 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003628 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003629
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003630 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003631 final long endTime = System.currentTimeMillis() + timeout;
3632 while (true) {
3633 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003634 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3635 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003636 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3637 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3638 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003639 }
3640 if (cantShutdown) {
3641 long timeRemaining = endTime - System.currentTimeMillis();
3642 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003643 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003644 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003645 } catch (InterruptedException e) {
3646 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003647 } else {
3648 Slog.w(TAG, "Activity manager shutdown timed out");
3649 timedout = true;
3650 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003651 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003652 } else {
3653 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003654 }
3655 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003656
3657 // Force checkReadyForSleep to complete.
3658 mSleepTimeout = true;
3659 checkReadyForSleepLocked();
3660
Craig Mautner8d341ef2013-03-26 09:03:27 -07003661 return timedout;
3662 }
3663
3664 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003665 removeSleepTimeouts();
3666 if (mGoingToSleep.isHeld()) {
3667 mGoingToSleep.release();
3668 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003669 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3670 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003671 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3672 final ActivityStack stack = stacks.get(stackNdx);
3673 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003674 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003675 resumeTopActivitiesLocked();
3676 }
Craig Mautner5314a402013-09-26 12:40:16 -07003677 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003678 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003679 mGoingToSleepActivities.clear();
3680 }
3681
3682 void activitySleptLocked(ActivityRecord r) {
3683 mGoingToSleepActivities.remove(r);
3684 checkReadyForSleepLocked();
3685 }
3686
3687 void checkReadyForSleepLocked() {
3688 if (!mService.isSleepingOrShuttingDown()) {
3689 // Do not care.
3690 return;
3691 }
3692
3693 if (!mSleepTimeout) {
3694 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003695 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3696 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003697 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3698 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3699 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003700 }
3701
3702 if (mStoppingActivities.size() > 0) {
3703 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003704 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003705 + mStoppingActivities.size() + " activities");
3706 scheduleIdleLocked();
3707 dontSleep = true;
3708 }
3709
3710 if (mGoingToSleepActivities.size() > 0) {
3711 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003712 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003713 + mGoingToSleepActivities.size() + " activities");
3714 dontSleep = true;
3715 }
3716
3717 if (dontSleep) {
3718 return;
3719 }
3720 }
3721
Craig Mautnere0a38842013-12-16 16:14:02 -08003722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3723 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3725 stacks.get(stackNdx).goToSleep();
3726 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003727 }
3728
3729 removeSleepTimeouts();
3730
3731 if (mGoingToSleep.isHeld()) {
3732 mGoingToSleep.release();
3733 }
3734 if (mService.mShuttingDown) {
3735 mService.notifyAll();
3736 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003737 }
3738
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003739 boolean reportResumedActivityLocked(ActivityRecord r) {
3740 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003741 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003742 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003743 }
3744 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003745 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003746 mWindowManager.executeAppTransition();
3747 return true;
3748 }
3749 return false;
3750 }
3751
Craig Mautner8d341ef2013-03-26 09:03:27 -07003752 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3754 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003755 int stackNdx = stacks.size() - 1;
3756 while (stackNdx >= 0) {
3757 stacks.get(stackNdx).handleAppCrashLocked(app);
3758 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003759 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003760 }
3761 }
3762
Jose Lima4b6c6692014-08-12 17:41:12 -07003763 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003764 final ActivityStack stack = r.task.stack;
3765 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003766 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3767 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003768 return false;
3769 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003770 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003771 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3772 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003773
3774 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003775 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003776 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003777 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003778 return true;
3779 }
3780
3781 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003782 if (visible && top.fullscreen) {
3783 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003784 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3785 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3786 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3787 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003788 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003789 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3790 // Only the activity set as currently visible behind should actively reset its
3791 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003792 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3793 "requestVisibleBehind: returning visible=" + visible
3794 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3795 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003796 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003797 }
3798
Jose Lima4b6c6692014-08-12 17:41:12 -07003799 stack.setVisibleBehindActivity(visible ? r : null);
3800 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003801 // Make the activity immediately above r opaque.
3802 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3803 if (next != null) {
3804 mService.convertFromTranslucent(next.appToken);
3805 }
3806 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003807 if (top.app != null && top.app.thread != null) {
3808 // Notify the top app of the change.
3809 try {
3810 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3811 } catch (RemoteException e) {
3812 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003813 }
3814 return true;
3815 }
3816
Craig Mautnerbb742462014-07-07 15:28:55 -07003817 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3818 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3819 r.mLaunchTaskBehind = false;
3820 final TaskRecord task = r.task;
Winson8b1871d2015-11-20 09:56:20 -08003821 task.setLastThumbnailLocked(task.stack.screenshotActivitiesLocked(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003822 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003823 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003824 mWindowManager.setAppVisibility(r.appToken, false);
3825 }
3826
3827 void scheduleLaunchTaskBehindComplete(IBinder token) {
3828 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3829 }
3830
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003831 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3832 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003833 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003834 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3835 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003836 final int topStackNdx = stacks.size() - 1;
3837 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3838 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003839 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003840 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003841 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003842 }
3843
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003844 void invalidateTaskLayers() {
3845 mTaskLayersChanged = true;
3846 }
3847
3848 void rankTaskLayersIfNeeded() {
3849 if (!mTaskLayersChanged) {
3850 return;
3851 }
3852 mTaskLayersChanged = false;
3853 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3854 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3855 int baseLayer = 0;
3856 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3857 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3858 }
3859 }
3860 }
3861
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003862 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3863 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3864 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3865 final int topStackNdx = stacks.size() - 1;
3866 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3867 final ActivityStack stack = stacks.get(stackNdx);
3868 stack.clearOtherAppTimeTrackers(except);
3869 }
3870 }
3871 }
3872
Craig Mautner8d341ef2013-03-26 09:03:27 -07003873 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003874 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3875 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003876 final int numStacks = stacks.size();
3877 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3878 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003879 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003880 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003881 }
3882 }
3883
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003884 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3885 // Examine all activities currently running in the process.
3886 TaskRecord firstTask = null;
3887 // Tasks is non-null only if two or more tasks are found.
3888 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003889 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3890 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003891 ActivityRecord r = app.activities.get(i);
3892 // First, if we find an activity that is in the process of being destroyed,
3893 // then we just aren't going to do anything for now; we want things to settle
3894 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003895 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003896 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003897 return;
3898 }
3899 // Don't consider any activies that are currently not in a state where they
3900 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003901 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3902 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003903 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003904 continue;
3905 }
3906 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003907 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003908 + " from " + r);
3909 if (firstTask == null) {
3910 firstTask = r.task;
3911 } else if (firstTask != r.task) {
3912 if (tasks == null) {
3913 tasks = new ArraySet<>();
3914 tasks.add(firstTask);
3915 }
3916 tasks.add(r.task);
3917 }
3918 }
3919 }
3920 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003921 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003922 return;
3923 }
3924 // If we have activities in multiple tasks that are in a position to be destroyed,
3925 // let's iterate through the tasks and release the oldest one.
3926 final int numDisplays = mActivityDisplays.size();
3927 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3928 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3929 // Step through all stacks starting from behind, to hit the oldest things first.
3930 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3931 final ActivityStack stack = stacks.get(stackNdx);
3932 // Try to release activities in this stack; if we manage to, we are done.
3933 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3934 return;
3935 }
3936 }
3937 }
3938 }
3939
Amith Yamasani37a40c22015-06-17 13:25:42 -07003940 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003941 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003942 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003943 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003944
Craig Mautner858d8a62013-04-23 17:08:34 -07003945 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003946 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3947 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003948 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003949 final ActivityStack stack = stacks.get(stackNdx);
3950 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003951 TaskRecord task = stack.topTask();
3952 if (task != null) {
3953 mWindowManager.moveTaskToTop(task.taskId);
3954 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003955 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003956 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003957
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003958 ActivityStack stack = getStack(restoreStackId);
3959 if (stack == null) {
3960 stack = mHomeStack;
3961 }
3962 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003963 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003964 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003965 } else {
3966 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003967 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003968 }
Craig Mautner93529a42013-10-04 15:03:13 -07003969 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003970 }
3971
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003972 /**
3973 * Add background users to send boot completed events to.
3974 * @param userId The user being started in the background
3975 * @param uss The state object for the user.
3976 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003977 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003978 mStartingBackgroundUsers.add(uss);
3979 }
3980
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003981 /** Checks whether the userid is a profile of the current user. */
3982 boolean isCurrentProfileLocked(int userId) {
3983 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003984 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003985 }
3986
Chong Zhang45c25ce2015-08-10 22:18:26 -07003987 /** Checks whether the activity should be shown for current user. */
3988 boolean okToShowLocked(ActivityRecord r) {
3989 return r != null && (isCurrentProfileLocked(r.userId)
3990 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3991 }
3992
Craig Mautnerde4ef022013-04-07 19:01:33 -07003993 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003994 ArrayList<ActivityRecord> stops = null;
3995
3996 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003997 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3998 ActivityRecord s = mStoppingActivities.get(activityNdx);
3999 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004000 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08004001 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
4002 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07004003 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004004 if (s.finishing) {
4005 // If this activity is finishing, it is sitting on top of
4006 // everyone else but we now know it is no longer needed...
4007 // so get rid of it. Otherwise, we need to go through the
4008 // normal flow and hide it once we determine that it is
4009 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004010 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004011 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004012 }
4013 }
Craig Mautner8c14c152015-01-15 17:32:07 -08004014 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004015 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004016 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08004017 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07004018 }
4019 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08004020 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07004021 }
4022 }
4023
4024 return stops;
4025 }
4026
Craig Mautnercf910b02013-04-23 11:23:27 -07004027 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004028 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4029 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4030 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4031 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004032 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004033 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004034 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004035 if (r == null) Slog.e(TAG,
4036 "validateTop...: null top activity, stack=" + stack);
4037 else {
4038 final ActivityRecord pausing = stack.mPausingActivity;
4039 if (pausing != null && pausing == r) Slog.e(TAG,
4040 "validateTop...: top stack has pausing activity r=" + r
4041 + " state=" + state);
4042 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
4043 "validateTop...: activity in front not resumed r=" + r
4044 + " state=" + state);
4045 }
Craig Mautnercf910b02013-04-23 11:23:27 -07004046 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004047 final ActivityRecord resumed = stack.mResumedActivity;
4048 if (resumed != null && resumed == r) Slog.e(TAG,
4049 "validateTop...: back stack has resumed activity r=" + r
4050 + " state=" + state);
4051 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
4052 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07004053 }
4054 }
4055 }
Craig Mautner76ea2242013-05-15 11:40:05 -07004056 }
4057
Craig Mautnere0570202015-05-13 13:06:11 -07004058 private String lockTaskModeToString() {
4059 switch (mLockTaskModeState) {
4060 case LOCK_TASK_MODE_LOCKED:
4061 return "LOCKED";
4062 case LOCK_TASK_MODE_PINNED:
4063 return "PINNED";
4064 case LOCK_TASK_MODE_NONE:
4065 return "NONE";
4066 default: return "unknown=" + mLockTaskModeState;
4067 }
4068 }
4069
Craig Mautner27084302013-03-25 08:05:25 -07004070 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004071 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004072 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07004073 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
4074 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
4075 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08004076 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07004077 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
4078 final SparseArray<String[]> packages = mService.mLockTaskPackages;
4079 if (packages.size() > 0) {
4080 pw.println(" mLockTaskPackages (userId:packages)=");
4081 for (int i = 0; i < packages.size(); ++i) {
4082 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
4083 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
4084 }
4085 }
4086 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07004087 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004088
Craig Mautner20e72272013-04-01 13:45:53 -07004089 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004090 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07004091 }
4092
Dianne Hackborn390517b2013-05-30 15:03:32 -07004093 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
4094 boolean needSep, String prefix) {
4095 if (activity != null) {
4096 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
4097 if (needSep) {
4098 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07004099 }
4100 pw.print(prefix);
4101 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004102 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004103 }
4104 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004105 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004106 }
4107
Craig Mautner8d341ef2013-03-26 09:03:27 -07004108 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
4109 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004110 boolean printed = false;
4111 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08004112 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4113 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004114 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07004115 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08004116 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07004117 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004118 final ActivityStack stack = stacks.get(stackNdx);
4119 StringBuilder stackHeader = new StringBuilder(128);
4120 stackHeader.append(" Stack #");
4121 stackHeader.append(stack.mStackId);
4122 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004123 stackHeader.append("\n");
4124 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
4125 stackHeader.append("\n");
4126 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004127 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
4128 needSep, stackHeader.toString());
4129 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
4130 !dumpAll, false, dumpPackage, true,
4131 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07004132
Craig Mautner4a1cb222013-12-04 16:14:06 -08004133 needSep = printed;
4134 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
4135 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004136 if (pr) {
4137 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004138 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004139 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
4141 " mResumedActivity: ");
4142 if (pr) {
4143 printed = true;
4144 needSep = false;
4145 }
4146 if (dumpAll) {
4147 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
4148 " mLastPausedActivity: ");
4149 if (pr) {
4150 printed = true;
4151 needSep = true;
4152 }
4153 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
4154 needSep, " mLastNoHistoryActivity: ");
4155 }
4156 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004157 }
4158 }
4159
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004160 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
4161 false, dumpPackage, true, " Activities waiting to finish:", null);
4162 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
4163 false, dumpPackage, true, " Activities waiting to stop:", null);
4164 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
4165 false, dumpPackage, true, " Activities waiting for another to become visible:",
4166 null);
4167 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4168 false, dumpPackage, true, " Activities waiting to sleep:", null);
4169 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
4170 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07004171
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004172 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004173 }
4174
Dianne Hackborn390517b2013-05-30 15:03:32 -07004175 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07004176 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004177 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07004178 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004179 String innerPrefix = null;
4180 String[] args = null;
4181 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004182 for (int i=list.size()-1; i>=0; i--) {
4183 final ActivityRecord r = list.get(i);
4184 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
4185 continue;
4186 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004187 if (innerPrefix == null) {
4188 innerPrefix = prefix + " ";
4189 args = new String[0];
4190 }
4191 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004192 final boolean full = !brief && (complete || !r.isInHistory());
4193 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004194 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004195 needNL = false;
4196 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004197 if (header1 != null) {
4198 pw.println(header1);
4199 header1 = null;
4200 }
4201 if (header2 != null) {
4202 pw.println(header2);
4203 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004204 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004205 if (lastTask != r.task) {
4206 lastTask = r.task;
4207 pw.print(prefix);
4208 pw.print(full ? "* " : " ");
4209 pw.println(lastTask);
4210 if (full) {
4211 lastTask.dump(pw, prefix + " ");
4212 } else if (complete) {
4213 // Complete + brief == give a summary. Isn't that obvious?!?
4214 if (lastTask.intent != null) {
4215 pw.print(prefix); pw.print(" ");
4216 pw.println(lastTask.intent.toInsecureStringWithClip());
4217 }
4218 }
4219 }
4220 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4221 pw.print(" #"); pw.print(i); pw.print(": ");
4222 pw.println(r);
4223 if (full) {
4224 r.dump(pw, innerPrefix);
4225 } else if (complete) {
4226 // Complete + brief == give a summary. Isn't that obvious?!?
4227 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4228 if (r.app != null) {
4229 pw.print(innerPrefix); pw.println(r.app);
4230 }
4231 }
4232 if (client && r.app != null && r.app.thread != null) {
4233 // flush anything that is already in the PrintWriter since the thread is going
4234 // to write to the file descriptor directly
4235 pw.flush();
4236 try {
4237 TransferPipe tp = new TransferPipe();
4238 try {
4239 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4240 r.appToken, innerPrefix, args);
4241 // Short timeout, since blocking here can
4242 // deadlock with the application.
4243 tp.go(fd, 2000);
4244 } finally {
4245 tp.kill();
4246 }
4247 } catch (IOException e) {
4248 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4249 } catch (RemoteException e) {
4250 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4251 }
4252 needNL = true;
4253 }
4254 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004255 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004256 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004257
Craig Mautnerf3333272013-04-22 10:55:53 -07004258 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004259 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4260 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004261 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4262 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004263 }
4264
4265 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004266 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004267 }
4268
4269 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004270 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4271 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004272 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4273 }
4274
Craig Mautner05d29032013-05-03 13:40:13 -07004275 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004276 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4277 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4278 }
Craig Mautner05d29032013-05-03 13:40:13 -07004279 }
4280
Craig Mautner0eea92c2013-05-16 13:35:39 -07004281 void removeSleepTimeouts() {
4282 mSleepTimeout = false;
4283 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4284 }
4285
4286 final void scheduleSleepTimeout() {
4287 removeSleepTimeouts();
4288 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4289 }
4290
Craig Mautner4a1cb222013-12-04 16:14:06 -08004291 @Override
4292 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004293 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004294 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4295 }
4296
4297 @Override
4298 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004299 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004300 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4301 }
4302
4303 @Override
4304 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004305 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004306 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4307 }
4308
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004309 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004310 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004311 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004312 newDisplay = mActivityDisplays.get(displayId) == null;
4313 if (newDisplay) {
4314 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004315 if (activityDisplay.mDisplay == null) {
4316 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4317 return;
4318 }
Craig Mautner4504de52013-12-20 09:06:56 -08004319 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004320 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004321 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004322 }
Craig Mautner4504de52013-12-20 09:06:56 -08004323 if (newDisplay) {
4324 mWindowManager.onDisplayAdded(displayId);
4325 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004326 }
4327
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004328 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4329 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4330 return;
4331 }
4332 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4333 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4334 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4335 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4336 }
4337
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004338 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004339 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004340 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4341 if (activityDisplay != null) {
4342 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004343 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004344 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004345 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004346 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004347 }
4348 }
4349 mWindowManager.onDisplayRemoved(displayId);
4350 }
4351
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004352 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004353 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004354 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4355 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004356 // TODO: Update the bounds.
4357 }
4358 }
4359 mWindowManager.onDisplayChanged(displayId);
4360 }
4361
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004362 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004363 StackInfo info = new StackInfo();
4364 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4365 info.displayId = Display.DEFAULT_DISPLAY;
4366 info.stackId = stack.mStackId;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004367 info.userId = stack.mCurrentUser;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004368
4369 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4370 final int numTasks = tasks.size();
4371 int[] taskIds = new int[numTasks];
4372 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004373 Rect[] taskBounds = new Rect[numTasks];
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004374 int[] taskUserIds = new int[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004375 for (int i = 0; i < numTasks; ++i) {
4376 final TaskRecord task = tasks.get(i);
4377 taskIds[i] = task.taskId;
4378 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4379 : task.realActivity != null ? task.realActivity.flattenToString()
4380 : task.getTopActivity() != null ? task.getTopActivity().packageName
4381 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004382 taskBounds[i] = new Rect();
4383 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004384 taskUserIds[i] = task.userId;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004385 }
4386 info.taskIds = taskIds;
4387 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004388 info.taskBounds = taskBounds;
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08004389 info.taskUserIds = taskUserIds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004390 return info;
4391 }
4392
4393 StackInfo getStackInfoLocked(int stackId) {
4394 ActivityStack stack = getStack(stackId);
4395 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004396 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004397 }
4398 return null;
4399 }
4400
4401 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004402 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004403 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4404 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004405 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004406 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004407 }
4408 }
4409 return list;
4410 }
4411
Craig Mautner15df08a2015-04-01 12:17:18 -07004412 TaskRecord getLockedTaskLocked() {
4413 final int top = mLockTaskModeTasks.size() - 1;
4414 if (top >= 0) {
4415 return mLockTaskModeTasks.get(top);
4416 }
4417 return null;
4418 }
4419
4420 boolean isLockedTask(TaskRecord task) {
4421 return mLockTaskModeTasks.contains(task);
4422 }
4423
4424 boolean isLastLockedTask(TaskRecord task) {
4425 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4426 }
4427
4428 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004429 if (!mLockTaskModeTasks.remove(task)) {
4430 return;
4431 }
4432 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4433 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004434 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004435 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4436 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004437 final Message lockTaskMsg = Message.obtain();
4438 lockTaskMsg.arg1 = task.userId;
4439 lockTaskMsg.what = LOCK_TASK_END_MSG;
4440 mHandler.sendMessage(lockTaskMsg);
4441 }
4442 }
4443
Chong Zhangc806d902015-11-30 09:44:27 -08004444 private void showNonResizeableDockToast(int taskId) {
4445 mWindowManager.scheduleShowNonResizeableDockToast(taskId);
Chong Zhangb15758a2015-11-17 12:12:03 -08004446 }
4447
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004448 void showLockTaskToast() {
4449 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004450 }
4451
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004452 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4453 if (mLockTaskModeTasks.contains(task)) {
4454 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4455 }
4456 }
4457
Craig Mautner432f64e2015-05-20 14:59:57 -07004458 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4459 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004460 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004461 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004462 final TaskRecord lockedTask = getLockedTaskLocked();
4463 if (lockedTask != null) {
4464 removeLockedTaskLocked(lockedTask);
4465 if (!mLockTaskModeTasks.isEmpty()) {
4466 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004467 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4468 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004469 lockedTask.performClearTaskLocked();
4470 resumeTopActivitiesLocked();
4471 return;
4472 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004473 }
Craig Mautnere0570202015-05-13 13:06:11 -07004474 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4475 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004476 return;
4477 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004478
4479 // Should have already been checked, but do it again.
4480 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004481 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4482 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004483 return;
4484 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004485 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004486 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004487 return;
4488 }
4489
4490 if (mLockTaskModeTasks.isEmpty()) {
4491 // First locktask.
4492 final Message lockTaskMsg = Message.obtain();
4493 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4494 lockTaskMsg.arg1 = task.userId;
4495 lockTaskMsg.what = LOCK_TASK_START_MSG;
4496 lockTaskMsg.arg2 = lockTaskModeState;
4497 mHandler.sendMessage(lockTaskMsg);
4498 }
4499 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004500 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4501 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004502 mLockTaskModeTasks.remove(task);
4503 mLockTaskModeTasks.add(task);
4504
4505 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004506 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004507 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004508
4509 if (andResume) {
4510 findTaskToMoveToFrontLocked(task, 0, null, reason);
4511 resumeTopActivitiesLocked();
4512 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004513 }
4514
4515 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004516 return isLockTaskModeViolation(task, false);
4517 }
4518
4519 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4520 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004521 return false;
4522 }
4523 final int lockTaskAuth = task.mLockTaskAuth;
4524 switch (lockTaskAuth) {
4525 case LOCK_TASK_AUTH_DONT_LOCK:
4526 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004527 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004528 case LOCK_TASK_AUTH_LAUNCHABLE:
4529 case LOCK_TASK_AUTH_WHITELISTED:
4530 return false;
4531 case LOCK_TASK_AUTH_PINNABLE:
4532 // Pinnable tasks can't be launched on top of locktask tasks.
4533 return !mLockTaskModeTasks.isEmpty();
4534 default:
4535 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4536 return true;
4537 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004538 }
4539
Craig Mautner15df08a2015-04-01 12:17:18 -07004540 void onLockTaskPackagesUpdatedLocked() {
4541 boolean didSomething = false;
4542 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4543 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004544 final boolean wasWhitelisted =
4545 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4546 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004547 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004548 final boolean isWhitelisted =
4549 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4550 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4551 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004552 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004553 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4554 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004555 removeLockedTaskLocked(lockedTask);
4556 lockedTask.performClearTaskLocked();
4557 didSomething = true;
4558 }
4559 }
4560 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4561 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4562 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4563 final ActivityStack stack = stacks.get(stackNdx);
4564 stack.onLockTaskPackagesUpdatedLocked();
4565 }
4566 }
Craig Mautnere0570202015-05-13 13:06:11 -07004567 final ActivityRecord r = topRunningActivityLocked();
4568 final TaskRecord task = r != null ? r.task : null;
4569 if (mLockTaskModeTasks.isEmpty() && task != null
4570 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4571 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004572 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4573 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4574 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4575 false);
4576 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004577 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004578 if (didSomething) {
4579 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004580 }
4581 }
4582
Benjamin Franz43261142015-02-11 15:59:44 +00004583 int getLockTaskModeState() {
4584 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004585 }
4586
Filip Gruszczynski77d94482015-12-11 13:59:52 -08004587 void logStackState() {
4588 mActivityMetricsLogger.logWindowState();
4589 }
4590
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004591 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004592
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004593 public ActivityStackSupervisorHandler(Looper looper) {
4594 super(looper);
4595 }
4596
Craig Mautnerf3333272013-04-22 10:55:53 -07004597 void activityIdleInternal(ActivityRecord r) {
4598 synchronized (mService) {
4599 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4600 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004601 }
4602
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004603 @Override
4604 public void handleMessage(Message msg) {
4605 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004606 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004607 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4608 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004609 if (mService.mDidDexOpt) {
4610 mService.mDidDexOpt = false;
4611 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4612 nmsg.obj = msg.obj;
4613 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4614 return;
4615 }
4616 // We don't at this point know if the activity is fullscreen,
4617 // so we need to be conservative and assume it isn't.
4618 activityIdleInternal((ActivityRecord)msg.obj);
4619 } break;
4620 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004621 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004622 activityIdleInternal((ActivityRecord)msg.obj);
4623 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004624 case RESUME_TOP_ACTIVITY_MSG: {
4625 synchronized (mService) {
4626 resumeTopActivitiesLocked();
4627 }
4628 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004629 case SLEEP_TIMEOUT_MSG: {
4630 synchronized (mService) {
4631 if (mService.isSleepingOrShuttingDown()) {
4632 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4633 mSleepTimeout = true;
4634 checkReadyForSleepLocked();
4635 }
4636 }
4637 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004638 case LAUNCH_TIMEOUT_MSG: {
4639 if (mService.mDidDexOpt) {
4640 mService.mDidDexOpt = false;
4641 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4642 return;
4643 }
4644 synchronized (mService) {
4645 if (mLaunchingActivity.isHeld()) {
4646 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4647 if (VALIDATE_WAKE_LOCK_CALLER
4648 && Binder.getCallingUid() != Process.myUid()) {
4649 throw new IllegalStateException("Calling must be system uid");
4650 }
4651 mLaunchingActivity.release();
4652 }
4653 }
4654 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004655 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004656 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004657 } break;
4658 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004659 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004660 } break;
4661 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004662 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004663 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004664 case CONTAINER_CALLBACK_VISIBILITY: {
4665 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004666 final IActivityContainerCallback callback = container.mCallback;
4667 if (callback != null) {
4668 try {
4669 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4670 } catch (RemoteException e) {
4671 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004672 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004673 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004674 case LOCK_TASK_START_MSG: {
4675 // When lock task starts, we disable the status bars.
4676 try {
Jason Monk62515be2014-05-21 16:06:19 -04004677 if (mLockTaskNotify == null) {
4678 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004679 }
Jason Monk62515be2014-05-21 16:06:19 -04004680 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004681 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004682 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004683 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004684 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004685 flags = StatusBarManager.DISABLE_MASK
4686 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004687 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004688 flags = StatusBarManager.DISABLE_MASK
4689 & (~StatusBarManager.DISABLE_BACK)
4690 & (~StatusBarManager.DISABLE_HOME)
4691 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004692 }
4693 getStatusBarService().disable(flags, mToken,
4694 mService.mContext.getPackageName());
4695 }
4696 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004697 if (getDevicePolicyManager() != null) {
4698 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004699 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004700 }
justinzhang5286d3f2014-05-12 17:06:01 -04004701 } catch (RemoteException ex) {
4702 throw new RuntimeException(ex);
4703 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004704 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004705 case LOCK_TASK_END_MSG: {
4706 // When lock task ends, we enable the status bars.
4707 try {
Jason Monk62515be2014-05-21 16:06:19 -04004708 if (getStatusBarService() != null) {
4709 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4710 mService.mContext.getPackageName());
4711 }
4712 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004713 if (getDevicePolicyManager() != null) {
4714 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4715 msg.arg1);
4716 }
Jason Monk62515be2014-05-21 16:06:19 -04004717 if (mLockTaskNotify == null) {
4718 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4719 }
4720 mLockTaskNotify.show(false);
4721 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004722 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004723 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004724 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004725 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004726 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004727 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004728 new LockPatternUtils(mService.mContext)
4729 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004730 }
4731 } catch (SettingNotFoundException e) {
4732 // No setting, don't lock.
4733 }
justinzhang5286d3f2014-05-12 17:06:01 -04004734 } catch (RemoteException ex) {
4735 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004736 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004737 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004738 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004739 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004740 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4741 if (mLockTaskNotify == null) {
4742 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4743 }
4744 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4745 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004746 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4747 final ActivityContainer container = (ActivityContainer) msg.obj;
4748 final IActivityContainerCallback callback = container.mCallback;
4749 if (callback != null) {
4750 try {
4751 callback.onAllActivitiesComplete(container.asBinder());
4752 } catch (RemoteException e) {
4753 }
4754 }
4755 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004756 case LAUNCH_TASK_BEHIND_COMPLETE: {
4757 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004758 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004759 if (r != null) {
4760 handleLaunchTaskBehindCompleteLocked(r);
4761 }
4762 }
4763 } break;
Chong Zhangc806d902015-11-30 09:44:27 -08004764
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004765 }
4766 }
4767 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004768
Ying Wangb081a592014-04-22 15:20:16 -07004769 class ActivityContainer extends android.app.IActivityContainer.Stub {
Filip Gruszczynskidc394902015-12-14 10:20:22 -08004770 final static int FORCE_NEW_TASK_FLAGS = FLAG_ACTIVITY_NEW_TASK |
4771 FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004772 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004773 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004774 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004775 ActivityRecord mParentActivity = null;
4776 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004777
Craig Mautnere3a00d72014-04-16 08:31:19 -07004778 boolean mVisible = true;
4779
Craig Mautner4a1cb222013-12-04 16:14:06 -08004780 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004781 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004782
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004783 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4784 final static int CONTAINER_STATE_NO_SURFACE = 1;
4785 final static int CONTAINER_STATE_FINISHING = 2;
4786 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4787
4788 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004789 synchronized (mService) {
4790 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004791 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004792 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004793 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004794 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004795 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004796
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004797 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004798 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004799 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004800 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004801 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004802 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004803 }
4804
4805 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004806 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004807 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004808 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4809 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004810 return;
4811 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004812 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004813 }
4814 }
4815
4816 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004817 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004818 synchronized (mService) {
4819 if (mActivityDisplay != null) {
4820 return mActivityDisplay.mDisplayId;
4821 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004822 }
4823 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004824 }
4825
Jeff Brownca9bc702014-02-11 14:32:56 -08004826 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004827 public int getStackId() {
4828 synchronized (mService) {
4829 return mStackId;
4830 }
4831 }
4832
4833 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004834 public boolean injectEvent(InputEvent event) {
4835 final long origId = Binder.clearCallingIdentity();
4836 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004837 synchronized (mService) {
4838 if (mActivityDisplay != null) {
4839 return mInputManagerInternal.injectInputEvent(event,
4840 mActivityDisplay.mDisplayId,
4841 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4842 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004843 }
4844 return false;
4845 } finally {
4846 Binder.restoreCallingIdentity(origId);
4847 }
4848 }
4849
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004850 @Override
4851 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004852 synchronized (mService) {
4853 if (mContainerState == CONTAINER_STATE_FINISHING) {
4854 return;
4855 }
4856 mContainerState = CONTAINER_STATE_FINISHING;
4857
Craig Mautnerd163e752014-06-13 17:18:47 -07004858 long origId = Binder.clearCallingIdentity();
4859 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004860 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004861 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004862 } finally {
4863 Binder.restoreCallingIdentity(origId);
4864 }
4865 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004866 }
4867
Craig Mautner60257702014-09-17 15:02:33 -07004868 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004869 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004870 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004871 if (mActivityDisplay != null) {
4872 mActivityDisplay.detachActivitiesLocked(mStack);
4873 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004874 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004875 }
4876 }
4877
4878 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004879 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004880 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004881 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004882 Binder.getCallingUid(), mCurrentUser, false,
4883 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004884
Craig Mautnere0a38842013-12-16 16:14:02 -08004885 // TODO: Switch to user app stacks here.
4886 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004887 final Uri data = intent.getData();
4888 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4889 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004890 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004891 checkEmbeddedAllowedInner(userId, intent, mimeType);
4892
4893 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4894 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004895 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004896 }
4897
4898 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004899 public final int startActivityIntentSender(IIntentSender intentSender)
4900 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004901 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4902
4903 if (!(intentSender instanceof PendingIntentRecord)) {
4904 throw new IllegalArgumentException("Bad PendingIntent object");
4905 }
4906
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004907 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004908 Binder.getCallingUid(), mCurrentUser, false,
4909 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004910
4911 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4912 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4913 pendingIntent.key.requestResolvedType);
4914
4915 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4916 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4917 }
4918
4919 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004920 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004921 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004922 throw new SecurityException(
4923 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4924 }
4925 }
4926
Craig Mautnerdf88d732014-01-27 09:21:32 -08004927 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004928 public IBinder asBinder() {
4929 return this;
4930 }
4931
Craig Mautner4504de52013-12-20 09:06:56 -08004932 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004933 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004934 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004935 }
4936
Craig Mautner4a1cb222013-12-04 16:14:06 -08004937 ActivityStackSupervisor getOuter() {
4938 return ActivityStackSupervisor.this;
4939 }
4940
Craig Mautnerd163e752014-06-13 17:18:47 -07004941 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004942 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004943 }
4944
Craig Mautner6985bad2014-04-21 15:22:06 -07004945 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004946 void setVisible(boolean visible) {
4947 if (mVisible != visible) {
4948 mVisible = visible;
4949 if (mCallback != null) {
4950 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4951 0 /* unused */, this).sendToTarget();
4952 }
4953 }
4954 }
4955
Craig Mautner6985bad2014-04-21 15:22:06 -07004956 void setDrawn() {
4957 }
4958
Craig Mautner1b4bf852014-05-26 15:06:32 -07004959 // You can always start a new task on a regular ActivityStack.
4960 boolean isEligibleForNewTasks() {
4961 return true;
4962 }
4963
Craig Mautnerd163e752014-06-13 17:18:47 -07004964 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004965 detachLocked();
4966 deleteActivityContainer(this);
4967 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004968 }
4969
Craig Mautner34b73df2014-01-12 21:11:08 -08004970 @Override
4971 public String toString() {
4972 return mIdString + (mActivityDisplay == null ? "N" : "A");
4973 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004974 }
4975
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004976 private class VirtualActivityContainer extends ActivityContainer {
4977 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004978 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004979
4980 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4981 super(getNextStackId());
4982 mParentActivity = parent;
4983 mCallback = callback;
4984 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004985 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004986 }
4987
4988 @Override
4989 public void setSurface(Surface surface, int width, int height, int density) {
4990 super.setSurface(surface, width, height, density);
4991
4992 synchronized (mService) {
4993 final long origId = Binder.clearCallingIdentity();
4994 try {
4995 setSurfaceLocked(surface, width, height, density);
4996 } finally {
4997 Binder.restoreCallingIdentity(origId);
4998 }
4999 }
5000 }
5001
5002 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
5003 if (mContainerState == CONTAINER_STATE_FINISHING) {
5004 return;
5005 }
5006 VirtualActivityDisplay virtualActivityDisplay =
5007 (VirtualActivityDisplay) mActivityDisplay;
5008 if (virtualActivityDisplay == null) {
5009 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07005010 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005011 mActivityDisplay = virtualActivityDisplay;
5012 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005013 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005014 }
5015
5016 if (mSurface != null) {
5017 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005018 }
5019
Craig Mautner6985bad2014-04-21 15:22:06 -07005020 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005021 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005022 mStack.resumeTopActivityLocked(null);
5023 } else {
5024 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07005025 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07005026 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07005027 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07005028 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005029 }
Craig Mautner6985bad2014-04-21 15:22:06 -07005030
Craig Mautnerd163e752014-06-13 17:18:47 -07005031 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07005032
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005033 if (DEBUG_STACK) Slog.d(TAG_STACK,
5034 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07005035 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005036
Craig Mautner6985bad2014-04-21 15:22:06 -07005037 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07005038 boolean isAttachedLocked() {
5039 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07005040 }
5041
5042 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07005043 void setDrawn() {
5044 synchronized (mService) {
5045 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07005046 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07005047 }
5048 }
5049
Craig Mautner1b4bf852014-05-26 15:06:32 -07005050 // Never start a new task on an ActivityView if it isn't explicitly specified.
5051 @Override
5052 boolean isEligibleForNewTasks() {
5053 return false;
5054 }
5055
Craig Mautnerd163e752014-06-13 17:18:47 -07005056 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005057 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07005058 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
5059 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
5060 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
5061 mContainerState = CONTAINER_STATE_HAS_SURFACE;
5062 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005063 }
5064 }
5065
Craig Mautner4a1cb222013-12-04 16:14:06 -08005066 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
5067 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005068 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08005069 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08005070 int mDisplayId;
5071 Display mDisplay;
5072 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08005073
Craig Mautner4a1cb222013-12-04 16:14:06 -08005074 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
5075 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08005076 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08005077
Jose Lima4b6c6692014-08-12 17:41:12 -07005078 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005079
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005080 ActivityDisplay() {
5081 }
Craig Mautner4504de52013-12-20 09:06:56 -08005082
Craig Mautner1a70a162014-09-13 12:09:31 -07005083 // After instantiation, check that mDisplay is not null before using this. The alternative
5084 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08005085 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07005086 final Display display = mDisplayManager.getDisplay(displayId);
5087 if (display == null) {
5088 return;
5089 }
5090 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08005091 }
5092
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005093 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08005094 mDisplay = display;
5095 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08005096 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08005097 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08005098
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005099 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005100 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07005101 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
5102 + " onTop=" + onTop);
5103 if (onTop) {
5104 mStacks.add(stack);
5105 } else {
5106 mStacks.add(0, stack);
5107 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08005108 }
5109
Craig Mautnere0a38842013-12-16 16:14:02 -08005110 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07005111 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08005112 + " from displayId=" + mDisplayId);
5113 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08005114 }
5115
Jose Lima4b6c6692014-08-12 17:41:12 -07005116 void setVisibleBehindActivity(ActivityRecord r) {
5117 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005118 }
5119
Jose Lima4b6c6692014-08-12 17:41:12 -07005120 boolean hasVisibleBehindActivity() {
5121 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07005122 }
5123
Craig Mautner34b73df2014-01-12 21:11:08 -08005124 @Override
5125 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005126 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
5127 }
5128 }
5129
5130 class VirtualActivityDisplay extends ActivityDisplay {
5131 VirtualDisplay mVirtualDisplay;
5132
Craig Mautner6985bad2014-04-21 15:22:06 -07005133 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005134 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07005135 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
5136 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
5137 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
5138 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07005139
5140 init(mVirtualDisplay.getDisplay());
5141
5142 mWindowManager.handleDisplayAdded(mDisplayId);
5143 }
5144
5145 void setSurface(Surface surface) {
5146 if (mVirtualDisplay != null) {
5147 mVirtualDisplay.setSurface(surface);
5148 }
5149 }
5150
5151 @Override
5152 void detachActivitiesLocked(ActivityStack stack) {
5153 super.detachActivitiesLocked(stack);
5154 if (mVirtualDisplay != null) {
5155 mVirtualDisplay.release();
5156 mVirtualDisplay = null;
5157 }
5158 }
5159
5160 @Override
5161 public String toString() {
5162 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08005163 }
Craig Mautnered6649f2013-12-02 14:08:25 -08005164 }
Jose Lima58e66d62014-05-27 20:00:27 -07005165
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07005166 /**
5167 * Adjust bounds to stay within stack bounds.
5168 *
5169 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
5170 * that keep them unchanged, but be contained within the stack bounds.
5171 *
5172 * @param bounds Bounds to be adjusted.
5173 * @param stackBounds Bounds within which the other bounds should remain.
5174 */
5175 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
5176 if (stackBounds == null || stackBounds.contains(bounds)) {
5177 return;
5178 }
5179
5180 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
5181 final int maxRight = stackBounds.right
5182 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
5183 int horizontalDiff = stackBounds.left - bounds.left;
5184 if ((horizontalDiff < 0 && bounds.left >= maxRight)
5185 || (bounds.left + horizontalDiff >= maxRight)) {
5186 horizontalDiff = maxRight - bounds.left;
5187 }
5188 bounds.left += horizontalDiff;
5189 bounds.right += horizontalDiff;
5190 }
5191
5192 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
5193 final int maxBottom = stackBounds.bottom
5194 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
5195 int verticalDiff = stackBounds.top - bounds.top;
5196 if ((verticalDiff < 0 && bounds.top >= maxBottom)
5197 || (bounds.top + verticalDiff >= maxBottom)) {
5198 verticalDiff = maxBottom - bounds.top;
5199 }
5200 bounds.top += verticalDiff;
5201 bounds.bottom += verticalDiff;
5202 }
5203 }
5204
Craig Mautner27084302013-03-25 08:05:25 -07005205}