blob: 98b6ee6d2402ee294021a3420f80d8b5818dbd7a [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;
Craig Mautner4a1cb222013-12-04 16:14:06 -080069import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080070import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080071import android.hardware.display.DisplayManager;
72import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080073import android.hardware.display.DisplayManagerGlobal;
74import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080075import android.hardware.input.InputManager;
76import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080077import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070078import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070079import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070080import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070081import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070082import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070083import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070084import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070085import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070086import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070087import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070088import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040089import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070090import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070091import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070092import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070093import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070094import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070095import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040096import android.provider.Settings;
97import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070098import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070099import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700100import android.util.ArraySet;
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
Svetoslav7008b512015-06-24 18:47:07 -0700194 // Activity actions an app cannot start if it uses a permission which is not granted.
195 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
196 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700197
Svetoslav7008b512015-06-24 18:47:07 -0700198 static {
199 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
200 Manifest.permission.CAMERA);
201 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
202 Manifest.permission.CAMERA);
203 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
204 Manifest.permission.CALL_PHONE);
205 }
206
Svet Ganov99b60432015-06-27 13:15:22 -0700207 /** Action restriction: launching the activity is not restricted. */
208 private static final int ACTIVITY_RESTRICTION_NONE = 0;
209 /** Action restriction: launching the activity is restricted by a permission. */
210 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
211 /** Action restriction: launching the activity is restricted by an app op. */
212 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700213
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700214 // The height/width divide used when fitting a task within a bounds with method
215 // {@link #fitWithinBounds}.
216 // We always want the task to to be visible in the bounds without affecting its size when
217 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
218 // the input bounds right or bottom side minus the width or height divided by this value.
219 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
220
justinzhang5286d3f2014-05-12 17:06:01 -0400221 /** Status Bar Service **/
222 private IBinder mToken = new Binder();
223 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400224 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400225
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700226 // For debugging to make sure the caller when acquiring/releasing our
227 // wake lock is the system process.
228 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700229
Craig Mautner27084302013-03-25 08:05:25 -0700230 final ActivityManagerService mService;
231
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800232 private final RecentTasks mRecentTasks;
233
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700234 final ActivityStackSupervisorHandler mHandler;
235
236 /** Short cut */
237 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800238 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700239
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700240 /** Counter for next free stack ID to use for dynamic activity stacks. */
241 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700242
243 /** Task identifier that activities are currently being started in. Incremented each time a
244 * new task is created. */
245 private int mCurTaskId = 0;
246
Craig Mautner2420ead2013-04-01 17:13:20 -0700247 /** The current user */
248 private int mCurrentUser;
249
Craig Mautnere0a38842013-12-16 16:14:02 -0800250 /** The stack containing the launcher app. Assumed to always be attached to
251 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700252 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700253
Craig Mautnere0a38842013-12-16 16:14:02 -0800254 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700255 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700256
Craig Mautner4a1cb222013-12-04 16:14:06 -0800257 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
258 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800259 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800260 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700261
262 /** List of activities that are waiting for a new activity to become visible before completing
263 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800264 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700265
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700266 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800267 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700268
269 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800270 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700271
Craig Mautnerde4ef022013-04-07 19:01:33 -0700272 /** List of activities that are ready to be stopped, but waiting for the next activity to
273 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800274 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700275
Craig Mautnerf3333272013-04-22 10:55:53 -0700276 /** List of activities that are ready to be finished, but waiting for the previous activity to
277 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800278 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700279
Craig Mautner0eea92c2013-05-16 13:35:39 -0700280 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800281 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700282
Craig Mautnerf3333272013-04-22 10:55:53 -0700283 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700284 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700285
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700286 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700287 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700288
Craig Mautnerde4ef022013-04-07 19:01:33 -0700289 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
290 * is being brought in front of us. */
291 boolean mUserLeaving = false;
292
Craig Mautner0eea92c2013-05-16 13:35:39 -0700293 /** Set when we have taken too long waiting to go to sleep. */
294 boolean mSleepTimeout = false;
295
Jose Lima58e66d62014-05-27 20:00:27 -0700296 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
297 * setWindowManager is called. **/
298 private boolean mLeanbackOnlyDevice;
299
Craig Mautner0eea92c2013-05-16 13:35:39 -0700300 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700301 * We don't want to allow the device to go to sleep while in the process
302 * of launching an activity. This is primarily to allow alarm intent
303 * receivers to launch an activity and get that to run before the device
304 * goes back to sleep.
305 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700306 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700307
308 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700309 * Set when the system is going to sleep, until we have
310 * successfully paused the current activity and released our wake lock.
311 * At that point the system is allowed to actually sleep.
312 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700313 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700314
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700315 /** Stack id of the front stack when user switched, indexed by userId. */
316 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700317
Craig Mautner4504de52013-12-20 09:06:56 -0800318 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700320 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800321
322 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700323 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800324
Jeff Brownca9bc702014-02-11 14:32:56 -0800325 InputManagerInternal mInputManagerInternal;
326
Craig Mautner15df08a2015-04-01 12:17:18 -0700327 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
328 * may be finished until there is only one entry left. If this is empty the system is not
329 * in lockTask mode. */
330 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000331 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700332 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
333 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000334 */
335 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400336 /**
337 * Notifies the user when entering/exiting lock-task.
338 */
339 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800340
Craig Mautner15df08a2015-04-01 12:17:18 -0700341 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700342
Craig Mautner42d04db2014-11-06 12:13:23 -0800343 /** Used to keep resumeTopActivityLocked() from being entered recursively */
344 boolean inResumeTopActivity;
345
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700346 // 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 -0700347 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700348 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700349
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700350 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
351 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
352
Craig Mautneree36c772014-07-16 14:56:05 -0700353 /**
354 * Description of a request to start a new activity, which has been held
355 * due to app switches being disabled.
356 */
357 static class PendingActivityLaunch {
358 final ActivityRecord r;
359 final ActivityRecord sourceRecord;
360 final int startFlags;
361 final ActivityStack stack;
362
363 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
364 int _startFlags, ActivityStack _stack) {
365 r = _r;
366 sourceRecord = _sourceRecord;
367 startFlags = _startFlags;
368 stack = _stack;
369 }
370 }
371
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800372 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700373 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800374 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700375 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
376 }
377
378 /**
379 * At the time when the constructor runs, the power manager has not yet been
380 * initialized. So we initialize our wakelocks afterwards.
381 */
382 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800383 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700384 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700385 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700386 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700387 }
388
justinzhang5286d3f2014-05-12 17:06:01 -0400389 // This function returns a IStatusBarService. The value is from ServiceManager.
390 // getService and is cached.
391 private IStatusBarService getStatusBarService() {
392 synchronized (mService) {
393 if (mStatusBarService == null) {
394 mStatusBarService = IStatusBarService.Stub.asInterface(
395 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
396 if (mStatusBarService == null) {
397 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
398 }
399 }
400 return mStatusBarService;
401 }
402 }
403
Jason Monk35c62a42014-06-17 10:24:47 -0400404 private IDevicePolicyManager getDevicePolicyManager() {
405 synchronized (mService) {
406 if (mDevicePolicyManager == null) {
407 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
408 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
409 if (mDevicePolicyManager == null) {
410 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
411 }
412 }
413 return mDevicePolicyManager;
414 }
415 }
416
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700417 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800418 synchronized (mService) {
419 mWindowManager = wm;
420
421 mDisplayManager =
422 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
423 mDisplayManager.registerDisplayListener(this, null);
424
425 Display[] displays = mDisplayManager.getDisplays();
426 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
427 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800428 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700429 if (activityDisplay.mDisplay == null) {
430 throw new IllegalStateException("Default Display does not exist");
431 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800432 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800433 }
434
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700435 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800436 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800437
438 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700439
440 // Initialize this here, now that we can get a valid reference to PackageManager.
441 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800442 }
Craig Mautner27084302013-03-25 08:05:25 -0700443 }
444
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200445 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700446 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200447 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700448 }
449
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700450 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800451 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700452 }
453
Craig Mautnerde4ef022013-04-07 19:01:33 -0700454 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800455 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700456 }
457
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800458 /** Top of all visible stacks is/should always be equal to the focused stack.
459 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
460 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700461 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700462 if (stack == null) {
463 return false;
464 }
465
Craig Mautnerdf88d732014-01-27 09:21:32 -0800466 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
467 if (parent != null) {
468 stack = parent.task.stack;
469 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800470 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700471 }
472
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700473 void setFocusStack(String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800474 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800475 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800476 if (topNdx <= 0) {
477 return;
478 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700479
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700480 final ActivityStack topStack = stacks.get(topNdx);
481 mFocusedStack = topStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800482 if (lastFocusedStack != null) {
483 mLastFocusedStack = lastFocusedStack;
484 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800485
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700486 EventLogTags.writeAmFocusedStack(
487 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
488 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800489
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800490 if (mService.mBooting || !mService.mBooted) {
491 final ActivityRecord r = topRunningActivityLocked();
492 if (r != null && r.idle) {
493 checkFinishBootingLocked();
494 }
495 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700496 }
497
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700498 void moveHomeStackToFront(String reason) {
499 mHomeStack.moveToFront(reason);
500 }
501
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700502 /** Returns true if the focus activity was adjusted to the home stack top activity. */
503 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700504 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
505 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700506 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700507 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700508
Craig Mautner84984fa2014-06-19 11:19:20 -0700509 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700510
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700511 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700512 if (top == null) {
513 return false;
514 }
515 mService.setFocusedActivityLocked(top, reason);
516 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700517 }
518
Craig Mautner299f9602015-01-26 09:47:33 -0800519 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700520 if (!mService.mBooting && !mService.mBooted) {
521 // Not ready yet!
522 return false;
523 }
524
Craig Mautner84984fa2014-06-19 11:19:20 -0700525 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
526 mWindowManager.showRecentApps();
527 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700528 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700529
Craig Mautner84984fa2014-06-19 11:19:20 -0700530 if (prev != null) {
531 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
532 }
533
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700534 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
535 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700536 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800537 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700538 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700539 }
Craig Mautner299f9602015-01-26 09:47:33 -0800540 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700541 }
542
Craig Mautner8d341ef2013-03-26 09:03:27 -0700543 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700544 return anyTaskForIdLocked(id, true);
545 }
546
547 /**
548 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
549 * @param id Id of the task we would like returned.
550 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
551 * restore the task from recents to the active list.
552 */
553 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800554 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800555 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800556 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800557 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
558 ActivityStack stack = stacks.get(stackNdx);
559 TaskRecord task = stack.taskForIdLocked(id);
560 if (task != null) {
561 return task;
562 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700563 }
564 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800565
566 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700567 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800568 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800569 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700570 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800571 return null;
572 }
573
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700574 if (!restoreFromRecents) {
575 return task;
576 }
577
Chong Zhang5dcb2752015-08-18 13:50:26 -0700578 if (!restoreRecentTaskLocked(task, INVALID_STACK_ID)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700579 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
580 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800581 return null;
582 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800584 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700585 }
586
Craig Mautner6170f732013-04-02 13:05:23 -0700587 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800588 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800589 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800590 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
592 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
593 if (r != null) {
594 return r;
595 }
Craig Mautner6170f732013-04-02 13:05:23 -0700596 }
597 }
598 return null;
599 }
600
Craig Mautneref73ee12014-04-23 11:45:37 -0700601 void setNextTaskId(int taskId) {
602 if (taskId > mCurTaskId) {
603 mCurTaskId = taskId;
604 }
605 }
606
Craig Mautner8d341ef2013-03-26 09:03:27 -0700607 int getNextTaskId() {
608 do {
609 mCurTaskId++;
610 if (mCurTaskId <= 0) {
611 mCurTaskId = 1;
612 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700613 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700614 return mCurTaskId;
615 }
616
Craig Mautnerde4ef022013-04-07 19:01:33 -0700617 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800618 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700619 if (stack == null) {
620 return null;
621 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700622 ActivityRecord resumedActivity = stack.mResumedActivity;
623 if (resumedActivity == null || resumedActivity.app == null) {
624 resumedActivity = stack.mPausingActivity;
625 if (resumedActivity == null || resumedActivity.app == null) {
626 resumedActivity = stack.topRunningActivityLocked(null);
627 }
628 }
629 return resumedActivity;
630 }
631
Dianne Hackbornff072722014-09-24 10:56:28 -0700632 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700633 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800635 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
636 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800637 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
638 final ActivityStack stack = stacks.get(stackNdx);
639 if (!isFrontStack(stack)) {
640 continue;
641 }
642 ActivityRecord hr = stack.topRunningActivityLocked(null);
643 if (hr != null) {
644 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
645 && processName.equals(hr.processName)) {
646 try {
George Mount2c92c972014-03-20 09:38:23 -0700647 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800648 didSomething = true;
649 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700650 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651 Slog.w(TAG, "Exception in new application when starting activity "
652 + hr.intent.getComponent().flattenToShortString(), e);
653 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700654 }
Craig Mautner20e72272013-04-01 13:45:53 -0700655 }
Craig Mautner20e72272013-04-01 13:45:53 -0700656 }
657 }
658 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700659 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700660 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700661 }
Craig Mautner20e72272013-04-01 13:45:53 -0700662 return didSomething;
663 }
664
665 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800666 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
667 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
669 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800670 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 continue;
672 }
673 final ActivityRecord resumedActivity = stack.mResumedActivity;
674 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700675 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800676 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 return false;
678 }
Craig Mautner20e72272013-04-01 13:45:53 -0700679 }
680 }
681 return true;
682 }
683
Craig Mautnerde4ef022013-04-07 19:01:33 -0700684 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800685 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
686 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800687 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
688 final ActivityStack stack = stacks.get(stackNdx);
689 if (isFrontStack(stack)) {
690 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700691 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 return false;
693 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700694 }
695 }
696 }
697 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700698 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800699 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
700 mLastFocusedStack + " to=" + mFocusedStack);
701 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700702 return true;
703 }
704
705 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800706 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800707 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
708 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800709 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
710 final ActivityStack stack = stacks.get(stackNdx);
711 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800712 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700713 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800714 return false;
715 }
716 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800717 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700718 }
719 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800720 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700721 }
722
Craig Mautner2acc3892013-09-23 10:28:14 -0700723 /**
724 * Pause all activities in either all of the stacks or just the back stacks.
725 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700726 * @return true if any activity was paused as a result of this call.
727 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700728 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700729 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800730 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
731 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800732 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
733 final ActivityStack stack = stacks.get(stackNdx);
734 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700735 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800736 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700737 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
738 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800739 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700740 }
741 }
742 return someActivityPaused;
743 }
744
Craig Mautnerde4ef022013-04-07 19:01:33 -0700745 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700746 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800747 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
748 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800749 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
750 final ActivityStack stack = stacks.get(stackNdx);
751 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700752 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800753 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700754 Slog.d(TAG_STATES,
755 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 pausing = false;
757 } else {
758 return false;
759 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700760 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700761 }
762 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700763 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700764 }
765
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700766 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
767 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500768 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800769 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
770 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
771 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
772 final ActivityStack stack = stacks.get(stackNdx);
773 if (stack.mResumedActivity != null &&
774 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700775 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800776 }
777 }
778 }
779 }
780
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700781 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700782 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700783 }
784
785 void sendWaitingVisibleReportLocked(ActivityRecord r) {
786 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700787 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700788 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700789 if (w.who == null) {
790 changed = true;
791 w.timeout = false;
792 if (r != null) {
793 w.who = new ComponentName(r.info.packageName, r.info.name);
794 }
795 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
796 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700797 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700798 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700799 if (changed) {
800 mService.notifyAll();
801 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700802 }
803
804 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
805 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700806 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700807 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700808 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700809 if (w.who == null) {
810 changed = true;
811 w.timeout = timeout;
812 if (r != null) {
813 w.who = new ComponentName(r.info.packageName, r.info.name);
814 }
815 w.thisTime = thisTime;
816 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700817 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700818 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700819 if (changed) {
820 mService.notifyAll();
821 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700822 }
823
Craig Mautner29219d92013-04-16 20:19:12 -0700824 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800825 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700826 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
827 if (r != null) {
828 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700829 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700830
Craig Mautner4a1cb222013-12-04 16:14:06 -0800831 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800832 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
834 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700835 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700836 r = stack.topRunningActivityLocked(null);
837 if (r != null) {
838 return r;
839 }
840 }
841 }
842 return null;
843 }
844
Dianne Hackborn09233282014-04-30 11:33:59 -0700845 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700846 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800847 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
848 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800849 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800850 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800851 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800852 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
853 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700854 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800855 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700856 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700857 }
858 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700859
860 // The lists are already sorted from most recent to oldest. Just pull the most recent off
861 // each list and add it to list. Stop when all lists are empty or maxNum reached.
862 while (maxNum > 0) {
863 long mostRecentActiveTime = Long.MIN_VALUE;
864 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 final int numTaskLists = runningTaskLists.size();
866 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
867 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700868 if (!stackTaskList.isEmpty()) {
869 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
870 if (lastActiveTime > mostRecentActiveTime) {
871 mostRecentActiveTime = lastActiveTime;
872 selectedStackList = stackTaskList;
873 }
874 }
875 }
876 if (selectedStackList != null) {
877 list.add(selectedStackList.remove(0));
878 --maxNum;
879 } else {
880 break;
881 }
882 }
Craig Mautner20e72272013-04-01 13:45:53 -0700883 }
884
Craig Mautner23ac33b2013-04-01 16:26:35 -0700885 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700886 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700887 // Collect information about the target of the Intent.
888 ActivityInfo aInfo;
889 try {
890 ResolveInfo rInfo =
891 AppGlobals.getPackageManager().resolveIntent(
892 intent, resolvedType,
893 PackageManager.MATCH_DEFAULT_ONLY
894 | ActivityManagerService.STOCK_PM_FLAGS, userId);
895 aInfo = rInfo != null ? rInfo.activityInfo : null;
896 } catch (RemoteException e) {
897 aInfo = null;
898 }
899
900 if (aInfo != null) {
901 // Store the found target back into the intent, because now that
902 // we have it we never want to do this again. For example, if the
903 // user navigates back to this point in the history, we should
904 // always restart the exact same activity.
905 intent.setComponent(new ComponentName(
906 aInfo.applicationInfo.packageName, aInfo.name));
907
908 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700909 if (!aInfo.processName.equals("system")) {
910 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700911 mService.setDebugApp(aInfo.processName, true, false);
912 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700913
Man Caocfa78b22015-06-11 20:14:34 -0700914 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700915 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
916 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700917
Man Caocfa78b22015-06-11 20:14:34 -0700918 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
919 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
920 }
921
922 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700923 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924 }
925 }
926 }
927 return aInfo;
928 }
929
Craig Mautner299f9602015-01-26 09:47:33 -0800930 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
931 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700932 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
933 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
934 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
935 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700936 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
937 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700938 null /* outActivity */, null /* container */, null /* inTask */);
939 if (inResumeTopActivity) {
940 // If we are in resume section already, home activity will be initialized, but not
941 // resumed (to avoid recursive resume) and will stay that way until something pokes it
942 // again. We need to schedule another resume.
943 scheduleResumeTopActivities();
944 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700945 }
946
Craig Mautner23ac33b2013-04-01 16:26:35 -0700947 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700948 String callingPackage, Intent intent, String resolvedType,
949 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700950 IBinder resultTo, String resultWho, int requestCode, int startFlags,
951 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700952 Bundle options, boolean ignoreTargetSecurity, int userId,
953 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700954 // Refuse possible leaked file descriptors
955 if (intent != null && intent.hasFileDescriptors()) {
956 throw new IllegalArgumentException("File descriptors passed in Intent");
957 }
958 boolean componentSpecified = intent.getComponent() != null;
959
960 // Don't modify the client's object!
961 intent = new Intent(intent);
962
963 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700964 ActivityInfo aInfo =
965 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700966
Craig Mautnere0a38842013-12-16 16:14:02 -0800967 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700968 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800969 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700970 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800971 // Cannot start a child activity if the parent is not resumed.
972 return ActivityManager.START_CANCELED;
973 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700974 final int realCallingPid = Binder.getCallingPid();
975 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700976 int callingPid;
977 if (callingUid >= 0) {
978 callingPid = -1;
979 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700980 callingPid = realCallingPid;
981 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700982 } else {
983 callingPid = callingUid = -1;
984 }
985
Craig Mautnere0a38842013-12-16 16:14:02 -0800986 final ActivityStack stack;
987 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800988 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800989 } else {
990 stack = container.mStack;
991 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800992 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700993 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700994 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700995
996 final long origId = Binder.clearCallingIdentity();
997
998 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800999 (aInfo.applicationInfo.privateFlags
1000 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001001 // This may be a heavy-weight process! Check to see if we already
1002 // have another, different heavy-weight process running.
1003 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1004 if (mService.mHeavyWeightProcess != null &&
1005 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1006 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001007 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001008 if (caller != null) {
1009 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1010 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001011 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001012 } else {
1013 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001014 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001015 + intent.toString());
1016 ActivityOptions.abort(options);
1017 return ActivityManager.START_PERMISSION_DENIED;
1018 }
1019 }
1020
1021 IIntentSender target = mService.getIntentSenderLocked(
1022 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001023 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001024 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1025 | PendingIntent.FLAG_ONE_SHOT, null);
1026
1027 Intent newIntent = new Intent();
1028 if (requestCode >= 0) {
1029 // Caller is requesting a result.
1030 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1031 }
1032 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1033 new IntentSender(target));
1034 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1035 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1036 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1037 hist.packageName);
1038 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1039 hist.task.taskId);
1040 }
1041 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1042 aInfo.packageName);
1043 newIntent.setFlags(intent.getFlags());
1044 newIntent.setClassName("android",
1045 HeavyWeightSwitcherActivity.class.getName());
1046 intent = newIntent;
1047 resolvedType = null;
1048 caller = null;
1049 callingUid = Binder.getCallingUid();
1050 callingPid = Binder.getCallingPid();
1051 componentSpecified = true;
1052 try {
1053 ResolveInfo rInfo =
1054 AppGlobals.getPackageManager().resolveIntent(
1055 intent, null,
1056 PackageManager.MATCH_DEFAULT_ONLY
1057 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1058 aInfo = rInfo != null ? rInfo.activityInfo : null;
1059 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1060 } catch (RemoteException e) {
1061 aInfo = null;
1062 }
1063 }
1064 }
1065 }
1066
Dianne Hackborn91097de2014-04-04 18:02:06 -07001067 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1068 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001069 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001070 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001071 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001072
Craig Mautner85c11a82014-07-17 12:38:18 -07001073 Binder.restoreCallingIdentity(origId);
1074
Craig Mautnerde4ef022013-04-07 19:01:33 -07001075 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001076 // If the caller also wants to switch to a new configuration,
1077 // do so now. This allows a clean switch, as we are waiting
1078 // for the current activity to pause (so we will not destroy
1079 // it), and have not yet started the next activity.
1080 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1081 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001082 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001083 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001084 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001085 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001086 }
1087
Craig Mautner23ac33b2013-04-01 16:26:35 -07001088 if (outResult != null) {
1089 outResult.result = res;
1090 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001091 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001092 do {
1093 try {
1094 mService.wait();
1095 } catch (InterruptedException e) {
1096 }
1097 } while (!outResult.timeout && outResult.who == null);
1098 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001099 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001100 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001101 outResult.timeout = false;
1102 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1103 outResult.totalTime = 0;
1104 outResult.thisTime = 0;
1105 } else {
1106 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001107 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001108 do {
1109 try {
1110 mService.wait();
1111 } catch (InterruptedException e) {
1112 }
1113 } while (!outResult.timeout && outResult.who == null);
1114 }
1115 }
1116 }
1117
1118 return res;
1119 }
1120 }
1121
1122 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1123 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1124 Bundle options, int userId) {
1125 if (intents == null) {
1126 throw new NullPointerException("intents is null");
1127 }
1128 if (resolvedTypes == null) {
1129 throw new NullPointerException("resolvedTypes is null");
1130 }
1131 if (intents.length != resolvedTypes.length) {
1132 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1133 }
1134
Craig Mautner23ac33b2013-04-01 16:26:35 -07001135
1136 int callingPid;
1137 if (callingUid >= 0) {
1138 callingPid = -1;
1139 } else if (caller == null) {
1140 callingPid = Binder.getCallingPid();
1141 callingUid = Binder.getCallingUid();
1142 } else {
1143 callingPid = callingUid = -1;
1144 }
1145 final long origId = Binder.clearCallingIdentity();
1146 try {
1147 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001148 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001149 for (int i=0; i<intents.length; i++) {
1150 Intent intent = intents[i];
1151 if (intent == null) {
1152 continue;
1153 }
1154
1155 // Refuse possible leaked file descriptors
1156 if (intent != null && intent.hasFileDescriptors()) {
1157 throw new IllegalArgumentException("File descriptors passed in Intent");
1158 }
1159
1160 boolean componentSpecified = intent.getComponent() != null;
1161
1162 // Don't modify the client's object!
1163 intent = new Intent(intent);
1164
1165 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001166 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001167 // TODO: New, check if this is correct
1168 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1169
1170 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001171 (aInfo.applicationInfo.privateFlags
1172 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001173 throw new IllegalArgumentException(
1174 "FLAG_CANT_SAVE_STATE not supported here");
1175 }
1176
1177 Bundle theseOptions;
1178 if (options != null && i == intents.length-1) {
1179 theseOptions = options;
1180 } else {
1181 theseOptions = null;
1182 }
Craig Mautner6170f732013-04-02 13:05:23 -07001183 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001184 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1185 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001186 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001187 if (res < 0) {
1188 return res;
1189 }
1190
1191 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1192 }
1193 }
1194 } finally {
1195 Binder.restoreCallingIdentity(origId);
1196 }
1197
1198 return ActivityManager.START_SUCCESS;
1199 }
1200
Craig Mautner2420ead2013-04-01 17:13:20 -07001201 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001202 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001203 throws RemoteException {
1204
Craig Mautner2568c3a2015-03-26 14:22:34 -07001205 if (andResume) {
1206 r.startFreezingScreenLocked(app, 0);
1207 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001208
Craig Mautner2568c3a2015-03-26 14:22:34 -07001209 // schedule launch ticks to collect information about slow apps.
1210 r.startLaunchTickingLocked();
1211 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001212
1213 // Have the window manager re-evaluate the orientation of
1214 // the screen based on the new activity order. Note that
1215 // as a result of this, it can call back into the activity
1216 // manager with a new orientation. We don't care about that,
1217 // because the activity is not currently running so we are
1218 // just restarting it anyway.
1219 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001220 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001221 mService.mConfiguration,
1222 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001223 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001224 }
1225
1226 r.app = app;
1227 app.waitingToKill = null;
1228 r.launchCount++;
1229 r.lastLaunchTime = SystemClock.uptimeMillis();
1230
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001231 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001232
1233 int idx = app.activities.indexOf(r);
1234 if (idx < 0) {
1235 app.activities.add(r);
1236 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001237 mService.updateLruProcessLocked(app, true, null);
1238 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001239
Craig Mautner15df08a2015-04-01 12:17:18 -07001240 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001241 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1242 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001243 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001244 }
1245
1246 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 try {
1248 if (app.thread == null) {
1249 throw new RemoteException();
1250 }
1251 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001252 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001253 if (andResume) {
1254 results = r.results;
1255 newIntents = r.newIntents;
1256 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001257 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1258 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1259 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001260 if (andResume) {
1261 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1262 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001263 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001264 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001265 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001266 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001267 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001268 }
1269 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1270 r.sleeping = false;
1271 r.forceNewConfig = false;
1272 mService.showAskCompatModeDialogLocked(r);
1273 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001274 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1276 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1277 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001278 final String profileFile = mService.mProfileFile;
1279 if (profileFile != null) {
1280 ParcelFileDescriptor profileFd = mService.mProfileFd;
1281 if (profileFd != null) {
1282 try {
1283 profileFd = profileFd.dup();
1284 } catch (IOException e) {
1285 if (profileFd != null) {
1286 try {
1287 profileFd.close();
1288 } catch (IOException o) {
1289 }
1290 profileFd = null;
1291 }
1292 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001293 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001294
1295 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1296 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001297 }
1298 }
1299 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001300
Craig Mautner2568c3a2015-03-26 14:22:34 -07001301 if (andResume) {
1302 app.hasShownUi = true;
1303 app.pendingUiClean = true;
1304 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001305 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001306 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001307 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001308 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001309 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001310 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001311
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001312 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001313 // This may be a heavy-weight process! Note that the package
1314 // manager will ensure that only activity can run in the main
1315 // process of the .apk, which is the only thing that will be
1316 // considered heavy-weight.
1317 if (app.processName.equals(app.info.packageName)) {
1318 if (mService.mHeavyWeightProcess != null
1319 && mService.mHeavyWeightProcess != app) {
1320 Slog.w(TAG, "Starting new heavy weight process " + app
1321 + " when already running "
1322 + mService.mHeavyWeightProcess);
1323 }
1324 mService.mHeavyWeightProcess = app;
1325 Message msg = mService.mHandler.obtainMessage(
1326 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1327 msg.obj = r;
1328 mService.mHandler.sendMessage(msg);
1329 }
1330 }
1331
1332 } catch (RemoteException e) {
1333 if (r.launchFailed) {
1334 // This is the second time we failed -- finish activity
1335 // and give up.
1336 Slog.e(TAG, "Second failure launching "
1337 + r.intent.getComponent().flattenToShortString()
1338 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001339 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001340 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1341 "2nd-crash", false);
1342 return false;
1343 }
1344
1345 // This is the first time we failed -- restart process and
1346 // retry.
1347 app.activities.remove(r);
1348 throw e;
1349 }
1350
1351 r.launchFailed = false;
1352 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001353 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001354 }
1355
1356 if (andResume) {
1357 // As part of the process of launching, ActivityThread also performs
1358 // a resume.
1359 stack.minimalResumeActivityLocked(r);
1360 } else {
1361 // This activity is not starting in the resumed state... which
1362 // should look like we asked it to pause+stop (but remain visible),
1363 // and it has done so and reported back the current icicle and
1364 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001365 if (DEBUG_STATES) Slog.v(TAG_STATES,
1366 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001367 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001368 r.stopped = true;
1369 }
1370
1371 // Launch the new version setup screen if needed. We do this -after-
1372 // launching the initial activity (that is, home), so that it can have
1373 // a chance to initialize itself while in the background, making the
1374 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001375 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001376 mService.startSetupActivityLocked();
1377 }
1378
Dianne Hackborn465fa392014-09-14 14:21:18 -07001379 // Update any services we are bound to that might care about whether
1380 // their client may have activities.
1381 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1382
Craig Mautner2420ead2013-04-01 17:13:20 -07001383 return true;
1384 }
1385
Craig Mautnere79d42682013-04-01 19:01:53 -07001386 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001387 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001388 // Is this activity's application already running?
1389 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001390 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001391
1392 r.task.stack.setLaunchTime(r);
1393
1394 if (app != null && app.thread != null) {
1395 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001396 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1397 || !"android".equals(r.info.packageName)) {
1398 // Don't add this if it is a platform component that is marked
1399 // to run in multiple processes, because this is actually
1400 // part of the framework so doesn't make sense to track as a
1401 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001402 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1403 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001404 }
George Mount2c92c972014-03-20 09:38:23 -07001405 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001406 return;
1407 } catch (RemoteException e) {
1408 Slog.w(TAG, "Exception when starting activity "
1409 + r.intent.getComponent().flattenToShortString(), e);
1410 }
1411
1412 // If a dead object exception was thrown -- fall through to
1413 // restart the application.
1414 }
1415
1416 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001417 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001418 }
1419
Craig Mautner6170f732013-04-02 13:05:23 -07001420 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001421 Intent intent, String resolvedType, ActivityInfo aInfo,
1422 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1423 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001424 int callingPid, int callingUid, String callingPackage,
1425 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001426 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1427 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001428 int err = ActivityManager.START_SUCCESS;
1429
1430 ProcessRecord callerApp = null;
1431 if (caller != null) {
1432 callerApp = mService.getRecordForAppLocked(caller);
1433 if (callerApp != null) {
1434 callingPid = callerApp.pid;
1435 callingUid = callerApp.info.uid;
1436 } else {
1437 Slog.w(TAG, "Unable to find app for caller " + caller
1438 + " (pid=" + callingPid + ") when starting: "
1439 + intent.toString());
1440 err = ActivityManager.START_PERMISSION_DENIED;
1441 }
1442 }
1443
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001444 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1445
Craig Mautner6170f732013-04-02 13:05:23 -07001446 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001447 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001448 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001449 + " on display " + (container == null ? (mFocusedStack == null ?
1450 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1451 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1452 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001453 }
1454
1455 ActivityRecord sourceRecord = null;
1456 ActivityRecord resultRecord = null;
1457 if (resultTo != null) {
1458 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001459 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1460 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001461 if (sourceRecord != null) {
1462 if (requestCode >= 0 && !sourceRecord.finishing) {
1463 resultRecord = sourceRecord;
1464 }
1465 }
1466 }
Craig Mautner6170f732013-04-02 13:05:23 -07001467
Dianne Hackborn91097de2014-04-04 18:02:06 -07001468 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001469
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001470 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001471 // Transfer the result target from the source activity to the new
1472 // one being started, including any failures.
1473 if (requestCode >= 0) {
1474 ActivityOptions.abort(options);
1475 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1476 }
1477 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001478 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1479 resultRecord = null;
1480 }
Craig Mautner6170f732013-04-02 13:05:23 -07001481 resultWho = sourceRecord.resultWho;
1482 requestCode = sourceRecord.requestCode;
1483 sourceRecord.resultTo = null;
1484 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001485 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001486 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001487 if (sourceRecord.launchedFromUid == callingUid) {
1488 // The new activity is being launched from the same uid as the previous
1489 // activity in the flow, and asking to forward its result back to the
1490 // previous. In this case the activity is serving as a trampoline between
1491 // the two, so we also want to update its launchedFromPackage to be the
1492 // same as the previous activity. Note that this is safe, since we know
1493 // these two packages come from the same uid; the caller could just as
1494 // well have supplied that same package name itself. This specifially
1495 // deals with the case of an intent picker/chooser being launched in the app
1496 // flow to redirect to an activity picked by the user, where we want the final
1497 // activity to consider it to have been launched by the previous app activity.
1498 callingPackage = sourceRecord.launchedFromPackage;
1499 }
Craig Mautner6170f732013-04-02 13:05:23 -07001500 }
1501
1502 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1503 // We couldn't find a class that can handle the given Intent.
1504 // That's the end of that!
1505 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1506 }
1507
1508 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1509 // We couldn't find the specific class specified in the Intent.
1510 // Also the end of the line.
1511 err = ActivityManager.START_CLASS_NOT_FOUND;
1512 }
1513
Dianne Hackborn91097de2014-04-04 18:02:06 -07001514 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1515 && sourceRecord.task.voiceSession != null) {
1516 // If this activity is being launched as part of a voice session, we need
1517 // to ensure that it is safe to do so. If the upcoming activity will also
1518 // be part of the voice session, we can only launch it if it has explicitly
1519 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001520 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001521 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1522 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001523 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001524 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1525 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001526 Slog.w(TAG,
1527 "Activity being started in current voice task does not support voice: "
1528 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001529 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1530 }
1531 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001532 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001533 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1534 }
1535 }
1536 }
1537
1538 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1539 // If the caller is starting a new voice session, just make sure the target
1540 // is actually allowing it to run this way.
1541 try {
1542 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1543 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001544 Slog.w(TAG,
1545 "Activity being started in new voice task does not support: "
1546 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001547 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1548 }
1549 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001550 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001551 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1552 }
1553 }
1554
louis_changcd5d1982014-08-01 10:09:08 +08001555 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1556
Craig Mautner6170f732013-04-02 13:05:23 -07001557 if (err != ActivityManager.START_SUCCESS) {
1558 if (resultRecord != null) {
1559 resultStack.sendActivityResultLocked(-1,
1560 resultRecord, resultWho, requestCode,
1561 Activity.RESULT_CANCELED, null);
1562 }
Craig Mautner6170f732013-04-02 13:05:23 -07001563 ActivityOptions.abort(options);
1564 return err;
1565 }
1566
Svetoslav7008b512015-06-24 18:47:07 -07001567 boolean abort = false;
1568
Svet Ganov99b60432015-06-27 13:15:22 -07001569 final int startAnyPerm = mService.checkPermission(
1570 START_ANY_ACTIVITY, callingPid, callingUid);
1571
1572 if (startAnyPerm != PERMISSION_GRANTED) {
1573 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001574 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001575 final int actionRestriction = getActionRestrictionForCallingPackage(
1576 intent.getAction(), callingPackage, callingPid, callingUid);
1577
1578 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1579 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1580 if (resultRecord != null) {
1581 resultStack.sendActivityResultLocked(-1,
1582 resultRecord, resultWho, requestCode,
1583 Activity.RESULT_CANCELED, null);
1584 }
1585 String msg;
1586 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1587 msg = "Permission Denial: starting " + intent.toString()
1588 + " from " + callerApp + " (pid=" + callingPid
1589 + ", uid=" + callingUid + ")" + " with revoked permission "
1590 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1591 } else if (!aInfo.exported) {
1592 msg = "Permission Denial: starting " + intent.toString()
1593 + " from " + callerApp + " (pid=" + callingPid
1594 + ", uid=" + callingUid + ")"
1595 + " not exported from uid " + aInfo.applicationInfo.uid;
1596 } else {
1597 msg = "Permission Denial: starting " + intent.toString()
1598 + " from " + callerApp + " (pid=" + callingPid
1599 + ", uid=" + callingUid + ")"
1600 + " requires " + aInfo.permission;
1601 }
1602 Slog.w(TAG, msg);
1603 throw new SecurityException(msg);
1604 }
1605
1606 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1607 String message = "Appop Denial: starting " + intent.toString()
1608 + " from " + callerApp + " (pid=" + callingPid
1609 + ", uid=" + callingUid + ")"
1610 + " requires " + AppOpsManager.permissionToOp(
1611 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1612 Slog.w(TAG, message);
1613 abort = true;
1614 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1615 String message = "Appop Denial: starting " + intent.toString()
1616 + " from " + callerApp + " (pid=" + callingPid
1617 + ", uid=" + callingUid + ")"
1618 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1619 Slog.w(TAG, message);
1620 abort = true;
1621 }
Svetoslav7008b512015-06-24 18:47:07 -07001622 }
1623
1624 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001625 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001626
Craig Mautner6170f732013-04-02 13:05:23 -07001627 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001628 try {
1629 // The Intent we give to the watcher has the extra data
1630 // stripped off, since it can contain private information.
1631 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001632 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001633 aInfo.applicationInfo.packageName);
1634 } catch (RemoteException e) {
1635 mService.mController = null;
1636 }
Ben Gruver5e207332013-04-03 17:41:37 -07001637 }
Craig Mautner6170f732013-04-02 13:05:23 -07001638
Ben Gruver5e207332013-04-03 17:41:37 -07001639 if (abort) {
1640 if (resultRecord != null) {
1641 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001642 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001643 }
Ben Gruver5e207332013-04-03 17:41:37 -07001644 // We pretend to the caller that it was really started, but
1645 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001646 ActivityOptions.abort(options);
1647 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001648 }
1649
1650 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001651 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001652 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001653 if (outActivity != null) {
1654 outActivity[0] = r;
1655 }
1656
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001657 if (r.appTimeTracker == null && sourceRecord != null) {
1658 // If the caller didn't specify an explicit time tracker, we want to continue
1659 // tracking under any it has.
1660 r.appTimeTracker = sourceRecord.appTimeTracker;
1661 }
1662
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001663 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001664 if (voiceSession == null && (stack.mResumedActivity == null
1665 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001666 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1667 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001668 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001669 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001670 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001671 ActivityOptions.abort(options);
1672 return ActivityManager.START_SWITCHES_CANCELED;
1673 }
1674 }
1675
1676 if (mService.mDidAppSwitch) {
1677 // This is the second allowed switch since we stopped switches,
1678 // so now just generally allow switches. Use case: user presses
1679 // home (switches disabled, switch to home, mDidAppSwitch now true);
1680 // user taps a home icon (coming from home so allowed, we hit here
1681 // and now allow anyone to switch again).
1682 mService.mAppSwitchesAllowedTime = 0;
1683 } else {
1684 mService.mDidAppSwitch = true;
1685 }
1686
Craig Mautneree36c772014-07-16 14:56:05 -07001687 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001688
Dianne Hackborn91097de2014-04-04 18:02:06 -07001689 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001690 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001691
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001692 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001693 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001694 // activity start, but we are not actually doing an activity
1695 // switch... just dismiss the keyguard now, because we
1696 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001697 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001698 }
1699 return err;
1700 }
1701
Svet Ganov99b60432015-06-27 13:15:22 -07001702 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001703 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001704 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1705 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001706 == PackageManager.PERMISSION_DENIED) {
1707 return ACTIVITY_RESTRICTION_PERMISSION;
1708 }
1709
Christopher Tateff7add02015-08-17 10:23:22 -07001710 if (activityInfo.permission == null) {
1711 return ACTIVITY_RESTRICTION_NONE;
1712 }
1713
Svet Ganov99b60432015-06-27 13:15:22 -07001714 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1715 if (opCode == AppOpsManager.OP_NONE) {
1716 return ACTIVITY_RESTRICTION_NONE;
1717 }
1718
1719 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1720 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001721 if (!ignoreTargetSecurity) {
1722 return ACTIVITY_RESTRICTION_APPOP;
1723 }
Svet Ganov99b60432015-06-27 13:15:22 -07001724 }
1725
1726 return ACTIVITY_RESTRICTION_NONE;
1727 }
1728
Svetoslav7008b512015-06-24 18:47:07 -07001729 private int getActionRestrictionForCallingPackage(String action,
1730 String callingPackage, int callingPid, int callingUid) {
1731 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001732 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001733 }
1734
1735 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1736 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001737 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001738 }
1739
1740 final PackageInfo packageInfo;
1741 try {
1742 packageInfo = mService.mContext.getPackageManager()
1743 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1744 } catch (PackageManager.NameNotFoundException e) {
1745 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001746 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001747 }
1748
1749 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001750 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001751 }
1752
1753 if (mService.checkPermission(permission, callingPid, callingUid) ==
1754 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001755 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001756 }
1757
1758 final int opCode = AppOpsManager.permissionToOpCode(permission);
1759 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001760 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001761 }
1762
1763 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1764 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001765 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001766 }
1767
Svet Ganov99b60432015-06-27 13:15:22 -07001768 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001769 }
1770
Chong Zhang0fa656b2015-08-31 15:17:21 -07001771 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001772 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001773
1774 // On leanback only devices we should keep all activities in the same stack.
1775 if (!mLeanbackOnlyDevice &&
1776 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001777
1778 ActivityStack stack;
1779
Wale Ogunwale7d701172015-03-11 15:36:30 -07001780 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001781 stack = task.stack;
1782 if (stack.isOnHomeDisplay()) {
1783 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001784 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1785 "computeStackFocus: Setting " + "focused stack to r=" + r
1786 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001787 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001788 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001789 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001790 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001791 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001792 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001793 }
1794
Craig Mautnere0a38842013-12-16 16:14:02 -08001795 final ActivityContainer container = r.mInitialActivityContainer;
1796 if (container != null) {
1797 // The first time put it on the desired stack, after this put on task stack.
1798 r.mInitialActivityContainer = null;
1799 return container.mStack;
1800 }
1801
Wale Ogunwale13a9b162015-09-22 21:38:51 -07001802 // The fullscreen stack is the only stack that can contain any task regardless of if
1803 // the task is resizeable or not. So, we let the task go in the fullscreen task if it
1804 // is the focus stack.
1805 if (mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1806 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001807 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001808 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001809 return mFocusedStack;
1810 }
1811
Wale Ogunwale706ed792015-08-02 10:29:44 -07001812 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001813 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1814 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001815 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001816 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1817 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001818 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001819 "computeStackFocus: Setting focused stack=" + stack);
1820 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001821 }
1822 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001823
Wale Ogunwale706ed792015-08-02 10:29:44 -07001824 // If there is no suitable dynamic stack then we figure out which static stack to use.
Wale Ogunwale13a9b162015-09-22 21:38:51 -07001825 final int stackId = task != null ? task.getLaunchStackId() :
Chong Zhang0fa656b2015-08-31 15:17:21 -07001826 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1827 FULLSCREEN_WORKSPACE_STACK_ID;
1828 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001829 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1830 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001831 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001832 }
1833 return mHomeStack;
1834 }
1835
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001836 boolean setFocusedStack(ActivityRecord r, String reason) {
1837 if (r == null) {
1838 // Not sure what you are trying to do, but it is not going to work...
1839 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001840 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001841 final TaskRecord task = r.task;
1842 if (task == null || task.stack == null) {
1843 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1844 return false;
1845 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001846 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001847 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001848 }
1849
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001850 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001851 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001852 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001853 final Intent intent = r.intent;
1854 final int callingUid = r.launchedFromUid;
1855
Chong Zhang0fa656b2015-08-31 15:17:21 -07001856 boolean overrideBounds = false;
1857 Rect newBounds = null;
1858 if (r.info.resizeable || (inTask != null && inTask.mResizeable)) {
1859 if (intent.hasExtra(ActivityOptions.KEY_BOUNDS)) {
1860 overrideBounds = true;
1861 newBounds = Rect.unflattenFromString(
1862 intent.getStringExtra(ActivityOptions.KEY_BOUNDS));
1863 } else if (options != null) {
1864 ActivityOptions opts = new ActivityOptions(options);
1865 if (opts.hasBounds()) {
1866 overrideBounds = true;
1867 newBounds = opts.getBounds();
1868 }
1869 }
1870 }
1871
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001872 // In some flows in to this function, we retrieve the task record and hold on to it
1873 // without a lock before calling back in to here... so the task at this point may
1874 // not actually be in recents. Check for that, and if it isn't in recents just
1875 // consider it invalid.
1876 if (inTask != null && !inTask.inRecents) {
1877 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1878 inTask = null;
1879 }
1880
Craig Mautnerad400af2014-08-13 16:41:36 -07001881 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001882 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1883 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001884
Craig Mautnera228ae92014-07-09 05:44:55 -07001885 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001886 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001887 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001888 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1889 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1890 "\"singleInstance\" or \"singleTask\"");
1891 launchFlags &=
1892 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1893 } else {
1894 switch (r.info.documentLaunchMode) {
1895 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1896 break;
1897 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1898 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1899 break;
1900 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1901 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1902 break;
1903 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1904 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1905 break;
1906 }
1907 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001908
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001909 final boolean launchTaskBehind = r.mLaunchTaskBehind
1910 && !launchSingleTask && !launchSingleInstance
1911 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001912
Wale Ogunwale7d701172015-03-11 15:36:30 -07001913 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1914 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001915 // For whatever reason this activity is being launched into a new
1916 // task... yet the caller has requested a result back. Well, that
1917 // is pretty messed up, so instead immediately send back a cancel
1918 // and let the new task continue launched as normal without a
1919 // dependency on its originator.
1920 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1921 r.resultTo.task.stack.sendActivityResultLocked(-1,
1922 r.resultTo, r.resultWho, r.requestCode,
1923 Activity.RESULT_CANCELED, null);
1924 r.resultTo = null;
1925 }
1926
1927 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1928 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1929 }
1930
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001931 // If we are actually going to launch in to a new task, there are some cases where
1932 // we further want to do multiple task.
1933 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1934 if (launchTaskBehind
1935 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1936 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1937 }
1938 }
1939
Craig Mautner8849a5e2013-04-02 16:41:03 -07001940 // We'll invoke onUserLeaving before onPause only if the launching
1941 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001942 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001943 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1944 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001945
1946 // If the caller has asked not to resume at this point, we make note
1947 // of this in the record so that we can skip it when trying to find
1948 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001949 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001950 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001951 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001952 }
1953
Craig Mautnera254cd72014-05-25 16:47:39 -07001954 ActivityRecord notTop =
1955 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001956
1957 // If the onlyIfNeeded flag is set, then we can do this if the activity
1958 // being launched is the same as the one making the call... or, as
1959 // a special case, if we do not know the caller then we count the
1960 // current top activity as the caller.
1961 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1962 ActivityRecord checkedCaller = sourceRecord;
1963 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001964 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001965 }
1966 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1967 // Caller is not the same as launcher, so always needed.
1968 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1969 }
1970 }
1971
Craig Mautner8849a5e2013-04-02 16:41:03 -07001972 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001973 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001974
1975 // If the caller is not coming from another activity, but has given us an
1976 // explicit task into which they would like us to launch the new activity,
1977 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001978 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1979 final Intent baseIntent = inTask.getBaseIntent();
1980 final ActivityRecord root = inTask.getRootActivity();
1981 if (baseIntent == null) {
1982 ActivityOptions.abort(options);
1983 throw new IllegalArgumentException("Launching into task without base intent: "
1984 + inTask);
1985 }
1986
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001987 // If this task is empty, then we are adding the first activity -- it
1988 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001989 if (launchSingleInstance || launchSingleTask) {
1990 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1991 ActivityOptions.abort(options);
1992 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1993 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001994 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001995 if (root != null) {
1996 ActivityOptions.abort(options);
1997 throw new IllegalArgumentException("Caller with inTask " + inTask
1998 + " has root " + root + " but target is singleInstance/Task");
1999 }
2000 }
2001
2002 // If task is empty, then adopt the interesting intent launch flags in to the
2003 // activity being started.
2004 if (root == null) {
2005 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2006 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2007 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2008 launchFlags = (launchFlags&~flagsOfInterest)
2009 | (baseIntent.getFlags()&flagsOfInterest);
2010 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002011 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002012 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002013
Dianne Hackborn962d5352014-08-29 16:27:40 -07002014 // If the task is not empty and the caller is asking to start it as the root
2015 // of a new task, then we don't actually want to start this on the task. We
2016 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002017 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002018 addingToTask = false;
2019
2020 } else {
2021 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002022 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002023
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002024 reuseTask = inTask;
2025 } else {
2026 inTask = null;
2027 }
2028
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002029 if (inTask == null) {
2030 if (sourceRecord == null) {
2031 // This activity is not being started from another... in this
2032 // case we -always- start a new task.
2033 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2034 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2035 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2036 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2037 }
2038 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2039 // The original activity who is starting us is running as a single
2040 // instance... this new activity it is starting must go on its
2041 // own task.
2042 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2043 } else if (launchSingleInstance || launchSingleTask) {
2044 // The activity being started is a single instance... it always
2045 // gets launched into its own task.
2046 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2047 }
2048 }
2049
2050 ActivityInfo newTaskInfo = null;
2051 Intent newTaskIntent = null;
2052 ActivityStack sourceStack;
2053 if (sourceRecord != null) {
2054 if (sourceRecord.finishing) {
2055 // If the source is finishing, we can't further count it as our source. This
2056 // is because the task it is associated with may now be empty and on its way out,
2057 // so we don't want to blindly throw it in to that task. Instead we will take
2058 // the NEW_TASK flow and try to find a task for it. But save the task information
2059 // so it can be used when creating the new task.
2060 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2061 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2062 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2063 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2064 newTaskInfo = sourceRecord.info;
2065 newTaskIntent = sourceRecord.task.intent;
2066 }
2067 sourceRecord = null;
2068 sourceStack = null;
2069 } else {
2070 sourceStack = sourceRecord.task.stack;
2071 }
2072 } else {
2073 sourceStack = null;
2074 }
2075
2076 boolean movedHome = false;
2077 ActivityStack targetStack;
2078
2079 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002080 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002081
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002082 // We may want to try to place the new activity in to an existing task. We always
2083 // do this if the target activity is singleTask or singleInstance; we will also do
2084 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2085 // us to still place it in a new task: multi task, always doc mode, or being asked to
2086 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002087 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2088 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002089 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002090 // If bring to front is requested, and no result is requested and we have not
2091 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002092 // we can find a task that was started with this same
2093 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002094 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002095 // See if there is a task to bring to the front. If this is
2096 // a SINGLE_INSTANCE activity, there can be one and only one
2097 // instance of it in the history, and it is always in its own
2098 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002099 ActivityRecord intentActivity = !launchSingleInstance ?
2100 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002101 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002102 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2103 // but still needs to be a lock task mode violation since the task gets
2104 // cleared out and the device would otherwise leave the locked task.
2105 if (isLockTaskModeViolation(intentActivity.task,
2106 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2107 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002108 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002109 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002110 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2111 }
Craig Mautner29219d92013-04-16 20:19:12 -07002112 if (r.task == null) {
2113 r.task = intentActivity.task;
2114 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002115 if (intentActivity.task.intent == null) {
2116 // This task was started because of movement of
2117 // the activity based on affinity... now that we
2118 // are actually launching it, we can assign the
2119 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002120 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002122 targetStack = intentActivity.task.stack;
2123 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002124 // If the target task is not in the front, then we need
2125 // to bring it to the front... except... well, with
2126 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2127 // to have the same behavior as if a new instance was
2128 // being started, which means not bringing it to the front
2129 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002130 final ActivityStack focusStack = getFocusedStack();
2131 ActivityRecord curTop = (focusStack == null)
2132 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002133 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002134 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002135 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002136 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002137 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2138 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002139 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002140 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002141 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2142 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002143 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002144 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002145 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002146 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002147 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002148 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2149 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002150 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002151 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002152 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002153 options = null;
2154 }
2155 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002156 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002157 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002158 + " from " + intentActivity);
2159 targetStack.moveToFront("intentActivityFound");
2160 }
2161
Craig Mautner8849a5e2013-04-02 16:41:03 -07002162 // If the caller has requested that the target task be
2163 // reset, then do so.
2164 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2165 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2166 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002167 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002168 // We don't need to start a new activity, and
2169 // the client said not to do anything if that
2170 // is the case, so this is it! And for paranoia, make
2171 // sure we have correctly resumed the top activity.
2172 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002173 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002174
2175 // Make sure to notify Keyguard as well if we are not running an app
2176 // transition later.
2177 if (!movedToFront) {
2178 notifyActivityDrawnForKeyguard();
2179 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002180 } else {
2181 ActivityOptions.abort(options);
2182 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002183 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002184 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2185 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002186 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002187 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002188 // The caller has requested to completely replace any
2189 // existing task with its new activity. Well that should
2190 // not be too hard...
2191 reuseTask = intentActivity.task;
2192 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002193 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002194 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002195 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 // In this situation we want to remove all activities
2197 // from the task up to the one being started. In most
2198 // cases this means we are resetting the task to its
2199 // initial state.
2200 ActivityRecord top =
2201 intentActivity.task.performClearTaskLocked(r, launchFlags);
2202 if (top != null) {
2203 if (top.frontOfTask) {
2204 // Activity aliases may mean we use different
2205 // intents for the top activity, so make sure
2206 // the task now has the identity of the new
2207 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002208 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002209 }
2210 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2211 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002212 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002213 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002214 // A special case: we need to start the activity because it is not
2215 // currently running, and the caller has asked to clear the current
2216 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002217 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002218 // Now pretend like this activity is being started by the top of its
2219 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002220 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002221 TaskRecord task = sourceRecord.task;
2222 if (task != null && task.stack == null) {
2223 // Target stack got cleared when we all activities were removed
2224 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002225 targetStack = computeStackFocus(
2226 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002227 targetStack.addTask(
2228 task, !launchTaskBehind /* toTop */, false /* moving */);
2229 }
2230
Craig Mautner8849a5e2013-04-02 16:41:03 -07002231 }
2232 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2233 // In this case the top activity on the task is the
2234 // same as the one being launched, so we take that
2235 // as a request to bring the task to the foreground.
2236 // If the top activity in the task is the root
2237 // activity, deliver this new intent to it if it
2238 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002239 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002240 && intentActivity.realActivity.equals(r.realActivity)) {
2241 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2242 intentActivity.task);
2243 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002244 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002245 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002246 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2247 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002248 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2249 // In this case we are launching the root activity
2250 // of the task, but with a different intent. We
2251 // should start a new instance on top.
2252 addingToTask = true;
2253 sourceRecord = intentActivity;
2254 }
2255 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2256 // In this case an activity is being launched in to an
2257 // existing task, without resetting that task. This
2258 // is typically the situation of launching an activity
2259 // from a notification or shortcut. We want to place
2260 // the new activity on top of the current task.
2261 addingToTask = true;
2262 sourceRecord = intentActivity;
2263 } else if (!intentActivity.task.rootWasReset) {
2264 // In this case we are launching in to an existing task
2265 // that has not yet been started from its front door.
2266 // The current task has been brought to the front.
2267 // Ideally, we'd probably like to place this new task
2268 // at the bottom of its stack, but that's a little hard
2269 // to do with the current organization of the code so
2270 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002271 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002272 }
2273 if (!addingToTask && reuseTask == null) {
2274 // We didn't do anything... but it was needed (a.k.a., client
2275 // don't use that intent!) And for paranoia, make
2276 // sure we have correctly resumed the top activity.
2277 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002278 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002279 if (!movedToFront) {
2280 // Make sure to notify Keyguard as well if we are not running an app
2281 // transition later.
2282 notifyActivityDrawnForKeyguard();
2283 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002284 } else {
2285 ActivityOptions.abort(options);
2286 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002287 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002288 return ActivityManager.START_TASK_TO_FRONT;
2289 }
2290 }
2291 }
2292 }
2293
2294 //String uri = r.intent.toURI();
2295 //Intent intent2 = new Intent(uri);
2296 //Slog.i(TAG, "Given intent: " + r.intent);
2297 //Slog.i(TAG, "URI is: " + uri);
2298 //Slog.i(TAG, "To intent: " + intent2);
2299
2300 if (r.packageName != null) {
2301 // If the activity being launched is the same as the one currently
2302 // at the top, then we need to check if it should only be launched
2303 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002304 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002305 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002306 if (top != null && r.resultTo == null) {
2307 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2308 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002309 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002310 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002311 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2312 top.task);
2313 // For paranoia, make sure we have correctly
2314 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002315 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002316 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002317 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002318 }
2319 ActivityOptions.abort(options);
2320 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2321 // We don't need to start a new activity, and
2322 // the client said not to do anything if that
2323 // is the case, so this is it!
2324 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2325 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002326 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002327 return ActivityManager.START_DELIVERED_TO_TOP;
2328 }
2329 }
2330 }
2331 }
2332
2333 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002334 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002335 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2336 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002337 }
2338 ActivityOptions.abort(options);
2339 return ActivityManager.START_CLASS_NOT_FOUND;
2340 }
2341
2342 boolean newTask = false;
2343 boolean keepCurTransition = false;
2344
Craig Mautnerbb742462014-07-07 15:28:55 -07002345 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002346 sourceRecord.task : null;
2347
Craig Mautner8849a5e2013-04-02 16:41:03 -07002348 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002349 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002350 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002351 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002352 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002353 if (doResume) {
2354 targetStack.moveToFront("startingNewTask");
2355 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002356
Craig Mautner8849a5e2013-04-02 16:41:03 -07002357 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002358 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2359 newTaskInfo != null ? newTaskInfo : r.info,
2360 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002361 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2362 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002363 if (overrideBounds) {
2364 r.task.updateOverrideConfiguration(newBounds);
2365 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002366 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2367 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002368 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002369 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002370 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002371 if (isLockTaskModeViolation(r.task)) {
2372 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2373 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2374 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002375 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002376 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002377 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2378 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002379 // Caller wants to appear on home activity, so before starting
2380 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002381 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002382 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002383 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002384 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002385 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002386 if (isLockTaskModeViolation(sourceTask)) {
2387 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2388 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2389 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002390 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002391 if (doResume) {
2392 targetStack.moveToFront("sourceStackToFront");
2393 }
Craig Mautner737fae22014-10-15 12:52:10 -07002394 final TaskRecord topTask = targetStack.topTask();
2395 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002396 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002397 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002398 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002399 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002400 // In this case, we are adding the activity to an existing
2401 // task, but the caller has asked to clear that task if the
2402 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002403 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002404 keepCurTransition = true;
2405 if (top != null) {
2406 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002407 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002408 // For paranoia, make sure we have correctly
2409 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002410 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002411 if (doResume) {
2412 targetStack.resumeTopActivityLocked(null);
2413 }
2414 ActivityOptions.abort(options);
2415 return ActivityManager.START_DELIVERED_TO_TOP;
2416 }
2417 } else if (!addingToTask &&
2418 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2419 // In this case, we are launching an activity in our own task
2420 // that may already be running somewhere in the history, and
2421 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002422 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002423 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002424 final TaskRecord task = top.task;
2425 task.moveActivityToFrontLocked(top);
2426 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002427 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002428 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002429 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002430 if (doResume) {
2431 targetStack.resumeTopActivityLocked(null);
2432 }
2433 return ActivityManager.START_DELIVERED_TO_TOP;
2434 }
2435 }
2436 // An existing activity is starting this new activity, so we want
2437 // to keep the new one in the same task as the one that is starting
2438 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002439 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002440 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002441 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002442
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002443 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002444 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002445 // task it has provided to us.
2446 if (isLockTaskModeViolation(inTask)) {
2447 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2448 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2449 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002450 if (overrideBounds) {
2451 inTask.updateOverrideConfiguration(newBounds);
2452 int stackId = inTask.getLaunchStackId();
2453 if (stackId != inTask.stack.mStackId) {
2454 moveTaskToStackUncheckedLocked(
2455 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2456 }
2457 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002458 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002459 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2460 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002461
2462 // Check whether we should actually launch the new activity in to the task,
2463 // or just reuse the current activity on top.
2464 ActivityRecord top = inTask.getTopActivity();
2465 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2466 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2467 || launchSingleTop || launchSingleTask) {
2468 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2469 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2470 // We don't need to start a new activity, and
2471 // the client said not to do anything if that
2472 // is the case, so this is it!
2473 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2474 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002475 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002476 return ActivityManager.START_DELIVERED_TO_TOP;
2477 }
2478 }
2479
Dianne Hackborn962d5352014-08-29 16:27:40 -07002480 if (!addingToTask) {
2481 // We don't actually want to have this activity added to the task, so just
2482 // stop here but still tell the caller that we consumed the intent.
2483 ActivityOptions.abort(options);
2484 return ActivityManager.START_TASK_TO_FRONT;
2485 }
2486
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002487 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002488 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002489 + " in explicit task " + r.task);
2490
Craig Mautner8849a5e2013-04-02 16:41:03 -07002491 } else {
2492 // This not being started from an existing activity, and not part
2493 // of a new task... just put it in the top task, though these days
2494 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002495 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002496 if (doResume) {
2497 targetStack.moveToFront("addingToTopTask");
2498 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002499 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002500 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002501 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002502 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002503 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002504 + " in new guessed " + r.task);
2505 }
2506
2507 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002508 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002509
Craig Mautner76e2a762014-06-24 09:05:43 -07002510 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2511 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2512 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002513 if (newTask) {
2514 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2515 }
2516 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002517 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002518 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002519 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002520 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002521 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002522 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002523 return ActivityManager.START_SUCCESS;
2524 }
2525
Craig Mautneree36c772014-07-16 14:56:05 -07002526 final void doPendingActivityLaunchesLocked(boolean doResume) {
2527 while (!mPendingActivityLaunches.isEmpty()) {
2528 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002529 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002530 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002531 }
2532 }
2533
Craig Mautner7f13ed32014-07-28 14:00:35 -07002534 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002535 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2536 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002537 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002538 mPendingActivityLaunches.remove(palNdx);
2539 }
2540 }
2541 }
2542
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002543 void setLaunchSource(int uid) {
2544 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2545 }
2546
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002547 void acquireLaunchWakelock() {
2548 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2549 throw new IllegalStateException("Calling must be system uid");
2550 }
2551 mLaunchingActivity.acquire();
2552 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2553 // To be safe, don't allow the wake lock to be held for too long.
2554 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2555 }
2556 }
2557
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002558 /**
2559 * Called when the frontmost task is idle.
2560 * @return the state of mService.mBooting before this was called.
2561 */
2562 private boolean checkFinishBootingLocked() {
2563 final boolean booting = mService.mBooting;
2564 boolean enableScreen = false;
2565 mService.mBooting = false;
2566 if (!mService.mBooted) {
2567 mService.mBooted = true;
2568 enableScreen = true;
2569 }
2570 if (booting || enableScreen) {
2571 mService.postFinishBooting(booting, enableScreen);
2572 }
2573 return booting;
2574 }
2575
Craig Mautnerf3333272013-04-22 10:55:53 -07002576 // Checked.
2577 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2578 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002579 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002580
Craig Mautnerf3333272013-04-22 10:55:53 -07002581 ArrayList<ActivityRecord> stops = null;
2582 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002583 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002584 int NS = 0;
2585 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002586 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002587 boolean activityRemoved = false;
2588
Wale Ogunwale7d701172015-03-11 15:36:30 -07002589 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002590 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002591 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2592 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002593 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2594 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002595 if (fromTimeout) {
2596 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002597 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002598
2599 // This is a hack to semi-deal with a race condition
2600 // in the client where it can be constructed with a
2601 // newer configuration from when we asked it to launch.
2602 // We'll update with whatever configuration it now says
2603 // it used to launch.
2604 if (config != null) {
2605 r.configuration = config;
2606 }
2607
2608 // We are now idle. If someone is waiting for a thumbnail from
2609 // us, we can now deliver.
2610 r.idle = true;
2611
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002612 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002613 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002614 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002615 }
2616 }
2617
2618 if (allResumedActivitiesIdle()) {
2619 if (r != null) {
2620 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002621 }
2622
2623 if (mLaunchingActivity.isHeld()) {
2624 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2625 if (VALIDATE_WAKE_LOCK_CALLER &&
2626 Binder.getCallingUid() != Process.myUid()) {
2627 throw new IllegalStateException("Calling must be system uid");
2628 }
2629 mLaunchingActivity.release();
2630 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002631 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002632 }
2633
2634 // Atomically retrieve all of the other things to do.
2635 stops = processStoppingActivitiesLocked(true);
2636 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002637 if ((NF = mFinishingActivities.size()) > 0) {
2638 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002639 mFinishingActivities.clear();
2640 }
2641
Craig Mautnerf3333272013-04-22 10:55:53 -07002642 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002643 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002644 mStartingUsers.clear();
2645 }
2646
Craig Mautnerf3333272013-04-22 10:55:53 -07002647 // Stop any activities that are scheduled to do so but have been
2648 // waiting for the next one to start.
2649 for (int i = 0; i < NS; i++) {
2650 r = stops.get(i);
2651 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002652 if (stack != null) {
2653 if (r.finishing) {
2654 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2655 } else {
2656 stack.stopActivityLocked(r);
2657 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002658 }
2659 }
2660
2661 // Finish any activities that are scheduled to do so but have been
2662 // waiting for the next one to start.
2663 for (int i = 0; i < NF; i++) {
2664 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002665 final ActivityStack stack = r.task.stack;
2666 if (stack != null) {
2667 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2668 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002669 }
2670
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002671 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002672 // Complete user switch
2673 if (startingUsers != null) {
2674 for (int i = 0; i < startingUsers.size(); i++) {
2675 mService.finishUserSwitch(startingUsers.get(i));
2676 }
2677 }
2678 // Complete starting up of background users
2679 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002680 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002681 mStartingBackgroundUsers.clear();
2682 for (int i = 0; i < startingUsers.size(); i++) {
2683 mService.finishUserBoot(startingUsers.get(i));
2684 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002685 }
2686 }
2687
2688 mService.trimApplications();
2689 //dump();
2690 //mWindowManager.dump();
2691
Craig Mautnerf3333272013-04-22 10:55:53 -07002692 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002693 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002694 }
2695
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002696 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002697 }
2698
Craig Mautner8e569572013-10-11 17:36:59 -07002699 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002700 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002701 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2702 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002703 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2704 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2705 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002706 }
Craig Mautner19091252013-10-05 00:03:53 -07002707 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002708 }
2709
2710 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002711 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2712 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002713 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2714 stacks.get(stackNdx).closeSystemDialogsLocked();
2715 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002716 }
2717 }
2718
Craig Mautner93529a42013-10-04 15:03:13 -07002719 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002720 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002721 }
2722
Craig Mautner8d341ef2013-03-26 09:03:27 -07002723 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002724 * Update the last used stack id for non-current user (current user's last
2725 * used stack is the focused stack)
2726 */
2727 void updateUserStackLocked(int userId, ActivityStack stack) {
2728 if (userId != mCurrentUser) {
2729 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2730 }
2731 }
2732
2733 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002734 * @return true if some activity was finished (or would have finished if doit were true).
2735 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002736 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2737 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002738 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002739 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2740 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002741 final int numStacks = stacks.size();
2742 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2743 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002744 if (stack.finishDisabledPackageActivitiesLocked(
2745 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002746 didSomething = true;
2747 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002748 }
2749 }
2750 return didSomething;
2751 }
2752
Dianne Hackborna413dc02013-07-12 12:02:55 -07002753 void updatePreviousProcessLocked(ActivityRecord r) {
2754 // Now that this process has stopped, we may want to consider
2755 // it to be the previous app to try to keep around in case
2756 // the user wants to return to it.
2757
2758 // First, found out what is currently the foreground app, so that
2759 // we don't blow away the previous app if this activity is being
2760 // hosted by the process that is actually still the foreground.
2761 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002762 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2763 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002764 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2765 final ActivityStack stack = stacks.get(stackNdx);
2766 if (isFrontStack(stack)) {
2767 if (stack.mResumedActivity != null) {
2768 fgApp = stack.mResumedActivity.app;
2769 } else if (stack.mPausingActivity != null) {
2770 fgApp = stack.mPausingActivity.app;
2771 }
2772 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002773 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002774 }
2775 }
2776
2777 // Now set this one as the previous process, only if that really
2778 // makes sense to.
2779 if (r.app != null && fgApp != null && r.app != fgApp
2780 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002781 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002782 mService.mPreviousProcess = r.app;
2783 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2784 }
2785 }
2786
Craig Mautner05d29032013-05-03 13:40:13 -07002787 boolean resumeTopActivitiesLocked() {
2788 return resumeTopActivitiesLocked(null, null, null);
2789 }
2790
2791 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2792 Bundle targetOptions) {
2793 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002794 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002795 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002796 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002797 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002798 if (isFrontStack(targetStack)) {
2799 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2800 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002801
Craig Mautnere0a38842013-12-16 16:14:02 -08002802 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2803 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002804 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2805 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002806 if (stack == targetStack) {
2807 // Already started above.
2808 continue;
2809 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002810 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002811 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002812 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002813 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002814 }
Craig Mautner05d29032013-05-03 13:40:13 -07002815 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002816 }
2817
Todd Kennedy539db512014-12-15 09:57:55 -08002818 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
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 final int numStacks = stacks.size();
2822 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2823 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002824 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002825 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002826 }
2827 }
2828
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002829 void finishVoiceTask(IVoiceInteractionSession session) {
2830 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2831 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2832 final int numStacks = stacks.size();
2833 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2834 final ActivityStack stack = stacks.get(stackNdx);
2835 stack.finishVoiceTask(session);
2836 }
2837 }
2838 }
2839
Craig Mautner299f9602015-01-26 09:47:33 -08002840 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002841 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2842 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002843 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002844 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2845 // Caller wants the home activity moved with it. To accomplish this,
2846 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002847 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002848 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002849 if (task.stack == null) {
2850 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2851 + task + " to front. Stack is null");
2852 return;
2853 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002854
2855 int stackId = task.stack.mStackId;
2856 if (task.mResizeable && options != null) {
2857 ActivityOptions opts = new ActivityOptions(options);
2858 if (opts.hasBounds()) {
2859 Rect bounds = opts.getBounds();
2860 task.updateOverrideConfiguration(bounds);
Chong Zhang87b21722015-09-21 15:39:51 -07002861 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2862 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002863 stackId = task.getLaunchStackId();
2864 }
2865 }
2866
2867 if (stackId != task.stack.mStackId) {
2868 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, FORCE_FOCUS, reason);
2869 } else {
2870 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002871 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2872 reason);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002873 }
2874
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002875 if (DEBUG_STACK) Slog.d(TAG_STACK,
2876 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002877 }
2878
Craig Mautner967212c2013-04-13 21:10:58 -07002879 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002880 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002881 }
2882
2883 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002884 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2885 if (activityContainer != null) {
2886 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002887 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002888 if (!createStaticStackIfNeeded
2889 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2890 return null;
2891 }
2892 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002893 }
2894
Craig Mautner967212c2013-04-13 21:10:58 -07002895 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002896 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002897 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2898 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002899 }
2900 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002901 }
2902
Craig Mautner4a1cb222013-12-04 16:14:06 -08002903 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002904 ActivityRecord homeActivity = getHomeActivity();
2905 if (homeActivity != null) {
2906 return homeActivity.appToken;
2907 }
2908 return null;
2909 }
2910
2911 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002912 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002913 }
2914
2915 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002916 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2917 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2918 final TaskRecord task = tasks.get(taskNdx);
2919 if (task.isHomeTask()) {
2920 final ArrayList<ActivityRecord> activities = task.mActivities;
2921 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2922 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002923 if (r.isHomeActivity()
2924 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002925 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002926 }
2927 }
2928 }
2929 }
2930 return null;
2931 }
2932
Todd Kennedyca4d8422015-01-15 15:19:22 -08002933 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002934 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002935 ActivityContainer activityContainer =
2936 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002937 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002938 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2939 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002940 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002941 return activityContainer;
2942 }
2943
Craig Mautner34b73df2014-01-12 21:11:08 -08002944 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002945 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2946 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2947 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002948 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2949 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002950 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002951 }
2952 }
2953
Craig Mautner95da1082014-02-24 17:54:35 -08002954 void deleteActivityContainer(IActivityContainer container) {
2955 ActivityContainer activityContainer = (ActivityContainer)container;
2956 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002957 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2958 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002959 final int stackId = activityContainer.mStackId;
2960 mActivityContainers.remove(stackId);
2961 mWindowManager.removeStack(stackId);
2962 }
2963 }
2964
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002965 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002966 final ActivityStack stack = getStack(stackId);
2967 if (stack == null) {
2968 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2969 return;
2970 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002971
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002972 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
2973
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002974 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002975
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002976 mTmpBounds.clear();
2977 mTmpConfigs.clear();
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002978 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2979 for (int i = tasks.size() - 1; i >= 0; i--) {
2980 TaskRecord task = tasks.get(i);
2981 if (task.mResizeable) {
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07002982 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002983 // For freeform stack we don't adjust the size of the tasks to match that
2984 // of the stack, but we do try to make sure the tasks are still contained
2985 // with the bounds of the stack.
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07002986 tempRect2.set(task.mBounds);
2987 fitWithinBounds(tempRect2, bounds);
2988 task.updateOverrideConfiguration(tempRect2);
2989 } else {
2990 task.updateOverrideConfiguration(bounds);
2991 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002992 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002993
2994 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2995 mTmpBounds.put(task.taskId, task.mBounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002996 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07002997 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002998 if (stack.mStackId == DOCKED_STACK_ID) {
2999 // Dock stack funness...Yay!
3000 if (stack.mFullscreen) {
3001 // The dock stack went fullscreen which is kinda like dismissing it.
3002 // In this case we make all other static stacks fullscreen and move all
3003 // docked stack tasks to the fullscreen stack.
3004 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3005 if (i != DOCKED_STACK_ID) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003006 resizeStackLocked(i, null, preserveWindows);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003007 }
3008 }
3009
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003010 final int count = tasks.size();
3011 for (int i = 0; i < count; i++) {
3012 moveTaskToStackLocked(tasks.get(i).taskId,
3013 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
3014 }
3015
3016 // stack shouldn't contain anymore activities, so nothing to resume.
3017 r = null;
3018 } else {
3019 // Docked stacks occupy a dedicated region on screen so the size of all other
3020 // static stacks need to be adjusted so they don't overlap with the docked stack.
3021 final int leftChange = stack.mBounds.left - bounds.left;
3022 final int rightChange = stack.mBounds.right - bounds.right;
3023 final int topChange = stack.mBounds.top - bounds.top;
3024 final int bottomChange = stack.mBounds.bottom - bounds.bottom;
3025
3026 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3027 if (i != DOCKED_STACK_ID) {
3028 ActivityStack otherStack = getStack(i);
3029 if (otherStack != null) {
3030 tempRect.set(otherStack.mBounds);
3031 // We adjust the opposing sides of the other stacks to
3032 // the side in the dock stack that changed.
3033 tempRect.left -= rightChange;
3034 tempRect.right -= leftChange;
3035 tempRect.top -= bottomChange;
3036 tempRect.bottom -= topChange;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003037 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003038 }
3039 }
3040 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003041 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003042 // Since we are resizing the stack, all other operations should strive to preserve
3043 // windows.
3044 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003045 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003046 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003047
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003048 if (r != null) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003049 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003050 // And we need to make sure at this point that all other activities
3051 // are made visible with the correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003052 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003053 if (!updated) {
3054 resumeTopActivitiesLocked(stack, null, null);
3055 }
3056 }
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003057
3058 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003059 }
3060
Chong Zhang6de2ae82015-09-30 18:25:21 -07003061 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003062 if (!task.mResizeable) {
3063 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3064 return;
3065 }
3066
Chong Zhang87b21722015-09-21 15:39:51 -07003067 // If this is a forced resize, let it go through even if the bounds is not changing,
3068 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003069 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003070 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003071 // Nothing to do here...
3072 return;
3073 }
3074
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003075 if (!mWindowManager.isValidTaskId(task.taskId)) {
3076 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003077 // All we can do for now is update the bounds so it can be used when the task is
3078 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003079 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003080 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3081 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003082 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003083 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003084 return;
3085 }
3086
Chong Zhang6de2ae82015-09-30 18:25:21 -07003087 // Do not move the task to another stack here.
3088 // This method assumes that the task is already placed in the right stack.
3089 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003090
Chong Zhang6de2ae82015-09-30 18:25:21 -07003091 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003092
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003093 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003094 // This variable holds information whether the configuration didn't change in a signficant
3095 // way and the activity was kept the way it was. If it's false, it means the activity had
3096 // to be relaunched due to configuration change.
3097 boolean kept = true;
3098 if (overrideConfig != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003099 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003100 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003101 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003102 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003103 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003104 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003105 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003106 resumeTopActivitiesLocked(stack, null, null);
3107 }
3108 }
3109 }
Chong Zhang87b21722015-09-21 15:39:51 -07003110 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003111
3112 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003113 }
3114
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003115 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003116 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3117 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003118 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003119 }
3120
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003121 ActivityContainer activityContainer = new ActivityContainer(stackId);
3122 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003123 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003124 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003125 }
3126
3127 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003128 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003129 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3130 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003131 break;
3132 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003133 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003134 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003135 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003136 }
3137
Chong Zhang5dcb2752015-08-18 13:50:26 -07003138 /**
3139 * Restores a recent task to a stack
3140 * @param task The recent task to be restored.
3141 * @param stackId The stack to restore the task to (default launch stack will be used
3142 * if stackId is invalid).
3143 * @return true if the task has been restored successfully.
3144 */
3145 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3146 if (stackId == INVALID_STACK_ID) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07003147 stackId = mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003148 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003149 if (task.stack != null) {
3150 // Task has already been restored once. See if we need to do anything more
3151 if (task.stack.mStackId == stackId) {
3152 // Nothing else to do since it is already restored in the right stack.
3153 return true;
3154 }
3155 // Remove current stack association, so we can re-associate the task with the
3156 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003157 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003158 }
3159
3160 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003161 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003162
3163 if (stack == null) {
3164 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003165 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3166 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003167 return false;
3168 }
3169
3170 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003171 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3172 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003173 final ArrayList<ActivityRecord> activities = task.mActivities;
3174 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003175 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003176 }
3177 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003178 }
3179
Wale Ogunwale040b4702015-08-06 18:10:50 -07003180 /**
3181 * Moves the specified task record to the input stack id.
3182 * WARNING: This method performs an unchecked/raw move of the task and
3183 * can leave the system in an unstable state if used incorrectly.
3184 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3185 * to a stack.
3186 * @param task Task to move.
3187 * @param stackId Id of stack to move task to.
3188 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003189 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003190 * @param reason Reason the task is been moved.
3191 * @return The stack the task was moved to.
3192 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003193 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003194 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3195 final ActivityRecord r = task.getTopActivity();
3196 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3197 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3198
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003199 final boolean resizeable = task.mResizeable;
3200 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3201 // if a docked stack is created below which will lead to the stack we are moving from and
3202 // its resizeable tasks being resized.
3203 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003204 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003205 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003206 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3207 if (task.stack != null) {
3208 task.stack.removeTask(task, reason, MOVING);
3209 }
3210 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003211
3212 // If the task had focus before (or we're requested to move focus),
3213 // move focus to the new stack.
3214 if (forceFocus || wasFocused) {
3215 // If the task owns the last resumed activity, transfer that together,
3216 // so that we don't resume the same activity again in the new stack.
3217 // Apps may depend on onResume()/onPause() being called in pairs.
3218 if (wasResumed) {
3219 stack.mResumedActivity = r;
3220 }
3221 // move the stack in which we are placing the task to the front.
3222 stack.moveToFront(reason);
3223 }
3224
Wale Ogunwale040b4702015-08-06 18:10:50 -07003225 return stack;
3226 }
3227
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003228 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003229 final TaskRecord task = anyTaskForIdLocked(taskId);
3230 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003231 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003232 return;
3233 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003234 final String reason = "moveTaskToStack";
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003235 if (stackId == DOCKED_STACK_ID || stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
3236 // We are about to relaunch the activity because its configuration changed due to
3237 // being maximized, i.e. size change. The activity will first remove the old window
3238 // and then add a new one. This call will tell window manager about this, so it can
3239 // preserve the old window until the new one is drawn. This prevents having a gap
3240 // between the removal and addition, in which no window is visible. We also want the
3241 // entrace of the new window to be properly animated.
3242 ActivityRecord r = task.getTopActivity();
3243 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3244 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003245 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003246 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003247
3248 // Make sure the task has the appropriate bounds/size for the stack it is in.
3249 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003250 resizeTaskLocked(task, stack.mBounds,
3251 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003252 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3253 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003254 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3255 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003256 } else if (stackId == DOCKED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003257 resizeTaskLocked(task, stack.mBounds,
3258 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003259 }
3260
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003261 // The task might have already been running and its visibility needs to be synchronized with
3262 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003263 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003264 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003265 }
3266
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003267 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3268 final TaskRecord task = anyTaskForIdLocked(taskId);
3269 if (task == null) {
3270 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3271 return;
3272 }
3273 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003274 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003275 mWindowManager.positionTaskInStack(taskId, stackId, position);
3276 final boolean stackChanged = task.stack != null && task.stack != stack;
3277 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003278 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003279 }
3280 stack.positionTask(task, position, stackChanged);
3281 // The task might have already been running and its visibility needs to be synchronized with
3282 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003283 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003284 resumeTopActivitiesLocked();
3285 }
3286
Craig Mautnerac6f8432013-07-17 13:24:59 -07003287 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003288 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003289 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3290 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003291 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3292 final ActivityStack stack = stacks.get(stackNdx);
3293 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003294 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003295 continue;
3296 }
3297 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003298 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3299 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003300 continue;
3301 }
3302 final ActivityRecord ar = stack.findTaskLocked(r);
3303 if (ar != null) {
3304 return ar;
3305 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003306 }
3307 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003308 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003309 return null;
3310 }
3311
3312 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003313 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3314 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003315 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3316 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3317 if (ar != null) {
3318 return ar;
3319 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003320 }
3321 }
3322 return null;
3323 }
3324
Craig Mautner8d341ef2013-03-26 09:03:27 -07003325 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003326 scheduleSleepTimeout();
3327 if (!mGoingToSleep.isHeld()) {
3328 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003329 if (mLaunchingActivity.isHeld()) {
3330 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3331 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003332 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003333 mLaunchingActivity.release();
3334 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003335 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003336 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003337 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003338 }
3339
3340 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003341 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003342
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003343 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003344 final long endTime = System.currentTimeMillis() + timeout;
3345 while (true) {
3346 boolean cantShutdown = false;
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 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3351 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003352 }
3353 if (cantShutdown) {
3354 long timeRemaining = endTime - System.currentTimeMillis();
3355 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003356 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003357 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003358 } catch (InterruptedException e) {
3359 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003360 } else {
3361 Slog.w(TAG, "Activity manager shutdown timed out");
3362 timedout = true;
3363 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003364 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003365 } else {
3366 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003367 }
3368 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003369
3370 // Force checkReadyForSleep to complete.
3371 mSleepTimeout = true;
3372 checkReadyForSleepLocked();
3373
Craig Mautner8d341ef2013-03-26 09:03:27 -07003374 return timedout;
3375 }
3376
3377 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003378 removeSleepTimeouts();
3379 if (mGoingToSleep.isHeld()) {
3380 mGoingToSleep.release();
3381 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003382 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3383 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3385 final ActivityStack stack = stacks.get(stackNdx);
3386 stack.awakeFromSleepingLocked();
3387 if (isFrontStack(stack)) {
3388 resumeTopActivitiesLocked();
3389 }
Craig Mautner5314a402013-09-26 12:40:16 -07003390 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003391 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003392 mGoingToSleepActivities.clear();
3393 }
3394
3395 void activitySleptLocked(ActivityRecord r) {
3396 mGoingToSleepActivities.remove(r);
3397 checkReadyForSleepLocked();
3398 }
3399
3400 void checkReadyForSleepLocked() {
3401 if (!mService.isSleepingOrShuttingDown()) {
3402 // Do not care.
3403 return;
3404 }
3405
3406 if (!mSleepTimeout) {
3407 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003408 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3409 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003410 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3411 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3412 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003413 }
3414
3415 if (mStoppingActivities.size() > 0) {
3416 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003417 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003418 + mStoppingActivities.size() + " activities");
3419 scheduleIdleLocked();
3420 dontSleep = true;
3421 }
3422
3423 if (mGoingToSleepActivities.size() > 0) {
3424 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003425 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003426 + mGoingToSleepActivities.size() + " activities");
3427 dontSleep = true;
3428 }
3429
3430 if (dontSleep) {
3431 return;
3432 }
3433 }
3434
Craig Mautnere0a38842013-12-16 16:14:02 -08003435 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3436 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003437 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3438 stacks.get(stackNdx).goToSleep();
3439 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003440 }
3441
3442 removeSleepTimeouts();
3443
3444 if (mGoingToSleep.isHeld()) {
3445 mGoingToSleep.release();
3446 }
3447 if (mService.mShuttingDown) {
3448 mService.notifyAll();
3449 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003450 }
3451
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003452 boolean reportResumedActivityLocked(ActivityRecord r) {
3453 final ActivityStack stack = r.task.stack;
3454 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003455 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003456 }
3457 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003458 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003459 mWindowManager.executeAppTransition();
3460 return true;
3461 }
3462 return false;
3463 }
3464
Craig Mautner8d341ef2013-03-26 09:03:27 -07003465 void handleAppCrashLocked(ProcessRecord app) {
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;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003468 int stackNdx = stacks.size() - 1;
3469 while (stackNdx >= 0) {
3470 stacks.get(stackNdx).handleAppCrashLocked(app);
3471 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003472 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003473 }
3474 }
3475
Jose Lima4b6c6692014-08-12 17:41:12 -07003476 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003477 final ActivityStack stack = r.task.stack;
3478 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003479 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3480 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003481 return false;
3482 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003483 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003484 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3485 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003486
3487 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003488 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003489 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003490 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003491 return true;
3492 }
3493
3494 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003495 if (visible && top.fullscreen) {
3496 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003497 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3498 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3499 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3500 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003501 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003502 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3503 // Only the activity set as currently visible behind should actively reset its
3504 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003505 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3506 "requestVisibleBehind: returning visible=" + visible
3507 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3508 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003509 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003510 }
3511
Jose Lima4b6c6692014-08-12 17:41:12 -07003512 stack.setVisibleBehindActivity(visible ? r : null);
3513 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003514 // Make the activity immediately above r opaque.
3515 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3516 if (next != null) {
3517 mService.convertFromTranslucent(next.appToken);
3518 }
3519 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003520 if (top.app != null && top.app.thread != null) {
3521 // Notify the top app of the change.
3522 try {
3523 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3524 } catch (RemoteException e) {
3525 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003526 }
3527 return true;
3528 }
3529
Craig Mautnerbb742462014-07-07 15:28:55 -07003530 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3531 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3532 r.mLaunchTaskBehind = false;
3533 final TaskRecord task = r.task;
3534 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003535 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003536 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003537 mWindowManager.setAppVisibility(r.appToken, false);
3538 }
3539
3540 void scheduleLaunchTaskBehindComplete(IBinder token) {
3541 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3542 }
3543
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003544 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3545 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003546 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003547 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3548 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003549 final int topStackNdx = stacks.size() - 1;
3550 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3551 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003552 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003553 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003554 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003555 }
3556
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003557 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3558 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3559 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3560 final int topStackNdx = stacks.size() - 1;
3561 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3562 final ActivityStack stack = stacks.get(stackNdx);
3563 stack.clearOtherAppTimeTrackers(except);
3564 }
3565 }
3566 }
3567
Craig Mautner8d341ef2013-03-26 09:03:27 -07003568 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003569 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3570 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003571 final int numStacks = stacks.size();
3572 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3573 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003574 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003575 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003576 }
3577 }
3578
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003579 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3580 // Examine all activities currently running in the process.
3581 TaskRecord firstTask = null;
3582 // Tasks is non-null only if two or more tasks are found.
3583 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003584 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3585 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003586 ActivityRecord r = app.activities.get(i);
3587 // First, if we find an activity that is in the process of being destroyed,
3588 // then we just aren't going to do anything for now; we want things to settle
3589 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003590 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003591 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003592 return;
3593 }
3594 // Don't consider any activies that are currently not in a state where they
3595 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003596 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3597 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003598 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003599 continue;
3600 }
3601 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003602 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003603 + " from " + r);
3604 if (firstTask == null) {
3605 firstTask = r.task;
3606 } else if (firstTask != r.task) {
3607 if (tasks == null) {
3608 tasks = new ArraySet<>();
3609 tasks.add(firstTask);
3610 }
3611 tasks.add(r.task);
3612 }
3613 }
3614 }
3615 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003616 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003617 return;
3618 }
3619 // If we have activities in multiple tasks that are in a position to be destroyed,
3620 // let's iterate through the tasks and release the oldest one.
3621 final int numDisplays = mActivityDisplays.size();
3622 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3623 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3624 // Step through all stacks starting from behind, to hit the oldest things first.
3625 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3626 final ActivityStack stack = stacks.get(stackNdx);
3627 // Try to release activities in this stack; if we manage to, we are done.
3628 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3629 return;
3630 }
3631 }
3632 }
3633 }
3634
Amith Yamasani37a40c22015-06-17 13:25:42 -07003635 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003636 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003637 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003638 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003639
Craig Mautner858d8a62013-04-23 17:08:34 -07003640 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3642 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003643 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003644 final ActivityStack stack = stacks.get(stackNdx);
3645 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003646 TaskRecord task = stack.topTask();
3647 if (task != null) {
3648 mWindowManager.moveTaskToTop(task.taskId);
3649 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003650 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003651 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003652
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003653 ActivityStack stack = getStack(restoreStackId);
3654 if (stack == null) {
3655 stack = mHomeStack;
3656 }
3657 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003658 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003659 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003660 } else {
3661 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003662 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003663 }
Craig Mautner93529a42013-10-04 15:03:13 -07003664 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003665 }
3666
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003667 /**
3668 * Add background users to send boot completed events to.
3669 * @param userId The user being started in the background
3670 * @param uss The state object for the user.
3671 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003672 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003673 mStartingBackgroundUsers.add(uss);
3674 }
3675
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003676 /** Checks whether the userid is a profile of the current user. */
3677 boolean isCurrentProfileLocked(int userId) {
3678 if (userId == mCurrentUser) return true;
3679 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3680 if (mService.mCurrentProfileIds[i] == userId) return true;
3681 }
3682 return false;
3683 }
3684
Chong Zhang45c25ce2015-08-10 22:18:26 -07003685 /** Checks whether the activity should be shown for current user. */
3686 boolean okToShowLocked(ActivityRecord r) {
3687 return r != null && (isCurrentProfileLocked(r.userId)
3688 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3689 }
3690
Craig Mautnerde4ef022013-04-07 19:01:33 -07003691 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003692 ArrayList<ActivityRecord> stops = null;
3693
3694 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003695 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3696 ActivityRecord s = mStoppingActivities.get(activityNdx);
3697 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003698 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003699 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3700 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003701 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003702 if (s.finishing) {
3703 // If this activity is finishing, it is sitting on top of
3704 // everyone else but we now know it is no longer needed...
3705 // so get rid of it. Otherwise, we need to go through the
3706 // normal flow and hide it once we determine that it is
3707 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003708 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003709 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003710 }
3711 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003712 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003713 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003714 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003715 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003716 }
3717 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003718 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003719 }
3720 }
3721
3722 return stops;
3723 }
3724
Craig Mautnercf910b02013-04-23 11:23:27 -07003725 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003726 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3727 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3728 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3729 final ActivityStack stack = stacks.get(stackNdx);
3730 final ActivityRecord r = stack.topRunningActivityLocked(null);
3731 final ActivityState state = r == null ? DESTROYED : r.state;
3732 if (isFrontStack(stack)) {
3733 if (r == null) Slog.e(TAG,
3734 "validateTop...: null top activity, stack=" + stack);
3735 else {
3736 final ActivityRecord pausing = stack.mPausingActivity;
3737 if (pausing != null && pausing == r) Slog.e(TAG,
3738 "validateTop...: top stack has pausing activity r=" + r
3739 + " state=" + state);
3740 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3741 "validateTop...: activity in front not resumed r=" + r
3742 + " state=" + state);
3743 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003744 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003745 final ActivityRecord resumed = stack.mResumedActivity;
3746 if (resumed != null && resumed == r) Slog.e(TAG,
3747 "validateTop...: back stack has resumed activity r=" + r
3748 + " state=" + state);
3749 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3750 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003751 }
3752 }
3753 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003754 }
3755
Craig Mautnere0570202015-05-13 13:06:11 -07003756 private String lockTaskModeToString() {
3757 switch (mLockTaskModeState) {
3758 case LOCK_TASK_MODE_LOCKED:
3759 return "LOCKED";
3760 case LOCK_TASK_MODE_PINNED:
3761 return "PINNED";
3762 case LOCK_TASK_MODE_NONE:
3763 return "NONE";
3764 default: return "unknown=" + mLockTaskModeState;
3765 }
3766 }
3767
Craig Mautner27084302013-03-25 08:05:25 -07003768 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003769 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003770 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003771 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3772 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3773 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003774 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003775 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3776 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3777 if (packages.size() > 0) {
3778 pw.println(" mLockTaskPackages (userId:packages)=");
3779 for (int i = 0; i < packages.size(); ++i) {
3780 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3781 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3782 }
3783 }
3784 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003785 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003786
Craig Mautner20e72272013-04-01 13:45:53 -07003787 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003788 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003789 }
3790
Dianne Hackborn390517b2013-05-30 15:03:32 -07003791 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3792 boolean needSep, String prefix) {
3793 if (activity != null) {
3794 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3795 if (needSep) {
3796 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003797 }
3798 pw.print(prefix);
3799 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003800 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003801 }
3802 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003803 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003804 }
3805
Craig Mautner8d341ef2013-03-26 09:03:27 -07003806 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3807 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003808 boolean printed = false;
3809 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003810 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3811 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003812 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003813 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003814 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003815 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003816 final ActivityStack stack = stacks.get(stackNdx);
3817 StringBuilder stackHeader = new StringBuilder(128);
3818 stackHeader.append(" Stack #");
3819 stackHeader.append(stack.mStackId);
3820 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003821 stackHeader.append("\n");
3822 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3823 stackHeader.append("\n");
3824 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003825 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3826 needSep, stackHeader.toString());
3827 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3828 !dumpAll, false, dumpPackage, true,
3829 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003830
Craig Mautner4a1cb222013-12-04 16:14:06 -08003831 needSep = printed;
3832 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3833 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003834 if (pr) {
3835 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003836 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003837 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003838 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3839 " mResumedActivity: ");
3840 if (pr) {
3841 printed = true;
3842 needSep = false;
3843 }
3844 if (dumpAll) {
3845 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3846 " mLastPausedActivity: ");
3847 if (pr) {
3848 printed = true;
3849 needSep = true;
3850 }
3851 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3852 needSep, " mLastNoHistoryActivity: ");
3853 }
3854 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003855 }
3856 }
3857
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003858 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3859 false, dumpPackage, true, " Activities waiting to finish:", null);
3860 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3861 false, dumpPackage, true, " Activities waiting to stop:", null);
3862 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3863 false, dumpPackage, true, " Activities waiting for another to become visible:",
3864 null);
3865 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3866 false, dumpPackage, true, " Activities waiting to sleep:", null);
3867 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3868 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003869
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003870 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003871 }
3872
Dianne Hackborn390517b2013-05-30 15:03:32 -07003873 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003874 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003875 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003876 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003877 String innerPrefix = null;
3878 String[] args = null;
3879 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003880 for (int i=list.size()-1; i>=0; i--) {
3881 final ActivityRecord r = list.get(i);
3882 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3883 continue;
3884 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003885 if (innerPrefix == null) {
3886 innerPrefix = prefix + " ";
3887 args = new String[0];
3888 }
3889 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003890 final boolean full = !brief && (complete || !r.isInHistory());
3891 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003892 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003893 needNL = false;
3894 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003895 if (header1 != null) {
3896 pw.println(header1);
3897 header1 = null;
3898 }
3899 if (header2 != null) {
3900 pw.println(header2);
3901 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003902 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003903 if (lastTask != r.task) {
3904 lastTask = r.task;
3905 pw.print(prefix);
3906 pw.print(full ? "* " : " ");
3907 pw.println(lastTask);
3908 if (full) {
3909 lastTask.dump(pw, prefix + " ");
3910 } else if (complete) {
3911 // Complete + brief == give a summary. Isn't that obvious?!?
3912 if (lastTask.intent != null) {
3913 pw.print(prefix); pw.print(" ");
3914 pw.println(lastTask.intent.toInsecureStringWithClip());
3915 }
3916 }
3917 }
3918 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3919 pw.print(" #"); pw.print(i); pw.print(": ");
3920 pw.println(r);
3921 if (full) {
3922 r.dump(pw, innerPrefix);
3923 } else if (complete) {
3924 // Complete + brief == give a summary. Isn't that obvious?!?
3925 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3926 if (r.app != null) {
3927 pw.print(innerPrefix); pw.println(r.app);
3928 }
3929 }
3930 if (client && r.app != null && r.app.thread != null) {
3931 // flush anything that is already in the PrintWriter since the thread is going
3932 // to write to the file descriptor directly
3933 pw.flush();
3934 try {
3935 TransferPipe tp = new TransferPipe();
3936 try {
3937 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3938 r.appToken, innerPrefix, args);
3939 // Short timeout, since blocking here can
3940 // deadlock with the application.
3941 tp.go(fd, 2000);
3942 } finally {
3943 tp.kill();
3944 }
3945 } catch (IOException e) {
3946 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3947 } catch (RemoteException e) {
3948 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3949 }
3950 needNL = true;
3951 }
3952 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003953 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003954 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003955
Craig Mautnerf3333272013-04-22 10:55:53 -07003956 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003957 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3958 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003959 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3960 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003961 }
3962
3963 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003964 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003965 }
3966
3967 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003968 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3969 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003970 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3971 }
3972
Craig Mautner05d29032013-05-03 13:40:13 -07003973 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003974 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3975 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3976 }
Craig Mautner05d29032013-05-03 13:40:13 -07003977 }
3978
Craig Mautner0eea92c2013-05-16 13:35:39 -07003979 void removeSleepTimeouts() {
3980 mSleepTimeout = false;
3981 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3982 }
3983
3984 final void scheduleSleepTimeout() {
3985 removeSleepTimeouts();
3986 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3987 }
3988
Craig Mautner4a1cb222013-12-04 16:14:06 -08003989 @Override
3990 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003991 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003992 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3993 }
3994
3995 @Override
3996 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003997 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003998 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3999 }
4000
4001 @Override
4002 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004003 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004004 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4005 }
4006
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004007 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004008 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004009 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004010 newDisplay = mActivityDisplays.get(displayId) == null;
4011 if (newDisplay) {
4012 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004013 if (activityDisplay.mDisplay == null) {
4014 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4015 return;
4016 }
Craig Mautner4504de52013-12-20 09:06:56 -08004017 mActivityDisplays.put(displayId, activityDisplay);
4018 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004019 }
Craig Mautner4504de52013-12-20 09:06:56 -08004020 if (newDisplay) {
4021 mWindowManager.onDisplayAdded(displayId);
4022 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004023 }
4024
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004025 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004026 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004027 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4028 if (activityDisplay != null) {
4029 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004030 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004031 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004032 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004033 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004034 }
4035 }
4036 mWindowManager.onDisplayRemoved(displayId);
4037 }
4038
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004039 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004040 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004041 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4042 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004043 // TODO: Update the bounds.
4044 }
4045 }
4046 mWindowManager.onDisplayChanged(displayId);
4047 }
4048
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004049 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004050 StackInfo info = new StackInfo();
4051 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4052 info.displayId = Display.DEFAULT_DISPLAY;
4053 info.stackId = stack.mStackId;
4054
4055 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4056 final int numTasks = tasks.size();
4057 int[] taskIds = new int[numTasks];
4058 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004059 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004060 for (int i = 0; i < numTasks; ++i) {
4061 final TaskRecord task = tasks.get(i);
4062 taskIds[i] = task.taskId;
4063 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4064 : task.realActivity != null ? task.realActivity.flattenToString()
4065 : task.getTopActivity() != null ? task.getTopActivity().packageName
4066 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004067 taskBounds[i] = new Rect();
4068 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004069 }
4070 info.taskIds = taskIds;
4071 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004072 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004073 return info;
4074 }
4075
4076 StackInfo getStackInfoLocked(int stackId) {
4077 ActivityStack stack = getStack(stackId);
4078 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004079 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004080 }
4081 return null;
4082 }
4083
4084 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004085 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004086 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4087 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004088 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004089 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004090 }
4091 }
4092 return list;
4093 }
4094
Craig Mautner15df08a2015-04-01 12:17:18 -07004095 TaskRecord getLockedTaskLocked() {
4096 final int top = mLockTaskModeTasks.size() - 1;
4097 if (top >= 0) {
4098 return mLockTaskModeTasks.get(top);
4099 }
4100 return null;
4101 }
4102
4103 boolean isLockedTask(TaskRecord task) {
4104 return mLockTaskModeTasks.contains(task);
4105 }
4106
4107 boolean isLastLockedTask(TaskRecord task) {
4108 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4109 }
4110
4111 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004112 if (!mLockTaskModeTasks.remove(task)) {
4113 return;
4114 }
4115 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4116 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004117 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004118 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4119 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004120 final Message lockTaskMsg = Message.obtain();
4121 lockTaskMsg.arg1 = task.userId;
4122 lockTaskMsg.what = LOCK_TASK_END_MSG;
4123 mHandler.sendMessage(lockTaskMsg);
4124 }
4125 }
4126
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004127 void showLockTaskToast() {
4128 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004129 }
4130
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004131 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4132 if (mLockTaskModeTasks.contains(task)) {
4133 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4134 }
4135 }
4136
Craig Mautner432f64e2015-05-20 14:59:57 -07004137 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4138 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004139 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004140 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004141 final TaskRecord lockedTask = getLockedTaskLocked();
4142 if (lockedTask != null) {
4143 removeLockedTaskLocked(lockedTask);
4144 if (!mLockTaskModeTasks.isEmpty()) {
4145 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004146 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4147 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004148 lockedTask.performClearTaskLocked();
4149 resumeTopActivitiesLocked();
4150 return;
4151 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004152 }
Craig Mautnere0570202015-05-13 13:06:11 -07004153 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4154 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004155 return;
4156 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004157
4158 // Should have already been checked, but do it again.
4159 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004160 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4161 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004162 return;
4163 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004164 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004165 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004166 return;
4167 }
4168
4169 if (mLockTaskModeTasks.isEmpty()) {
4170 // First locktask.
4171 final Message lockTaskMsg = Message.obtain();
4172 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4173 lockTaskMsg.arg1 = task.userId;
4174 lockTaskMsg.what = LOCK_TASK_START_MSG;
4175 lockTaskMsg.arg2 = lockTaskModeState;
4176 mHandler.sendMessage(lockTaskMsg);
4177 }
4178 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004179 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4180 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004181 mLockTaskModeTasks.remove(task);
4182 mLockTaskModeTasks.add(task);
4183
4184 if (task.mLockTaskUid == -1) {
4185 task.mLockTaskUid = task.mCallingUid;
4186 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004187
4188 if (andResume) {
4189 findTaskToMoveToFrontLocked(task, 0, null, reason);
4190 resumeTopActivitiesLocked();
4191 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004192 }
4193
4194 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004195 return isLockTaskModeViolation(task, false);
4196 }
4197
4198 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4199 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004200 return false;
4201 }
4202 final int lockTaskAuth = task.mLockTaskAuth;
4203 switch (lockTaskAuth) {
4204 case LOCK_TASK_AUTH_DONT_LOCK:
4205 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004206 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004207 case LOCK_TASK_AUTH_LAUNCHABLE:
4208 case LOCK_TASK_AUTH_WHITELISTED:
4209 return false;
4210 case LOCK_TASK_AUTH_PINNABLE:
4211 // Pinnable tasks can't be launched on top of locktask tasks.
4212 return !mLockTaskModeTasks.isEmpty();
4213 default:
4214 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4215 return true;
4216 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004217 }
4218
Craig Mautner15df08a2015-04-01 12:17:18 -07004219 void onLockTaskPackagesUpdatedLocked() {
4220 boolean didSomething = false;
4221 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4222 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004223 final boolean wasWhitelisted =
4224 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4225 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004226 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004227 final boolean isWhitelisted =
4228 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4229 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4230 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004231 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004232 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4233 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004234 removeLockedTaskLocked(lockedTask);
4235 lockedTask.performClearTaskLocked();
4236 didSomething = true;
4237 }
4238 }
4239 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4240 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4241 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4242 final ActivityStack stack = stacks.get(stackNdx);
4243 stack.onLockTaskPackagesUpdatedLocked();
4244 }
4245 }
Craig Mautnere0570202015-05-13 13:06:11 -07004246 final ActivityRecord r = topRunningActivityLocked();
4247 final TaskRecord task = r != null ? r.task : null;
4248 if (mLockTaskModeTasks.isEmpty() && task != null
4249 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4250 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004251 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4252 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4253 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4254 false);
4255 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004256 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004257 if (didSomething) {
4258 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004259 }
4260 }
4261
Benjamin Franz43261142015-02-11 15:59:44 +00004262 int getLockTaskModeState() {
4263 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004264 }
4265
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004266 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004267
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004268 public ActivityStackSupervisorHandler(Looper looper) {
4269 super(looper);
4270 }
4271
Craig Mautnerf3333272013-04-22 10:55:53 -07004272 void activityIdleInternal(ActivityRecord r) {
4273 synchronized (mService) {
4274 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4275 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004276 }
4277
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004278 @Override
4279 public void handleMessage(Message msg) {
4280 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004281 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004282 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4283 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004284 if (mService.mDidDexOpt) {
4285 mService.mDidDexOpt = false;
4286 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4287 nmsg.obj = msg.obj;
4288 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4289 return;
4290 }
4291 // We don't at this point know if the activity is fullscreen,
4292 // so we need to be conservative and assume it isn't.
4293 activityIdleInternal((ActivityRecord)msg.obj);
4294 } break;
4295 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004296 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004297 activityIdleInternal((ActivityRecord)msg.obj);
4298 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004299 case RESUME_TOP_ACTIVITY_MSG: {
4300 synchronized (mService) {
4301 resumeTopActivitiesLocked();
4302 }
4303 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004304 case SLEEP_TIMEOUT_MSG: {
4305 synchronized (mService) {
4306 if (mService.isSleepingOrShuttingDown()) {
4307 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4308 mSleepTimeout = true;
4309 checkReadyForSleepLocked();
4310 }
4311 }
4312 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004313 case LAUNCH_TIMEOUT_MSG: {
4314 if (mService.mDidDexOpt) {
4315 mService.mDidDexOpt = false;
4316 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4317 return;
4318 }
4319 synchronized (mService) {
4320 if (mLaunchingActivity.isHeld()) {
4321 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4322 if (VALIDATE_WAKE_LOCK_CALLER
4323 && Binder.getCallingUid() != Process.myUid()) {
4324 throw new IllegalStateException("Calling must be system uid");
4325 }
4326 mLaunchingActivity.release();
4327 }
4328 }
4329 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004330 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004331 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004332 } break;
4333 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004334 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004335 } break;
4336 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004337 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004338 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004339 case CONTAINER_CALLBACK_VISIBILITY: {
4340 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004341 final IActivityContainerCallback callback = container.mCallback;
4342 if (callback != null) {
4343 try {
4344 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4345 } catch (RemoteException e) {
4346 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004347 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004348 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004349 case LOCK_TASK_START_MSG: {
4350 // When lock task starts, we disable the status bars.
4351 try {
Jason Monk62515be2014-05-21 16:06:19 -04004352 if (mLockTaskNotify == null) {
4353 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004354 }
Jason Monk62515be2014-05-21 16:06:19 -04004355 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004356 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004357 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004358 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004359 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004360 flags = StatusBarManager.DISABLE_MASK
4361 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004362 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004363 flags = StatusBarManager.DISABLE_MASK
4364 & (~StatusBarManager.DISABLE_BACK)
4365 & (~StatusBarManager.DISABLE_HOME)
4366 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004367 }
4368 getStatusBarService().disable(flags, mToken,
4369 mService.mContext.getPackageName());
4370 }
4371 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004372 if (getDevicePolicyManager() != null) {
4373 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004374 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004375 }
justinzhang5286d3f2014-05-12 17:06:01 -04004376 } catch (RemoteException ex) {
4377 throw new RuntimeException(ex);
4378 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004379 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004380 case LOCK_TASK_END_MSG: {
4381 // When lock task ends, we enable the status bars.
4382 try {
Jason Monk62515be2014-05-21 16:06:19 -04004383 if (getStatusBarService() != null) {
4384 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4385 mService.mContext.getPackageName());
4386 }
4387 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004388 if (getDevicePolicyManager() != null) {
4389 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4390 msg.arg1);
4391 }
Jason Monk62515be2014-05-21 16:06:19 -04004392 if (mLockTaskNotify == null) {
4393 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4394 }
4395 mLockTaskNotify.show(false);
4396 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004397 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004398 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004399 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004400 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004401 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004402 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004403 new LockPatternUtils(mService.mContext)
4404 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004405 }
4406 } catch (SettingNotFoundException e) {
4407 // No setting, don't lock.
4408 }
justinzhang5286d3f2014-05-12 17:06:01 -04004409 } catch (RemoteException ex) {
4410 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004411 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004412 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004413 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004414 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004415 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4416 if (mLockTaskNotify == null) {
4417 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4418 }
4419 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4420 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004421 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4422 final ActivityContainer container = (ActivityContainer) msg.obj;
4423 final IActivityContainerCallback callback = container.mCallback;
4424 if (callback != null) {
4425 try {
4426 callback.onAllActivitiesComplete(container.asBinder());
4427 } catch (RemoteException e) {
4428 }
4429 }
4430 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004431 case LAUNCH_TASK_BEHIND_COMPLETE: {
4432 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004433 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004434 if (r != null) {
4435 handleLaunchTaskBehindCompleteLocked(r);
4436 }
4437 }
4438 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004439 }
4440 }
4441 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004442
Ying Wangb081a592014-04-22 15:20:16 -07004443 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004444 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004445 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004446 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004447 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004448 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004449 ActivityRecord mParentActivity = null;
4450 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004451
Craig Mautnere3a00d72014-04-16 08:31:19 -07004452 boolean mVisible = true;
4453
Craig Mautner4a1cb222013-12-04 16:14:06 -08004454 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004455 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004456
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004457 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4458 final static int CONTAINER_STATE_NO_SURFACE = 1;
4459 final static int CONTAINER_STATE_FINISHING = 2;
4460 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4461
4462 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004463 synchronized (mService) {
4464 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004465 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004466 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004467 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004468 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004469 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004470
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004471 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004472 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004473 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004474 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004475 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004476 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004477 }
4478
4479 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004480 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004481 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004482 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4483 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004484 return;
4485 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004486 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004487 }
4488 }
4489
4490 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004491 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004492 synchronized (mService) {
4493 if (mActivityDisplay != null) {
4494 return mActivityDisplay.mDisplayId;
4495 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004496 }
4497 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004498 }
4499
Jeff Brownca9bc702014-02-11 14:32:56 -08004500 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004501 public int getStackId() {
4502 synchronized (mService) {
4503 return mStackId;
4504 }
4505 }
4506
4507 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004508 public boolean injectEvent(InputEvent event) {
4509 final long origId = Binder.clearCallingIdentity();
4510 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004511 synchronized (mService) {
4512 if (mActivityDisplay != null) {
4513 return mInputManagerInternal.injectInputEvent(event,
4514 mActivityDisplay.mDisplayId,
4515 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4516 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004517 }
4518 return false;
4519 } finally {
4520 Binder.restoreCallingIdentity(origId);
4521 }
4522 }
4523
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004524 @Override
4525 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004526 synchronized (mService) {
4527 if (mContainerState == CONTAINER_STATE_FINISHING) {
4528 return;
4529 }
4530 mContainerState = CONTAINER_STATE_FINISHING;
4531
Craig Mautnerd163e752014-06-13 17:18:47 -07004532 long origId = Binder.clearCallingIdentity();
4533 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004534 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004535 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004536 } finally {
4537 Binder.restoreCallingIdentity(origId);
4538 }
4539 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004540 }
4541
Craig Mautner60257702014-09-17 15:02:33 -07004542 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004543 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004544 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004545 if (mActivityDisplay != null) {
4546 mActivityDisplay.detachActivitiesLocked(mStack);
4547 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004548 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004549 }
4550 }
4551
4552 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004553 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004554 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004555 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004556 Binder.getCallingUid(), mCurrentUser, false,
4557 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004558
Craig Mautnere0a38842013-12-16 16:14:02 -08004559 // TODO: Switch to user app stacks here.
4560 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004561 final Uri data = intent.getData();
4562 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4563 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004564 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004565 checkEmbeddedAllowedInner(userId, intent, mimeType);
4566
4567 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4568 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004569 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004570 }
4571
4572 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004573 public final int startActivityIntentSender(IIntentSender intentSender)
4574 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004575 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4576
4577 if (!(intentSender instanceof PendingIntentRecord)) {
4578 throw new IllegalArgumentException("Bad PendingIntent object");
4579 }
4580
Craig Mautnerb9168362015-02-26 20:40:19 -08004581 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004582 Binder.getCallingUid(), mCurrentUser, false,
4583 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004584
4585 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4586 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4587 pendingIntent.key.requestResolvedType);
4588
4589 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4590 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4591 }
4592
4593 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004594 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004595 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004596 throw new SecurityException(
4597 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4598 }
4599 }
4600
Craig Mautnerdf88d732014-01-27 09:21:32 -08004601 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004602 public IBinder asBinder() {
4603 return this;
4604 }
4605
Craig Mautner4504de52013-12-20 09:06:56 -08004606 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004607 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004608 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004609 }
4610
Craig Mautner4a1cb222013-12-04 16:14:06 -08004611 ActivityStackSupervisor getOuter() {
4612 return ActivityStackSupervisor.this;
4613 }
4614
Craig Mautnerd163e752014-06-13 17:18:47 -07004615 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004616 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004617 }
4618
Craig Mautner6985bad2014-04-21 15:22:06 -07004619 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004620 void setVisible(boolean visible) {
4621 if (mVisible != visible) {
4622 mVisible = visible;
4623 if (mCallback != null) {
4624 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4625 0 /* unused */, this).sendToTarget();
4626 }
4627 }
4628 }
4629
Craig Mautner6985bad2014-04-21 15:22:06 -07004630 void setDrawn() {
4631 }
4632
Craig Mautner1b4bf852014-05-26 15:06:32 -07004633 // You can always start a new task on a regular ActivityStack.
4634 boolean isEligibleForNewTasks() {
4635 return true;
4636 }
4637
Craig Mautnerd163e752014-06-13 17:18:47 -07004638 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004639 detachLocked();
4640 deleteActivityContainer(this);
4641 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004642 }
4643
Craig Mautner34b73df2014-01-12 21:11:08 -08004644 @Override
4645 public String toString() {
4646 return mIdString + (mActivityDisplay == null ? "N" : "A");
4647 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004648 }
4649
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004650 private class VirtualActivityContainer extends ActivityContainer {
4651 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004652 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004653
4654 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4655 super(getNextStackId());
4656 mParentActivity = parent;
4657 mCallback = callback;
4658 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004659 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004660 }
4661
4662 @Override
4663 public void setSurface(Surface surface, int width, int height, int density) {
4664 super.setSurface(surface, width, height, density);
4665
4666 synchronized (mService) {
4667 final long origId = Binder.clearCallingIdentity();
4668 try {
4669 setSurfaceLocked(surface, width, height, density);
4670 } finally {
4671 Binder.restoreCallingIdentity(origId);
4672 }
4673 }
4674 }
4675
4676 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4677 if (mContainerState == CONTAINER_STATE_FINISHING) {
4678 return;
4679 }
4680 VirtualActivityDisplay virtualActivityDisplay =
4681 (VirtualActivityDisplay) mActivityDisplay;
4682 if (virtualActivityDisplay == null) {
4683 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004684 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004685 mActivityDisplay = virtualActivityDisplay;
4686 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004687 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004688 }
4689
4690 if (mSurface != null) {
4691 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004692 }
4693
Craig Mautner6985bad2014-04-21 15:22:06 -07004694 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004695 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004696 mStack.resumeTopActivityLocked(null);
4697 } else {
4698 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004699 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004700 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004701 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004702 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004703 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004704
Craig Mautnerd163e752014-06-13 17:18:47 -07004705 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004706
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004707 if (DEBUG_STACK) Slog.d(TAG_STACK,
4708 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004709 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004710
Craig Mautner6985bad2014-04-21 15:22:06 -07004711 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004712 boolean isAttachedLocked() {
4713 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004714 }
4715
4716 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004717 void setDrawn() {
4718 synchronized (mService) {
4719 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004720 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004721 }
4722 }
4723
Craig Mautner1b4bf852014-05-26 15:06:32 -07004724 // Never start a new task on an ActivityView if it isn't explicitly specified.
4725 @Override
4726 boolean isEligibleForNewTasks() {
4727 return false;
4728 }
4729
Craig Mautnerd163e752014-06-13 17:18:47 -07004730 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004731 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004732 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4733 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4734 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4735 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4736 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004737 }
4738 }
4739
Craig Mautner4a1cb222013-12-04 16:14:06 -08004740 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4741 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004742 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004743 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004744 int mDisplayId;
4745 Display mDisplay;
4746 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004747
Craig Mautner4a1cb222013-12-04 16:14:06 -08004748 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4749 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004750 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004751
Jose Lima4b6c6692014-08-12 17:41:12 -07004752 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004753
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004754 ActivityDisplay() {
4755 }
Craig Mautner4504de52013-12-20 09:06:56 -08004756
Craig Mautner1a70a162014-09-13 12:09:31 -07004757 // After instantiation, check that mDisplay is not null before using this. The alternative
4758 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004759 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004760 final Display display = mDisplayManager.getDisplay(displayId);
4761 if (display == null) {
4762 return;
4763 }
4764 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004765 }
4766
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004767 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004768 mDisplay = display;
4769 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004770 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004771 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004772
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004773 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004774 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004775 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4776 + " onTop=" + onTop);
4777 if (onTop) {
4778 mStacks.add(stack);
4779 } else {
4780 mStacks.add(0, stack);
4781 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004782 }
4783
Craig Mautnere0a38842013-12-16 16:14:02 -08004784 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004785 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004786 + " from displayId=" + mDisplayId);
4787 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004788 }
4789
Jose Lima4b6c6692014-08-12 17:41:12 -07004790 void setVisibleBehindActivity(ActivityRecord r) {
4791 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004792 }
4793
Jose Lima4b6c6692014-08-12 17:41:12 -07004794 boolean hasVisibleBehindActivity() {
4795 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004796 }
4797
Craig Mautner34b73df2014-01-12 21:11:08 -08004798 @Override
4799 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004800 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4801 }
4802 }
4803
4804 class VirtualActivityDisplay extends ActivityDisplay {
4805 VirtualDisplay mVirtualDisplay;
4806
Craig Mautner6985bad2014-04-21 15:22:06 -07004807 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004808 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004809 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4810 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4811 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4812 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004813
4814 init(mVirtualDisplay.getDisplay());
4815
4816 mWindowManager.handleDisplayAdded(mDisplayId);
4817 }
4818
4819 void setSurface(Surface surface) {
4820 if (mVirtualDisplay != null) {
4821 mVirtualDisplay.setSurface(surface);
4822 }
4823 }
4824
4825 @Override
4826 void detachActivitiesLocked(ActivityStack stack) {
4827 super.detachActivitiesLocked(stack);
4828 if (mVirtualDisplay != null) {
4829 mVirtualDisplay.release();
4830 mVirtualDisplay = null;
4831 }
4832 }
4833
4834 @Override
4835 public String toString() {
4836 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004837 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004838 }
Jose Lima58e66d62014-05-27 20:00:27 -07004839
4840 private boolean isLeanbackOnlyDevice() {
4841 boolean onLeanbackOnly = false;
4842 try {
4843 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4844 PackageManager.FEATURE_LEANBACK_ONLY);
4845 } catch (RemoteException e) {
4846 // noop
4847 }
4848
4849 return onLeanbackOnly;
4850 }
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004851
4852 /**
4853 * Adjust bounds to stay within stack bounds.
4854 *
4855 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4856 * that keep them unchanged, but be contained within the stack bounds.
4857 *
4858 * @param bounds Bounds to be adjusted.
4859 * @param stackBounds Bounds within which the other bounds should remain.
4860 */
4861 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4862 if (stackBounds == null || stackBounds.contains(bounds)) {
4863 return;
4864 }
4865
4866 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4867 final int maxRight = stackBounds.right
4868 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4869 int horizontalDiff = stackBounds.left - bounds.left;
4870 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4871 || (bounds.left + horizontalDiff >= maxRight)) {
4872 horizontalDiff = maxRight - bounds.left;
4873 }
4874 bounds.left += horizontalDiff;
4875 bounds.right += horizontalDiff;
4876 }
4877
4878 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4879 final int maxBottom = stackBounds.bottom
4880 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4881 int verticalDiff = stackBounds.top - bounds.top;
4882 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4883 || (bounds.top + verticalDiff >= maxBottom)) {
4884 verticalDiff = maxBottom - bounds.top;
4885 }
4886 bounds.top += verticalDiff;
4887 bounds.bottom += verticalDiff;
4888 }
4889 }
4890
Craig Mautner27084302013-03-25 08:05:25 -07004891}