blob: 7e47006ecc2b881ba9aa1b1164a7d339c439bbcc [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 Ogunwalecb82f302015-02-25 07:53:40 -0800461 /** Top of all visible stacks is/should always be equal to the focused stack.
462 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
463 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700464 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700465 if (stack == null) {
466 return false;
467 }
468
Craig Mautnerdf88d732014-01-27 09:21:32 -0800469 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
470 if (parent != null) {
471 stack = parent.task.stack;
472 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800473 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700474 }
475
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700476 void setFocusStack(String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800477 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800478 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800479 if (topNdx <= 0) {
480 return;
481 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700482
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700483 final ActivityStack topStack = stacks.get(topNdx);
484 mFocusedStack = topStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800485 if (lastFocusedStack != null) {
486 mLastFocusedStack = lastFocusedStack;
487 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800488
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700489 EventLogTags.writeAmFocusedStack(
490 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
491 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800492
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800493 if (mService.mBooting || !mService.mBooted) {
494 final ActivityRecord r = topRunningActivityLocked();
495 if (r != null && r.idle) {
496 checkFinishBootingLocked();
497 }
498 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700499 }
500
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700501 void moveHomeStackToFront(String reason) {
502 mHomeStack.moveToFront(reason);
503 }
504
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700505 /** Returns true if the focus activity was adjusted to the home stack top activity. */
506 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700507 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
508 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700509 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700510 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700511
Craig Mautner84984fa2014-06-19 11:19:20 -0700512 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700513
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700514 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700515 if (top == null) {
516 return false;
517 }
518 mService.setFocusedActivityLocked(top, reason);
519 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700520 }
521
Craig Mautner299f9602015-01-26 09:47:33 -0800522 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700523 if (!mService.mBooting && !mService.mBooted) {
524 // Not ready yet!
525 return false;
526 }
527
Craig Mautner84984fa2014-06-19 11:19:20 -0700528 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
529 mWindowManager.showRecentApps();
530 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700531 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700532
Craig Mautner84984fa2014-06-19 11:19:20 -0700533 if (prev != null) {
534 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
535 }
536
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700537 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
538 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800539 // Only resume home activity if isn't finishing.
540 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800541 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700542 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700543 }
Craig Mautner299f9602015-01-26 09:47:33 -0800544 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700545 }
546
Craig Mautner8d341ef2013-03-26 09:03:27 -0700547 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700548 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700549 }
550
551 /**
552 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
553 * @param id Id of the task we would like returned.
554 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
555 * restore the task from recents to the active list.
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700556 * @param stackId The stack to restore the task to (default launch stack will be used
557 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700558 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700559 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800560 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800562 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800563 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
564 ActivityStack stack = stacks.get(stackNdx);
565 TaskRecord task = stack.taskForIdLocked(id);
566 if (task != null) {
567 return task;
568 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700569 }
570 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800571
572 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700573 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800574 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800575 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700576 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800577 return null;
578 }
579
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700580 if (!restoreFromRecents) {
581 return task;
582 }
583
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700584 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700585 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
586 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800587 return null;
588 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700589 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800590 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700591 }
592
Craig Mautner6170f732013-04-02 13:05:23 -0700593 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800594 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800596 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
598 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
599 if (r != null) {
600 return r;
601 }
Craig Mautner6170f732013-04-02 13:05:23 -0700602 }
603 }
604 return null;
605 }
606
Craig Mautneref73ee12014-04-23 11:45:37 -0700607 void setNextTaskId(int taskId) {
608 if (taskId > mCurTaskId) {
609 mCurTaskId = taskId;
610 }
611 }
612
Craig Mautner8d341ef2013-03-26 09:03:27 -0700613 int getNextTaskId() {
614 do {
615 mCurTaskId++;
616 if (mCurTaskId <= 0) {
617 mCurTaskId = 1;
618 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700619 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700620 return mCurTaskId;
621 }
622
Craig Mautnerde4ef022013-04-07 19:01:33 -0700623 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800624 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700625 if (stack == null) {
626 return null;
627 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700628 ActivityRecord resumedActivity = stack.mResumedActivity;
629 if (resumedActivity == null || resumedActivity.app == null) {
630 resumedActivity = stack.mPausingActivity;
631 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700632 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700633 }
634 }
635 return resumedActivity;
636 }
637
Dianne Hackbornff072722014-09-24 10:56:28 -0700638 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700639 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
642 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800643 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
644 final ActivityStack stack = stacks.get(stackNdx);
645 if (!isFrontStack(stack)) {
646 continue;
647 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700648 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800649 if (hr != null) {
650 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
651 && processName.equals(hr.processName)) {
652 try {
George Mount2c92c972014-03-20 09:38:23 -0700653 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800654 didSomething = true;
655 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700656 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800657 Slog.w(TAG, "Exception in new application when starting activity "
658 + hr.intent.getComponent().flattenToShortString(), e);
659 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700660 }
Craig Mautner20e72272013-04-01 13:45:53 -0700661 }
Craig Mautner20e72272013-04-01 13:45:53 -0700662 }
663 }
664 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700665 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700666 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700667 }
Craig Mautner20e72272013-04-01 13:45:53 -0700668 return didSomething;
669 }
670
671 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
673 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
675 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800676 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 continue;
678 }
679 final ActivityRecord resumedActivity = stack.mResumedActivity;
680 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700681 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800682 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 return false;
684 }
Craig Mautner20e72272013-04-01 13:45:53 -0700685 }
686 }
687 return true;
688 }
689
Craig Mautnerde4ef022013-04-07 19:01:33 -0700690 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800691 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
692 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800693 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
694 final ActivityStack stack = stacks.get(stackNdx);
695 if (isFrontStack(stack)) {
696 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700697 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800698 return false;
699 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700700 }
701 }
702 }
703 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700704 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
706 mLastFocusedStack + " to=" + mFocusedStack);
707 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700708 return true;
709 }
710
711 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800712 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800713 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
714 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800715 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
716 final ActivityStack stack = stacks.get(stackNdx);
717 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800718 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700719 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800720 return false;
721 }
722 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800723 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700724 }
725 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800726 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700727 }
728
Craig Mautner2acc3892013-09-23 10:28:14 -0700729 /**
730 * Pause all activities in either all of the stacks or just the back stacks.
731 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700732 * @return true if any activity was paused as a result of this call.
733 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700734 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700735 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800736 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
737 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
739 final ActivityStack stack = stacks.get(stackNdx);
740 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700741 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800742 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700743 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
744 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700746 }
747 }
748 return someActivityPaused;
749 }
750
Craig Mautnerde4ef022013-04-07 19:01:33 -0700751 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700752 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
754 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
756 final ActivityStack stack = stacks.get(stackNdx);
757 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700758 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800759 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700760 Slog.d(TAG_STATES,
761 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800762 pausing = false;
763 } else {
764 return false;
765 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700766 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700767 }
768 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700769 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700770 }
771
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700772 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
773 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500774 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800775 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
776 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
777 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
778 final ActivityStack stack = stacks.get(stackNdx);
779 if (stack.mResumedActivity != null &&
780 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700781 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800782 }
783 }
784 }
785 }
786
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700787 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700788 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700789 }
790
791 void sendWaitingVisibleReportLocked(ActivityRecord r) {
792 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700793 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700794 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700795 if (w.who == null) {
796 changed = true;
797 w.timeout = false;
798 if (r != null) {
799 w.who = new ComponentName(r.info.packageName, r.info.name);
800 }
801 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
802 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700803 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700804 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700805 if (changed) {
806 mService.notifyAll();
807 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700808 }
809
810 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
811 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700812 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700813 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700814 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700815 if (w.who == null) {
816 changed = true;
817 w.timeout = timeout;
818 if (r != null) {
819 w.who = new ComponentName(r.info.packageName, r.info.name);
820 }
821 w.thisTime = thisTime;
822 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700823 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700824 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700825 if (changed) {
826 mService.notifyAll();
827 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700828 }
829
Craig Mautner29219d92013-04-16 20:19:12 -0700830 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800831 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700832 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700833 if (r != null) {
834 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700835 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700836
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800838 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
840 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700841 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700842 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700843 if (r != null) {
844 return r;
845 }
846 }
847 }
848 return null;
849 }
850
Dianne Hackborn09233282014-04-30 11:33:59 -0700851 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700852 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
854 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800855 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800856 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800857 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
859 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700860 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700862 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700863 }
864 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700865
866 // The lists are already sorted from most recent to oldest. Just pull the most recent off
867 // each list and add it to list. Stop when all lists are empty or maxNum reached.
868 while (maxNum > 0) {
869 long mostRecentActiveTime = Long.MIN_VALUE;
870 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800871 final int numTaskLists = runningTaskLists.size();
872 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
873 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700874 if (!stackTaskList.isEmpty()) {
875 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
876 if (lastActiveTime > mostRecentActiveTime) {
877 mostRecentActiveTime = lastActiveTime;
878 selectedStackList = stackTaskList;
879 }
880 }
881 }
882 if (selectedStackList != null) {
883 list.add(selectedStackList.remove(0));
884 --maxNum;
885 } else {
886 break;
887 }
888 }
Craig Mautner20e72272013-04-01 13:45:53 -0700889 }
890
Craig Mautner23ac33b2013-04-01 16:26:35 -0700891 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700892 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700893 // Collect information about the target of the Intent.
894 ActivityInfo aInfo;
895 try {
896 ResolveInfo rInfo =
897 AppGlobals.getPackageManager().resolveIntent(
898 intent, resolvedType,
899 PackageManager.MATCH_DEFAULT_ONLY
900 | ActivityManagerService.STOCK_PM_FLAGS, userId);
901 aInfo = rInfo != null ? rInfo.activityInfo : null;
902 } catch (RemoteException e) {
903 aInfo = null;
904 }
905
906 if (aInfo != null) {
907 // Store the found target back into the intent, because now that
908 // we have it we never want to do this again. For example, if the
909 // user navigates back to this point in the history, we should
910 // always restart the exact same activity.
911 intent.setComponent(new ComponentName(
912 aInfo.applicationInfo.packageName, aInfo.name));
913
914 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700915 if (!aInfo.processName.equals("system")) {
916 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700917 mService.setDebugApp(aInfo.processName, true, false);
918 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700919
Man Caocfa78b22015-06-11 20:14:34 -0700920 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
921 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
922 }
923
924 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700925 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 }
927 }
928 }
929 return aInfo;
930 }
931
Craig Mautner299f9602015-01-26 09:47:33 -0800932 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
933 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700934 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
935 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
936 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
937 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700938 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
939 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700940 null /* outActivity */, null /* container */, null /* inTask */);
941 if (inResumeTopActivity) {
942 // If we are in resume section already, home activity will be initialized, but not
943 // resumed (to avoid recursive resume) and will stay that way until something pokes it
944 // again. We need to schedule another resume.
945 scheduleResumeTopActivities();
946 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700947 }
948
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700950 String callingPackage, Intent intent, String resolvedType,
951 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700952 IBinder resultTo, String resultWho, int requestCode, int startFlags,
953 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700954 Bundle options, boolean ignoreTargetSecurity, int userId,
955 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700956 // Refuse possible leaked file descriptors
957 if (intent != null && intent.hasFileDescriptors()) {
958 throw new IllegalArgumentException("File descriptors passed in Intent");
959 }
960 boolean componentSpecified = intent.getComponent() != null;
961
962 // Don't modify the client's object!
963 intent = new Intent(intent);
964
965 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700966 ActivityInfo aInfo =
967 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700968
Craig Mautnere0a38842013-12-16 16:14:02 -0800969 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700970 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800971 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700972 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800973 // Cannot start a child activity if the parent is not resumed.
974 return ActivityManager.START_CANCELED;
975 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700976 final int realCallingPid = Binder.getCallingPid();
977 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978 int callingPid;
979 if (callingUid >= 0) {
980 callingPid = -1;
981 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700982 callingPid = realCallingPid;
983 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700984 } else {
985 callingPid = callingUid = -1;
986 }
987
Craig Mautnere0a38842013-12-16 16:14:02 -0800988 final ActivityStack stack;
989 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800990 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800991 } else {
992 stack = container.mStack;
993 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800994 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700995 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700996 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700997
998 final long origId = Binder.clearCallingIdentity();
999
1000 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001001 (aInfo.applicationInfo.privateFlags
1002 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001003 // This may be a heavy-weight process! Check to see if we already
1004 // have another, different heavy-weight process running.
1005 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1006 if (mService.mHeavyWeightProcess != null &&
1007 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1008 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001009 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001010 if (caller != null) {
1011 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1012 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001013 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001014 } else {
1015 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001016 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 + intent.toString());
1018 ActivityOptions.abort(options);
1019 return ActivityManager.START_PERMISSION_DENIED;
1020 }
1021 }
1022
1023 IIntentSender target = mService.getIntentSenderLocked(
1024 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001025 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1027 | PendingIntent.FLAG_ONE_SHOT, null);
1028
1029 Intent newIntent = new Intent();
1030 if (requestCode >= 0) {
1031 // Caller is requesting a result.
1032 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1033 }
1034 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1035 new IntentSender(target));
1036 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1037 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1038 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1039 hist.packageName);
1040 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1041 hist.task.taskId);
1042 }
1043 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1044 aInfo.packageName);
1045 newIntent.setFlags(intent.getFlags());
1046 newIntent.setClassName("android",
1047 HeavyWeightSwitcherActivity.class.getName());
1048 intent = newIntent;
1049 resolvedType = null;
1050 caller = null;
1051 callingUid = Binder.getCallingUid();
1052 callingPid = Binder.getCallingPid();
1053 componentSpecified = true;
1054 try {
1055 ResolveInfo rInfo =
1056 AppGlobals.getPackageManager().resolveIntent(
1057 intent, null,
1058 PackageManager.MATCH_DEFAULT_ONLY
1059 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1060 aInfo = rInfo != null ? rInfo.activityInfo : null;
1061 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1062 } catch (RemoteException e) {
1063 aInfo = null;
1064 }
1065 }
1066 }
1067 }
1068
Dianne Hackborn91097de2014-04-04 18:02:06 -07001069 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1070 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001071 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001072 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001073 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001074
Craig Mautner85c11a82014-07-17 12:38:18 -07001075 Binder.restoreCallingIdentity(origId);
1076
Craig Mautnerde4ef022013-04-07 19:01:33 -07001077 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001078 // If the caller also wants to switch to a new configuration,
1079 // do so now. This allows a clean switch, as we are waiting
1080 // for the current activity to pause (so we will not destroy
1081 // it), and have not yet started the next activity.
1082 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1083 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001084 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001085 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001086 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001087 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001088 }
1089
Craig Mautner23ac33b2013-04-01 16:26:35 -07001090 if (outResult != null) {
1091 outResult.result = res;
1092 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001093 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001094 do {
1095 try {
1096 mService.wait();
1097 } catch (InterruptedException e) {
1098 }
1099 } while (!outResult.timeout && outResult.who == null);
1100 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001101 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001102 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001103 outResult.timeout = false;
1104 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1105 outResult.totalTime = 0;
1106 outResult.thisTime = 0;
1107 } else {
1108 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001109 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001110 do {
1111 try {
1112 mService.wait();
1113 } catch (InterruptedException e) {
1114 }
1115 } while (!outResult.timeout && outResult.who == null);
1116 }
1117 }
1118 }
1119
1120 return res;
1121 }
1122 }
1123
1124 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1125 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1126 Bundle options, int userId) {
1127 if (intents == null) {
1128 throw new NullPointerException("intents is null");
1129 }
1130 if (resolvedTypes == null) {
1131 throw new NullPointerException("resolvedTypes is null");
1132 }
1133 if (intents.length != resolvedTypes.length) {
1134 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1135 }
1136
Craig Mautner23ac33b2013-04-01 16:26:35 -07001137
1138 int callingPid;
1139 if (callingUid >= 0) {
1140 callingPid = -1;
1141 } else if (caller == null) {
1142 callingPid = Binder.getCallingPid();
1143 callingUid = Binder.getCallingUid();
1144 } else {
1145 callingPid = callingUid = -1;
1146 }
1147 final long origId = Binder.clearCallingIdentity();
1148 try {
1149 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001150 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001151 for (int i=0; i<intents.length; i++) {
1152 Intent intent = intents[i];
1153 if (intent == null) {
1154 continue;
1155 }
1156
1157 // Refuse possible leaked file descriptors
1158 if (intent != null && intent.hasFileDescriptors()) {
1159 throw new IllegalArgumentException("File descriptors passed in Intent");
1160 }
1161
1162 boolean componentSpecified = intent.getComponent() != null;
1163
1164 // Don't modify the client's object!
1165 intent = new Intent(intent);
1166
1167 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001168 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001169 // TODO: New, check if this is correct
1170 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1171
1172 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001173 (aInfo.applicationInfo.privateFlags
1174 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001175 throw new IllegalArgumentException(
1176 "FLAG_CANT_SAVE_STATE not supported here");
1177 }
1178
1179 Bundle theseOptions;
1180 if (options != null && i == intents.length-1) {
1181 theseOptions = options;
1182 } else {
1183 theseOptions = null;
1184 }
Craig Mautner6170f732013-04-02 13:05:23 -07001185 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001186 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1187 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001188 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001189 if (res < 0) {
1190 return res;
1191 }
1192
1193 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1194 }
1195 }
1196 } finally {
1197 Binder.restoreCallingIdentity(origId);
1198 }
1199
1200 return ActivityManager.START_SUCCESS;
1201 }
1202
Craig Mautner2420ead2013-04-01 17:13:20 -07001203 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001204 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001205 throws RemoteException {
1206
Craig Mautner2568c3a2015-03-26 14:22:34 -07001207 if (andResume) {
1208 r.startFreezingScreenLocked(app, 0);
1209 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001210
Craig Mautner2568c3a2015-03-26 14:22:34 -07001211 // schedule launch ticks to collect information about slow apps.
1212 r.startLaunchTickingLocked();
1213 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001214
1215 // Have the window manager re-evaluate the orientation of
1216 // the screen based on the new activity order. Note that
1217 // as a result of this, it can call back into the activity
1218 // manager with a new orientation. We don't care about that,
1219 // because the activity is not currently running so we are
1220 // just restarting it anyway.
1221 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001222 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001223 mService.mConfiguration,
1224 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001225 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001226 }
1227
1228 r.app = app;
1229 app.waitingToKill = null;
1230 r.launchCount++;
1231 r.lastLaunchTime = SystemClock.uptimeMillis();
1232
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001233 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001234
1235 int idx = app.activities.indexOf(r);
1236 if (idx < 0) {
1237 app.activities.add(r);
1238 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001239 mService.updateLruProcessLocked(app, true, null);
1240 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001241
Craig Mautner15df08a2015-04-01 12:17:18 -07001242 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001243 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1244 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001245 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001246 }
1247
1248 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 try {
1250 if (app.thread == null) {
1251 throw new RemoteException();
1252 }
1253 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001254 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 if (andResume) {
1256 results = r.results;
1257 newIntents = r.newIntents;
1258 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001259 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1260 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1261 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 if (andResume) {
1263 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1264 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001265 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001266 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001267 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001268 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001269 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001270 }
1271 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1272 r.sleeping = false;
1273 r.forceNewConfig = false;
1274 mService.showAskCompatModeDialogLocked(r);
1275 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001276 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1278 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1279 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001280 final String profileFile = mService.mProfileFile;
1281 if (profileFile != null) {
1282 ParcelFileDescriptor profileFd = mService.mProfileFd;
1283 if (profileFd != null) {
1284 try {
1285 profileFd = profileFd.dup();
1286 } catch (IOException e) {
1287 if (profileFd != null) {
1288 try {
1289 profileFd.close();
1290 } catch (IOException o) {
1291 }
1292 profileFd = null;
1293 }
1294 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001296
1297 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1298 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001299 }
1300 }
1301 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001302
Craig Mautner2568c3a2015-03-26 14:22:34 -07001303 if (andResume) {
1304 app.hasShownUi = true;
1305 app.pendingUiClean = true;
1306 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001307 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001308 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001309 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001310 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001311 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001312 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001313
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001314 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001315 // This may be a heavy-weight process! Note that the package
1316 // manager will ensure that only activity can run in the main
1317 // process of the .apk, which is the only thing that will be
1318 // considered heavy-weight.
1319 if (app.processName.equals(app.info.packageName)) {
1320 if (mService.mHeavyWeightProcess != null
1321 && mService.mHeavyWeightProcess != app) {
1322 Slog.w(TAG, "Starting new heavy weight process " + app
1323 + " when already running "
1324 + mService.mHeavyWeightProcess);
1325 }
1326 mService.mHeavyWeightProcess = app;
1327 Message msg = mService.mHandler.obtainMessage(
1328 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1329 msg.obj = r;
1330 mService.mHandler.sendMessage(msg);
1331 }
1332 }
1333
1334 } catch (RemoteException e) {
1335 if (r.launchFailed) {
1336 // This is the second time we failed -- finish activity
1337 // and give up.
1338 Slog.e(TAG, "Second failure launching "
1339 + r.intent.getComponent().flattenToShortString()
1340 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001341 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001342 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1343 "2nd-crash", false);
1344 return false;
1345 }
1346
1347 // This is the first time we failed -- restart process and
1348 // retry.
1349 app.activities.remove(r);
1350 throw e;
1351 }
1352
1353 r.launchFailed = false;
1354 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001355 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 }
1357
1358 if (andResume) {
1359 // As part of the process of launching, ActivityThread also performs
1360 // a resume.
1361 stack.minimalResumeActivityLocked(r);
1362 } else {
1363 // This activity is not starting in the resumed state... which
1364 // should look like we asked it to pause+stop (but remain visible),
1365 // and it has done so and reported back the current icicle and
1366 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001367 if (DEBUG_STATES) Slog.v(TAG_STATES,
1368 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001369 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001370 r.stopped = true;
1371 }
1372
1373 // Launch the new version setup screen if needed. We do this -after-
1374 // launching the initial activity (that is, home), so that it can have
1375 // a chance to initialize itself while in the background, making the
1376 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001377 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001378 mService.startSetupActivityLocked();
1379 }
1380
Dianne Hackborn465fa392014-09-14 14:21:18 -07001381 // Update any services we are bound to that might care about whether
1382 // their client may have activities.
1383 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1384
Craig Mautner2420ead2013-04-01 17:13:20 -07001385 return true;
1386 }
1387
Craig Mautnere79d42682013-04-01 19:01:53 -07001388 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001389 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001390 // Is this activity's application already running?
1391 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001392 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001393
1394 r.task.stack.setLaunchTime(r);
1395
1396 if (app != null && app.thread != null) {
1397 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001398 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1399 || !"android".equals(r.info.packageName)) {
1400 // Don't add this if it is a platform component that is marked
1401 // to run in multiple processes, because this is actually
1402 // part of the framework so doesn't make sense to track as a
1403 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001404 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1405 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001406 }
George Mount2c92c972014-03-20 09:38:23 -07001407 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001408 return;
1409 } catch (RemoteException e) {
1410 Slog.w(TAG, "Exception when starting activity "
1411 + r.intent.getComponent().flattenToShortString(), e);
1412 }
1413
1414 // If a dead object exception was thrown -- fall through to
1415 // restart the application.
1416 }
1417
1418 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001419 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001420 }
1421
Craig Mautner6170f732013-04-02 13:05:23 -07001422 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001423 Intent intent, String resolvedType, ActivityInfo aInfo,
1424 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1425 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001426 int callingPid, int callingUid, String callingPackage,
1427 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001428 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1429 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001430 int err = ActivityManager.START_SUCCESS;
1431
1432 ProcessRecord callerApp = null;
1433 if (caller != null) {
1434 callerApp = mService.getRecordForAppLocked(caller);
1435 if (callerApp != null) {
1436 callingPid = callerApp.pid;
1437 callingUid = callerApp.info.uid;
1438 } else {
1439 Slog.w(TAG, "Unable to find app for caller " + caller
1440 + " (pid=" + callingPid + ") when starting: "
1441 + intent.toString());
1442 err = ActivityManager.START_PERMISSION_DENIED;
1443 }
1444 }
1445
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001446 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1447
Craig Mautner6170f732013-04-02 13:05:23 -07001448 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001449 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001450 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001451 + " on display " + (container == null ? (mFocusedStack == null ?
1452 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1453 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1454 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001455 }
1456
1457 ActivityRecord sourceRecord = null;
1458 ActivityRecord resultRecord = null;
1459 if (resultTo != null) {
1460 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001461 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1462 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001463 if (sourceRecord != null) {
1464 if (requestCode >= 0 && !sourceRecord.finishing) {
1465 resultRecord = sourceRecord;
1466 }
1467 }
1468 }
Craig Mautner6170f732013-04-02 13:05:23 -07001469
Dianne Hackborn91097de2014-04-04 18:02:06 -07001470 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001471
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001472 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001473 // Transfer the result target from the source activity to the new
1474 // one being started, including any failures.
1475 if (requestCode >= 0) {
1476 ActivityOptions.abort(options);
1477 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1478 }
1479 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001480 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1481 resultRecord = null;
1482 }
Craig Mautner6170f732013-04-02 13:05:23 -07001483 resultWho = sourceRecord.resultWho;
1484 requestCode = sourceRecord.requestCode;
1485 sourceRecord.resultTo = null;
1486 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001487 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001488 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001489 if (sourceRecord.launchedFromUid == callingUid) {
1490 // The new activity is being launched from the same uid as the previous
1491 // activity in the flow, and asking to forward its result back to the
1492 // previous. In this case the activity is serving as a trampoline between
1493 // the two, so we also want to update its launchedFromPackage to be the
1494 // same as the previous activity. Note that this is safe, since we know
1495 // these two packages come from the same uid; the caller could just as
1496 // well have supplied that same package name itself. This specifially
1497 // deals with the case of an intent picker/chooser being launched in the app
1498 // flow to redirect to an activity picked by the user, where we want the final
1499 // activity to consider it to have been launched by the previous app activity.
1500 callingPackage = sourceRecord.launchedFromPackage;
1501 }
Craig Mautner6170f732013-04-02 13:05:23 -07001502 }
1503
1504 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1505 // We couldn't find a class that can handle the given Intent.
1506 // That's the end of that!
1507 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1508 }
1509
1510 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1511 // We couldn't find the specific class specified in the Intent.
1512 // Also the end of the line.
1513 err = ActivityManager.START_CLASS_NOT_FOUND;
1514 }
1515
Dianne Hackborn91097de2014-04-04 18:02:06 -07001516 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1517 && sourceRecord.task.voiceSession != null) {
1518 // If this activity is being launched as part of a voice session, we need
1519 // to ensure that it is safe to do so. If the upcoming activity will also
1520 // be part of the voice session, we can only launch it if it has explicitly
1521 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001522 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001523 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1524 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001525 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001526 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1527 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001528 Slog.w(TAG,
1529 "Activity being started in current voice task does not support voice: "
1530 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001531 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1532 }
1533 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001534 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001535 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1536 }
1537 }
1538 }
1539
1540 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1541 // If the caller is starting a new voice session, just make sure the target
1542 // is actually allowing it to run this way.
1543 try {
1544 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1545 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001546 Slog.w(TAG,
1547 "Activity being started in new voice task does not support: "
1548 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001549 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1550 }
1551 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001552 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001553 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1554 }
1555 }
1556
louis_changcd5d1982014-08-01 10:09:08 +08001557 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1558
Craig Mautner6170f732013-04-02 13:05:23 -07001559 if (err != ActivityManager.START_SUCCESS) {
1560 if (resultRecord != null) {
1561 resultStack.sendActivityResultLocked(-1,
1562 resultRecord, resultWho, requestCode,
1563 Activity.RESULT_CANCELED, null);
1564 }
Craig Mautner6170f732013-04-02 13:05:23 -07001565 ActivityOptions.abort(options);
1566 return err;
1567 }
1568
Svetoslav7008b512015-06-24 18:47:07 -07001569 boolean abort = false;
1570
Svet Ganov99b60432015-06-27 13:15:22 -07001571 final int startAnyPerm = mService.checkPermission(
1572 START_ANY_ACTIVITY, callingPid, callingUid);
1573
1574 if (startAnyPerm != PERMISSION_GRANTED) {
1575 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001576 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001577 final int actionRestriction = getActionRestrictionForCallingPackage(
1578 intent.getAction(), callingPackage, callingPid, callingUid);
1579
1580 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1581 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1582 if (resultRecord != null) {
1583 resultStack.sendActivityResultLocked(-1,
1584 resultRecord, resultWho, requestCode,
1585 Activity.RESULT_CANCELED, null);
1586 }
1587 String msg;
1588 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1589 msg = "Permission Denial: starting " + intent.toString()
1590 + " from " + callerApp + " (pid=" + callingPid
1591 + ", uid=" + callingUid + ")" + " with revoked permission "
1592 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1593 } else if (!aInfo.exported) {
1594 msg = "Permission Denial: starting " + intent.toString()
1595 + " from " + callerApp + " (pid=" + callingPid
1596 + ", uid=" + callingUid + ")"
1597 + " not exported from uid " + aInfo.applicationInfo.uid;
1598 } else {
1599 msg = "Permission Denial: starting " + intent.toString()
1600 + " from " + callerApp + " (pid=" + callingPid
1601 + ", uid=" + callingUid + ")"
1602 + " requires " + aInfo.permission;
1603 }
1604 Slog.w(TAG, msg);
1605 throw new SecurityException(msg);
1606 }
1607
1608 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1609 String message = "Appop Denial: starting " + intent.toString()
1610 + " from " + callerApp + " (pid=" + callingPid
1611 + ", uid=" + callingUid + ")"
1612 + " requires " + AppOpsManager.permissionToOp(
1613 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1614 Slog.w(TAG, message);
1615 abort = true;
1616 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1617 String message = "Appop Denial: starting " + intent.toString()
1618 + " from " + callerApp + " (pid=" + callingPid
1619 + ", uid=" + callingUid + ")"
1620 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1621 Slog.w(TAG, message);
1622 abort = true;
1623 }
Svetoslav7008b512015-06-24 18:47:07 -07001624 }
1625
1626 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001627 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001628
Craig Mautner6170f732013-04-02 13:05:23 -07001629 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001630 try {
1631 // The Intent we give to the watcher has the extra data
1632 // stripped off, since it can contain private information.
1633 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001634 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001635 aInfo.applicationInfo.packageName);
1636 } catch (RemoteException e) {
1637 mService.mController = null;
1638 }
Ben Gruver5e207332013-04-03 17:41:37 -07001639 }
Craig Mautner6170f732013-04-02 13:05:23 -07001640
Ben Gruver5e207332013-04-03 17:41:37 -07001641 if (abort) {
1642 if (resultRecord != null) {
1643 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001644 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001645 }
Ben Gruver5e207332013-04-03 17:41:37 -07001646 // We pretend to the caller that it was really started, but
1647 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001648 ActivityOptions.abort(options);
1649 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001650 }
1651
1652 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001653 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001654 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001655 if (outActivity != null) {
1656 outActivity[0] = r;
1657 }
1658
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001659 if (r.appTimeTracker == null && sourceRecord != null) {
1660 // If the caller didn't specify an explicit time tracker, we want to continue
1661 // tracking under any it has.
1662 r.appTimeTracker = sourceRecord.appTimeTracker;
1663 }
1664
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001665 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001666 if (voiceSession == null && (stack.mResumedActivity == null
1667 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001668 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1669 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001670 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001671 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001672 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001673 ActivityOptions.abort(options);
1674 return ActivityManager.START_SWITCHES_CANCELED;
1675 }
1676 }
1677
1678 if (mService.mDidAppSwitch) {
1679 // This is the second allowed switch since we stopped switches,
1680 // so now just generally allow switches. Use case: user presses
1681 // home (switches disabled, switch to home, mDidAppSwitch now true);
1682 // user taps a home icon (coming from home so allowed, we hit here
1683 // and now allow anyone to switch again).
1684 mService.mAppSwitchesAllowedTime = 0;
1685 } else {
1686 mService.mDidAppSwitch = true;
1687 }
1688
Craig Mautneree36c772014-07-16 14:56:05 -07001689 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001690
Dianne Hackborn91097de2014-04-04 18:02:06 -07001691 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001692 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001693
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001694 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001695 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001696 // activity start, but we are not actually doing an activity
1697 // switch... just dismiss the keyguard now, because we
1698 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001699 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001700 }
1701 return err;
1702 }
1703
Svet Ganov99b60432015-06-27 13:15:22 -07001704 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001705 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001706 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1707 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001708 == PackageManager.PERMISSION_DENIED) {
1709 return ACTIVITY_RESTRICTION_PERMISSION;
1710 }
1711
Christopher Tateff7add02015-08-17 10:23:22 -07001712 if (activityInfo.permission == null) {
1713 return ACTIVITY_RESTRICTION_NONE;
1714 }
1715
Svet Ganov99b60432015-06-27 13:15:22 -07001716 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1717 if (opCode == AppOpsManager.OP_NONE) {
1718 return ACTIVITY_RESTRICTION_NONE;
1719 }
1720
1721 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1722 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001723 if (!ignoreTargetSecurity) {
1724 return ACTIVITY_RESTRICTION_APPOP;
1725 }
Svet Ganov99b60432015-06-27 13:15:22 -07001726 }
1727
1728 return ACTIVITY_RESTRICTION_NONE;
1729 }
1730
Svetoslav7008b512015-06-24 18:47:07 -07001731 private int getActionRestrictionForCallingPackage(String action,
1732 String callingPackage, int callingPid, int callingUid) {
1733 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001734 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001735 }
1736
1737 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1738 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001739 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001740 }
1741
1742 final PackageInfo packageInfo;
1743 try {
1744 packageInfo = mService.mContext.getPackageManager()
1745 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1746 } catch (PackageManager.NameNotFoundException e) {
1747 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001748 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001749 }
1750
1751 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001752 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001753 }
1754
1755 if (mService.checkPermission(permission, callingPid, callingUid) ==
1756 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001757 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001758 }
1759
1760 final int opCode = AppOpsManager.permissionToOpCode(permission);
1761 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001762 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001763 }
1764
1765 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1766 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001767 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001768 }
1769
Svet Ganov99b60432015-06-27 13:15:22 -07001770 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001771 }
1772
Chong Zhang0fa656b2015-08-31 15:17:21 -07001773 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001774 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001775
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001776 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1777 return mHomeStack;
1778 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001779
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001780 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001781
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001782 if (task != null && task.stack != null) {
1783 stack = task.stack;
1784 if (stack.isOnHomeDisplay()) {
1785 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001786 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001787 "computeStackFocus: Setting " + "focused stack to r=" + r
1788 + " task=" + task);
1789 } else {
1790 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1791 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001792 }
1793 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001794 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001795 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001796
1797 final ActivityContainer container = r.mInitialActivityContainer;
1798 if (container != null) {
1799 // The first time put it on the desired stack, after this put on task stack.
1800 r.mInitialActivityContainer = null;
1801 return container.mStack;
1802 }
1803
1804 // The fullscreen stack can contain any task regardless of if the task is resizeable
1805 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1806 // If the freeform stack has focus, and the activity to be launched is resizeable,
1807 // we can also put it in the focused stack.
1808 final boolean canUseFocusedStack =
1809 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1810 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1811 if (canUseFocusedStack
1812 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1813 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1814 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1815 return mFocusedStack;
1816 }
1817
1818 // We first try to put the task in the first dynamic stack.
1819 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1820 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1821 stack = homeDisplayStacks.get(stackNdx);
1822 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1823 if (isDynamicStack) {
1824 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1825 "computeStackFocus: Setting focused stack=" + stack);
1826 return stack;
1827 }
1828 }
1829
1830 // If there is no suitable dynamic stack then we figure out which static stack to use.
1831 final int stackId = task != null ? task.getLaunchStackId() :
1832 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1833 FULLSCREEN_WORKSPACE_STACK_ID;
1834 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1835 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1836 + r + " stackId=" + stack.mStackId);
1837 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001838 }
1839
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001840 boolean setFocusedStack(ActivityRecord r, String reason) {
1841 if (r == null) {
1842 // Not sure what you are trying to do, but it is not going to work...
1843 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001844 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001845 final TaskRecord task = r.task;
1846 if (task == null || task.stack == null) {
1847 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1848 return false;
1849 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001850 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001851 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001852 }
1853
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001854 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001855 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001856 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001857 final Intent intent = r.intent;
1858 final int callingUid = r.launchedFromUid;
1859
Chong Zhang0fa656b2015-08-31 15:17:21 -07001860 boolean overrideBounds = false;
1861 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001862 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
1863 ActivityOptions opts = new ActivityOptions(options);
1864 if (opts.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001865 overrideBounds = true;
Chong Zhang56f171d2015-10-08 10:39:23 -07001866 newBounds = opts.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001867 }
1868 }
1869
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001870 // In some flows in to this function, we retrieve the task record and hold on to it
1871 // without a lock before calling back in to here... so the task at this point may
1872 // not actually be in recents. Check for that, and if it isn't in recents just
1873 // consider it invalid.
1874 if (inTask != null && !inTask.inRecents) {
1875 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1876 inTask = null;
1877 }
1878
Craig Mautnerad400af2014-08-13 16:41:36 -07001879 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001880 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1881 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001882
Craig Mautnera228ae92014-07-09 05:44:55 -07001883 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001884 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001885 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001886 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1887 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1888 "\"singleInstance\" or \"singleTask\"");
1889 launchFlags &=
1890 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1891 } else {
1892 switch (r.info.documentLaunchMode) {
1893 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1894 break;
1895 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1896 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1897 break;
1898 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1899 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1900 break;
1901 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1902 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1903 break;
1904 }
1905 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001906
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001907 final boolean launchTaskBehind = r.mLaunchTaskBehind
1908 && !launchSingleTask && !launchSingleInstance
1909 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001910
Wale Ogunwale7d701172015-03-11 15:36:30 -07001911 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1912 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001913 // For whatever reason this activity is being launched into a new
1914 // task... yet the caller has requested a result back. Well, that
1915 // is pretty messed up, so instead immediately send back a cancel
1916 // and let the new task continue launched as normal without a
1917 // dependency on its originator.
1918 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1919 r.resultTo.task.stack.sendActivityResultLocked(-1,
1920 r.resultTo, r.resultWho, r.requestCode,
1921 Activity.RESULT_CANCELED, null);
1922 r.resultTo = null;
1923 }
1924
1925 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1926 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1927 }
1928
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001929 // If we are actually going to launch in to a new task, there are some cases where
1930 // we further want to do multiple task.
1931 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1932 if (launchTaskBehind
1933 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1934 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1935 }
1936 }
1937
Craig Mautner8849a5e2013-04-02 16:41:03 -07001938 // We'll invoke onUserLeaving before onPause only if the launching
1939 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001940 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001941 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1942 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001943
1944 // If the caller has asked not to resume at this point, we make note
1945 // of this in the record so that we can skip it when trying to find
1946 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001947 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001948 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001949 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001950 }
1951
Craig Mautnera254cd72014-05-25 16:47:39 -07001952 ActivityRecord notTop =
1953 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001954
1955 // If the onlyIfNeeded flag is set, then we can do this if the activity
1956 // being launched is the same as the one making the call... or, as
1957 // a special case, if we do not know the caller then we count the
1958 // current top activity as the caller.
1959 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1960 ActivityRecord checkedCaller = sourceRecord;
1961 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001962 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963 }
1964 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1965 // Caller is not the same as launcher, so always needed.
1966 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1967 }
1968 }
1969
Craig Mautner8849a5e2013-04-02 16:41:03 -07001970 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001971 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001972
1973 // If the caller is not coming from another activity, but has given us an
1974 // explicit task into which they would like us to launch the new activity,
1975 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001976 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1977 final Intent baseIntent = inTask.getBaseIntent();
1978 final ActivityRecord root = inTask.getRootActivity();
1979 if (baseIntent == null) {
1980 ActivityOptions.abort(options);
1981 throw new IllegalArgumentException("Launching into task without base intent: "
1982 + inTask);
1983 }
1984
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001985 // If this task is empty, then we are adding the first activity -- it
1986 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001987 if (launchSingleInstance || launchSingleTask) {
1988 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1989 ActivityOptions.abort(options);
1990 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1991 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001992 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001993 if (root != null) {
1994 ActivityOptions.abort(options);
1995 throw new IllegalArgumentException("Caller with inTask " + inTask
1996 + " has root " + root + " but target is singleInstance/Task");
1997 }
1998 }
1999
2000 // If task is empty, then adopt the interesting intent launch flags in to the
2001 // activity being started.
2002 if (root == null) {
2003 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2004 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2005 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2006 launchFlags = (launchFlags&~flagsOfInterest)
2007 | (baseIntent.getFlags()&flagsOfInterest);
2008 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002009 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002010 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002011
Dianne Hackborn962d5352014-08-29 16:27:40 -07002012 // If the task is not empty and the caller is asking to start it as the root
2013 // of a new task, then we don't actually want to start this on the task. We
2014 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002015 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002016 addingToTask = false;
2017
2018 } else {
2019 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002020 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002021
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002022 reuseTask = inTask;
2023 } else {
2024 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002025 // Launch ResolverActivity in the source task, so that it stays in the task
2026 // bounds when in freeform workspace.
2027 // Also put noDisplay activities in the source task. These by itself can
2028 // be placed in any task/stack, however it could launch other activities
2029 // like ResolverActivity, and we want those to stay in the original task.
2030 if (r.isResolverActivity() || r.noDisplay) {
2031 addingToTask = true;
2032 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002033 }
2034
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002035 if (inTask == null) {
2036 if (sourceRecord == null) {
2037 // This activity is not being started from another... in this
2038 // case we -always- start a new task.
2039 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2040 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2041 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2042 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2043 }
2044 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2045 // The original activity who is starting us is running as a single
2046 // instance... this new activity it is starting must go on its
2047 // own task.
2048 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2049 } else if (launchSingleInstance || launchSingleTask) {
2050 // The activity being started is a single instance... it always
2051 // gets launched into its own task.
2052 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2053 }
2054 }
2055
2056 ActivityInfo newTaskInfo = null;
2057 Intent newTaskIntent = null;
2058 ActivityStack sourceStack;
2059 if (sourceRecord != null) {
2060 if (sourceRecord.finishing) {
2061 // If the source is finishing, we can't further count it as our source. This
2062 // is because the task it is associated with may now be empty and on its way out,
2063 // so we don't want to blindly throw it in to that task. Instead we will take
2064 // the NEW_TASK flow and try to find a task for it. But save the task information
2065 // so it can be used when creating the new task.
2066 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2067 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2068 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2069 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2070 newTaskInfo = sourceRecord.info;
2071 newTaskIntent = sourceRecord.task.intent;
2072 }
2073 sourceRecord = null;
2074 sourceStack = null;
2075 } else {
2076 sourceStack = sourceRecord.task.stack;
2077 }
2078 } else {
2079 sourceStack = null;
2080 }
2081
2082 boolean movedHome = false;
2083 ActivityStack targetStack;
2084
2085 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002086 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002087
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002088 // We may want to try to place the new activity in to an existing task. We always
2089 // do this if the target activity is singleTask or singleInstance; we will also do
2090 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2091 // us to still place it in a new task: multi task, always doc mode, or being asked to
2092 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002093 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2094 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002095 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002096 // If bring to front is requested, and no result is requested and we have not
2097 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002098 // we can find a task that was started with this same
2099 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002100 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002101 // See if there is a task to bring to the front. If this is
2102 // a SINGLE_INSTANCE activity, there can be one and only one
2103 // instance of it in the history, and it is always in its own
2104 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002105 ActivityRecord intentActivity = !launchSingleInstance ?
2106 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002107 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002108 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2109 // but still needs to be a lock task mode violation since the task gets
2110 // cleared out and the device would otherwise leave the locked task.
2111 if (isLockTaskModeViolation(intentActivity.task,
2112 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2113 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002114 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002115 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002116 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2117 }
Craig Mautner29219d92013-04-16 20:19:12 -07002118 if (r.task == null) {
2119 r.task = intentActivity.task;
2120 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 if (intentActivity.task.intent == null) {
2122 // This task was started because of movement of
2123 // the activity based on affinity... now that we
2124 // are actually launching it, we can assign the
2125 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002126 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002127 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002128 targetStack = intentActivity.task.stack;
2129 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002130 // If the target task is not in the front, then we need
2131 // to bring it to the front... except... well, with
2132 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2133 // to have the same behavior as if a new instance was
2134 // being started, which means not bringing it to the front
2135 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002136 final ActivityStack focusStack = getFocusedStack();
2137 ActivityRecord curTop = (focusStack == null)
2138 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002139 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002140 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002141 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002142 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002143 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2144 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002145 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002146 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002147 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2148 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002149 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002150 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002151 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002152 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002153 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002154 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2155 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002156 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002157 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002158 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002159 options = null;
2160 }
2161 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002162 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002163 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002164 + " from " + intentActivity);
2165 targetStack.moveToFront("intentActivityFound");
2166 }
2167
Craig Mautner8849a5e2013-04-02 16:41:03 -07002168 // If the caller has requested that the target task be
2169 // reset, then do so.
2170 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2171 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2172 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002173 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002174 // We don't need to start a new activity, and
2175 // the client said not to do anything if that
2176 // is the case, so this is it! And for paranoia, make
2177 // sure we have correctly resumed the top activity.
2178 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002179 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002180
2181 // Make sure to notify Keyguard as well if we are not running an app
2182 // transition later.
2183 if (!movedToFront) {
2184 notifyActivityDrawnForKeyguard();
2185 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186 } else {
2187 ActivityOptions.abort(options);
2188 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002189 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002190 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2191 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002192 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002193 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002194 // The caller has requested to completely replace any
2195 // existing task with its new activity. Well that should
2196 // not be too hard...
2197 reuseTask = intentActivity.task;
2198 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002199 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002200 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002201 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002202 // In this situation we want to remove all activities
2203 // from the task up to the one being started. In most
2204 // cases this means we are resetting the task to its
2205 // initial state.
2206 ActivityRecord top =
2207 intentActivity.task.performClearTaskLocked(r, launchFlags);
2208 if (top != null) {
2209 if (top.frontOfTask) {
2210 // Activity aliases may mean we use different
2211 // intents for the top activity, so make sure
2212 // the task now has the identity of the new
2213 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002214 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002215 }
2216 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2217 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002218 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002219 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002220 // A special case: we need to start the activity because it is not
2221 // currently running, and the caller has asked to clear the current
2222 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002223 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002224 // Now pretend like this activity is being started by the top of its
2225 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002227 TaskRecord task = sourceRecord.task;
2228 if (task != null && task.stack == null) {
2229 // Target stack got cleared when we all activities were removed
2230 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002231 targetStack = computeStackFocus(
2232 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002233 targetStack.addTask(
2234 task, !launchTaskBehind /* toTop */, false /* moving */);
2235 }
2236
Craig Mautner8849a5e2013-04-02 16:41:03 -07002237 }
2238 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2239 // In this case the top activity on the task is the
2240 // same as the one being launched, so we take that
2241 // as a request to bring the task to the foreground.
2242 // If the top activity in the task is the root
2243 // activity, deliver this new intent to it if it
2244 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002245 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002246 && intentActivity.realActivity.equals(r.realActivity)) {
2247 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2248 intentActivity.task);
2249 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002250 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002251 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002252 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2253 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002254 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2255 // In this case we are launching the root activity
2256 // of the task, but with a different intent. We
2257 // should start a new instance on top.
2258 addingToTask = true;
2259 sourceRecord = intentActivity;
2260 }
2261 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2262 // In this case an activity is being launched in to an
2263 // existing task, without resetting that task. This
2264 // is typically the situation of launching an activity
2265 // from a notification or shortcut. We want to place
2266 // the new activity on top of the current task.
2267 addingToTask = true;
2268 sourceRecord = intentActivity;
2269 } else if (!intentActivity.task.rootWasReset) {
2270 // In this case we are launching in to an existing task
2271 // that has not yet been started from its front door.
2272 // The current task has been brought to the front.
2273 // Ideally, we'd probably like to place this new task
2274 // at the bottom of its stack, but that's a little hard
2275 // to do with the current organization of the code so
2276 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002277 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002278 }
2279 if (!addingToTask && reuseTask == null) {
2280 // We didn't do anything... but it was needed (a.k.a., client
2281 // don't use that intent!) And for paranoia, make
2282 // sure we have correctly resumed the top activity.
2283 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002284 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002285 if (!movedToFront) {
2286 // Make sure to notify Keyguard as well if we are not running an app
2287 // transition later.
2288 notifyActivityDrawnForKeyguard();
2289 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002290 } else {
2291 ActivityOptions.abort(options);
2292 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002293 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002294 return ActivityManager.START_TASK_TO_FRONT;
2295 }
2296 }
2297 }
2298 }
2299
2300 //String uri = r.intent.toURI();
2301 //Intent intent2 = new Intent(uri);
2302 //Slog.i(TAG, "Given intent: " + r.intent);
2303 //Slog.i(TAG, "URI is: " + uri);
2304 //Slog.i(TAG, "To intent: " + intent2);
2305
2306 if (r.packageName != null) {
2307 // If the activity being launched is the same as the one currently
2308 // at the top, then we need to check if it should only be launched
2309 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002310 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002311 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002312 if (top != null && r.resultTo == null) {
2313 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2314 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002315 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002316 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002317 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2318 top.task);
2319 // For paranoia, make sure we have correctly
2320 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002321 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002322 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002323 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002324 }
2325 ActivityOptions.abort(options);
2326 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2327 // We don't need to start a new activity, and
2328 // the client said not to do anything if that
2329 // is the case, so this is it!
2330 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2331 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002332 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002333 return ActivityManager.START_DELIVERED_TO_TOP;
2334 }
2335 }
2336 }
2337 }
2338
2339 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002340 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002341 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2342 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002343 }
2344 ActivityOptions.abort(options);
2345 return ActivityManager.START_CLASS_NOT_FOUND;
2346 }
2347
2348 boolean newTask = false;
2349 boolean keepCurTransition = false;
2350
Craig Mautnerbb742462014-07-07 15:28:55 -07002351 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002352 sourceRecord.task : null;
2353
Craig Mautner8849a5e2013-04-02 16:41:03 -07002354 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002355 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002356 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002357 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002358 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002359 if (doResume) {
2360 targetStack.moveToFront("startingNewTask");
2361 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002362
Craig Mautner8849a5e2013-04-02 16:41:03 -07002363 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002364 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2365 newTaskInfo != null ? newTaskInfo : r.info,
2366 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002367 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2368 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002369 if (overrideBounds) {
2370 r.task.updateOverrideConfiguration(newBounds);
2371 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002372 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2373 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002374 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002375 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002376 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002377 if (isLockTaskModeViolation(r.task)) {
2378 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2379 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2380 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002381 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002382 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002383 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2384 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002385 // Caller wants to appear on home activity, so before starting
2386 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002387 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002388 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002389 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002390 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002391 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002392 if (isLockTaskModeViolation(sourceTask)) {
2393 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2394 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2395 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002396 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002397 if (doResume) {
2398 targetStack.moveToFront("sourceStackToFront");
2399 }
Craig Mautner737fae22014-10-15 12:52:10 -07002400 final TaskRecord topTask = targetStack.topTask();
2401 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002402 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002403 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002404 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002405 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002406 // In this case, we are adding the activity to an existing
2407 // task, but the caller has asked to clear that task if the
2408 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002409 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002410 keepCurTransition = true;
2411 if (top != null) {
2412 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002413 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002414 // For paranoia, make sure we have correctly
2415 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002416 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002417 if (doResume) {
2418 targetStack.resumeTopActivityLocked(null);
2419 }
2420 ActivityOptions.abort(options);
2421 return ActivityManager.START_DELIVERED_TO_TOP;
2422 }
2423 } else if (!addingToTask &&
2424 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2425 // In this case, we are launching an activity in our own task
2426 // that may already be running somewhere in the history, and
2427 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002428 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002429 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002430 final TaskRecord task = top.task;
2431 task.moveActivityToFrontLocked(top);
2432 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002433 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002434 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002435 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002436 if (doResume) {
2437 targetStack.resumeTopActivityLocked(null);
2438 }
2439 return ActivityManager.START_DELIVERED_TO_TOP;
2440 }
2441 }
2442 // An existing activity is starting this new activity, so we want
2443 // to keep the new one in the same task as the one that is starting
2444 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002445 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002446 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002447 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002448
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002449 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002450 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002451 // task it has provided to us.
2452 if (isLockTaskModeViolation(inTask)) {
2453 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2454 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2455 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002456 if (overrideBounds) {
2457 inTask.updateOverrideConfiguration(newBounds);
2458 int stackId = inTask.getLaunchStackId();
2459 if (stackId != inTask.stack.mStackId) {
2460 moveTaskToStackUncheckedLocked(
2461 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2462 }
2463 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002464 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002465 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2466 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002467
2468 // Check whether we should actually launch the new activity in to the task,
2469 // or just reuse the current activity on top.
2470 ActivityRecord top = inTask.getTopActivity();
2471 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2472 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2473 || launchSingleTop || launchSingleTask) {
2474 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2475 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2476 // We don't need to start a new activity, and
2477 // the client said not to do anything if that
2478 // is the case, so this is it!
2479 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2480 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002481 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002482 return ActivityManager.START_DELIVERED_TO_TOP;
2483 }
2484 }
2485
Dianne Hackborn962d5352014-08-29 16:27:40 -07002486 if (!addingToTask) {
2487 // We don't actually want to have this activity added to the task, so just
2488 // stop here but still tell the caller that we consumed the intent.
2489 ActivityOptions.abort(options);
2490 return ActivityManager.START_TASK_TO_FRONT;
2491 }
2492
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002493 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002494 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002495 + " in explicit task " + r.task);
2496
Craig Mautner8849a5e2013-04-02 16:41:03 -07002497 } else {
2498 // This not being started from an existing activity, and not part
2499 // of a new task... just put it in the top task, though these days
2500 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002501 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002502 if (doResume) {
2503 targetStack.moveToFront("addingToTopTask");
2504 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002505 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002506 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002507 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002508 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002509 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002510 + " in new guessed " + r.task);
2511 }
2512
2513 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002514 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002515
Craig Mautner76e2a762014-06-24 09:05:43 -07002516 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2517 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2518 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002519 if (newTask) {
2520 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2521 }
2522 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002523 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002524 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002525 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002526 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002527 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002528 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002529 return ActivityManager.START_SUCCESS;
2530 }
2531
Craig Mautneree36c772014-07-16 14:56:05 -07002532 final void doPendingActivityLaunchesLocked(boolean doResume) {
2533 while (!mPendingActivityLaunches.isEmpty()) {
2534 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002535
2536 try {
2537 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
2538 doResume && mPendingActivityLaunches.isEmpty(), null, null);
2539 } catch (Exception e) {
2540 Slog.w(TAG, "Exception during pending activity launch pal=" + pal, e);
2541 }
Craig Mautneree36c772014-07-16 14:56:05 -07002542 }
2543 }
2544
Craig Mautner7f13ed32014-07-28 14:00:35 -07002545 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002546 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2547 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002548 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002549 mPendingActivityLaunches.remove(palNdx);
2550 }
2551 }
2552 }
2553
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002554 void setLaunchSource(int uid) {
2555 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2556 }
2557
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002558 void acquireLaunchWakelock() {
2559 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2560 throw new IllegalStateException("Calling must be system uid");
2561 }
2562 mLaunchingActivity.acquire();
2563 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2564 // To be safe, don't allow the wake lock to be held for too long.
2565 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2566 }
2567 }
2568
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002569 /**
2570 * Called when the frontmost task is idle.
2571 * @return the state of mService.mBooting before this was called.
2572 */
2573 private boolean checkFinishBootingLocked() {
2574 final boolean booting = mService.mBooting;
2575 boolean enableScreen = false;
2576 mService.mBooting = false;
2577 if (!mService.mBooted) {
2578 mService.mBooted = true;
2579 enableScreen = true;
2580 }
2581 if (booting || enableScreen) {
2582 mService.postFinishBooting(booting, enableScreen);
2583 }
2584 return booting;
2585 }
2586
Craig Mautnerf3333272013-04-22 10:55:53 -07002587 // Checked.
2588 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2589 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002590 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002591
Craig Mautnerf3333272013-04-22 10:55:53 -07002592 ArrayList<ActivityRecord> stops = null;
2593 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002594 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002595 int NS = 0;
2596 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002597 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002598 boolean activityRemoved = false;
2599
Wale Ogunwale7d701172015-03-11 15:36:30 -07002600 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002601 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002602 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2603 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002604 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2605 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002606 if (fromTimeout) {
2607 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002608 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002609
2610 // This is a hack to semi-deal with a race condition
2611 // in the client where it can be constructed with a
2612 // newer configuration from when we asked it to launch.
2613 // We'll update with whatever configuration it now says
2614 // it used to launch.
2615 if (config != null) {
2616 r.configuration = config;
2617 }
2618
2619 // We are now idle. If someone is waiting for a thumbnail from
2620 // us, we can now deliver.
2621 r.idle = true;
2622
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002623 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002624 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002625 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002626 }
2627 }
2628
2629 if (allResumedActivitiesIdle()) {
2630 if (r != null) {
2631 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002632 }
2633
2634 if (mLaunchingActivity.isHeld()) {
2635 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2636 if (VALIDATE_WAKE_LOCK_CALLER &&
2637 Binder.getCallingUid() != Process.myUid()) {
2638 throw new IllegalStateException("Calling must be system uid");
2639 }
2640 mLaunchingActivity.release();
2641 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002642 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002643 }
2644
2645 // Atomically retrieve all of the other things to do.
2646 stops = processStoppingActivitiesLocked(true);
2647 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002648 if ((NF = mFinishingActivities.size()) > 0) {
2649 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002650 mFinishingActivities.clear();
2651 }
2652
Craig Mautnerf3333272013-04-22 10:55:53 -07002653 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002654 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002655 mStartingUsers.clear();
2656 }
2657
Craig Mautnerf3333272013-04-22 10:55:53 -07002658 // Stop any activities that are scheduled to do so but have been
2659 // waiting for the next one to start.
2660 for (int i = 0; i < NS; i++) {
2661 r = stops.get(i);
2662 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002663 if (stack != null) {
2664 if (r.finishing) {
2665 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2666 } else {
2667 stack.stopActivityLocked(r);
2668 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002669 }
2670 }
2671
2672 // Finish any activities that are scheduled to do so but have been
2673 // waiting for the next one to start.
2674 for (int i = 0; i < NF; i++) {
2675 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002676 final ActivityStack stack = r.task.stack;
2677 if (stack != null) {
2678 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2679 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002680 }
2681
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002682 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002683 // Complete user switch
2684 if (startingUsers != null) {
2685 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002686 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002687 }
2688 }
2689 // Complete starting up of background users
2690 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002691 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002692 mStartingBackgroundUsers.clear();
2693 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002694 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002695 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002696 }
2697 }
2698
2699 mService.trimApplications();
2700 //dump();
2701 //mWindowManager.dump();
2702
Craig Mautnerf3333272013-04-22 10:55:53 -07002703 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002704 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002705 }
2706
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002707 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002708 }
2709
Craig Mautner8e569572013-10-11 17:36:59 -07002710 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002711 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002712 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2713 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002714 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2715 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2716 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002717 }
Craig Mautner19091252013-10-05 00:03:53 -07002718 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002719 }
2720
2721 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2723 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2725 stacks.get(stackNdx).closeSystemDialogsLocked();
2726 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002727 }
2728 }
2729
Craig Mautner93529a42013-10-04 15:03:13 -07002730 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002731 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002732 }
2733
Craig Mautner8d341ef2013-03-26 09:03:27 -07002734 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002735 * Update the last used stack id for non-current user (current user's last
2736 * used stack is the focused stack)
2737 */
2738 void updateUserStackLocked(int userId, ActivityStack stack) {
2739 if (userId != mCurrentUser) {
2740 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2741 }
2742 }
2743
2744 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002745 * @return true if some activity was finished (or would have finished if doit were true).
2746 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002747 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2748 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002749 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2751 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002752 final int numStacks = stacks.size();
2753 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2754 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);
2777 if (isFrontStack(stack)) {
2778 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;
Craig Mautner12ff7392014-02-21 21:08:00 -08002809 if (isFrontStack(targetStack)) {
2810 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 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002821 if (isFrontStack(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);
2995
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002996 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002997
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002998 mTmpBounds.clear();
2999 mTmpConfigs.clear();
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003000 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3001 for (int i = tasks.size() - 1; i >= 0; i--) {
3002 TaskRecord task = tasks.get(i);
3003 if (task.mResizeable) {
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003004 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003005 // For freeform stack we don't adjust the size of the tasks to match that
3006 // of the stack, but we do try to make sure the tasks are still contained
3007 // with the bounds of the stack.
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003008 tempRect2.set(task.mBounds);
3009 fitWithinBounds(tempRect2, bounds);
3010 task.updateOverrideConfiguration(tempRect2);
3011 } else {
3012 task.updateOverrideConfiguration(bounds);
3013 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003014 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003015
3016 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3017 mTmpBounds.put(task.taskId, task.mBounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003018 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003019 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003020 if (stack.mStackId == DOCKED_STACK_ID) {
3021 // Dock stack funness...Yay!
3022 if (stack.mFullscreen) {
3023 // The dock stack went fullscreen which is kinda like dismissing it.
3024 // In this case we make all other static stacks fullscreen and move all
3025 // docked stack tasks to the fullscreen stack.
3026 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwalea4e86602015-10-08 18:09:02 -07003027 if (i != DOCKED_STACK_ID && getStack(i) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003028 resizeStackLocked(i, null, preserveWindows, true);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003029 }
3030 }
3031
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003032 final int count = tasks.size();
3033 for (int i = 0; i < count; i++) {
3034 moveTaskToStackLocked(tasks.get(i).taskId,
3035 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
3036 }
3037
3038 // stack shouldn't contain anymore activities, so nothing to resume.
3039 r = null;
3040 } else {
3041 // Docked stacks occupy a dedicated region on screen so the size of all other
3042 // static stacks need to be adjusted so they don't overlap with the docked stack.
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003043 // We get the bounds to use from window manager which has been adjusted for any
3044 // screen controls and is also the same for all stacks.
3045 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003046
3047 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3048 if (i != DOCKED_STACK_ID) {
3049 ActivityStack otherStack = getStack(i);
3050 if (otherStack != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003051 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003052 }
3053 }
3054 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003055 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003056 // Since we are resizing the stack, all other operations should strive to preserve
3057 // windows.
3058 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003059 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003060 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003061
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003062 if (r != null) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003063 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003064 // And we need to make sure at this point that all other activities
3065 // are made visible with the correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003066 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003067 if (!updated) {
3068 resumeTopActivitiesLocked(stack, null, null);
3069 }
3070 }
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003071
3072 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003073 }
3074
Chong Zhang6de2ae82015-09-30 18:25:21 -07003075 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003076 if (!task.mResizeable) {
3077 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3078 return;
3079 }
3080
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003081 adjustForMinimalTaskDimensions(task, bounds);
3082
Chong Zhang87b21722015-09-21 15:39:51 -07003083 // If this is a forced resize, let it go through even if the bounds is not changing,
3084 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003085 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003086 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003087 // Nothing to do here...
3088 return;
3089 }
3090
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003091 if (!mWindowManager.isValidTaskId(task.taskId)) {
3092 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003093 // All we can do for now is update the bounds so it can be used when the task is
3094 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003095 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003096 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3097 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003098 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003099 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003100 return;
3101 }
3102
Chong Zhang6de2ae82015-09-30 18:25:21 -07003103 // Do not move the task to another stack here.
3104 // This method assumes that the task is already placed in the right stack.
3105 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003106
Chong Zhang6de2ae82015-09-30 18:25:21 -07003107 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003108
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003109 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003110 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003111 // way and the activity was kept the way it was. If it's false, it means the activity had
3112 // to be relaunched due to configuration change.
3113 boolean kept = true;
3114 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003115 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003116 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003117 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003118 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003119 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003120 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003121 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003122 resumeTopActivitiesLocked(stack, null, null);
3123 }
3124 }
3125 }
Chong Zhang87b21722015-09-21 15:39:51 -07003126 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003127
3128 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003129 }
3130
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003131 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3132 if (bounds == null) {
3133 return;
3134 }
3135 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3136 : task.mMinimalSize;
3137 final boolean adjustWidth = minimalSize > bounds.width();
3138 final boolean adjustHeight = minimalSize > bounds.height();
3139 if (!(adjustWidth || adjustHeight)) {
3140 return;
3141 }
3142 Rect taskBounds = task.mBounds;
3143 if (adjustWidth) {
3144 if (taskBounds != null && bounds.right == taskBounds.right) {
3145 bounds.left = bounds.right - minimalSize;
3146 } else {
3147 // Either left bounds match, or neither match, or the previous bounds were
3148 // fullscreen and we default to keeping left.
3149 bounds.right = bounds.left + minimalSize;
3150 }
3151 }
3152 if (adjustHeight) {
3153 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3154 bounds.top = bounds.bottom - minimalSize;
3155 } else {
3156 // Either top bounds match, or neither match, or the previous bounds were
3157 // fullscreen and we default to keeping top.
3158 bounds.bottom = bounds.top + minimalSize;
3159 }
3160 }
3161 }
3162
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003163 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003164 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3165 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003166 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003167 }
3168
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003169 ActivityContainer activityContainer = new ActivityContainer(stackId);
3170 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003171 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003172 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003173 }
3174
3175 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003176 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003177 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3178 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003179 break;
3180 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003181 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003182 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003183 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003184 }
3185
Chong Zhang5dcb2752015-08-18 13:50:26 -07003186 /**
3187 * Restores a recent task to a stack
3188 * @param task The recent task to be restored.
3189 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003190 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003191 * @return true if the task has been restored successfully.
3192 */
3193 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3194 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003195 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003196 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003197 if (task.stack != null) {
3198 // Task has already been restored once. See if we need to do anything more
3199 if (task.stack.mStackId == stackId) {
3200 // Nothing else to do since it is already restored in the right stack.
3201 return true;
3202 }
3203 // Remove current stack association, so we can re-associate the task with the
3204 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003205 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003206 }
3207
3208 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003209 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003210
3211 if (stack == null) {
3212 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003213 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3214 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003215 return false;
3216 }
3217
3218 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003219 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3220 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003221 final ArrayList<ActivityRecord> activities = task.mActivities;
3222 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003223 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003224 }
3225 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003226 }
3227
Wale Ogunwale040b4702015-08-06 18:10:50 -07003228 /**
3229 * Moves the specified task record to the input stack id.
3230 * WARNING: This method performs an unchecked/raw move of the task and
3231 * can leave the system in an unstable state if used incorrectly.
3232 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3233 * to a stack.
3234 * @param task Task to move.
3235 * @param stackId Id of stack to move task to.
3236 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003237 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003238 * @param reason Reason the task is been moved.
3239 * @return The stack the task was moved to.
3240 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003241 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003242 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3243 final ActivityRecord r = task.getTopActivity();
3244 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3245 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3246
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003247 final boolean resizeable = task.mResizeable;
3248 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3249 // if a docked stack is created below which will lead to the stack we are moving from and
3250 // its resizeable tasks being resized.
3251 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003252 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003253 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003254 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3255 if (task.stack != null) {
3256 task.stack.removeTask(task, reason, MOVING);
3257 }
3258 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003259
3260 // If the task had focus before (or we're requested to move focus),
3261 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003262 stack.setFocusAndResumeStateIfNeeded(
3263 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003264
Wale Ogunwale040b4702015-08-06 18:10:50 -07003265 return stack;
3266 }
3267
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003268 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003269 final TaskRecord task = anyTaskForIdLocked(taskId);
3270 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003271 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003272 return;
3273 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003274 final String reason = "moveTaskToStack";
Wale Ogunwale99db1862015-10-23 20:08:22 -07003275 if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID
3276 || stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003277 // We are about to relaunch the activity because its configuration changed due to
3278 // being maximized, i.e. size change. The activity will first remove the old window
3279 // and then add a new one. This call will tell window manager about this, so it can
3280 // preserve the old window until the new one is drawn. This prevents having a gap
3281 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003282 // entrance of the new window to be properly animated.
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003283 ActivityRecord r = task.getTopActivity();
3284 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3285 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003286 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003287 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003288
3289 // Make sure the task has the appropriate bounds/size for the stack it is in.
3290 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003291 resizeTaskLocked(task, stack.mBounds,
3292 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003293 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3294 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003295 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3296 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003297 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003298 resizeTaskLocked(task, stack.mBounds,
3299 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003300 }
3301
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003302 // The task might have already been running and its visibility needs to be synchronized with
3303 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003304 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003305 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003306 }
3307
Wale Ogunwale079a0042015-10-24 11:44:07 -07003308 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3309 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3310 if (stack == null) {
3311 throw new IllegalArgumentException(
3312 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3313 }
3314
3315 final ActivityRecord r = stack.topRunningActivityLocked();
3316 if (r == null) {
3317 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3318 + " in stack=" + stack);
3319 return false;
3320 }
3321
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003322 if (!r.info.supportsPip) {
3323 Slog.w(TAG,
3324 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003325 + " r=" + r);
3326 return false;
3327 }
3328
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003329 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003330 if (task.mActivities.size() == 1) {
3331 // There is only one activity in the task. So, we can just move the task over to the
3332 // pinned stack without re-parenting the activity in a different task.
3333 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS);
3334 } else {
3335 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3336 pinnedStack.moveActivityToStack(r);
3337 }
3338
3339 resizeStackLocked(PINNED_STACK_ID, bounds, PRESERVE_WINDOWS, true);
3340
3341 // The task might have already been running and its visibility needs to be synchronized with
3342 // the visibility of the stack / windows.
3343 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3344 resumeTopActivitiesLocked();
3345 return true;
3346 }
3347
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003348 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3349 final TaskRecord task = anyTaskForIdLocked(taskId);
3350 if (task == null) {
3351 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3352 return;
3353 }
3354 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003355 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003356 mWindowManager.positionTaskInStack(taskId, stackId, position);
3357 final boolean stackChanged = task.stack != null && task.stack != stack;
3358 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003359 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003360 }
3361 stack.positionTask(task, position, stackChanged);
3362 // The task might have already been running and its visibility needs to be synchronized with
3363 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003364 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003365 resumeTopActivitiesLocked();
3366 }
3367
Craig Mautnerac6f8432013-07-17 13:24:59 -07003368 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003369 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003370 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3371 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003372 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3373 final ActivityStack stack = stacks.get(stackNdx);
3374 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003375 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003376 continue;
3377 }
3378 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003379 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3380 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003381 continue;
3382 }
3383 final ActivityRecord ar = stack.findTaskLocked(r);
3384 if (ar != null) {
3385 return ar;
3386 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003387 }
3388 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003389 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003390 return null;
3391 }
3392
3393 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003394 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3395 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003396 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3397 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3398 if (ar != null) {
3399 return ar;
3400 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003401 }
3402 }
3403 return null;
3404 }
3405
Craig Mautner8d341ef2013-03-26 09:03:27 -07003406 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003407 scheduleSleepTimeout();
3408 if (!mGoingToSleep.isHeld()) {
3409 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003410 if (mLaunchingActivity.isHeld()) {
3411 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3412 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003413 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003414 mLaunchingActivity.release();
3415 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003416 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003417 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003418 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003419 }
3420
3421 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003422 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003423
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003424 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003425 final long endTime = System.currentTimeMillis() + timeout;
3426 while (true) {
3427 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003428 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3429 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003430 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3431 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3432 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003433 }
3434 if (cantShutdown) {
3435 long timeRemaining = endTime - System.currentTimeMillis();
3436 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003437 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003438 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003439 } catch (InterruptedException e) {
3440 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003441 } else {
3442 Slog.w(TAG, "Activity manager shutdown timed out");
3443 timedout = true;
3444 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003445 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003446 } else {
3447 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003448 }
3449 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003450
3451 // Force checkReadyForSleep to complete.
3452 mSleepTimeout = true;
3453 checkReadyForSleepLocked();
3454
Craig Mautner8d341ef2013-03-26 09:03:27 -07003455 return timedout;
3456 }
3457
3458 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003459 removeSleepTimeouts();
3460 if (mGoingToSleep.isHeld()) {
3461 mGoingToSleep.release();
3462 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003463 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3464 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003465 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3466 final ActivityStack stack = stacks.get(stackNdx);
3467 stack.awakeFromSleepingLocked();
3468 if (isFrontStack(stack)) {
3469 resumeTopActivitiesLocked();
3470 }
Craig Mautner5314a402013-09-26 12:40:16 -07003471 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003472 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003473 mGoingToSleepActivities.clear();
3474 }
3475
3476 void activitySleptLocked(ActivityRecord r) {
3477 mGoingToSleepActivities.remove(r);
3478 checkReadyForSleepLocked();
3479 }
3480
3481 void checkReadyForSleepLocked() {
3482 if (!mService.isSleepingOrShuttingDown()) {
3483 // Do not care.
3484 return;
3485 }
3486
3487 if (!mSleepTimeout) {
3488 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003489 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3490 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003491 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3492 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3493 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003494 }
3495
3496 if (mStoppingActivities.size() > 0) {
3497 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003498 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003499 + mStoppingActivities.size() + " activities");
3500 scheduleIdleLocked();
3501 dontSleep = true;
3502 }
3503
3504 if (mGoingToSleepActivities.size() > 0) {
3505 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003506 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003507 + mGoingToSleepActivities.size() + " activities");
3508 dontSleep = true;
3509 }
3510
3511 if (dontSleep) {
3512 return;
3513 }
3514 }
3515
Craig Mautnere0a38842013-12-16 16:14:02 -08003516 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3517 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003518 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3519 stacks.get(stackNdx).goToSleep();
3520 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003521 }
3522
3523 removeSleepTimeouts();
3524
3525 if (mGoingToSleep.isHeld()) {
3526 mGoingToSleep.release();
3527 }
3528 if (mService.mShuttingDown) {
3529 mService.notifyAll();
3530 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003531 }
3532
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003533 boolean reportResumedActivityLocked(ActivityRecord r) {
3534 final ActivityStack stack = r.task.stack;
3535 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003536 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003537 }
3538 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003539 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003540 mWindowManager.executeAppTransition();
3541 return true;
3542 }
3543 return false;
3544 }
3545
Craig Mautner8d341ef2013-03-26 09:03:27 -07003546 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003547 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3548 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003549 int stackNdx = stacks.size() - 1;
3550 while (stackNdx >= 0) {
3551 stacks.get(stackNdx).handleAppCrashLocked(app);
3552 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003553 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003554 }
3555 }
3556
Jose Lima4b6c6692014-08-12 17:41:12 -07003557 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003558 final ActivityStack stack = r.task.stack;
3559 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003560 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3561 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003562 return false;
3563 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003564 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003565 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3566 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003567
3568 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003569 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003570 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003571 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003572 return true;
3573 }
3574
3575 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003576 if (visible && top.fullscreen) {
3577 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003578 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3579 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3580 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3581 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003582 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003583 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3584 // Only the activity set as currently visible behind should actively reset its
3585 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003586 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3587 "requestVisibleBehind: returning visible=" + visible
3588 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3589 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003590 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003591 }
3592
Jose Lima4b6c6692014-08-12 17:41:12 -07003593 stack.setVisibleBehindActivity(visible ? r : null);
3594 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003595 // Make the activity immediately above r opaque.
3596 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3597 if (next != null) {
3598 mService.convertFromTranslucent(next.appToken);
3599 }
3600 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003601 if (top.app != null && top.app.thread != null) {
3602 // Notify the top app of the change.
3603 try {
3604 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3605 } catch (RemoteException e) {
3606 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003607 }
3608 return true;
3609 }
3610
Craig Mautnerbb742462014-07-07 15:28:55 -07003611 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3612 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3613 r.mLaunchTaskBehind = false;
3614 final TaskRecord task = r.task;
3615 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003616 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003617 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003618 mWindowManager.setAppVisibility(r.appToken, false);
3619 }
3620
3621 void scheduleLaunchTaskBehindComplete(IBinder token) {
3622 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3623 }
3624
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003625 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3626 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003627 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003628 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3629 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003630 final int topStackNdx = stacks.size() - 1;
3631 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3632 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003633 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003634 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003635 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003636 }
3637
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003638 void invalidateTaskLayers() {
3639 mTaskLayersChanged = true;
3640 }
3641
3642 void rankTaskLayersIfNeeded() {
3643 if (!mTaskLayersChanged) {
3644 return;
3645 }
3646 mTaskLayersChanged = false;
3647 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3648 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3649 int baseLayer = 0;
3650 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3651 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3652 }
3653 }
3654 }
3655
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003656 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3657 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3658 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3659 final int topStackNdx = stacks.size() - 1;
3660 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3661 final ActivityStack stack = stacks.get(stackNdx);
3662 stack.clearOtherAppTimeTrackers(except);
3663 }
3664 }
3665 }
3666
Craig Mautner8d341ef2013-03-26 09:03:27 -07003667 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003668 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3669 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003670 final int numStacks = stacks.size();
3671 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3672 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003673 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003674 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003675 }
3676 }
3677
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003678 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3679 // Examine all activities currently running in the process.
3680 TaskRecord firstTask = null;
3681 // Tasks is non-null only if two or more tasks are found.
3682 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003683 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3684 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003685 ActivityRecord r = app.activities.get(i);
3686 // First, if we find an activity that is in the process of being destroyed,
3687 // then we just aren't going to do anything for now; we want things to settle
3688 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003689 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003690 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003691 return;
3692 }
3693 // Don't consider any activies that are currently not in a state where they
3694 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003695 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3696 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003697 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003698 continue;
3699 }
3700 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003701 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003702 + " from " + r);
3703 if (firstTask == null) {
3704 firstTask = r.task;
3705 } else if (firstTask != r.task) {
3706 if (tasks == null) {
3707 tasks = new ArraySet<>();
3708 tasks.add(firstTask);
3709 }
3710 tasks.add(r.task);
3711 }
3712 }
3713 }
3714 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003715 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003716 return;
3717 }
3718 // If we have activities in multiple tasks that are in a position to be destroyed,
3719 // let's iterate through the tasks and release the oldest one.
3720 final int numDisplays = mActivityDisplays.size();
3721 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3722 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3723 // Step through all stacks starting from behind, to hit the oldest things first.
3724 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3725 final ActivityStack stack = stacks.get(stackNdx);
3726 // Try to release activities in this stack; if we manage to, we are done.
3727 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3728 return;
3729 }
3730 }
3731 }
3732 }
3733
Amith Yamasani37a40c22015-06-17 13:25:42 -07003734 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003735 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003736 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003737 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003738
Craig Mautner858d8a62013-04-23 17:08:34 -07003739 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003740 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3741 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003742 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003743 final ActivityStack stack = stacks.get(stackNdx);
3744 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003745 TaskRecord task = stack.topTask();
3746 if (task != null) {
3747 mWindowManager.moveTaskToTop(task.taskId);
3748 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003749 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003750 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003751
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003752 ActivityStack stack = getStack(restoreStackId);
3753 if (stack == null) {
3754 stack = mHomeStack;
3755 }
3756 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003757 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003758 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003759 } else {
3760 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003761 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003762 }
Craig Mautner93529a42013-10-04 15:03:13 -07003763 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003764 }
3765
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003766 /**
3767 * Add background users to send boot completed events to.
3768 * @param userId The user being started in the background
3769 * @param uss The state object for the user.
3770 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003771 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003772 mStartingBackgroundUsers.add(uss);
3773 }
3774
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003775 /** Checks whether the userid is a profile of the current user. */
3776 boolean isCurrentProfileLocked(int userId) {
3777 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003778 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003779 }
3780
Chong Zhang45c25ce2015-08-10 22:18:26 -07003781 /** Checks whether the activity should be shown for current user. */
3782 boolean okToShowLocked(ActivityRecord r) {
3783 return r != null && (isCurrentProfileLocked(r.userId)
3784 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3785 }
3786
Craig Mautnerde4ef022013-04-07 19:01:33 -07003787 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003788 ArrayList<ActivityRecord> stops = null;
3789
3790 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003791 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3792 ActivityRecord s = mStoppingActivities.get(activityNdx);
3793 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003794 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003795 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3796 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003797 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003798 if (s.finishing) {
3799 // If this activity is finishing, it is sitting on top of
3800 // everyone else but we now know it is no longer needed...
3801 // so get rid of it. Otherwise, we need to go through the
3802 // normal flow and hide it once we determine that it is
3803 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003804 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003805 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003806 }
3807 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003808 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003809 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003810 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003811 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003812 }
3813 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003814 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003815 }
3816 }
3817
3818 return stops;
3819 }
3820
Craig Mautnercf910b02013-04-23 11:23:27 -07003821 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003822 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3823 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3824 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3825 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003826 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003827 final ActivityState state = r == null ? DESTROYED : r.state;
3828 if (isFrontStack(stack)) {
3829 if (r == null) Slog.e(TAG,
3830 "validateTop...: null top activity, stack=" + stack);
3831 else {
3832 final ActivityRecord pausing = stack.mPausingActivity;
3833 if (pausing != null && pausing == r) Slog.e(TAG,
3834 "validateTop...: top stack has pausing activity r=" + r
3835 + " state=" + state);
3836 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3837 "validateTop...: activity in front not resumed r=" + r
3838 + " state=" + state);
3839 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003840 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003841 final ActivityRecord resumed = stack.mResumedActivity;
3842 if (resumed != null && resumed == r) Slog.e(TAG,
3843 "validateTop...: back stack has resumed activity r=" + r
3844 + " state=" + state);
3845 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3846 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003847 }
3848 }
3849 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003850 }
3851
Craig Mautnere0570202015-05-13 13:06:11 -07003852 private String lockTaskModeToString() {
3853 switch (mLockTaskModeState) {
3854 case LOCK_TASK_MODE_LOCKED:
3855 return "LOCKED";
3856 case LOCK_TASK_MODE_PINNED:
3857 return "PINNED";
3858 case LOCK_TASK_MODE_NONE:
3859 return "NONE";
3860 default: return "unknown=" + mLockTaskModeState;
3861 }
3862 }
3863
Craig Mautner27084302013-03-25 08:05:25 -07003864 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003865 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003866 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003867 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3868 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3869 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003870 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003871 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3872 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3873 if (packages.size() > 0) {
3874 pw.println(" mLockTaskPackages (userId:packages)=");
3875 for (int i = 0; i < packages.size(); ++i) {
3876 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3877 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3878 }
3879 }
3880 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003881 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003882
Craig Mautner20e72272013-04-01 13:45:53 -07003883 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003884 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003885 }
3886
Dianne Hackborn390517b2013-05-30 15:03:32 -07003887 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3888 boolean needSep, String prefix) {
3889 if (activity != null) {
3890 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3891 if (needSep) {
3892 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003893 }
3894 pw.print(prefix);
3895 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003896 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003897 }
3898 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003899 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003900 }
3901
Craig Mautner8d341ef2013-03-26 09:03:27 -07003902 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3903 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003904 boolean printed = false;
3905 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003906 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3907 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003908 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003909 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003910 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003911 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003912 final ActivityStack stack = stacks.get(stackNdx);
3913 StringBuilder stackHeader = new StringBuilder(128);
3914 stackHeader.append(" Stack #");
3915 stackHeader.append(stack.mStackId);
3916 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003917 stackHeader.append("\n");
3918 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3919 stackHeader.append("\n");
3920 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003921 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3922 needSep, stackHeader.toString());
3923 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3924 !dumpAll, false, dumpPackage, true,
3925 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003926
Craig Mautner4a1cb222013-12-04 16:14:06 -08003927 needSep = printed;
3928 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3929 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003930 if (pr) {
3931 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003932 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003933 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3935 " mResumedActivity: ");
3936 if (pr) {
3937 printed = true;
3938 needSep = false;
3939 }
3940 if (dumpAll) {
3941 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3942 " mLastPausedActivity: ");
3943 if (pr) {
3944 printed = true;
3945 needSep = true;
3946 }
3947 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3948 needSep, " mLastNoHistoryActivity: ");
3949 }
3950 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003951 }
3952 }
3953
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003954 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3955 false, dumpPackage, true, " Activities waiting to finish:", null);
3956 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3957 false, dumpPackage, true, " Activities waiting to stop:", null);
3958 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3959 false, dumpPackage, true, " Activities waiting for another to become visible:",
3960 null);
3961 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3962 false, dumpPackage, true, " Activities waiting to sleep:", null);
3963 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3964 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003965
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003966 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003967 }
3968
Dianne Hackborn390517b2013-05-30 15:03:32 -07003969 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003970 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003971 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003972 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003973 String innerPrefix = null;
3974 String[] args = null;
3975 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003976 for (int i=list.size()-1; i>=0; i--) {
3977 final ActivityRecord r = list.get(i);
3978 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3979 continue;
3980 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003981 if (innerPrefix == null) {
3982 innerPrefix = prefix + " ";
3983 args = new String[0];
3984 }
3985 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003986 final boolean full = !brief && (complete || !r.isInHistory());
3987 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003988 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003989 needNL = false;
3990 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003991 if (header1 != null) {
3992 pw.println(header1);
3993 header1 = null;
3994 }
3995 if (header2 != null) {
3996 pw.println(header2);
3997 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003998 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003999 if (lastTask != r.task) {
4000 lastTask = r.task;
4001 pw.print(prefix);
4002 pw.print(full ? "* " : " ");
4003 pw.println(lastTask);
4004 if (full) {
4005 lastTask.dump(pw, prefix + " ");
4006 } else if (complete) {
4007 // Complete + brief == give a summary. Isn't that obvious?!?
4008 if (lastTask.intent != null) {
4009 pw.print(prefix); pw.print(" ");
4010 pw.println(lastTask.intent.toInsecureStringWithClip());
4011 }
4012 }
4013 }
4014 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4015 pw.print(" #"); pw.print(i); pw.print(": ");
4016 pw.println(r);
4017 if (full) {
4018 r.dump(pw, innerPrefix);
4019 } else if (complete) {
4020 // Complete + brief == give a summary. Isn't that obvious?!?
4021 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4022 if (r.app != null) {
4023 pw.print(innerPrefix); pw.println(r.app);
4024 }
4025 }
4026 if (client && r.app != null && r.app.thread != null) {
4027 // flush anything that is already in the PrintWriter since the thread is going
4028 // to write to the file descriptor directly
4029 pw.flush();
4030 try {
4031 TransferPipe tp = new TransferPipe();
4032 try {
4033 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4034 r.appToken, innerPrefix, args);
4035 // Short timeout, since blocking here can
4036 // deadlock with the application.
4037 tp.go(fd, 2000);
4038 } finally {
4039 tp.kill();
4040 }
4041 } catch (IOException e) {
4042 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4043 } catch (RemoteException e) {
4044 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4045 }
4046 needNL = true;
4047 }
4048 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004049 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004050 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004051
Craig Mautnerf3333272013-04-22 10:55:53 -07004052 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004053 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4054 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004055 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4056 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004057 }
4058
4059 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004060 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004061 }
4062
4063 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004064 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4065 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004066 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4067 }
4068
Craig Mautner05d29032013-05-03 13:40:13 -07004069 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004070 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4071 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4072 }
Craig Mautner05d29032013-05-03 13:40:13 -07004073 }
4074
Craig Mautner0eea92c2013-05-16 13:35:39 -07004075 void removeSleepTimeouts() {
4076 mSleepTimeout = false;
4077 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4078 }
4079
4080 final void scheduleSleepTimeout() {
4081 removeSleepTimeouts();
4082 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4083 }
4084
Craig Mautner4a1cb222013-12-04 16:14:06 -08004085 @Override
4086 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004087 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004088 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4089 }
4090
4091 @Override
4092 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004093 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004094 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4095 }
4096
4097 @Override
4098 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004099 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4101 }
4102
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004103 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004104 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004105 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004106 newDisplay = mActivityDisplays.get(displayId) == null;
4107 if (newDisplay) {
4108 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004109 if (activityDisplay.mDisplay == null) {
4110 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4111 return;
4112 }
Craig Mautner4504de52013-12-20 09:06:56 -08004113 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004114 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004115 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004116 }
Craig Mautner4504de52013-12-20 09:06:56 -08004117 if (newDisplay) {
4118 mWindowManager.onDisplayAdded(displayId);
4119 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004120 }
4121
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004122 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4123 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4124 return;
4125 }
4126 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4127 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4128 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4129 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4130 }
4131
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004132 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004133 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004134 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4135 if (activityDisplay != null) {
4136 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004137 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004138 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004139 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004140 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004141 }
4142 }
4143 mWindowManager.onDisplayRemoved(displayId);
4144 }
4145
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004146 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004147 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004148 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4149 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004150 // TODO: Update the bounds.
4151 }
4152 }
4153 mWindowManager.onDisplayChanged(displayId);
4154 }
4155
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004156 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004157 StackInfo info = new StackInfo();
4158 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4159 info.displayId = Display.DEFAULT_DISPLAY;
4160 info.stackId = stack.mStackId;
4161
4162 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4163 final int numTasks = tasks.size();
4164 int[] taskIds = new int[numTasks];
4165 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004166 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004167 for (int i = 0; i < numTasks; ++i) {
4168 final TaskRecord task = tasks.get(i);
4169 taskIds[i] = task.taskId;
4170 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4171 : task.realActivity != null ? task.realActivity.flattenToString()
4172 : task.getTopActivity() != null ? task.getTopActivity().packageName
4173 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004174 taskBounds[i] = new Rect();
4175 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004176 }
4177 info.taskIds = taskIds;
4178 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004179 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004180 return info;
4181 }
4182
4183 StackInfo getStackInfoLocked(int stackId) {
4184 ActivityStack stack = getStack(stackId);
4185 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004186 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004187 }
4188 return null;
4189 }
4190
4191 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004192 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004193 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4194 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004195 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004196 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004197 }
4198 }
4199 return list;
4200 }
4201
Craig Mautner15df08a2015-04-01 12:17:18 -07004202 TaskRecord getLockedTaskLocked() {
4203 final int top = mLockTaskModeTasks.size() - 1;
4204 if (top >= 0) {
4205 return mLockTaskModeTasks.get(top);
4206 }
4207 return null;
4208 }
4209
4210 boolean isLockedTask(TaskRecord task) {
4211 return mLockTaskModeTasks.contains(task);
4212 }
4213
4214 boolean isLastLockedTask(TaskRecord task) {
4215 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4216 }
4217
4218 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004219 if (!mLockTaskModeTasks.remove(task)) {
4220 return;
4221 }
4222 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4223 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004224 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004225 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4226 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004227 final Message lockTaskMsg = Message.obtain();
4228 lockTaskMsg.arg1 = task.userId;
4229 lockTaskMsg.what = LOCK_TASK_END_MSG;
4230 mHandler.sendMessage(lockTaskMsg);
4231 }
4232 }
4233
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004234 void showLockTaskToast() {
4235 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004236 }
4237
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004238 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4239 if (mLockTaskModeTasks.contains(task)) {
4240 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4241 }
4242 }
4243
Craig Mautner432f64e2015-05-20 14:59:57 -07004244 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4245 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004246 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004247 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004248 final TaskRecord lockedTask = getLockedTaskLocked();
4249 if (lockedTask != null) {
4250 removeLockedTaskLocked(lockedTask);
4251 if (!mLockTaskModeTasks.isEmpty()) {
4252 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004253 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4254 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004255 lockedTask.performClearTaskLocked();
4256 resumeTopActivitiesLocked();
4257 return;
4258 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004259 }
Craig Mautnere0570202015-05-13 13:06:11 -07004260 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4261 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004262 return;
4263 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004264
4265 // Should have already been checked, but do it again.
4266 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004267 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4268 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004269 return;
4270 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004271 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004272 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004273 return;
4274 }
4275
4276 if (mLockTaskModeTasks.isEmpty()) {
4277 // First locktask.
4278 final Message lockTaskMsg = Message.obtain();
4279 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4280 lockTaskMsg.arg1 = task.userId;
4281 lockTaskMsg.what = LOCK_TASK_START_MSG;
4282 lockTaskMsg.arg2 = lockTaskModeState;
4283 mHandler.sendMessage(lockTaskMsg);
4284 }
4285 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004286 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4287 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004288 mLockTaskModeTasks.remove(task);
4289 mLockTaskModeTasks.add(task);
4290
4291 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004292 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004293 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004294
4295 if (andResume) {
4296 findTaskToMoveToFrontLocked(task, 0, null, reason);
4297 resumeTopActivitiesLocked();
4298 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004299 }
4300
4301 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004302 return isLockTaskModeViolation(task, false);
4303 }
4304
4305 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4306 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004307 return false;
4308 }
4309 final int lockTaskAuth = task.mLockTaskAuth;
4310 switch (lockTaskAuth) {
4311 case LOCK_TASK_AUTH_DONT_LOCK:
4312 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004313 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004314 case LOCK_TASK_AUTH_LAUNCHABLE:
4315 case LOCK_TASK_AUTH_WHITELISTED:
4316 return false;
4317 case LOCK_TASK_AUTH_PINNABLE:
4318 // Pinnable tasks can't be launched on top of locktask tasks.
4319 return !mLockTaskModeTasks.isEmpty();
4320 default:
4321 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4322 return true;
4323 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004324 }
4325
Craig Mautner15df08a2015-04-01 12:17:18 -07004326 void onLockTaskPackagesUpdatedLocked() {
4327 boolean didSomething = false;
4328 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4329 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004330 final boolean wasWhitelisted =
4331 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4332 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004333 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004334 final boolean isWhitelisted =
4335 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4336 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4337 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004338 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004339 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4340 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004341 removeLockedTaskLocked(lockedTask);
4342 lockedTask.performClearTaskLocked();
4343 didSomething = true;
4344 }
4345 }
4346 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4347 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4348 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4349 final ActivityStack stack = stacks.get(stackNdx);
4350 stack.onLockTaskPackagesUpdatedLocked();
4351 }
4352 }
Craig Mautnere0570202015-05-13 13:06:11 -07004353 final ActivityRecord r = topRunningActivityLocked();
4354 final TaskRecord task = r != null ? r.task : null;
4355 if (mLockTaskModeTasks.isEmpty() && task != null
4356 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4357 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004358 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4359 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4360 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4361 false);
4362 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004363 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004364 if (didSomething) {
4365 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004366 }
4367 }
4368
Benjamin Franz43261142015-02-11 15:59:44 +00004369 int getLockTaskModeState() {
4370 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004371 }
4372
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004373 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004374
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004375 public ActivityStackSupervisorHandler(Looper looper) {
4376 super(looper);
4377 }
4378
Craig Mautnerf3333272013-04-22 10:55:53 -07004379 void activityIdleInternal(ActivityRecord r) {
4380 synchronized (mService) {
4381 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4382 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004383 }
4384
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004385 @Override
4386 public void handleMessage(Message msg) {
4387 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004388 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004389 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4390 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004391 if (mService.mDidDexOpt) {
4392 mService.mDidDexOpt = false;
4393 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4394 nmsg.obj = msg.obj;
4395 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4396 return;
4397 }
4398 // We don't at this point know if the activity is fullscreen,
4399 // so we need to be conservative and assume it isn't.
4400 activityIdleInternal((ActivityRecord)msg.obj);
4401 } break;
4402 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004403 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004404 activityIdleInternal((ActivityRecord)msg.obj);
4405 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004406 case RESUME_TOP_ACTIVITY_MSG: {
4407 synchronized (mService) {
4408 resumeTopActivitiesLocked();
4409 }
4410 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004411 case SLEEP_TIMEOUT_MSG: {
4412 synchronized (mService) {
4413 if (mService.isSleepingOrShuttingDown()) {
4414 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4415 mSleepTimeout = true;
4416 checkReadyForSleepLocked();
4417 }
4418 }
4419 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004420 case LAUNCH_TIMEOUT_MSG: {
4421 if (mService.mDidDexOpt) {
4422 mService.mDidDexOpt = false;
4423 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4424 return;
4425 }
4426 synchronized (mService) {
4427 if (mLaunchingActivity.isHeld()) {
4428 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4429 if (VALIDATE_WAKE_LOCK_CALLER
4430 && Binder.getCallingUid() != Process.myUid()) {
4431 throw new IllegalStateException("Calling must be system uid");
4432 }
4433 mLaunchingActivity.release();
4434 }
4435 }
4436 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004437 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004438 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004439 } break;
4440 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004441 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004442 } break;
4443 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004444 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004445 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004446 case CONTAINER_CALLBACK_VISIBILITY: {
4447 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004448 final IActivityContainerCallback callback = container.mCallback;
4449 if (callback != null) {
4450 try {
4451 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4452 } catch (RemoteException e) {
4453 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004454 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004455 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004456 case LOCK_TASK_START_MSG: {
4457 // When lock task starts, we disable the status bars.
4458 try {
Jason Monk62515be2014-05-21 16:06:19 -04004459 if (mLockTaskNotify == null) {
4460 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004461 }
Jason Monk62515be2014-05-21 16:06:19 -04004462 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004463 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004464 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004465 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004466 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004467 flags = StatusBarManager.DISABLE_MASK
4468 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004469 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004470 flags = StatusBarManager.DISABLE_MASK
4471 & (~StatusBarManager.DISABLE_BACK)
4472 & (~StatusBarManager.DISABLE_HOME)
4473 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004474 }
4475 getStatusBarService().disable(flags, mToken,
4476 mService.mContext.getPackageName());
4477 }
4478 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004479 if (getDevicePolicyManager() != null) {
4480 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004481 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004482 }
justinzhang5286d3f2014-05-12 17:06:01 -04004483 } catch (RemoteException ex) {
4484 throw new RuntimeException(ex);
4485 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004486 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004487 case LOCK_TASK_END_MSG: {
4488 // When lock task ends, we enable the status bars.
4489 try {
Jason Monk62515be2014-05-21 16:06:19 -04004490 if (getStatusBarService() != null) {
4491 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4492 mService.mContext.getPackageName());
4493 }
4494 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004495 if (getDevicePolicyManager() != null) {
4496 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4497 msg.arg1);
4498 }
Jason Monk62515be2014-05-21 16:06:19 -04004499 if (mLockTaskNotify == null) {
4500 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4501 }
4502 mLockTaskNotify.show(false);
4503 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004504 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004505 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004506 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004507 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004508 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004509 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004510 new LockPatternUtils(mService.mContext)
4511 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004512 }
4513 } catch (SettingNotFoundException e) {
4514 // No setting, don't lock.
4515 }
justinzhang5286d3f2014-05-12 17:06:01 -04004516 } catch (RemoteException ex) {
4517 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004518 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004519 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004520 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004521 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004522 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4523 if (mLockTaskNotify == null) {
4524 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4525 }
4526 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4527 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004528 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4529 final ActivityContainer container = (ActivityContainer) msg.obj;
4530 final IActivityContainerCallback callback = container.mCallback;
4531 if (callback != null) {
4532 try {
4533 callback.onAllActivitiesComplete(container.asBinder());
4534 } catch (RemoteException e) {
4535 }
4536 }
4537 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004538 case LAUNCH_TASK_BEHIND_COMPLETE: {
4539 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004540 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004541 if (r != null) {
4542 handleLaunchTaskBehindCompleteLocked(r);
4543 }
4544 }
4545 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004546 }
4547 }
4548 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004549
Ying Wangb081a592014-04-22 15:20:16 -07004550 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004551 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004552 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004553 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004554 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004555 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004556 ActivityRecord mParentActivity = null;
4557 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004558
Craig Mautnere3a00d72014-04-16 08:31:19 -07004559 boolean mVisible = true;
4560
Craig Mautner4a1cb222013-12-04 16:14:06 -08004561 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004562 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004563
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004564 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4565 final static int CONTAINER_STATE_NO_SURFACE = 1;
4566 final static int CONTAINER_STATE_FINISHING = 2;
4567 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4568
4569 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004570 synchronized (mService) {
4571 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004572 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004573 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004574 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004575 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004576 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004577
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004578 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004579 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004580 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004581 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004582 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004583 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004584 }
4585
4586 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004587 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004588 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004589 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4590 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004591 return;
4592 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004593 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004594 }
4595 }
4596
4597 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004598 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004599 synchronized (mService) {
4600 if (mActivityDisplay != null) {
4601 return mActivityDisplay.mDisplayId;
4602 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004603 }
4604 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004605 }
4606
Jeff Brownca9bc702014-02-11 14:32:56 -08004607 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004608 public int getStackId() {
4609 synchronized (mService) {
4610 return mStackId;
4611 }
4612 }
4613
4614 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004615 public boolean injectEvent(InputEvent event) {
4616 final long origId = Binder.clearCallingIdentity();
4617 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004618 synchronized (mService) {
4619 if (mActivityDisplay != null) {
4620 return mInputManagerInternal.injectInputEvent(event,
4621 mActivityDisplay.mDisplayId,
4622 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4623 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004624 }
4625 return false;
4626 } finally {
4627 Binder.restoreCallingIdentity(origId);
4628 }
4629 }
4630
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004631 @Override
4632 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004633 synchronized (mService) {
4634 if (mContainerState == CONTAINER_STATE_FINISHING) {
4635 return;
4636 }
4637 mContainerState = CONTAINER_STATE_FINISHING;
4638
Craig Mautnerd163e752014-06-13 17:18:47 -07004639 long origId = Binder.clearCallingIdentity();
4640 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004641 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004642 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004643 } finally {
4644 Binder.restoreCallingIdentity(origId);
4645 }
4646 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004647 }
4648
Craig Mautner60257702014-09-17 15:02:33 -07004649 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004650 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004651 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004652 if (mActivityDisplay != null) {
4653 mActivityDisplay.detachActivitiesLocked(mStack);
4654 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004655 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004656 }
4657 }
4658
4659 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004660 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004661 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004662 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004663 Binder.getCallingUid(), mCurrentUser, false,
4664 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004665
Craig Mautnere0a38842013-12-16 16:14:02 -08004666 // TODO: Switch to user app stacks here.
4667 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004668 final Uri data = intent.getData();
4669 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4670 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004671 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004672 checkEmbeddedAllowedInner(userId, intent, mimeType);
4673
4674 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4675 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004676 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004677 }
4678
4679 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004680 public final int startActivityIntentSender(IIntentSender intentSender)
4681 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004682 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4683
4684 if (!(intentSender instanceof PendingIntentRecord)) {
4685 throw new IllegalArgumentException("Bad PendingIntent object");
4686 }
4687
Craig Mautnerb9168362015-02-26 20:40:19 -08004688 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004689 Binder.getCallingUid(), mCurrentUser, false,
4690 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004691
4692 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4693 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4694 pendingIntent.key.requestResolvedType);
4695
4696 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4697 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4698 }
4699
4700 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004701 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004702 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004703 throw new SecurityException(
4704 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4705 }
4706 }
4707
Craig Mautnerdf88d732014-01-27 09:21:32 -08004708 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004709 public IBinder asBinder() {
4710 return this;
4711 }
4712
Craig Mautner4504de52013-12-20 09:06:56 -08004713 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004714 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004715 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004716 }
4717
Craig Mautner4a1cb222013-12-04 16:14:06 -08004718 ActivityStackSupervisor getOuter() {
4719 return ActivityStackSupervisor.this;
4720 }
4721
Craig Mautnerd163e752014-06-13 17:18:47 -07004722 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004723 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004724 }
4725
Craig Mautner6985bad2014-04-21 15:22:06 -07004726 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004727 void setVisible(boolean visible) {
4728 if (mVisible != visible) {
4729 mVisible = visible;
4730 if (mCallback != null) {
4731 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4732 0 /* unused */, this).sendToTarget();
4733 }
4734 }
4735 }
4736
Craig Mautner6985bad2014-04-21 15:22:06 -07004737 void setDrawn() {
4738 }
4739
Craig Mautner1b4bf852014-05-26 15:06:32 -07004740 // You can always start a new task on a regular ActivityStack.
4741 boolean isEligibleForNewTasks() {
4742 return true;
4743 }
4744
Craig Mautnerd163e752014-06-13 17:18:47 -07004745 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004746 detachLocked();
4747 deleteActivityContainer(this);
4748 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004749 }
4750
Craig Mautner34b73df2014-01-12 21:11:08 -08004751 @Override
4752 public String toString() {
4753 return mIdString + (mActivityDisplay == null ? "N" : "A");
4754 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004755 }
4756
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004757 private class VirtualActivityContainer extends ActivityContainer {
4758 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004759 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004760
4761 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4762 super(getNextStackId());
4763 mParentActivity = parent;
4764 mCallback = callback;
4765 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004766 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004767 }
4768
4769 @Override
4770 public void setSurface(Surface surface, int width, int height, int density) {
4771 super.setSurface(surface, width, height, density);
4772
4773 synchronized (mService) {
4774 final long origId = Binder.clearCallingIdentity();
4775 try {
4776 setSurfaceLocked(surface, width, height, density);
4777 } finally {
4778 Binder.restoreCallingIdentity(origId);
4779 }
4780 }
4781 }
4782
4783 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4784 if (mContainerState == CONTAINER_STATE_FINISHING) {
4785 return;
4786 }
4787 VirtualActivityDisplay virtualActivityDisplay =
4788 (VirtualActivityDisplay) mActivityDisplay;
4789 if (virtualActivityDisplay == null) {
4790 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004791 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004792 mActivityDisplay = virtualActivityDisplay;
4793 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004794 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004795 }
4796
4797 if (mSurface != null) {
4798 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004799 }
4800
Craig Mautner6985bad2014-04-21 15:22:06 -07004801 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004802 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004803 mStack.resumeTopActivityLocked(null);
4804 } else {
4805 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004806 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004807 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004808 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004809 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004810 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004811
Craig Mautnerd163e752014-06-13 17:18:47 -07004812 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004813
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004814 if (DEBUG_STACK) Slog.d(TAG_STACK,
4815 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004816 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004817
Craig Mautner6985bad2014-04-21 15:22:06 -07004818 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004819 boolean isAttachedLocked() {
4820 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004821 }
4822
4823 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004824 void setDrawn() {
4825 synchronized (mService) {
4826 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004827 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004828 }
4829 }
4830
Craig Mautner1b4bf852014-05-26 15:06:32 -07004831 // Never start a new task on an ActivityView if it isn't explicitly specified.
4832 @Override
4833 boolean isEligibleForNewTasks() {
4834 return false;
4835 }
4836
Craig Mautnerd163e752014-06-13 17:18:47 -07004837 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004838 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004839 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4840 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4841 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4842 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4843 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004844 }
4845 }
4846
Craig Mautner4a1cb222013-12-04 16:14:06 -08004847 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4848 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004849 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004850 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004851 int mDisplayId;
4852 Display mDisplay;
4853 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004854
Craig Mautner4a1cb222013-12-04 16:14:06 -08004855 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4856 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004857 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004858
Jose Lima4b6c6692014-08-12 17:41:12 -07004859 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004860
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004861 ActivityDisplay() {
4862 }
Craig Mautner4504de52013-12-20 09:06:56 -08004863
Craig Mautner1a70a162014-09-13 12:09:31 -07004864 // After instantiation, check that mDisplay is not null before using this. The alternative
4865 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004866 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004867 final Display display = mDisplayManager.getDisplay(displayId);
4868 if (display == null) {
4869 return;
4870 }
4871 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004872 }
4873
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004874 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004875 mDisplay = display;
4876 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004877 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004878 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004879
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004880 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004881 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004882 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4883 + " onTop=" + onTop);
4884 if (onTop) {
4885 mStacks.add(stack);
4886 } else {
4887 mStacks.add(0, stack);
4888 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004889 }
4890
Craig Mautnere0a38842013-12-16 16:14:02 -08004891 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004892 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004893 + " from displayId=" + mDisplayId);
4894 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004895 }
4896
Jose Lima4b6c6692014-08-12 17:41:12 -07004897 void setVisibleBehindActivity(ActivityRecord r) {
4898 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004899 }
4900
Jose Lima4b6c6692014-08-12 17:41:12 -07004901 boolean hasVisibleBehindActivity() {
4902 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004903 }
4904
Craig Mautner34b73df2014-01-12 21:11:08 -08004905 @Override
4906 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004907 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4908 }
4909 }
4910
4911 class VirtualActivityDisplay extends ActivityDisplay {
4912 VirtualDisplay mVirtualDisplay;
4913
Craig Mautner6985bad2014-04-21 15:22:06 -07004914 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004915 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004916 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4917 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4918 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4919 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004920
4921 init(mVirtualDisplay.getDisplay());
4922
4923 mWindowManager.handleDisplayAdded(mDisplayId);
4924 }
4925
4926 void setSurface(Surface surface) {
4927 if (mVirtualDisplay != null) {
4928 mVirtualDisplay.setSurface(surface);
4929 }
4930 }
4931
4932 @Override
4933 void detachActivitiesLocked(ActivityStack stack) {
4934 super.detachActivitiesLocked(stack);
4935 if (mVirtualDisplay != null) {
4936 mVirtualDisplay.release();
4937 mVirtualDisplay = null;
4938 }
4939 }
4940
4941 @Override
4942 public String toString() {
4943 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004944 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004945 }
Jose Lima58e66d62014-05-27 20:00:27 -07004946
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004947 /**
4948 * Adjust bounds to stay within stack bounds.
4949 *
4950 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4951 * that keep them unchanged, but be contained within the stack bounds.
4952 *
4953 * @param bounds Bounds to be adjusted.
4954 * @param stackBounds Bounds within which the other bounds should remain.
4955 */
4956 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4957 if (stackBounds == null || stackBounds.contains(bounds)) {
4958 return;
4959 }
4960
4961 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4962 final int maxRight = stackBounds.right
4963 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4964 int horizontalDiff = stackBounds.left - bounds.left;
4965 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4966 || (bounds.left + horizontalDiff >= maxRight)) {
4967 horizontalDiff = maxRight - bounds.left;
4968 }
4969 bounds.left += horizontalDiff;
4970 bounds.right += horizontalDiff;
4971 }
4972
4973 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4974 final int maxBottom = stackBounds.bottom
4975 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4976 int verticalDiff = stackBounds.top - bounds.top;
4977 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4978 || (bounds.top + verticalDiff >= maxBottom)) {
4979 verticalDiff = maxBottom - bounds.top;
4980 }
4981 bounds.top += verticalDiff;
4982 bounds.bottom += verticalDiff;
4983 }
4984 }
4985
Craig Mautner27084302013-03-25 08:05:25 -07004986}