blob: 1cd71a1cb61a3e653fc336bdf500e7dcf9612f47 [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
298 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700299 * We don't want to allow the device to go to sleep while in the process
300 * of launching an activity. This is primarily to allow alarm intent
301 * receivers to launch an activity and get that to run before the device
302 * goes back to sleep.
303 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700304 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700305
306 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700307 * Set when the system is going to sleep, until we have
308 * successfully paused the current activity and released our wake lock.
309 * At that point the system is allowed to actually sleep.
310 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700311 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700312
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700313 /** Stack id of the front stack when user switched, indexed by userId. */
314 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700315
Craig Mautner4504de52013-12-20 09:06:56 -0800316 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700318 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319
320 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700321 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322
Jeff Brownca9bc702014-02-11 14:32:56 -0800323 InputManagerInternal mInputManagerInternal;
324
Craig Mautner15df08a2015-04-01 12:17:18 -0700325 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
326 * may be finished until there is only one entry left. If this is empty the system is not
327 * in lockTask mode. */
328 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000329 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700330 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
331 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000332 */
333 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400334 /**
335 * Notifies the user when entering/exiting lock-task.
336 */
337 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800338
Craig Mautner15df08a2015-04-01 12:17:18 -0700339 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700340
Craig Mautner42d04db2014-11-06 12:13:23 -0800341 /** Used to keep resumeTopActivityLocked() from being entered recursively */
342 boolean inResumeTopActivity;
343
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700344 // 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 -0700345 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700346 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700347
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700348 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
349 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
350
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700351 // The default minimal size that will be used if the activity doesn't specify its minimal size.
352 // It will be calculated when the default display gets added.
353 private int mDefaultMinimalSizeOfResizeableTask = -1;
354
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700355 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
356 private boolean mTaskLayersChanged = true;
357
Craig Mautneree36c772014-07-16 14:56:05 -0700358 /**
359 * Description of a request to start a new activity, which has been held
360 * due to app switches being disabled.
361 */
362 static class PendingActivityLaunch {
363 final ActivityRecord r;
364 final ActivityRecord sourceRecord;
365 final int startFlags;
366 final ActivityStack stack;
367
368 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
369 int _startFlags, ActivityStack _stack) {
370 r = _r;
371 sourceRecord = _sourceRecord;
372 startFlags = _startFlags;
373 stack = _stack;
374 }
375 }
376
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800377 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700378 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800379 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700380 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
381 }
382
383 /**
384 * At the time when the constructor runs, the power manager has not yet been
385 * initialized. So we initialize our wakelocks afterwards.
386 */
387 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700389 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700390 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700391 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700392 }
393
justinzhang5286d3f2014-05-12 17:06:01 -0400394 // This function returns a IStatusBarService. The value is from ServiceManager.
395 // getService and is cached.
396 private IStatusBarService getStatusBarService() {
397 synchronized (mService) {
398 if (mStatusBarService == null) {
399 mStatusBarService = IStatusBarService.Stub.asInterface(
400 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
401 if (mStatusBarService == null) {
402 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
403 }
404 }
405 return mStatusBarService;
406 }
407 }
408
Jason Monk35c62a42014-06-17 10:24:47 -0400409 private IDevicePolicyManager getDevicePolicyManager() {
410 synchronized (mService) {
411 if (mDevicePolicyManager == null) {
412 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
413 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
414 if (mDevicePolicyManager == null) {
415 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
416 }
417 }
418 return mDevicePolicyManager;
419 }
420 }
421
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700422 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800423 synchronized (mService) {
424 mWindowManager = wm;
425
426 mDisplayManager =
427 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
428 mDisplayManager.registerDisplayListener(this, null);
429
430 Display[] displays = mDisplayManager.getDisplays();
431 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
432 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800433 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700434 if (activityDisplay.mDisplay == null) {
435 throw new IllegalStateException("Default Display does not exist");
436 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800437 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700438 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800439 }
440
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700441 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800442 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800443
444 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800445 }
Craig Mautner27084302013-03-25 08:05:25 -0700446 }
447
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200448 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700449 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200450 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700451 }
452
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700453 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800454 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700455 }
456
Craig Mautnerde4ef022013-04-07 19:01:33 -0700457 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800458 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700459 }
460
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700461 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700462 if (stack == null) {
463 return false;
464 }
465
Craig Mautnerdf88d732014-01-27 09:21:32 -0800466 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
467 if (parent != null) {
468 stack = parent.task.stack;
469 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800470 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700471 }
472
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700473 /** The top most stack. */
474 boolean isFrontStack(ActivityStack stack) {
475 if (stack == null) {
476 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800477 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700478
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700479 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
480 if (parent != null) {
481 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800482 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700483 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
484 }
485
486 void setFocusStack(String reason, ActivityStack focusedStack) {
487 mLastFocusedStack = mFocusedStack;
488 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800489
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700490 EventLogTags.writeAmFocusedStack(
491 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
492 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800493
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800494 if (mService.mBooting || !mService.mBooted) {
495 final ActivityRecord r = topRunningActivityLocked();
496 if (r != null && r.idle) {
497 checkFinishBootingLocked();
498 }
499 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700500 }
501
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700502 void moveHomeStackToFront(String reason) {
503 mHomeStack.moveToFront(reason);
504 }
505
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700506 /** Returns true if the focus activity was adjusted to the home stack top activity. */
507 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700508 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
509 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700510 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700511 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700512
Craig Mautner84984fa2014-06-19 11:19:20 -0700513 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700514
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700515 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700516 if (top == null) {
517 return false;
518 }
519 mService.setFocusedActivityLocked(top, reason);
520 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700521 }
522
Craig Mautner299f9602015-01-26 09:47:33 -0800523 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700524 if (!mService.mBooting && !mService.mBooted) {
525 // Not ready yet!
526 return false;
527 }
528
Craig Mautner84984fa2014-06-19 11:19:20 -0700529 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
530 mWindowManager.showRecentApps();
531 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700532 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700533
Craig Mautner84984fa2014-06-19 11:19:20 -0700534 if (prev != null) {
535 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
536 }
537
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700538 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
539 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800540 // Only resume home activity if isn't finishing.
541 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800542 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700543 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700544 }
Craig Mautner299f9602015-01-26 09:47:33 -0800545 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700546 }
547
Craig Mautner8d341ef2013-03-26 09:03:27 -0700548 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700549 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700550 }
551
552 /**
553 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
554 * @param id Id of the task we would like returned.
555 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
556 * restore the task from recents to the active list.
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700557 * @param stackId The stack to restore the task to (default launch stack will be used
558 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700559 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700560 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800561 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800563 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
565 ActivityStack stack = stacks.get(stackNdx);
566 TaskRecord task = stack.taskForIdLocked(id);
567 if (task != null) {
568 return task;
569 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700570 }
571 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800572
573 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700574 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800575 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800576 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700577 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800578 return null;
579 }
580
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700581 if (!restoreFromRecents) {
582 return task;
583 }
584
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700585 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700586 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
587 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800588 return null;
589 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800591 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700592 }
593
Craig Mautner6170f732013-04-02 13:05:23 -0700594 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800595 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800596 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800597 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
599 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
600 if (r != null) {
601 return r;
602 }
Craig Mautner6170f732013-04-02 13:05:23 -0700603 }
604 }
605 return null;
606 }
607
Craig Mautneref73ee12014-04-23 11:45:37 -0700608 void setNextTaskId(int taskId) {
609 if (taskId > mCurTaskId) {
610 mCurTaskId = taskId;
611 }
612 }
613
Craig Mautner8d341ef2013-03-26 09:03:27 -0700614 int getNextTaskId() {
615 do {
616 mCurTaskId++;
617 if (mCurTaskId <= 0) {
618 mCurTaskId = 1;
619 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700620 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700621 return mCurTaskId;
622 }
623
Craig Mautnerde4ef022013-04-07 19:01:33 -0700624 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800625 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700626 if (stack == null) {
627 return null;
628 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700629 ActivityRecord resumedActivity = stack.mResumedActivity;
630 if (resumedActivity == null || resumedActivity.app == null) {
631 resumedActivity = stack.mPausingActivity;
632 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700633 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700634 }
635 }
636 return resumedActivity;
637 }
638
Dianne Hackbornff072722014-09-24 10:56:28 -0700639 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700640 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800642 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
643 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800644 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
645 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700646 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647 continue;
648 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700649 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650 if (hr != null) {
651 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
652 && processName.equals(hr.processName)) {
653 try {
George Mount2c92c972014-03-20 09:38:23 -0700654 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 didSomething = true;
656 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700657 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800658 Slog.w(TAG, "Exception in new application when starting activity "
659 + hr.intent.getComponent().flattenToShortString(), e);
660 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700661 }
Craig Mautner20e72272013-04-01 13:45:53 -0700662 }
Craig Mautner20e72272013-04-01 13:45:53 -0700663 }
664 }
665 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700666 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700667 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700668 }
Craig Mautner20e72272013-04-01 13:45:53 -0700669 return didSomething;
670 }
671
672 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
674 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
676 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700677 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 continue;
679 }
680 final ActivityRecord resumedActivity = stack.mResumedActivity;
681 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700682 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800683 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800684 return false;
685 }
Craig Mautner20e72272013-04-01 13:45:53 -0700686 }
687 }
688 return true;
689 }
690
Craig Mautnerde4ef022013-04-07 19:01:33 -0700691 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800692 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
693 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800694 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
695 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700696 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800697 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700698 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800699 return false;
700 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700701 }
702 }
703 }
704 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700705 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
707 mLastFocusedStack + " to=" + mFocusedStack);
708 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700709 return true;
710 }
711
712 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800713 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800714 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
715 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800716 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
717 final ActivityStack stack = stacks.get(stackNdx);
718 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800719 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700720 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800721 return false;
722 }
723 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700725 }
726 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800727 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700728 }
729
Craig Mautner2acc3892013-09-23 10:28:14 -0700730 /**
731 * Pause all activities in either all of the stacks or just the back stacks.
732 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700733 * @return true if any activity was paused as a result of this call.
734 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700735 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700736 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
738 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800739 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
740 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700741 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700742 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700744 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
745 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800746 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700747 }
748 }
749 return someActivityPaused;
750 }
751
Craig Mautnerde4ef022013-04-07 19:01:33 -0700752 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700753 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800754 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
755 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
757 final ActivityStack stack = stacks.get(stackNdx);
758 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700759 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800760 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700761 Slog.d(TAG_STATES,
762 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800763 pausing = false;
764 } else {
765 return false;
766 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700767 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700768 }
769 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700770 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700771 }
772
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700773 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
774 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500775 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800776 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
777 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
778 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
779 final ActivityStack stack = stacks.get(stackNdx);
780 if (stack.mResumedActivity != null &&
781 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700782 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800783 }
784 }
785 }
786 }
787
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700788 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700789 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700790 }
791
792 void sendWaitingVisibleReportLocked(ActivityRecord r) {
793 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700794 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700795 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700796 if (w.who == null) {
797 changed = true;
798 w.timeout = false;
799 if (r != null) {
800 w.who = new ComponentName(r.info.packageName, r.info.name);
801 }
802 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
803 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700804 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700805 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700806 if (changed) {
807 mService.notifyAll();
808 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700809 }
810
811 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
812 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700813 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700814 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700815 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700816 if (w.who == null) {
817 changed = true;
818 w.timeout = timeout;
819 if (r != null) {
820 w.who = new ComponentName(r.info.packageName, r.info.name);
821 }
822 w.thisTime = thisTime;
823 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700824 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700825 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700826 if (changed) {
827 mService.notifyAll();
828 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700829 }
830
Craig Mautner29219d92013-04-16 20:19:12 -0700831 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800832 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700833 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700834 if (r != null) {
835 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700836 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700837
Craig Mautner4a1cb222013-12-04 16:14:06 -0800838 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800839 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
841 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700842 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700843 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700844 if (r != null) {
845 return r;
846 }
847 }
848 }
849 return null;
850 }
851
Dianne Hackborn09233282014-04-30 11:33:59 -0700852 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700853 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800854 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
855 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800856 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800857 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800858 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800859 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
860 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700861 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700863 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700864 }
865 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700866
867 // The lists are already sorted from most recent to oldest. Just pull the most recent off
868 // each list and add it to list. Stop when all lists are empty or maxNum reached.
869 while (maxNum > 0) {
870 long mostRecentActiveTime = Long.MIN_VALUE;
871 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 final int numTaskLists = runningTaskLists.size();
873 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
874 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700875 if (!stackTaskList.isEmpty()) {
876 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
877 if (lastActiveTime > mostRecentActiveTime) {
878 mostRecentActiveTime = lastActiveTime;
879 selectedStackList = stackTaskList;
880 }
881 }
882 }
883 if (selectedStackList != null) {
884 list.add(selectedStackList.remove(0));
885 --maxNum;
886 } else {
887 break;
888 }
889 }
Craig Mautner20e72272013-04-01 13:45:53 -0700890 }
891
Craig Mautner23ac33b2013-04-01 16:26:35 -0700892 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700893 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894 // Collect information about the target of the Intent.
895 ActivityInfo aInfo;
896 try {
897 ResolveInfo rInfo =
898 AppGlobals.getPackageManager().resolveIntent(
899 intent, resolvedType,
900 PackageManager.MATCH_DEFAULT_ONLY
901 | ActivityManagerService.STOCK_PM_FLAGS, userId);
902 aInfo = rInfo != null ? rInfo.activityInfo : null;
903 } catch (RemoteException e) {
904 aInfo = null;
905 }
906
907 if (aInfo != null) {
908 // Store the found target back into the intent, because now that
909 // we have it we never want to do this again. For example, if the
910 // user navigates back to this point in the history, we should
911 // always restart the exact same activity.
912 intent.setComponent(new ComponentName(
913 aInfo.applicationInfo.packageName, aInfo.name));
914
915 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700916 if (!aInfo.processName.equals("system")) {
917 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700918 mService.setDebugApp(aInfo.processName, true, false);
919 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700920
Man Caocfa78b22015-06-11 20:14:34 -0700921 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
922 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
923 }
924
925 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700926 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700927 }
928 }
929 }
930 return aInfo;
931 }
932
Craig Mautner299f9602015-01-26 09:47:33 -0800933 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
934 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700935 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
936 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
937 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
938 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700939 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
940 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700941 null /* outActivity */, null /* container */, null /* inTask */);
942 if (inResumeTopActivity) {
943 // If we are in resume section already, home activity will be initialized, but not
944 // resumed (to avoid recursive resume) and will stay that way until something pokes it
945 // again. We need to schedule another resume.
946 scheduleResumeTopActivities();
947 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700948 }
949
Craig Mautner23ac33b2013-04-01 16:26:35 -0700950 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700951 String callingPackage, Intent intent, String resolvedType,
952 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700953 IBinder resultTo, String resultWho, int requestCode, int startFlags,
954 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700955 Bundle options, boolean ignoreTargetSecurity, int userId,
956 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700957 // Refuse possible leaked file descriptors
958 if (intent != null && intent.hasFileDescriptors()) {
959 throw new IllegalArgumentException("File descriptors passed in Intent");
960 }
961 boolean componentSpecified = intent.getComponent() != null;
962
963 // Don't modify the client's object!
964 intent = new Intent(intent);
965
966 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700967 ActivityInfo aInfo =
968 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700969
Craig Mautnere0a38842013-12-16 16:14:02 -0800970 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700971 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800972 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700973 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800974 // Cannot start a child activity if the parent is not resumed.
975 return ActivityManager.START_CANCELED;
976 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700977 final int realCallingPid = Binder.getCallingPid();
978 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700979 int callingPid;
980 if (callingUid >= 0) {
981 callingPid = -1;
982 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700983 callingPid = realCallingPid;
984 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985 } else {
986 callingPid = callingUid = -1;
987 }
988
Craig Mautnere0a38842013-12-16 16:14:02 -0800989 final ActivityStack stack;
990 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800991 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800992 } else {
993 stack = container.mStack;
994 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800995 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700996 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700997 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700998
999 final long origId = Binder.clearCallingIdentity();
1000
1001 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001002 (aInfo.applicationInfo.privateFlags
1003 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001004 // This may be a heavy-weight process! Check to see if we already
1005 // have another, different heavy-weight process running.
1006 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1007 if (mService.mHeavyWeightProcess != null &&
1008 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1009 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001010 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001011 if (caller != null) {
1012 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1013 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001014 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001015 } else {
1016 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001017 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001018 + intent.toString());
1019 ActivityOptions.abort(options);
1020 return ActivityManager.START_PERMISSION_DENIED;
1021 }
1022 }
1023
1024 IIntentSender target = mService.getIntentSenderLocked(
1025 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001026 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001027 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1028 | PendingIntent.FLAG_ONE_SHOT, null);
1029
1030 Intent newIntent = new Intent();
1031 if (requestCode >= 0) {
1032 // Caller is requesting a result.
1033 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1034 }
1035 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1036 new IntentSender(target));
1037 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1038 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1039 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1040 hist.packageName);
1041 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1042 hist.task.taskId);
1043 }
1044 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1045 aInfo.packageName);
1046 newIntent.setFlags(intent.getFlags());
1047 newIntent.setClassName("android",
1048 HeavyWeightSwitcherActivity.class.getName());
1049 intent = newIntent;
1050 resolvedType = null;
1051 caller = null;
1052 callingUid = Binder.getCallingUid();
1053 callingPid = Binder.getCallingPid();
1054 componentSpecified = true;
1055 try {
1056 ResolveInfo rInfo =
1057 AppGlobals.getPackageManager().resolveIntent(
1058 intent, null,
1059 PackageManager.MATCH_DEFAULT_ONLY
1060 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1061 aInfo = rInfo != null ? rInfo.activityInfo : null;
1062 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1063 } catch (RemoteException e) {
1064 aInfo = null;
1065 }
1066 }
1067 }
1068 }
1069
Dianne Hackborn91097de2014-04-04 18:02:06 -07001070 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1071 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001072 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001073 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001074 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001075
Craig Mautner85c11a82014-07-17 12:38:18 -07001076 Binder.restoreCallingIdentity(origId);
1077
Craig Mautnerde4ef022013-04-07 19:01:33 -07001078 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001079 // If the caller also wants to switch to a new configuration,
1080 // do so now. This allows a clean switch, as we are waiting
1081 // for the current activity to pause (so we will not destroy
1082 // it), and have not yet started the next activity.
1083 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1084 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001085 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001086 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001087 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001088 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001089 }
1090
Craig Mautner23ac33b2013-04-01 16:26:35 -07001091 if (outResult != null) {
1092 outResult.result = res;
1093 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001094 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 do {
1096 try {
1097 mService.wait();
1098 } catch (InterruptedException e) {
1099 }
1100 } while (!outResult.timeout && outResult.who == null);
1101 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001102 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001103 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001104 outResult.timeout = false;
1105 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1106 outResult.totalTime = 0;
1107 outResult.thisTime = 0;
1108 } else {
1109 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001110 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001111 do {
1112 try {
1113 mService.wait();
1114 } catch (InterruptedException e) {
1115 }
1116 } while (!outResult.timeout && outResult.who == null);
1117 }
1118 }
1119 }
1120
1121 return res;
1122 }
1123 }
1124
1125 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1126 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1127 Bundle options, int userId) {
1128 if (intents == null) {
1129 throw new NullPointerException("intents is null");
1130 }
1131 if (resolvedTypes == null) {
1132 throw new NullPointerException("resolvedTypes is null");
1133 }
1134 if (intents.length != resolvedTypes.length) {
1135 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1136 }
1137
Craig Mautner23ac33b2013-04-01 16:26:35 -07001138
1139 int callingPid;
1140 if (callingUid >= 0) {
1141 callingPid = -1;
1142 } else if (caller == null) {
1143 callingPid = Binder.getCallingPid();
1144 callingUid = Binder.getCallingUid();
1145 } else {
1146 callingPid = callingUid = -1;
1147 }
1148 final long origId = Binder.clearCallingIdentity();
1149 try {
1150 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001151 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001152 for (int i=0; i<intents.length; i++) {
1153 Intent intent = intents[i];
1154 if (intent == null) {
1155 continue;
1156 }
1157
1158 // Refuse possible leaked file descriptors
1159 if (intent != null && intent.hasFileDescriptors()) {
1160 throw new IllegalArgumentException("File descriptors passed in Intent");
1161 }
1162
1163 boolean componentSpecified = intent.getComponent() != null;
1164
1165 // Don't modify the client's object!
1166 intent = new Intent(intent);
1167
1168 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001169 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001170 // TODO: New, check if this is correct
1171 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1172
1173 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001174 (aInfo.applicationInfo.privateFlags
1175 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001176 throw new IllegalArgumentException(
1177 "FLAG_CANT_SAVE_STATE not supported here");
1178 }
1179
1180 Bundle theseOptions;
1181 if (options != null && i == intents.length-1) {
1182 theseOptions = options;
1183 } else {
1184 theseOptions = null;
1185 }
Craig Mautner6170f732013-04-02 13:05:23 -07001186 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001187 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1188 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001189 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001190 if (res < 0) {
1191 return res;
1192 }
1193
1194 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1195 }
1196 }
1197 } finally {
1198 Binder.restoreCallingIdentity(origId);
1199 }
1200
1201 return ActivityManager.START_SUCCESS;
1202 }
1203
Craig Mautner2420ead2013-04-01 17:13:20 -07001204 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001205 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001206 throws RemoteException {
1207
Craig Mautner2568c3a2015-03-26 14:22:34 -07001208 if (andResume) {
1209 r.startFreezingScreenLocked(app, 0);
1210 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001211
Craig Mautner2568c3a2015-03-26 14:22:34 -07001212 // schedule launch ticks to collect information about slow apps.
1213 r.startLaunchTickingLocked();
1214 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001215
1216 // Have the window manager re-evaluate the orientation of
1217 // the screen based on the new activity order. Note that
1218 // as a result of this, it can call back into the activity
1219 // manager with a new orientation. We don't care about that,
1220 // because the activity is not currently running so we are
1221 // just restarting it anyway.
1222 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001223 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001224 mService.mConfiguration,
1225 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001226 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001227 }
1228
1229 r.app = app;
1230 app.waitingToKill = null;
1231 r.launchCount++;
1232 r.lastLaunchTime = SystemClock.uptimeMillis();
1233
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001234 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001235
1236 int idx = app.activities.indexOf(r);
1237 if (idx < 0) {
1238 app.activities.add(r);
1239 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001240 mService.updateLruProcessLocked(app, true, null);
1241 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001242
Craig Mautner15df08a2015-04-01 12:17:18 -07001243 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001244 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1245 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001246 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001247 }
1248
1249 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001250 try {
1251 if (app.thread == null) {
1252 throw new RemoteException();
1253 }
1254 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001255 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001256 if (andResume) {
1257 results = r.results;
1258 newIntents = r.newIntents;
1259 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001260 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1261 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1262 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001263 if (andResume) {
1264 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1265 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001266 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001268 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001269 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001270 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001271 }
1272 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1273 r.sleeping = false;
1274 r.forceNewConfig = false;
1275 mService.showAskCompatModeDialogLocked(r);
1276 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001277 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001278 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1279 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1280 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001281 final String profileFile = mService.mProfileFile;
1282 if (profileFile != null) {
1283 ParcelFileDescriptor profileFd = mService.mProfileFd;
1284 if (profileFd != null) {
1285 try {
1286 profileFd = profileFd.dup();
1287 } catch (IOException e) {
1288 if (profileFd != null) {
1289 try {
1290 profileFd.close();
1291 } catch (IOException o) {
1292 }
1293 profileFd = null;
1294 }
1295 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001297
1298 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1299 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 }
1301 }
1302 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001303
Craig Mautner2568c3a2015-03-26 14:22:34 -07001304 if (andResume) {
1305 app.hasShownUi = true;
1306 app.pendingUiClean = true;
1307 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001308 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001309 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001310 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001311 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001312 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001313 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001314
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001315 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001316 // This may be a heavy-weight process! Note that the package
1317 // manager will ensure that only activity can run in the main
1318 // process of the .apk, which is the only thing that will be
1319 // considered heavy-weight.
1320 if (app.processName.equals(app.info.packageName)) {
1321 if (mService.mHeavyWeightProcess != null
1322 && mService.mHeavyWeightProcess != app) {
1323 Slog.w(TAG, "Starting new heavy weight process " + app
1324 + " when already running "
1325 + mService.mHeavyWeightProcess);
1326 }
1327 mService.mHeavyWeightProcess = app;
1328 Message msg = mService.mHandler.obtainMessage(
1329 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1330 msg.obj = r;
1331 mService.mHandler.sendMessage(msg);
1332 }
1333 }
1334
1335 } catch (RemoteException e) {
1336 if (r.launchFailed) {
1337 // This is the second time we failed -- finish activity
1338 // and give up.
1339 Slog.e(TAG, "Second failure launching "
1340 + r.intent.getComponent().flattenToShortString()
1341 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001342 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001343 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1344 "2nd-crash", false);
1345 return false;
1346 }
1347
1348 // This is the first time we failed -- restart process and
1349 // retry.
1350 app.activities.remove(r);
1351 throw e;
1352 }
1353
1354 r.launchFailed = false;
1355 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001356 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001357 }
1358
1359 if (andResume) {
1360 // As part of the process of launching, ActivityThread also performs
1361 // a resume.
1362 stack.minimalResumeActivityLocked(r);
1363 } else {
1364 // This activity is not starting in the resumed state... which
1365 // should look like we asked it to pause+stop (but remain visible),
1366 // and it has done so and reported back the current icicle and
1367 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001368 if (DEBUG_STATES) Slog.v(TAG_STATES,
1369 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001370 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001371 r.stopped = true;
1372 }
1373
1374 // Launch the new version setup screen if needed. We do this -after-
1375 // launching the initial activity (that is, home), so that it can have
1376 // a chance to initialize itself while in the background, making the
1377 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001378 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 mService.startSetupActivityLocked();
1380 }
1381
Dianne Hackborn465fa392014-09-14 14:21:18 -07001382 // Update any services we are bound to that might care about whether
1383 // their client may have activities.
1384 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1385
Craig Mautner2420ead2013-04-01 17:13:20 -07001386 return true;
1387 }
1388
Craig Mautnere79d42682013-04-01 19:01:53 -07001389 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001390 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001391 // Is this activity's application already running?
1392 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001393 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001394
1395 r.task.stack.setLaunchTime(r);
1396
1397 if (app != null && app.thread != null) {
1398 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001399 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1400 || !"android".equals(r.info.packageName)) {
1401 // Don't add this if it is a platform component that is marked
1402 // to run in multiple processes, because this is actually
1403 // part of the framework so doesn't make sense to track as a
1404 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001405 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1406 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001407 }
George Mount2c92c972014-03-20 09:38:23 -07001408 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001409 return;
1410 } catch (RemoteException e) {
1411 Slog.w(TAG, "Exception when starting activity "
1412 + r.intent.getComponent().flattenToShortString(), e);
1413 }
1414
1415 // If a dead object exception was thrown -- fall through to
1416 // restart the application.
1417 }
1418
1419 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001420 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001421 }
1422
Craig Mautner6170f732013-04-02 13:05:23 -07001423 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001424 Intent intent, String resolvedType, ActivityInfo aInfo,
1425 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1426 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001427 int callingPid, int callingUid, String callingPackage,
1428 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001429 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1430 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001431 int err = ActivityManager.START_SUCCESS;
1432
1433 ProcessRecord callerApp = null;
1434 if (caller != null) {
1435 callerApp = mService.getRecordForAppLocked(caller);
1436 if (callerApp != null) {
1437 callingPid = callerApp.pid;
1438 callingUid = callerApp.info.uid;
1439 } else {
1440 Slog.w(TAG, "Unable to find app for caller " + caller
1441 + " (pid=" + callingPid + ") when starting: "
1442 + intent.toString());
1443 err = ActivityManager.START_PERMISSION_DENIED;
1444 }
1445 }
1446
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001447 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1448
Craig Mautner6170f732013-04-02 13:05:23 -07001449 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001450 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001451 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001452 + " on display " + (container == null ? (mFocusedStack == null ?
1453 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1454 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1455 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001456 }
1457
1458 ActivityRecord sourceRecord = null;
1459 ActivityRecord resultRecord = null;
1460 if (resultTo != null) {
1461 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001462 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1463 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001464 if (sourceRecord != null) {
1465 if (requestCode >= 0 && !sourceRecord.finishing) {
1466 resultRecord = sourceRecord;
1467 }
1468 }
1469 }
Craig Mautner6170f732013-04-02 13:05:23 -07001470
Dianne Hackborn91097de2014-04-04 18:02:06 -07001471 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001472
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001473 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001474 // Transfer the result target from the source activity to the new
1475 // one being started, including any failures.
1476 if (requestCode >= 0) {
1477 ActivityOptions.abort(options);
1478 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1479 }
1480 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001481 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1482 resultRecord = null;
1483 }
Craig Mautner6170f732013-04-02 13:05:23 -07001484 resultWho = sourceRecord.resultWho;
1485 requestCode = sourceRecord.requestCode;
1486 sourceRecord.resultTo = null;
1487 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001488 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001489 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001490 if (sourceRecord.launchedFromUid == callingUid) {
1491 // The new activity is being launched from the same uid as the previous
1492 // activity in the flow, and asking to forward its result back to the
1493 // previous. In this case the activity is serving as a trampoline between
1494 // the two, so we also want to update its launchedFromPackage to be the
1495 // same as the previous activity. Note that this is safe, since we know
1496 // these two packages come from the same uid; the caller could just as
1497 // well have supplied that same package name itself. This specifially
1498 // deals with the case of an intent picker/chooser being launched in the app
1499 // flow to redirect to an activity picked by the user, where we want the final
1500 // activity to consider it to have been launched by the previous app activity.
1501 callingPackage = sourceRecord.launchedFromPackage;
1502 }
Craig Mautner6170f732013-04-02 13:05:23 -07001503 }
1504
1505 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1506 // We couldn't find a class that can handle the given Intent.
1507 // That's the end of that!
1508 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1509 }
1510
1511 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1512 // We couldn't find the specific class specified in the Intent.
1513 // Also the end of the line.
1514 err = ActivityManager.START_CLASS_NOT_FOUND;
1515 }
1516
Dianne Hackborn91097de2014-04-04 18:02:06 -07001517 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1518 && sourceRecord.task.voiceSession != null) {
1519 // If this activity is being launched as part of a voice session, we need
1520 // to ensure that it is safe to do so. If the upcoming activity will also
1521 // be part of the voice session, we can only launch it if it has explicitly
1522 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001523 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001524 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1525 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001526 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001527 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1528 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001529 Slog.w(TAG,
1530 "Activity being started in current voice task does not support voice: "
1531 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001532 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1533 }
1534 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001535 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001536 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1537 }
1538 }
1539 }
1540
1541 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1542 // If the caller is starting a new voice session, just make sure the target
1543 // is actually allowing it to run this way.
1544 try {
1545 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1546 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001547 Slog.w(TAG,
1548 "Activity being started in new voice task does not support: "
1549 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001550 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1551 }
1552 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001553 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001554 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1555 }
1556 }
1557
louis_changcd5d1982014-08-01 10:09:08 +08001558 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1559
Craig Mautner6170f732013-04-02 13:05:23 -07001560 if (err != ActivityManager.START_SUCCESS) {
1561 if (resultRecord != null) {
1562 resultStack.sendActivityResultLocked(-1,
1563 resultRecord, resultWho, requestCode,
1564 Activity.RESULT_CANCELED, null);
1565 }
Craig Mautner6170f732013-04-02 13:05:23 -07001566 ActivityOptions.abort(options);
1567 return err;
1568 }
1569
Svetoslav7008b512015-06-24 18:47:07 -07001570 boolean abort = false;
1571
Svet Ganov99b60432015-06-27 13:15:22 -07001572 final int startAnyPerm = mService.checkPermission(
1573 START_ANY_ACTIVITY, callingPid, callingUid);
1574
1575 if (startAnyPerm != PERMISSION_GRANTED) {
1576 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001577 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001578 final int actionRestriction = getActionRestrictionForCallingPackage(
1579 intent.getAction(), callingPackage, callingPid, callingUid);
1580
1581 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1582 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1583 if (resultRecord != null) {
1584 resultStack.sendActivityResultLocked(-1,
1585 resultRecord, resultWho, requestCode,
1586 Activity.RESULT_CANCELED, null);
1587 }
1588 String msg;
1589 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1590 msg = "Permission Denial: starting " + intent.toString()
1591 + " from " + callerApp + " (pid=" + callingPid
1592 + ", uid=" + callingUid + ")" + " with revoked permission "
1593 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1594 } else if (!aInfo.exported) {
1595 msg = "Permission Denial: starting " + intent.toString()
1596 + " from " + callerApp + " (pid=" + callingPid
1597 + ", uid=" + callingUid + ")"
1598 + " not exported from uid " + aInfo.applicationInfo.uid;
1599 } else {
1600 msg = "Permission Denial: starting " + intent.toString()
1601 + " from " + callerApp + " (pid=" + callingPid
1602 + ", uid=" + callingUid + ")"
1603 + " requires " + aInfo.permission;
1604 }
1605 Slog.w(TAG, msg);
1606 throw new SecurityException(msg);
1607 }
1608
1609 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1610 String message = "Appop Denial: starting " + intent.toString()
1611 + " from " + callerApp + " (pid=" + callingPid
1612 + ", uid=" + callingUid + ")"
1613 + " requires " + AppOpsManager.permissionToOp(
1614 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1615 Slog.w(TAG, message);
1616 abort = true;
1617 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1618 String message = "Appop Denial: starting " + intent.toString()
1619 + " from " + callerApp + " (pid=" + callingPid
1620 + ", uid=" + callingUid + ")"
1621 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1622 Slog.w(TAG, message);
1623 abort = true;
1624 }
Svetoslav7008b512015-06-24 18:47:07 -07001625 }
1626
1627 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001628 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001629
Craig Mautner6170f732013-04-02 13:05:23 -07001630 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001631 try {
1632 // The Intent we give to the watcher has the extra data
1633 // stripped off, since it can contain private information.
1634 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001635 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001636 aInfo.applicationInfo.packageName);
1637 } catch (RemoteException e) {
1638 mService.mController = null;
1639 }
Ben Gruver5e207332013-04-03 17:41:37 -07001640 }
Craig Mautner6170f732013-04-02 13:05:23 -07001641
Ben Gruver5e207332013-04-03 17:41:37 -07001642 if (abort) {
1643 if (resultRecord != null) {
1644 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001645 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001646 }
Ben Gruver5e207332013-04-03 17:41:37 -07001647 // We pretend to the caller that it was really started, but
1648 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001649 ActivityOptions.abort(options);
1650 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001651 }
1652
1653 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001654 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001655 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001656 if (outActivity != null) {
1657 outActivity[0] = r;
1658 }
1659
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001660 if (r.appTimeTracker == null && sourceRecord != null) {
1661 // If the caller didn't specify an explicit time tracker, we want to continue
1662 // tracking under any it has.
1663 r.appTimeTracker = sourceRecord.appTimeTracker;
1664 }
1665
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001666 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001667 if (voiceSession == null && (stack.mResumedActivity == null
1668 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001669 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1670 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001671 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001672 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001673 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001674 ActivityOptions.abort(options);
1675 return ActivityManager.START_SWITCHES_CANCELED;
1676 }
1677 }
1678
1679 if (mService.mDidAppSwitch) {
1680 // This is the second allowed switch since we stopped switches,
1681 // so now just generally allow switches. Use case: user presses
1682 // home (switches disabled, switch to home, mDidAppSwitch now true);
1683 // user taps a home icon (coming from home so allowed, we hit here
1684 // and now allow anyone to switch again).
1685 mService.mAppSwitchesAllowedTime = 0;
1686 } else {
1687 mService.mDidAppSwitch = true;
1688 }
1689
Craig Mautneree36c772014-07-16 14:56:05 -07001690 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001691
Dianne Hackborn91097de2014-04-04 18:02:06 -07001692 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001693 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001694
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001695 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001696 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001697 // activity start, but we are not actually doing an activity
1698 // switch... just dismiss the keyguard now, because we
1699 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001700 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001701 }
1702 return err;
1703 }
1704
Svet Ganov99b60432015-06-27 13:15:22 -07001705 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001706 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001707 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1708 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001709 == PackageManager.PERMISSION_DENIED) {
1710 return ACTIVITY_RESTRICTION_PERMISSION;
1711 }
1712
Christopher Tateff7add02015-08-17 10:23:22 -07001713 if (activityInfo.permission == null) {
1714 return ACTIVITY_RESTRICTION_NONE;
1715 }
1716
Svet Ganov99b60432015-06-27 13:15:22 -07001717 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1718 if (opCode == AppOpsManager.OP_NONE) {
1719 return ACTIVITY_RESTRICTION_NONE;
1720 }
1721
1722 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1723 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001724 if (!ignoreTargetSecurity) {
1725 return ACTIVITY_RESTRICTION_APPOP;
1726 }
Svet Ganov99b60432015-06-27 13:15:22 -07001727 }
1728
1729 return ACTIVITY_RESTRICTION_NONE;
1730 }
1731
Svetoslav7008b512015-06-24 18:47:07 -07001732 private int getActionRestrictionForCallingPackage(String action,
1733 String callingPackage, int callingPid, int callingUid) {
1734 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001735 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001736 }
1737
1738 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1739 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001740 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001741 }
1742
1743 final PackageInfo packageInfo;
1744 try {
1745 packageInfo = mService.mContext.getPackageManager()
1746 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1747 } catch (PackageManager.NameNotFoundException e) {
1748 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001749 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001750 }
1751
1752 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001753 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001754 }
1755
1756 if (mService.checkPermission(permission, callingPid, callingUid) ==
1757 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001758 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001759 }
1760
1761 final int opCode = AppOpsManager.permissionToOpCode(permission);
1762 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001763 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001764 }
1765
1766 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1767 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001768 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001769 }
1770
Svet Ganov99b60432015-06-27 13:15:22 -07001771 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001772 }
1773
Chong Zhang0fa656b2015-08-31 15:17:21 -07001774 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001775 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001776
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001777 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1778 return mHomeStack;
1779 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001780
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001781 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001782
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001783 if (task != null && task.stack != null) {
1784 stack = task.stack;
1785 if (stack.isOnHomeDisplay()) {
1786 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001787 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001788 "computeStackFocus: Setting " + "focused stack to r=" + r
1789 + " task=" + task);
1790 } else {
1791 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1792 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001793 }
1794 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001795 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001796 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001797
1798 final ActivityContainer container = r.mInitialActivityContainer;
1799 if (container != null) {
1800 // The first time put it on the desired stack, after this put on task stack.
1801 r.mInitialActivityContainer = null;
1802 return container.mStack;
1803 }
1804
1805 // The fullscreen stack can contain any task regardless of if the task is resizeable
1806 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1807 // If the freeform stack has focus, and the activity to be launched is resizeable,
1808 // we can also put it in the focused stack.
1809 final boolean canUseFocusedStack =
1810 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1811 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1812 if (canUseFocusedStack
1813 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1814 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1815 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1816 return mFocusedStack;
1817 }
1818
1819 // We first try to put the task in the first dynamic stack.
1820 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1821 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1822 stack = homeDisplayStacks.get(stackNdx);
1823 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1824 if (isDynamicStack) {
1825 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1826 "computeStackFocus: Setting focused stack=" + stack);
1827 return stack;
1828 }
1829 }
1830
1831 // If there is no suitable dynamic stack then we figure out which static stack to use.
1832 final int stackId = task != null ? task.getLaunchStackId() :
1833 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1834 FULLSCREEN_WORKSPACE_STACK_ID;
1835 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1836 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1837 + r + " stackId=" + stack.mStackId);
1838 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001839 }
1840
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001841 boolean setFocusedStack(ActivityRecord r, String reason) {
1842 if (r == null) {
1843 // Not sure what you are trying to do, but it is not going to work...
1844 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001845 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001846 final TaskRecord task = r.task;
1847 if (task == null || task.stack == null) {
1848 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1849 return false;
1850 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001851 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001852 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001853 }
1854
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001855 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001856 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001857 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001858 final Intent intent = r.intent;
1859 final int callingUid = r.launchedFromUid;
1860
Chong Zhang0fa656b2015-08-31 15:17:21 -07001861 boolean overrideBounds = false;
1862 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001863 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
1864 ActivityOptions opts = new ActivityOptions(options);
1865 if (opts.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001866 overrideBounds = true;
Chong Zhang56f171d2015-10-08 10:39:23 -07001867 newBounds = opts.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001868 }
1869 }
1870
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001871 // In some flows in to this function, we retrieve the task record and hold on to it
1872 // without a lock before calling back in to here... so the task at this point may
1873 // not actually be in recents. Check for that, and if it isn't in recents just
1874 // consider it invalid.
1875 if (inTask != null && !inTask.inRecents) {
1876 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1877 inTask = null;
1878 }
1879
Craig Mautnerad400af2014-08-13 16:41:36 -07001880 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001881 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1882 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001883
Craig Mautnera228ae92014-07-09 05:44:55 -07001884 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001885 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001886 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001887 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1888 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1889 "\"singleInstance\" or \"singleTask\"");
1890 launchFlags &=
1891 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1892 } else {
1893 switch (r.info.documentLaunchMode) {
1894 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1895 break;
1896 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1897 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1898 break;
1899 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1900 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1901 break;
1902 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1903 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1904 break;
1905 }
1906 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001907
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001908 final boolean launchTaskBehind = r.mLaunchTaskBehind
1909 && !launchSingleTask && !launchSingleInstance
1910 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001911
Wale Ogunwale7d701172015-03-11 15:36:30 -07001912 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1913 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001914 // For whatever reason this activity is being launched into a new
1915 // task... yet the caller has requested a result back. Well, that
1916 // is pretty messed up, so instead immediately send back a cancel
1917 // and let the new task continue launched as normal without a
1918 // dependency on its originator.
1919 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1920 r.resultTo.task.stack.sendActivityResultLocked(-1,
1921 r.resultTo, r.resultWho, r.requestCode,
1922 Activity.RESULT_CANCELED, null);
1923 r.resultTo = null;
1924 }
1925
1926 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1927 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1928 }
1929
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001930 // If we are actually going to launch in to a new task, there are some cases where
1931 // we further want to do multiple task.
1932 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1933 if (launchTaskBehind
1934 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1935 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1936 }
1937 }
1938
Craig Mautner8849a5e2013-04-02 16:41:03 -07001939 // We'll invoke onUserLeaving before onPause only if the launching
1940 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001941 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001942 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1943 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001944
1945 // If the caller has asked not to resume at this point, we make note
1946 // of this in the record so that we can skip it when trying to find
1947 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001948 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001949 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001950 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951 }
1952
Craig Mautnera254cd72014-05-25 16:47:39 -07001953 ActivityRecord notTop =
1954 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001955
1956 // If the onlyIfNeeded flag is set, then we can do this if the activity
1957 // being launched is the same as the one making the call... or, as
1958 // a special case, if we do not know the caller then we count the
1959 // current top activity as the caller.
1960 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1961 ActivityRecord checkedCaller = sourceRecord;
1962 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001963 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001964 }
1965 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1966 // Caller is not the same as launcher, so always needed.
1967 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1968 }
1969 }
1970
Craig Mautner8849a5e2013-04-02 16:41:03 -07001971 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001972 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001973
1974 // If the caller is not coming from another activity, but has given us an
1975 // explicit task into which they would like us to launch the new activity,
1976 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001977 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1978 final Intent baseIntent = inTask.getBaseIntent();
1979 final ActivityRecord root = inTask.getRootActivity();
1980 if (baseIntent == null) {
1981 ActivityOptions.abort(options);
1982 throw new IllegalArgumentException("Launching into task without base intent: "
1983 + inTask);
1984 }
1985
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001986 // If this task is empty, then we are adding the first activity -- it
1987 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001988 if (launchSingleInstance || launchSingleTask) {
1989 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1990 ActivityOptions.abort(options);
1991 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1992 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001993 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001994 if (root != null) {
1995 ActivityOptions.abort(options);
1996 throw new IllegalArgumentException("Caller with inTask " + inTask
1997 + " has root " + root + " but target is singleInstance/Task");
1998 }
1999 }
2000
2001 // If task is empty, then adopt the interesting intent launch flags in to the
2002 // activity being started.
2003 if (root == null) {
2004 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2005 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2006 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2007 launchFlags = (launchFlags&~flagsOfInterest)
2008 | (baseIntent.getFlags()&flagsOfInterest);
2009 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002010 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002011 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002012
Dianne Hackborn962d5352014-08-29 16:27:40 -07002013 // If the task is not empty and the caller is asking to start it as the root
2014 // of a new task, then we don't actually want to start this on the task. We
2015 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002016 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002017 addingToTask = false;
2018
2019 } else {
2020 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002021 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002022
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002023 reuseTask = inTask;
2024 } else {
2025 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002026 // Launch ResolverActivity in the source task, so that it stays in the task
2027 // bounds when in freeform workspace.
2028 // Also put noDisplay activities in the source task. These by itself can
2029 // be placed in any task/stack, however it could launch other activities
2030 // like ResolverActivity, and we want those to stay in the original task.
2031 if (r.isResolverActivity() || r.noDisplay) {
2032 addingToTask = true;
2033 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002034 }
2035
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002036 if (inTask == null) {
2037 if (sourceRecord == null) {
2038 // This activity is not being started from another... in this
2039 // case we -always- start a new task.
2040 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2041 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2042 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2043 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2044 }
2045 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2046 // The original activity who is starting us is running as a single
2047 // instance... this new activity it is starting must go on its
2048 // own task.
2049 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2050 } else if (launchSingleInstance || launchSingleTask) {
2051 // The activity being started is a single instance... it always
2052 // gets launched into its own task.
2053 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2054 }
2055 }
2056
2057 ActivityInfo newTaskInfo = null;
2058 Intent newTaskIntent = null;
2059 ActivityStack sourceStack;
2060 if (sourceRecord != null) {
2061 if (sourceRecord.finishing) {
2062 // If the source is finishing, we can't further count it as our source. This
2063 // is because the task it is associated with may now be empty and on its way out,
2064 // so we don't want to blindly throw it in to that task. Instead we will take
2065 // the NEW_TASK flow and try to find a task for it. But save the task information
2066 // so it can be used when creating the new task.
2067 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2068 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2069 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2070 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2071 newTaskInfo = sourceRecord.info;
2072 newTaskIntent = sourceRecord.task.intent;
2073 }
2074 sourceRecord = null;
2075 sourceStack = null;
2076 } else {
2077 sourceStack = sourceRecord.task.stack;
2078 }
2079 } else {
2080 sourceStack = null;
2081 }
2082
2083 boolean movedHome = false;
2084 ActivityStack targetStack;
2085
2086 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002087 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002088
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002089 // We may want to try to place the new activity in to an existing task. We always
2090 // do this if the target activity is singleTask or singleInstance; we will also do
2091 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2092 // us to still place it in a new task: multi task, always doc mode, or being asked to
2093 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002094 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2095 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002096 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002097 // If bring to front is requested, and no result is requested and we have not
2098 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002099 // we can find a task that was started with this same
2100 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002101 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002102 // See if there is a task to bring to the front. If this is
2103 // a SINGLE_INSTANCE activity, there can be one and only one
2104 // instance of it in the history, and it is always in its own
2105 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002106 ActivityRecord intentActivity = !launchSingleInstance ?
2107 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002108 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002109 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2110 // but still needs to be a lock task mode violation since the task gets
2111 // cleared out and the device would otherwise leave the locked task.
2112 if (isLockTaskModeViolation(intentActivity.task,
2113 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2114 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002115 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002116 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002117 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2118 }
Craig Mautner29219d92013-04-16 20:19:12 -07002119 if (r.task == null) {
2120 r.task = intentActivity.task;
2121 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002122 if (intentActivity.task.intent == null) {
2123 // This task was started because of movement of
2124 // the activity based on affinity... now that we
2125 // are actually launching it, we can assign the
2126 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002127 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002128 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002129 targetStack = intentActivity.task.stack;
2130 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002131 // If the target task is not in the front, then we need
2132 // to bring it to the front... except... well, with
2133 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2134 // to have the same behavior as if a new instance was
2135 // being started, which means not bringing it to the front
2136 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002137 final ActivityStack focusStack = getFocusedStack();
2138 ActivityRecord curTop = (focusStack == null)
2139 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002140 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002141 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002142 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002143 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002144 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2145 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002146 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002147 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002148 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2149 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002150 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002151 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002152 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002153 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002154 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002155 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2156 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002157 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002158 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002159 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002160 options = null;
2161 }
2162 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002163 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002164 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002165 + " from " + intentActivity);
2166 targetStack.moveToFront("intentActivityFound");
2167 }
2168
Craig Mautner8849a5e2013-04-02 16:41:03 -07002169 // If the caller has requested that the target task be
2170 // reset, then do so.
2171 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2172 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2173 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002174 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002175 // We don't need to start a new activity, and
2176 // the client said not to do anything if that
2177 // is the case, so this is it! And for paranoia, make
2178 // sure we have correctly resumed the top activity.
2179 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002180 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002181
2182 // Make sure to notify Keyguard as well if we are not running an app
2183 // transition later.
2184 if (!movedToFront) {
2185 notifyActivityDrawnForKeyguard();
2186 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002187 } else {
2188 ActivityOptions.abort(options);
2189 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002190 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002191 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2192 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002193 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002194 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002195 // The caller has requested to completely replace any
2196 // existing task with its new activity. Well that should
2197 // not be too hard...
2198 reuseTask = intentActivity.task;
2199 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002200 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002201 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002202 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002203 // In this situation we want to remove all activities
2204 // from the task up to the one being started. In most
2205 // cases this means we are resetting the task to its
2206 // initial state.
2207 ActivityRecord top =
2208 intentActivity.task.performClearTaskLocked(r, launchFlags);
2209 if (top != null) {
2210 if (top.frontOfTask) {
2211 // Activity aliases may mean we use different
2212 // intents for the top activity, so make sure
2213 // the task now has the identity of the new
2214 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002215 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002216 }
2217 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2218 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002219 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002220 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002221 // A special case: we need to start the activity because it is not
2222 // currently running, and the caller has asked to clear the current
2223 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002224 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002225 // Now pretend like this activity is being started by the top of its
2226 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002227 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002228 TaskRecord task = sourceRecord.task;
2229 if (task != null && task.stack == null) {
2230 // Target stack got cleared when we all activities were removed
2231 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002232 targetStack = computeStackFocus(
2233 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002234 targetStack.addTask(
2235 task, !launchTaskBehind /* toTop */, false /* moving */);
2236 }
2237
Craig Mautner8849a5e2013-04-02 16:41:03 -07002238 }
2239 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2240 // In this case the top activity on the task is the
2241 // same as the one being launched, so we take that
2242 // as a request to bring the task to the foreground.
2243 // If the top activity in the task is the root
2244 // activity, deliver this new intent to it if it
2245 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002246 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002247 && intentActivity.realActivity.equals(r.realActivity)) {
2248 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2249 intentActivity.task);
2250 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002251 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002252 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002253 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2254 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002255 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2256 // In this case we are launching the root activity
2257 // of the task, but with a different intent. We
2258 // should start a new instance on top.
2259 addingToTask = true;
2260 sourceRecord = intentActivity;
2261 }
2262 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2263 // In this case an activity is being launched in to an
2264 // existing task, without resetting that task. This
2265 // is typically the situation of launching an activity
2266 // from a notification or shortcut. We want to place
2267 // the new activity on top of the current task.
2268 addingToTask = true;
2269 sourceRecord = intentActivity;
2270 } else if (!intentActivity.task.rootWasReset) {
2271 // In this case we are launching in to an existing task
2272 // that has not yet been started from its front door.
2273 // The current task has been brought to the front.
2274 // Ideally, we'd probably like to place this new task
2275 // at the bottom of its stack, but that's a little hard
2276 // to do with the current organization of the code so
2277 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002278 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002279 }
2280 if (!addingToTask && reuseTask == null) {
2281 // We didn't do anything... but it was needed (a.k.a., client
2282 // don't use that intent!) And for paranoia, make
2283 // sure we have correctly resumed the top activity.
2284 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002285 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002286 if (!movedToFront) {
2287 // Make sure to notify Keyguard as well if we are not running an app
2288 // transition later.
2289 notifyActivityDrawnForKeyguard();
2290 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002291 } else {
2292 ActivityOptions.abort(options);
2293 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002294 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002295 return ActivityManager.START_TASK_TO_FRONT;
2296 }
2297 }
2298 }
2299 }
2300
2301 //String uri = r.intent.toURI();
2302 //Intent intent2 = new Intent(uri);
2303 //Slog.i(TAG, "Given intent: " + r.intent);
2304 //Slog.i(TAG, "URI is: " + uri);
2305 //Slog.i(TAG, "To intent: " + intent2);
2306
2307 if (r.packageName != null) {
2308 // If the activity being launched is the same as the one currently
2309 // at the top, then we need to check if it should only be launched
2310 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002311 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002312 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002313 if (top != null && r.resultTo == null) {
2314 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2315 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002316 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002317 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002318 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2319 top.task);
2320 // For paranoia, make sure we have correctly
2321 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002322 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002323 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002324 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002325 }
2326 ActivityOptions.abort(options);
2327 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2328 // We don't need to start a new activity, and
2329 // the client said not to do anything if that
2330 // is the case, so this is it!
2331 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2332 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002333 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002334 return ActivityManager.START_DELIVERED_TO_TOP;
2335 }
2336 }
2337 }
2338 }
2339
2340 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002341 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002342 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2343 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002344 }
2345 ActivityOptions.abort(options);
2346 return ActivityManager.START_CLASS_NOT_FOUND;
2347 }
2348
2349 boolean newTask = false;
2350 boolean keepCurTransition = false;
2351
Craig Mautnerbb742462014-07-07 15:28:55 -07002352 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002353 sourceRecord.task : null;
2354
Craig Mautner8849a5e2013-04-02 16:41:03 -07002355 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002356 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002357 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002358 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002359 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002360 if (doResume) {
2361 targetStack.moveToFront("startingNewTask");
2362 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002363
Craig Mautner8849a5e2013-04-02 16:41:03 -07002364 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002365 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2366 newTaskInfo != null ? newTaskInfo : r.info,
2367 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002368 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2369 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002370 if (overrideBounds) {
2371 r.task.updateOverrideConfiguration(newBounds);
2372 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002373 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2374 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002375 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002376 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002377 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002378 if (isLockTaskModeViolation(r.task)) {
2379 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2380 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2381 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002382 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002383 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002384 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2385 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002386 // Caller wants to appear on home activity, so before starting
2387 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002388 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002389 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002390 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002391 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002392 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002393 if (isLockTaskModeViolation(sourceTask)) {
2394 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2395 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2396 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002397 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002398 if (doResume) {
2399 targetStack.moveToFront("sourceStackToFront");
2400 }
Craig Mautner737fae22014-10-15 12:52:10 -07002401 final TaskRecord topTask = targetStack.topTask();
2402 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002403 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002404 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002405 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002406 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002407 // In this case, we are adding the activity to an existing
2408 // task, but the caller has asked to clear that task if the
2409 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002410 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002411 keepCurTransition = true;
2412 if (top != null) {
2413 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002414 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002415 // For paranoia, make sure we have correctly
2416 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002417 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002418 if (doResume) {
2419 targetStack.resumeTopActivityLocked(null);
2420 }
2421 ActivityOptions.abort(options);
2422 return ActivityManager.START_DELIVERED_TO_TOP;
2423 }
2424 } else if (!addingToTask &&
2425 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2426 // In this case, we are launching an activity in our own task
2427 // that may already be running somewhere in the history, and
2428 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002429 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002430 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002431 final TaskRecord task = top.task;
2432 task.moveActivityToFrontLocked(top);
2433 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002434 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002435 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002436 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002437 if (doResume) {
2438 targetStack.resumeTopActivityLocked(null);
2439 }
2440 return ActivityManager.START_DELIVERED_TO_TOP;
2441 }
2442 }
2443 // An existing activity is starting this new activity, so we want
2444 // to keep the new one in the same task as the one that is starting
2445 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002446 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002447 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002448 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002449
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002450 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002451 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002452 // task it has provided to us.
2453 if (isLockTaskModeViolation(inTask)) {
2454 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2455 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2456 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002457 if (overrideBounds) {
2458 inTask.updateOverrideConfiguration(newBounds);
2459 int stackId = inTask.getLaunchStackId();
2460 if (stackId != inTask.stack.mStackId) {
2461 moveTaskToStackUncheckedLocked(
2462 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2463 }
2464 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002465 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002466 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2467 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002468
2469 // Check whether we should actually launch the new activity in to the task,
2470 // or just reuse the current activity on top.
2471 ActivityRecord top = inTask.getTopActivity();
2472 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2473 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2474 || launchSingleTop || launchSingleTask) {
2475 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2476 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2477 // We don't need to start a new activity, and
2478 // the client said not to do anything if that
2479 // is the case, so this is it!
2480 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2481 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002482 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002483 return ActivityManager.START_DELIVERED_TO_TOP;
2484 }
2485 }
2486
Dianne Hackborn962d5352014-08-29 16:27:40 -07002487 if (!addingToTask) {
2488 // We don't actually want to have this activity added to the task, so just
2489 // stop here but still tell the caller that we consumed the intent.
2490 ActivityOptions.abort(options);
2491 return ActivityManager.START_TASK_TO_FRONT;
2492 }
2493
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002494 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002495 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002496 + " in explicit task " + r.task);
2497
Craig Mautner8849a5e2013-04-02 16:41:03 -07002498 } else {
2499 // This not being started from an existing activity, and not part
2500 // of a new task... just put it in the top task, though these days
2501 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002502 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002503 if (doResume) {
2504 targetStack.moveToFront("addingToTopTask");
2505 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002506 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002507 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002508 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002509 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002510 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002511 + " in new guessed " + r.task);
2512 }
2513
2514 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002515 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002516
Craig Mautner76e2a762014-06-24 09:05:43 -07002517 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2518 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2519 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002520 if (newTask) {
2521 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2522 }
2523 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002524 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002525 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002526 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002527 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002528 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002529 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002530 return ActivityManager.START_SUCCESS;
2531 }
2532
Craig Mautneree36c772014-07-16 14:56:05 -07002533 final void doPendingActivityLaunchesLocked(boolean doResume) {
2534 while (!mPendingActivityLaunches.isEmpty()) {
2535 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002536
2537 try {
2538 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
2539 doResume && mPendingActivityLaunches.isEmpty(), null, null);
2540 } catch (Exception e) {
2541 Slog.w(TAG, "Exception during pending activity launch pal=" + pal, e);
2542 }
Craig Mautneree36c772014-07-16 14:56:05 -07002543 }
2544 }
2545
Craig Mautner7f13ed32014-07-28 14:00:35 -07002546 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002547 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2548 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002549 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002550 mPendingActivityLaunches.remove(palNdx);
2551 }
2552 }
2553 }
2554
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002555 void setLaunchSource(int uid) {
2556 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2557 }
2558
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002559 void acquireLaunchWakelock() {
2560 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2561 throw new IllegalStateException("Calling must be system uid");
2562 }
2563 mLaunchingActivity.acquire();
2564 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2565 // To be safe, don't allow the wake lock to be held for too long.
2566 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2567 }
2568 }
2569
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002570 /**
2571 * Called when the frontmost task is idle.
2572 * @return the state of mService.mBooting before this was called.
2573 */
2574 private boolean checkFinishBootingLocked() {
2575 final boolean booting = mService.mBooting;
2576 boolean enableScreen = false;
2577 mService.mBooting = false;
2578 if (!mService.mBooted) {
2579 mService.mBooted = true;
2580 enableScreen = true;
2581 }
2582 if (booting || enableScreen) {
2583 mService.postFinishBooting(booting, enableScreen);
2584 }
2585 return booting;
2586 }
2587
Craig Mautnerf3333272013-04-22 10:55:53 -07002588 // Checked.
2589 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2590 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002591 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002592
Craig Mautnerf3333272013-04-22 10:55:53 -07002593 ArrayList<ActivityRecord> stops = null;
2594 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002595 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002596 int NS = 0;
2597 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002598 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002599 boolean activityRemoved = false;
2600
Wale Ogunwale7d701172015-03-11 15:36:30 -07002601 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002603 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2604 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002605 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2606 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002607 if (fromTimeout) {
2608 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002609 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002610
2611 // This is a hack to semi-deal with a race condition
2612 // in the client where it can be constructed with a
2613 // newer configuration from when we asked it to launch.
2614 // We'll update with whatever configuration it now says
2615 // it used to launch.
2616 if (config != null) {
2617 r.configuration = config;
2618 }
2619
2620 // We are now idle. If someone is waiting for a thumbnail from
2621 // us, we can now deliver.
2622 r.idle = true;
2623
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002624 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002625 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002626 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002627 }
2628 }
2629
2630 if (allResumedActivitiesIdle()) {
2631 if (r != null) {
2632 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002633 }
2634
2635 if (mLaunchingActivity.isHeld()) {
2636 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2637 if (VALIDATE_WAKE_LOCK_CALLER &&
2638 Binder.getCallingUid() != Process.myUid()) {
2639 throw new IllegalStateException("Calling must be system uid");
2640 }
2641 mLaunchingActivity.release();
2642 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002643 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002644 }
2645
2646 // Atomically retrieve all of the other things to do.
2647 stops = processStoppingActivitiesLocked(true);
2648 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002649 if ((NF = mFinishingActivities.size()) > 0) {
2650 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002651 mFinishingActivities.clear();
2652 }
2653
Craig Mautnerf3333272013-04-22 10:55:53 -07002654 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002655 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002656 mStartingUsers.clear();
2657 }
2658
Craig Mautnerf3333272013-04-22 10:55:53 -07002659 // Stop any activities that are scheduled to do so but have been
2660 // waiting for the next one to start.
2661 for (int i = 0; i < NS; i++) {
2662 r = stops.get(i);
2663 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002664 if (stack != null) {
2665 if (r.finishing) {
2666 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2667 } else {
2668 stack.stopActivityLocked(r);
2669 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002670 }
2671 }
2672
2673 // Finish any activities that are scheduled to do so but have been
2674 // waiting for the next one to start.
2675 for (int i = 0; i < NF; i++) {
2676 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002677 final ActivityStack stack = r.task.stack;
2678 if (stack != null) {
2679 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2680 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002681 }
2682
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002683 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002684 // Complete user switch
2685 if (startingUsers != null) {
2686 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002687 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002688 }
2689 }
2690 // Complete starting up of background users
2691 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002692 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002693 mStartingBackgroundUsers.clear();
2694 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002695 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002696 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002697 }
2698 }
2699
2700 mService.trimApplications();
2701 //dump();
2702 //mWindowManager.dump();
2703
Craig Mautnerf3333272013-04-22 10:55:53 -07002704 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002705 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002706 }
2707
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002708 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002709 }
2710
Craig Mautner8e569572013-10-11 17:36:59 -07002711 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002712 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002713 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2714 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002715 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2716 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2717 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002718 }
Craig Mautner19091252013-10-05 00:03:53 -07002719 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002720 }
2721
2722 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002723 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2724 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002725 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2726 stacks.get(stackNdx).closeSystemDialogsLocked();
2727 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002728 }
2729 }
2730
Craig Mautner93529a42013-10-04 15:03:13 -07002731 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002732 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002733 }
2734
Craig Mautner8d341ef2013-03-26 09:03:27 -07002735 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002736 * Update the last used stack id for non-current user (current user's last
2737 * used stack is the focused stack)
2738 */
2739 void updateUserStackLocked(int userId, ActivityStack stack) {
2740 if (userId != mCurrentUser) {
2741 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2742 }
2743 }
2744
2745 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002746 * @return true if some activity was finished (or would have finished if doit were true).
2747 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002748 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2749 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002750 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002751 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2752 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002753 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002754 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002755 if (stack.finishDisabledPackageActivitiesLocked(
2756 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002757 didSomething = true;
2758 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002759 }
2760 }
2761 return didSomething;
2762 }
2763
Dianne Hackborna413dc02013-07-12 12:02:55 -07002764 void updatePreviousProcessLocked(ActivityRecord r) {
2765 // Now that this process has stopped, we may want to consider
2766 // it to be the previous app to try to keep around in case
2767 // the user wants to return to it.
2768
2769 // First, found out what is currently the foreground app, so that
2770 // we don't blow away the previous app if this activity is being
2771 // hosted by the process that is actually still the foreground.
2772 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002773 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2774 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002775 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2776 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002777 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002778 if (stack.mResumedActivity != null) {
2779 fgApp = stack.mResumedActivity.app;
2780 } else if (stack.mPausingActivity != null) {
2781 fgApp = stack.mPausingActivity.app;
2782 }
2783 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002784 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002785 }
2786 }
2787
2788 // Now set this one as the previous process, only if that really
2789 // makes sense to.
2790 if (r.app != null && fgApp != null && r.app != fgApp
2791 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002792 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002793 mService.mPreviousProcess = r.app;
2794 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2795 }
2796 }
2797
Craig Mautner05d29032013-05-03 13:40:13 -07002798 boolean resumeTopActivitiesLocked() {
2799 return resumeTopActivitiesLocked(null, null, null);
2800 }
2801
2802 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2803 Bundle targetOptions) {
2804 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002805 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002806 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002807 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002808 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002809 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002810 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2811 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002812
Craig Mautnere0a38842013-12-16 16:14:02 -08002813 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2814 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002815 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2816 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002817 if (stack == targetStack) {
2818 // Already started above.
2819 continue;
2820 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002821 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002822 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002823 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002824 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002825 }
Craig Mautner05d29032013-05-03 13:40:13 -07002826 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002827 }
2828
Todd Kennedy539db512014-12-15 09:57:55 -08002829 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002830 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2831 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002832 final int numStacks = stacks.size();
2833 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2834 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002835 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002836 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002837 }
2838 }
2839
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002840 void finishVoiceTask(IVoiceInteractionSession session) {
2841 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2842 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2843 final int numStacks = stacks.size();
2844 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2845 final ActivityStack stack = stacks.get(stackNdx);
2846 stack.finishVoiceTask(session);
2847 }
2848 }
2849 }
2850
Craig Mautner299f9602015-01-26 09:47:33 -08002851 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002852 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2853 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002854 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002855 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2856 // Caller wants the home activity moved with it. To accomplish this,
2857 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002858 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002859 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002860 if (task.stack == null) {
2861 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2862 + task + " to front. Stack is null");
2863 return;
2864 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002865
2866 int stackId = task.stack.mStackId;
2867 if (task.mResizeable && options != null) {
2868 ActivityOptions opts = new ActivityOptions(options);
2869 if (opts.hasBounds()) {
2870 Rect bounds = opts.getBounds();
2871 task.updateOverrideConfiguration(bounds);
Chong Zhang87b21722015-09-21 15:39:51 -07002872 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2873 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002874 stackId = task.getLaunchStackId();
2875 }
2876 }
2877
2878 if (stackId != task.stack.mStackId) {
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002879 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2880
2881 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2882 // still need moveTaskToFrontLocked() below for any transition settings.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002883 }
2884
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002885 final ActivityRecord r = task.getTopActivity();
2886 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2887 r == null ? null : r.appTimeTracker, reason);
2888
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002889 if (DEBUG_STACK) Slog.d(TAG_STACK,
2890 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002891 }
2892
Craig Mautner967212c2013-04-13 21:10:58 -07002893 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002894 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002895 }
2896
2897 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002898 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2899 if (activityContainer != null) {
2900 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002901 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002902 if (!createStaticStackIfNeeded
2903 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2904 return null;
2905 }
2906 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 }
2908
Craig Mautner967212c2013-04-13 21:10:58 -07002909 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002910 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002911 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2912 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002913 }
2914 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002915 }
2916
Craig Mautner4a1cb222013-12-04 16:14:06 -08002917 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002918 ActivityRecord homeActivity = getHomeActivity();
2919 if (homeActivity != null) {
2920 return homeActivity.appToken;
2921 }
2922 return null;
2923 }
2924
2925 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002926 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002927 }
2928
2929 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002930 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2931 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2932 final TaskRecord task = tasks.get(taskNdx);
2933 if (task.isHomeTask()) {
2934 final ArrayList<ActivityRecord> activities = task.mActivities;
2935 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2936 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002937 if (r.isHomeActivity()
2938 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002939 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002940 }
2941 }
2942 }
2943 }
2944 return null;
2945 }
2946
Todd Kennedyca4d8422015-01-15 15:19:22 -08002947 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002948 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002949 ActivityContainer activityContainer =
2950 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002951 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002952 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2953 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002954 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002955 return activityContainer;
2956 }
2957
Craig Mautner34b73df2014-01-12 21:11:08 -08002958 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002959 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2960 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2961 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002962 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2963 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002964 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002965 }
2966 }
2967
Craig Mautner95da1082014-02-24 17:54:35 -08002968 void deleteActivityContainer(IActivityContainer container) {
2969 ActivityContainer activityContainer = (ActivityContainer)container;
2970 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002971 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2972 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002973 final int stackId = activityContainer.mStackId;
2974 mActivityContainers.remove(stackId);
2975 mWindowManager.removeStack(stackId);
2976 }
2977 }
2978
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002979 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
2980 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002981 final ActivityStack stack = getStack(stackId);
2982 if (stack == null) {
2983 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2984 return;
2985 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002986
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002987 if (!allowResizeInDockedMode
2988 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
2989 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2990 // stack size changing so things don't get out of sync.
2991 return;
2992 }
2993
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002994 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002995 mWindowManager.deferSurfaceLayout();
2996 try {
2997 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002998
Jorim Jaggic4025202015-10-22 16:43:34 +02002999 mTmpBounds.clear();
3000 mTmpConfigs.clear();
3001 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3002 for (int i = tasks.size() - 1; i >= 0; i--) {
3003 TaskRecord task = tasks.get(i);
3004 if (task.mResizeable) {
3005 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3006 // For freeform stack we don't adjust the size of the tasks to match that
3007 // of the stack, but we do try to make sure the tasks are still contained
3008 // with the bounds of the stack.
3009 tempRect2.set(task.mBounds);
3010 fitWithinBounds(tempRect2, bounds);
3011 task.updateOverrideConfiguration(tempRect2);
3012 } else {
3013 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003014 }
3015 }
3016
Jorim Jaggic4025202015-10-22 16:43:34 +02003017 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3018 mTmpBounds.put(task.taskId, task.mBounds);
3019 }
3020 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3021 if (stack.mStackId == DOCKED_STACK_ID) {
3022 // Dock stack funness...Yay!
3023 if (stack.mFullscreen) {
3024 // The dock stack went fullscreen which is kinda like dismissing it.
3025 // In this case we make all other static stacks fullscreen and move all
3026 // docked stack tasks to the fullscreen stack.
3027 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3028 if (i != DOCKED_STACK_ID && getStack(i) != null) {
3029 resizeStackLocked(i, null, preserveWindows, true);
3030 }
3031 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003032
Jorim Jaggic4025202015-10-22 16:43:34 +02003033 final int count = tasks.size();
3034 for (int i = 0; i < count; i++) {
3035 moveTaskToStackLocked(tasks.get(i).taskId,
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003036 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack");
Jorim Jaggic4025202015-10-22 16:43:34 +02003037 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003038
Jorim Jaggic4025202015-10-22 16:43:34 +02003039 // stack shouldn't contain anymore activities, so nothing to resume.
3040 r = null;
3041 } else {
3042 // Docked stacks occupy a dedicated region on screen so the size of all other
3043 // static stacks need to be adjusted so they don't overlap with the docked stack.
3044 // We get the bounds to use from window manager which has been adjusted for any
3045 // screen controls and is also the same for all stacks.
3046 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3047
3048 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3049 if (i != DOCKED_STACK_ID) {
3050 ActivityStack otherStack = getStack(i);
3051 if (otherStack != null) {
3052 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3053 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003054 }
3055 }
3056 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003057 // Since we are resizing the stack, all other operations should strive to preserve
3058 // windows.
3059 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003060 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003061 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003062
Jorim Jaggic4025202015-10-22 16:43:34 +02003063 if (r != null) {
3064 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3065 // And we need to make sure at this point that all other activities
3066 // are made visible with the correct configuration.
3067 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3068 if (!updated) {
3069 resumeTopActivitiesLocked(stack, null, null);
3070 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003071 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003072 } finally {
3073 mWindowManager.continueSurfaceLayout();
3074 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003075 }
3076 }
3077
Chong Zhang6de2ae82015-09-30 18:25:21 -07003078 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003079 if (!task.mResizeable) {
3080 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3081 return;
3082 }
3083
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003084 adjustForMinimalTaskDimensions(task, bounds);
3085
Chong Zhang87b21722015-09-21 15:39:51 -07003086 // If this is a forced resize, let it go through even if the bounds is not changing,
3087 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003088 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003089 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003090 // Nothing to do here...
3091 return;
3092 }
3093
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003094 if (!mWindowManager.isValidTaskId(task.taskId)) {
3095 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003096 // All we can do for now is update the bounds so it can be used when the task is
3097 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003098 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003099 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3100 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003101 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003102 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003103 return;
3104 }
3105
Chong Zhang6de2ae82015-09-30 18:25:21 -07003106 // Do not move the task to another stack here.
3107 // This method assumes that the task is already placed in the right stack.
3108 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003109
Chong Zhang6de2ae82015-09-30 18:25:21 -07003110 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003111
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003112 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003113 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003114 // way and the activity was kept the way it was. If it's false, it means the activity had
3115 // to be relaunched due to configuration change.
3116 boolean kept = true;
3117 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003118 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003119 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003120 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003121 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003122 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003123 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003124 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003125 resumeTopActivitiesLocked(stack, null, null);
3126 }
3127 }
3128 }
Chong Zhang87b21722015-09-21 15:39:51 -07003129 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003130
3131 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003132 }
3133
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003134 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3135 if (bounds == null) {
3136 return;
3137 }
3138 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3139 : task.mMinimalSize;
3140 final boolean adjustWidth = minimalSize > bounds.width();
3141 final boolean adjustHeight = minimalSize > bounds.height();
3142 if (!(adjustWidth || adjustHeight)) {
3143 return;
3144 }
3145 Rect taskBounds = task.mBounds;
3146 if (adjustWidth) {
3147 if (taskBounds != null && bounds.right == taskBounds.right) {
3148 bounds.left = bounds.right - minimalSize;
3149 } else {
3150 // Either left bounds match, or neither match, or the previous bounds were
3151 // fullscreen and we default to keeping left.
3152 bounds.right = bounds.left + minimalSize;
3153 }
3154 }
3155 if (adjustHeight) {
3156 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3157 bounds.top = bounds.bottom - minimalSize;
3158 } else {
3159 // Either top bounds match, or neither match, or the previous bounds were
3160 // fullscreen and we default to keeping top.
3161 bounds.bottom = bounds.top + minimalSize;
3162 }
3163 }
3164 }
3165
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003166 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003167 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3168 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003169 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003170 }
3171
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003172 ActivityContainer activityContainer = new ActivityContainer(stackId);
3173 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003174 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003175 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003176 }
3177
3178 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003179 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003180 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3181 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003182 break;
3183 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003184 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003185 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003186 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003187 }
3188
Chong Zhang5dcb2752015-08-18 13:50:26 -07003189 /**
3190 * Restores a recent task to a stack
3191 * @param task The recent task to be restored.
3192 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003193 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003194 * @return true if the task has been restored successfully.
3195 */
3196 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3197 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003198 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003199 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003200 if (task.stack != null) {
3201 // Task has already been restored once. See if we need to do anything more
3202 if (task.stack.mStackId == stackId) {
3203 // Nothing else to do since it is already restored in the right stack.
3204 return true;
3205 }
3206 // Remove current stack association, so we can re-associate the task with the
3207 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003208 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003209 }
3210
3211 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003212 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003213
3214 if (stack == null) {
3215 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003216 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3217 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003218 return false;
3219 }
3220
3221 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003222 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3223 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003224 final ArrayList<ActivityRecord> activities = task.mActivities;
3225 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003226 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003227 }
3228 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003229 }
3230
Wale Ogunwale040b4702015-08-06 18:10:50 -07003231 /**
3232 * Moves the specified task record to the input stack id.
3233 * WARNING: This method performs an unchecked/raw move of the task and
3234 * can leave the system in an unstable state if used incorrectly.
3235 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3236 * to a stack.
3237 * @param task Task to move.
3238 * @param stackId Id of stack to move task to.
3239 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003240 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003241 * @param reason Reason the task is been moved.
3242 * @return The stack the task was moved to.
3243 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003244 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003245 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3246 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003247 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003248 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3249
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003250 final boolean resizeable = task.mResizeable;
3251 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3252 // if a docked stack is created below which will lead to the stack we are moving from and
3253 // its resizeable tasks being resized.
3254 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003255 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003256 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003257 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3258 if (task.stack != null) {
3259 task.stack.removeTask(task, reason, MOVING);
3260 }
3261 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003262
3263 // If the task had focus before (or we're requested to move focus),
3264 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003265 stack.setFocusAndResumeStateIfNeeded(
3266 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003267
Wale Ogunwale040b4702015-08-06 18:10:50 -07003268 return stack;
3269 }
3270
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003271 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
3272 String reason) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003273 final TaskRecord task = anyTaskForIdLocked(taskId);
3274 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003275 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003276 return;
3277 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07003278 if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID
3279 || stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003280 // We are about to relaunch the activity because its configuration changed due to
3281 // being maximized, i.e. size change. The activity will first remove the old window
3282 // and then add a new one. This call will tell window manager about this, so it can
3283 // preserve the old window until the new one is drawn. This prevents having a gap
3284 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003285 // entrance of the new window to be properly animated.
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003286 ActivityRecord r = task.getTopActivity();
3287 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3288 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003289 final ActivityStack stack =
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003290 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus,
3291 "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003292
3293 // Make sure the task has the appropriate bounds/size for the stack it is in.
3294 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003295 resizeTaskLocked(task, stack.mBounds,
3296 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003297 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3298 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003299 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3300 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003301 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003302 resizeTaskLocked(task, stack.mBounds,
3303 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003304 }
3305
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003306 // The task might have already been running and its visibility needs to be synchronized with
3307 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003308 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003309 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003310 }
3311
Wale Ogunwale079a0042015-10-24 11:44:07 -07003312 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3313 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3314 if (stack == null) {
3315 throw new IllegalArgumentException(
3316 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3317 }
3318
3319 final ActivityRecord r = stack.topRunningActivityLocked();
3320 if (r == null) {
3321 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3322 + " in stack=" + stack);
3323 return false;
3324 }
3325
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003326 if (!r.info.supportsPip) {
3327 Slog.w(TAG,
3328 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003329 + " r=" + r);
3330 return false;
3331 }
3332
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003333 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003334 if (task.mActivities.size() == 1) {
3335 // There is only one activity in the task. So, we can just move the task over to the
3336 // pinned stack without re-parenting the activity in a different task.
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003337 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS,
3338 "moveTopActivityToPinnedStack");
Wale Ogunwale079a0042015-10-24 11:44:07 -07003339 } else {
3340 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3341 pinnedStack.moveActivityToStack(r);
3342 }
3343
3344 resizeStackLocked(PINNED_STACK_ID, bounds, PRESERVE_WINDOWS, true);
3345
3346 // The task might have already been running and its visibility needs to be synchronized with
3347 // the visibility of the stack / windows.
3348 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3349 resumeTopActivitiesLocked();
3350 return true;
3351 }
3352
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003353 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3354 final TaskRecord task = anyTaskForIdLocked(taskId);
3355 if (task == null) {
3356 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3357 return;
3358 }
3359 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003360 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003361 mWindowManager.positionTaskInStack(taskId, stackId, position);
3362 final boolean stackChanged = task.stack != null && task.stack != stack;
3363 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003364 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003365 }
3366 stack.positionTask(task, position, stackChanged);
3367 // The task might have already been running and its visibility needs to be synchronized with
3368 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003369 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003370 resumeTopActivitiesLocked();
3371 }
3372
Craig Mautnerac6f8432013-07-17 13:24:59 -07003373 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003374 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003375 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3376 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003377 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3378 final ActivityStack stack = stacks.get(stackNdx);
3379 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003380 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003381 continue;
3382 }
3383 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003384 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3385 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003386 continue;
3387 }
3388 final ActivityRecord ar = stack.findTaskLocked(r);
3389 if (ar != null) {
3390 return ar;
3391 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003392 }
3393 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003394 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003395 return null;
3396 }
3397
3398 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003399 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3400 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003401 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3402 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3403 if (ar != null) {
3404 return ar;
3405 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003406 }
3407 }
3408 return null;
3409 }
3410
Craig Mautner8d341ef2013-03-26 09:03:27 -07003411 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003412 scheduleSleepTimeout();
3413 if (!mGoingToSleep.isHeld()) {
3414 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003415 if (mLaunchingActivity.isHeld()) {
3416 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3417 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003418 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003419 mLaunchingActivity.release();
3420 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003421 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003422 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003423 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003424 }
3425
3426 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003427 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003428
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003429 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003430 final long endTime = System.currentTimeMillis() + timeout;
3431 while (true) {
3432 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003433 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3434 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003435 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3436 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3437 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003438 }
3439 if (cantShutdown) {
3440 long timeRemaining = endTime - System.currentTimeMillis();
3441 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003442 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003443 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003444 } catch (InterruptedException e) {
3445 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003446 } else {
3447 Slog.w(TAG, "Activity manager shutdown timed out");
3448 timedout = true;
3449 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003450 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003451 } else {
3452 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003453 }
3454 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003455
3456 // Force checkReadyForSleep to complete.
3457 mSleepTimeout = true;
3458 checkReadyForSleepLocked();
3459
Craig Mautner8d341ef2013-03-26 09:03:27 -07003460 return timedout;
3461 }
3462
3463 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003464 removeSleepTimeouts();
3465 if (mGoingToSleep.isHeld()) {
3466 mGoingToSleep.release();
3467 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003468 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3469 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003470 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3471 final ActivityStack stack = stacks.get(stackNdx);
3472 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003473 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003474 resumeTopActivitiesLocked();
3475 }
Craig Mautner5314a402013-09-26 12:40:16 -07003476 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003477 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003478 mGoingToSleepActivities.clear();
3479 }
3480
3481 void activitySleptLocked(ActivityRecord r) {
3482 mGoingToSleepActivities.remove(r);
3483 checkReadyForSleepLocked();
3484 }
3485
3486 void checkReadyForSleepLocked() {
3487 if (!mService.isSleepingOrShuttingDown()) {
3488 // Do not care.
3489 return;
3490 }
3491
3492 if (!mSleepTimeout) {
3493 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003494 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3495 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003496 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3497 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3498 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003499 }
3500
3501 if (mStoppingActivities.size() > 0) {
3502 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003503 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003504 + mStoppingActivities.size() + " activities");
3505 scheduleIdleLocked();
3506 dontSleep = true;
3507 }
3508
3509 if (mGoingToSleepActivities.size() > 0) {
3510 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003511 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003512 + mGoingToSleepActivities.size() + " activities");
3513 dontSleep = true;
3514 }
3515
3516 if (dontSleep) {
3517 return;
3518 }
3519 }
3520
Craig Mautnere0a38842013-12-16 16:14:02 -08003521 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3522 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003523 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3524 stacks.get(stackNdx).goToSleep();
3525 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003526 }
3527
3528 removeSleepTimeouts();
3529
3530 if (mGoingToSleep.isHeld()) {
3531 mGoingToSleep.release();
3532 }
3533 if (mService.mShuttingDown) {
3534 mService.notifyAll();
3535 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003536 }
3537
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003538 boolean reportResumedActivityLocked(ActivityRecord r) {
3539 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003540 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003541 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003542 }
3543 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003544 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003545 mWindowManager.executeAppTransition();
3546 return true;
3547 }
3548 return false;
3549 }
3550
Craig Mautner8d341ef2013-03-26 09:03:27 -07003551 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003552 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3553 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003554 int stackNdx = stacks.size() - 1;
3555 while (stackNdx >= 0) {
3556 stacks.get(stackNdx).handleAppCrashLocked(app);
3557 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003558 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003559 }
3560 }
3561
Jose Lima4b6c6692014-08-12 17:41:12 -07003562 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003563 final ActivityStack stack = r.task.stack;
3564 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003565 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3566 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003567 return false;
3568 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003569 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003570 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3571 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003572
3573 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003574 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003575 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003576 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003577 return true;
3578 }
3579
3580 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003581 if (visible && top.fullscreen) {
3582 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003583 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3584 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3585 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3586 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003587 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003588 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3589 // Only the activity set as currently visible behind should actively reset its
3590 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003591 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3592 "requestVisibleBehind: returning visible=" + visible
3593 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3594 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003595 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003596 }
3597
Jose Lima4b6c6692014-08-12 17:41:12 -07003598 stack.setVisibleBehindActivity(visible ? r : null);
3599 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003600 // Make the activity immediately above r opaque.
3601 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3602 if (next != null) {
3603 mService.convertFromTranslucent(next.appToken);
3604 }
3605 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003606 if (top.app != null && top.app.thread != null) {
3607 // Notify the top app of the change.
3608 try {
3609 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3610 } catch (RemoteException e) {
3611 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003612 }
3613 return true;
3614 }
3615
Craig Mautnerbb742462014-07-07 15:28:55 -07003616 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3617 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3618 r.mLaunchTaskBehind = false;
3619 final TaskRecord task = r.task;
3620 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003621 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003622 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003623 mWindowManager.setAppVisibility(r.appToken, false);
3624 }
3625
3626 void scheduleLaunchTaskBehindComplete(IBinder token) {
3627 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3628 }
3629
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003630 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3631 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003632 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003633 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3634 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003635 final int topStackNdx = stacks.size() - 1;
3636 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3637 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003638 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003639 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003640 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003641 }
3642
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003643 void invalidateTaskLayers() {
3644 mTaskLayersChanged = true;
3645 }
3646
3647 void rankTaskLayersIfNeeded() {
3648 if (!mTaskLayersChanged) {
3649 return;
3650 }
3651 mTaskLayersChanged = false;
3652 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3653 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3654 int baseLayer = 0;
3655 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3656 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3657 }
3658 }
3659 }
3660
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003661 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3662 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3663 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3664 final int topStackNdx = stacks.size() - 1;
3665 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3666 final ActivityStack stack = stacks.get(stackNdx);
3667 stack.clearOtherAppTimeTrackers(except);
3668 }
3669 }
3670 }
3671
Craig Mautner8d341ef2013-03-26 09:03:27 -07003672 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3674 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003675 final int numStacks = stacks.size();
3676 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3677 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003678 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003679 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003680 }
3681 }
3682
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003683 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3684 // Examine all activities currently running in the process.
3685 TaskRecord firstTask = null;
3686 // Tasks is non-null only if two or more tasks are found.
3687 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003688 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3689 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003690 ActivityRecord r = app.activities.get(i);
3691 // First, if we find an activity that is in the process of being destroyed,
3692 // then we just aren't going to do anything for now; we want things to settle
3693 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003694 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003695 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003696 return;
3697 }
3698 // Don't consider any activies that are currently not in a state where they
3699 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003700 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3701 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003702 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003703 continue;
3704 }
3705 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003706 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003707 + " from " + r);
3708 if (firstTask == null) {
3709 firstTask = r.task;
3710 } else if (firstTask != r.task) {
3711 if (tasks == null) {
3712 tasks = new ArraySet<>();
3713 tasks.add(firstTask);
3714 }
3715 tasks.add(r.task);
3716 }
3717 }
3718 }
3719 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003720 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003721 return;
3722 }
3723 // If we have activities in multiple tasks that are in a position to be destroyed,
3724 // let's iterate through the tasks and release the oldest one.
3725 final int numDisplays = mActivityDisplays.size();
3726 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3727 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3728 // Step through all stacks starting from behind, to hit the oldest things first.
3729 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3730 final ActivityStack stack = stacks.get(stackNdx);
3731 // Try to release activities in this stack; if we manage to, we are done.
3732 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3733 return;
3734 }
3735 }
3736 }
3737 }
3738
Amith Yamasani37a40c22015-06-17 13:25:42 -07003739 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003740 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003741 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003742 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003743
Craig Mautner858d8a62013-04-23 17:08:34 -07003744 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003745 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3746 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003747 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003748 final ActivityStack stack = stacks.get(stackNdx);
3749 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003750 TaskRecord task = stack.topTask();
3751 if (task != null) {
3752 mWindowManager.moveTaskToTop(task.taskId);
3753 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003754 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003755 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003756
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003757 ActivityStack stack = getStack(restoreStackId);
3758 if (stack == null) {
3759 stack = mHomeStack;
3760 }
3761 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003762 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003763 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003764 } else {
3765 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003766 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003767 }
Craig Mautner93529a42013-10-04 15:03:13 -07003768 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003769 }
3770
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003771 /**
3772 * Add background users to send boot completed events to.
3773 * @param userId The user being started in the background
3774 * @param uss The state object for the user.
3775 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003776 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003777 mStartingBackgroundUsers.add(uss);
3778 }
3779
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003780 /** Checks whether the userid is a profile of the current user. */
3781 boolean isCurrentProfileLocked(int userId) {
3782 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003783 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003784 }
3785
Chong Zhang45c25ce2015-08-10 22:18:26 -07003786 /** Checks whether the activity should be shown for current user. */
3787 boolean okToShowLocked(ActivityRecord r) {
3788 return r != null && (isCurrentProfileLocked(r.userId)
3789 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3790 }
3791
Craig Mautnerde4ef022013-04-07 19:01:33 -07003792 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003793 ArrayList<ActivityRecord> stops = null;
3794
3795 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003796 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3797 ActivityRecord s = mStoppingActivities.get(activityNdx);
3798 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003799 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003800 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3801 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003802 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003803 if (s.finishing) {
3804 // If this activity is finishing, it is sitting on top of
3805 // everyone else but we now know it is no longer needed...
3806 // so get rid of it. Otherwise, we need to go through the
3807 // normal flow and hide it once we determine that it is
3808 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003809 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003810 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003811 }
3812 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003813 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003814 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003815 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003816 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003817 }
3818 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003819 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003820 }
3821 }
3822
3823 return stops;
3824 }
3825
Craig Mautnercf910b02013-04-23 11:23:27 -07003826 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003827 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3828 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3829 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3830 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003831 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003832 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003833 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003834 if (r == null) Slog.e(TAG,
3835 "validateTop...: null top activity, stack=" + stack);
3836 else {
3837 final ActivityRecord pausing = stack.mPausingActivity;
3838 if (pausing != null && pausing == r) Slog.e(TAG,
3839 "validateTop...: top stack has pausing activity r=" + r
3840 + " state=" + state);
3841 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3842 "validateTop...: activity in front not resumed r=" + r
3843 + " state=" + state);
3844 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003845 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003846 final ActivityRecord resumed = stack.mResumedActivity;
3847 if (resumed != null && resumed == r) Slog.e(TAG,
3848 "validateTop...: back stack has resumed activity r=" + r
3849 + " state=" + state);
3850 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3851 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003852 }
3853 }
3854 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003855 }
3856
Craig Mautnere0570202015-05-13 13:06:11 -07003857 private String lockTaskModeToString() {
3858 switch (mLockTaskModeState) {
3859 case LOCK_TASK_MODE_LOCKED:
3860 return "LOCKED";
3861 case LOCK_TASK_MODE_PINNED:
3862 return "PINNED";
3863 case LOCK_TASK_MODE_NONE:
3864 return "NONE";
3865 default: return "unknown=" + mLockTaskModeState;
3866 }
3867 }
3868
Craig Mautner27084302013-03-25 08:05:25 -07003869 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003870 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003871 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003872 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3873 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3874 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003875 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003876 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3877 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3878 if (packages.size() > 0) {
3879 pw.println(" mLockTaskPackages (userId:packages)=");
3880 for (int i = 0; i < packages.size(); ++i) {
3881 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3882 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3883 }
3884 }
3885 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003886 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003887
Craig Mautner20e72272013-04-01 13:45:53 -07003888 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003889 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003890 }
3891
Dianne Hackborn390517b2013-05-30 15:03:32 -07003892 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3893 boolean needSep, String prefix) {
3894 if (activity != null) {
3895 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3896 if (needSep) {
3897 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003898 }
3899 pw.print(prefix);
3900 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003901 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003902 }
3903 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003904 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003905 }
3906
Craig Mautner8d341ef2013-03-26 09:03:27 -07003907 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3908 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003909 boolean printed = false;
3910 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003911 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3912 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003913 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003914 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003915 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003916 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003917 final ActivityStack stack = stacks.get(stackNdx);
3918 StringBuilder stackHeader = new StringBuilder(128);
3919 stackHeader.append(" Stack #");
3920 stackHeader.append(stack.mStackId);
3921 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003922 stackHeader.append("\n");
3923 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3924 stackHeader.append("\n");
3925 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003926 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3927 needSep, stackHeader.toString());
3928 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3929 !dumpAll, false, dumpPackage, true,
3930 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003931
Craig Mautner4a1cb222013-12-04 16:14:06 -08003932 needSep = printed;
3933 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3934 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003935 if (pr) {
3936 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003937 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003938 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003939 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3940 " mResumedActivity: ");
3941 if (pr) {
3942 printed = true;
3943 needSep = false;
3944 }
3945 if (dumpAll) {
3946 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3947 " mLastPausedActivity: ");
3948 if (pr) {
3949 printed = true;
3950 needSep = true;
3951 }
3952 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3953 needSep, " mLastNoHistoryActivity: ");
3954 }
3955 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003956 }
3957 }
3958
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003959 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3960 false, dumpPackage, true, " Activities waiting to finish:", null);
3961 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3962 false, dumpPackage, true, " Activities waiting to stop:", null);
3963 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3964 false, dumpPackage, true, " Activities waiting for another to become visible:",
3965 null);
3966 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3967 false, dumpPackage, true, " Activities waiting to sleep:", null);
3968 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3969 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003970
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003971 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003972 }
3973
Dianne Hackborn390517b2013-05-30 15:03:32 -07003974 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003975 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003976 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003977 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003978 String innerPrefix = null;
3979 String[] args = null;
3980 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003981 for (int i=list.size()-1; i>=0; i--) {
3982 final ActivityRecord r = list.get(i);
3983 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3984 continue;
3985 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003986 if (innerPrefix == null) {
3987 innerPrefix = prefix + " ";
3988 args = new String[0];
3989 }
3990 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003991 final boolean full = !brief && (complete || !r.isInHistory());
3992 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003993 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003994 needNL = false;
3995 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003996 if (header1 != null) {
3997 pw.println(header1);
3998 header1 = null;
3999 }
4000 if (header2 != null) {
4001 pw.println(header2);
4002 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004003 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004004 if (lastTask != r.task) {
4005 lastTask = r.task;
4006 pw.print(prefix);
4007 pw.print(full ? "* " : " ");
4008 pw.println(lastTask);
4009 if (full) {
4010 lastTask.dump(pw, prefix + " ");
4011 } else if (complete) {
4012 // Complete + brief == give a summary. Isn't that obvious?!?
4013 if (lastTask.intent != null) {
4014 pw.print(prefix); pw.print(" ");
4015 pw.println(lastTask.intent.toInsecureStringWithClip());
4016 }
4017 }
4018 }
4019 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4020 pw.print(" #"); pw.print(i); pw.print(": ");
4021 pw.println(r);
4022 if (full) {
4023 r.dump(pw, innerPrefix);
4024 } else if (complete) {
4025 // Complete + brief == give a summary. Isn't that obvious?!?
4026 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4027 if (r.app != null) {
4028 pw.print(innerPrefix); pw.println(r.app);
4029 }
4030 }
4031 if (client && r.app != null && r.app.thread != null) {
4032 // flush anything that is already in the PrintWriter since the thread is going
4033 // to write to the file descriptor directly
4034 pw.flush();
4035 try {
4036 TransferPipe tp = new TransferPipe();
4037 try {
4038 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4039 r.appToken, innerPrefix, args);
4040 // Short timeout, since blocking here can
4041 // deadlock with the application.
4042 tp.go(fd, 2000);
4043 } finally {
4044 tp.kill();
4045 }
4046 } catch (IOException e) {
4047 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4048 } catch (RemoteException e) {
4049 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4050 }
4051 needNL = true;
4052 }
4053 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004054 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004055 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004056
Craig Mautnerf3333272013-04-22 10:55:53 -07004057 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004058 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4059 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004060 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4061 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004062 }
4063
4064 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004065 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004066 }
4067
4068 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004069 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4070 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004071 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4072 }
4073
Craig Mautner05d29032013-05-03 13:40:13 -07004074 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004075 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4076 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4077 }
Craig Mautner05d29032013-05-03 13:40:13 -07004078 }
4079
Craig Mautner0eea92c2013-05-16 13:35:39 -07004080 void removeSleepTimeouts() {
4081 mSleepTimeout = false;
4082 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4083 }
4084
4085 final void scheduleSleepTimeout() {
4086 removeSleepTimeouts();
4087 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4088 }
4089
Craig Mautner4a1cb222013-12-04 16:14:06 -08004090 @Override
4091 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004092 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004093 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4094 }
4095
4096 @Override
4097 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004098 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004099 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4100 }
4101
4102 @Override
4103 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004104 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004105 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4106 }
4107
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004108 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004109 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004110 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004111 newDisplay = mActivityDisplays.get(displayId) == null;
4112 if (newDisplay) {
4113 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004114 if (activityDisplay.mDisplay == null) {
4115 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4116 return;
4117 }
Craig Mautner4504de52013-12-20 09:06:56 -08004118 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004119 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004120 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004121 }
Craig Mautner4504de52013-12-20 09:06:56 -08004122 if (newDisplay) {
4123 mWindowManager.onDisplayAdded(displayId);
4124 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004125 }
4126
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004127 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4128 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4129 return;
4130 }
4131 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4132 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4133 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4134 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4135 }
4136
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004137 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004138 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004139 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4140 if (activityDisplay != null) {
4141 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004142 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004143 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004144 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004145 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004146 }
4147 }
4148 mWindowManager.onDisplayRemoved(displayId);
4149 }
4150
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004151 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004152 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004153 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4154 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004155 // TODO: Update the bounds.
4156 }
4157 }
4158 mWindowManager.onDisplayChanged(displayId);
4159 }
4160
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004161 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004162 StackInfo info = new StackInfo();
4163 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4164 info.displayId = Display.DEFAULT_DISPLAY;
4165 info.stackId = stack.mStackId;
4166
4167 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4168 final int numTasks = tasks.size();
4169 int[] taskIds = new int[numTasks];
4170 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004171 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004172 for (int i = 0; i < numTasks; ++i) {
4173 final TaskRecord task = tasks.get(i);
4174 taskIds[i] = task.taskId;
4175 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4176 : task.realActivity != null ? task.realActivity.flattenToString()
4177 : task.getTopActivity() != null ? task.getTopActivity().packageName
4178 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004179 taskBounds[i] = new Rect();
4180 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004181 }
4182 info.taskIds = taskIds;
4183 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004184 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004185 return info;
4186 }
4187
4188 StackInfo getStackInfoLocked(int stackId) {
4189 ActivityStack stack = getStack(stackId);
4190 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004191 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004192 }
4193 return null;
4194 }
4195
4196 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004197 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004198 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4199 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004200 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004201 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004202 }
4203 }
4204 return list;
4205 }
4206
Craig Mautner15df08a2015-04-01 12:17:18 -07004207 TaskRecord getLockedTaskLocked() {
4208 final int top = mLockTaskModeTasks.size() - 1;
4209 if (top >= 0) {
4210 return mLockTaskModeTasks.get(top);
4211 }
4212 return null;
4213 }
4214
4215 boolean isLockedTask(TaskRecord task) {
4216 return mLockTaskModeTasks.contains(task);
4217 }
4218
4219 boolean isLastLockedTask(TaskRecord task) {
4220 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4221 }
4222
4223 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004224 if (!mLockTaskModeTasks.remove(task)) {
4225 return;
4226 }
4227 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4228 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004229 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004230 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4231 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004232 final Message lockTaskMsg = Message.obtain();
4233 lockTaskMsg.arg1 = task.userId;
4234 lockTaskMsg.what = LOCK_TASK_END_MSG;
4235 mHandler.sendMessage(lockTaskMsg);
4236 }
4237 }
4238
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004239 void showLockTaskToast() {
4240 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004241 }
4242
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004243 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4244 if (mLockTaskModeTasks.contains(task)) {
4245 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4246 }
4247 }
4248
Craig Mautner432f64e2015-05-20 14:59:57 -07004249 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4250 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004251 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004252 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004253 final TaskRecord lockedTask = getLockedTaskLocked();
4254 if (lockedTask != null) {
4255 removeLockedTaskLocked(lockedTask);
4256 if (!mLockTaskModeTasks.isEmpty()) {
4257 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004258 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4259 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004260 lockedTask.performClearTaskLocked();
4261 resumeTopActivitiesLocked();
4262 return;
4263 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004264 }
Craig Mautnere0570202015-05-13 13:06:11 -07004265 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4266 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004267 return;
4268 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004269
4270 // Should have already been checked, but do it again.
4271 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004272 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4273 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004274 return;
4275 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004276 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004277 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004278 return;
4279 }
4280
4281 if (mLockTaskModeTasks.isEmpty()) {
4282 // First locktask.
4283 final Message lockTaskMsg = Message.obtain();
4284 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4285 lockTaskMsg.arg1 = task.userId;
4286 lockTaskMsg.what = LOCK_TASK_START_MSG;
4287 lockTaskMsg.arg2 = lockTaskModeState;
4288 mHandler.sendMessage(lockTaskMsg);
4289 }
4290 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004291 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4292 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004293 mLockTaskModeTasks.remove(task);
4294 mLockTaskModeTasks.add(task);
4295
4296 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004297 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004298 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004299
4300 if (andResume) {
4301 findTaskToMoveToFrontLocked(task, 0, null, reason);
4302 resumeTopActivitiesLocked();
4303 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004304 }
4305
4306 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004307 return isLockTaskModeViolation(task, false);
4308 }
4309
4310 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4311 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004312 return false;
4313 }
4314 final int lockTaskAuth = task.mLockTaskAuth;
4315 switch (lockTaskAuth) {
4316 case LOCK_TASK_AUTH_DONT_LOCK:
4317 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004318 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004319 case LOCK_TASK_AUTH_LAUNCHABLE:
4320 case LOCK_TASK_AUTH_WHITELISTED:
4321 return false;
4322 case LOCK_TASK_AUTH_PINNABLE:
4323 // Pinnable tasks can't be launched on top of locktask tasks.
4324 return !mLockTaskModeTasks.isEmpty();
4325 default:
4326 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4327 return true;
4328 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004329 }
4330
Craig Mautner15df08a2015-04-01 12:17:18 -07004331 void onLockTaskPackagesUpdatedLocked() {
4332 boolean didSomething = false;
4333 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4334 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004335 final boolean wasWhitelisted =
4336 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4337 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004338 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004339 final boolean isWhitelisted =
4340 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4341 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4342 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004343 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004344 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4345 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004346 removeLockedTaskLocked(lockedTask);
4347 lockedTask.performClearTaskLocked();
4348 didSomething = true;
4349 }
4350 }
4351 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4352 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4353 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4354 final ActivityStack stack = stacks.get(stackNdx);
4355 stack.onLockTaskPackagesUpdatedLocked();
4356 }
4357 }
Craig Mautnere0570202015-05-13 13:06:11 -07004358 final ActivityRecord r = topRunningActivityLocked();
4359 final TaskRecord task = r != null ? r.task : null;
4360 if (mLockTaskModeTasks.isEmpty() && task != null
4361 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4362 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004363 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4364 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4365 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4366 false);
4367 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004368 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004369 if (didSomething) {
4370 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004371 }
4372 }
4373
Benjamin Franz43261142015-02-11 15:59:44 +00004374 int getLockTaskModeState() {
4375 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004376 }
4377
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004378 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004379
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004380 public ActivityStackSupervisorHandler(Looper looper) {
4381 super(looper);
4382 }
4383
Craig Mautnerf3333272013-04-22 10:55:53 -07004384 void activityIdleInternal(ActivityRecord r) {
4385 synchronized (mService) {
4386 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4387 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004388 }
4389
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004390 @Override
4391 public void handleMessage(Message msg) {
4392 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004393 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004394 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4395 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004396 if (mService.mDidDexOpt) {
4397 mService.mDidDexOpt = false;
4398 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4399 nmsg.obj = msg.obj;
4400 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4401 return;
4402 }
4403 // We don't at this point know if the activity is fullscreen,
4404 // so we need to be conservative and assume it isn't.
4405 activityIdleInternal((ActivityRecord)msg.obj);
4406 } break;
4407 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004408 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004409 activityIdleInternal((ActivityRecord)msg.obj);
4410 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004411 case RESUME_TOP_ACTIVITY_MSG: {
4412 synchronized (mService) {
4413 resumeTopActivitiesLocked();
4414 }
4415 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004416 case SLEEP_TIMEOUT_MSG: {
4417 synchronized (mService) {
4418 if (mService.isSleepingOrShuttingDown()) {
4419 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4420 mSleepTimeout = true;
4421 checkReadyForSleepLocked();
4422 }
4423 }
4424 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004425 case LAUNCH_TIMEOUT_MSG: {
4426 if (mService.mDidDexOpt) {
4427 mService.mDidDexOpt = false;
4428 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4429 return;
4430 }
4431 synchronized (mService) {
4432 if (mLaunchingActivity.isHeld()) {
4433 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4434 if (VALIDATE_WAKE_LOCK_CALLER
4435 && Binder.getCallingUid() != Process.myUid()) {
4436 throw new IllegalStateException("Calling must be system uid");
4437 }
4438 mLaunchingActivity.release();
4439 }
4440 }
4441 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004442 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004443 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004444 } break;
4445 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004446 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004447 } break;
4448 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004449 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004450 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004451 case CONTAINER_CALLBACK_VISIBILITY: {
4452 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004453 final IActivityContainerCallback callback = container.mCallback;
4454 if (callback != null) {
4455 try {
4456 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4457 } catch (RemoteException e) {
4458 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004459 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004460 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004461 case LOCK_TASK_START_MSG: {
4462 // When lock task starts, we disable the status bars.
4463 try {
Jason Monk62515be2014-05-21 16:06:19 -04004464 if (mLockTaskNotify == null) {
4465 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004466 }
Jason Monk62515be2014-05-21 16:06:19 -04004467 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004468 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004469 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004470 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004471 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004472 flags = StatusBarManager.DISABLE_MASK
4473 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004474 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004475 flags = StatusBarManager.DISABLE_MASK
4476 & (~StatusBarManager.DISABLE_BACK)
4477 & (~StatusBarManager.DISABLE_HOME)
4478 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004479 }
4480 getStatusBarService().disable(flags, mToken,
4481 mService.mContext.getPackageName());
4482 }
4483 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004484 if (getDevicePolicyManager() != null) {
4485 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004486 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004487 }
justinzhang5286d3f2014-05-12 17:06:01 -04004488 } catch (RemoteException ex) {
4489 throw new RuntimeException(ex);
4490 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004491 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004492 case LOCK_TASK_END_MSG: {
4493 // When lock task ends, we enable the status bars.
4494 try {
Jason Monk62515be2014-05-21 16:06:19 -04004495 if (getStatusBarService() != null) {
4496 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4497 mService.mContext.getPackageName());
4498 }
4499 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004500 if (getDevicePolicyManager() != null) {
4501 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4502 msg.arg1);
4503 }
Jason Monk62515be2014-05-21 16:06:19 -04004504 if (mLockTaskNotify == null) {
4505 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4506 }
4507 mLockTaskNotify.show(false);
4508 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004509 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004510 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004511 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004512 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004513 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004514 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004515 new LockPatternUtils(mService.mContext)
4516 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004517 }
4518 } catch (SettingNotFoundException e) {
4519 // No setting, don't lock.
4520 }
justinzhang5286d3f2014-05-12 17:06:01 -04004521 } catch (RemoteException ex) {
4522 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004523 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004524 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004525 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004526 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004527 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4528 if (mLockTaskNotify == null) {
4529 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4530 }
4531 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4532 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004533 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4534 final ActivityContainer container = (ActivityContainer) msg.obj;
4535 final IActivityContainerCallback callback = container.mCallback;
4536 if (callback != null) {
4537 try {
4538 callback.onAllActivitiesComplete(container.asBinder());
4539 } catch (RemoteException e) {
4540 }
4541 }
4542 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004543 case LAUNCH_TASK_BEHIND_COMPLETE: {
4544 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004545 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004546 if (r != null) {
4547 handleLaunchTaskBehindCompleteLocked(r);
4548 }
4549 }
4550 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004551 }
4552 }
4553 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004554
Ying Wangb081a592014-04-22 15:20:16 -07004555 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004556 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004557 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004558 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004559 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004560 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004561 ActivityRecord mParentActivity = null;
4562 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004563
Craig Mautnere3a00d72014-04-16 08:31:19 -07004564 boolean mVisible = true;
4565
Craig Mautner4a1cb222013-12-04 16:14:06 -08004566 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004567 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004568
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004569 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4570 final static int CONTAINER_STATE_NO_SURFACE = 1;
4571 final static int CONTAINER_STATE_FINISHING = 2;
4572 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4573
4574 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004575 synchronized (mService) {
4576 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004577 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004578 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004579 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004580 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004581 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004582
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004583 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004584 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004585 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004586 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004587 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004588 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004589 }
4590
4591 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004592 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004593 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004594 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4595 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004596 return;
4597 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004598 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004599 }
4600 }
4601
4602 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004603 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004604 synchronized (mService) {
4605 if (mActivityDisplay != null) {
4606 return mActivityDisplay.mDisplayId;
4607 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004608 }
4609 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004610 }
4611
Jeff Brownca9bc702014-02-11 14:32:56 -08004612 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004613 public int getStackId() {
4614 synchronized (mService) {
4615 return mStackId;
4616 }
4617 }
4618
4619 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004620 public boolean injectEvent(InputEvent event) {
4621 final long origId = Binder.clearCallingIdentity();
4622 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004623 synchronized (mService) {
4624 if (mActivityDisplay != null) {
4625 return mInputManagerInternal.injectInputEvent(event,
4626 mActivityDisplay.mDisplayId,
4627 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4628 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004629 }
4630 return false;
4631 } finally {
4632 Binder.restoreCallingIdentity(origId);
4633 }
4634 }
4635
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004636 @Override
4637 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004638 synchronized (mService) {
4639 if (mContainerState == CONTAINER_STATE_FINISHING) {
4640 return;
4641 }
4642 mContainerState = CONTAINER_STATE_FINISHING;
4643
Craig Mautnerd163e752014-06-13 17:18:47 -07004644 long origId = Binder.clearCallingIdentity();
4645 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004646 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004647 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004648 } finally {
4649 Binder.restoreCallingIdentity(origId);
4650 }
4651 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004652 }
4653
Craig Mautner60257702014-09-17 15:02:33 -07004654 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004655 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004656 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004657 if (mActivityDisplay != null) {
4658 mActivityDisplay.detachActivitiesLocked(mStack);
4659 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004660 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004661 }
4662 }
4663
4664 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004665 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004666 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004667 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004668 Binder.getCallingUid(), mCurrentUser, false,
4669 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004670
Craig Mautnere0a38842013-12-16 16:14:02 -08004671 // TODO: Switch to user app stacks here.
4672 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004673 final Uri data = intent.getData();
4674 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4675 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004676 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004677 checkEmbeddedAllowedInner(userId, intent, mimeType);
4678
4679 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4680 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004681 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004682 }
4683
4684 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004685 public final int startActivityIntentSender(IIntentSender intentSender)
4686 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004687 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4688
4689 if (!(intentSender instanceof PendingIntentRecord)) {
4690 throw new IllegalArgumentException("Bad PendingIntent object");
4691 }
4692
Craig Mautnerb9168362015-02-26 20:40:19 -08004693 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004694 Binder.getCallingUid(), mCurrentUser, false,
4695 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004696
4697 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4698 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4699 pendingIntent.key.requestResolvedType);
4700
4701 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4702 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4703 }
4704
4705 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004706 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004707 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004708 throw new SecurityException(
4709 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4710 }
4711 }
4712
Craig Mautnerdf88d732014-01-27 09:21:32 -08004713 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004714 public IBinder asBinder() {
4715 return this;
4716 }
4717
Craig Mautner4504de52013-12-20 09:06:56 -08004718 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004719 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004720 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004721 }
4722
Craig Mautner4a1cb222013-12-04 16:14:06 -08004723 ActivityStackSupervisor getOuter() {
4724 return ActivityStackSupervisor.this;
4725 }
4726
Craig Mautnerd163e752014-06-13 17:18:47 -07004727 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004728 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004729 }
4730
Craig Mautner6985bad2014-04-21 15:22:06 -07004731 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004732 void setVisible(boolean visible) {
4733 if (mVisible != visible) {
4734 mVisible = visible;
4735 if (mCallback != null) {
4736 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4737 0 /* unused */, this).sendToTarget();
4738 }
4739 }
4740 }
4741
Craig Mautner6985bad2014-04-21 15:22:06 -07004742 void setDrawn() {
4743 }
4744
Craig Mautner1b4bf852014-05-26 15:06:32 -07004745 // You can always start a new task on a regular ActivityStack.
4746 boolean isEligibleForNewTasks() {
4747 return true;
4748 }
4749
Craig Mautnerd163e752014-06-13 17:18:47 -07004750 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004751 detachLocked();
4752 deleteActivityContainer(this);
4753 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004754 }
4755
Craig Mautner34b73df2014-01-12 21:11:08 -08004756 @Override
4757 public String toString() {
4758 return mIdString + (mActivityDisplay == null ? "N" : "A");
4759 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004760 }
4761
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004762 private class VirtualActivityContainer extends ActivityContainer {
4763 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004764 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004765
4766 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4767 super(getNextStackId());
4768 mParentActivity = parent;
4769 mCallback = callback;
4770 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004771 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004772 }
4773
4774 @Override
4775 public void setSurface(Surface surface, int width, int height, int density) {
4776 super.setSurface(surface, width, height, density);
4777
4778 synchronized (mService) {
4779 final long origId = Binder.clearCallingIdentity();
4780 try {
4781 setSurfaceLocked(surface, width, height, density);
4782 } finally {
4783 Binder.restoreCallingIdentity(origId);
4784 }
4785 }
4786 }
4787
4788 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4789 if (mContainerState == CONTAINER_STATE_FINISHING) {
4790 return;
4791 }
4792 VirtualActivityDisplay virtualActivityDisplay =
4793 (VirtualActivityDisplay) mActivityDisplay;
4794 if (virtualActivityDisplay == null) {
4795 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004796 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004797 mActivityDisplay = virtualActivityDisplay;
4798 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004799 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004800 }
4801
4802 if (mSurface != null) {
4803 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004804 }
4805
Craig Mautner6985bad2014-04-21 15:22:06 -07004806 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004807 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004808 mStack.resumeTopActivityLocked(null);
4809 } else {
4810 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004811 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004812 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004813 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004814 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004815 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004816
Craig Mautnerd163e752014-06-13 17:18:47 -07004817 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004818
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004819 if (DEBUG_STACK) Slog.d(TAG_STACK,
4820 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004821 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004822
Craig Mautner6985bad2014-04-21 15:22:06 -07004823 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004824 boolean isAttachedLocked() {
4825 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004826 }
4827
4828 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004829 void setDrawn() {
4830 synchronized (mService) {
4831 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004832 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004833 }
4834 }
4835
Craig Mautner1b4bf852014-05-26 15:06:32 -07004836 // Never start a new task on an ActivityView if it isn't explicitly specified.
4837 @Override
4838 boolean isEligibleForNewTasks() {
4839 return false;
4840 }
4841
Craig Mautnerd163e752014-06-13 17:18:47 -07004842 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004843 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004844 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4845 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4846 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4847 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4848 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004849 }
4850 }
4851
Craig Mautner4a1cb222013-12-04 16:14:06 -08004852 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4853 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004854 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004855 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004856 int mDisplayId;
4857 Display mDisplay;
4858 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004859
Craig Mautner4a1cb222013-12-04 16:14:06 -08004860 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4861 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004862 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004863
Jose Lima4b6c6692014-08-12 17:41:12 -07004864 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004865
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004866 ActivityDisplay() {
4867 }
Craig Mautner4504de52013-12-20 09:06:56 -08004868
Craig Mautner1a70a162014-09-13 12:09:31 -07004869 // After instantiation, check that mDisplay is not null before using this. The alternative
4870 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004871 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004872 final Display display = mDisplayManager.getDisplay(displayId);
4873 if (display == null) {
4874 return;
4875 }
4876 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004877 }
4878
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004879 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004880 mDisplay = display;
4881 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004882 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004883 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004884
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004885 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004886 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004887 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4888 + " onTop=" + onTop);
4889 if (onTop) {
4890 mStacks.add(stack);
4891 } else {
4892 mStacks.add(0, stack);
4893 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004894 }
4895
Craig Mautnere0a38842013-12-16 16:14:02 -08004896 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004897 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004898 + " from displayId=" + mDisplayId);
4899 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004900 }
4901
Jose Lima4b6c6692014-08-12 17:41:12 -07004902 void setVisibleBehindActivity(ActivityRecord r) {
4903 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004904 }
4905
Jose Lima4b6c6692014-08-12 17:41:12 -07004906 boolean hasVisibleBehindActivity() {
4907 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004908 }
4909
Craig Mautner34b73df2014-01-12 21:11:08 -08004910 @Override
4911 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004912 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4913 }
4914 }
4915
4916 class VirtualActivityDisplay extends ActivityDisplay {
4917 VirtualDisplay mVirtualDisplay;
4918
Craig Mautner6985bad2014-04-21 15:22:06 -07004919 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004920 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004921 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4922 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4923 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4924 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004925
4926 init(mVirtualDisplay.getDisplay());
4927
4928 mWindowManager.handleDisplayAdded(mDisplayId);
4929 }
4930
4931 void setSurface(Surface surface) {
4932 if (mVirtualDisplay != null) {
4933 mVirtualDisplay.setSurface(surface);
4934 }
4935 }
4936
4937 @Override
4938 void detachActivitiesLocked(ActivityStack stack) {
4939 super.detachActivitiesLocked(stack);
4940 if (mVirtualDisplay != null) {
4941 mVirtualDisplay.release();
4942 mVirtualDisplay = null;
4943 }
4944 }
4945
4946 @Override
4947 public String toString() {
4948 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004949 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004950 }
Jose Lima58e66d62014-05-27 20:00:27 -07004951
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004952 /**
4953 * Adjust bounds to stay within stack bounds.
4954 *
4955 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4956 * that keep them unchanged, but be contained within the stack bounds.
4957 *
4958 * @param bounds Bounds to be adjusted.
4959 * @param stackBounds Bounds within which the other bounds should remain.
4960 */
4961 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4962 if (stackBounds == null || stackBounds.contains(bounds)) {
4963 return;
4964 }
4965
4966 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4967 final int maxRight = stackBounds.right
4968 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4969 int horizontalDiff = stackBounds.left - bounds.left;
4970 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4971 || (bounds.left + horizontalDiff >= maxRight)) {
4972 horizontalDiff = maxRight - bounds.left;
4973 }
4974 bounds.left += horizontalDiff;
4975 bounds.right += horizontalDiff;
4976 }
4977
4978 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4979 final int maxBottom = stackBounds.bottom
4980 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4981 int verticalDiff = stackBounds.top - bounds.top;
4982 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4983 || (bounds.top + verticalDiff >= maxBottom)) {
4984 verticalDiff = maxBottom - bounds.top;
4985 }
4986 bounds.top += verticalDiff;
4987 bounds.bottom += verticalDiff;
4988 }
4989 }
4990
Craig Mautner27084302013-03-25 08:05:25 -07004991}