blob: 8bd9462a290d7cbce7822966fb16f3302cf96918 [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 Ogunwaled80c2632015-03-13 10:26:26 -070021import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
22import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070023import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070026import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070027import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080028import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070029import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070030import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
31import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
32import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070033import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070034import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
35import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070037import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
38import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070039
Svetoslav7008b512015-06-24 18:47:07 -070040import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070041import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080043import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.ActivityOptions;
45import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070046import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080047import android.app.IActivityContainer;
48import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070049import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070052import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070053import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070055import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040056import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040057import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070061import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070063import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070064import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070065import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070066import android.content.pm.PackageManager;
67import android.content.pm.ResolveInfo;
68import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080069import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.hardware.display.DisplayManager;
71import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.hardware.display.DisplayManagerGlobal;
73import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080074import android.hardware.input.InputManager;
75import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080076import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070079import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070080import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070085import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070086import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070087import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040088import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070090import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070091import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070092import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070093import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070094import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040095import android.provider.Settings;
96import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070097import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070098import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070099import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700100import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700101import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103
Craig Mautner4a1cb222013-12-04 16:14:06 -0800104import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800105import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800107import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800108import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700110import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800111import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700112import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400113import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700114import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400115import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800116import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700118import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119
justinzhang5286d3f2014-05-12 17:06:01 -0400120
Craig Mautner8d341ef2013-03-26 09:03:27 -0700121import java.io.FileDescriptor;
122import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700123import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700124import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700125import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700126import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700127import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700128
Craig Mautner4a1cb222013-12-04 16:14:06 -0800129public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800130 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700131 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700132 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700133 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700134 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700135 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700137 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700138 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
139 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700140 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700141 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700142 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
143 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700144 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700145 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800146
Craig Mautnerf3333272013-04-22 10:55:53 -0700147 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700149
Craig Mautner0eea92c2013-05-16 13:35:39 -0700150 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700152
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700153 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700155
Craig Mautner05d29032013-05-03 13:40:13 -0700156 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
157 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
158 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700159 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700160 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
162 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
163 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700164 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400165 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
166 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700167 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700168 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700169 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800170
Wale Ogunwale040b4702015-08-06 18:10:50 -0700171 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700172
Jason Monk62515be2014-05-21 16:06:19 -0400173 private static final String LOCK_TASK_TAG = "Lock-to-App";
174
Wale Ogunwale040b4702015-08-06 18:10:50 -0700175 // Used to indicate if an object (e.g. stack) that we are trying to get
176 // should be created if it doesn't exist already.
177 private static final boolean CREATE_IF_NEEDED = true;
178
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700179 // Used to indicate that windows of activities should be preserved during the resize.
180 static final boolean PRESERVE_WINDOWS = true;
181
Wale Ogunwale040b4702015-08-06 18:10:50 -0700182 // Used to indicate if an object (e.g. task) should be moved/created
183 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700184 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700185
186 // Used to indicate that an objects (e.g. task) removal from its container
187 // (e.g. stack) is due to it moving to another container.
188 static final boolean MOVING = true;
189
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700190 // Force the focus to change to the stack we are moving a task to..
191 static final boolean FORCE_FOCUS = true;
192
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700193 // Restore task from the saved recents if it can't be found in any live stack.
194 static final boolean RESTORE_FROM_RECENTS = true;
195
Svetoslav7008b512015-06-24 18:47:07 -0700196 // Activity actions an app cannot start if it uses a permission which is not granted.
197 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
198 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700199
Svetoslav7008b512015-06-24 18:47:07 -0700200 static {
201 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
202 Manifest.permission.CAMERA);
203 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
204 Manifest.permission.CAMERA);
205 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
206 Manifest.permission.CALL_PHONE);
207 }
208
Svet Ganov99b60432015-06-27 13:15:22 -0700209 /** Action restriction: launching the activity is not restricted. */
210 private static final int ACTIVITY_RESTRICTION_NONE = 0;
211 /** Action restriction: launching the activity is restricted by a permission. */
212 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
213 /** Action restriction: launching the activity is restricted by an app op. */
214 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700215
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700216 // The height/width divide used when fitting a task within a bounds with method
217 // {@link #fitWithinBounds}.
218 // We always want the task to to be visible in the bounds without affecting its size when
219 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
220 // the input bounds right or bottom side minus the width or height divided by this value.
221 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
222
justinzhang5286d3f2014-05-12 17:06:01 -0400223 /** Status Bar Service **/
224 private IBinder mToken = new Binder();
225 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400226 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400227
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700228 // For debugging to make sure the caller when acquiring/releasing our
229 // wake lock is the system process.
230 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700231
Craig Mautner27084302013-03-25 08:05:25 -0700232 final ActivityManagerService mService;
233
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800234 private final RecentTasks mRecentTasks;
235
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700236 final ActivityStackSupervisorHandler mHandler;
237
238 /** Short cut */
239 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700241
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700242 /** Counter for next free stack ID to use for dynamic activity stacks. */
243 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700244
245 /** Task identifier that activities are currently being started in. Incremented each time a
246 * new task is created. */
247 private int mCurTaskId = 0;
248
Craig Mautner2420ead2013-04-01 17:13:20 -0700249 /** The current user */
250 private int mCurrentUser;
251
Craig Mautnere0a38842013-12-16 16:14:02 -0800252 /** The stack containing the launcher app. Assumed to always be attached to
253 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700254 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700255
Craig Mautnere0a38842013-12-16 16:14:02 -0800256 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700257 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700258
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
260 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800261 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800262 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700263
264 /** List of activities that are waiting for a new activity to become visible before completing
265 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800266 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700267
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700268 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800269 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700270
271 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800272 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700273
Craig Mautnerde4ef022013-04-07 19:01:33 -0700274 /** List of activities that are ready to be stopped, but waiting for the next activity to
275 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800276 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700277
Craig Mautnerf3333272013-04-22 10:55:53 -0700278 /** List of activities that are ready to be finished, but waiting for the previous activity to
279 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800280 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700281
Craig Mautner0eea92c2013-05-16 13:35:39 -0700282 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800283 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700284
Craig Mautnerf3333272013-04-22 10:55:53 -0700285 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700286 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700287
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700288 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700289 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700290
Craig Mautnerde4ef022013-04-07 19:01:33 -0700291 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
292 * is being brought in front of us. */
293 boolean mUserLeaving = false;
294
Craig Mautner0eea92c2013-05-16 13:35:39 -0700295 /** Set when we have taken too long waiting to go to sleep. */
296 boolean mSleepTimeout = false;
297
Jose Lima58e66d62014-05-27 20:00:27 -0700298 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
299 * setWindowManager is called. **/
300 private boolean mLeanbackOnlyDevice;
301
Craig Mautner0eea92c2013-05-16 13:35:39 -0700302 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700303 * We don't want to allow the device to go to sleep while in the process
304 * of launching an activity. This is primarily to allow alarm intent
305 * receivers to launch an activity and get that to run before the device
306 * goes back to sleep.
307 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700308 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700309
310 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700311 * Set when the system is going to sleep, until we have
312 * successfully paused the current activity and released our wake lock.
313 * At that point the system is allowed to actually sleep.
314 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700315 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700316
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700317 /** Stack id of the front stack when user switched, indexed by userId. */
318 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700319
Craig Mautner4504de52013-12-20 09:06:56 -0800320 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800321 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700322 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800323
324 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700325 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326
Jeff Brownca9bc702014-02-11 14:32:56 -0800327 InputManagerInternal mInputManagerInternal;
328
Craig Mautner15df08a2015-04-01 12:17:18 -0700329 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
330 * may be finished until there is only one entry left. If this is empty the system is not
331 * in lockTask mode. */
332 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000333 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700334 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
335 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000336 */
337 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400338 /**
339 * Notifies the user when entering/exiting lock-task.
340 */
341 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800342
Craig Mautner15df08a2015-04-01 12:17:18 -0700343 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700344
Craig Mautner42d04db2014-11-06 12:13:23 -0800345 /** Used to keep resumeTopActivityLocked() from being entered recursively */
346 boolean inResumeTopActivity;
347
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700348 // 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 -0700349 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700350 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700351
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700352 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
353 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
354
Craig Mautneree36c772014-07-16 14:56:05 -0700355 /**
356 * Description of a request to start a new activity, which has been held
357 * due to app switches being disabled.
358 */
359 static class PendingActivityLaunch {
360 final ActivityRecord r;
361 final ActivityRecord sourceRecord;
362 final int startFlags;
363 final ActivityStack stack;
364
365 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
366 int _startFlags, ActivityStack _stack) {
367 r = _r;
368 sourceRecord = _sourceRecord;
369 startFlags = _startFlags;
370 stack = _stack;
371 }
372 }
373
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800374 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700375 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800376 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700377 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
378 }
379
380 /**
381 * At the time when the constructor runs, the power manager has not yet been
382 * initialized. So we initialize our wakelocks afterwards.
383 */
384 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800385 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700386 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700387 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700388 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700389 }
390
justinzhang5286d3f2014-05-12 17:06:01 -0400391 // This function returns a IStatusBarService. The value is from ServiceManager.
392 // getService and is cached.
393 private IStatusBarService getStatusBarService() {
394 synchronized (mService) {
395 if (mStatusBarService == null) {
396 mStatusBarService = IStatusBarService.Stub.asInterface(
397 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
398 if (mStatusBarService == null) {
399 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
400 }
401 }
402 return mStatusBarService;
403 }
404 }
405
Jason Monk35c62a42014-06-17 10:24:47 -0400406 private IDevicePolicyManager getDevicePolicyManager() {
407 synchronized (mService) {
408 if (mDevicePolicyManager == null) {
409 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
410 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
411 if (mDevicePolicyManager == null) {
412 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
413 }
414 }
415 return mDevicePolicyManager;
416 }
417 }
418
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700419 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800420 synchronized (mService) {
421 mWindowManager = wm;
422
423 mDisplayManager =
424 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
425 mDisplayManager.registerDisplayListener(this, null);
426
427 Display[] displays = mDisplayManager.getDisplays();
428 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
429 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800430 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700431 if (activityDisplay.mDisplay == null) {
432 throw new IllegalStateException("Default Display does not exist");
433 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800434 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800435 }
436
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700437 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800438 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800439
440 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700441
442 // Initialize this here, now that we can get a valid reference to PackageManager.
443 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800444 }
Craig Mautner27084302013-03-25 08:05:25 -0700445 }
446
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200447 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700448 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200449 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700450 }
451
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700452 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800453 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700454 }
455
Craig Mautnerde4ef022013-04-07 19:01:33 -0700456 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800457 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700458 }
459
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800460 /** Top of all visible stacks is/should always be equal to the focused stack.
461 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
462 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700463 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700464 if (stack == null) {
465 return false;
466 }
467
Craig Mautnerdf88d732014-01-27 09:21:32 -0800468 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
469 if (parent != null) {
470 stack = parent.task.stack;
471 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800472 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700473 }
474
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700475 void setFocusStack(String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800476 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800477 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800478 if (topNdx <= 0) {
479 return;
480 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700481
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700482 final ActivityStack topStack = stacks.get(topNdx);
483 mFocusedStack = topStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800484 if (lastFocusedStack != null) {
485 mLastFocusedStack = lastFocusedStack;
486 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800487
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700488 EventLogTags.writeAmFocusedStack(
489 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
490 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800491
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800492 if (mService.mBooting || !mService.mBooted) {
493 final ActivityRecord r = topRunningActivityLocked();
494 if (r != null && r.idle) {
495 checkFinishBootingLocked();
496 }
497 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700498 }
499
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700500 void moveHomeStackToFront(String reason) {
501 mHomeStack.moveToFront(reason);
502 }
503
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700504 /** Returns true if the focus activity was adjusted to the home stack top activity. */
505 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700506 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
507 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700508 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700509 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700510
Craig Mautner84984fa2014-06-19 11:19:20 -0700511 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700512
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700513 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700514 if (top == null) {
515 return false;
516 }
517 mService.setFocusedActivityLocked(top, reason);
518 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700519 }
520
Craig Mautner299f9602015-01-26 09:47:33 -0800521 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700522 if (!mService.mBooting && !mService.mBooted) {
523 // Not ready yet!
524 return false;
525 }
526
Craig Mautner84984fa2014-06-19 11:19:20 -0700527 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
528 mWindowManager.showRecentApps();
529 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700530 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700531
Craig Mautner84984fa2014-06-19 11:19:20 -0700532 if (prev != null) {
533 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
534 }
535
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700536 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
537 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700538 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800539 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700540 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700541 }
Craig Mautner299f9602015-01-26 09:47:33 -0800542 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700543 }
544
Craig Mautner8d341ef2013-03-26 09:03:27 -0700545 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700546 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700547 }
548
549 /**
550 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
551 * @param id Id of the task we would like returned.
552 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
553 * restore the task from recents to the active list.
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700554 * @param stackId The stack to restore the task to (default launch stack will be used
555 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700556 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700557 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800558 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800559 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800560 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
562 ActivityStack stack = stacks.get(stackNdx);
563 TaskRecord task = stack.taskForIdLocked(id);
564 if (task != null) {
565 return task;
566 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700567 }
568 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800569
570 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700571 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800572 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800573 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700574 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800575 return null;
576 }
577
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700578 if (!restoreFromRecents) {
579 return task;
580 }
581
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700582 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
584 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800585 return null;
586 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700587 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800588 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700589 }
590
Craig Mautner6170f732013-04-02 13:05:23 -0700591 ActivityRecord isInAnyStackLocked(IBinder token) {
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 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
597 if (r != null) {
598 return r;
599 }
Craig Mautner6170f732013-04-02 13:05:23 -0700600 }
601 }
602 return null;
603 }
604
Craig Mautneref73ee12014-04-23 11:45:37 -0700605 void setNextTaskId(int taskId) {
606 if (taskId > mCurTaskId) {
607 mCurTaskId = taskId;
608 }
609 }
610
Craig Mautner8d341ef2013-03-26 09:03:27 -0700611 int getNextTaskId() {
612 do {
613 mCurTaskId++;
614 if (mCurTaskId <= 0) {
615 mCurTaskId = 1;
616 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700617 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700618 return mCurTaskId;
619 }
620
Craig Mautnerde4ef022013-04-07 19:01:33 -0700621 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800622 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700623 if (stack == null) {
624 return null;
625 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700626 ActivityRecord resumedActivity = stack.mResumedActivity;
627 if (resumedActivity == null || resumedActivity.app == null) {
628 resumedActivity = stack.mPausingActivity;
629 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700630 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700631 }
632 }
633 return resumedActivity;
634 }
635
Dianne Hackbornff072722014-09-24 10:56:28 -0700636 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700637 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800638 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
640 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
642 final ActivityStack stack = stacks.get(stackNdx);
643 if (!isFrontStack(stack)) {
644 continue;
645 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700646 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647 if (hr != null) {
648 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
649 && processName.equals(hr.processName)) {
650 try {
George Mount2c92c972014-03-20 09:38:23 -0700651 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652 didSomething = true;
653 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700654 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 Slog.w(TAG, "Exception in new application when starting activity "
656 + hr.intent.getComponent().flattenToShortString(), e);
657 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700658 }
Craig Mautner20e72272013-04-01 13:45:53 -0700659 }
Craig Mautner20e72272013-04-01 13:45:53 -0700660 }
661 }
662 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700663 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700664 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700665 }
Craig Mautner20e72272013-04-01 13:45:53 -0700666 return didSomething;
667 }
668
669 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800670 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
671 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800672 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
673 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800674 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 continue;
676 }
677 final ActivityRecord resumedActivity = stack.mResumedActivity;
678 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700679 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800680 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800681 return false;
682 }
Craig Mautner20e72272013-04-01 13:45:53 -0700683 }
684 }
685 return true;
686 }
687
Craig Mautnerde4ef022013-04-07 19:01:33 -0700688 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800689 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
690 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800691 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
692 final ActivityStack stack = stacks.get(stackNdx);
693 if (isFrontStack(stack)) {
694 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700695 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800696 return false;
697 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700698 }
699 }
700 }
701 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700702 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
704 mLastFocusedStack + " to=" + mFocusedStack);
705 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700706 return true;
707 }
708
709 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800710 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800711 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
712 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800713 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
714 final ActivityStack stack = stacks.get(stackNdx);
715 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800716 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700717 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800718 return false;
719 }
720 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800721 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700722 }
723 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800724 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700725 }
726
Craig Mautner2acc3892013-09-23 10:28:14 -0700727 /**
728 * Pause all activities in either all of the stacks or just the back stacks.
729 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700730 * @return true if any activity was paused as a result of this call.
731 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700732 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700733 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800734 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
735 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800736 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
737 final ActivityStack stack = stacks.get(stackNdx);
738 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700739 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800740 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700741 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
742 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700744 }
745 }
746 return someActivityPaused;
747 }
748
Craig Mautnerde4ef022013-04-07 19:01:33 -0700749 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700750 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800751 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
752 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800753 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
754 final ActivityStack stack = stacks.get(stackNdx);
755 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700756 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800757 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700758 Slog.d(TAG_STATES,
759 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800760 pausing = false;
761 } else {
762 return false;
763 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700764 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700765 }
766 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700767 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700768 }
769
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700770 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
771 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500772 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800773 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
774 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
775 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
776 final ActivityStack stack = stacks.get(stackNdx);
777 if (stack.mResumedActivity != null &&
778 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700779 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800780 }
781 }
782 }
783 }
784
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700785 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700786 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700787 }
788
789 void sendWaitingVisibleReportLocked(ActivityRecord r) {
790 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700791 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700792 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700793 if (w.who == null) {
794 changed = true;
795 w.timeout = false;
796 if (r != null) {
797 w.who = new ComponentName(r.info.packageName, r.info.name);
798 }
799 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
800 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700801 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700802 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700803 if (changed) {
804 mService.notifyAll();
805 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700806 }
807
808 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
809 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700810 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700811 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700812 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700813 if (w.who == null) {
814 changed = true;
815 w.timeout = timeout;
816 if (r != null) {
817 w.who = new ComponentName(r.info.packageName, r.info.name);
818 }
819 w.thisTime = thisTime;
820 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700821 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700822 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700823 if (changed) {
824 mService.notifyAll();
825 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700826 }
827
Craig Mautner29219d92013-04-16 20:19:12 -0700828 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800829 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700830 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700831 if (r != null) {
832 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700833 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700834
Craig Mautner4a1cb222013-12-04 16:14:06 -0800835 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800836 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
838 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700839 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700840 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700841 if (r != null) {
842 return r;
843 }
844 }
845 }
846 return null;
847 }
848
Dianne Hackborn09233282014-04-30 11:33:59 -0700849 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700850 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800851 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
852 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800853 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800854 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800855 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800856 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
857 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700858 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800859 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700860 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700861 }
862 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700863
864 // The lists are already sorted from most recent to oldest. Just pull the most recent off
865 // each list and add it to list. Stop when all lists are empty or maxNum reached.
866 while (maxNum > 0) {
867 long mostRecentActiveTime = Long.MIN_VALUE;
868 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 final int numTaskLists = runningTaskLists.size();
870 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
871 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700872 if (!stackTaskList.isEmpty()) {
873 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
874 if (lastActiveTime > mostRecentActiveTime) {
875 mostRecentActiveTime = lastActiveTime;
876 selectedStackList = stackTaskList;
877 }
878 }
879 }
880 if (selectedStackList != null) {
881 list.add(selectedStackList.remove(0));
882 --maxNum;
883 } else {
884 break;
885 }
886 }
Craig Mautner20e72272013-04-01 13:45:53 -0700887 }
888
Craig Mautner23ac33b2013-04-01 16:26:35 -0700889 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700890 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700891 // Collect information about the target of the Intent.
892 ActivityInfo aInfo;
893 try {
894 ResolveInfo rInfo =
895 AppGlobals.getPackageManager().resolveIntent(
896 intent, resolvedType,
897 PackageManager.MATCH_DEFAULT_ONLY
898 | ActivityManagerService.STOCK_PM_FLAGS, userId);
899 aInfo = rInfo != null ? rInfo.activityInfo : null;
900 } catch (RemoteException e) {
901 aInfo = null;
902 }
903
904 if (aInfo != null) {
905 // Store the found target back into the intent, because now that
906 // we have it we never want to do this again. For example, if the
907 // user navigates back to this point in the history, we should
908 // always restart the exact same activity.
909 intent.setComponent(new ComponentName(
910 aInfo.applicationInfo.packageName, aInfo.name));
911
912 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700913 if (!aInfo.processName.equals("system")) {
914 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700915 mService.setDebugApp(aInfo.processName, true, false);
916 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700917
Man Caocfa78b22015-06-11 20:14:34 -0700918 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700919 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
920 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700921
Man Caocfa78b22015-06-11 20:14:34 -0700922 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
923 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
924 }
925
926 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700927 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700928 }
929 }
930 }
931 return aInfo;
932 }
933
Craig Mautner299f9602015-01-26 09:47:33 -0800934 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
935 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700936 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
937 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
938 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
939 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700940 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
941 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700942 null /* outActivity */, null /* container */, null /* inTask */);
943 if (inResumeTopActivity) {
944 // If we are in resume section already, home activity will be initialized, but not
945 // resumed (to avoid recursive resume) and will stay that way until something pokes it
946 // again. We need to schedule another resume.
947 scheduleResumeTopActivities();
948 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700949 }
950
Craig Mautner23ac33b2013-04-01 16:26:35 -0700951 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700952 String callingPackage, Intent intent, String resolvedType,
953 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700954 IBinder resultTo, String resultWho, int requestCode, int startFlags,
955 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700956 Bundle options, boolean ignoreTargetSecurity, int userId,
957 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700958 // Refuse possible leaked file descriptors
959 if (intent != null && intent.hasFileDescriptors()) {
960 throw new IllegalArgumentException("File descriptors passed in Intent");
961 }
962 boolean componentSpecified = intent.getComponent() != null;
963
964 // Don't modify the client's object!
965 intent = new Intent(intent);
966
967 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700968 ActivityInfo aInfo =
969 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700970
Craig Mautnere0a38842013-12-16 16:14:02 -0800971 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700972 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800973 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700974 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800975 // Cannot start a child activity if the parent is not resumed.
976 return ActivityManager.START_CANCELED;
977 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700978 final int realCallingPid = Binder.getCallingPid();
979 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700980 int callingPid;
981 if (callingUid >= 0) {
982 callingPid = -1;
983 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700984 callingPid = realCallingPid;
985 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700986 } else {
987 callingPid = callingUid = -1;
988 }
989
Craig Mautnere0a38842013-12-16 16:14:02 -0800990 final ActivityStack stack;
991 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800992 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800993 } else {
994 stack = container.mStack;
995 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800996 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700997 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700998 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700999
1000 final long origId = Binder.clearCallingIdentity();
1001
1002 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001003 (aInfo.applicationInfo.privateFlags
1004 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001005 // This may be a heavy-weight process! Check to see if we already
1006 // have another, different heavy-weight process running.
1007 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1008 if (mService.mHeavyWeightProcess != null &&
1009 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1010 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001011 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001012 if (caller != null) {
1013 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1014 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001015 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001016 } else {
1017 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001018 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001019 + intent.toString());
1020 ActivityOptions.abort(options);
1021 return ActivityManager.START_PERMISSION_DENIED;
1022 }
1023 }
1024
1025 IIntentSender target = mService.getIntentSenderLocked(
1026 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001027 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001028 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1029 | PendingIntent.FLAG_ONE_SHOT, null);
1030
1031 Intent newIntent = new Intent();
1032 if (requestCode >= 0) {
1033 // Caller is requesting a result.
1034 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1035 }
1036 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1037 new IntentSender(target));
1038 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1039 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1040 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1041 hist.packageName);
1042 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1043 hist.task.taskId);
1044 }
1045 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1046 aInfo.packageName);
1047 newIntent.setFlags(intent.getFlags());
1048 newIntent.setClassName("android",
1049 HeavyWeightSwitcherActivity.class.getName());
1050 intent = newIntent;
1051 resolvedType = null;
1052 caller = null;
1053 callingUid = Binder.getCallingUid();
1054 callingPid = Binder.getCallingPid();
1055 componentSpecified = true;
1056 try {
1057 ResolveInfo rInfo =
1058 AppGlobals.getPackageManager().resolveIntent(
1059 intent, null,
1060 PackageManager.MATCH_DEFAULT_ONLY
1061 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1062 aInfo = rInfo != null ? rInfo.activityInfo : null;
1063 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1064 } catch (RemoteException e) {
1065 aInfo = null;
1066 }
1067 }
1068 }
1069 }
1070
Dianne Hackborn91097de2014-04-04 18:02:06 -07001071 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1072 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001073 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001074 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001075 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001076
Craig Mautner85c11a82014-07-17 12:38:18 -07001077 Binder.restoreCallingIdentity(origId);
1078
Craig Mautnerde4ef022013-04-07 19:01:33 -07001079 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001080 // If the caller also wants to switch to a new configuration,
1081 // do so now. This allows a clean switch, as we are waiting
1082 // for the current activity to pause (so we will not destroy
1083 // it), and have not yet started the next activity.
1084 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1085 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001086 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001087 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001088 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001089 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001090 }
1091
Craig Mautner23ac33b2013-04-01 16:26:35 -07001092 if (outResult != null) {
1093 outResult.result = res;
1094 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001095 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001096 do {
1097 try {
1098 mService.wait();
1099 } catch (InterruptedException e) {
1100 }
1101 } while (!outResult.timeout && outResult.who == null);
1102 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001103 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001104 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001105 outResult.timeout = false;
1106 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1107 outResult.totalTime = 0;
1108 outResult.thisTime = 0;
1109 } else {
1110 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001111 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001112 do {
1113 try {
1114 mService.wait();
1115 } catch (InterruptedException e) {
1116 }
1117 } while (!outResult.timeout && outResult.who == null);
1118 }
1119 }
1120 }
1121
1122 return res;
1123 }
1124 }
1125
1126 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1127 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1128 Bundle options, int userId) {
1129 if (intents == null) {
1130 throw new NullPointerException("intents is null");
1131 }
1132 if (resolvedTypes == null) {
1133 throw new NullPointerException("resolvedTypes is null");
1134 }
1135 if (intents.length != resolvedTypes.length) {
1136 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1137 }
1138
Craig Mautner23ac33b2013-04-01 16:26:35 -07001139
1140 int callingPid;
1141 if (callingUid >= 0) {
1142 callingPid = -1;
1143 } else if (caller == null) {
1144 callingPid = Binder.getCallingPid();
1145 callingUid = Binder.getCallingUid();
1146 } else {
1147 callingPid = callingUid = -1;
1148 }
1149 final long origId = Binder.clearCallingIdentity();
1150 try {
1151 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001152 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001153 for (int i=0; i<intents.length; i++) {
1154 Intent intent = intents[i];
1155 if (intent == null) {
1156 continue;
1157 }
1158
1159 // Refuse possible leaked file descriptors
1160 if (intent != null && intent.hasFileDescriptors()) {
1161 throw new IllegalArgumentException("File descriptors passed in Intent");
1162 }
1163
1164 boolean componentSpecified = intent.getComponent() != null;
1165
1166 // Don't modify the client's object!
1167 intent = new Intent(intent);
1168
1169 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001170 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001171 // TODO: New, check if this is correct
1172 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1173
1174 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001175 (aInfo.applicationInfo.privateFlags
1176 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001177 throw new IllegalArgumentException(
1178 "FLAG_CANT_SAVE_STATE not supported here");
1179 }
1180
1181 Bundle theseOptions;
1182 if (options != null && i == intents.length-1) {
1183 theseOptions = options;
1184 } else {
1185 theseOptions = null;
1186 }
Craig Mautner6170f732013-04-02 13:05:23 -07001187 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001188 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1189 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001190 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001191 if (res < 0) {
1192 return res;
1193 }
1194
1195 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1196 }
1197 }
1198 } finally {
1199 Binder.restoreCallingIdentity(origId);
1200 }
1201
1202 return ActivityManager.START_SUCCESS;
1203 }
1204
Craig Mautner2420ead2013-04-01 17:13:20 -07001205 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001206 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001207 throws RemoteException {
1208
Craig Mautner2568c3a2015-03-26 14:22:34 -07001209 if (andResume) {
1210 r.startFreezingScreenLocked(app, 0);
1211 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001212
Craig Mautner2568c3a2015-03-26 14:22:34 -07001213 // schedule launch ticks to collect information about slow apps.
1214 r.startLaunchTickingLocked();
1215 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001216
1217 // Have the window manager re-evaluate the orientation of
1218 // the screen based on the new activity order. Note that
1219 // as a result of this, it can call back into the activity
1220 // manager with a new orientation. We don't care about that,
1221 // because the activity is not currently running so we are
1222 // just restarting it anyway.
1223 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001224 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001225 mService.mConfiguration,
1226 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001227 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001228 }
1229
1230 r.app = app;
1231 app.waitingToKill = null;
1232 r.launchCount++;
1233 r.lastLaunchTime = SystemClock.uptimeMillis();
1234
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001235 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001236
1237 int idx = app.activities.indexOf(r);
1238 if (idx < 0) {
1239 app.activities.add(r);
1240 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001241 mService.updateLruProcessLocked(app, true, null);
1242 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001243
Craig Mautner15df08a2015-04-01 12:17:18 -07001244 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001245 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1246 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001247 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001248 }
1249
1250 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 try {
1252 if (app.thread == null) {
1253 throw new RemoteException();
1254 }
1255 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001256 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001257 if (andResume) {
1258 results = r.results;
1259 newIntents = r.newIntents;
1260 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001261 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1262 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1263 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001264 if (andResume) {
1265 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1266 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001267 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001269 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001270 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001271 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001272 }
1273 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1274 r.sleeping = false;
1275 r.forceNewConfig = false;
1276 mService.showAskCompatModeDialogLocked(r);
1277 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001278 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001279 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1280 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1281 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001282 final String profileFile = mService.mProfileFile;
1283 if (profileFile != null) {
1284 ParcelFileDescriptor profileFd = mService.mProfileFd;
1285 if (profileFd != null) {
1286 try {
1287 profileFd = profileFd.dup();
1288 } catch (IOException e) {
1289 if (profileFd != null) {
1290 try {
1291 profileFd.close();
1292 } catch (IOException o) {
1293 }
1294 profileFd = null;
1295 }
1296 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001297 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001298
1299 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1300 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001301 }
1302 }
1303 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001304
Craig Mautner2568c3a2015-03-26 14:22:34 -07001305 if (andResume) {
1306 app.hasShownUi = true;
1307 app.pendingUiClean = true;
1308 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001309 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001310 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001311 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001312 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001313 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001314 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001315
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001316 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001317 // This may be a heavy-weight process! Note that the package
1318 // manager will ensure that only activity can run in the main
1319 // process of the .apk, which is the only thing that will be
1320 // considered heavy-weight.
1321 if (app.processName.equals(app.info.packageName)) {
1322 if (mService.mHeavyWeightProcess != null
1323 && mService.mHeavyWeightProcess != app) {
1324 Slog.w(TAG, "Starting new heavy weight process " + app
1325 + " when already running "
1326 + mService.mHeavyWeightProcess);
1327 }
1328 mService.mHeavyWeightProcess = app;
1329 Message msg = mService.mHandler.obtainMessage(
1330 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1331 msg.obj = r;
1332 mService.mHandler.sendMessage(msg);
1333 }
1334 }
1335
1336 } catch (RemoteException e) {
1337 if (r.launchFailed) {
1338 // This is the second time we failed -- finish activity
1339 // and give up.
1340 Slog.e(TAG, "Second failure launching "
1341 + r.intent.getComponent().flattenToShortString()
1342 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001343 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001344 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1345 "2nd-crash", false);
1346 return false;
1347 }
1348
1349 // This is the first time we failed -- restart process and
1350 // retry.
1351 app.activities.remove(r);
1352 throw e;
1353 }
1354
1355 r.launchFailed = false;
1356 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001357 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001358 }
1359
1360 if (andResume) {
1361 // As part of the process of launching, ActivityThread also performs
1362 // a resume.
1363 stack.minimalResumeActivityLocked(r);
1364 } else {
1365 // This activity is not starting in the resumed state... which
1366 // should look like we asked it to pause+stop (but remain visible),
1367 // and it has done so and reported back the current icicle and
1368 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001369 if (DEBUG_STATES) Slog.v(TAG_STATES,
1370 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001371 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001372 r.stopped = true;
1373 }
1374
1375 // Launch the new version setup screen if needed. We do this -after-
1376 // launching the initial activity (that is, home), so that it can have
1377 // a chance to initialize itself while in the background, making the
1378 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001379 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001380 mService.startSetupActivityLocked();
1381 }
1382
Dianne Hackborn465fa392014-09-14 14:21:18 -07001383 // Update any services we are bound to that might care about whether
1384 // their client may have activities.
1385 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1386
Craig Mautner2420ead2013-04-01 17:13:20 -07001387 return true;
1388 }
1389
Craig Mautnere79d42682013-04-01 19:01:53 -07001390 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001391 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001392 // Is this activity's application already running?
1393 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001394 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001395
1396 r.task.stack.setLaunchTime(r);
1397
1398 if (app != null && app.thread != null) {
1399 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001400 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1401 || !"android".equals(r.info.packageName)) {
1402 // Don't add this if it is a platform component that is marked
1403 // to run in multiple processes, because this is actually
1404 // part of the framework so doesn't make sense to track as a
1405 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001406 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1407 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001408 }
George Mount2c92c972014-03-20 09:38:23 -07001409 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001410 return;
1411 } catch (RemoteException e) {
1412 Slog.w(TAG, "Exception when starting activity "
1413 + r.intent.getComponent().flattenToShortString(), e);
1414 }
1415
1416 // If a dead object exception was thrown -- fall through to
1417 // restart the application.
1418 }
1419
1420 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001421 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001422 }
1423
Craig Mautner6170f732013-04-02 13:05:23 -07001424 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001425 Intent intent, String resolvedType, ActivityInfo aInfo,
1426 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1427 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001428 int callingPid, int callingUid, String callingPackage,
1429 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001430 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1431 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001432 int err = ActivityManager.START_SUCCESS;
1433
1434 ProcessRecord callerApp = null;
1435 if (caller != null) {
1436 callerApp = mService.getRecordForAppLocked(caller);
1437 if (callerApp != null) {
1438 callingPid = callerApp.pid;
1439 callingUid = callerApp.info.uid;
1440 } else {
1441 Slog.w(TAG, "Unable to find app for caller " + caller
1442 + " (pid=" + callingPid + ") when starting: "
1443 + intent.toString());
1444 err = ActivityManager.START_PERMISSION_DENIED;
1445 }
1446 }
1447
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001448 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1449
Craig Mautner6170f732013-04-02 13:05:23 -07001450 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001451 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001452 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001453 + " on display " + (container == null ? (mFocusedStack == null ?
1454 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1455 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1456 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001457 }
1458
1459 ActivityRecord sourceRecord = null;
1460 ActivityRecord resultRecord = null;
1461 if (resultTo != null) {
1462 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001463 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1464 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001465 if (sourceRecord != null) {
1466 if (requestCode >= 0 && !sourceRecord.finishing) {
1467 resultRecord = sourceRecord;
1468 }
1469 }
1470 }
Craig Mautner6170f732013-04-02 13:05:23 -07001471
Dianne Hackborn91097de2014-04-04 18:02:06 -07001472 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001473
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001474 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001475 // Transfer the result target from the source activity to the new
1476 // one being started, including any failures.
1477 if (requestCode >= 0) {
1478 ActivityOptions.abort(options);
1479 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1480 }
1481 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001482 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1483 resultRecord = null;
1484 }
Craig Mautner6170f732013-04-02 13:05:23 -07001485 resultWho = sourceRecord.resultWho;
1486 requestCode = sourceRecord.requestCode;
1487 sourceRecord.resultTo = null;
1488 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001489 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001490 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001491 if (sourceRecord.launchedFromUid == callingUid) {
1492 // The new activity is being launched from the same uid as the previous
1493 // activity in the flow, and asking to forward its result back to the
1494 // previous. In this case the activity is serving as a trampoline between
1495 // the two, so we also want to update its launchedFromPackage to be the
1496 // same as the previous activity. Note that this is safe, since we know
1497 // these two packages come from the same uid; the caller could just as
1498 // well have supplied that same package name itself. This specifially
1499 // deals with the case of an intent picker/chooser being launched in the app
1500 // flow to redirect to an activity picked by the user, where we want the final
1501 // activity to consider it to have been launched by the previous app activity.
1502 callingPackage = sourceRecord.launchedFromPackage;
1503 }
Craig Mautner6170f732013-04-02 13:05:23 -07001504 }
1505
1506 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1507 // We couldn't find a class that can handle the given Intent.
1508 // That's the end of that!
1509 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1510 }
1511
1512 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1513 // We couldn't find the specific class specified in the Intent.
1514 // Also the end of the line.
1515 err = ActivityManager.START_CLASS_NOT_FOUND;
1516 }
1517
Dianne Hackborn91097de2014-04-04 18:02:06 -07001518 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1519 && sourceRecord.task.voiceSession != null) {
1520 // If this activity is being launched as part of a voice session, we need
1521 // to ensure that it is safe to do so. If the upcoming activity will also
1522 // be part of the voice session, we can only launch it if it has explicitly
1523 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001524 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001525 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1526 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001527 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001528 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1529 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001530 Slog.w(TAG,
1531 "Activity being started in current voice task does not support voice: "
1532 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001533 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1534 }
1535 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001536 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001537 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1538 }
1539 }
1540 }
1541
1542 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1543 // If the caller is starting a new voice session, just make sure the target
1544 // is actually allowing it to run this way.
1545 try {
1546 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1547 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001548 Slog.w(TAG,
1549 "Activity being started in new voice task does not support: "
1550 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001551 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1552 }
1553 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001554 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001555 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1556 }
1557 }
1558
louis_changcd5d1982014-08-01 10:09:08 +08001559 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1560
Craig Mautner6170f732013-04-02 13:05:23 -07001561 if (err != ActivityManager.START_SUCCESS) {
1562 if (resultRecord != null) {
1563 resultStack.sendActivityResultLocked(-1,
1564 resultRecord, resultWho, requestCode,
1565 Activity.RESULT_CANCELED, null);
1566 }
Craig Mautner6170f732013-04-02 13:05:23 -07001567 ActivityOptions.abort(options);
1568 return err;
1569 }
1570
Svetoslav7008b512015-06-24 18:47:07 -07001571 boolean abort = false;
1572
Svet Ganov99b60432015-06-27 13:15:22 -07001573 final int startAnyPerm = mService.checkPermission(
1574 START_ANY_ACTIVITY, callingPid, callingUid);
1575
1576 if (startAnyPerm != PERMISSION_GRANTED) {
1577 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001578 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001579 final int actionRestriction = getActionRestrictionForCallingPackage(
1580 intent.getAction(), callingPackage, callingPid, callingUid);
1581
1582 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1583 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1584 if (resultRecord != null) {
1585 resultStack.sendActivityResultLocked(-1,
1586 resultRecord, resultWho, requestCode,
1587 Activity.RESULT_CANCELED, null);
1588 }
1589 String msg;
1590 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1591 msg = "Permission Denial: starting " + intent.toString()
1592 + " from " + callerApp + " (pid=" + callingPid
1593 + ", uid=" + callingUid + ")" + " with revoked permission "
1594 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1595 } else if (!aInfo.exported) {
1596 msg = "Permission Denial: starting " + intent.toString()
1597 + " from " + callerApp + " (pid=" + callingPid
1598 + ", uid=" + callingUid + ")"
1599 + " not exported from uid " + aInfo.applicationInfo.uid;
1600 } else {
1601 msg = "Permission Denial: starting " + intent.toString()
1602 + " from " + callerApp + " (pid=" + callingPid
1603 + ", uid=" + callingUid + ")"
1604 + " requires " + aInfo.permission;
1605 }
1606 Slog.w(TAG, msg);
1607 throw new SecurityException(msg);
1608 }
1609
1610 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1611 String message = "Appop Denial: starting " + intent.toString()
1612 + " from " + callerApp + " (pid=" + callingPid
1613 + ", uid=" + callingUid + ")"
1614 + " requires " + AppOpsManager.permissionToOp(
1615 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1616 Slog.w(TAG, message);
1617 abort = true;
1618 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1619 String message = "Appop Denial: starting " + intent.toString()
1620 + " from " + callerApp + " (pid=" + callingPid
1621 + ", uid=" + callingUid + ")"
1622 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1623 Slog.w(TAG, message);
1624 abort = true;
1625 }
Svetoslav7008b512015-06-24 18:47:07 -07001626 }
1627
1628 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001629 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001630
Craig Mautner6170f732013-04-02 13:05:23 -07001631 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001632 try {
1633 // The Intent we give to the watcher has the extra data
1634 // stripped off, since it can contain private information.
1635 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001636 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001637 aInfo.applicationInfo.packageName);
1638 } catch (RemoteException e) {
1639 mService.mController = null;
1640 }
Ben Gruver5e207332013-04-03 17:41:37 -07001641 }
Craig Mautner6170f732013-04-02 13:05:23 -07001642
Ben Gruver5e207332013-04-03 17:41:37 -07001643 if (abort) {
1644 if (resultRecord != null) {
1645 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001646 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001647 }
Ben Gruver5e207332013-04-03 17:41:37 -07001648 // We pretend to the caller that it was really started, but
1649 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001650 ActivityOptions.abort(options);
1651 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001652 }
1653
1654 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001655 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001656 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001657 if (outActivity != null) {
1658 outActivity[0] = r;
1659 }
1660
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001661 if (r.appTimeTracker == null && sourceRecord != null) {
1662 // If the caller didn't specify an explicit time tracker, we want to continue
1663 // tracking under any it has.
1664 r.appTimeTracker = sourceRecord.appTimeTracker;
1665 }
1666
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001667 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001668 if (voiceSession == null && (stack.mResumedActivity == null
1669 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001670 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1671 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001672 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001673 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001674 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001675 ActivityOptions.abort(options);
1676 return ActivityManager.START_SWITCHES_CANCELED;
1677 }
1678 }
1679
1680 if (mService.mDidAppSwitch) {
1681 // This is the second allowed switch since we stopped switches,
1682 // so now just generally allow switches. Use case: user presses
1683 // home (switches disabled, switch to home, mDidAppSwitch now true);
1684 // user taps a home icon (coming from home so allowed, we hit here
1685 // and now allow anyone to switch again).
1686 mService.mAppSwitchesAllowedTime = 0;
1687 } else {
1688 mService.mDidAppSwitch = true;
1689 }
1690
Craig Mautneree36c772014-07-16 14:56:05 -07001691 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001692
Dianne Hackborn91097de2014-04-04 18:02:06 -07001693 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001694 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001695
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001696 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001697 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001698 // activity start, but we are not actually doing an activity
1699 // switch... just dismiss the keyguard now, because we
1700 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001701 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001702 }
1703 return err;
1704 }
1705
Svet Ganov99b60432015-06-27 13:15:22 -07001706 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001707 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001708 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1709 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001710 == PackageManager.PERMISSION_DENIED) {
1711 return ACTIVITY_RESTRICTION_PERMISSION;
1712 }
1713
Christopher Tateff7add02015-08-17 10:23:22 -07001714 if (activityInfo.permission == null) {
1715 return ACTIVITY_RESTRICTION_NONE;
1716 }
1717
Svet Ganov99b60432015-06-27 13:15:22 -07001718 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1719 if (opCode == AppOpsManager.OP_NONE) {
1720 return ACTIVITY_RESTRICTION_NONE;
1721 }
1722
1723 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1724 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001725 if (!ignoreTargetSecurity) {
1726 return ACTIVITY_RESTRICTION_APPOP;
1727 }
Svet Ganov99b60432015-06-27 13:15:22 -07001728 }
1729
1730 return ACTIVITY_RESTRICTION_NONE;
1731 }
1732
Svetoslav7008b512015-06-24 18:47:07 -07001733 private int getActionRestrictionForCallingPackage(String action,
1734 String callingPackage, int callingPid, int callingUid) {
1735 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001736 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001737 }
1738
1739 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1740 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001741 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001742 }
1743
1744 final PackageInfo packageInfo;
1745 try {
1746 packageInfo = mService.mContext.getPackageManager()
1747 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1748 } catch (PackageManager.NameNotFoundException e) {
1749 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001750 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001751 }
1752
1753 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001754 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001755 }
1756
1757 if (mService.checkPermission(permission, callingPid, callingUid) ==
1758 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001759 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001760 }
1761
1762 final int opCode = AppOpsManager.permissionToOpCode(permission);
1763 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001764 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001765 }
1766
1767 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1768 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001769 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001770 }
1771
Svet Ganov99b60432015-06-27 13:15:22 -07001772 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001773 }
1774
Chong Zhang0fa656b2015-08-31 15:17:21 -07001775 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001776 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001777
1778 // On leanback only devices we should keep all activities in the same stack.
1779 if (!mLeanbackOnlyDevice &&
1780 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001781
1782 ActivityStack stack;
1783
Wale Ogunwale7d701172015-03-11 15:36:30 -07001784 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001785 stack = task.stack;
1786 if (stack.isOnHomeDisplay()) {
1787 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001788 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1789 "computeStackFocus: Setting " + "focused stack to r=" + r
1790 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001791 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001792 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001793 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001794 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001795 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001796 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001797 }
1798
Craig Mautnere0a38842013-12-16 16:14:02 -08001799 final ActivityContainer container = r.mInitialActivityContainer;
1800 if (container != null) {
1801 // The first time put it on the desired stack, after this put on task stack.
1802 r.mInitialActivityContainer = null;
1803 return container.mStack;
1804 }
1805
Chong Zhanga8be5e52015-10-05 17:06:28 -07001806 // The fullscreen stack can contain any task regardless of if the task is resizeable
1807 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1808 // If the freeform stack has focus, and the activity to be launched is resizeable,
1809 // we can also put it in the focused stack.
1810 final boolean canUseFocusedStack =
1811 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1812 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1813 if (canUseFocusedStack
Wale Ogunwale13a9b162015-09-22 21:38:51 -07001814 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001815 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001816 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001817 return mFocusedStack;
1818 }
1819
Wale Ogunwale706ed792015-08-02 10:29:44 -07001820 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001821 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1822 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001823 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001824 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1825 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001826 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001827 "computeStackFocus: Setting focused stack=" + stack);
1828 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001829 }
1830 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001831
Wale Ogunwale706ed792015-08-02 10:29:44 -07001832 // If there is no suitable dynamic stack then we figure out which static stack to use.
Wale Ogunwale13a9b162015-09-22 21:38:51 -07001833 final int stackId = task != null ? task.getLaunchStackId() :
Chong Zhang0fa656b2015-08-31 15:17:21 -07001834 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1835 FULLSCREEN_WORKSPACE_STACK_ID;
1836 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001837 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1838 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001839 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001840 }
1841 return mHomeStack;
1842 }
1843
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001844 boolean setFocusedStack(ActivityRecord r, String reason) {
1845 if (r == null) {
1846 // Not sure what you are trying to do, but it is not going to work...
1847 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001848 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001849 final TaskRecord task = r.task;
1850 if (task == null || task.stack == null) {
1851 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1852 return false;
1853 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001854 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001855 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001856 }
1857
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001858 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001859 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001860 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001861 final Intent intent = r.intent;
1862 final int callingUid = r.launchedFromUid;
1863
Chong Zhang0fa656b2015-08-31 15:17:21 -07001864 boolean overrideBounds = false;
1865 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001866 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
1867 ActivityOptions opts = new ActivityOptions(options);
1868 if (opts.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001869 overrideBounds = true;
Chong Zhang56f171d2015-10-08 10:39:23 -07001870 newBounds = opts.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001871 }
1872 }
1873
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001874 // In some flows in to this function, we retrieve the task record and hold on to it
1875 // without a lock before calling back in to here... so the task at this point may
1876 // not actually be in recents. Check for that, and if it isn't in recents just
1877 // consider it invalid.
1878 if (inTask != null && !inTask.inRecents) {
1879 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1880 inTask = null;
1881 }
1882
Craig Mautnerad400af2014-08-13 16:41:36 -07001883 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001884 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1885 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001886
Craig Mautnera228ae92014-07-09 05:44:55 -07001887 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001888 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001889 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001890 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1891 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1892 "\"singleInstance\" or \"singleTask\"");
1893 launchFlags &=
1894 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1895 } else {
1896 switch (r.info.documentLaunchMode) {
1897 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1898 break;
1899 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1900 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1901 break;
1902 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1903 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1904 break;
1905 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1906 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1907 break;
1908 }
1909 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001910
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001911 final boolean launchTaskBehind = r.mLaunchTaskBehind
1912 && !launchSingleTask && !launchSingleInstance
1913 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001914
Wale Ogunwale7d701172015-03-11 15:36:30 -07001915 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1916 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001917 // For whatever reason this activity is being launched into a new
1918 // task... yet the caller has requested a result back. Well, that
1919 // is pretty messed up, so instead immediately send back a cancel
1920 // and let the new task continue launched as normal without a
1921 // dependency on its originator.
1922 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1923 r.resultTo.task.stack.sendActivityResultLocked(-1,
1924 r.resultTo, r.resultWho, r.requestCode,
1925 Activity.RESULT_CANCELED, null);
1926 r.resultTo = null;
1927 }
1928
1929 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1930 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1931 }
1932
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001933 // If we are actually going to launch in to a new task, there are some cases where
1934 // we further want to do multiple task.
1935 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1936 if (launchTaskBehind
1937 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1938 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1939 }
1940 }
1941
Craig Mautner8849a5e2013-04-02 16:41:03 -07001942 // We'll invoke onUserLeaving before onPause only if the launching
1943 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001944 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001945 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1946 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947
1948 // If the caller has asked not to resume at this point, we make note
1949 // of this in the record so that we can skip it when trying to find
1950 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001951 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001952 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001953 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001954 }
1955
Craig Mautnera254cd72014-05-25 16:47:39 -07001956 ActivityRecord notTop =
1957 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001958
1959 // If the onlyIfNeeded flag is set, then we can do this if the activity
1960 // being launched is the same as the one making the call... or, as
1961 // a special case, if we do not know the caller then we count the
1962 // current top activity as the caller.
1963 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1964 ActivityRecord checkedCaller = sourceRecord;
1965 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001966 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001967 }
1968 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1969 // Caller is not the same as launcher, so always needed.
1970 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1971 }
1972 }
1973
Craig Mautner8849a5e2013-04-02 16:41:03 -07001974 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001975 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001976
1977 // If the caller is not coming from another activity, but has given us an
1978 // explicit task into which they would like us to launch the new activity,
1979 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001980 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1981 final Intent baseIntent = inTask.getBaseIntent();
1982 final ActivityRecord root = inTask.getRootActivity();
1983 if (baseIntent == null) {
1984 ActivityOptions.abort(options);
1985 throw new IllegalArgumentException("Launching into task without base intent: "
1986 + inTask);
1987 }
1988
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001989 // If this task is empty, then we are adding the first activity -- it
1990 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001991 if (launchSingleInstance || launchSingleTask) {
1992 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1993 ActivityOptions.abort(options);
1994 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1995 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001996 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001997 if (root != null) {
1998 ActivityOptions.abort(options);
1999 throw new IllegalArgumentException("Caller with inTask " + inTask
2000 + " has root " + root + " but target is singleInstance/Task");
2001 }
2002 }
2003
2004 // If task is empty, then adopt the interesting intent launch flags in to the
2005 // activity being started.
2006 if (root == null) {
2007 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2008 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2009 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2010 launchFlags = (launchFlags&~flagsOfInterest)
2011 | (baseIntent.getFlags()&flagsOfInterest);
2012 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002013 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002014 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002015
Dianne Hackborn962d5352014-08-29 16:27:40 -07002016 // If the task is not empty and the caller is asking to start it as the root
2017 // of a new task, then we don't actually want to start this on the task. We
2018 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002019 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002020 addingToTask = false;
2021
2022 } else {
2023 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002024 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002025
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002026 reuseTask = inTask;
2027 } else {
2028 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002029 // Launch ResolverActivity in the source task, so that it stays in the task
2030 // bounds when in freeform workspace.
2031 // Also put noDisplay activities in the source task. These by itself can
2032 // be placed in any task/stack, however it could launch other activities
2033 // like ResolverActivity, and we want those to stay in the original task.
2034 if (r.isResolverActivity() || r.noDisplay) {
2035 addingToTask = true;
2036 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002037 }
2038
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002039 if (inTask == null) {
2040 if (sourceRecord == null) {
2041 // This activity is not being started from another... in this
2042 // case we -always- start a new task.
2043 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2044 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2045 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2046 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2047 }
2048 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2049 // The original activity who is starting us is running as a single
2050 // instance... this new activity it is starting must go on its
2051 // own task.
2052 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2053 } else if (launchSingleInstance || launchSingleTask) {
2054 // The activity being started is a single instance... it always
2055 // gets launched into its own task.
2056 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2057 }
2058 }
2059
2060 ActivityInfo newTaskInfo = null;
2061 Intent newTaskIntent = null;
2062 ActivityStack sourceStack;
2063 if (sourceRecord != null) {
2064 if (sourceRecord.finishing) {
2065 // If the source is finishing, we can't further count it as our source. This
2066 // is because the task it is associated with may now be empty and on its way out,
2067 // so we don't want to blindly throw it in to that task. Instead we will take
2068 // the NEW_TASK flow and try to find a task for it. But save the task information
2069 // so it can be used when creating the new task.
2070 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2071 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2072 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2073 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2074 newTaskInfo = sourceRecord.info;
2075 newTaskIntent = sourceRecord.task.intent;
2076 }
2077 sourceRecord = null;
2078 sourceStack = null;
2079 } else {
2080 sourceStack = sourceRecord.task.stack;
2081 }
2082 } else {
2083 sourceStack = null;
2084 }
2085
2086 boolean movedHome = false;
2087 ActivityStack targetStack;
2088
2089 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002090 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002091
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002092 // We may want to try to place the new activity in to an existing task. We always
2093 // do this if the target activity is singleTask or singleInstance; we will also do
2094 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2095 // us to still place it in a new task: multi task, always doc mode, or being asked to
2096 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002097 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2098 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002099 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002100 // If bring to front is requested, and no result is requested and we have not
2101 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002102 // we can find a task that was started with this same
2103 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002104 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002105 // See if there is a task to bring to the front. If this is
2106 // a SINGLE_INSTANCE activity, there can be one and only one
2107 // instance of it in the history, and it is always in its own
2108 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002109 ActivityRecord intentActivity = !launchSingleInstance ?
2110 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002111 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002112 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2113 // but still needs to be a lock task mode violation since the task gets
2114 // cleared out and the device would otherwise leave the locked task.
2115 if (isLockTaskModeViolation(intentActivity.task,
2116 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2117 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002118 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002119 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002120 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2121 }
Craig Mautner29219d92013-04-16 20:19:12 -07002122 if (r.task == null) {
2123 r.task = intentActivity.task;
2124 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002125 if (intentActivity.task.intent == null) {
2126 // This task was started because of movement of
2127 // the activity based on affinity... now that we
2128 // are actually launching it, we can assign the
2129 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002130 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002131 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002132 targetStack = intentActivity.task.stack;
2133 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002134 // If the target task is not in the front, then we need
2135 // to bring it to the front... except... well, with
2136 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2137 // to have the same behavior as if a new instance was
2138 // being started, which means not bringing it to the front
2139 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002140 final ActivityStack focusStack = getFocusedStack();
2141 ActivityRecord curTop = (focusStack == null)
2142 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002143 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002144 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002145 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002146 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002147 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2148 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002149 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002150 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002151 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2152 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002153 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002154 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002155 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002156 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002157 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002158 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2159 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002160 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002161 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002162 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163 options = null;
2164 }
2165 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002166 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002167 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002168 + " from " + intentActivity);
2169 targetStack.moveToFront("intentActivityFound");
2170 }
2171
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 // If the caller has requested that the target task be
2173 // reset, then do so.
2174 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2175 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2176 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002177 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002178 // We don't need to start a new activity, and
2179 // the client said not to do anything if that
2180 // is the case, so this is it! And for paranoia, make
2181 // sure we have correctly resumed the top activity.
2182 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002183 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002184
2185 // Make sure to notify Keyguard as well if we are not running an app
2186 // transition later.
2187 if (!movedToFront) {
2188 notifyActivityDrawnForKeyguard();
2189 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002190 } else {
2191 ActivityOptions.abort(options);
2192 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002193 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002194 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2195 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002196 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002197 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002198 // The caller has requested to completely replace any
2199 // existing task with its new activity. Well that should
2200 // not be too hard...
2201 reuseTask = intentActivity.task;
2202 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002203 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002204 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002205 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002206 // In this situation we want to remove all activities
2207 // from the task up to the one being started. In most
2208 // cases this means we are resetting the task to its
2209 // initial state.
2210 ActivityRecord top =
2211 intentActivity.task.performClearTaskLocked(r, launchFlags);
2212 if (top != null) {
2213 if (top.frontOfTask) {
2214 // Activity aliases may mean we use different
2215 // intents for the top activity, so make sure
2216 // the task now has the identity of the new
2217 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002218 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002219 }
2220 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2221 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002222 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002223 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002224 // A special case: we need to start the activity because it is not
2225 // currently running, and the caller has asked to clear the current
2226 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002227 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002228 // Now pretend like this activity is being started by the top of its
2229 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002230 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002231 TaskRecord task = sourceRecord.task;
2232 if (task != null && task.stack == null) {
2233 // Target stack got cleared when we all activities were removed
2234 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002235 targetStack = computeStackFocus(
2236 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002237 targetStack.addTask(
2238 task, !launchTaskBehind /* toTop */, false /* moving */);
2239 }
2240
Craig Mautner8849a5e2013-04-02 16:41:03 -07002241 }
2242 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2243 // In this case the top activity on the task is the
2244 // same as the one being launched, so we take that
2245 // as a request to bring the task to the foreground.
2246 // If the top activity in the task is the root
2247 // activity, deliver this new intent to it if it
2248 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002249 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002250 && intentActivity.realActivity.equals(r.realActivity)) {
2251 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2252 intentActivity.task);
2253 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002254 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002255 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002256 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2257 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002258 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2259 // In this case we are launching the root activity
2260 // of the task, but with a different intent. We
2261 // should start a new instance on top.
2262 addingToTask = true;
2263 sourceRecord = intentActivity;
2264 }
2265 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2266 // In this case an activity is being launched in to an
2267 // existing task, without resetting that task. This
2268 // is typically the situation of launching an activity
2269 // from a notification or shortcut. We want to place
2270 // the new activity on top of the current task.
2271 addingToTask = true;
2272 sourceRecord = intentActivity;
2273 } else if (!intentActivity.task.rootWasReset) {
2274 // In this case we are launching in to an existing task
2275 // that has not yet been started from its front door.
2276 // The current task has been brought to the front.
2277 // Ideally, we'd probably like to place this new task
2278 // at the bottom of its stack, but that's a little hard
2279 // to do with the current organization of the code so
2280 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002281 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002282 }
2283 if (!addingToTask && reuseTask == null) {
2284 // We didn't do anything... but it was needed (a.k.a., client
2285 // don't use that intent!) And for paranoia, make
2286 // sure we have correctly resumed the top activity.
2287 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002288 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002289 if (!movedToFront) {
2290 // Make sure to notify Keyguard as well if we are not running an app
2291 // transition later.
2292 notifyActivityDrawnForKeyguard();
2293 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002294 } else {
2295 ActivityOptions.abort(options);
2296 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002297 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002298 return ActivityManager.START_TASK_TO_FRONT;
2299 }
2300 }
2301 }
2302 }
2303
2304 //String uri = r.intent.toURI();
2305 //Intent intent2 = new Intent(uri);
2306 //Slog.i(TAG, "Given intent: " + r.intent);
2307 //Slog.i(TAG, "URI is: " + uri);
2308 //Slog.i(TAG, "To intent: " + intent2);
2309
2310 if (r.packageName != null) {
2311 // If the activity being launched is the same as the one currently
2312 // at the top, then we need to check if it should only be launched
2313 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002314 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002315 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002316 if (top != null && r.resultTo == null) {
2317 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2318 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002319 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002320 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002321 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2322 top.task);
2323 // For paranoia, make sure we have correctly
2324 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002325 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002326 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002327 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002328 }
2329 ActivityOptions.abort(options);
2330 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2331 // We don't need to start a new activity, and
2332 // the client said not to do anything if that
2333 // is the case, so this is it!
2334 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2335 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002336 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002337 return ActivityManager.START_DELIVERED_TO_TOP;
2338 }
2339 }
2340 }
2341 }
2342
2343 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002344 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002345 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2346 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002347 }
2348 ActivityOptions.abort(options);
2349 return ActivityManager.START_CLASS_NOT_FOUND;
2350 }
2351
2352 boolean newTask = false;
2353 boolean keepCurTransition = false;
2354
Craig Mautnerbb742462014-07-07 15:28:55 -07002355 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002356 sourceRecord.task : null;
2357
Craig Mautner8849a5e2013-04-02 16:41:03 -07002358 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002359 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002360 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002361 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002362 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002363 if (doResume) {
2364 targetStack.moveToFront("startingNewTask");
2365 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002366
Craig Mautner8849a5e2013-04-02 16:41:03 -07002367 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002368 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2369 newTaskInfo != null ? newTaskInfo : r.info,
2370 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002371 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2372 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002373 if (overrideBounds) {
2374 r.task.updateOverrideConfiguration(newBounds);
2375 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002376 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2377 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002378 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002379 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002380 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002381 if (isLockTaskModeViolation(r.task)) {
2382 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2383 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2384 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002385 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002386 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002387 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2388 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002389 // Caller wants to appear on home activity, so before starting
2390 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002391 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002392 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002393 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002394 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002395 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002396 if (isLockTaskModeViolation(sourceTask)) {
2397 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2398 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2399 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002400 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002401 if (doResume) {
2402 targetStack.moveToFront("sourceStackToFront");
2403 }
Craig Mautner737fae22014-10-15 12:52:10 -07002404 final TaskRecord topTask = targetStack.topTask();
2405 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002406 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002407 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002408 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002409 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002410 // In this case, we are adding the activity to an existing
2411 // task, but the caller has asked to clear that task if the
2412 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002413 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002414 keepCurTransition = true;
2415 if (top != null) {
2416 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002417 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002418 // For paranoia, make sure we have correctly
2419 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002420 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002421 if (doResume) {
2422 targetStack.resumeTopActivityLocked(null);
2423 }
2424 ActivityOptions.abort(options);
2425 return ActivityManager.START_DELIVERED_TO_TOP;
2426 }
2427 } else if (!addingToTask &&
2428 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2429 // In this case, we are launching an activity in our own task
2430 // that may already be running somewhere in the history, and
2431 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002432 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002433 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002434 final TaskRecord task = top.task;
2435 task.moveActivityToFrontLocked(top);
2436 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002437 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002438 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002439 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002440 if (doResume) {
2441 targetStack.resumeTopActivityLocked(null);
2442 }
2443 return ActivityManager.START_DELIVERED_TO_TOP;
2444 }
2445 }
2446 // An existing activity is starting this new activity, so we want
2447 // to keep the new one in the same task as the one that is starting
2448 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002449 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002450 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002451 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002452
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002453 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002454 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002455 // task it has provided to us.
2456 if (isLockTaskModeViolation(inTask)) {
2457 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2458 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2459 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002460 if (overrideBounds) {
2461 inTask.updateOverrideConfiguration(newBounds);
2462 int stackId = inTask.getLaunchStackId();
2463 if (stackId != inTask.stack.mStackId) {
2464 moveTaskToStackUncheckedLocked(
2465 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2466 }
2467 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002468 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002469 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2470 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002471
2472 // Check whether we should actually launch the new activity in to the task,
2473 // or just reuse the current activity on top.
2474 ActivityRecord top = inTask.getTopActivity();
2475 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2476 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2477 || launchSingleTop || launchSingleTask) {
2478 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2479 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2480 // We don't need to start a new activity, and
2481 // the client said not to do anything if that
2482 // is the case, so this is it!
2483 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2484 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002485 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002486 return ActivityManager.START_DELIVERED_TO_TOP;
2487 }
2488 }
2489
Dianne Hackborn962d5352014-08-29 16:27:40 -07002490 if (!addingToTask) {
2491 // We don't actually want to have this activity added to the task, so just
2492 // stop here but still tell the caller that we consumed the intent.
2493 ActivityOptions.abort(options);
2494 return ActivityManager.START_TASK_TO_FRONT;
2495 }
2496
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002497 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002498 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002499 + " in explicit task " + r.task);
2500
Craig Mautner8849a5e2013-04-02 16:41:03 -07002501 } else {
2502 // This not being started from an existing activity, and not part
2503 // of a new task... just put it in the top task, though these days
2504 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002505 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002506 if (doResume) {
2507 targetStack.moveToFront("addingToTopTask");
2508 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002509 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002510 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002511 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002512 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002513 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002514 + " in new guessed " + r.task);
2515 }
2516
2517 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002518 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002519
Craig Mautner76e2a762014-06-24 09:05:43 -07002520 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2521 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2522 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002523 if (newTask) {
2524 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2525 }
2526 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002527 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002528 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002529 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002530 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002531 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002532 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002533 return ActivityManager.START_SUCCESS;
2534 }
2535
Craig Mautneree36c772014-07-16 14:56:05 -07002536 final void doPendingActivityLaunchesLocked(boolean doResume) {
2537 while (!mPendingActivityLaunches.isEmpty()) {
2538 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002539 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002540 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002541 }
2542 }
2543
Craig Mautner7f13ed32014-07-28 14:00:35 -07002544 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002545 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2546 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002547 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002548 mPendingActivityLaunches.remove(palNdx);
2549 }
2550 }
2551 }
2552
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002553 void setLaunchSource(int uid) {
2554 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2555 }
2556
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002557 void acquireLaunchWakelock() {
2558 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2559 throw new IllegalStateException("Calling must be system uid");
2560 }
2561 mLaunchingActivity.acquire();
2562 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2563 // To be safe, don't allow the wake lock to be held for too long.
2564 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2565 }
2566 }
2567
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002568 /**
2569 * Called when the frontmost task is idle.
2570 * @return the state of mService.mBooting before this was called.
2571 */
2572 private boolean checkFinishBootingLocked() {
2573 final boolean booting = mService.mBooting;
2574 boolean enableScreen = false;
2575 mService.mBooting = false;
2576 if (!mService.mBooted) {
2577 mService.mBooted = true;
2578 enableScreen = true;
2579 }
2580 if (booting || enableScreen) {
2581 mService.postFinishBooting(booting, enableScreen);
2582 }
2583 return booting;
2584 }
2585
Craig Mautnerf3333272013-04-22 10:55:53 -07002586 // Checked.
2587 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2588 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002589 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002590
Craig Mautnerf3333272013-04-22 10:55:53 -07002591 ArrayList<ActivityRecord> stops = null;
2592 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002593 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002594 int NS = 0;
2595 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002596 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002597 boolean activityRemoved = false;
2598
Wale Ogunwale7d701172015-03-11 15:36:30 -07002599 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002600 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002601 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2602 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002603 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2604 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002605 if (fromTimeout) {
2606 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002607 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002608
2609 // This is a hack to semi-deal with a race condition
2610 // in the client where it can be constructed with a
2611 // newer configuration from when we asked it to launch.
2612 // We'll update with whatever configuration it now says
2613 // it used to launch.
2614 if (config != null) {
2615 r.configuration = config;
2616 }
2617
2618 // We are now idle. If someone is waiting for a thumbnail from
2619 // us, we can now deliver.
2620 r.idle = true;
2621
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002622 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002623 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002624 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002625 }
2626 }
2627
2628 if (allResumedActivitiesIdle()) {
2629 if (r != null) {
2630 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002631 }
2632
2633 if (mLaunchingActivity.isHeld()) {
2634 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2635 if (VALIDATE_WAKE_LOCK_CALLER &&
2636 Binder.getCallingUid() != Process.myUid()) {
2637 throw new IllegalStateException("Calling must be system uid");
2638 }
2639 mLaunchingActivity.release();
2640 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002641 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002642 }
2643
2644 // Atomically retrieve all of the other things to do.
2645 stops = processStoppingActivitiesLocked(true);
2646 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002647 if ((NF = mFinishingActivities.size()) > 0) {
2648 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002649 mFinishingActivities.clear();
2650 }
2651
Craig Mautnerf3333272013-04-22 10:55:53 -07002652 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002653 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002654 mStartingUsers.clear();
2655 }
2656
Craig Mautnerf3333272013-04-22 10:55:53 -07002657 // Stop any activities that are scheduled to do so but have been
2658 // waiting for the next one to start.
2659 for (int i = 0; i < NS; i++) {
2660 r = stops.get(i);
2661 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002662 if (stack != null) {
2663 if (r.finishing) {
2664 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2665 } else {
2666 stack.stopActivityLocked(r);
2667 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002668 }
2669 }
2670
2671 // Finish any activities that are scheduled to do so but have been
2672 // waiting for the next one to start.
2673 for (int i = 0; i < NF; i++) {
2674 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002675 final ActivityStack stack = r.task.stack;
2676 if (stack != null) {
2677 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2678 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002679 }
2680
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002681 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002682 // Complete user switch
2683 if (startingUsers != null) {
2684 for (int i = 0; i < startingUsers.size(); i++) {
2685 mService.finishUserSwitch(startingUsers.get(i));
2686 }
2687 }
2688 // Complete starting up of background users
2689 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002690 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002691 mStartingBackgroundUsers.clear();
2692 for (int i = 0; i < startingUsers.size(); i++) {
2693 mService.finishUserBoot(startingUsers.get(i));
2694 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002695 }
2696 }
2697
2698 mService.trimApplications();
2699 //dump();
2700 //mWindowManager.dump();
2701
Craig Mautnerf3333272013-04-22 10:55:53 -07002702 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002703 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002704 }
2705
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002706 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002707 }
2708
Craig Mautner8e569572013-10-11 17:36:59 -07002709 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002710 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002711 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2712 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002713 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2714 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2715 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002716 }
Craig Mautner19091252013-10-05 00:03:53 -07002717 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002718 }
2719
2720 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002721 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2722 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002723 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2724 stacks.get(stackNdx).closeSystemDialogsLocked();
2725 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002726 }
2727 }
2728
Craig Mautner93529a42013-10-04 15:03:13 -07002729 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002730 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002731 }
2732
Craig Mautner8d341ef2013-03-26 09:03:27 -07002733 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002734 * Update the last used stack id for non-current user (current user's last
2735 * used stack is the focused stack)
2736 */
2737 void updateUserStackLocked(int userId, ActivityStack stack) {
2738 if (userId != mCurrentUser) {
2739 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2740 }
2741 }
2742
2743 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002744 * @return true if some activity was finished (or would have finished if doit were true).
2745 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002746 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2747 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002748 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002749 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2750 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002751 final int numStacks = stacks.size();
2752 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2753 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002754 if (stack.finishDisabledPackageActivitiesLocked(
2755 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002756 didSomething = true;
2757 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002758 }
2759 }
2760 return didSomething;
2761 }
2762
Dianne Hackborna413dc02013-07-12 12:02:55 -07002763 void updatePreviousProcessLocked(ActivityRecord r) {
2764 // Now that this process has stopped, we may want to consider
2765 // it to be the previous app to try to keep around in case
2766 // the user wants to return to it.
2767
2768 // First, found out what is currently the foreground app, so that
2769 // we don't blow away the previous app if this activity is being
2770 // hosted by the process that is actually still the foreground.
2771 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002772 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2773 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002774 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2775 final ActivityStack stack = stacks.get(stackNdx);
2776 if (isFrontStack(stack)) {
2777 if (stack.mResumedActivity != null) {
2778 fgApp = stack.mResumedActivity.app;
2779 } else if (stack.mPausingActivity != null) {
2780 fgApp = stack.mPausingActivity.app;
2781 }
2782 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002783 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002784 }
2785 }
2786
2787 // Now set this one as the previous process, only if that really
2788 // makes sense to.
2789 if (r.app != null && fgApp != null && r.app != fgApp
2790 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002791 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002792 mService.mPreviousProcess = r.app;
2793 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2794 }
2795 }
2796
Craig Mautner05d29032013-05-03 13:40:13 -07002797 boolean resumeTopActivitiesLocked() {
2798 return resumeTopActivitiesLocked(null, null, null);
2799 }
2800
2801 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2802 Bundle targetOptions) {
2803 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002804 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002805 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002806 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002807 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002808 if (isFrontStack(targetStack)) {
2809 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2810 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002811
Craig Mautnere0a38842013-12-16 16:14:02 -08002812 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2813 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002814 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2815 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002816 if (stack == targetStack) {
2817 // Already started above.
2818 continue;
2819 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002820 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002821 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002822 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002823 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002824 }
Craig Mautner05d29032013-05-03 13:40:13 -07002825 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002826 }
2827
Todd Kennedy539db512014-12-15 09:57:55 -08002828 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002829 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2830 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002831 final int numStacks = stacks.size();
2832 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2833 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002834 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002835 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002836 }
2837 }
2838
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002839 void finishVoiceTask(IVoiceInteractionSession session) {
2840 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2841 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2842 final int numStacks = stacks.size();
2843 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2844 final ActivityStack stack = stacks.get(stackNdx);
2845 stack.finishVoiceTask(session);
2846 }
2847 }
2848 }
2849
Craig Mautner299f9602015-01-26 09:47:33 -08002850 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002851 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2852 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002853 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002854 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2855 // Caller wants the home activity moved with it. To accomplish this,
2856 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002857 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002858 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002859 if (task.stack == null) {
2860 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2861 + task + " to front. Stack is null");
2862 return;
2863 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002864
2865 int stackId = task.stack.mStackId;
2866 if (task.mResizeable && options != null) {
2867 ActivityOptions opts = new ActivityOptions(options);
2868 if (opts.hasBounds()) {
2869 Rect bounds = opts.getBounds();
2870 task.updateOverrideConfiguration(bounds);
Chong Zhang87b21722015-09-21 15:39:51 -07002871 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2872 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002873 stackId = task.getLaunchStackId();
2874 }
2875 }
2876
2877 if (stackId != task.stack.mStackId) {
2878 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, FORCE_FOCUS, reason);
2879 } else {
2880 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002881 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2882 reason);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002883 }
2884
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002885 if (DEBUG_STACK) Slog.d(TAG_STACK,
2886 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002887 }
2888
Craig Mautner967212c2013-04-13 21:10:58 -07002889 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002890 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002891 }
2892
2893 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002894 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2895 if (activityContainer != null) {
2896 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002897 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002898 if (!createStaticStackIfNeeded
2899 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2900 return null;
2901 }
2902 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002903 }
2904
Craig Mautner967212c2013-04-13 21:10:58 -07002905 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002906 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002907 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2908 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002909 }
2910 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002911 }
2912
Craig Mautner4a1cb222013-12-04 16:14:06 -08002913 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002914 ActivityRecord homeActivity = getHomeActivity();
2915 if (homeActivity != null) {
2916 return homeActivity.appToken;
2917 }
2918 return null;
2919 }
2920
2921 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002922 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002923 }
2924
2925 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2927 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2928 final TaskRecord task = tasks.get(taskNdx);
2929 if (task.isHomeTask()) {
2930 final ArrayList<ActivityRecord> activities = task.mActivities;
2931 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2932 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002933 if (r.isHomeActivity()
2934 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002935 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002936 }
2937 }
2938 }
2939 }
2940 return null;
2941 }
2942
Todd Kennedyca4d8422015-01-15 15:19:22 -08002943 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002944 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002945 ActivityContainer activityContainer =
2946 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002947 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002948 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2949 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002950 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 return activityContainer;
2952 }
2953
Craig Mautner34b73df2014-01-12 21:11:08 -08002954 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002955 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2956 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2957 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002958 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2959 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002960 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002961 }
2962 }
2963
Craig Mautner95da1082014-02-24 17:54:35 -08002964 void deleteActivityContainer(IActivityContainer container) {
2965 ActivityContainer activityContainer = (ActivityContainer)container;
2966 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002967 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2968 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002969 final int stackId = activityContainer.mStackId;
2970 mActivityContainers.remove(stackId);
2971 mWindowManager.removeStack(stackId);
2972 }
2973 }
2974
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002975 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002976 final ActivityStack stack = getStack(stackId);
2977 if (stack == null) {
2978 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2979 return;
2980 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002981
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002982 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
2983
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002984 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002985
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002986 mTmpBounds.clear();
2987 mTmpConfigs.clear();
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002988 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2989 for (int i = tasks.size() - 1; i >= 0; i--) {
2990 TaskRecord task = tasks.get(i);
2991 if (task.mResizeable) {
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07002992 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002993 // For freeform stack we don't adjust the size of the tasks to match that
2994 // of the stack, but we do try to make sure the tasks are still contained
2995 // with the bounds of the stack.
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07002996 tempRect2.set(task.mBounds);
2997 fitWithinBounds(tempRect2, bounds);
2998 task.updateOverrideConfiguration(tempRect2);
2999 } else {
3000 task.updateOverrideConfiguration(bounds);
3001 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003002 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003003
3004 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3005 mTmpBounds.put(task.taskId, task.mBounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003006 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003007 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003008 if (stack.mStackId == DOCKED_STACK_ID) {
3009 // Dock stack funness...Yay!
3010 if (stack.mFullscreen) {
3011 // The dock stack went fullscreen which is kinda like dismissing it.
3012 // In this case we make all other static stacks fullscreen and move all
3013 // docked stack tasks to the fullscreen stack.
3014 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3015 if (i != DOCKED_STACK_ID) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003016 resizeStackLocked(i, null, preserveWindows);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003017 }
3018 }
3019
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003020 final int count = tasks.size();
3021 for (int i = 0; i < count; i++) {
3022 moveTaskToStackLocked(tasks.get(i).taskId,
3023 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
3024 }
3025
3026 // stack shouldn't contain anymore activities, so nothing to resume.
3027 r = null;
3028 } else {
3029 // Docked stacks occupy a dedicated region on screen so the size of all other
3030 // static stacks need to be adjusted so they don't overlap with the docked stack.
3031 final int leftChange = stack.mBounds.left - bounds.left;
3032 final int rightChange = stack.mBounds.right - bounds.right;
3033 final int topChange = stack.mBounds.top - bounds.top;
3034 final int bottomChange = stack.mBounds.bottom - bounds.bottom;
3035
3036 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3037 if (i != DOCKED_STACK_ID) {
3038 ActivityStack otherStack = getStack(i);
3039 if (otherStack != null) {
3040 tempRect.set(otherStack.mBounds);
3041 // We adjust the opposing sides of the other stacks to
3042 // the side in the dock stack that changed.
3043 tempRect.left -= rightChange;
3044 tempRect.right -= leftChange;
3045 tempRect.top -= bottomChange;
3046 tempRect.bottom -= topChange;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003047 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003048 }
3049 }
3050 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003051 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003052 // Since we are resizing the stack, all other operations should strive to preserve
3053 // windows.
3054 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003055 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003056 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003057
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003058 if (r != null) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003059 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003060 // And we need to make sure at this point that all other activities
3061 // are made visible with the correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003062 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003063 if (!updated) {
3064 resumeTopActivitiesLocked(stack, null, null);
3065 }
3066 }
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003067
3068 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003069 }
3070
Chong Zhang6de2ae82015-09-30 18:25:21 -07003071 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003072 if (!task.mResizeable) {
3073 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3074 return;
3075 }
3076
Chong Zhang87b21722015-09-21 15:39:51 -07003077 // If this is a forced resize, let it go through even if the bounds is not changing,
3078 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003079 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003080 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003081 // Nothing to do here...
3082 return;
3083 }
3084
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003085 if (!mWindowManager.isValidTaskId(task.taskId)) {
3086 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003087 // All we can do for now is update the bounds so it can be used when the task is
3088 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003089 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003090 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3091 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003092 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003093 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003094 return;
3095 }
3096
Chong Zhang6de2ae82015-09-30 18:25:21 -07003097 // Do not move the task to another stack here.
3098 // This method assumes that the task is already placed in the right stack.
3099 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003100
Chong Zhang6de2ae82015-09-30 18:25:21 -07003101 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003102
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003103 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003104 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003105 // way and the activity was kept the way it was. If it's false, it means the activity had
3106 // to be relaunched due to configuration change.
3107 boolean kept = true;
3108 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003109 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003110 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003111 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003112 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003113 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003114 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003115 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003116 resumeTopActivitiesLocked(stack, null, null);
3117 }
3118 }
3119 }
Chong Zhang87b21722015-09-21 15:39:51 -07003120 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003121
3122 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003123 }
3124
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003125 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003126 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3127 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003128 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003129 }
3130
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003131 ActivityContainer activityContainer = new ActivityContainer(stackId);
3132 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003133 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003134 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003135 }
3136
3137 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003138 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003139 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3140 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003141 break;
3142 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003143 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003144 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003145 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003146 }
3147
Chong Zhang5dcb2752015-08-18 13:50:26 -07003148 /**
3149 * Restores a recent task to a stack
3150 * @param task The recent task to be restored.
3151 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003152 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003153 * @return true if the task has been restored successfully.
3154 */
3155 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3156 if (stackId == INVALID_STACK_ID) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07003157 stackId = mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003158 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003159 if (task.stack != null) {
3160 // Task has already been restored once. See if we need to do anything more
3161 if (task.stack.mStackId == stackId) {
3162 // Nothing else to do since it is already restored in the right stack.
3163 return true;
3164 }
3165 // Remove current stack association, so we can re-associate the task with the
3166 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003167 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003168 }
3169
3170 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003171 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003172
3173 if (stack == null) {
3174 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003175 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3176 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003177 return false;
3178 }
3179
3180 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003181 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3182 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003183 final ArrayList<ActivityRecord> activities = task.mActivities;
3184 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003185 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003186 }
3187 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003188 }
3189
Wale Ogunwale040b4702015-08-06 18:10:50 -07003190 /**
3191 * Moves the specified task record to the input stack id.
3192 * WARNING: This method performs an unchecked/raw move of the task and
3193 * can leave the system in an unstable state if used incorrectly.
3194 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3195 * to a stack.
3196 * @param task Task to move.
3197 * @param stackId Id of stack to move task to.
3198 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003199 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003200 * @param reason Reason the task is been moved.
3201 * @return The stack the task was moved to.
3202 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003203 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003204 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3205 final ActivityRecord r = task.getTopActivity();
3206 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3207 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3208
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003209 final boolean resizeable = task.mResizeable;
3210 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3211 // if a docked stack is created below which will lead to the stack we are moving from and
3212 // its resizeable tasks being resized.
3213 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003214 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003215 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003216 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3217 if (task.stack != null) {
3218 task.stack.removeTask(task, reason, MOVING);
3219 }
3220 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003221
3222 // If the task had focus before (or we're requested to move focus),
3223 // move focus to the new stack.
3224 if (forceFocus || wasFocused) {
3225 // If the task owns the last resumed activity, transfer that together,
3226 // so that we don't resume the same activity again in the new stack.
3227 // Apps may depend on onResume()/onPause() being called in pairs.
3228 if (wasResumed) {
3229 stack.mResumedActivity = r;
3230 }
3231 // move the stack in which we are placing the task to the front.
3232 stack.moveToFront(reason);
3233 }
3234
Wale Ogunwale040b4702015-08-06 18:10:50 -07003235 return stack;
3236 }
3237
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003238 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003239 final TaskRecord task = anyTaskForIdLocked(taskId);
3240 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003241 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003242 return;
3243 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003244 final String reason = "moveTaskToStack";
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003245 if (stackId == DOCKED_STACK_ID || stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
3246 // We are about to relaunch the activity because its configuration changed due to
3247 // being maximized, i.e. size change. The activity will first remove the old window
3248 // and then add a new one. This call will tell window manager about this, so it can
3249 // preserve the old window until the new one is drawn. This prevents having a gap
3250 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003251 // entrance of the new window to be properly animated.
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003252 ActivityRecord r = task.getTopActivity();
3253 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3254 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003255 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003256 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003257
3258 // Make sure the task has the appropriate bounds/size for the stack it is in.
3259 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003260 resizeTaskLocked(task, stack.mBounds,
3261 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003262 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3263 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003264 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3265 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003266 } else if (stackId == DOCKED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003267 resizeTaskLocked(task, stack.mBounds,
3268 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003269 }
3270
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003271 // The task might have already been running and its visibility needs to be synchronized with
3272 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003273 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003274 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003275 }
3276
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003277 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3278 final TaskRecord task = anyTaskForIdLocked(taskId);
3279 if (task == null) {
3280 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3281 return;
3282 }
3283 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003284 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003285 mWindowManager.positionTaskInStack(taskId, stackId, position);
3286 final boolean stackChanged = task.stack != null && task.stack != stack;
3287 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003288 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003289 }
3290 stack.positionTask(task, position, stackChanged);
3291 // The task might have already been running and its visibility needs to be synchronized with
3292 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003293 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003294 resumeTopActivitiesLocked();
3295 }
3296
Craig Mautnerac6f8432013-07-17 13:24:59 -07003297 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003298 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003299 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3300 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003301 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3302 final ActivityStack stack = stacks.get(stackNdx);
3303 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003304 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003305 continue;
3306 }
3307 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003308 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3309 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003310 continue;
3311 }
3312 final ActivityRecord ar = stack.findTaskLocked(r);
3313 if (ar != null) {
3314 return ar;
3315 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003316 }
3317 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003318 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003319 return null;
3320 }
3321
3322 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003323 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3324 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003325 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3326 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3327 if (ar != null) {
3328 return ar;
3329 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003330 }
3331 }
3332 return null;
3333 }
3334
Craig Mautner8d341ef2013-03-26 09:03:27 -07003335 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003336 scheduleSleepTimeout();
3337 if (!mGoingToSleep.isHeld()) {
3338 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003339 if (mLaunchingActivity.isHeld()) {
3340 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3341 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003342 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003343 mLaunchingActivity.release();
3344 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003345 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003346 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003347 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003348 }
3349
3350 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003351 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003352
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003353 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003354 final long endTime = System.currentTimeMillis() + timeout;
3355 while (true) {
3356 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003357 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3358 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003359 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3360 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3361 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003362 }
3363 if (cantShutdown) {
3364 long timeRemaining = endTime - System.currentTimeMillis();
3365 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003366 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003367 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003368 } catch (InterruptedException e) {
3369 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003370 } else {
3371 Slog.w(TAG, "Activity manager shutdown timed out");
3372 timedout = true;
3373 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003374 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003375 } else {
3376 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003377 }
3378 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003379
3380 // Force checkReadyForSleep to complete.
3381 mSleepTimeout = true;
3382 checkReadyForSleepLocked();
3383
Craig Mautner8d341ef2013-03-26 09:03:27 -07003384 return timedout;
3385 }
3386
3387 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003388 removeSleepTimeouts();
3389 if (mGoingToSleep.isHeld()) {
3390 mGoingToSleep.release();
3391 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003392 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3393 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003394 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3395 final ActivityStack stack = stacks.get(stackNdx);
3396 stack.awakeFromSleepingLocked();
3397 if (isFrontStack(stack)) {
3398 resumeTopActivitiesLocked();
3399 }
Craig Mautner5314a402013-09-26 12:40:16 -07003400 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003401 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003402 mGoingToSleepActivities.clear();
3403 }
3404
3405 void activitySleptLocked(ActivityRecord r) {
3406 mGoingToSleepActivities.remove(r);
3407 checkReadyForSleepLocked();
3408 }
3409
3410 void checkReadyForSleepLocked() {
3411 if (!mService.isSleepingOrShuttingDown()) {
3412 // Do not care.
3413 return;
3414 }
3415
3416 if (!mSleepTimeout) {
3417 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003418 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3419 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003420 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3421 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3422 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003423 }
3424
3425 if (mStoppingActivities.size() > 0) {
3426 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003427 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003428 + mStoppingActivities.size() + " activities");
3429 scheduleIdleLocked();
3430 dontSleep = true;
3431 }
3432
3433 if (mGoingToSleepActivities.size() > 0) {
3434 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003435 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003436 + mGoingToSleepActivities.size() + " activities");
3437 dontSleep = true;
3438 }
3439
3440 if (dontSleep) {
3441 return;
3442 }
3443 }
3444
Craig Mautnere0a38842013-12-16 16:14:02 -08003445 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3446 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003447 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3448 stacks.get(stackNdx).goToSleep();
3449 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003450 }
3451
3452 removeSleepTimeouts();
3453
3454 if (mGoingToSleep.isHeld()) {
3455 mGoingToSleep.release();
3456 }
3457 if (mService.mShuttingDown) {
3458 mService.notifyAll();
3459 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003460 }
3461
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003462 boolean reportResumedActivityLocked(ActivityRecord r) {
3463 final ActivityStack stack = r.task.stack;
3464 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003465 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003466 }
3467 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003468 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003469 mWindowManager.executeAppTransition();
3470 return true;
3471 }
3472 return false;
3473 }
3474
Craig Mautner8d341ef2013-03-26 09:03:27 -07003475 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003476 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3477 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003478 int stackNdx = stacks.size() - 1;
3479 while (stackNdx >= 0) {
3480 stacks.get(stackNdx).handleAppCrashLocked(app);
3481 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003482 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003483 }
3484 }
3485
Jose Lima4b6c6692014-08-12 17:41:12 -07003486 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003487 final ActivityStack stack = r.task.stack;
3488 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003489 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3490 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003491 return false;
3492 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003493 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003494 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3495 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003496
3497 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003498 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003499 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003500 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003501 return true;
3502 }
3503
3504 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003505 if (visible && top.fullscreen) {
3506 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003507 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3508 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3509 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3510 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003511 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003512 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3513 // Only the activity set as currently visible behind should actively reset its
3514 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003515 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3516 "requestVisibleBehind: returning visible=" + visible
3517 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3518 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003519 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003520 }
3521
Jose Lima4b6c6692014-08-12 17:41:12 -07003522 stack.setVisibleBehindActivity(visible ? r : null);
3523 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003524 // Make the activity immediately above r opaque.
3525 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3526 if (next != null) {
3527 mService.convertFromTranslucent(next.appToken);
3528 }
3529 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003530 if (top.app != null && top.app.thread != null) {
3531 // Notify the top app of the change.
3532 try {
3533 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3534 } catch (RemoteException e) {
3535 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003536 }
3537 return true;
3538 }
3539
Craig Mautnerbb742462014-07-07 15:28:55 -07003540 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3541 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3542 r.mLaunchTaskBehind = false;
3543 final TaskRecord task = r.task;
3544 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003545 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003546 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003547 mWindowManager.setAppVisibility(r.appToken, false);
3548 }
3549
3550 void scheduleLaunchTaskBehindComplete(IBinder token) {
3551 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3552 }
3553
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003554 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3555 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003556 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003557 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3558 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003559 final int topStackNdx = stacks.size() - 1;
3560 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3561 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003562 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003563 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003564 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003565 }
3566
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003567 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3568 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3569 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3570 final int topStackNdx = stacks.size() - 1;
3571 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3572 final ActivityStack stack = stacks.get(stackNdx);
3573 stack.clearOtherAppTimeTrackers(except);
3574 }
3575 }
3576 }
3577
Craig Mautner8d341ef2013-03-26 09:03:27 -07003578 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003579 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3580 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003581 final int numStacks = stacks.size();
3582 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3583 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003584 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003585 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003586 }
3587 }
3588
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003589 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3590 // Examine all activities currently running in the process.
3591 TaskRecord firstTask = null;
3592 // Tasks is non-null only if two or more tasks are found.
3593 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003594 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3595 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003596 ActivityRecord r = app.activities.get(i);
3597 // First, if we find an activity that is in the process of being destroyed,
3598 // then we just aren't going to do anything for now; we want things to settle
3599 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003600 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003601 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003602 return;
3603 }
3604 // Don't consider any activies that are currently not in a state where they
3605 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003606 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3607 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003608 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003609 continue;
3610 }
3611 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003612 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003613 + " from " + r);
3614 if (firstTask == null) {
3615 firstTask = r.task;
3616 } else if (firstTask != r.task) {
3617 if (tasks == null) {
3618 tasks = new ArraySet<>();
3619 tasks.add(firstTask);
3620 }
3621 tasks.add(r.task);
3622 }
3623 }
3624 }
3625 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003626 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003627 return;
3628 }
3629 // If we have activities in multiple tasks that are in a position to be destroyed,
3630 // let's iterate through the tasks and release the oldest one.
3631 final int numDisplays = mActivityDisplays.size();
3632 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3633 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3634 // Step through all stacks starting from behind, to hit the oldest things first.
3635 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3636 final ActivityStack stack = stacks.get(stackNdx);
3637 // Try to release activities in this stack; if we manage to, we are done.
3638 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3639 return;
3640 }
3641 }
3642 }
3643 }
3644
Amith Yamasani37a40c22015-06-17 13:25:42 -07003645 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003646 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003647 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003648 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003649
Craig Mautner858d8a62013-04-23 17:08:34 -07003650 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003651 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3652 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003653 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003654 final ActivityStack stack = stacks.get(stackNdx);
3655 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003656 TaskRecord task = stack.topTask();
3657 if (task != null) {
3658 mWindowManager.moveTaskToTop(task.taskId);
3659 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003660 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003661 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003662
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003663 ActivityStack stack = getStack(restoreStackId);
3664 if (stack == null) {
3665 stack = mHomeStack;
3666 }
3667 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003668 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003669 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003670 } else {
3671 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003672 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003673 }
Craig Mautner93529a42013-10-04 15:03:13 -07003674 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003675 }
3676
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003677 /**
3678 * Add background users to send boot completed events to.
3679 * @param userId The user being started in the background
3680 * @param uss The state object for the user.
3681 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003682 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003683 mStartingBackgroundUsers.add(uss);
3684 }
3685
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003686 /** Checks whether the userid is a profile of the current user. */
3687 boolean isCurrentProfileLocked(int userId) {
3688 if (userId == mCurrentUser) return true;
3689 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3690 if (mService.mCurrentProfileIds[i] == userId) return true;
3691 }
3692 return false;
3693 }
3694
Chong Zhang45c25ce2015-08-10 22:18:26 -07003695 /** Checks whether the activity should be shown for current user. */
3696 boolean okToShowLocked(ActivityRecord r) {
3697 return r != null && (isCurrentProfileLocked(r.userId)
3698 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3699 }
3700
Craig Mautnerde4ef022013-04-07 19:01:33 -07003701 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003702 ArrayList<ActivityRecord> stops = null;
3703
3704 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003705 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3706 ActivityRecord s = mStoppingActivities.get(activityNdx);
3707 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003708 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003709 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3710 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003711 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003712 if (s.finishing) {
3713 // If this activity is finishing, it is sitting on top of
3714 // everyone else but we now know it is no longer needed...
3715 // so get rid of it. Otherwise, we need to go through the
3716 // normal flow and hide it once we determine that it is
3717 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003718 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003719 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003720 }
3721 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003722 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003723 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003724 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003725 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003726 }
3727 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003728 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003729 }
3730 }
3731
3732 return stops;
3733 }
3734
Craig Mautnercf910b02013-04-23 11:23:27 -07003735 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003736 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3737 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3738 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3739 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003740 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003741 final ActivityState state = r == null ? DESTROYED : r.state;
3742 if (isFrontStack(stack)) {
3743 if (r == null) Slog.e(TAG,
3744 "validateTop...: null top activity, stack=" + stack);
3745 else {
3746 final ActivityRecord pausing = stack.mPausingActivity;
3747 if (pausing != null && pausing == r) Slog.e(TAG,
3748 "validateTop...: top stack has pausing activity r=" + r
3749 + " state=" + state);
3750 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3751 "validateTop...: activity in front not resumed r=" + r
3752 + " state=" + state);
3753 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003754 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003755 final ActivityRecord resumed = stack.mResumedActivity;
3756 if (resumed != null && resumed == r) Slog.e(TAG,
3757 "validateTop...: back stack has resumed activity r=" + r
3758 + " state=" + state);
3759 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3760 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003761 }
3762 }
3763 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003764 }
3765
Craig Mautnere0570202015-05-13 13:06:11 -07003766 private String lockTaskModeToString() {
3767 switch (mLockTaskModeState) {
3768 case LOCK_TASK_MODE_LOCKED:
3769 return "LOCKED";
3770 case LOCK_TASK_MODE_PINNED:
3771 return "PINNED";
3772 case LOCK_TASK_MODE_NONE:
3773 return "NONE";
3774 default: return "unknown=" + mLockTaskModeState;
3775 }
3776 }
3777
Craig Mautner27084302013-03-25 08:05:25 -07003778 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003779 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003781 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3782 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3783 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003784 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003785 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3786 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3787 if (packages.size() > 0) {
3788 pw.println(" mLockTaskPackages (userId:packages)=");
3789 for (int i = 0; i < packages.size(); ++i) {
3790 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3791 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3792 }
3793 }
3794 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003795 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003796
Craig Mautner20e72272013-04-01 13:45:53 -07003797 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003798 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003799 }
3800
Dianne Hackborn390517b2013-05-30 15:03:32 -07003801 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3802 boolean needSep, String prefix) {
3803 if (activity != null) {
3804 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3805 if (needSep) {
3806 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003807 }
3808 pw.print(prefix);
3809 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003810 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003811 }
3812 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003813 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003814 }
3815
Craig Mautner8d341ef2013-03-26 09:03:27 -07003816 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3817 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003818 boolean printed = false;
3819 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003820 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3821 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003822 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003823 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003824 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003825 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003826 final ActivityStack stack = stacks.get(stackNdx);
3827 StringBuilder stackHeader = new StringBuilder(128);
3828 stackHeader.append(" Stack #");
3829 stackHeader.append(stack.mStackId);
3830 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003831 stackHeader.append("\n");
3832 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3833 stackHeader.append("\n");
3834 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003835 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3836 needSep, stackHeader.toString());
3837 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3838 !dumpAll, false, dumpPackage, true,
3839 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003840
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841 needSep = printed;
3842 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3843 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003844 if (pr) {
3845 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003846 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003847 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3849 " mResumedActivity: ");
3850 if (pr) {
3851 printed = true;
3852 needSep = false;
3853 }
3854 if (dumpAll) {
3855 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3856 " mLastPausedActivity: ");
3857 if (pr) {
3858 printed = true;
3859 needSep = true;
3860 }
3861 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3862 needSep, " mLastNoHistoryActivity: ");
3863 }
3864 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003865 }
3866 }
3867
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003868 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3869 false, dumpPackage, true, " Activities waiting to finish:", null);
3870 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3871 false, dumpPackage, true, " Activities waiting to stop:", null);
3872 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3873 false, dumpPackage, true, " Activities waiting for another to become visible:",
3874 null);
3875 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3876 false, dumpPackage, true, " Activities waiting to sleep:", null);
3877 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3878 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003879
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003880 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003881 }
3882
Dianne Hackborn390517b2013-05-30 15:03:32 -07003883 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003884 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003885 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003886 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003887 String innerPrefix = null;
3888 String[] args = null;
3889 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003890 for (int i=list.size()-1; i>=0; i--) {
3891 final ActivityRecord r = list.get(i);
3892 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3893 continue;
3894 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003895 if (innerPrefix == null) {
3896 innerPrefix = prefix + " ";
3897 args = new String[0];
3898 }
3899 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003900 final boolean full = !brief && (complete || !r.isInHistory());
3901 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003902 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003903 needNL = false;
3904 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003905 if (header1 != null) {
3906 pw.println(header1);
3907 header1 = null;
3908 }
3909 if (header2 != null) {
3910 pw.println(header2);
3911 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003912 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003913 if (lastTask != r.task) {
3914 lastTask = r.task;
3915 pw.print(prefix);
3916 pw.print(full ? "* " : " ");
3917 pw.println(lastTask);
3918 if (full) {
3919 lastTask.dump(pw, prefix + " ");
3920 } else if (complete) {
3921 // Complete + brief == give a summary. Isn't that obvious?!?
3922 if (lastTask.intent != null) {
3923 pw.print(prefix); pw.print(" ");
3924 pw.println(lastTask.intent.toInsecureStringWithClip());
3925 }
3926 }
3927 }
3928 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3929 pw.print(" #"); pw.print(i); pw.print(": ");
3930 pw.println(r);
3931 if (full) {
3932 r.dump(pw, innerPrefix);
3933 } else if (complete) {
3934 // Complete + brief == give a summary. Isn't that obvious?!?
3935 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3936 if (r.app != null) {
3937 pw.print(innerPrefix); pw.println(r.app);
3938 }
3939 }
3940 if (client && r.app != null && r.app.thread != null) {
3941 // flush anything that is already in the PrintWriter since the thread is going
3942 // to write to the file descriptor directly
3943 pw.flush();
3944 try {
3945 TransferPipe tp = new TransferPipe();
3946 try {
3947 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3948 r.appToken, innerPrefix, args);
3949 // Short timeout, since blocking here can
3950 // deadlock with the application.
3951 tp.go(fd, 2000);
3952 } finally {
3953 tp.kill();
3954 }
3955 } catch (IOException e) {
3956 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3957 } catch (RemoteException e) {
3958 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3959 }
3960 needNL = true;
3961 }
3962 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003963 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003964 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003965
Craig Mautnerf3333272013-04-22 10:55:53 -07003966 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003967 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3968 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003969 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3970 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003971 }
3972
3973 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003974 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003975 }
3976
3977 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003978 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3979 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003980 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3981 }
3982
Craig Mautner05d29032013-05-03 13:40:13 -07003983 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003984 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3985 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3986 }
Craig Mautner05d29032013-05-03 13:40:13 -07003987 }
3988
Craig Mautner0eea92c2013-05-16 13:35:39 -07003989 void removeSleepTimeouts() {
3990 mSleepTimeout = false;
3991 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3992 }
3993
3994 final void scheduleSleepTimeout() {
3995 removeSleepTimeouts();
3996 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3997 }
3998
Craig Mautner4a1cb222013-12-04 16:14:06 -08003999 @Override
4000 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004001 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004002 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4003 }
4004
4005 @Override
4006 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004007 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004008 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4009 }
4010
4011 @Override
4012 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004013 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004014 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4015 }
4016
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004017 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004018 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004019 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004020 newDisplay = mActivityDisplays.get(displayId) == null;
4021 if (newDisplay) {
4022 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004023 if (activityDisplay.mDisplay == null) {
4024 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4025 return;
4026 }
Craig Mautner4504de52013-12-20 09:06:56 -08004027 mActivityDisplays.put(displayId, activityDisplay);
4028 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004029 }
Craig Mautner4504de52013-12-20 09:06:56 -08004030 if (newDisplay) {
4031 mWindowManager.onDisplayAdded(displayId);
4032 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004033 }
4034
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004035 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004036 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004037 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4038 if (activityDisplay != null) {
4039 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004040 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004041 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004042 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004043 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004044 }
4045 }
4046 mWindowManager.onDisplayRemoved(displayId);
4047 }
4048
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004049 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004050 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004051 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4052 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 // TODO: Update the bounds.
4054 }
4055 }
4056 mWindowManager.onDisplayChanged(displayId);
4057 }
4058
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004059 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004060 StackInfo info = new StackInfo();
4061 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4062 info.displayId = Display.DEFAULT_DISPLAY;
4063 info.stackId = stack.mStackId;
4064
4065 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4066 final int numTasks = tasks.size();
4067 int[] taskIds = new int[numTasks];
4068 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004069 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004070 for (int i = 0; i < numTasks; ++i) {
4071 final TaskRecord task = tasks.get(i);
4072 taskIds[i] = task.taskId;
4073 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4074 : task.realActivity != null ? task.realActivity.flattenToString()
4075 : task.getTopActivity() != null ? task.getTopActivity().packageName
4076 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004077 taskBounds[i] = new Rect();
4078 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004079 }
4080 info.taskIds = taskIds;
4081 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004082 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004083 return info;
4084 }
4085
4086 StackInfo getStackInfoLocked(int stackId) {
4087 ActivityStack stack = getStack(stackId);
4088 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004089 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004090 }
4091 return null;
4092 }
4093
4094 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004095 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004096 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4097 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004098 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004099 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 }
4101 }
4102 return list;
4103 }
4104
Craig Mautner15df08a2015-04-01 12:17:18 -07004105 TaskRecord getLockedTaskLocked() {
4106 final int top = mLockTaskModeTasks.size() - 1;
4107 if (top >= 0) {
4108 return mLockTaskModeTasks.get(top);
4109 }
4110 return null;
4111 }
4112
4113 boolean isLockedTask(TaskRecord task) {
4114 return mLockTaskModeTasks.contains(task);
4115 }
4116
4117 boolean isLastLockedTask(TaskRecord task) {
4118 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4119 }
4120
4121 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004122 if (!mLockTaskModeTasks.remove(task)) {
4123 return;
4124 }
4125 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4126 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004127 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004128 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4129 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004130 final Message lockTaskMsg = Message.obtain();
4131 lockTaskMsg.arg1 = task.userId;
4132 lockTaskMsg.what = LOCK_TASK_END_MSG;
4133 mHandler.sendMessage(lockTaskMsg);
4134 }
4135 }
4136
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004137 void showLockTaskToast() {
4138 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004139 }
4140
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004141 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4142 if (mLockTaskModeTasks.contains(task)) {
4143 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4144 }
4145 }
4146
Craig Mautner432f64e2015-05-20 14:59:57 -07004147 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4148 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004149 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004150 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004151 final TaskRecord lockedTask = getLockedTaskLocked();
4152 if (lockedTask != null) {
4153 removeLockedTaskLocked(lockedTask);
4154 if (!mLockTaskModeTasks.isEmpty()) {
4155 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004156 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4157 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004158 lockedTask.performClearTaskLocked();
4159 resumeTopActivitiesLocked();
4160 return;
4161 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004162 }
Craig Mautnere0570202015-05-13 13:06:11 -07004163 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4164 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004165 return;
4166 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004167
4168 // Should have already been checked, but do it again.
4169 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004170 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4171 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004172 return;
4173 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004174 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004175 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004176 return;
4177 }
4178
4179 if (mLockTaskModeTasks.isEmpty()) {
4180 // First locktask.
4181 final Message lockTaskMsg = Message.obtain();
4182 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4183 lockTaskMsg.arg1 = task.userId;
4184 lockTaskMsg.what = LOCK_TASK_START_MSG;
4185 lockTaskMsg.arg2 = lockTaskModeState;
4186 mHandler.sendMessage(lockTaskMsg);
4187 }
4188 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004189 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4190 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004191 mLockTaskModeTasks.remove(task);
4192 mLockTaskModeTasks.add(task);
4193
4194 if (task.mLockTaskUid == -1) {
4195 task.mLockTaskUid = task.mCallingUid;
4196 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004197
4198 if (andResume) {
4199 findTaskToMoveToFrontLocked(task, 0, null, reason);
4200 resumeTopActivitiesLocked();
4201 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004202 }
4203
4204 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004205 return isLockTaskModeViolation(task, false);
4206 }
4207
4208 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4209 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004210 return false;
4211 }
4212 final int lockTaskAuth = task.mLockTaskAuth;
4213 switch (lockTaskAuth) {
4214 case LOCK_TASK_AUTH_DONT_LOCK:
4215 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004216 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004217 case LOCK_TASK_AUTH_LAUNCHABLE:
4218 case LOCK_TASK_AUTH_WHITELISTED:
4219 return false;
4220 case LOCK_TASK_AUTH_PINNABLE:
4221 // Pinnable tasks can't be launched on top of locktask tasks.
4222 return !mLockTaskModeTasks.isEmpty();
4223 default:
4224 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4225 return true;
4226 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004227 }
4228
Craig Mautner15df08a2015-04-01 12:17:18 -07004229 void onLockTaskPackagesUpdatedLocked() {
4230 boolean didSomething = false;
4231 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4232 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004233 final boolean wasWhitelisted =
4234 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4235 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004236 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004237 final boolean isWhitelisted =
4238 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4239 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4240 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004241 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004242 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4243 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004244 removeLockedTaskLocked(lockedTask);
4245 lockedTask.performClearTaskLocked();
4246 didSomething = true;
4247 }
4248 }
4249 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4250 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4251 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4252 final ActivityStack stack = stacks.get(stackNdx);
4253 stack.onLockTaskPackagesUpdatedLocked();
4254 }
4255 }
Craig Mautnere0570202015-05-13 13:06:11 -07004256 final ActivityRecord r = topRunningActivityLocked();
4257 final TaskRecord task = r != null ? r.task : null;
4258 if (mLockTaskModeTasks.isEmpty() && task != null
4259 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4260 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004261 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4262 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4263 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4264 false);
4265 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004266 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004267 if (didSomething) {
4268 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004269 }
4270 }
4271
Benjamin Franz43261142015-02-11 15:59:44 +00004272 int getLockTaskModeState() {
4273 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004274 }
4275
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004276 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004277
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004278 public ActivityStackSupervisorHandler(Looper looper) {
4279 super(looper);
4280 }
4281
Craig Mautnerf3333272013-04-22 10:55:53 -07004282 void activityIdleInternal(ActivityRecord r) {
4283 synchronized (mService) {
4284 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4285 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004286 }
4287
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004288 @Override
4289 public void handleMessage(Message msg) {
4290 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004291 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004292 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4293 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004294 if (mService.mDidDexOpt) {
4295 mService.mDidDexOpt = false;
4296 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4297 nmsg.obj = msg.obj;
4298 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4299 return;
4300 }
4301 // We don't at this point know if the activity is fullscreen,
4302 // so we need to be conservative and assume it isn't.
4303 activityIdleInternal((ActivityRecord)msg.obj);
4304 } break;
4305 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004306 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004307 activityIdleInternal((ActivityRecord)msg.obj);
4308 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004309 case RESUME_TOP_ACTIVITY_MSG: {
4310 synchronized (mService) {
4311 resumeTopActivitiesLocked();
4312 }
4313 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004314 case SLEEP_TIMEOUT_MSG: {
4315 synchronized (mService) {
4316 if (mService.isSleepingOrShuttingDown()) {
4317 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4318 mSleepTimeout = true;
4319 checkReadyForSleepLocked();
4320 }
4321 }
4322 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004323 case LAUNCH_TIMEOUT_MSG: {
4324 if (mService.mDidDexOpt) {
4325 mService.mDidDexOpt = false;
4326 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4327 return;
4328 }
4329 synchronized (mService) {
4330 if (mLaunchingActivity.isHeld()) {
4331 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4332 if (VALIDATE_WAKE_LOCK_CALLER
4333 && Binder.getCallingUid() != Process.myUid()) {
4334 throw new IllegalStateException("Calling must be system uid");
4335 }
4336 mLaunchingActivity.release();
4337 }
4338 }
4339 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004340 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004341 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004342 } break;
4343 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004344 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004345 } break;
4346 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004347 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004348 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004349 case CONTAINER_CALLBACK_VISIBILITY: {
4350 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004351 final IActivityContainerCallback callback = container.mCallback;
4352 if (callback != null) {
4353 try {
4354 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4355 } catch (RemoteException e) {
4356 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004357 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004358 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004359 case LOCK_TASK_START_MSG: {
4360 // When lock task starts, we disable the status bars.
4361 try {
Jason Monk62515be2014-05-21 16:06:19 -04004362 if (mLockTaskNotify == null) {
4363 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004364 }
Jason Monk62515be2014-05-21 16:06:19 -04004365 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004366 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004367 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004368 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004369 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004370 flags = StatusBarManager.DISABLE_MASK
4371 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004372 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004373 flags = StatusBarManager.DISABLE_MASK
4374 & (~StatusBarManager.DISABLE_BACK)
4375 & (~StatusBarManager.DISABLE_HOME)
4376 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004377 }
4378 getStatusBarService().disable(flags, mToken,
4379 mService.mContext.getPackageName());
4380 }
4381 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004382 if (getDevicePolicyManager() != null) {
4383 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004384 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004385 }
justinzhang5286d3f2014-05-12 17:06:01 -04004386 } catch (RemoteException ex) {
4387 throw new RuntimeException(ex);
4388 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004389 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004390 case LOCK_TASK_END_MSG: {
4391 // When lock task ends, we enable the status bars.
4392 try {
Jason Monk62515be2014-05-21 16:06:19 -04004393 if (getStatusBarService() != null) {
4394 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4395 mService.mContext.getPackageName());
4396 }
4397 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004398 if (getDevicePolicyManager() != null) {
4399 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4400 msg.arg1);
4401 }
Jason Monk62515be2014-05-21 16:06:19 -04004402 if (mLockTaskNotify == null) {
4403 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4404 }
4405 mLockTaskNotify.show(false);
4406 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004407 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004408 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004409 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004410 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004411 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004412 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004413 new LockPatternUtils(mService.mContext)
4414 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004415 }
4416 } catch (SettingNotFoundException e) {
4417 // No setting, don't lock.
4418 }
justinzhang5286d3f2014-05-12 17:06:01 -04004419 } catch (RemoteException ex) {
4420 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004421 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004422 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004423 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004424 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004425 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4426 if (mLockTaskNotify == null) {
4427 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4428 }
4429 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4430 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004431 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4432 final ActivityContainer container = (ActivityContainer) msg.obj;
4433 final IActivityContainerCallback callback = container.mCallback;
4434 if (callback != null) {
4435 try {
4436 callback.onAllActivitiesComplete(container.asBinder());
4437 } catch (RemoteException e) {
4438 }
4439 }
4440 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004441 case LAUNCH_TASK_BEHIND_COMPLETE: {
4442 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004443 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004444 if (r != null) {
4445 handleLaunchTaskBehindCompleteLocked(r);
4446 }
4447 }
4448 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004449 }
4450 }
4451 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004452
Ying Wangb081a592014-04-22 15:20:16 -07004453 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004454 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004455 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004456 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004457 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004458 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004459 ActivityRecord mParentActivity = null;
4460 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004461
Craig Mautnere3a00d72014-04-16 08:31:19 -07004462 boolean mVisible = true;
4463
Craig Mautner4a1cb222013-12-04 16:14:06 -08004464 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004465 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004466
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004467 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4468 final static int CONTAINER_STATE_NO_SURFACE = 1;
4469 final static int CONTAINER_STATE_FINISHING = 2;
4470 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4471
4472 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004473 synchronized (mService) {
4474 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004475 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004476 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004477 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004478 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004479 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004480
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004481 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004482 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004483 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004484 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004485 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004486 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004487 }
4488
4489 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004490 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004491 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004492 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4493 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004494 return;
4495 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004496 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004497 }
4498 }
4499
4500 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004501 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004502 synchronized (mService) {
4503 if (mActivityDisplay != null) {
4504 return mActivityDisplay.mDisplayId;
4505 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004506 }
4507 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004508 }
4509
Jeff Brownca9bc702014-02-11 14:32:56 -08004510 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004511 public int getStackId() {
4512 synchronized (mService) {
4513 return mStackId;
4514 }
4515 }
4516
4517 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004518 public boolean injectEvent(InputEvent event) {
4519 final long origId = Binder.clearCallingIdentity();
4520 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004521 synchronized (mService) {
4522 if (mActivityDisplay != null) {
4523 return mInputManagerInternal.injectInputEvent(event,
4524 mActivityDisplay.mDisplayId,
4525 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4526 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004527 }
4528 return false;
4529 } finally {
4530 Binder.restoreCallingIdentity(origId);
4531 }
4532 }
4533
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004534 @Override
4535 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004536 synchronized (mService) {
4537 if (mContainerState == CONTAINER_STATE_FINISHING) {
4538 return;
4539 }
4540 mContainerState = CONTAINER_STATE_FINISHING;
4541
Craig Mautnerd163e752014-06-13 17:18:47 -07004542 long origId = Binder.clearCallingIdentity();
4543 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004544 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004545 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004546 } finally {
4547 Binder.restoreCallingIdentity(origId);
4548 }
4549 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004550 }
4551
Craig Mautner60257702014-09-17 15:02:33 -07004552 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004553 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004554 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004555 if (mActivityDisplay != null) {
4556 mActivityDisplay.detachActivitiesLocked(mStack);
4557 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004558 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004559 }
4560 }
4561
4562 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004563 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004564 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004565 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004566 Binder.getCallingUid(), mCurrentUser, false,
4567 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004568
Craig Mautnere0a38842013-12-16 16:14:02 -08004569 // TODO: Switch to user app stacks here.
4570 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004571 final Uri data = intent.getData();
4572 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4573 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004574 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004575 checkEmbeddedAllowedInner(userId, intent, mimeType);
4576
4577 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4578 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004579 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004580 }
4581
4582 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004583 public final int startActivityIntentSender(IIntentSender intentSender)
4584 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004585 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4586
4587 if (!(intentSender instanceof PendingIntentRecord)) {
4588 throw new IllegalArgumentException("Bad PendingIntent object");
4589 }
4590
Craig Mautnerb9168362015-02-26 20:40:19 -08004591 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004592 Binder.getCallingUid(), mCurrentUser, false,
4593 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004594
4595 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4596 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4597 pendingIntent.key.requestResolvedType);
4598
4599 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4600 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4601 }
4602
4603 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004604 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004605 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004606 throw new SecurityException(
4607 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4608 }
4609 }
4610
Craig Mautnerdf88d732014-01-27 09:21:32 -08004611 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004612 public IBinder asBinder() {
4613 return this;
4614 }
4615
Craig Mautner4504de52013-12-20 09:06:56 -08004616 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004617 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004618 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004619 }
4620
Craig Mautner4a1cb222013-12-04 16:14:06 -08004621 ActivityStackSupervisor getOuter() {
4622 return ActivityStackSupervisor.this;
4623 }
4624
Craig Mautnerd163e752014-06-13 17:18:47 -07004625 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004626 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004627 }
4628
Craig Mautner6985bad2014-04-21 15:22:06 -07004629 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004630 void setVisible(boolean visible) {
4631 if (mVisible != visible) {
4632 mVisible = visible;
4633 if (mCallback != null) {
4634 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4635 0 /* unused */, this).sendToTarget();
4636 }
4637 }
4638 }
4639
Craig Mautner6985bad2014-04-21 15:22:06 -07004640 void setDrawn() {
4641 }
4642
Craig Mautner1b4bf852014-05-26 15:06:32 -07004643 // You can always start a new task on a regular ActivityStack.
4644 boolean isEligibleForNewTasks() {
4645 return true;
4646 }
4647
Craig Mautnerd163e752014-06-13 17:18:47 -07004648 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004649 detachLocked();
4650 deleteActivityContainer(this);
4651 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004652 }
4653
Craig Mautner34b73df2014-01-12 21:11:08 -08004654 @Override
4655 public String toString() {
4656 return mIdString + (mActivityDisplay == null ? "N" : "A");
4657 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004658 }
4659
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004660 private class VirtualActivityContainer extends ActivityContainer {
4661 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004662 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004663
4664 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4665 super(getNextStackId());
4666 mParentActivity = parent;
4667 mCallback = callback;
4668 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004669 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004670 }
4671
4672 @Override
4673 public void setSurface(Surface surface, int width, int height, int density) {
4674 super.setSurface(surface, width, height, density);
4675
4676 synchronized (mService) {
4677 final long origId = Binder.clearCallingIdentity();
4678 try {
4679 setSurfaceLocked(surface, width, height, density);
4680 } finally {
4681 Binder.restoreCallingIdentity(origId);
4682 }
4683 }
4684 }
4685
4686 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4687 if (mContainerState == CONTAINER_STATE_FINISHING) {
4688 return;
4689 }
4690 VirtualActivityDisplay virtualActivityDisplay =
4691 (VirtualActivityDisplay) mActivityDisplay;
4692 if (virtualActivityDisplay == null) {
4693 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004694 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004695 mActivityDisplay = virtualActivityDisplay;
4696 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004697 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004698 }
4699
4700 if (mSurface != null) {
4701 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004702 }
4703
Craig Mautner6985bad2014-04-21 15:22:06 -07004704 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004705 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004706 mStack.resumeTopActivityLocked(null);
4707 } else {
4708 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004709 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004710 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004711 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004712 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004713 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004714
Craig Mautnerd163e752014-06-13 17:18:47 -07004715 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004716
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004717 if (DEBUG_STACK) Slog.d(TAG_STACK,
4718 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004719 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004720
Craig Mautner6985bad2014-04-21 15:22:06 -07004721 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004722 boolean isAttachedLocked() {
4723 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004724 }
4725
4726 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004727 void setDrawn() {
4728 synchronized (mService) {
4729 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004730 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004731 }
4732 }
4733
Craig Mautner1b4bf852014-05-26 15:06:32 -07004734 // Never start a new task on an ActivityView if it isn't explicitly specified.
4735 @Override
4736 boolean isEligibleForNewTasks() {
4737 return false;
4738 }
4739
Craig Mautnerd163e752014-06-13 17:18:47 -07004740 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004741 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004742 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4743 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4744 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4745 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4746 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004747 }
4748 }
4749
Craig Mautner4a1cb222013-12-04 16:14:06 -08004750 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4751 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004752 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004753 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004754 int mDisplayId;
4755 Display mDisplay;
4756 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004757
Craig Mautner4a1cb222013-12-04 16:14:06 -08004758 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4759 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004760 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004761
Jose Lima4b6c6692014-08-12 17:41:12 -07004762 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004763
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004764 ActivityDisplay() {
4765 }
Craig Mautner4504de52013-12-20 09:06:56 -08004766
Craig Mautner1a70a162014-09-13 12:09:31 -07004767 // After instantiation, check that mDisplay is not null before using this. The alternative
4768 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004769 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004770 final Display display = mDisplayManager.getDisplay(displayId);
4771 if (display == null) {
4772 return;
4773 }
4774 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004775 }
4776
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004777 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004778 mDisplay = display;
4779 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004780 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004781 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004782
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004783 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004784 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004785 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4786 + " onTop=" + onTop);
4787 if (onTop) {
4788 mStacks.add(stack);
4789 } else {
4790 mStacks.add(0, stack);
4791 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004792 }
4793
Craig Mautnere0a38842013-12-16 16:14:02 -08004794 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004795 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004796 + " from displayId=" + mDisplayId);
4797 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004798 }
4799
Jose Lima4b6c6692014-08-12 17:41:12 -07004800 void setVisibleBehindActivity(ActivityRecord r) {
4801 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004802 }
4803
Jose Lima4b6c6692014-08-12 17:41:12 -07004804 boolean hasVisibleBehindActivity() {
4805 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004806 }
4807
Craig Mautner34b73df2014-01-12 21:11:08 -08004808 @Override
4809 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004810 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4811 }
4812 }
4813
4814 class VirtualActivityDisplay extends ActivityDisplay {
4815 VirtualDisplay mVirtualDisplay;
4816
Craig Mautner6985bad2014-04-21 15:22:06 -07004817 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004818 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004819 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4820 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4821 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4822 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004823
4824 init(mVirtualDisplay.getDisplay());
4825
4826 mWindowManager.handleDisplayAdded(mDisplayId);
4827 }
4828
4829 void setSurface(Surface surface) {
4830 if (mVirtualDisplay != null) {
4831 mVirtualDisplay.setSurface(surface);
4832 }
4833 }
4834
4835 @Override
4836 void detachActivitiesLocked(ActivityStack stack) {
4837 super.detachActivitiesLocked(stack);
4838 if (mVirtualDisplay != null) {
4839 mVirtualDisplay.release();
4840 mVirtualDisplay = null;
4841 }
4842 }
4843
4844 @Override
4845 public String toString() {
4846 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004847 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004848 }
Jose Lima58e66d62014-05-27 20:00:27 -07004849
4850 private boolean isLeanbackOnlyDevice() {
4851 boolean onLeanbackOnly = false;
4852 try {
4853 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4854 PackageManager.FEATURE_LEANBACK_ONLY);
4855 } catch (RemoteException e) {
4856 // noop
4857 }
4858
4859 return onLeanbackOnly;
4860 }
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004861
4862 /**
4863 * Adjust bounds to stay within stack bounds.
4864 *
4865 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4866 * that keep them unchanged, but be contained within the stack bounds.
4867 *
4868 * @param bounds Bounds to be adjusted.
4869 * @param stackBounds Bounds within which the other bounds should remain.
4870 */
4871 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4872 if (stackBounds == null || stackBounds.contains(bounds)) {
4873 return;
4874 }
4875
4876 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4877 final int maxRight = stackBounds.right
4878 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4879 int horizontalDiff = stackBounds.left - bounds.left;
4880 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4881 || (bounds.left + horizontalDiff >= maxRight)) {
4882 horizontalDiff = maxRight - bounds.left;
4883 }
4884 bounds.left += horizontalDiff;
4885 bounds.right += horizontalDiff;
4886 }
4887
4888 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4889 final int maxBottom = stackBounds.bottom
4890 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4891 int verticalDiff = stackBounds.top - bounds.top;
4892 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4893 || (bounds.top + verticalDiff >= maxBottom)) {
4894 verticalDiff = maxBottom - bounds.top;
4895 }
4896 bounds.top += verticalDiff;
4897 bounds.bottom += verticalDiff;
4898 }
4899 }
4900
Craig Mautner27084302013-03-25 08:05:25 -07004901}