blob: 2a36230e0c721c6be00cef4ab765871671421fd4 [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;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700100import android.util.DisplayMetrics;
Craig Mautner2420ead2013-04-01 17:13:20 -0700101import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700102import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800103import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700104
Craig Mautner4a1cb222013-12-04 16:14:06 -0800105import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800106import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800107import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800108import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800109import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700110import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700111import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800112import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700113import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400114import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700115import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400116import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800117import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700118import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700119import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700120
justinzhang5286d3f2014-05-12 17:06:01 -0400121
Craig Mautner8d341ef2013-03-26 09:03:27 -0700122import java.io.FileDescriptor;
123import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700124import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700125import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700126import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700127import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700128import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700129
Craig Mautner4a1cb222013-12-04 16:14:06 -0800130public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800131 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700132 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700133 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700134 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700135 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700136 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700137 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700138 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700139 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
140 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700141 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700142 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700143 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
144 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700145 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700146 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800147
Craig Mautnerf3333272013-04-22 10:55:53 -0700148 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700149 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700150
Craig Mautner0eea92c2013-05-16 13:35:39 -0700151 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700152 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700153
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700154 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700155 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700156
Craig Mautner05d29032013-05-03 13:40:13 -0700157 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
158 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
159 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700160 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700161 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800162 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
163 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
164 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700165 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400166 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
167 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700168 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700169 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700170 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800171
Wale Ogunwale040b4702015-08-06 18:10:50 -0700172 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700173
Jason Monk62515be2014-05-21 16:06:19 -0400174 private static final String LOCK_TASK_TAG = "Lock-to-App";
175
Wale Ogunwale040b4702015-08-06 18:10:50 -0700176 // Used to indicate if an object (e.g. stack) that we are trying to get
177 // should be created if it doesn't exist already.
178 private static final boolean CREATE_IF_NEEDED = true;
179
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700180 // Used to indicate that windows of activities should be preserved during the resize.
181 static final boolean PRESERVE_WINDOWS = true;
182
Wale Ogunwale040b4702015-08-06 18:10:50 -0700183 // Used to indicate if an object (e.g. task) should be moved/created
184 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700185 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700186
187 // Used to indicate that an objects (e.g. task) removal from its container
188 // (e.g. stack) is due to it moving to another container.
189 static final boolean MOVING = true;
190
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700191 // Force the focus to change to the stack we are moving a task to..
192 static final boolean FORCE_FOCUS = true;
193
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700194 // Restore task from the saved recents if it can't be found in any live stack.
195 static final boolean RESTORE_FROM_RECENTS = true;
196
Svetoslav7008b512015-06-24 18:47:07 -0700197 // Activity actions an app cannot start if it uses a permission which is not granted.
198 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
199 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700200
Svetoslav7008b512015-06-24 18:47:07 -0700201 static {
202 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
203 Manifest.permission.CAMERA);
204 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
205 Manifest.permission.CAMERA);
206 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
207 Manifest.permission.CALL_PHONE);
208 }
209
Svet Ganov99b60432015-06-27 13:15:22 -0700210 /** Action restriction: launching the activity is not restricted. */
211 private static final int ACTIVITY_RESTRICTION_NONE = 0;
212 /** Action restriction: launching the activity is restricted by a permission. */
213 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
214 /** Action restriction: launching the activity is restricted by an app op. */
215 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700216
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700217 // The height/width divide used when fitting a task within a bounds with method
218 // {@link #fitWithinBounds}.
219 // We always want the task to to be visible in the bounds without affecting its size when
220 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
221 // the input bounds right or bottom side minus the width or height divided by this value.
222 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
223
justinzhang5286d3f2014-05-12 17:06:01 -0400224 /** Status Bar Service **/
225 private IBinder mToken = new Binder();
226 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400227 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400228
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700229 // For debugging to make sure the caller when acquiring/releasing our
230 // wake lock is the system process.
231 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700232
Craig Mautner27084302013-03-25 08:05:25 -0700233 final ActivityManagerService mService;
234
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800235 private final RecentTasks mRecentTasks;
236
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700237 final ActivityStackSupervisorHandler mHandler;
238
239 /** Short cut */
240 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800241 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700242
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700243 /** Counter for next free stack ID to use for dynamic activity stacks. */
244 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700245
246 /** Task identifier that activities are currently being started in. Incremented each time a
247 * new task is created. */
248 private int mCurTaskId = 0;
249
Craig Mautner2420ead2013-04-01 17:13:20 -0700250 /** The current user */
251 private int mCurrentUser;
252
Craig Mautnere0a38842013-12-16 16:14:02 -0800253 /** The stack containing the launcher app. Assumed to always be attached to
254 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700255 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700256
Craig Mautnere0a38842013-12-16 16:14:02 -0800257 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700258 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700259
Craig Mautner4a1cb222013-12-04 16:14:06 -0800260 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
261 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800262 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800263 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700264
265 /** List of activities that are waiting for a new activity to become visible before completing
266 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800267 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700268
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700269 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800270 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700271
272 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800273 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700274
Craig Mautnerde4ef022013-04-07 19:01:33 -0700275 /** List of activities that are ready to be stopped, but waiting for the next activity to
276 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800277 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700278
Craig Mautnerf3333272013-04-22 10:55:53 -0700279 /** List of activities that are ready to be finished, but waiting for the previous activity to
280 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800281 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700282
Craig Mautner0eea92c2013-05-16 13:35:39 -0700283 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800284 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700285
Craig Mautnerf3333272013-04-22 10:55:53 -0700286 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700287 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700288
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700289 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700290 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700291
Craig Mautnerde4ef022013-04-07 19:01:33 -0700292 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
293 * is being brought in front of us. */
294 boolean mUserLeaving = false;
295
Craig Mautner0eea92c2013-05-16 13:35:39 -0700296 /** Set when we have taken too long waiting to go to sleep. */
297 boolean mSleepTimeout = false;
298
Jose Lima58e66d62014-05-27 20:00:27 -0700299 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
300 * setWindowManager is called. **/
301 private boolean mLeanbackOnlyDevice;
302
Craig Mautner0eea92c2013-05-16 13:35:39 -0700303 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700304 * We don't want to allow the device to go to sleep while in the process
305 * of launching an activity. This is primarily to allow alarm intent
306 * receivers to launch an activity and get that to run before the device
307 * goes back to sleep.
308 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700309 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700310
311 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700312 * Set when the system is going to sleep, until we have
313 * successfully paused the current activity and released our wake lock.
314 * At that point the system is allowed to actually sleep.
315 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700316 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700317
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700318 /** Stack id of the front stack when user switched, indexed by userId. */
319 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700320
Craig Mautner4504de52013-12-20 09:06:56 -0800321 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700323 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800324
325 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700326 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800327
Jeff Brownca9bc702014-02-11 14:32:56 -0800328 InputManagerInternal mInputManagerInternal;
329
Craig Mautner15df08a2015-04-01 12:17:18 -0700330 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
331 * may be finished until there is only one entry left. If this is empty the system is not
332 * in lockTask mode. */
333 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000334 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700335 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
336 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000337 */
338 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400339 /**
340 * Notifies the user when entering/exiting lock-task.
341 */
342 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800343
Craig Mautner15df08a2015-04-01 12:17:18 -0700344 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700345
Craig Mautner42d04db2014-11-06 12:13:23 -0800346 /** Used to keep resumeTopActivityLocked() from being entered recursively */
347 boolean inResumeTopActivity;
348
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700349 // 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 -0700350 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700351 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700352
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700353 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
354 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
355
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700356 // The default minimal size that will be used if the activity doesn't specify its minimal size.
357 // It will be calculated when the default display gets added.
358 private int mDefaultMinimalSizeOfResizeableTask = -1;
359
Craig Mautneree36c772014-07-16 14:56:05 -0700360 /**
361 * Description of a request to start a new activity, which has been held
362 * due to app switches being disabled.
363 */
364 static class PendingActivityLaunch {
365 final ActivityRecord r;
366 final ActivityRecord sourceRecord;
367 final int startFlags;
368 final ActivityStack stack;
369
370 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
371 int _startFlags, ActivityStack _stack) {
372 r = _r;
373 sourceRecord = _sourceRecord;
374 startFlags = _startFlags;
375 stack = _stack;
376 }
377 }
378
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800379 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700380 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800381 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700382 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
383 }
384
385 /**
386 * At the time when the constructor runs, the power manager has not yet been
387 * initialized. So we initialize our wakelocks afterwards.
388 */
389 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700391 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700392 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700393 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700394 }
395
justinzhang5286d3f2014-05-12 17:06:01 -0400396 // This function returns a IStatusBarService. The value is from ServiceManager.
397 // getService and is cached.
398 private IStatusBarService getStatusBarService() {
399 synchronized (mService) {
400 if (mStatusBarService == null) {
401 mStatusBarService = IStatusBarService.Stub.asInterface(
402 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
403 if (mStatusBarService == null) {
404 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
405 }
406 }
407 return mStatusBarService;
408 }
409 }
410
Jason Monk35c62a42014-06-17 10:24:47 -0400411 private IDevicePolicyManager getDevicePolicyManager() {
412 synchronized (mService) {
413 if (mDevicePolicyManager == null) {
414 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
415 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
416 if (mDevicePolicyManager == null) {
417 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
418 }
419 }
420 return mDevicePolicyManager;
421 }
422 }
423
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700424 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425 synchronized (mService) {
426 mWindowManager = wm;
427
428 mDisplayManager =
429 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
430 mDisplayManager.registerDisplayListener(this, null);
431
432 Display[] displays = mDisplayManager.getDisplays();
433 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
434 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800435 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700436 if (activityDisplay.mDisplay == null) {
437 throw new IllegalStateException("Default Display does not exist");
438 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800439 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700440 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800441 }
442
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700443 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800444 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800445
446 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700447
448 // Initialize this here, now that we can get a valid reference to PackageManager.
449 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800450 }
Craig Mautner27084302013-03-25 08:05:25 -0700451 }
452
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200453 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700454 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200455 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700456 }
457
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700458 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800459 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700460 }
461
Craig Mautnerde4ef022013-04-07 19:01:33 -0700462 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800463 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700464 }
465
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800466 /** Top of all visible stacks is/should always be equal to the focused stack.
467 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
468 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700469 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700470 if (stack == null) {
471 return false;
472 }
473
Craig Mautnerdf88d732014-01-27 09:21:32 -0800474 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
475 if (parent != null) {
476 stack = parent.task.stack;
477 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800478 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700479 }
480
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700481 void setFocusStack(String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800482 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800483 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800484 if (topNdx <= 0) {
485 return;
486 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700487
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700488 final ActivityStack topStack = stacks.get(topNdx);
489 mFocusedStack = topStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800490 if (lastFocusedStack != null) {
491 mLastFocusedStack = lastFocusedStack;
492 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800493
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700494 EventLogTags.writeAmFocusedStack(
495 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
496 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800497
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800498 if (mService.mBooting || !mService.mBooted) {
499 final ActivityRecord r = topRunningActivityLocked();
500 if (r != null && r.idle) {
501 checkFinishBootingLocked();
502 }
503 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700504 }
505
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700506 void moveHomeStackToFront(String reason) {
507 mHomeStack.moveToFront(reason);
508 }
509
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700510 /** Returns true if the focus activity was adjusted to the home stack top activity. */
511 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700512 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
513 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700514 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700515 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700516
Craig Mautner84984fa2014-06-19 11:19:20 -0700517 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700518
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700519 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700520 if (top == null) {
521 return false;
522 }
523 mService.setFocusedActivityLocked(top, reason);
524 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700525 }
526
Craig Mautner299f9602015-01-26 09:47:33 -0800527 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700528 if (!mService.mBooting && !mService.mBooted) {
529 // Not ready yet!
530 return false;
531 }
532
Craig Mautner84984fa2014-06-19 11:19:20 -0700533 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
534 mWindowManager.showRecentApps();
535 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700536 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700537
Craig Mautner84984fa2014-06-19 11:19:20 -0700538 if (prev != null) {
539 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
540 }
541
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700542 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
543 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800544 // Only resume home activity if isn't finishing.
545 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800546 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700547 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700548 }
Craig Mautner299f9602015-01-26 09:47:33 -0800549 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700550 }
551
Craig Mautner8d341ef2013-03-26 09:03:27 -0700552 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700553 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700554 }
555
556 /**
557 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
558 * @param id Id of the task we would like returned.
559 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
560 * restore the task from recents to the active list.
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700561 * @param stackId The stack to restore the task to (default launch stack will be used
562 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700563 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700564 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800565 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800566 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800567 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
569 ActivityStack stack = stacks.get(stackNdx);
570 TaskRecord task = stack.taskForIdLocked(id);
571 if (task != null) {
572 return task;
573 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700574 }
575 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800576
577 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700578 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800579 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800580 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700581 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800582 return null;
583 }
584
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700585 if (!restoreFromRecents) {
586 return task;
587 }
588
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700589 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
591 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800592 return null;
593 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700594 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800595 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700596 }
597
Craig Mautner6170f732013-04-02 13:05:23 -0700598 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800599 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800600 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800601 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
603 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
604 if (r != null) {
605 return r;
606 }
Craig Mautner6170f732013-04-02 13:05:23 -0700607 }
608 }
609 return null;
610 }
611
Craig Mautneref73ee12014-04-23 11:45:37 -0700612 void setNextTaskId(int taskId) {
613 if (taskId > mCurTaskId) {
614 mCurTaskId = taskId;
615 }
616 }
617
Craig Mautner8d341ef2013-03-26 09:03:27 -0700618 int getNextTaskId() {
619 do {
620 mCurTaskId++;
621 if (mCurTaskId <= 0) {
622 mCurTaskId = 1;
623 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700624 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700625 return mCurTaskId;
626 }
627
Craig Mautnerde4ef022013-04-07 19:01:33 -0700628 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800629 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700630 if (stack == null) {
631 return null;
632 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700633 ActivityRecord resumedActivity = stack.mResumedActivity;
634 if (resumedActivity == null || resumedActivity.app == null) {
635 resumedActivity = stack.mPausingActivity;
636 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700637 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700638 }
639 }
640 return resumedActivity;
641 }
642
Dianne Hackbornff072722014-09-24 10:56:28 -0700643 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700644 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800645 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800646 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
647 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800648 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
649 final ActivityStack stack = stacks.get(stackNdx);
650 if (!isFrontStack(stack)) {
651 continue;
652 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700653 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800654 if (hr != null) {
655 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
656 && processName.equals(hr.processName)) {
657 try {
George Mount2c92c972014-03-20 09:38:23 -0700658 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800659 didSomething = true;
660 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700661 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800662 Slog.w(TAG, "Exception in new application when starting activity "
663 + hr.intent.getComponent().flattenToShortString(), e);
664 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700665 }
Craig Mautner20e72272013-04-01 13:45:53 -0700666 }
Craig Mautner20e72272013-04-01 13:45:53 -0700667 }
668 }
669 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700670 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700671 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700672 }
Craig Mautner20e72272013-04-01 13:45:53 -0700673 return didSomething;
674 }
675
676 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800677 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
678 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800679 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
680 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800681 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800682 continue;
683 }
684 final ActivityRecord resumedActivity = stack.mResumedActivity;
685 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700686 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800687 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 return false;
689 }
Craig Mautner20e72272013-04-01 13:45:53 -0700690 }
691 }
692 return true;
693 }
694
Craig Mautnerde4ef022013-04-07 19:01:33 -0700695 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800696 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
697 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800698 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
699 final ActivityStack stack = stacks.get(stackNdx);
700 if (isFrontStack(stack)) {
701 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700702 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 return false;
704 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700705 }
706 }
707 }
708 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700709 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800710 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
711 mLastFocusedStack + " to=" + mFocusedStack);
712 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700713 return true;
714 }
715
716 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800717 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800718 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
719 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800720 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
721 final ActivityStack stack = stacks.get(stackNdx);
722 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800723 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700724 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800725 return false;
726 }
727 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800728 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700729 }
730 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800731 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700732 }
733
Craig Mautner2acc3892013-09-23 10:28:14 -0700734 /**
735 * Pause all activities in either all of the stacks or just the back stacks.
736 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700737 * @return true if any activity was paused as a result of this call.
738 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700739 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700740 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800741 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
742 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
744 final ActivityStack stack = stacks.get(stackNdx);
745 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700746 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800747 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700748 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
749 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800750 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700751 }
752 }
753 return someActivityPaused;
754 }
755
Craig Mautnerde4ef022013-04-07 19:01:33 -0700756 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700757 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800758 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
759 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800760 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
761 final ActivityStack stack = stacks.get(stackNdx);
762 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700763 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800764 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700765 Slog.d(TAG_STATES,
766 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800767 pausing = false;
768 } else {
769 return false;
770 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700771 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700772 }
773 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700774 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700775 }
776
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700777 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
778 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500779 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800780 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
781 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
782 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
783 final ActivityStack stack = stacks.get(stackNdx);
784 if (stack.mResumedActivity != null &&
785 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700786 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800787 }
788 }
789 }
790 }
791
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700792 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700793 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700794 }
795
796 void sendWaitingVisibleReportLocked(ActivityRecord r) {
797 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700798 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700799 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700800 if (w.who == null) {
801 changed = true;
802 w.timeout = false;
803 if (r != null) {
804 w.who = new ComponentName(r.info.packageName, r.info.name);
805 }
806 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
807 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700808 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700809 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700810 if (changed) {
811 mService.notifyAll();
812 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700813 }
814
815 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
816 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700817 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700818 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700819 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700820 if (w.who == null) {
821 changed = true;
822 w.timeout = timeout;
823 if (r != null) {
824 w.who = new ComponentName(r.info.packageName, r.info.name);
825 }
826 w.thisTime = thisTime;
827 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700828 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700829 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700830 if (changed) {
831 mService.notifyAll();
832 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700833 }
834
Craig Mautner29219d92013-04-16 20:19:12 -0700835 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800836 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700837 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700838 if (r != null) {
839 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700840 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700841
Craig Mautner4a1cb222013-12-04 16:14:06 -0800842 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800843 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
845 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700846 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700847 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700848 if (r != null) {
849 return r;
850 }
851 }
852 }
853 return null;
854 }
855
Dianne Hackborn09233282014-04-30 11:33:59 -0700856 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700857 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
859 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800860 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800862 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800863 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
864 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700865 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800866 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700867 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700868 }
869 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700870
871 // The lists are already sorted from most recent to oldest. Just pull the most recent off
872 // each list and add it to list. Stop when all lists are empty or maxNum reached.
873 while (maxNum > 0) {
874 long mostRecentActiveTime = Long.MIN_VALUE;
875 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800876 final int numTaskLists = runningTaskLists.size();
877 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
878 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700879 if (!stackTaskList.isEmpty()) {
880 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
881 if (lastActiveTime > mostRecentActiveTime) {
882 mostRecentActiveTime = lastActiveTime;
883 selectedStackList = stackTaskList;
884 }
885 }
886 }
887 if (selectedStackList != null) {
888 list.add(selectedStackList.remove(0));
889 --maxNum;
890 } else {
891 break;
892 }
893 }
Craig Mautner20e72272013-04-01 13:45:53 -0700894 }
895
Craig Mautner23ac33b2013-04-01 16:26:35 -0700896 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700897 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700898 // Collect information about the target of the Intent.
899 ActivityInfo aInfo;
900 try {
901 ResolveInfo rInfo =
902 AppGlobals.getPackageManager().resolveIntent(
903 intent, resolvedType,
904 PackageManager.MATCH_DEFAULT_ONLY
905 | ActivityManagerService.STOCK_PM_FLAGS, userId);
906 aInfo = rInfo != null ? rInfo.activityInfo : null;
907 } catch (RemoteException e) {
908 aInfo = null;
909 }
910
911 if (aInfo != null) {
912 // Store the found target back into the intent, because now that
913 // we have it we never want to do this again. For example, if the
914 // user navigates back to this point in the history, we should
915 // always restart the exact same activity.
916 intent.setComponent(new ComponentName(
917 aInfo.applicationInfo.packageName, aInfo.name));
918
919 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700920 if (!aInfo.processName.equals("system")) {
921 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922 mService.setDebugApp(aInfo.processName, true, false);
923 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924
Man Caocfa78b22015-06-11 20:14:34 -0700925 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
927 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700928
Man Caocfa78b22015-06-11 20:14:34 -0700929 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
930 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
931 }
932
933 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700934 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700935 }
936 }
937 }
938 return aInfo;
939 }
940
Craig Mautner299f9602015-01-26 09:47:33 -0800941 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
942 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700943 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
944 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
945 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
946 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700947 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
948 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700949 null /* outActivity */, null /* container */, null /* inTask */);
950 if (inResumeTopActivity) {
951 // If we are in resume section already, home activity will be initialized, but not
952 // resumed (to avoid recursive resume) and will stay that way until something pokes it
953 // again. We need to schedule another resume.
954 scheduleResumeTopActivities();
955 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700956 }
957
Craig Mautner23ac33b2013-04-01 16:26:35 -0700958 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700959 String callingPackage, Intent intent, String resolvedType,
960 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700961 IBinder resultTo, String resultWho, int requestCode, int startFlags,
962 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700963 Bundle options, boolean ignoreTargetSecurity, int userId,
964 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700965 // Refuse possible leaked file descriptors
966 if (intent != null && intent.hasFileDescriptors()) {
967 throw new IllegalArgumentException("File descriptors passed in Intent");
968 }
969 boolean componentSpecified = intent.getComponent() != null;
970
971 // Don't modify the client's object!
972 intent = new Intent(intent);
973
974 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700975 ActivityInfo aInfo =
976 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700977
Craig Mautnere0a38842013-12-16 16:14:02 -0800978 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700979 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800980 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700981 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800982 // Cannot start a child activity if the parent is not resumed.
983 return ActivityManager.START_CANCELED;
984 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700985 final int realCallingPid = Binder.getCallingPid();
986 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700987 int callingPid;
988 if (callingUid >= 0) {
989 callingPid = -1;
990 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700991 callingPid = realCallingPid;
992 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700993 } else {
994 callingPid = callingUid = -1;
995 }
996
Craig Mautnere0a38842013-12-16 16:14:02 -0800997 final ActivityStack stack;
998 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800999 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001000 } else {
1001 stack = container.mStack;
1002 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001003 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001004 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001005 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001006
1007 final long origId = Binder.clearCallingIdentity();
1008
1009 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001010 (aInfo.applicationInfo.privateFlags
1011 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001012 // This may be a heavy-weight process! Check to see if we already
1013 // have another, different heavy-weight process running.
1014 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1015 if (mService.mHeavyWeightProcess != null &&
1016 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1017 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001018 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001019 if (caller != null) {
1020 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1021 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001022 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001023 } else {
1024 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001025 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026 + intent.toString());
1027 ActivityOptions.abort(options);
1028 return ActivityManager.START_PERMISSION_DENIED;
1029 }
1030 }
1031
1032 IIntentSender target = mService.getIntentSenderLocked(
1033 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001034 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001035 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1036 | PendingIntent.FLAG_ONE_SHOT, null);
1037
1038 Intent newIntent = new Intent();
1039 if (requestCode >= 0) {
1040 // Caller is requesting a result.
1041 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1042 }
1043 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1044 new IntentSender(target));
1045 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1046 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1047 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1048 hist.packageName);
1049 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1050 hist.task.taskId);
1051 }
1052 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1053 aInfo.packageName);
1054 newIntent.setFlags(intent.getFlags());
1055 newIntent.setClassName("android",
1056 HeavyWeightSwitcherActivity.class.getName());
1057 intent = newIntent;
1058 resolvedType = null;
1059 caller = null;
1060 callingUid = Binder.getCallingUid();
1061 callingPid = Binder.getCallingPid();
1062 componentSpecified = true;
1063 try {
1064 ResolveInfo rInfo =
1065 AppGlobals.getPackageManager().resolveIntent(
1066 intent, null,
1067 PackageManager.MATCH_DEFAULT_ONLY
1068 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1069 aInfo = rInfo != null ? rInfo.activityInfo : null;
1070 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1071 } catch (RemoteException e) {
1072 aInfo = null;
1073 }
1074 }
1075 }
1076 }
1077
Dianne Hackborn91097de2014-04-04 18:02:06 -07001078 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1079 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001080 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001081 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001082 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001083
Craig Mautner85c11a82014-07-17 12:38:18 -07001084 Binder.restoreCallingIdentity(origId);
1085
Craig Mautnerde4ef022013-04-07 19:01:33 -07001086 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001087 // If the caller also wants to switch to a new configuration,
1088 // do so now. This allows a clean switch, as we are waiting
1089 // for the current activity to pause (so we will not destroy
1090 // it), and have not yet started the next activity.
1091 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1092 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001093 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001094 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001096 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001097 }
1098
Craig Mautner23ac33b2013-04-01 16:26:35 -07001099 if (outResult != null) {
1100 outResult.result = res;
1101 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001102 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001103 do {
1104 try {
1105 mService.wait();
1106 } catch (InterruptedException e) {
1107 }
1108 } while (!outResult.timeout && outResult.who == null);
1109 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001110 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001111 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001112 outResult.timeout = false;
1113 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1114 outResult.totalTime = 0;
1115 outResult.thisTime = 0;
1116 } else {
1117 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001118 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001119 do {
1120 try {
1121 mService.wait();
1122 } catch (InterruptedException e) {
1123 }
1124 } while (!outResult.timeout && outResult.who == null);
1125 }
1126 }
1127 }
1128
1129 return res;
1130 }
1131 }
1132
1133 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1134 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1135 Bundle options, int userId) {
1136 if (intents == null) {
1137 throw new NullPointerException("intents is null");
1138 }
1139 if (resolvedTypes == null) {
1140 throw new NullPointerException("resolvedTypes is null");
1141 }
1142 if (intents.length != resolvedTypes.length) {
1143 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1144 }
1145
Craig Mautner23ac33b2013-04-01 16:26:35 -07001146
1147 int callingPid;
1148 if (callingUid >= 0) {
1149 callingPid = -1;
1150 } else if (caller == null) {
1151 callingPid = Binder.getCallingPid();
1152 callingUid = Binder.getCallingUid();
1153 } else {
1154 callingPid = callingUid = -1;
1155 }
1156 final long origId = Binder.clearCallingIdentity();
1157 try {
1158 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001159 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001160 for (int i=0; i<intents.length; i++) {
1161 Intent intent = intents[i];
1162 if (intent == null) {
1163 continue;
1164 }
1165
1166 // Refuse possible leaked file descriptors
1167 if (intent != null && intent.hasFileDescriptors()) {
1168 throw new IllegalArgumentException("File descriptors passed in Intent");
1169 }
1170
1171 boolean componentSpecified = intent.getComponent() != null;
1172
1173 // Don't modify the client's object!
1174 intent = new Intent(intent);
1175
1176 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001177 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001178 // TODO: New, check if this is correct
1179 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1180
1181 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001182 (aInfo.applicationInfo.privateFlags
1183 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001184 throw new IllegalArgumentException(
1185 "FLAG_CANT_SAVE_STATE not supported here");
1186 }
1187
1188 Bundle theseOptions;
1189 if (options != null && i == intents.length-1) {
1190 theseOptions = options;
1191 } else {
1192 theseOptions = null;
1193 }
Craig Mautner6170f732013-04-02 13:05:23 -07001194 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001195 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1196 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001197 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001198 if (res < 0) {
1199 return res;
1200 }
1201
1202 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1203 }
1204 }
1205 } finally {
1206 Binder.restoreCallingIdentity(origId);
1207 }
1208
1209 return ActivityManager.START_SUCCESS;
1210 }
1211
Craig Mautner2420ead2013-04-01 17:13:20 -07001212 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001213 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001214 throws RemoteException {
1215
Craig Mautner2568c3a2015-03-26 14:22:34 -07001216 if (andResume) {
1217 r.startFreezingScreenLocked(app, 0);
1218 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001219
Craig Mautner2568c3a2015-03-26 14:22:34 -07001220 // schedule launch ticks to collect information about slow apps.
1221 r.startLaunchTickingLocked();
1222 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001223
1224 // Have the window manager re-evaluate the orientation of
1225 // the screen based on the new activity order. Note that
1226 // as a result of this, it can call back into the activity
1227 // manager with a new orientation. We don't care about that,
1228 // because the activity is not currently running so we are
1229 // just restarting it anyway.
1230 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001231 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001232 mService.mConfiguration,
1233 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001234 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 }
1236
1237 r.app = app;
1238 app.waitingToKill = null;
1239 r.launchCount++;
1240 r.lastLaunchTime = SystemClock.uptimeMillis();
1241
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001242 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001243
1244 int idx = app.activities.indexOf(r);
1245 if (idx < 0) {
1246 app.activities.add(r);
1247 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001248 mService.updateLruProcessLocked(app, true, null);
1249 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001250
Craig Mautner15df08a2015-04-01 12:17:18 -07001251 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001252 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1253 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001254 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001255 }
1256
1257 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001258 try {
1259 if (app.thread == null) {
1260 throw new RemoteException();
1261 }
1262 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001263 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001264 if (andResume) {
1265 results = r.results;
1266 newIntents = r.newIntents;
1267 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001268 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1269 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1270 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001271 if (andResume) {
1272 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1273 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001274 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001276 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001277 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001278 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001279 }
1280 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1281 r.sleeping = false;
1282 r.forceNewConfig = false;
1283 mService.showAskCompatModeDialogLocked(r);
1284 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001285 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001286 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1287 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1288 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001289 final String profileFile = mService.mProfileFile;
1290 if (profileFile != null) {
1291 ParcelFileDescriptor profileFd = mService.mProfileFd;
1292 if (profileFd != null) {
1293 try {
1294 profileFd = profileFd.dup();
1295 } catch (IOException e) {
1296 if (profileFd != null) {
1297 try {
1298 profileFd.close();
1299 } catch (IOException o) {
1300 }
1301 profileFd = null;
1302 }
1303 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001304 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001305
1306 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1307 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001308 }
1309 }
1310 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001311
Craig Mautner2568c3a2015-03-26 14:22:34 -07001312 if (andResume) {
1313 app.hasShownUi = true;
1314 app.pendingUiClean = true;
1315 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001316 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001317 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001318 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001319 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001320 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001321 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001322
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001323 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001324 // This may be a heavy-weight process! Note that the package
1325 // manager will ensure that only activity can run in the main
1326 // process of the .apk, which is the only thing that will be
1327 // considered heavy-weight.
1328 if (app.processName.equals(app.info.packageName)) {
1329 if (mService.mHeavyWeightProcess != null
1330 && mService.mHeavyWeightProcess != app) {
1331 Slog.w(TAG, "Starting new heavy weight process " + app
1332 + " when already running "
1333 + mService.mHeavyWeightProcess);
1334 }
1335 mService.mHeavyWeightProcess = app;
1336 Message msg = mService.mHandler.obtainMessage(
1337 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1338 msg.obj = r;
1339 mService.mHandler.sendMessage(msg);
1340 }
1341 }
1342
1343 } catch (RemoteException e) {
1344 if (r.launchFailed) {
1345 // This is the second time we failed -- finish activity
1346 // and give up.
1347 Slog.e(TAG, "Second failure launching "
1348 + r.intent.getComponent().flattenToShortString()
1349 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001350 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001351 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1352 "2nd-crash", false);
1353 return false;
1354 }
1355
1356 // This is the first time we failed -- restart process and
1357 // retry.
1358 app.activities.remove(r);
1359 throw e;
1360 }
1361
1362 r.launchFailed = false;
1363 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001364 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001365 }
1366
1367 if (andResume) {
1368 // As part of the process of launching, ActivityThread also performs
1369 // a resume.
1370 stack.minimalResumeActivityLocked(r);
1371 } else {
1372 // This activity is not starting in the resumed state... which
1373 // should look like we asked it to pause+stop (but remain visible),
1374 // and it has done so and reported back the current icicle and
1375 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001376 if (DEBUG_STATES) Slog.v(TAG_STATES,
1377 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001378 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001379 r.stopped = true;
1380 }
1381
1382 // Launch the new version setup screen if needed. We do this -after-
1383 // launching the initial activity (that is, home), so that it can have
1384 // a chance to initialize itself while in the background, making the
1385 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001386 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001387 mService.startSetupActivityLocked();
1388 }
1389
Dianne Hackborn465fa392014-09-14 14:21:18 -07001390 // Update any services we are bound to that might care about whether
1391 // their client may have activities.
1392 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1393
Craig Mautner2420ead2013-04-01 17:13:20 -07001394 return true;
1395 }
1396
Craig Mautnere79d42682013-04-01 19:01:53 -07001397 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001398 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001399 // Is this activity's application already running?
1400 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001401 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001402
1403 r.task.stack.setLaunchTime(r);
1404
1405 if (app != null && app.thread != null) {
1406 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001407 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1408 || !"android".equals(r.info.packageName)) {
1409 // Don't add this if it is a platform component that is marked
1410 // to run in multiple processes, because this is actually
1411 // part of the framework so doesn't make sense to track as a
1412 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001413 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1414 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001415 }
George Mount2c92c972014-03-20 09:38:23 -07001416 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001417 return;
1418 } catch (RemoteException e) {
1419 Slog.w(TAG, "Exception when starting activity "
1420 + r.intent.getComponent().flattenToShortString(), e);
1421 }
1422
1423 // If a dead object exception was thrown -- fall through to
1424 // restart the application.
1425 }
1426
1427 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001428 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001429 }
1430
Craig Mautner6170f732013-04-02 13:05:23 -07001431 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001432 Intent intent, String resolvedType, ActivityInfo aInfo,
1433 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1434 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001435 int callingPid, int callingUid, String callingPackage,
1436 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001437 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1438 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001439 int err = ActivityManager.START_SUCCESS;
1440
1441 ProcessRecord callerApp = null;
1442 if (caller != null) {
1443 callerApp = mService.getRecordForAppLocked(caller);
1444 if (callerApp != null) {
1445 callingPid = callerApp.pid;
1446 callingUid = callerApp.info.uid;
1447 } else {
1448 Slog.w(TAG, "Unable to find app for caller " + caller
1449 + " (pid=" + callingPid + ") when starting: "
1450 + intent.toString());
1451 err = ActivityManager.START_PERMISSION_DENIED;
1452 }
1453 }
1454
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001455 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1456
Craig Mautner6170f732013-04-02 13:05:23 -07001457 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001458 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001459 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001460 + " on display " + (container == null ? (mFocusedStack == null ?
1461 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1462 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1463 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001464 }
1465
1466 ActivityRecord sourceRecord = null;
1467 ActivityRecord resultRecord = null;
1468 if (resultTo != null) {
1469 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001470 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1471 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001472 if (sourceRecord != null) {
1473 if (requestCode >= 0 && !sourceRecord.finishing) {
1474 resultRecord = sourceRecord;
1475 }
1476 }
1477 }
Craig Mautner6170f732013-04-02 13:05:23 -07001478
Dianne Hackborn91097de2014-04-04 18:02:06 -07001479 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001480
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001481 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001482 // Transfer the result target from the source activity to the new
1483 // one being started, including any failures.
1484 if (requestCode >= 0) {
1485 ActivityOptions.abort(options);
1486 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1487 }
1488 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001489 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1490 resultRecord = null;
1491 }
Craig Mautner6170f732013-04-02 13:05:23 -07001492 resultWho = sourceRecord.resultWho;
1493 requestCode = sourceRecord.requestCode;
1494 sourceRecord.resultTo = null;
1495 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001496 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001497 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001498 if (sourceRecord.launchedFromUid == callingUid) {
1499 // The new activity is being launched from the same uid as the previous
1500 // activity in the flow, and asking to forward its result back to the
1501 // previous. In this case the activity is serving as a trampoline between
1502 // the two, so we also want to update its launchedFromPackage to be the
1503 // same as the previous activity. Note that this is safe, since we know
1504 // these two packages come from the same uid; the caller could just as
1505 // well have supplied that same package name itself. This specifially
1506 // deals with the case of an intent picker/chooser being launched in the app
1507 // flow to redirect to an activity picked by the user, where we want the final
1508 // activity to consider it to have been launched by the previous app activity.
1509 callingPackage = sourceRecord.launchedFromPackage;
1510 }
Craig Mautner6170f732013-04-02 13:05:23 -07001511 }
1512
1513 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1514 // We couldn't find a class that can handle the given Intent.
1515 // That's the end of that!
1516 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1517 }
1518
1519 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1520 // We couldn't find the specific class specified in the Intent.
1521 // Also the end of the line.
1522 err = ActivityManager.START_CLASS_NOT_FOUND;
1523 }
1524
Dianne Hackborn91097de2014-04-04 18:02:06 -07001525 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1526 && sourceRecord.task.voiceSession != null) {
1527 // If this activity is being launched as part of a voice session, we need
1528 // to ensure that it is safe to do so. If the upcoming activity will also
1529 // be part of the voice session, we can only launch it if it has explicitly
1530 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001531 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001532 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1533 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001534 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001535 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1536 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001537 Slog.w(TAG,
1538 "Activity being started in current voice task does not support voice: "
1539 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001540 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1541 }
1542 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001543 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001544 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1545 }
1546 }
1547 }
1548
1549 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1550 // If the caller is starting a new voice session, just make sure the target
1551 // is actually allowing it to run this way.
1552 try {
1553 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1554 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001555 Slog.w(TAG,
1556 "Activity being started in new voice task does not support: "
1557 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001558 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1559 }
1560 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001561 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001562 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1563 }
1564 }
1565
louis_changcd5d1982014-08-01 10:09:08 +08001566 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1567
Craig Mautner6170f732013-04-02 13:05:23 -07001568 if (err != ActivityManager.START_SUCCESS) {
1569 if (resultRecord != null) {
1570 resultStack.sendActivityResultLocked(-1,
1571 resultRecord, resultWho, requestCode,
1572 Activity.RESULT_CANCELED, null);
1573 }
Craig Mautner6170f732013-04-02 13:05:23 -07001574 ActivityOptions.abort(options);
1575 return err;
1576 }
1577
Svetoslav7008b512015-06-24 18:47:07 -07001578 boolean abort = false;
1579
Svet Ganov99b60432015-06-27 13:15:22 -07001580 final int startAnyPerm = mService.checkPermission(
1581 START_ANY_ACTIVITY, callingPid, callingUid);
1582
1583 if (startAnyPerm != PERMISSION_GRANTED) {
1584 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001585 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001586 final int actionRestriction = getActionRestrictionForCallingPackage(
1587 intent.getAction(), callingPackage, callingPid, callingUid);
1588
1589 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1590 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1591 if (resultRecord != null) {
1592 resultStack.sendActivityResultLocked(-1,
1593 resultRecord, resultWho, requestCode,
1594 Activity.RESULT_CANCELED, null);
1595 }
1596 String msg;
1597 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1598 msg = "Permission Denial: starting " + intent.toString()
1599 + " from " + callerApp + " (pid=" + callingPid
1600 + ", uid=" + callingUid + ")" + " with revoked permission "
1601 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1602 } else if (!aInfo.exported) {
1603 msg = "Permission Denial: starting " + intent.toString()
1604 + " from " + callerApp + " (pid=" + callingPid
1605 + ", uid=" + callingUid + ")"
1606 + " not exported from uid " + aInfo.applicationInfo.uid;
1607 } else {
1608 msg = "Permission Denial: starting " + intent.toString()
1609 + " from " + callerApp + " (pid=" + callingPid
1610 + ", uid=" + callingUid + ")"
1611 + " requires " + aInfo.permission;
1612 }
1613 Slog.w(TAG, msg);
1614 throw new SecurityException(msg);
1615 }
1616
1617 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1618 String message = "Appop Denial: starting " + intent.toString()
1619 + " from " + callerApp + " (pid=" + callingPid
1620 + ", uid=" + callingUid + ")"
1621 + " requires " + AppOpsManager.permissionToOp(
1622 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1623 Slog.w(TAG, message);
1624 abort = true;
1625 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1626 String message = "Appop Denial: starting " + intent.toString()
1627 + " from " + callerApp + " (pid=" + callingPid
1628 + ", uid=" + callingUid + ")"
1629 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1630 Slog.w(TAG, message);
1631 abort = true;
1632 }
Svetoslav7008b512015-06-24 18:47:07 -07001633 }
1634
1635 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001636 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001637
Craig Mautner6170f732013-04-02 13:05:23 -07001638 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001639 try {
1640 // The Intent we give to the watcher has the extra data
1641 // stripped off, since it can contain private information.
1642 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001643 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001644 aInfo.applicationInfo.packageName);
1645 } catch (RemoteException e) {
1646 mService.mController = null;
1647 }
Ben Gruver5e207332013-04-03 17:41:37 -07001648 }
Craig Mautner6170f732013-04-02 13:05:23 -07001649
Ben Gruver5e207332013-04-03 17:41:37 -07001650 if (abort) {
1651 if (resultRecord != null) {
1652 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001653 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001654 }
Ben Gruver5e207332013-04-03 17:41:37 -07001655 // We pretend to the caller that it was really started, but
1656 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001657 ActivityOptions.abort(options);
1658 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001659 }
1660
1661 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001662 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001663 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001664 if (outActivity != null) {
1665 outActivity[0] = r;
1666 }
1667
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001668 if (r.appTimeTracker == null && sourceRecord != null) {
1669 // If the caller didn't specify an explicit time tracker, we want to continue
1670 // tracking under any it has.
1671 r.appTimeTracker = sourceRecord.appTimeTracker;
1672 }
1673
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001674 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001675 if (voiceSession == null && (stack.mResumedActivity == null
1676 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001677 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1678 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001679 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001680 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001681 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001682 ActivityOptions.abort(options);
1683 return ActivityManager.START_SWITCHES_CANCELED;
1684 }
1685 }
1686
1687 if (mService.mDidAppSwitch) {
1688 // This is the second allowed switch since we stopped switches,
1689 // so now just generally allow switches. Use case: user presses
1690 // home (switches disabled, switch to home, mDidAppSwitch now true);
1691 // user taps a home icon (coming from home so allowed, we hit here
1692 // and now allow anyone to switch again).
1693 mService.mAppSwitchesAllowedTime = 0;
1694 } else {
1695 mService.mDidAppSwitch = true;
1696 }
1697
Craig Mautneree36c772014-07-16 14:56:05 -07001698 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001699
Dianne Hackborn91097de2014-04-04 18:02:06 -07001700 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001701 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001702
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001703 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001704 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001705 // activity start, but we are not actually doing an activity
1706 // switch... just dismiss the keyguard now, because we
1707 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001708 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001709 }
1710 return err;
1711 }
1712
Svet Ganov99b60432015-06-27 13:15:22 -07001713 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001714 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001715 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1716 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001717 == PackageManager.PERMISSION_DENIED) {
1718 return ACTIVITY_RESTRICTION_PERMISSION;
1719 }
1720
Christopher Tateff7add02015-08-17 10:23:22 -07001721 if (activityInfo.permission == null) {
1722 return ACTIVITY_RESTRICTION_NONE;
1723 }
1724
Svet Ganov99b60432015-06-27 13:15:22 -07001725 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1726 if (opCode == AppOpsManager.OP_NONE) {
1727 return ACTIVITY_RESTRICTION_NONE;
1728 }
1729
1730 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1731 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001732 if (!ignoreTargetSecurity) {
1733 return ACTIVITY_RESTRICTION_APPOP;
1734 }
Svet Ganov99b60432015-06-27 13:15:22 -07001735 }
1736
1737 return ACTIVITY_RESTRICTION_NONE;
1738 }
1739
Svetoslav7008b512015-06-24 18:47:07 -07001740 private int getActionRestrictionForCallingPackage(String action,
1741 String callingPackage, int callingPid, int callingUid) {
1742 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001743 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001744 }
1745
1746 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1747 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001748 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001749 }
1750
1751 final PackageInfo packageInfo;
1752 try {
1753 packageInfo = mService.mContext.getPackageManager()
1754 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1755 } catch (PackageManager.NameNotFoundException e) {
1756 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001757 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001758 }
1759
1760 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001761 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001762 }
1763
1764 if (mService.checkPermission(permission, callingPid, callingUid) ==
1765 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001766 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001767 }
1768
1769 final int opCode = AppOpsManager.permissionToOpCode(permission);
1770 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001771 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001772 }
1773
1774 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1775 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001776 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001777 }
1778
Svet Ganov99b60432015-06-27 13:15:22 -07001779 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001780 }
1781
Chong Zhang0fa656b2015-08-31 15:17:21 -07001782 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001783 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001784
1785 // On leanback only devices we should keep all activities in the same stack.
1786 if (!mLeanbackOnlyDevice &&
1787 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001788
1789 ActivityStack stack;
1790
Wale Ogunwale7d701172015-03-11 15:36:30 -07001791 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001792 stack = task.stack;
1793 if (stack.isOnHomeDisplay()) {
1794 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001795 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1796 "computeStackFocus: Setting " + "focused stack to r=" + r
1797 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001798 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001799 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001800 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001801 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001802 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001803 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001804 }
1805
Craig Mautnere0a38842013-12-16 16:14:02 -08001806 final ActivityContainer container = r.mInitialActivityContainer;
1807 if (container != null) {
1808 // The first time put it on the desired stack, after this put on task stack.
1809 r.mInitialActivityContainer = null;
1810 return container.mStack;
1811 }
1812
Chong Zhanga8be5e52015-10-05 17:06:28 -07001813 // The fullscreen stack can contain any task regardless of if the task is resizeable
1814 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1815 // If the freeform stack has focus, and the activity to be launched is resizeable,
1816 // we can also put it in the focused stack.
1817 final boolean canUseFocusedStack =
1818 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1819 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1820 if (canUseFocusedStack
Wale Ogunwale13a9b162015-09-22 21:38:51 -07001821 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001822 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001823 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001824 return mFocusedStack;
1825 }
1826
Wale Ogunwale706ed792015-08-02 10:29:44 -07001827 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001828 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1829 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001830 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001831 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1832 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001833 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001834 "computeStackFocus: Setting focused stack=" + stack);
1835 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001836 }
1837 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001838
Wale Ogunwale706ed792015-08-02 10:29:44 -07001839 // If there is no suitable dynamic stack then we figure out which static stack to use.
Wale Ogunwale13a9b162015-09-22 21:38:51 -07001840 final int stackId = task != null ? task.getLaunchStackId() :
Chong Zhang0fa656b2015-08-31 15:17:21 -07001841 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1842 FULLSCREEN_WORKSPACE_STACK_ID;
1843 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001844 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1845 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001846 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001847 }
1848 return mHomeStack;
1849 }
1850
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001851 boolean setFocusedStack(ActivityRecord r, String reason) {
1852 if (r == null) {
1853 // Not sure what you are trying to do, but it is not going to work...
1854 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001855 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001856 final TaskRecord task = r.task;
1857 if (task == null || task.stack == null) {
1858 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1859 return false;
1860 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001861 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001862 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001863 }
1864
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001865 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001866 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001867 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001868 final Intent intent = r.intent;
1869 final int callingUid = r.launchedFromUid;
1870
Chong Zhang0fa656b2015-08-31 15:17:21 -07001871 boolean overrideBounds = false;
1872 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001873 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
1874 ActivityOptions opts = new ActivityOptions(options);
1875 if (opts.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001876 overrideBounds = true;
Chong Zhang56f171d2015-10-08 10:39:23 -07001877 newBounds = opts.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001878 }
1879 }
1880
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001881 // In some flows in to this function, we retrieve the task record and hold on to it
1882 // without a lock before calling back in to here... so the task at this point may
1883 // not actually be in recents. Check for that, and if it isn't in recents just
1884 // consider it invalid.
1885 if (inTask != null && !inTask.inRecents) {
1886 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1887 inTask = null;
1888 }
1889
Craig Mautnerad400af2014-08-13 16:41:36 -07001890 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001891 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1892 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001893
Craig Mautnera228ae92014-07-09 05:44:55 -07001894 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001895 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001896 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001897 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1898 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1899 "\"singleInstance\" or \"singleTask\"");
1900 launchFlags &=
1901 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1902 } else {
1903 switch (r.info.documentLaunchMode) {
1904 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1905 break;
1906 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1907 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1908 break;
1909 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1910 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1911 break;
1912 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1913 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1914 break;
1915 }
1916 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001917
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001918 final boolean launchTaskBehind = r.mLaunchTaskBehind
1919 && !launchSingleTask && !launchSingleInstance
1920 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001921
Wale Ogunwale7d701172015-03-11 15:36:30 -07001922 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1923 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001924 // For whatever reason this activity is being launched into a new
1925 // task... yet the caller has requested a result back. Well, that
1926 // is pretty messed up, so instead immediately send back a cancel
1927 // and let the new task continue launched as normal without a
1928 // dependency on its originator.
1929 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1930 r.resultTo.task.stack.sendActivityResultLocked(-1,
1931 r.resultTo, r.resultWho, r.requestCode,
1932 Activity.RESULT_CANCELED, null);
1933 r.resultTo = null;
1934 }
1935
1936 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1937 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1938 }
1939
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001940 // If we are actually going to launch in to a new task, there are some cases where
1941 // we further want to do multiple task.
1942 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1943 if (launchTaskBehind
1944 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1945 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1946 }
1947 }
1948
Craig Mautner8849a5e2013-04-02 16:41:03 -07001949 // We'll invoke onUserLeaving before onPause only if the launching
1950 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001951 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001952 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1953 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001954
1955 // If the caller has asked not to resume at this point, we make note
1956 // of this in the record so that we can skip it when trying to find
1957 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001958 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001959 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001960 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001961 }
1962
Craig Mautnera254cd72014-05-25 16:47:39 -07001963 ActivityRecord notTop =
1964 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001965
1966 // If the onlyIfNeeded flag is set, then we can do this if the activity
1967 // being launched is the same as the one making the call... or, as
1968 // a special case, if we do not know the caller then we count the
1969 // current top activity as the caller.
1970 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1971 ActivityRecord checkedCaller = sourceRecord;
1972 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001973 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001974 }
1975 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1976 // Caller is not the same as launcher, so always needed.
1977 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1978 }
1979 }
1980
Craig Mautner8849a5e2013-04-02 16:41:03 -07001981 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001982 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001983
1984 // If the caller is not coming from another activity, but has given us an
1985 // explicit task into which they would like us to launch the new activity,
1986 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001987 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1988 final Intent baseIntent = inTask.getBaseIntent();
1989 final ActivityRecord root = inTask.getRootActivity();
1990 if (baseIntent == null) {
1991 ActivityOptions.abort(options);
1992 throw new IllegalArgumentException("Launching into task without base intent: "
1993 + inTask);
1994 }
1995
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001996 // If this task is empty, then we are adding the first activity -- it
1997 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001998 if (launchSingleInstance || launchSingleTask) {
1999 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2000 ActivityOptions.abort(options);
2001 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2002 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002003 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002004 if (root != null) {
2005 ActivityOptions.abort(options);
2006 throw new IllegalArgumentException("Caller with inTask " + inTask
2007 + " has root " + root + " but target is singleInstance/Task");
2008 }
2009 }
2010
2011 // If task is empty, then adopt the interesting intent launch flags in to the
2012 // activity being started.
2013 if (root == null) {
2014 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2015 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2016 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2017 launchFlags = (launchFlags&~flagsOfInterest)
2018 | (baseIntent.getFlags()&flagsOfInterest);
2019 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002020 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002021 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002022
Dianne Hackborn962d5352014-08-29 16:27:40 -07002023 // If the task is not empty and the caller is asking to start it as the root
2024 // of a new task, then we don't actually want to start this on the task. We
2025 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002026 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002027 addingToTask = false;
2028
2029 } else {
2030 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002031 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002032
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002033 reuseTask = inTask;
2034 } else {
2035 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002036 // Launch ResolverActivity in the source task, so that it stays in the task
2037 // bounds when in freeform workspace.
2038 // Also put noDisplay activities in the source task. These by itself can
2039 // be placed in any task/stack, however it could launch other activities
2040 // like ResolverActivity, and we want those to stay in the original task.
2041 if (r.isResolverActivity() || r.noDisplay) {
2042 addingToTask = true;
2043 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002044 }
2045
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002046 if (inTask == null) {
2047 if (sourceRecord == null) {
2048 // This activity is not being started from another... in this
2049 // case we -always- start a new task.
2050 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2051 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2052 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2053 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2054 }
2055 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2056 // The original activity who is starting us is running as a single
2057 // instance... this new activity it is starting must go on its
2058 // own task.
2059 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2060 } else if (launchSingleInstance || launchSingleTask) {
2061 // The activity being started is a single instance... it always
2062 // gets launched into its own task.
2063 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2064 }
2065 }
2066
2067 ActivityInfo newTaskInfo = null;
2068 Intent newTaskIntent = null;
2069 ActivityStack sourceStack;
2070 if (sourceRecord != null) {
2071 if (sourceRecord.finishing) {
2072 // If the source is finishing, we can't further count it as our source. This
2073 // is because the task it is associated with may now be empty and on its way out,
2074 // so we don't want to blindly throw it in to that task. Instead we will take
2075 // the NEW_TASK flow and try to find a task for it. But save the task information
2076 // so it can be used when creating the new task.
2077 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2078 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2079 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2080 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2081 newTaskInfo = sourceRecord.info;
2082 newTaskIntent = sourceRecord.task.intent;
2083 }
2084 sourceRecord = null;
2085 sourceStack = null;
2086 } else {
2087 sourceStack = sourceRecord.task.stack;
2088 }
2089 } else {
2090 sourceStack = null;
2091 }
2092
2093 boolean movedHome = false;
2094 ActivityStack targetStack;
2095
2096 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002097 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002098
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002099 // We may want to try to place the new activity in to an existing task. We always
2100 // do this if the target activity is singleTask or singleInstance; we will also do
2101 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2102 // us to still place it in a new task: multi task, always doc mode, or being asked to
2103 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002104 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2105 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002106 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002107 // If bring to front is requested, and no result is requested and we have not
2108 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002109 // we can find a task that was started with this same
2110 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002111 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002112 // See if there is a task to bring to the front. If this is
2113 // a SINGLE_INSTANCE activity, there can be one and only one
2114 // instance of it in the history, and it is always in its own
2115 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002116 ActivityRecord intentActivity = !launchSingleInstance ?
2117 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002118 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002119 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2120 // but still needs to be a lock task mode violation since the task gets
2121 // cleared out and the device would otherwise leave the locked task.
2122 if (isLockTaskModeViolation(intentActivity.task,
2123 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2124 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002125 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002126 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002127 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2128 }
Craig Mautner29219d92013-04-16 20:19:12 -07002129 if (r.task == null) {
2130 r.task = intentActivity.task;
2131 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002132 if (intentActivity.task.intent == null) {
2133 // This task was started because of movement of
2134 // the activity based on affinity... now that we
2135 // are actually launching it, we can assign the
2136 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002137 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002138 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002139 targetStack = intentActivity.task.stack;
2140 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002141 // If the target task is not in the front, then we need
2142 // to bring it to the front... except... well, with
2143 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2144 // to have the same behavior as if a new instance was
2145 // being started, which means not bringing it to the front
2146 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002147 final ActivityStack focusStack = getFocusedStack();
2148 ActivityRecord curTop = (focusStack == null)
2149 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002150 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002151 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002152 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002153 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002154 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2155 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002156 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002157 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002158 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2159 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002160 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002161 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002162 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002163 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002164 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002165 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2166 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002167 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002168 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002169 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002170 options = null;
2171 }
2172 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002173 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002174 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002175 + " from " + intentActivity);
2176 targetStack.moveToFront("intentActivityFound");
2177 }
2178
Craig Mautner8849a5e2013-04-02 16:41:03 -07002179 // If the caller has requested that the target task be
2180 // reset, then do so.
2181 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2182 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2183 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002184 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002185 // We don't need to start a new activity, and
2186 // the client said not to do anything if that
2187 // is the case, so this is it! And for paranoia, make
2188 // sure we have correctly resumed the top activity.
2189 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002190 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002191
2192 // Make sure to notify Keyguard as well if we are not running an app
2193 // transition later.
2194 if (!movedToFront) {
2195 notifyActivityDrawnForKeyguard();
2196 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002197 } else {
2198 ActivityOptions.abort(options);
2199 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002200 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002201 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2202 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002203 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002204 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002205 // The caller has requested to completely replace any
2206 // existing task with its new activity. Well that should
2207 // not be too hard...
2208 reuseTask = intentActivity.task;
2209 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002210 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002211 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002212 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002213 // In this situation we want to remove all activities
2214 // from the task up to the one being started. In most
2215 // cases this means we are resetting the task to its
2216 // initial state.
2217 ActivityRecord top =
2218 intentActivity.task.performClearTaskLocked(r, launchFlags);
2219 if (top != null) {
2220 if (top.frontOfTask) {
2221 // Activity aliases may mean we use different
2222 // intents for the top activity, so make sure
2223 // the task now has the identity of the new
2224 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002225 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226 }
2227 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2228 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002229 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002230 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002231 // A special case: we need to start the activity because it is not
2232 // currently running, and the caller has asked to clear the current
2233 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002234 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002235 // Now pretend like this activity is being started by the top of its
2236 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002237 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002238 TaskRecord task = sourceRecord.task;
2239 if (task != null && task.stack == null) {
2240 // Target stack got cleared when we all activities were removed
2241 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002242 targetStack = computeStackFocus(
2243 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002244 targetStack.addTask(
2245 task, !launchTaskBehind /* toTop */, false /* moving */);
2246 }
2247
Craig Mautner8849a5e2013-04-02 16:41:03 -07002248 }
2249 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2250 // In this case the top activity on the task is the
2251 // same as the one being launched, so we take that
2252 // as a request to bring the task to the foreground.
2253 // If the top activity in the task is the root
2254 // activity, deliver this new intent to it if it
2255 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002256 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002257 && intentActivity.realActivity.equals(r.realActivity)) {
2258 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2259 intentActivity.task);
2260 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002261 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002262 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002263 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2264 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002265 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2266 // In this case we are launching the root activity
2267 // of the task, but with a different intent. We
2268 // should start a new instance on top.
2269 addingToTask = true;
2270 sourceRecord = intentActivity;
2271 }
2272 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2273 // In this case an activity is being launched in to an
2274 // existing task, without resetting that task. This
2275 // is typically the situation of launching an activity
2276 // from a notification or shortcut. We want to place
2277 // the new activity on top of the current task.
2278 addingToTask = true;
2279 sourceRecord = intentActivity;
2280 } else if (!intentActivity.task.rootWasReset) {
2281 // In this case we are launching in to an existing task
2282 // that has not yet been started from its front door.
2283 // The current task has been brought to the front.
2284 // Ideally, we'd probably like to place this new task
2285 // at the bottom of its stack, but that's a little hard
2286 // to do with the current organization of the code so
2287 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002288 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002289 }
2290 if (!addingToTask && reuseTask == null) {
2291 // We didn't do anything... but it was needed (a.k.a., client
2292 // don't use that intent!) And for paranoia, make
2293 // sure we have correctly resumed the top activity.
2294 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002295 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002296 if (!movedToFront) {
2297 // Make sure to notify Keyguard as well if we are not running an app
2298 // transition later.
2299 notifyActivityDrawnForKeyguard();
2300 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002301 } else {
2302 ActivityOptions.abort(options);
2303 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002304 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002305 return ActivityManager.START_TASK_TO_FRONT;
2306 }
2307 }
2308 }
2309 }
2310
2311 //String uri = r.intent.toURI();
2312 //Intent intent2 = new Intent(uri);
2313 //Slog.i(TAG, "Given intent: " + r.intent);
2314 //Slog.i(TAG, "URI is: " + uri);
2315 //Slog.i(TAG, "To intent: " + intent2);
2316
2317 if (r.packageName != null) {
2318 // If the activity being launched is the same as the one currently
2319 // at the top, then we need to check if it should only be launched
2320 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002321 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002322 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002323 if (top != null && r.resultTo == null) {
2324 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2325 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002326 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002327 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002328 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2329 top.task);
2330 // For paranoia, make sure we have correctly
2331 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002332 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002333 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002334 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002335 }
2336 ActivityOptions.abort(options);
2337 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2338 // We don't need to start a new activity, and
2339 // the client said not to do anything if that
2340 // is the case, so this is it!
2341 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2342 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002343 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002344 return ActivityManager.START_DELIVERED_TO_TOP;
2345 }
2346 }
2347 }
2348 }
2349
2350 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002351 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002352 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2353 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002354 }
2355 ActivityOptions.abort(options);
2356 return ActivityManager.START_CLASS_NOT_FOUND;
2357 }
2358
2359 boolean newTask = false;
2360 boolean keepCurTransition = false;
2361
Craig Mautnerbb742462014-07-07 15:28:55 -07002362 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002363 sourceRecord.task : null;
2364
Craig Mautner8849a5e2013-04-02 16:41:03 -07002365 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002366 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002367 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002368 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002369 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002370 if (doResume) {
2371 targetStack.moveToFront("startingNewTask");
2372 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002373
Craig Mautner8849a5e2013-04-02 16:41:03 -07002374 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002375 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2376 newTaskInfo != null ? newTaskInfo : r.info,
2377 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002378 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2379 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002380 if (overrideBounds) {
2381 r.task.updateOverrideConfiguration(newBounds);
2382 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002383 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2384 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002385 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002386 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002387 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002388 if (isLockTaskModeViolation(r.task)) {
2389 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2390 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2391 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002392 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002393 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002394 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2395 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002396 // Caller wants to appear on home activity, so before starting
2397 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002398 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002399 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002400 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002401 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002402 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002403 if (isLockTaskModeViolation(sourceTask)) {
2404 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2405 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2406 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002407 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002408 if (doResume) {
2409 targetStack.moveToFront("sourceStackToFront");
2410 }
Craig Mautner737fae22014-10-15 12:52:10 -07002411 final TaskRecord topTask = targetStack.topTask();
2412 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002413 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002414 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002415 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002416 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002417 // In this case, we are adding the activity to an existing
2418 // task, but the caller has asked to clear that task if the
2419 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002420 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002421 keepCurTransition = true;
2422 if (top != null) {
2423 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002424 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002425 // For paranoia, make sure we have correctly
2426 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002427 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002428 if (doResume) {
2429 targetStack.resumeTopActivityLocked(null);
2430 }
2431 ActivityOptions.abort(options);
2432 return ActivityManager.START_DELIVERED_TO_TOP;
2433 }
2434 } else if (!addingToTask &&
2435 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2436 // In this case, we are launching an activity in our own task
2437 // that may already be running somewhere in the history, and
2438 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002439 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002440 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002441 final TaskRecord task = top.task;
2442 task.moveActivityToFrontLocked(top);
2443 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002444 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002445 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002446 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002447 if (doResume) {
2448 targetStack.resumeTopActivityLocked(null);
2449 }
2450 return ActivityManager.START_DELIVERED_TO_TOP;
2451 }
2452 }
2453 // An existing activity is starting this new activity, so we want
2454 // to keep the new one in the same task as the one that is starting
2455 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002456 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002457 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002458 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002459
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002460 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002461 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002462 // task it has provided to us.
2463 if (isLockTaskModeViolation(inTask)) {
2464 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2465 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2466 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002467 if (overrideBounds) {
2468 inTask.updateOverrideConfiguration(newBounds);
2469 int stackId = inTask.getLaunchStackId();
2470 if (stackId != inTask.stack.mStackId) {
2471 moveTaskToStackUncheckedLocked(
2472 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2473 }
2474 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002475 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002476 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2477 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002478
2479 // Check whether we should actually launch the new activity in to the task,
2480 // or just reuse the current activity on top.
2481 ActivityRecord top = inTask.getTopActivity();
2482 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2483 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2484 || launchSingleTop || launchSingleTask) {
2485 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2486 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2487 // We don't need to start a new activity, and
2488 // the client said not to do anything if that
2489 // is the case, so this is it!
2490 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2491 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002492 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002493 return ActivityManager.START_DELIVERED_TO_TOP;
2494 }
2495 }
2496
Dianne Hackborn962d5352014-08-29 16:27:40 -07002497 if (!addingToTask) {
2498 // We don't actually want to have this activity added to the task, so just
2499 // stop here but still tell the caller that we consumed the intent.
2500 ActivityOptions.abort(options);
2501 return ActivityManager.START_TASK_TO_FRONT;
2502 }
2503
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002504 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002505 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002506 + " in explicit task " + r.task);
2507
Craig Mautner8849a5e2013-04-02 16:41:03 -07002508 } else {
2509 // This not being started from an existing activity, and not part
2510 // of a new task... just put it in the top task, though these days
2511 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002512 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002513 if (doResume) {
2514 targetStack.moveToFront("addingToTopTask");
2515 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002516 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002517 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002518 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002519 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002520 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002521 + " in new guessed " + r.task);
2522 }
2523
2524 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002525 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002526
Craig Mautner76e2a762014-06-24 09:05:43 -07002527 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2528 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2529 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002530 if (newTask) {
2531 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2532 }
2533 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002534 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002535 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002536 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002537 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002538 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002539 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002540 return ActivityManager.START_SUCCESS;
2541 }
2542
Craig Mautneree36c772014-07-16 14:56:05 -07002543 final void doPendingActivityLaunchesLocked(boolean doResume) {
2544 while (!mPendingActivityLaunches.isEmpty()) {
2545 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002546 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002547 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002548 }
2549 }
2550
Craig Mautner7f13ed32014-07-28 14:00:35 -07002551 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002552 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2553 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002554 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002555 mPendingActivityLaunches.remove(palNdx);
2556 }
2557 }
2558 }
2559
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002560 void setLaunchSource(int uid) {
2561 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2562 }
2563
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002564 void acquireLaunchWakelock() {
2565 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2566 throw new IllegalStateException("Calling must be system uid");
2567 }
2568 mLaunchingActivity.acquire();
2569 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2570 // To be safe, don't allow the wake lock to be held for too long.
2571 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2572 }
2573 }
2574
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002575 /**
2576 * Called when the frontmost task is idle.
2577 * @return the state of mService.mBooting before this was called.
2578 */
2579 private boolean checkFinishBootingLocked() {
2580 final boolean booting = mService.mBooting;
2581 boolean enableScreen = false;
2582 mService.mBooting = false;
2583 if (!mService.mBooted) {
2584 mService.mBooted = true;
2585 enableScreen = true;
2586 }
2587 if (booting || enableScreen) {
2588 mService.postFinishBooting(booting, enableScreen);
2589 }
2590 return booting;
2591 }
2592
Craig Mautnerf3333272013-04-22 10:55:53 -07002593 // Checked.
2594 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2595 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002596 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002597
Craig Mautnerf3333272013-04-22 10:55:53 -07002598 ArrayList<ActivityRecord> stops = null;
2599 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002600 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002601 int NS = 0;
2602 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002603 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002604 boolean activityRemoved = false;
2605
Wale Ogunwale7d701172015-03-11 15:36:30 -07002606 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002607 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002608 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2609 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002610 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2611 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002612 if (fromTimeout) {
2613 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002614 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002615
2616 // This is a hack to semi-deal with a race condition
2617 // in the client where it can be constructed with a
2618 // newer configuration from when we asked it to launch.
2619 // We'll update with whatever configuration it now says
2620 // it used to launch.
2621 if (config != null) {
2622 r.configuration = config;
2623 }
2624
2625 // We are now idle. If someone is waiting for a thumbnail from
2626 // us, we can now deliver.
2627 r.idle = true;
2628
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002629 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002630 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002631 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002632 }
2633 }
2634
2635 if (allResumedActivitiesIdle()) {
2636 if (r != null) {
2637 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002638 }
2639
2640 if (mLaunchingActivity.isHeld()) {
2641 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2642 if (VALIDATE_WAKE_LOCK_CALLER &&
2643 Binder.getCallingUid() != Process.myUid()) {
2644 throw new IllegalStateException("Calling must be system uid");
2645 }
2646 mLaunchingActivity.release();
2647 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002648 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002649 }
2650
2651 // Atomically retrieve all of the other things to do.
2652 stops = processStoppingActivitiesLocked(true);
2653 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002654 if ((NF = mFinishingActivities.size()) > 0) {
2655 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002656 mFinishingActivities.clear();
2657 }
2658
Craig Mautnerf3333272013-04-22 10:55:53 -07002659 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002660 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002661 mStartingUsers.clear();
2662 }
2663
Craig Mautnerf3333272013-04-22 10:55:53 -07002664 // Stop any activities that are scheduled to do so but have been
2665 // waiting for the next one to start.
2666 for (int i = 0; i < NS; i++) {
2667 r = stops.get(i);
2668 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002669 if (stack != null) {
2670 if (r.finishing) {
2671 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2672 } else {
2673 stack.stopActivityLocked(r);
2674 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002675 }
2676 }
2677
2678 // Finish any activities that are scheduled to do so but have been
2679 // waiting for the next one to start.
2680 for (int i = 0; i < NF; i++) {
2681 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002682 final ActivityStack stack = r.task.stack;
2683 if (stack != null) {
2684 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2685 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002686 }
2687
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002688 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002689 // Complete user switch
2690 if (startingUsers != null) {
2691 for (int i = 0; i < startingUsers.size(); i++) {
2692 mService.finishUserSwitch(startingUsers.get(i));
2693 }
2694 }
2695 // Complete starting up of background users
2696 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002697 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002698 mStartingBackgroundUsers.clear();
2699 for (int i = 0; i < startingUsers.size(); i++) {
2700 mService.finishUserBoot(startingUsers.get(i));
2701 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002702 }
2703 }
2704
2705 mService.trimApplications();
2706 //dump();
2707 //mWindowManager.dump();
2708
Craig Mautnerf3333272013-04-22 10:55:53 -07002709 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002710 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002711 }
2712
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002713 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002714 }
2715
Craig Mautner8e569572013-10-11 17:36:59 -07002716 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002717 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002718 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2719 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002720 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2721 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2722 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002723 }
Craig Mautner19091252013-10-05 00:03:53 -07002724 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002725 }
2726
2727 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002728 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2729 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002730 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2731 stacks.get(stackNdx).closeSystemDialogsLocked();
2732 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002733 }
2734 }
2735
Craig Mautner93529a42013-10-04 15:03:13 -07002736 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002737 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002738 }
2739
Craig Mautner8d341ef2013-03-26 09:03:27 -07002740 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002741 * Update the last used stack id for non-current user (current user's last
2742 * used stack is the focused stack)
2743 */
2744 void updateUserStackLocked(int userId, ActivityStack stack) {
2745 if (userId != mCurrentUser) {
2746 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2747 }
2748 }
2749
2750 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002751 * @return true if some activity was finished (or would have finished if doit were true).
2752 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002753 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2754 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002755 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002756 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2757 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002758 final int numStacks = stacks.size();
2759 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2760 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002761 if (stack.finishDisabledPackageActivitiesLocked(
2762 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002763 didSomething = true;
2764 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002765 }
2766 }
2767 return didSomething;
2768 }
2769
Dianne Hackborna413dc02013-07-12 12:02:55 -07002770 void updatePreviousProcessLocked(ActivityRecord r) {
2771 // Now that this process has stopped, we may want to consider
2772 // it to be the previous app to try to keep around in case
2773 // the user wants to return to it.
2774
2775 // First, found out what is currently the foreground app, so that
2776 // we don't blow away the previous app if this activity is being
2777 // hosted by the process that is actually still the foreground.
2778 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002779 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2780 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002781 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2782 final ActivityStack stack = stacks.get(stackNdx);
2783 if (isFrontStack(stack)) {
2784 if (stack.mResumedActivity != null) {
2785 fgApp = stack.mResumedActivity.app;
2786 } else if (stack.mPausingActivity != null) {
2787 fgApp = stack.mPausingActivity.app;
2788 }
2789 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002790 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002791 }
2792 }
2793
2794 // Now set this one as the previous process, only if that really
2795 // makes sense to.
2796 if (r.app != null && fgApp != null && r.app != fgApp
2797 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002798 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002799 mService.mPreviousProcess = r.app;
2800 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2801 }
2802 }
2803
Craig Mautner05d29032013-05-03 13:40:13 -07002804 boolean resumeTopActivitiesLocked() {
2805 return resumeTopActivitiesLocked(null, null, null);
2806 }
2807
2808 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2809 Bundle targetOptions) {
2810 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002811 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002812 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002813 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002814 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002815 if (isFrontStack(targetStack)) {
2816 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2817 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002818
Craig Mautnere0a38842013-12-16 16:14:02 -08002819 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2820 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002821 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2822 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002823 if (stack == targetStack) {
2824 // Already started above.
2825 continue;
2826 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002827 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002828 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002829 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002830 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002831 }
Craig Mautner05d29032013-05-03 13:40:13 -07002832 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002833 }
2834
Todd Kennedy539db512014-12-15 09:57:55 -08002835 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002836 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2837 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002838 final int numStacks = stacks.size();
2839 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2840 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002841 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002842 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002843 }
2844 }
2845
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002846 void finishVoiceTask(IVoiceInteractionSession session) {
2847 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2848 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2849 final int numStacks = stacks.size();
2850 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2851 final ActivityStack stack = stacks.get(stackNdx);
2852 stack.finishVoiceTask(session);
2853 }
2854 }
2855 }
2856
Craig Mautner299f9602015-01-26 09:47:33 -08002857 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002858 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2859 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002860 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002861 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2862 // Caller wants the home activity moved with it. To accomplish this,
2863 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002864 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002865 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002866 if (task.stack == null) {
2867 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2868 + task + " to front. Stack is null");
2869 return;
2870 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002871
2872 int stackId = task.stack.mStackId;
2873 if (task.mResizeable && options != null) {
2874 ActivityOptions opts = new ActivityOptions(options);
2875 if (opts.hasBounds()) {
2876 Rect bounds = opts.getBounds();
2877 task.updateOverrideConfiguration(bounds);
Chong Zhang87b21722015-09-21 15:39:51 -07002878 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2879 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002880 stackId = task.getLaunchStackId();
2881 }
2882 }
2883
2884 if (stackId != task.stack.mStackId) {
2885 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, FORCE_FOCUS, reason);
2886 } else {
2887 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002888 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2889 reason);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002890 }
2891
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002892 if (DEBUG_STACK) Slog.d(TAG_STACK,
2893 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002894 }
2895
Craig Mautner967212c2013-04-13 21:10:58 -07002896 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002897 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002898 }
2899
2900 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002901 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2902 if (activityContainer != null) {
2903 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002904 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002905 if (!createStaticStackIfNeeded
2906 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2907 return null;
2908 }
2909 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002910 }
2911
Craig Mautner967212c2013-04-13 21:10:58 -07002912 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002913 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002914 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2915 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002916 }
2917 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002918 }
2919
Craig Mautner4a1cb222013-12-04 16:14:06 -08002920 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002921 ActivityRecord homeActivity = getHomeActivity();
2922 if (homeActivity != null) {
2923 return homeActivity.appToken;
2924 }
2925 return null;
2926 }
2927
2928 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002929 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002930 }
2931
2932 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002933 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2934 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2935 final TaskRecord task = tasks.get(taskNdx);
2936 if (task.isHomeTask()) {
2937 final ArrayList<ActivityRecord> activities = task.mActivities;
2938 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2939 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002940 if (r.isHomeActivity()
2941 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002942 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002943 }
2944 }
2945 }
2946 }
2947 return null;
2948 }
2949
Todd Kennedyca4d8422015-01-15 15:19:22 -08002950 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002952 ActivityContainer activityContainer =
2953 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002954 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002955 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2956 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002957 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002958 return activityContainer;
2959 }
2960
Craig Mautner34b73df2014-01-12 21:11:08 -08002961 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002962 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2963 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2964 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002965 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2966 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002967 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002968 }
2969 }
2970
Craig Mautner95da1082014-02-24 17:54:35 -08002971 void deleteActivityContainer(IActivityContainer container) {
2972 ActivityContainer activityContainer = (ActivityContainer)container;
2973 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002974 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2975 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002976 final int stackId = activityContainer.mStackId;
2977 mActivityContainers.remove(stackId);
2978 mWindowManager.removeStack(stackId);
2979 }
2980 }
2981
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002982 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
2983 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002984 final ActivityStack stack = getStack(stackId);
2985 if (stack == null) {
2986 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2987 return;
2988 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002989
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002990 if (!allowResizeInDockedMode
2991 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
2992 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2993 // stack size changing so things don't get out of sync.
2994 return;
2995 }
2996
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002997 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
2998
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002999 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003000
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003001 mTmpBounds.clear();
3002 mTmpConfigs.clear();
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003003 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3004 for (int i = tasks.size() - 1; i >= 0; i--) {
3005 TaskRecord task = tasks.get(i);
3006 if (task.mResizeable) {
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003007 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003008 // For freeform stack we don't adjust the size of the tasks to match that
3009 // of the stack, but we do try to make sure the tasks are still contained
3010 // with the bounds of the stack.
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003011 tempRect2.set(task.mBounds);
3012 fitWithinBounds(tempRect2, bounds);
3013 task.updateOverrideConfiguration(tempRect2);
3014 } else {
3015 task.updateOverrideConfiguration(bounds);
3016 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003017 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003018
3019 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3020 mTmpBounds.put(task.taskId, task.mBounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003021 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003022 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003023 if (stack.mStackId == DOCKED_STACK_ID) {
3024 // Dock stack funness...Yay!
3025 if (stack.mFullscreen) {
3026 // The dock stack went fullscreen which is kinda like dismissing it.
3027 // In this case we make all other static stacks fullscreen and move all
3028 // docked stack tasks to the fullscreen stack.
3029 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwalea4e86602015-10-08 18:09:02 -07003030 if (i != DOCKED_STACK_ID && getStack(i) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003031 resizeStackLocked(i, null, preserveWindows, true);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003032 }
3033 }
3034
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003035 final int count = tasks.size();
3036 for (int i = 0; i < count; i++) {
3037 moveTaskToStackLocked(tasks.get(i).taskId,
3038 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
3039 }
3040
3041 // stack shouldn't contain anymore activities, so nothing to resume.
3042 r = null;
3043 } else {
3044 // Docked stacks occupy a dedicated region on screen so the size of all other
3045 // static stacks need to be adjusted so they don't overlap with the docked stack.
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003046 // We get the bounds to use from window manager which has been adjusted for any
3047 // screen controls and is also the same for all stacks.
3048 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003049
3050 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3051 if (i != DOCKED_STACK_ID) {
3052 ActivityStack otherStack = getStack(i);
3053 if (otherStack != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003054 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003055 }
3056 }
3057 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003058 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003059 // Since we are resizing the stack, all other operations should strive to preserve
3060 // windows.
3061 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003062 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003063 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003064
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003065 if (r != null) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003066 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003067 // And we need to make sure at this point that all other activities
3068 // are made visible with the correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003069 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003070 if (!updated) {
3071 resumeTopActivitiesLocked(stack, null, null);
3072 }
3073 }
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003074
3075 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003076 }
3077
Chong Zhang6de2ae82015-09-30 18:25:21 -07003078 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003079 if (!task.mResizeable) {
3080 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3081 return;
3082 }
3083
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003084 adjustForMinimalTaskDimensions(task, bounds);
3085
Chong Zhang87b21722015-09-21 15:39:51 -07003086 // If this is a forced resize, let it go through even if the bounds is not changing,
3087 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003088 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003089 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003090 // Nothing to do here...
3091 return;
3092 }
3093
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003094 if (!mWindowManager.isValidTaskId(task.taskId)) {
3095 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003096 // All we can do for now is update the bounds so it can be used when the task is
3097 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003098 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003099 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3100 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003101 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003102 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003103 return;
3104 }
3105
Chong Zhang6de2ae82015-09-30 18:25:21 -07003106 // Do not move the task to another stack here.
3107 // This method assumes that the task is already placed in the right stack.
3108 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003109
Chong Zhang6de2ae82015-09-30 18:25:21 -07003110 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003111
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003112 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003113 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003114 // way and the activity was kept the way it was. If it's false, it means the activity had
3115 // to be relaunched due to configuration change.
3116 boolean kept = true;
3117 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003118 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003119 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003120 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003121 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003122 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003123 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003124 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003125 resumeTopActivitiesLocked(stack, null, null);
3126 }
3127 }
3128 }
Chong Zhang87b21722015-09-21 15:39:51 -07003129 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003130
3131 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003132 }
3133
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003134 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3135 if (bounds == null) {
3136 return;
3137 }
3138 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3139 : task.mMinimalSize;
3140 final boolean adjustWidth = minimalSize > bounds.width();
3141 final boolean adjustHeight = minimalSize > bounds.height();
3142 if (!(adjustWidth || adjustHeight)) {
3143 return;
3144 }
3145 Rect taskBounds = task.mBounds;
3146 if (adjustWidth) {
3147 if (taskBounds != null && bounds.right == taskBounds.right) {
3148 bounds.left = bounds.right - minimalSize;
3149 } else {
3150 // Either left bounds match, or neither match, or the previous bounds were
3151 // fullscreen and we default to keeping left.
3152 bounds.right = bounds.left + minimalSize;
3153 }
3154 }
3155 if (adjustHeight) {
3156 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3157 bounds.top = bounds.bottom - minimalSize;
3158 } else {
3159 // Either top bounds match, or neither match, or the previous bounds were
3160 // fullscreen and we default to keeping top.
3161 bounds.bottom = bounds.top + minimalSize;
3162 }
3163 }
3164 }
3165
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003166 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003167 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3168 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003169 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003170 }
3171
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003172 ActivityContainer activityContainer = new ActivityContainer(stackId);
3173 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003174 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003175 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003176 }
3177
3178 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003179 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003180 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3181 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003182 break;
3183 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003184 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003185 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003186 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003187 }
3188
Chong Zhang5dcb2752015-08-18 13:50:26 -07003189 /**
3190 * Restores a recent task to a stack
3191 * @param task The recent task to be restored.
3192 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003193 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003194 * @return true if the task has been restored successfully.
3195 */
3196 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3197 if (stackId == INVALID_STACK_ID) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07003198 stackId = mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003199 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003200 if (task.stack != null) {
3201 // Task has already been restored once. See if we need to do anything more
3202 if (task.stack.mStackId == stackId) {
3203 // Nothing else to do since it is already restored in the right stack.
3204 return true;
3205 }
3206 // Remove current stack association, so we can re-associate the task with the
3207 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003208 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003209 }
3210
3211 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003212 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003213
3214 if (stack == null) {
3215 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003216 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3217 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003218 return false;
3219 }
3220
3221 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003222 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3223 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003224 final ArrayList<ActivityRecord> activities = task.mActivities;
3225 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003226 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003227 }
3228 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003229 }
3230
Wale Ogunwale040b4702015-08-06 18:10:50 -07003231 /**
3232 * Moves the specified task record to the input stack id.
3233 * WARNING: This method performs an unchecked/raw move of the task and
3234 * can leave the system in an unstable state if used incorrectly.
3235 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3236 * to a stack.
3237 * @param task Task to move.
3238 * @param stackId Id of stack to move task to.
3239 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003240 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003241 * @param reason Reason the task is been moved.
3242 * @return The stack the task was moved to.
3243 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003244 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003245 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3246 final ActivityRecord r = task.getTopActivity();
3247 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3248 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3249
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003250 final boolean resizeable = task.mResizeable;
3251 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3252 // if a docked stack is created below which will lead to the stack we are moving from and
3253 // its resizeable tasks being resized.
3254 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003255 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003256 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003257 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3258 if (task.stack != null) {
3259 task.stack.removeTask(task, reason, MOVING);
3260 }
3261 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003262
3263 // If the task had focus before (or we're requested to move focus),
3264 // move focus to the new stack.
3265 if (forceFocus || wasFocused) {
3266 // If the task owns the last resumed activity, transfer that together,
3267 // so that we don't resume the same activity again in the new stack.
3268 // Apps may depend on onResume()/onPause() being called in pairs.
3269 if (wasResumed) {
3270 stack.mResumedActivity = r;
Filip Gruszczynski013b12f2015-10-12 17:32:10 -07003271 // Move the stack in which we are placing the task to the front. We don't use
3272 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
3273 // already focused, the call will short-circuit and do nothing.
3274 stack.moveToFront(reason);
3275 } else {
3276 // We need to not only move the stack to the front, but also have the activity
3277 // focused. This will achieve both goals.
3278 mService.setFocusedActivityLocked(r, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003279 }
Filip Gruszczynski013b12f2015-10-12 17:32:10 -07003280
Chong Zhang02898352015-08-21 17:27:14 -07003281 }
3282
Wale Ogunwale040b4702015-08-06 18:10:50 -07003283 return stack;
3284 }
3285
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003286 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003287 final TaskRecord task = anyTaskForIdLocked(taskId);
3288 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003289 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003290 return;
3291 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003292 final String reason = "moveTaskToStack";
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003293 if (stackId == DOCKED_STACK_ID || stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
3294 // We are about to relaunch the activity because its configuration changed due to
3295 // being maximized, i.e. size change. The activity will first remove the old window
3296 // and then add a new one. This call will tell window manager about this, so it can
3297 // preserve the old window until the new one is drawn. This prevents having a gap
3298 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003299 // entrance of the new window to be properly animated.
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003300 ActivityRecord r = task.getTopActivity();
3301 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3302 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003303 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003304 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003305
3306 // Make sure the task has the appropriate bounds/size for the stack it is in.
3307 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003308 resizeTaskLocked(task, stack.mBounds,
3309 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003310 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3311 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003312 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3313 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003314 } else if (stackId == DOCKED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003315 resizeTaskLocked(task, stack.mBounds,
3316 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003317 }
3318
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003319 // The task might have already been running and its visibility needs to be synchronized with
3320 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003321 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003322 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003323 }
3324
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003325 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3326 final TaskRecord task = anyTaskForIdLocked(taskId);
3327 if (task == null) {
3328 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3329 return;
3330 }
3331 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003332 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003333 mWindowManager.positionTaskInStack(taskId, stackId, position);
3334 final boolean stackChanged = task.stack != null && task.stack != stack;
3335 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003336 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003337 }
3338 stack.positionTask(task, position, stackChanged);
3339 // The task might have already been running and its visibility needs to be synchronized with
3340 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003341 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003342 resumeTopActivitiesLocked();
3343 }
3344
Craig Mautnerac6f8432013-07-17 13:24:59 -07003345 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003346 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003347 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3348 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003349 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3350 final ActivityStack stack = stacks.get(stackNdx);
3351 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003352 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003353 continue;
3354 }
3355 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003356 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3357 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003358 continue;
3359 }
3360 final ActivityRecord ar = stack.findTaskLocked(r);
3361 if (ar != null) {
3362 return ar;
3363 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003364 }
3365 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003366 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003367 return null;
3368 }
3369
3370 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003371 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3372 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003373 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3374 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3375 if (ar != null) {
3376 return ar;
3377 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003378 }
3379 }
3380 return null;
3381 }
3382
Craig Mautner8d341ef2013-03-26 09:03:27 -07003383 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003384 scheduleSleepTimeout();
3385 if (!mGoingToSleep.isHeld()) {
3386 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003387 if (mLaunchingActivity.isHeld()) {
3388 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3389 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003390 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003391 mLaunchingActivity.release();
3392 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003393 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003394 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003395 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003396 }
3397
3398 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003399 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003400
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003401 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003402 final long endTime = System.currentTimeMillis() + timeout;
3403 while (true) {
3404 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003405 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3406 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003407 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3408 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3409 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003410 }
3411 if (cantShutdown) {
3412 long timeRemaining = endTime - System.currentTimeMillis();
3413 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003414 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003415 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003416 } catch (InterruptedException e) {
3417 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003418 } else {
3419 Slog.w(TAG, "Activity manager shutdown timed out");
3420 timedout = true;
3421 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003422 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003423 } else {
3424 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003425 }
3426 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003427
3428 // Force checkReadyForSleep to complete.
3429 mSleepTimeout = true;
3430 checkReadyForSleepLocked();
3431
Craig Mautner8d341ef2013-03-26 09:03:27 -07003432 return timedout;
3433 }
3434
3435 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003436 removeSleepTimeouts();
3437 if (mGoingToSleep.isHeld()) {
3438 mGoingToSleep.release();
3439 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003440 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3441 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003442 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3443 final ActivityStack stack = stacks.get(stackNdx);
3444 stack.awakeFromSleepingLocked();
3445 if (isFrontStack(stack)) {
3446 resumeTopActivitiesLocked();
3447 }
Craig Mautner5314a402013-09-26 12:40:16 -07003448 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003449 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003450 mGoingToSleepActivities.clear();
3451 }
3452
3453 void activitySleptLocked(ActivityRecord r) {
3454 mGoingToSleepActivities.remove(r);
3455 checkReadyForSleepLocked();
3456 }
3457
3458 void checkReadyForSleepLocked() {
3459 if (!mService.isSleepingOrShuttingDown()) {
3460 // Do not care.
3461 return;
3462 }
3463
3464 if (!mSleepTimeout) {
3465 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003466 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3467 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003468 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3469 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3470 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003471 }
3472
3473 if (mStoppingActivities.size() > 0) {
3474 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003475 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003476 + mStoppingActivities.size() + " activities");
3477 scheduleIdleLocked();
3478 dontSleep = true;
3479 }
3480
3481 if (mGoingToSleepActivities.size() > 0) {
3482 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003483 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003484 + mGoingToSleepActivities.size() + " activities");
3485 dontSleep = true;
3486 }
3487
3488 if (dontSleep) {
3489 return;
3490 }
3491 }
3492
Craig Mautnere0a38842013-12-16 16:14:02 -08003493 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3494 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003495 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3496 stacks.get(stackNdx).goToSleep();
3497 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003498 }
3499
3500 removeSleepTimeouts();
3501
3502 if (mGoingToSleep.isHeld()) {
3503 mGoingToSleep.release();
3504 }
3505 if (mService.mShuttingDown) {
3506 mService.notifyAll();
3507 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003508 }
3509
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003510 boolean reportResumedActivityLocked(ActivityRecord r) {
3511 final ActivityStack stack = r.task.stack;
3512 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003513 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003514 }
3515 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003516 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003517 mWindowManager.executeAppTransition();
3518 return true;
3519 }
3520 return false;
3521 }
3522
Craig Mautner8d341ef2013-03-26 09:03:27 -07003523 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003524 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3525 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003526 int stackNdx = stacks.size() - 1;
3527 while (stackNdx >= 0) {
3528 stacks.get(stackNdx).handleAppCrashLocked(app);
3529 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003531 }
3532 }
3533
Jose Lima4b6c6692014-08-12 17:41:12 -07003534 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003535 final ActivityStack stack = r.task.stack;
3536 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003537 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3538 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003539 return false;
3540 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003541 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003542 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3543 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003544
3545 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003546 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003547 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003548 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003549 return true;
3550 }
3551
3552 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003553 if (visible && top.fullscreen) {
3554 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003555 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3556 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3557 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3558 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003559 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003560 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3561 // Only the activity set as currently visible behind should actively reset its
3562 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003563 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3564 "requestVisibleBehind: returning visible=" + visible
3565 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3566 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003567 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003568 }
3569
Jose Lima4b6c6692014-08-12 17:41:12 -07003570 stack.setVisibleBehindActivity(visible ? r : null);
3571 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003572 // Make the activity immediately above r opaque.
3573 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3574 if (next != null) {
3575 mService.convertFromTranslucent(next.appToken);
3576 }
3577 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003578 if (top.app != null && top.app.thread != null) {
3579 // Notify the top app of the change.
3580 try {
3581 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3582 } catch (RemoteException e) {
3583 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003584 }
3585 return true;
3586 }
3587
Craig Mautnerbb742462014-07-07 15:28:55 -07003588 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3589 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3590 r.mLaunchTaskBehind = false;
3591 final TaskRecord task = r.task;
3592 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003593 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003594 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003595 mWindowManager.setAppVisibility(r.appToken, false);
3596 }
3597
3598 void scheduleLaunchTaskBehindComplete(IBinder token) {
3599 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3600 }
3601
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003602 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3603 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003604 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003605 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3606 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003607 final int topStackNdx = stacks.size() - 1;
3608 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3609 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003610 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003611 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003612 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003613 }
3614
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003615 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3616 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3617 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3618 final int topStackNdx = stacks.size() - 1;
3619 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3620 final ActivityStack stack = stacks.get(stackNdx);
3621 stack.clearOtherAppTimeTrackers(except);
3622 }
3623 }
3624 }
3625
Craig Mautner8d341ef2013-03-26 09:03:27 -07003626 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003627 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3628 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003629 final int numStacks = stacks.size();
3630 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3631 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003632 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003633 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003634 }
3635 }
3636
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003637 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3638 // Examine all activities currently running in the process.
3639 TaskRecord firstTask = null;
3640 // Tasks is non-null only if two or more tasks are found.
3641 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003642 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3643 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003644 ActivityRecord r = app.activities.get(i);
3645 // First, if we find an activity that is in the process of being destroyed,
3646 // then we just aren't going to do anything for now; we want things to settle
3647 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003648 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003649 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003650 return;
3651 }
3652 // Don't consider any activies that are currently not in a state where they
3653 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003654 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3655 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003656 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003657 continue;
3658 }
3659 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003660 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003661 + " from " + r);
3662 if (firstTask == null) {
3663 firstTask = r.task;
3664 } else if (firstTask != r.task) {
3665 if (tasks == null) {
3666 tasks = new ArraySet<>();
3667 tasks.add(firstTask);
3668 }
3669 tasks.add(r.task);
3670 }
3671 }
3672 }
3673 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003674 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003675 return;
3676 }
3677 // If we have activities in multiple tasks that are in a position to be destroyed,
3678 // let's iterate through the tasks and release the oldest one.
3679 final int numDisplays = mActivityDisplays.size();
3680 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3681 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3682 // Step through all stacks starting from behind, to hit the oldest things first.
3683 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3684 final ActivityStack stack = stacks.get(stackNdx);
3685 // Try to release activities in this stack; if we manage to, we are done.
3686 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3687 return;
3688 }
3689 }
3690 }
3691 }
3692
Amith Yamasani37a40c22015-06-17 13:25:42 -07003693 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003694 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003695 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003696 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003697
Craig Mautner858d8a62013-04-23 17:08:34 -07003698 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003699 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3700 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003701 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003702 final ActivityStack stack = stacks.get(stackNdx);
3703 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003704 TaskRecord task = stack.topTask();
3705 if (task != null) {
3706 mWindowManager.moveTaskToTop(task.taskId);
3707 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003708 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003709 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003710
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003711 ActivityStack stack = getStack(restoreStackId);
3712 if (stack == null) {
3713 stack = mHomeStack;
3714 }
3715 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003716 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003717 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003718 } else {
3719 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003720 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003721 }
Craig Mautner93529a42013-10-04 15:03:13 -07003722 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003723 }
3724
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003725 /**
3726 * Add background users to send boot completed events to.
3727 * @param userId The user being started in the background
3728 * @param uss The state object for the user.
3729 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003730 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003731 mStartingBackgroundUsers.add(uss);
3732 }
3733
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003734 /** Checks whether the userid is a profile of the current user. */
3735 boolean isCurrentProfileLocked(int userId) {
3736 if (userId == mCurrentUser) return true;
3737 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3738 if (mService.mCurrentProfileIds[i] == userId) return true;
3739 }
3740 return false;
3741 }
3742
Chong Zhang45c25ce2015-08-10 22:18:26 -07003743 /** Checks whether the activity should be shown for current user. */
3744 boolean okToShowLocked(ActivityRecord r) {
3745 return r != null && (isCurrentProfileLocked(r.userId)
3746 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3747 }
3748
Craig Mautnerde4ef022013-04-07 19:01:33 -07003749 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003750 ArrayList<ActivityRecord> stops = null;
3751
3752 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003753 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3754 ActivityRecord s = mStoppingActivities.get(activityNdx);
3755 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003756 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003757 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3758 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003759 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003760 if (s.finishing) {
3761 // If this activity is finishing, it is sitting on top of
3762 // everyone else but we now know it is no longer needed...
3763 // so get rid of it. Otherwise, we need to go through the
3764 // normal flow and hide it once we determine that it is
3765 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003766 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003767 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003768 }
3769 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003770 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003771 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003772 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003773 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003774 }
3775 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003776 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003777 }
3778 }
3779
3780 return stops;
3781 }
3782
Craig Mautnercf910b02013-04-23 11:23:27 -07003783 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003784 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3785 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3786 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3787 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003788 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003789 final ActivityState state = r == null ? DESTROYED : r.state;
3790 if (isFrontStack(stack)) {
3791 if (r == null) Slog.e(TAG,
3792 "validateTop...: null top activity, stack=" + stack);
3793 else {
3794 final ActivityRecord pausing = stack.mPausingActivity;
3795 if (pausing != null && pausing == r) Slog.e(TAG,
3796 "validateTop...: top stack has pausing activity r=" + r
3797 + " state=" + state);
3798 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3799 "validateTop...: activity in front not resumed r=" + r
3800 + " state=" + state);
3801 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003802 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003803 final ActivityRecord resumed = stack.mResumedActivity;
3804 if (resumed != null && resumed == r) Slog.e(TAG,
3805 "validateTop...: back stack has resumed activity r=" + r
3806 + " state=" + state);
3807 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3808 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003809 }
3810 }
3811 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003812 }
3813
Craig Mautnere0570202015-05-13 13:06:11 -07003814 private String lockTaskModeToString() {
3815 switch (mLockTaskModeState) {
3816 case LOCK_TASK_MODE_LOCKED:
3817 return "LOCKED";
3818 case LOCK_TASK_MODE_PINNED:
3819 return "PINNED";
3820 case LOCK_TASK_MODE_NONE:
3821 return "NONE";
3822 default: return "unknown=" + mLockTaskModeState;
3823 }
3824 }
3825
Craig Mautner27084302013-03-25 08:05:25 -07003826 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003827 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003829 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3830 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3831 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003832 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003833 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3834 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3835 if (packages.size() > 0) {
3836 pw.println(" mLockTaskPackages (userId:packages)=");
3837 for (int i = 0; i < packages.size(); ++i) {
3838 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3839 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3840 }
3841 }
3842 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003843 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003844
Craig Mautner20e72272013-04-01 13:45:53 -07003845 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003846 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003847 }
3848
Dianne Hackborn390517b2013-05-30 15:03:32 -07003849 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3850 boolean needSep, String prefix) {
3851 if (activity != null) {
3852 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3853 if (needSep) {
3854 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003855 }
3856 pw.print(prefix);
3857 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003858 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003859 }
3860 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003861 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003862 }
3863
Craig Mautner8d341ef2013-03-26 09:03:27 -07003864 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3865 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003866 boolean printed = false;
3867 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003868 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3869 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003870 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003871 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003872 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003873 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003874 final ActivityStack stack = stacks.get(stackNdx);
3875 StringBuilder stackHeader = new StringBuilder(128);
3876 stackHeader.append(" Stack #");
3877 stackHeader.append(stack.mStackId);
3878 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003879 stackHeader.append("\n");
3880 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3881 stackHeader.append("\n");
3882 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003883 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3884 needSep, stackHeader.toString());
3885 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3886 !dumpAll, false, dumpPackage, true,
3887 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003888
Craig Mautner4a1cb222013-12-04 16:14:06 -08003889 needSep = printed;
3890 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3891 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003892 if (pr) {
3893 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003894 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003895 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003896 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3897 " mResumedActivity: ");
3898 if (pr) {
3899 printed = true;
3900 needSep = false;
3901 }
3902 if (dumpAll) {
3903 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3904 " mLastPausedActivity: ");
3905 if (pr) {
3906 printed = true;
3907 needSep = true;
3908 }
3909 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3910 needSep, " mLastNoHistoryActivity: ");
3911 }
3912 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003913 }
3914 }
3915
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003916 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3917 false, dumpPackage, true, " Activities waiting to finish:", null);
3918 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3919 false, dumpPackage, true, " Activities waiting to stop:", null);
3920 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3921 false, dumpPackage, true, " Activities waiting for another to become visible:",
3922 null);
3923 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3924 false, dumpPackage, true, " Activities waiting to sleep:", null);
3925 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3926 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003927
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003928 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003929 }
3930
Dianne Hackborn390517b2013-05-30 15:03:32 -07003931 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003932 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003933 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003934 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003935 String innerPrefix = null;
3936 String[] args = null;
3937 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003938 for (int i=list.size()-1; i>=0; i--) {
3939 final ActivityRecord r = list.get(i);
3940 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3941 continue;
3942 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003943 if (innerPrefix == null) {
3944 innerPrefix = prefix + " ";
3945 args = new String[0];
3946 }
3947 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003948 final boolean full = !brief && (complete || !r.isInHistory());
3949 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003950 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003951 needNL = false;
3952 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003953 if (header1 != null) {
3954 pw.println(header1);
3955 header1 = null;
3956 }
3957 if (header2 != null) {
3958 pw.println(header2);
3959 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003960 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003961 if (lastTask != r.task) {
3962 lastTask = r.task;
3963 pw.print(prefix);
3964 pw.print(full ? "* " : " ");
3965 pw.println(lastTask);
3966 if (full) {
3967 lastTask.dump(pw, prefix + " ");
3968 } else if (complete) {
3969 // Complete + brief == give a summary. Isn't that obvious?!?
3970 if (lastTask.intent != null) {
3971 pw.print(prefix); pw.print(" ");
3972 pw.println(lastTask.intent.toInsecureStringWithClip());
3973 }
3974 }
3975 }
3976 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3977 pw.print(" #"); pw.print(i); pw.print(": ");
3978 pw.println(r);
3979 if (full) {
3980 r.dump(pw, innerPrefix);
3981 } else if (complete) {
3982 // Complete + brief == give a summary. Isn't that obvious?!?
3983 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3984 if (r.app != null) {
3985 pw.print(innerPrefix); pw.println(r.app);
3986 }
3987 }
3988 if (client && r.app != null && r.app.thread != null) {
3989 // flush anything that is already in the PrintWriter since the thread is going
3990 // to write to the file descriptor directly
3991 pw.flush();
3992 try {
3993 TransferPipe tp = new TransferPipe();
3994 try {
3995 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3996 r.appToken, innerPrefix, args);
3997 // Short timeout, since blocking here can
3998 // deadlock with the application.
3999 tp.go(fd, 2000);
4000 } finally {
4001 tp.kill();
4002 }
4003 } catch (IOException e) {
4004 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4005 } catch (RemoteException e) {
4006 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4007 }
4008 needNL = true;
4009 }
4010 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004011 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004012 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004013
Craig Mautnerf3333272013-04-22 10:55:53 -07004014 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004015 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4016 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004017 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4018 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004019 }
4020
4021 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004022 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004023 }
4024
4025 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004026 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4027 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004028 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4029 }
4030
Craig Mautner05d29032013-05-03 13:40:13 -07004031 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004032 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4033 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4034 }
Craig Mautner05d29032013-05-03 13:40:13 -07004035 }
4036
Craig Mautner0eea92c2013-05-16 13:35:39 -07004037 void removeSleepTimeouts() {
4038 mSleepTimeout = false;
4039 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4040 }
4041
4042 final void scheduleSleepTimeout() {
4043 removeSleepTimeouts();
4044 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4045 }
4046
Craig Mautner4a1cb222013-12-04 16:14:06 -08004047 @Override
4048 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004049 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004050 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4051 }
4052
4053 @Override
4054 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004055 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004056 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4057 }
4058
4059 @Override
4060 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004061 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004062 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4063 }
4064
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004065 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004066 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004067 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004068 newDisplay = mActivityDisplays.get(displayId) == null;
4069 if (newDisplay) {
4070 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004071 if (activityDisplay.mDisplay == null) {
4072 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4073 return;
4074 }
Craig Mautner4504de52013-12-20 09:06:56 -08004075 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004076 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004077 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004078 }
Craig Mautner4504de52013-12-20 09:06:56 -08004079 if (newDisplay) {
4080 mWindowManager.onDisplayAdded(displayId);
4081 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004082 }
4083
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004084 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4085 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4086 return;
4087 }
4088 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4089 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4090 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4091 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4092 }
4093
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004094 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004095 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004096 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4097 if (activityDisplay != null) {
4098 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004099 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004100 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004101 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004102 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004103 }
4104 }
4105 mWindowManager.onDisplayRemoved(displayId);
4106 }
4107
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004108 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004109 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004110 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4111 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004112 // TODO: Update the bounds.
4113 }
4114 }
4115 mWindowManager.onDisplayChanged(displayId);
4116 }
4117
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004118 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004119 StackInfo info = new StackInfo();
4120 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4121 info.displayId = Display.DEFAULT_DISPLAY;
4122 info.stackId = stack.mStackId;
4123
4124 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4125 final int numTasks = tasks.size();
4126 int[] taskIds = new int[numTasks];
4127 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004128 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004129 for (int i = 0; i < numTasks; ++i) {
4130 final TaskRecord task = tasks.get(i);
4131 taskIds[i] = task.taskId;
4132 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4133 : task.realActivity != null ? task.realActivity.flattenToString()
4134 : task.getTopActivity() != null ? task.getTopActivity().packageName
4135 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004136 taskBounds[i] = new Rect();
4137 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004138 }
4139 info.taskIds = taskIds;
4140 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004141 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004142 return info;
4143 }
4144
4145 StackInfo getStackInfoLocked(int stackId) {
4146 ActivityStack stack = getStack(stackId);
4147 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004148 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004149 }
4150 return null;
4151 }
4152
4153 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004154 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004155 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4156 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004157 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004158 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004159 }
4160 }
4161 return list;
4162 }
4163
Craig Mautner15df08a2015-04-01 12:17:18 -07004164 TaskRecord getLockedTaskLocked() {
4165 final int top = mLockTaskModeTasks.size() - 1;
4166 if (top >= 0) {
4167 return mLockTaskModeTasks.get(top);
4168 }
4169 return null;
4170 }
4171
4172 boolean isLockedTask(TaskRecord task) {
4173 return mLockTaskModeTasks.contains(task);
4174 }
4175
4176 boolean isLastLockedTask(TaskRecord task) {
4177 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4178 }
4179
4180 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004181 if (!mLockTaskModeTasks.remove(task)) {
4182 return;
4183 }
4184 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4185 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004186 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004187 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4188 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004189 final Message lockTaskMsg = Message.obtain();
4190 lockTaskMsg.arg1 = task.userId;
4191 lockTaskMsg.what = LOCK_TASK_END_MSG;
4192 mHandler.sendMessage(lockTaskMsg);
4193 }
4194 }
4195
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004196 void showLockTaskToast() {
4197 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004198 }
4199
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004200 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4201 if (mLockTaskModeTasks.contains(task)) {
4202 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4203 }
4204 }
4205
Craig Mautner432f64e2015-05-20 14:59:57 -07004206 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4207 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004208 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004209 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004210 final TaskRecord lockedTask = getLockedTaskLocked();
4211 if (lockedTask != null) {
4212 removeLockedTaskLocked(lockedTask);
4213 if (!mLockTaskModeTasks.isEmpty()) {
4214 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004215 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4216 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004217 lockedTask.performClearTaskLocked();
4218 resumeTopActivitiesLocked();
4219 return;
4220 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004221 }
Craig Mautnere0570202015-05-13 13:06:11 -07004222 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4223 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004224 return;
4225 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004226
4227 // Should have already been checked, but do it again.
4228 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004229 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4230 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004231 return;
4232 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004233 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004234 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004235 return;
4236 }
4237
4238 if (mLockTaskModeTasks.isEmpty()) {
4239 // First locktask.
4240 final Message lockTaskMsg = Message.obtain();
4241 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4242 lockTaskMsg.arg1 = task.userId;
4243 lockTaskMsg.what = LOCK_TASK_START_MSG;
4244 lockTaskMsg.arg2 = lockTaskModeState;
4245 mHandler.sendMessage(lockTaskMsg);
4246 }
4247 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004248 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4249 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004250 mLockTaskModeTasks.remove(task);
4251 mLockTaskModeTasks.add(task);
4252
4253 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004254 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004255 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004256
4257 if (andResume) {
4258 findTaskToMoveToFrontLocked(task, 0, null, reason);
4259 resumeTopActivitiesLocked();
4260 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004261 }
4262
4263 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004264 return isLockTaskModeViolation(task, false);
4265 }
4266
4267 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4268 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004269 return false;
4270 }
4271 final int lockTaskAuth = task.mLockTaskAuth;
4272 switch (lockTaskAuth) {
4273 case LOCK_TASK_AUTH_DONT_LOCK:
4274 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004275 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004276 case LOCK_TASK_AUTH_LAUNCHABLE:
4277 case LOCK_TASK_AUTH_WHITELISTED:
4278 return false;
4279 case LOCK_TASK_AUTH_PINNABLE:
4280 // Pinnable tasks can't be launched on top of locktask tasks.
4281 return !mLockTaskModeTasks.isEmpty();
4282 default:
4283 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4284 return true;
4285 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004286 }
4287
Craig Mautner15df08a2015-04-01 12:17:18 -07004288 void onLockTaskPackagesUpdatedLocked() {
4289 boolean didSomething = false;
4290 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4291 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004292 final boolean wasWhitelisted =
4293 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4294 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004295 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004296 final boolean isWhitelisted =
4297 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4298 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4299 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004300 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004301 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4302 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004303 removeLockedTaskLocked(lockedTask);
4304 lockedTask.performClearTaskLocked();
4305 didSomething = true;
4306 }
4307 }
4308 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4309 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4310 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4311 final ActivityStack stack = stacks.get(stackNdx);
4312 stack.onLockTaskPackagesUpdatedLocked();
4313 }
4314 }
Craig Mautnere0570202015-05-13 13:06:11 -07004315 final ActivityRecord r = topRunningActivityLocked();
4316 final TaskRecord task = r != null ? r.task : null;
4317 if (mLockTaskModeTasks.isEmpty() && task != null
4318 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4319 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004320 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4321 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4322 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4323 false);
4324 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004325 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004326 if (didSomething) {
4327 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004328 }
4329 }
4330
Benjamin Franz43261142015-02-11 15:59:44 +00004331 int getLockTaskModeState() {
4332 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004333 }
4334
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004335 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004336
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004337 public ActivityStackSupervisorHandler(Looper looper) {
4338 super(looper);
4339 }
4340
Craig Mautnerf3333272013-04-22 10:55:53 -07004341 void activityIdleInternal(ActivityRecord r) {
4342 synchronized (mService) {
4343 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4344 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004345 }
4346
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004347 @Override
4348 public void handleMessage(Message msg) {
4349 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004350 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004351 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4352 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004353 if (mService.mDidDexOpt) {
4354 mService.mDidDexOpt = false;
4355 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4356 nmsg.obj = msg.obj;
4357 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4358 return;
4359 }
4360 // We don't at this point know if the activity is fullscreen,
4361 // so we need to be conservative and assume it isn't.
4362 activityIdleInternal((ActivityRecord)msg.obj);
4363 } break;
4364 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004365 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004366 activityIdleInternal((ActivityRecord)msg.obj);
4367 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004368 case RESUME_TOP_ACTIVITY_MSG: {
4369 synchronized (mService) {
4370 resumeTopActivitiesLocked();
4371 }
4372 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004373 case SLEEP_TIMEOUT_MSG: {
4374 synchronized (mService) {
4375 if (mService.isSleepingOrShuttingDown()) {
4376 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4377 mSleepTimeout = true;
4378 checkReadyForSleepLocked();
4379 }
4380 }
4381 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004382 case LAUNCH_TIMEOUT_MSG: {
4383 if (mService.mDidDexOpt) {
4384 mService.mDidDexOpt = false;
4385 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4386 return;
4387 }
4388 synchronized (mService) {
4389 if (mLaunchingActivity.isHeld()) {
4390 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4391 if (VALIDATE_WAKE_LOCK_CALLER
4392 && Binder.getCallingUid() != Process.myUid()) {
4393 throw new IllegalStateException("Calling must be system uid");
4394 }
4395 mLaunchingActivity.release();
4396 }
4397 }
4398 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004399 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004400 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004401 } break;
4402 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004403 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004404 } break;
4405 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004406 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004407 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004408 case CONTAINER_CALLBACK_VISIBILITY: {
4409 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004410 final IActivityContainerCallback callback = container.mCallback;
4411 if (callback != null) {
4412 try {
4413 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4414 } catch (RemoteException e) {
4415 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004416 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004417 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004418 case LOCK_TASK_START_MSG: {
4419 // When lock task starts, we disable the status bars.
4420 try {
Jason Monk62515be2014-05-21 16:06:19 -04004421 if (mLockTaskNotify == null) {
4422 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004423 }
Jason Monk62515be2014-05-21 16:06:19 -04004424 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004425 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004426 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004427 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004428 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004429 flags = StatusBarManager.DISABLE_MASK
4430 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004431 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004432 flags = StatusBarManager.DISABLE_MASK
4433 & (~StatusBarManager.DISABLE_BACK)
4434 & (~StatusBarManager.DISABLE_HOME)
4435 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004436 }
4437 getStatusBarService().disable(flags, mToken,
4438 mService.mContext.getPackageName());
4439 }
4440 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004441 if (getDevicePolicyManager() != null) {
4442 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004443 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004444 }
justinzhang5286d3f2014-05-12 17:06:01 -04004445 } catch (RemoteException ex) {
4446 throw new RuntimeException(ex);
4447 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004448 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004449 case LOCK_TASK_END_MSG: {
4450 // When lock task ends, we enable the status bars.
4451 try {
Jason Monk62515be2014-05-21 16:06:19 -04004452 if (getStatusBarService() != null) {
4453 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4454 mService.mContext.getPackageName());
4455 }
4456 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004457 if (getDevicePolicyManager() != null) {
4458 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4459 msg.arg1);
4460 }
Jason Monk62515be2014-05-21 16:06:19 -04004461 if (mLockTaskNotify == null) {
4462 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4463 }
4464 mLockTaskNotify.show(false);
4465 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004466 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004467 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004468 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004469 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004470 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004471 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004472 new LockPatternUtils(mService.mContext)
4473 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004474 }
4475 } catch (SettingNotFoundException e) {
4476 // No setting, don't lock.
4477 }
justinzhang5286d3f2014-05-12 17:06:01 -04004478 } catch (RemoteException ex) {
4479 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004480 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004481 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004482 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004483 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004484 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4485 if (mLockTaskNotify == null) {
4486 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4487 }
4488 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4489 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004490 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4491 final ActivityContainer container = (ActivityContainer) msg.obj;
4492 final IActivityContainerCallback callback = container.mCallback;
4493 if (callback != null) {
4494 try {
4495 callback.onAllActivitiesComplete(container.asBinder());
4496 } catch (RemoteException e) {
4497 }
4498 }
4499 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004500 case LAUNCH_TASK_BEHIND_COMPLETE: {
4501 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004502 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004503 if (r != null) {
4504 handleLaunchTaskBehindCompleteLocked(r);
4505 }
4506 }
4507 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004508 }
4509 }
4510 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004511
Ying Wangb081a592014-04-22 15:20:16 -07004512 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004513 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004514 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004515 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004516 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004517 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004518 ActivityRecord mParentActivity = null;
4519 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004520
Craig Mautnere3a00d72014-04-16 08:31:19 -07004521 boolean mVisible = true;
4522
Craig Mautner4a1cb222013-12-04 16:14:06 -08004523 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004524 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004525
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004526 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4527 final static int CONTAINER_STATE_NO_SURFACE = 1;
4528 final static int CONTAINER_STATE_FINISHING = 2;
4529 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4530
4531 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004532 synchronized (mService) {
4533 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004534 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004535 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004536 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004537 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004538 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004539
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004540 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004541 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004542 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004543 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004544 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004545 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004546 }
4547
4548 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004549 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004550 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004551 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4552 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004553 return;
4554 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004555 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004556 }
4557 }
4558
4559 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004560 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004561 synchronized (mService) {
4562 if (mActivityDisplay != null) {
4563 return mActivityDisplay.mDisplayId;
4564 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004565 }
4566 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004567 }
4568
Jeff Brownca9bc702014-02-11 14:32:56 -08004569 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004570 public int getStackId() {
4571 synchronized (mService) {
4572 return mStackId;
4573 }
4574 }
4575
4576 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004577 public boolean injectEvent(InputEvent event) {
4578 final long origId = Binder.clearCallingIdentity();
4579 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004580 synchronized (mService) {
4581 if (mActivityDisplay != null) {
4582 return mInputManagerInternal.injectInputEvent(event,
4583 mActivityDisplay.mDisplayId,
4584 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4585 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004586 }
4587 return false;
4588 } finally {
4589 Binder.restoreCallingIdentity(origId);
4590 }
4591 }
4592
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004593 @Override
4594 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004595 synchronized (mService) {
4596 if (mContainerState == CONTAINER_STATE_FINISHING) {
4597 return;
4598 }
4599 mContainerState = CONTAINER_STATE_FINISHING;
4600
Craig Mautnerd163e752014-06-13 17:18:47 -07004601 long origId = Binder.clearCallingIdentity();
4602 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004603 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004604 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004605 } finally {
4606 Binder.restoreCallingIdentity(origId);
4607 }
4608 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004609 }
4610
Craig Mautner60257702014-09-17 15:02:33 -07004611 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004612 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004613 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004614 if (mActivityDisplay != null) {
4615 mActivityDisplay.detachActivitiesLocked(mStack);
4616 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004617 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004618 }
4619 }
4620
4621 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004622 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004623 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004624 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004625 Binder.getCallingUid(), mCurrentUser, false,
4626 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004627
Craig Mautnere0a38842013-12-16 16:14:02 -08004628 // TODO: Switch to user app stacks here.
4629 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004630 final Uri data = intent.getData();
4631 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4632 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004633 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004634 checkEmbeddedAllowedInner(userId, intent, mimeType);
4635
4636 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4637 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004638 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004639 }
4640
4641 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004642 public final int startActivityIntentSender(IIntentSender intentSender)
4643 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004644 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4645
4646 if (!(intentSender instanceof PendingIntentRecord)) {
4647 throw new IllegalArgumentException("Bad PendingIntent object");
4648 }
4649
Craig Mautnerb9168362015-02-26 20:40:19 -08004650 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004651 Binder.getCallingUid(), mCurrentUser, false,
4652 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004653
4654 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4655 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4656 pendingIntent.key.requestResolvedType);
4657
4658 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4659 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4660 }
4661
4662 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004663 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004664 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004665 throw new SecurityException(
4666 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4667 }
4668 }
4669
Craig Mautnerdf88d732014-01-27 09:21:32 -08004670 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004671 public IBinder asBinder() {
4672 return this;
4673 }
4674
Craig Mautner4504de52013-12-20 09:06:56 -08004675 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004676 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004677 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004678 }
4679
Craig Mautner4a1cb222013-12-04 16:14:06 -08004680 ActivityStackSupervisor getOuter() {
4681 return ActivityStackSupervisor.this;
4682 }
4683
Craig Mautnerd163e752014-06-13 17:18:47 -07004684 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004685 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004686 }
4687
Craig Mautner6985bad2014-04-21 15:22:06 -07004688 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004689 void setVisible(boolean visible) {
4690 if (mVisible != visible) {
4691 mVisible = visible;
4692 if (mCallback != null) {
4693 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4694 0 /* unused */, this).sendToTarget();
4695 }
4696 }
4697 }
4698
Craig Mautner6985bad2014-04-21 15:22:06 -07004699 void setDrawn() {
4700 }
4701
Craig Mautner1b4bf852014-05-26 15:06:32 -07004702 // You can always start a new task on a regular ActivityStack.
4703 boolean isEligibleForNewTasks() {
4704 return true;
4705 }
4706
Craig Mautnerd163e752014-06-13 17:18:47 -07004707 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004708 detachLocked();
4709 deleteActivityContainer(this);
4710 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004711 }
4712
Craig Mautner34b73df2014-01-12 21:11:08 -08004713 @Override
4714 public String toString() {
4715 return mIdString + (mActivityDisplay == null ? "N" : "A");
4716 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004717 }
4718
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004719 private class VirtualActivityContainer extends ActivityContainer {
4720 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004721 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004722
4723 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4724 super(getNextStackId());
4725 mParentActivity = parent;
4726 mCallback = callback;
4727 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004728 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004729 }
4730
4731 @Override
4732 public void setSurface(Surface surface, int width, int height, int density) {
4733 super.setSurface(surface, width, height, density);
4734
4735 synchronized (mService) {
4736 final long origId = Binder.clearCallingIdentity();
4737 try {
4738 setSurfaceLocked(surface, width, height, density);
4739 } finally {
4740 Binder.restoreCallingIdentity(origId);
4741 }
4742 }
4743 }
4744
4745 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4746 if (mContainerState == CONTAINER_STATE_FINISHING) {
4747 return;
4748 }
4749 VirtualActivityDisplay virtualActivityDisplay =
4750 (VirtualActivityDisplay) mActivityDisplay;
4751 if (virtualActivityDisplay == null) {
4752 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004753 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004754 mActivityDisplay = virtualActivityDisplay;
4755 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004756 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004757 }
4758
4759 if (mSurface != null) {
4760 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004761 }
4762
Craig Mautner6985bad2014-04-21 15:22:06 -07004763 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004764 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004765 mStack.resumeTopActivityLocked(null);
4766 } else {
4767 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004768 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004769 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004770 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004771 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004772 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004773
Craig Mautnerd163e752014-06-13 17:18:47 -07004774 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004775
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004776 if (DEBUG_STACK) Slog.d(TAG_STACK,
4777 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004778 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004779
Craig Mautner6985bad2014-04-21 15:22:06 -07004780 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004781 boolean isAttachedLocked() {
4782 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004783 }
4784
4785 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004786 void setDrawn() {
4787 synchronized (mService) {
4788 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004789 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004790 }
4791 }
4792
Craig Mautner1b4bf852014-05-26 15:06:32 -07004793 // Never start a new task on an ActivityView if it isn't explicitly specified.
4794 @Override
4795 boolean isEligibleForNewTasks() {
4796 return false;
4797 }
4798
Craig Mautnerd163e752014-06-13 17:18:47 -07004799 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004800 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004801 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4802 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4803 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4804 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4805 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004806 }
4807 }
4808
Craig Mautner4a1cb222013-12-04 16:14:06 -08004809 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4810 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004811 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004812 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004813 int mDisplayId;
4814 Display mDisplay;
4815 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004816
Craig Mautner4a1cb222013-12-04 16:14:06 -08004817 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4818 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004819 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004820
Jose Lima4b6c6692014-08-12 17:41:12 -07004821 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004822
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004823 ActivityDisplay() {
4824 }
Craig Mautner4504de52013-12-20 09:06:56 -08004825
Craig Mautner1a70a162014-09-13 12:09:31 -07004826 // After instantiation, check that mDisplay is not null before using this. The alternative
4827 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004828 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004829 final Display display = mDisplayManager.getDisplay(displayId);
4830 if (display == null) {
4831 return;
4832 }
4833 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004834 }
4835
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004836 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004837 mDisplay = display;
4838 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004839 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004840 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004841
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004842 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004843 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004844 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4845 + " onTop=" + onTop);
4846 if (onTop) {
4847 mStacks.add(stack);
4848 } else {
4849 mStacks.add(0, stack);
4850 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004851 }
4852
Craig Mautnere0a38842013-12-16 16:14:02 -08004853 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004854 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004855 + " from displayId=" + mDisplayId);
4856 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004857 }
4858
Jose Lima4b6c6692014-08-12 17:41:12 -07004859 void setVisibleBehindActivity(ActivityRecord r) {
4860 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004861 }
4862
Jose Lima4b6c6692014-08-12 17:41:12 -07004863 boolean hasVisibleBehindActivity() {
4864 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004865 }
4866
Craig Mautner34b73df2014-01-12 21:11:08 -08004867 @Override
4868 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004869 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4870 }
4871 }
4872
4873 class VirtualActivityDisplay extends ActivityDisplay {
4874 VirtualDisplay mVirtualDisplay;
4875
Craig Mautner6985bad2014-04-21 15:22:06 -07004876 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004877 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004878 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4879 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4880 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4881 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004882
4883 init(mVirtualDisplay.getDisplay());
4884
4885 mWindowManager.handleDisplayAdded(mDisplayId);
4886 }
4887
4888 void setSurface(Surface surface) {
4889 if (mVirtualDisplay != null) {
4890 mVirtualDisplay.setSurface(surface);
4891 }
4892 }
4893
4894 @Override
4895 void detachActivitiesLocked(ActivityStack stack) {
4896 super.detachActivitiesLocked(stack);
4897 if (mVirtualDisplay != null) {
4898 mVirtualDisplay.release();
4899 mVirtualDisplay = null;
4900 }
4901 }
4902
4903 @Override
4904 public String toString() {
4905 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004906 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004907 }
Jose Lima58e66d62014-05-27 20:00:27 -07004908
4909 private boolean isLeanbackOnlyDevice() {
4910 boolean onLeanbackOnly = false;
4911 try {
4912 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4913 PackageManager.FEATURE_LEANBACK_ONLY);
4914 } catch (RemoteException e) {
4915 // noop
4916 }
4917
4918 return onLeanbackOnly;
4919 }
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004920
4921 /**
4922 * Adjust bounds to stay within stack bounds.
4923 *
4924 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4925 * that keep them unchanged, but be contained within the stack bounds.
4926 *
4927 * @param bounds Bounds to be adjusted.
4928 * @param stackBounds Bounds within which the other bounds should remain.
4929 */
4930 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4931 if (stackBounds == null || stackBounds.contains(bounds)) {
4932 return;
4933 }
4934
4935 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4936 final int maxRight = stackBounds.right
4937 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4938 int horizontalDiff = stackBounds.left - bounds.left;
4939 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4940 || (bounds.left + horizontalDiff >= maxRight)) {
4941 horizontalDiff = maxRight - bounds.left;
4942 }
4943 bounds.left += horizontalDiff;
4944 bounds.right += horizontalDiff;
4945 }
4946
4947 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4948 final int maxBottom = stackBounds.bottom
4949 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4950 int verticalDiff = stackBounds.top - bounds.top;
4951 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4952 || (bounds.top + verticalDiff >= maxBottom)) {
4953 verticalDiff = maxBottom - bounds.top;
4954 }
4955 bounds.top += verticalDiff;
4956 bounds.bottom += verticalDiff;
4957 }
4958 }
4959
Craig Mautner27084302013-03-25 08:05:25 -07004960}