blob: 4793d6af1485cd6a2e096f738f5fc0ceff2cb36b [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Wale Ogunwale868a5e12015-08-02 16:19:20 -070020import static android.app.ActivityManager.*;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070021import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
22import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070023import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070026import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070027import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080028import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070029import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070030import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
31import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
32import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070033import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070034import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
35import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070037import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
38import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070039
Svetoslav7008b512015-06-24 18:47:07 -070040import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070041import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080043import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.ActivityOptions;
45import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070046import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080047import android.app.IActivityContainer;
48import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070049import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070052import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070053import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070055import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040056import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040057import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070061import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070063import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070064import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070065import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070066import android.content.pm.PackageManager;
67import android.content.pm.ResolveInfo;
68import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080069import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.hardware.display.DisplayManager;
71import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.hardware.display.DisplayManagerGlobal;
73import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080074import android.hardware.input.InputManager;
75import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080076import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070079import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070080import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070085import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070086import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070087import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040088import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070090import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070091import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070092import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070093import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070094import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040095import android.provider.Settings;
96import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070097import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070098import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070099import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700100import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700101import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103
Craig Mautner4a1cb222013-12-04 16:14:06 -0800104import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800105import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800107import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800108import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700110import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800111import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700112import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400113import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700114import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400115import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800116import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700118import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119
justinzhang5286d3f2014-05-12 17:06:01 -0400120
Craig Mautner8d341ef2013-03-26 09:03:27 -0700121import java.io.FileDescriptor;
122import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700123import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700124import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700125import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700126import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700127import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700128
Craig Mautner4a1cb222013-12-04 16:14:06 -0800129public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800130 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700131 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700132 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700133 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700134 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700135 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700137 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700138 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
139 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700140 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700141 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700142 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
143 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700144 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700145 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800146
Craig Mautnerf3333272013-04-22 10:55:53 -0700147 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700149
Craig Mautner0eea92c2013-05-16 13:35:39 -0700150 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700152
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700153 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700155
Craig Mautner05d29032013-05-03 13:40:13 -0700156 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
157 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
158 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700159 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700160 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
162 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
163 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700164 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400165 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
166 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700167 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700168 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700169 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800170
Wale Ogunwale040b4702015-08-06 18:10:50 -0700171 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700172
Jason Monk62515be2014-05-21 16:06:19 -0400173 private static final String LOCK_TASK_TAG = "Lock-to-App";
174
Wale Ogunwale040b4702015-08-06 18:10:50 -0700175 // Used to indicate if an object (e.g. stack) that we are trying to get
176 // should be created if it doesn't exist already.
177 private static final boolean CREATE_IF_NEEDED = true;
178
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700179 // Used to indicate that windows of activities should be preserved during the resize.
180 static final boolean PRESERVE_WINDOWS = true;
181
Wale Ogunwale040b4702015-08-06 18:10:50 -0700182 // Used to indicate if an object (e.g. task) should be moved/created
183 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700184 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700185
186 // Used to indicate that an objects (e.g. task) removal from its container
187 // (e.g. stack) is due to it moving to another container.
188 static final boolean MOVING = true;
189
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700190 // Force the focus to change to the stack we are moving a task to..
191 static final boolean FORCE_FOCUS = true;
192
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700193 // Restore task from the saved recents if it can't be found in any live stack.
194 static final boolean RESTORE_FROM_RECENTS = true;
195
Svetoslav7008b512015-06-24 18:47:07 -0700196 // Activity actions an app cannot start if it uses a permission which is not granted.
197 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
198 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700199
Svetoslav7008b512015-06-24 18:47:07 -0700200 static {
201 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
202 Manifest.permission.CAMERA);
203 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
204 Manifest.permission.CAMERA);
205 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
206 Manifest.permission.CALL_PHONE);
207 }
208
Svet Ganov99b60432015-06-27 13:15:22 -0700209 /** Action restriction: launching the activity is not restricted. */
210 private static final int ACTIVITY_RESTRICTION_NONE = 0;
211 /** Action restriction: launching the activity is restricted by a permission. */
212 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
213 /** Action restriction: launching the activity is restricted by an app op. */
214 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700215
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700216 // The height/width divide used when fitting a task within a bounds with method
217 // {@link #fitWithinBounds}.
218 // We always want the task to to be visible in the bounds without affecting its size when
219 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
220 // the input bounds right or bottom side minus the width or height divided by this value.
221 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
222
justinzhang5286d3f2014-05-12 17:06:01 -0400223 /** Status Bar Service **/
224 private IBinder mToken = new Binder();
225 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400226 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400227
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700228 // For debugging to make sure the caller when acquiring/releasing our
229 // wake lock is the system process.
230 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700231
Craig Mautner27084302013-03-25 08:05:25 -0700232 final ActivityManagerService mService;
233
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800234 private final RecentTasks mRecentTasks;
235
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700236 final ActivityStackSupervisorHandler mHandler;
237
238 /** Short cut */
239 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700241
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700242 /** Counter for next free stack ID to use for dynamic activity stacks. */
243 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700244
245 /** Task identifier that activities are currently being started in. Incremented each time a
246 * new task is created. */
247 private int mCurTaskId = 0;
248
Craig Mautner2420ead2013-04-01 17:13:20 -0700249 /** The current user */
250 private int mCurrentUser;
251
Craig Mautnere0a38842013-12-16 16:14:02 -0800252 /** The stack containing the launcher app. Assumed to always be attached to
253 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700254 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700255
Craig Mautnere0a38842013-12-16 16:14:02 -0800256 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700257 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700258
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
260 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800261 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800262 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700263
264 /** List of activities that are waiting for a new activity to become visible before completing
265 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800266 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700267
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700268 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800269 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700270
271 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800272 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700273
Craig Mautnerde4ef022013-04-07 19:01:33 -0700274 /** List of activities that are ready to be stopped, but waiting for the next activity to
275 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800276 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700277
Craig Mautnerf3333272013-04-22 10:55:53 -0700278 /** List of activities that are ready to be finished, but waiting for the previous activity to
279 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800280 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700281
Craig Mautner0eea92c2013-05-16 13:35:39 -0700282 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800283 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700284
Craig Mautnerf3333272013-04-22 10:55:53 -0700285 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700286 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700287
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700288 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700289 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700290
Craig Mautnerde4ef022013-04-07 19:01:33 -0700291 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
292 * is being brought in front of us. */
293 boolean mUserLeaving = false;
294
Craig Mautner0eea92c2013-05-16 13:35:39 -0700295 /** Set when we have taken too long waiting to go to sleep. */
296 boolean mSleepTimeout = false;
297
298 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700299 * We don't want to allow the device to go to sleep while in the process
300 * of launching an activity. This is primarily to allow alarm intent
301 * receivers to launch an activity and get that to run before the device
302 * goes back to sleep.
303 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700304 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700305
306 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700307 * Set when the system is going to sleep, until we have
308 * successfully paused the current activity and released our wake lock.
309 * At that point the system is allowed to actually sleep.
310 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700311 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700312
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700313 /** Stack id of the front stack when user switched, indexed by userId. */
314 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700315
Craig Mautner4504de52013-12-20 09:06:56 -0800316 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700318 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319
320 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700321 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322
Jeff Brownca9bc702014-02-11 14:32:56 -0800323 InputManagerInternal mInputManagerInternal;
324
Craig Mautner15df08a2015-04-01 12:17:18 -0700325 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
326 * may be finished until there is only one entry left. If this is empty the system is not
327 * in lockTask mode. */
328 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000329 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700330 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
331 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000332 */
333 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400334 /**
335 * Notifies the user when entering/exiting lock-task.
336 */
337 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800338
Craig Mautner15df08a2015-04-01 12:17:18 -0700339 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700340
Craig Mautner42d04db2014-11-06 12:13:23 -0800341 /** Used to keep resumeTopActivityLocked() from being entered recursively */
342 boolean inResumeTopActivity;
343
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700344 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700345 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700346 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700347
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700348 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
349 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
350
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700351 // The default minimal size that will be used if the activity doesn't specify its minimal size.
352 // It will be calculated when the default display gets added.
353 private int mDefaultMinimalSizeOfResizeableTask = -1;
354
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700355 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
356 private boolean mTaskLayersChanged = true;
357
Craig Mautneree36c772014-07-16 14:56:05 -0700358 /**
359 * Description of a request to start a new activity, which has been held
360 * due to app switches being disabled.
361 */
362 static class PendingActivityLaunch {
363 final ActivityRecord r;
364 final ActivityRecord sourceRecord;
365 final int startFlags;
366 final ActivityStack stack;
367
368 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
369 int _startFlags, ActivityStack _stack) {
370 r = _r;
371 sourceRecord = _sourceRecord;
372 startFlags = _startFlags;
373 stack = _stack;
374 }
375 }
376
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800377 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700378 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800379 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700380 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
381 }
382
383 /**
384 * At the time when the constructor runs, the power manager has not yet been
385 * initialized. So we initialize our wakelocks afterwards.
386 */
387 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800388 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700389 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700390 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700391 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700392 }
393
justinzhang5286d3f2014-05-12 17:06:01 -0400394 // This function returns a IStatusBarService. The value is from ServiceManager.
395 // getService and is cached.
396 private IStatusBarService getStatusBarService() {
397 synchronized (mService) {
398 if (mStatusBarService == null) {
399 mStatusBarService = IStatusBarService.Stub.asInterface(
400 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
401 if (mStatusBarService == null) {
402 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
403 }
404 }
405 return mStatusBarService;
406 }
407 }
408
Jason Monk35c62a42014-06-17 10:24:47 -0400409 private IDevicePolicyManager getDevicePolicyManager() {
410 synchronized (mService) {
411 if (mDevicePolicyManager == null) {
412 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
413 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
414 if (mDevicePolicyManager == null) {
415 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
416 }
417 }
418 return mDevicePolicyManager;
419 }
420 }
421
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700422 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800423 synchronized (mService) {
424 mWindowManager = wm;
425
426 mDisplayManager =
427 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
428 mDisplayManager.registerDisplayListener(this, null);
429
430 Display[] displays = mDisplayManager.getDisplays();
431 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
432 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800433 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700434 if (activityDisplay.mDisplay == null) {
435 throw new IllegalStateException("Default Display does not exist");
436 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800437 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700438 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800439 }
440
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700441 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800442 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800443
444 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800445 }
Craig Mautner27084302013-03-25 08:05:25 -0700446 }
447
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200448 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700449 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200450 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700451 }
452
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700453 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800454 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700455 }
456
Craig Mautnerde4ef022013-04-07 19:01:33 -0700457 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800458 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700459 }
460
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800461 /** Top of all visible stacks is/should always be equal to the focused stack.
462 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
463 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700464 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700465 if (stack == null) {
466 return false;
467 }
468
Craig Mautnerdf88d732014-01-27 09:21:32 -0800469 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
470 if (parent != null) {
471 stack = parent.task.stack;
472 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800473 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700474 }
475
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700476 void setFocusStack(String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800477 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800478 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800479 if (topNdx <= 0) {
480 return;
481 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700482
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700483 final ActivityStack topStack = stacks.get(topNdx);
484 mFocusedStack = topStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800485 if (lastFocusedStack != null) {
486 mLastFocusedStack = lastFocusedStack;
487 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800488
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700489 EventLogTags.writeAmFocusedStack(
490 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
491 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800492
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800493 if (mService.mBooting || !mService.mBooted) {
494 final ActivityRecord r = topRunningActivityLocked();
495 if (r != null && r.idle) {
496 checkFinishBootingLocked();
497 }
498 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700499 }
500
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700501 void moveHomeStackToFront(String reason) {
502 mHomeStack.moveToFront(reason);
503 }
504
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700505 /** Returns true if the focus activity was adjusted to the home stack top activity. */
506 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700507 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
508 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700509 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700510 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700511
Craig Mautner84984fa2014-06-19 11:19:20 -0700512 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700513
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700514 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700515 if (top == null) {
516 return false;
517 }
518 mService.setFocusedActivityLocked(top, reason);
519 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700520 }
521
Craig Mautner299f9602015-01-26 09:47:33 -0800522 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700523 if (!mService.mBooting && !mService.mBooted) {
524 // Not ready yet!
525 return false;
526 }
527
Craig Mautner84984fa2014-06-19 11:19:20 -0700528 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
529 mWindowManager.showRecentApps();
530 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700531 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700532
Craig Mautner84984fa2014-06-19 11:19:20 -0700533 if (prev != null) {
534 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
535 }
536
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700537 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
538 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800539 // Only resume home activity if isn't finishing.
540 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800541 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700542 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700543 }
Craig Mautner299f9602015-01-26 09:47:33 -0800544 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700545 }
546
Craig Mautner8d341ef2013-03-26 09:03:27 -0700547 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700548 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700549 }
550
551 /**
552 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
553 * @param id Id of the task we would like returned.
554 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
555 * restore the task from recents to the active list.
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700556 * @param stackId The stack to restore the task to (default launch stack will be used
557 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700558 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700559 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800560 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800562 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800563 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
564 ActivityStack stack = stacks.get(stackNdx);
565 TaskRecord task = stack.taskForIdLocked(id);
566 if (task != null) {
567 return task;
568 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700569 }
570 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800571
572 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700573 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800574 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800575 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700576 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800577 return null;
578 }
579
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700580 if (!restoreFromRecents) {
581 return task;
582 }
583
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700584 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700585 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
586 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800587 return null;
588 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700589 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800590 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700591 }
592
Craig Mautner6170f732013-04-02 13:05:23 -0700593 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800594 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800596 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
598 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
599 if (r != null) {
600 return r;
601 }
Craig Mautner6170f732013-04-02 13:05:23 -0700602 }
603 }
604 return null;
605 }
606
Craig Mautneref73ee12014-04-23 11:45:37 -0700607 void setNextTaskId(int taskId) {
608 if (taskId > mCurTaskId) {
609 mCurTaskId = taskId;
610 }
611 }
612
Craig Mautner8d341ef2013-03-26 09:03:27 -0700613 int getNextTaskId() {
614 do {
615 mCurTaskId++;
616 if (mCurTaskId <= 0) {
617 mCurTaskId = 1;
618 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700619 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700620 return mCurTaskId;
621 }
622
Craig Mautnerde4ef022013-04-07 19:01:33 -0700623 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800624 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700625 if (stack == null) {
626 return null;
627 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700628 ActivityRecord resumedActivity = stack.mResumedActivity;
629 if (resumedActivity == null || resumedActivity.app == null) {
630 resumedActivity = stack.mPausingActivity;
631 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700632 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700633 }
634 }
635 return resumedActivity;
636 }
637
Dianne Hackbornff072722014-09-24 10:56:28 -0700638 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700639 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800641 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
642 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800643 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
644 final ActivityStack stack = stacks.get(stackNdx);
645 if (!isFrontStack(stack)) {
646 continue;
647 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700648 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800649 if (hr != null) {
650 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
651 && processName.equals(hr.processName)) {
652 try {
George Mount2c92c972014-03-20 09:38:23 -0700653 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800654 didSomething = true;
655 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700656 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800657 Slog.w(TAG, "Exception in new application when starting activity "
658 + hr.intent.getComponent().flattenToShortString(), e);
659 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700660 }
Craig Mautner20e72272013-04-01 13:45:53 -0700661 }
Craig Mautner20e72272013-04-01 13:45:53 -0700662 }
663 }
664 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700665 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700666 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700667 }
Craig Mautner20e72272013-04-01 13:45:53 -0700668 return didSomething;
669 }
670
671 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800672 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
673 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
675 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800676 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800677 continue;
678 }
679 final ActivityRecord resumedActivity = stack.mResumedActivity;
680 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700681 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800682 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 return false;
684 }
Craig Mautner20e72272013-04-01 13:45:53 -0700685 }
686 }
687 return true;
688 }
689
Craig Mautnerde4ef022013-04-07 19:01:33 -0700690 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800691 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
692 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800693 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
694 final ActivityStack stack = stacks.get(stackNdx);
695 if (isFrontStack(stack)) {
696 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700697 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800698 return false;
699 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700700 }
701 }
702 }
703 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700704 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
706 mLastFocusedStack + " to=" + mFocusedStack);
707 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700708 return true;
709 }
710
711 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800712 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800713 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
714 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800715 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
716 final ActivityStack stack = stacks.get(stackNdx);
717 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800718 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700719 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800720 return false;
721 }
722 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800723 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700724 }
725 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800726 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700727 }
728
Craig Mautner2acc3892013-09-23 10:28:14 -0700729 /**
730 * Pause all activities in either all of the stacks or just the back stacks.
731 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700732 * @return true if any activity was paused as a result of this call.
733 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700734 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700735 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800736 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
737 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800738 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
739 final ActivityStack stack = stacks.get(stackNdx);
740 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700741 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800742 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700743 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
744 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700746 }
747 }
748 return someActivityPaused;
749 }
750
Craig Mautnerde4ef022013-04-07 19:01:33 -0700751 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700752 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
754 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800755 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
756 final ActivityStack stack = stacks.get(stackNdx);
757 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700758 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800759 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700760 Slog.d(TAG_STATES,
761 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800762 pausing = false;
763 } else {
764 return false;
765 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700766 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700767 }
768 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700769 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700770 }
771
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700772 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
773 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500774 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800775 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
776 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
777 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
778 final ActivityStack stack = stacks.get(stackNdx);
779 if (stack.mResumedActivity != null &&
780 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700781 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800782 }
783 }
784 }
785 }
786
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700787 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700788 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700789 }
790
791 void sendWaitingVisibleReportLocked(ActivityRecord r) {
792 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700793 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700794 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700795 if (w.who == null) {
796 changed = true;
797 w.timeout = false;
798 if (r != null) {
799 w.who = new ComponentName(r.info.packageName, r.info.name);
800 }
801 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
802 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700803 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700804 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700805 if (changed) {
806 mService.notifyAll();
807 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700808 }
809
810 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
811 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700812 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700813 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700814 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700815 if (w.who == null) {
816 changed = true;
817 w.timeout = timeout;
818 if (r != null) {
819 w.who = new ComponentName(r.info.packageName, r.info.name);
820 }
821 w.thisTime = thisTime;
822 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700823 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700824 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700825 if (changed) {
826 mService.notifyAll();
827 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700828 }
829
Craig Mautner29219d92013-04-16 20:19:12 -0700830 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800831 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700832 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700833 if (r != null) {
834 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700835 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700836
Craig Mautner4a1cb222013-12-04 16:14:06 -0800837 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800838 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800839 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
840 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700841 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700842 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700843 if (r != null) {
844 return r;
845 }
846 }
847 }
848 return null;
849 }
850
Dianne Hackborn09233282014-04-30 11:33:59 -0700851 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700852 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
854 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800855 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800856 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800857 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800858 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
859 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700860 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800861 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700862 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700863 }
864 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700865
866 // The lists are already sorted from most recent to oldest. Just pull the most recent off
867 // each list and add it to list. Stop when all lists are empty or maxNum reached.
868 while (maxNum > 0) {
869 long mostRecentActiveTime = Long.MIN_VALUE;
870 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800871 final int numTaskLists = runningTaskLists.size();
872 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
873 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700874 if (!stackTaskList.isEmpty()) {
875 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
876 if (lastActiveTime > mostRecentActiveTime) {
877 mostRecentActiveTime = lastActiveTime;
878 selectedStackList = stackTaskList;
879 }
880 }
881 }
882 if (selectedStackList != null) {
883 list.add(selectedStackList.remove(0));
884 --maxNum;
885 } else {
886 break;
887 }
888 }
Craig Mautner20e72272013-04-01 13:45:53 -0700889 }
890
Craig Mautner23ac33b2013-04-01 16:26:35 -0700891 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700892 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700893 // Collect information about the target of the Intent.
894 ActivityInfo aInfo;
895 try {
896 ResolveInfo rInfo =
897 AppGlobals.getPackageManager().resolveIntent(
898 intent, resolvedType,
899 PackageManager.MATCH_DEFAULT_ONLY
900 | ActivityManagerService.STOCK_PM_FLAGS, userId);
901 aInfo = rInfo != null ? rInfo.activityInfo : null;
902 } catch (RemoteException e) {
903 aInfo = null;
904 }
905
906 if (aInfo != null) {
907 // Store the found target back into the intent, because now that
908 // we have it we never want to do this again. For example, if the
909 // user navigates back to this point in the history, we should
910 // always restart the exact same activity.
911 intent.setComponent(new ComponentName(
912 aInfo.applicationInfo.packageName, aInfo.name));
913
914 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700915 if (!aInfo.processName.equals("system")) {
916 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700917 mService.setDebugApp(aInfo.processName, true, false);
918 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700919
Man Caocfa78b22015-06-11 20:14:34 -0700920 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
921 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
922 }
923
924 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700925 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926 }
927 }
928 }
929 return aInfo;
930 }
931
Craig Mautner299f9602015-01-26 09:47:33 -0800932 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
933 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700934 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
935 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
936 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
937 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700938 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
939 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700940 null /* outActivity */, null /* container */, null /* inTask */);
941 if (inResumeTopActivity) {
942 // If we are in resume section already, home activity will be initialized, but not
943 // resumed (to avoid recursive resume) and will stay that way until something pokes it
944 // again. We need to schedule another resume.
945 scheduleResumeTopActivities();
946 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700947 }
948
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700950 String callingPackage, Intent intent, String resolvedType,
951 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700952 IBinder resultTo, String resultWho, int requestCode, int startFlags,
953 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700954 Bundle options, boolean ignoreTargetSecurity, int userId,
955 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700956 // Refuse possible leaked file descriptors
957 if (intent != null && intent.hasFileDescriptors()) {
958 throw new IllegalArgumentException("File descriptors passed in Intent");
959 }
960 boolean componentSpecified = intent.getComponent() != null;
961
962 // Don't modify the client's object!
963 intent = new Intent(intent);
964
965 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700966 ActivityInfo aInfo =
967 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700968
Craig Mautnere0a38842013-12-16 16:14:02 -0800969 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700970 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800971 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700972 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800973 // Cannot start a child activity if the parent is not resumed.
974 return ActivityManager.START_CANCELED;
975 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700976 final int realCallingPid = Binder.getCallingPid();
977 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978 int callingPid;
979 if (callingUid >= 0) {
980 callingPid = -1;
981 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700982 callingPid = realCallingPid;
983 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700984 } else {
985 callingPid = callingUid = -1;
986 }
987
Craig Mautnere0a38842013-12-16 16:14:02 -0800988 final ActivityStack stack;
989 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800990 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800991 } else {
992 stack = container.mStack;
993 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800994 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700995 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700996 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700997
998 final long origId = Binder.clearCallingIdentity();
999
1000 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001001 (aInfo.applicationInfo.privateFlags
1002 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001003 // This may be a heavy-weight process! Check to see if we already
1004 // have another, different heavy-weight process running.
1005 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1006 if (mService.mHeavyWeightProcess != null &&
1007 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1008 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001009 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001010 if (caller != null) {
1011 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1012 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001013 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001014 } else {
1015 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001016 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 + intent.toString());
1018 ActivityOptions.abort(options);
1019 return ActivityManager.START_PERMISSION_DENIED;
1020 }
1021 }
1022
1023 IIntentSender target = mService.getIntentSenderLocked(
1024 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001025 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001026 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1027 | PendingIntent.FLAG_ONE_SHOT, null);
1028
1029 Intent newIntent = new Intent();
1030 if (requestCode >= 0) {
1031 // Caller is requesting a result.
1032 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1033 }
1034 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1035 new IntentSender(target));
1036 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1037 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1038 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1039 hist.packageName);
1040 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1041 hist.task.taskId);
1042 }
1043 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1044 aInfo.packageName);
1045 newIntent.setFlags(intent.getFlags());
1046 newIntent.setClassName("android",
1047 HeavyWeightSwitcherActivity.class.getName());
1048 intent = newIntent;
1049 resolvedType = null;
1050 caller = null;
1051 callingUid = Binder.getCallingUid();
1052 callingPid = Binder.getCallingPid();
1053 componentSpecified = true;
1054 try {
1055 ResolveInfo rInfo =
1056 AppGlobals.getPackageManager().resolveIntent(
1057 intent, null,
1058 PackageManager.MATCH_DEFAULT_ONLY
1059 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1060 aInfo = rInfo != null ? rInfo.activityInfo : null;
1061 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1062 } catch (RemoteException e) {
1063 aInfo = null;
1064 }
1065 }
1066 }
1067 }
1068
Dianne Hackborn91097de2014-04-04 18:02:06 -07001069 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1070 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001071 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001072 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001073 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001074
Craig Mautner85c11a82014-07-17 12:38:18 -07001075 Binder.restoreCallingIdentity(origId);
1076
Craig Mautnerde4ef022013-04-07 19:01:33 -07001077 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001078 // If the caller also wants to switch to a new configuration,
1079 // do so now. This allows a clean switch, as we are waiting
1080 // for the current activity to pause (so we will not destroy
1081 // it), and have not yet started the next activity.
1082 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1083 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001084 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001085 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001086 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001087 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001088 }
1089
Craig Mautner23ac33b2013-04-01 16:26:35 -07001090 if (outResult != null) {
1091 outResult.result = res;
1092 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001093 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001094 do {
1095 try {
1096 mService.wait();
1097 } catch (InterruptedException e) {
1098 }
1099 } while (!outResult.timeout && outResult.who == null);
1100 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001101 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001102 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001103 outResult.timeout = false;
1104 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1105 outResult.totalTime = 0;
1106 outResult.thisTime = 0;
1107 } else {
1108 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001109 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001110 do {
1111 try {
1112 mService.wait();
1113 } catch (InterruptedException e) {
1114 }
1115 } while (!outResult.timeout && outResult.who == null);
1116 }
1117 }
1118 }
1119
1120 return res;
1121 }
1122 }
1123
1124 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1125 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1126 Bundle options, int userId) {
1127 if (intents == null) {
1128 throw new NullPointerException("intents is null");
1129 }
1130 if (resolvedTypes == null) {
1131 throw new NullPointerException("resolvedTypes is null");
1132 }
1133 if (intents.length != resolvedTypes.length) {
1134 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1135 }
1136
Craig Mautner23ac33b2013-04-01 16:26:35 -07001137
1138 int callingPid;
1139 if (callingUid >= 0) {
1140 callingPid = -1;
1141 } else if (caller == null) {
1142 callingPid = Binder.getCallingPid();
1143 callingUid = Binder.getCallingUid();
1144 } else {
1145 callingPid = callingUid = -1;
1146 }
1147 final long origId = Binder.clearCallingIdentity();
1148 try {
1149 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001150 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001151 for (int i=0; i<intents.length; i++) {
1152 Intent intent = intents[i];
1153 if (intent == null) {
1154 continue;
1155 }
1156
1157 // Refuse possible leaked file descriptors
1158 if (intent != null && intent.hasFileDescriptors()) {
1159 throw new IllegalArgumentException("File descriptors passed in Intent");
1160 }
1161
1162 boolean componentSpecified = intent.getComponent() != null;
1163
1164 // Don't modify the client's object!
1165 intent = new Intent(intent);
1166
1167 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001168 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001169 // TODO: New, check if this is correct
1170 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1171
1172 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001173 (aInfo.applicationInfo.privateFlags
1174 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001175 throw new IllegalArgumentException(
1176 "FLAG_CANT_SAVE_STATE not supported here");
1177 }
1178
1179 Bundle theseOptions;
1180 if (options != null && i == intents.length-1) {
1181 theseOptions = options;
1182 } else {
1183 theseOptions = null;
1184 }
Craig Mautner6170f732013-04-02 13:05:23 -07001185 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001186 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1187 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001188 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001189 if (res < 0) {
1190 return res;
1191 }
1192
1193 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1194 }
1195 }
1196 } finally {
1197 Binder.restoreCallingIdentity(origId);
1198 }
1199
1200 return ActivityManager.START_SUCCESS;
1201 }
1202
Craig Mautner2420ead2013-04-01 17:13:20 -07001203 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001204 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001205 throws RemoteException {
1206
Craig Mautner2568c3a2015-03-26 14:22:34 -07001207 if (andResume) {
1208 r.startFreezingScreenLocked(app, 0);
1209 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001210
Craig Mautner2568c3a2015-03-26 14:22:34 -07001211 // schedule launch ticks to collect information about slow apps.
1212 r.startLaunchTickingLocked();
1213 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001214
1215 // Have the window manager re-evaluate the orientation of
1216 // the screen based on the new activity order. Note that
1217 // as a result of this, it can call back into the activity
1218 // manager with a new orientation. We don't care about that,
1219 // because the activity is not currently running so we are
1220 // just restarting it anyway.
1221 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001222 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001223 mService.mConfiguration,
1224 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001225 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001226 }
1227
1228 r.app = app;
1229 app.waitingToKill = null;
1230 r.launchCount++;
1231 r.lastLaunchTime = SystemClock.uptimeMillis();
1232
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001233 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001234
1235 int idx = app.activities.indexOf(r);
1236 if (idx < 0) {
1237 app.activities.add(r);
1238 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001239 mService.updateLruProcessLocked(app, true, null);
1240 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001241
Craig Mautner15df08a2015-04-01 12:17:18 -07001242 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001243 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1244 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001245 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001246 }
1247
1248 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001249 try {
1250 if (app.thread == null) {
1251 throw new RemoteException();
1252 }
1253 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001254 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001255 if (andResume) {
1256 results = r.results;
1257 newIntents = r.newIntents;
1258 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001259 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1260 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1261 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 if (andResume) {
1263 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1264 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001265 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001266 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001267 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001268 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001269 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001270 }
1271 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1272 r.sleeping = false;
1273 r.forceNewConfig = false;
1274 mService.showAskCompatModeDialogLocked(r);
1275 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001276 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1278 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1279 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001280 final String profileFile = mService.mProfileFile;
1281 if (profileFile != null) {
1282 ParcelFileDescriptor profileFd = mService.mProfileFd;
1283 if (profileFd != null) {
1284 try {
1285 profileFd = profileFd.dup();
1286 } catch (IOException e) {
1287 if (profileFd != null) {
1288 try {
1289 profileFd.close();
1290 } catch (IOException o) {
1291 }
1292 profileFd = null;
1293 }
1294 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001295 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001296
1297 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1298 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001299 }
1300 }
1301 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001302
Craig Mautner2568c3a2015-03-26 14:22:34 -07001303 if (andResume) {
1304 app.hasShownUi = true;
1305 app.pendingUiClean = true;
1306 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001307 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001308 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001309 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001310 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001311 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001312 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001313
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001314 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001315 // This may be a heavy-weight process! Note that the package
1316 // manager will ensure that only activity can run in the main
1317 // process of the .apk, which is the only thing that will be
1318 // considered heavy-weight.
1319 if (app.processName.equals(app.info.packageName)) {
1320 if (mService.mHeavyWeightProcess != null
1321 && mService.mHeavyWeightProcess != app) {
1322 Slog.w(TAG, "Starting new heavy weight process " + app
1323 + " when already running "
1324 + mService.mHeavyWeightProcess);
1325 }
1326 mService.mHeavyWeightProcess = app;
1327 Message msg = mService.mHandler.obtainMessage(
1328 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1329 msg.obj = r;
1330 mService.mHandler.sendMessage(msg);
1331 }
1332 }
1333
1334 } catch (RemoteException e) {
1335 if (r.launchFailed) {
1336 // This is the second time we failed -- finish activity
1337 // and give up.
1338 Slog.e(TAG, "Second failure launching "
1339 + r.intent.getComponent().flattenToShortString()
1340 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001341 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001342 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1343 "2nd-crash", false);
1344 return false;
1345 }
1346
1347 // This is the first time we failed -- restart process and
1348 // retry.
1349 app.activities.remove(r);
1350 throw e;
1351 }
1352
1353 r.launchFailed = false;
1354 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001355 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 }
1357
1358 if (andResume) {
1359 // As part of the process of launching, ActivityThread also performs
1360 // a resume.
1361 stack.minimalResumeActivityLocked(r);
1362 } else {
1363 // This activity is not starting in the resumed state... which
1364 // should look like we asked it to pause+stop (but remain visible),
1365 // and it has done so and reported back the current icicle and
1366 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001367 if (DEBUG_STATES) Slog.v(TAG_STATES,
1368 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001369 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001370 r.stopped = true;
1371 }
1372
1373 // Launch the new version setup screen if needed. We do this -after-
1374 // launching the initial activity (that is, home), so that it can have
1375 // a chance to initialize itself while in the background, making the
1376 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001377 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001378 mService.startSetupActivityLocked();
1379 }
1380
Dianne Hackborn465fa392014-09-14 14:21:18 -07001381 // Update any services we are bound to that might care about whether
1382 // their client may have activities.
1383 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1384
Craig Mautner2420ead2013-04-01 17:13:20 -07001385 return true;
1386 }
1387
Craig Mautnere79d42682013-04-01 19:01:53 -07001388 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001389 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001390 // Is this activity's application already running?
1391 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001392 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001393
1394 r.task.stack.setLaunchTime(r);
1395
1396 if (app != null && app.thread != null) {
1397 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001398 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1399 || !"android".equals(r.info.packageName)) {
1400 // Don't add this if it is a platform component that is marked
1401 // to run in multiple processes, because this is actually
1402 // part of the framework so doesn't make sense to track as a
1403 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001404 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1405 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001406 }
George Mount2c92c972014-03-20 09:38:23 -07001407 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001408 return;
1409 } catch (RemoteException e) {
1410 Slog.w(TAG, "Exception when starting activity "
1411 + r.intent.getComponent().flattenToShortString(), e);
1412 }
1413
1414 // If a dead object exception was thrown -- fall through to
1415 // restart the application.
1416 }
1417
1418 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001419 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001420 }
1421
Craig Mautner6170f732013-04-02 13:05:23 -07001422 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001423 Intent intent, String resolvedType, ActivityInfo aInfo,
1424 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1425 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001426 int callingPid, int callingUid, String callingPackage,
1427 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001428 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1429 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001430 int err = ActivityManager.START_SUCCESS;
1431
1432 ProcessRecord callerApp = null;
1433 if (caller != null) {
1434 callerApp = mService.getRecordForAppLocked(caller);
1435 if (callerApp != null) {
1436 callingPid = callerApp.pid;
1437 callingUid = callerApp.info.uid;
1438 } else {
1439 Slog.w(TAG, "Unable to find app for caller " + caller
1440 + " (pid=" + callingPid + ") when starting: "
1441 + intent.toString());
1442 err = ActivityManager.START_PERMISSION_DENIED;
1443 }
1444 }
1445
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001446 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1447
Craig Mautner6170f732013-04-02 13:05:23 -07001448 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001449 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001450 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001451 + " on display " + (container == null ? (mFocusedStack == null ?
1452 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1453 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1454 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001455 }
1456
1457 ActivityRecord sourceRecord = null;
1458 ActivityRecord resultRecord = null;
1459 if (resultTo != null) {
1460 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001461 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1462 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001463 if (sourceRecord != null) {
1464 if (requestCode >= 0 && !sourceRecord.finishing) {
1465 resultRecord = sourceRecord;
1466 }
1467 }
1468 }
Craig Mautner6170f732013-04-02 13:05:23 -07001469
Dianne Hackborn91097de2014-04-04 18:02:06 -07001470 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001471
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001472 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001473 // Transfer the result target from the source activity to the new
1474 // one being started, including any failures.
1475 if (requestCode >= 0) {
1476 ActivityOptions.abort(options);
1477 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1478 }
1479 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001480 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1481 resultRecord = null;
1482 }
Craig Mautner6170f732013-04-02 13:05:23 -07001483 resultWho = sourceRecord.resultWho;
1484 requestCode = sourceRecord.requestCode;
1485 sourceRecord.resultTo = null;
1486 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001487 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001488 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001489 if (sourceRecord.launchedFromUid == callingUid) {
1490 // The new activity is being launched from the same uid as the previous
1491 // activity in the flow, and asking to forward its result back to the
1492 // previous. In this case the activity is serving as a trampoline between
1493 // the two, so we also want to update its launchedFromPackage to be the
1494 // same as the previous activity. Note that this is safe, since we know
1495 // these two packages come from the same uid; the caller could just as
1496 // well have supplied that same package name itself. This specifially
1497 // deals with the case of an intent picker/chooser being launched in the app
1498 // flow to redirect to an activity picked by the user, where we want the final
1499 // activity to consider it to have been launched by the previous app activity.
1500 callingPackage = sourceRecord.launchedFromPackage;
1501 }
Craig Mautner6170f732013-04-02 13:05:23 -07001502 }
1503
1504 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1505 // We couldn't find a class that can handle the given Intent.
1506 // That's the end of that!
1507 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1508 }
1509
1510 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1511 // We couldn't find the specific class specified in the Intent.
1512 // Also the end of the line.
1513 err = ActivityManager.START_CLASS_NOT_FOUND;
1514 }
1515
Dianne Hackborn91097de2014-04-04 18:02:06 -07001516 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1517 && sourceRecord.task.voiceSession != null) {
1518 // If this activity is being launched as part of a voice session, we need
1519 // to ensure that it is safe to do so. If the upcoming activity will also
1520 // be part of the voice session, we can only launch it if it has explicitly
1521 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001522 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001523 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1524 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001525 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001526 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1527 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001528 Slog.w(TAG,
1529 "Activity being started in current voice task does not support voice: "
1530 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001531 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1532 }
1533 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001534 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001535 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1536 }
1537 }
1538 }
1539
1540 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1541 // If the caller is starting a new voice session, just make sure the target
1542 // is actually allowing it to run this way.
1543 try {
1544 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1545 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001546 Slog.w(TAG,
1547 "Activity being started in new voice task does not support: "
1548 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001549 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1550 }
1551 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001552 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001553 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1554 }
1555 }
1556
louis_changcd5d1982014-08-01 10:09:08 +08001557 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1558
Craig Mautner6170f732013-04-02 13:05:23 -07001559 if (err != ActivityManager.START_SUCCESS) {
1560 if (resultRecord != null) {
1561 resultStack.sendActivityResultLocked(-1,
1562 resultRecord, resultWho, requestCode,
1563 Activity.RESULT_CANCELED, null);
1564 }
Craig Mautner6170f732013-04-02 13:05:23 -07001565 ActivityOptions.abort(options);
1566 return err;
1567 }
1568
Svetoslav7008b512015-06-24 18:47:07 -07001569 boolean abort = false;
1570
Svet Ganov99b60432015-06-27 13:15:22 -07001571 final int startAnyPerm = mService.checkPermission(
1572 START_ANY_ACTIVITY, callingPid, callingUid);
1573
1574 if (startAnyPerm != PERMISSION_GRANTED) {
1575 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001576 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001577 final int actionRestriction = getActionRestrictionForCallingPackage(
1578 intent.getAction(), callingPackage, callingPid, callingUid);
1579
1580 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1581 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1582 if (resultRecord != null) {
1583 resultStack.sendActivityResultLocked(-1,
1584 resultRecord, resultWho, requestCode,
1585 Activity.RESULT_CANCELED, null);
1586 }
1587 String msg;
1588 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1589 msg = "Permission Denial: starting " + intent.toString()
1590 + " from " + callerApp + " (pid=" + callingPid
1591 + ", uid=" + callingUid + ")" + " with revoked permission "
1592 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1593 } else if (!aInfo.exported) {
1594 msg = "Permission Denial: starting " + intent.toString()
1595 + " from " + callerApp + " (pid=" + callingPid
1596 + ", uid=" + callingUid + ")"
1597 + " not exported from uid " + aInfo.applicationInfo.uid;
1598 } else {
1599 msg = "Permission Denial: starting " + intent.toString()
1600 + " from " + callerApp + " (pid=" + callingPid
1601 + ", uid=" + callingUid + ")"
1602 + " requires " + aInfo.permission;
1603 }
1604 Slog.w(TAG, msg);
1605 throw new SecurityException(msg);
1606 }
1607
1608 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1609 String message = "Appop Denial: starting " + intent.toString()
1610 + " from " + callerApp + " (pid=" + callingPid
1611 + ", uid=" + callingUid + ")"
1612 + " requires " + AppOpsManager.permissionToOp(
1613 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1614 Slog.w(TAG, message);
1615 abort = true;
1616 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1617 String message = "Appop Denial: starting " + intent.toString()
1618 + " from " + callerApp + " (pid=" + callingPid
1619 + ", uid=" + callingUid + ")"
1620 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1621 Slog.w(TAG, message);
1622 abort = true;
1623 }
Svetoslav7008b512015-06-24 18:47:07 -07001624 }
1625
1626 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001627 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001628
Craig Mautner6170f732013-04-02 13:05:23 -07001629 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001630 try {
1631 // The Intent we give to the watcher has the extra data
1632 // stripped off, since it can contain private information.
1633 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001634 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001635 aInfo.applicationInfo.packageName);
1636 } catch (RemoteException e) {
1637 mService.mController = null;
1638 }
Ben Gruver5e207332013-04-03 17:41:37 -07001639 }
Craig Mautner6170f732013-04-02 13:05:23 -07001640
Ben Gruver5e207332013-04-03 17:41:37 -07001641 if (abort) {
1642 if (resultRecord != null) {
1643 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001644 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001645 }
Ben Gruver5e207332013-04-03 17:41:37 -07001646 // We pretend to the caller that it was really started, but
1647 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001648 ActivityOptions.abort(options);
1649 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001650 }
1651
1652 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001653 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001654 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001655 if (outActivity != null) {
1656 outActivity[0] = r;
1657 }
1658
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001659 if (r.appTimeTracker == null && sourceRecord != null) {
1660 // If the caller didn't specify an explicit time tracker, we want to continue
1661 // tracking under any it has.
1662 r.appTimeTracker = sourceRecord.appTimeTracker;
1663 }
1664
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001665 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001666 if (voiceSession == null && (stack.mResumedActivity == null
1667 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001668 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1669 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001670 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001671 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001672 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001673 ActivityOptions.abort(options);
1674 return ActivityManager.START_SWITCHES_CANCELED;
1675 }
1676 }
1677
1678 if (mService.mDidAppSwitch) {
1679 // This is the second allowed switch since we stopped switches,
1680 // so now just generally allow switches. Use case: user presses
1681 // home (switches disabled, switch to home, mDidAppSwitch now true);
1682 // user taps a home icon (coming from home so allowed, we hit here
1683 // and now allow anyone to switch again).
1684 mService.mAppSwitchesAllowedTime = 0;
1685 } else {
1686 mService.mDidAppSwitch = true;
1687 }
1688
Craig Mautneree36c772014-07-16 14:56:05 -07001689 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001690
Dianne Hackborn91097de2014-04-04 18:02:06 -07001691 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001692 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001693
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001694 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001695 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001696 // activity start, but we are not actually doing an activity
1697 // switch... just dismiss the keyguard now, because we
1698 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001699 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001700 }
1701 return err;
1702 }
1703
Svet Ganov99b60432015-06-27 13:15:22 -07001704 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001705 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001706 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1707 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001708 == PackageManager.PERMISSION_DENIED) {
1709 return ACTIVITY_RESTRICTION_PERMISSION;
1710 }
1711
Christopher Tateff7add02015-08-17 10:23:22 -07001712 if (activityInfo.permission == null) {
1713 return ACTIVITY_RESTRICTION_NONE;
1714 }
1715
Svet Ganov99b60432015-06-27 13:15:22 -07001716 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1717 if (opCode == AppOpsManager.OP_NONE) {
1718 return ACTIVITY_RESTRICTION_NONE;
1719 }
1720
1721 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1722 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001723 if (!ignoreTargetSecurity) {
1724 return ACTIVITY_RESTRICTION_APPOP;
1725 }
Svet Ganov99b60432015-06-27 13:15:22 -07001726 }
1727
1728 return ACTIVITY_RESTRICTION_NONE;
1729 }
1730
Svetoslav7008b512015-06-24 18:47:07 -07001731 private int getActionRestrictionForCallingPackage(String action,
1732 String callingPackage, int callingPid, int callingUid) {
1733 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001734 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001735 }
1736
1737 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1738 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001739 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001740 }
1741
1742 final PackageInfo packageInfo;
1743 try {
1744 packageInfo = mService.mContext.getPackageManager()
1745 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1746 } catch (PackageManager.NameNotFoundException e) {
1747 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001748 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001749 }
1750
1751 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001752 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001753 }
1754
1755 if (mService.checkPermission(permission, callingPid, callingUid) ==
1756 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001757 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001758 }
1759
1760 final int opCode = AppOpsManager.permissionToOpCode(permission);
1761 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001762 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001763 }
1764
1765 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1766 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001767 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001768 }
1769
Svet Ganov99b60432015-06-27 13:15:22 -07001770 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001771 }
1772
Chong Zhang0fa656b2015-08-31 15:17:21 -07001773 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001774 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001775
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001776 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1777 return mHomeStack;
1778 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001779
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001780 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001781
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001782 if (task != null && task.stack != null) {
1783 stack = task.stack;
1784 if (stack.isOnHomeDisplay()) {
1785 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001786 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001787 "computeStackFocus: Setting " + "focused stack to r=" + r
1788 + " task=" + task);
1789 } else {
1790 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1791 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001792 }
1793 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001794 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001795 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001796
1797 final ActivityContainer container = r.mInitialActivityContainer;
1798 if (container != null) {
1799 // The first time put it on the desired stack, after this put on task stack.
1800 r.mInitialActivityContainer = null;
1801 return container.mStack;
1802 }
1803
1804 // The fullscreen stack can contain any task regardless of if the task is resizeable
1805 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1806 // If the freeform stack has focus, and the activity to be launched is resizeable,
1807 // we can also put it in the focused stack.
1808 final boolean canUseFocusedStack =
1809 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1810 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1811 if (canUseFocusedStack
1812 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1813 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1814 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1815 return mFocusedStack;
1816 }
1817
1818 // We first try to put the task in the first dynamic stack.
1819 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1820 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1821 stack = homeDisplayStacks.get(stackNdx);
1822 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1823 if (isDynamicStack) {
1824 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1825 "computeStackFocus: Setting focused stack=" + stack);
1826 return stack;
1827 }
1828 }
1829
1830 // If there is no suitable dynamic stack then we figure out which static stack to use.
1831 final int stackId = task != null ? task.getLaunchStackId() :
1832 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1833 FULLSCREEN_WORKSPACE_STACK_ID;
1834 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1835 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1836 + r + " stackId=" + stack.mStackId);
1837 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001838 }
1839
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001840 boolean setFocusedStack(ActivityRecord r, String reason) {
1841 if (r == null) {
1842 // Not sure what you are trying to do, but it is not going to work...
1843 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001844 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001845 final TaskRecord task = r.task;
1846 if (task == null || task.stack == null) {
1847 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1848 return false;
1849 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001850 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001851 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001852 }
1853
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001854 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001855 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001856 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001857 final Intent intent = r.intent;
1858 final int callingUid = r.launchedFromUid;
1859
Chong Zhang0fa656b2015-08-31 15:17:21 -07001860 boolean overrideBounds = false;
1861 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001862 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
1863 ActivityOptions opts = new ActivityOptions(options);
1864 if (opts.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001865 overrideBounds = true;
Chong Zhang56f171d2015-10-08 10:39:23 -07001866 newBounds = opts.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001867 }
1868 }
1869
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001870 // In some flows in to this function, we retrieve the task record and hold on to it
1871 // without a lock before calling back in to here... so the task at this point may
1872 // not actually be in recents. Check for that, and if it isn't in recents just
1873 // consider it invalid.
1874 if (inTask != null && !inTask.inRecents) {
1875 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1876 inTask = null;
1877 }
1878
Craig Mautnerad400af2014-08-13 16:41:36 -07001879 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001880 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1881 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001882
Craig Mautnera228ae92014-07-09 05:44:55 -07001883 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001884 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001885 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001886 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1887 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1888 "\"singleInstance\" or \"singleTask\"");
1889 launchFlags &=
1890 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1891 } else {
1892 switch (r.info.documentLaunchMode) {
1893 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1894 break;
1895 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1896 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1897 break;
1898 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1899 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1900 break;
1901 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1902 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1903 break;
1904 }
1905 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001906
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001907 final boolean launchTaskBehind = r.mLaunchTaskBehind
1908 && !launchSingleTask && !launchSingleInstance
1909 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001910
Wale Ogunwale7d701172015-03-11 15:36:30 -07001911 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1912 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001913 // For whatever reason this activity is being launched into a new
1914 // task... yet the caller has requested a result back. Well, that
1915 // is pretty messed up, so instead immediately send back a cancel
1916 // and let the new task continue launched as normal without a
1917 // dependency on its originator.
1918 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1919 r.resultTo.task.stack.sendActivityResultLocked(-1,
1920 r.resultTo, r.resultWho, r.requestCode,
1921 Activity.RESULT_CANCELED, null);
1922 r.resultTo = null;
1923 }
1924
1925 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1926 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1927 }
1928
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001929 // If we are actually going to launch in to a new task, there are some cases where
1930 // we further want to do multiple task.
1931 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1932 if (launchTaskBehind
1933 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1934 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1935 }
1936 }
1937
Craig Mautner8849a5e2013-04-02 16:41:03 -07001938 // We'll invoke onUserLeaving before onPause only if the launching
1939 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001940 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001941 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1942 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001943
1944 // If the caller has asked not to resume at this point, we make note
1945 // of this in the record so that we can skip it when trying to find
1946 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001947 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001948 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001949 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001950 }
1951
Craig Mautnera254cd72014-05-25 16:47:39 -07001952 ActivityRecord notTop =
1953 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001954
1955 // If the onlyIfNeeded flag is set, then we can do this if the activity
1956 // being launched is the same as the one making the call... or, as
1957 // a special case, if we do not know the caller then we count the
1958 // current top activity as the caller.
1959 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1960 ActivityRecord checkedCaller = sourceRecord;
1961 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001962 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963 }
1964 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1965 // Caller is not the same as launcher, so always needed.
1966 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1967 }
1968 }
1969
Craig Mautner8849a5e2013-04-02 16:41:03 -07001970 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001971 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001972
1973 // If the caller is not coming from another activity, but has given us an
1974 // explicit task into which they would like us to launch the new activity,
1975 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001976 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1977 final Intent baseIntent = inTask.getBaseIntent();
1978 final ActivityRecord root = inTask.getRootActivity();
1979 if (baseIntent == null) {
1980 ActivityOptions.abort(options);
1981 throw new IllegalArgumentException("Launching into task without base intent: "
1982 + inTask);
1983 }
1984
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001985 // If this task is empty, then we are adding the first activity -- it
1986 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001987 if (launchSingleInstance || launchSingleTask) {
1988 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1989 ActivityOptions.abort(options);
1990 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1991 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001992 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001993 if (root != null) {
1994 ActivityOptions.abort(options);
1995 throw new IllegalArgumentException("Caller with inTask " + inTask
1996 + " has root " + root + " but target is singleInstance/Task");
1997 }
1998 }
1999
2000 // If task is empty, then adopt the interesting intent launch flags in to the
2001 // activity being started.
2002 if (root == null) {
2003 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2004 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2005 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2006 launchFlags = (launchFlags&~flagsOfInterest)
2007 | (baseIntent.getFlags()&flagsOfInterest);
2008 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002009 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002010 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002011
Dianne Hackborn962d5352014-08-29 16:27:40 -07002012 // If the task is not empty and the caller is asking to start it as the root
2013 // of a new task, then we don't actually want to start this on the task. We
2014 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002015 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002016 addingToTask = false;
2017
2018 } else {
2019 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002020 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002021
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002022 reuseTask = inTask;
2023 } else {
2024 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002025 // Launch ResolverActivity in the source task, so that it stays in the task
2026 // bounds when in freeform workspace.
2027 // Also put noDisplay activities in the source task. These by itself can
2028 // be placed in any task/stack, however it could launch other activities
2029 // like ResolverActivity, and we want those to stay in the original task.
2030 if (r.isResolverActivity() || r.noDisplay) {
2031 addingToTask = true;
2032 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002033 }
2034
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002035 if (inTask == null) {
2036 if (sourceRecord == null) {
2037 // This activity is not being started from another... in this
2038 // case we -always- start a new task.
2039 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2040 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2041 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2042 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2043 }
2044 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2045 // The original activity who is starting us is running as a single
2046 // instance... this new activity it is starting must go on its
2047 // own task.
2048 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2049 } else if (launchSingleInstance || launchSingleTask) {
2050 // The activity being started is a single instance... it always
2051 // gets launched into its own task.
2052 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2053 }
2054 }
2055
2056 ActivityInfo newTaskInfo = null;
2057 Intent newTaskIntent = null;
2058 ActivityStack sourceStack;
2059 if (sourceRecord != null) {
2060 if (sourceRecord.finishing) {
2061 // If the source is finishing, we can't further count it as our source. This
2062 // is because the task it is associated with may now be empty and on its way out,
2063 // so we don't want to blindly throw it in to that task. Instead we will take
2064 // the NEW_TASK flow and try to find a task for it. But save the task information
2065 // so it can be used when creating the new task.
2066 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2067 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2068 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2069 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2070 newTaskInfo = sourceRecord.info;
2071 newTaskIntent = sourceRecord.task.intent;
2072 }
2073 sourceRecord = null;
2074 sourceStack = null;
2075 } else {
2076 sourceStack = sourceRecord.task.stack;
2077 }
2078 } else {
2079 sourceStack = null;
2080 }
2081
2082 boolean movedHome = false;
2083 ActivityStack targetStack;
2084
2085 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002086 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002087
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002088 // We may want to try to place the new activity in to an existing task. We always
2089 // do this if the target activity is singleTask or singleInstance; we will also do
2090 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2091 // us to still place it in a new task: multi task, always doc mode, or being asked to
2092 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002093 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2094 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002095 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002096 // If bring to front is requested, and no result is requested and we have not
2097 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002098 // we can find a task that was started with this same
2099 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002100 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002101 // See if there is a task to bring to the front. If this is
2102 // a SINGLE_INSTANCE activity, there can be one and only one
2103 // instance of it in the history, and it is always in its own
2104 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002105 ActivityRecord intentActivity = !launchSingleInstance ?
2106 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002107 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002108 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2109 // but still needs to be a lock task mode violation since the task gets
2110 // cleared out and the device would otherwise leave the locked task.
2111 if (isLockTaskModeViolation(intentActivity.task,
2112 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2113 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002114 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002115 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002116 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2117 }
Craig Mautner29219d92013-04-16 20:19:12 -07002118 if (r.task == null) {
2119 r.task = intentActivity.task;
2120 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 if (intentActivity.task.intent == null) {
2122 // This task was started because of movement of
2123 // the activity based on affinity... now that we
2124 // are actually launching it, we can assign the
2125 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002126 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002127 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002128 targetStack = intentActivity.task.stack;
2129 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002130 // If the target task is not in the front, then we need
2131 // to bring it to the front... except... well, with
2132 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2133 // to have the same behavior as if a new instance was
2134 // being started, which means not bringing it to the front
2135 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002136 final ActivityStack focusStack = getFocusedStack();
2137 ActivityRecord curTop = (focusStack == null)
2138 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002139 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002140 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002141 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002142 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002143 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2144 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002145 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002146 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002147 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2148 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002149 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002150 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002151 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002152 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002153 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002154 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2155 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002156 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002157 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002158 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002159 options = null;
2160 }
2161 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002162 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002163 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002164 + " from " + intentActivity);
2165 targetStack.moveToFront("intentActivityFound");
2166 }
2167
Craig Mautner8849a5e2013-04-02 16:41:03 -07002168 // If the caller has requested that the target task be
2169 // reset, then do so.
2170 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2171 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2172 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002173 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002174 // We don't need to start a new activity, and
2175 // the client said not to do anything if that
2176 // is the case, so this is it! And for paranoia, make
2177 // sure we have correctly resumed the top activity.
2178 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002179 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002180
2181 // Make sure to notify Keyguard as well if we are not running an app
2182 // transition later.
2183 if (!movedToFront) {
2184 notifyActivityDrawnForKeyguard();
2185 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002186 } else {
2187 ActivityOptions.abort(options);
2188 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002189 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002190 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2191 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002192 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002193 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002194 // The caller has requested to completely replace any
2195 // existing task with its new activity. Well that should
2196 // not be too hard...
2197 reuseTask = intentActivity.task;
2198 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002199 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002200 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002201 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002202 // In this situation we want to remove all activities
2203 // from the task up to the one being started. In most
2204 // cases this means we are resetting the task to its
2205 // initial state.
2206 ActivityRecord top =
2207 intentActivity.task.performClearTaskLocked(r, launchFlags);
2208 if (top != null) {
2209 if (top.frontOfTask) {
2210 // Activity aliases may mean we use different
2211 // intents for the top activity, so make sure
2212 // the task now has the identity of the new
2213 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002214 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002215 }
2216 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2217 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002218 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002219 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002220 // A special case: we need to start the activity because it is not
2221 // currently running, and the caller has asked to clear the current
2222 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002223 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002224 // Now pretend like this activity is being started by the top of its
2225 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002226 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002227 TaskRecord task = sourceRecord.task;
2228 if (task != null && task.stack == null) {
2229 // Target stack got cleared when we all activities were removed
2230 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002231 targetStack = computeStackFocus(
2232 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002233 targetStack.addTask(
2234 task, !launchTaskBehind /* toTop */, false /* moving */);
2235 }
2236
Craig Mautner8849a5e2013-04-02 16:41:03 -07002237 }
2238 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2239 // In this case the top activity on the task is the
2240 // same as the one being launched, so we take that
2241 // as a request to bring the task to the foreground.
2242 // If the top activity in the task is the root
2243 // activity, deliver this new intent to it if it
2244 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002245 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002246 && intentActivity.realActivity.equals(r.realActivity)) {
2247 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2248 intentActivity.task);
2249 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002250 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002251 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002252 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2253 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002254 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2255 // In this case we are launching the root activity
2256 // of the task, but with a different intent. We
2257 // should start a new instance on top.
2258 addingToTask = true;
2259 sourceRecord = intentActivity;
2260 }
2261 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2262 // In this case an activity is being launched in to an
2263 // existing task, without resetting that task. This
2264 // is typically the situation of launching an activity
2265 // from a notification or shortcut. We want to place
2266 // the new activity on top of the current task.
2267 addingToTask = true;
2268 sourceRecord = intentActivity;
2269 } else if (!intentActivity.task.rootWasReset) {
2270 // In this case we are launching in to an existing task
2271 // that has not yet been started from its front door.
2272 // The current task has been brought to the front.
2273 // Ideally, we'd probably like to place this new task
2274 // at the bottom of its stack, but that's a little hard
2275 // to do with the current organization of the code so
2276 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002277 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002278 }
2279 if (!addingToTask && reuseTask == null) {
2280 // We didn't do anything... but it was needed (a.k.a., client
2281 // don't use that intent!) And for paranoia, make
2282 // sure we have correctly resumed the top activity.
2283 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002284 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002285 if (!movedToFront) {
2286 // Make sure to notify Keyguard as well if we are not running an app
2287 // transition later.
2288 notifyActivityDrawnForKeyguard();
2289 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002290 } else {
2291 ActivityOptions.abort(options);
2292 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002293 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002294 return ActivityManager.START_TASK_TO_FRONT;
2295 }
2296 }
2297 }
2298 }
2299
2300 //String uri = r.intent.toURI();
2301 //Intent intent2 = new Intent(uri);
2302 //Slog.i(TAG, "Given intent: " + r.intent);
2303 //Slog.i(TAG, "URI is: " + uri);
2304 //Slog.i(TAG, "To intent: " + intent2);
2305
2306 if (r.packageName != null) {
2307 // If the activity being launched is the same as the one currently
2308 // at the top, then we need to check if it should only be launched
2309 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002310 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002311 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002312 if (top != null && r.resultTo == null) {
2313 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2314 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002315 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002316 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002317 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2318 top.task);
2319 // For paranoia, make sure we have correctly
2320 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002321 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002322 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002323 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002324 }
2325 ActivityOptions.abort(options);
2326 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2327 // We don't need to start a new activity, and
2328 // the client said not to do anything if that
2329 // is the case, so this is it!
2330 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2331 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002332 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002333 return ActivityManager.START_DELIVERED_TO_TOP;
2334 }
2335 }
2336 }
2337 }
2338
2339 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002340 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002341 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2342 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002343 }
2344 ActivityOptions.abort(options);
2345 return ActivityManager.START_CLASS_NOT_FOUND;
2346 }
2347
2348 boolean newTask = false;
2349 boolean keepCurTransition = false;
2350
Craig Mautnerbb742462014-07-07 15:28:55 -07002351 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002352 sourceRecord.task : null;
2353
Craig Mautner8849a5e2013-04-02 16:41:03 -07002354 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002355 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002356 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002357 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002358 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002359 if (doResume) {
2360 targetStack.moveToFront("startingNewTask");
2361 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002362
Craig Mautner8849a5e2013-04-02 16:41:03 -07002363 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002364 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2365 newTaskInfo != null ? newTaskInfo : r.info,
2366 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002367 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2368 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002369 if (overrideBounds) {
2370 r.task.updateOverrideConfiguration(newBounds);
2371 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002372 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2373 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002374 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002375 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002376 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002377 if (isLockTaskModeViolation(r.task)) {
2378 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2379 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2380 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002381 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002382 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002383 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2384 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002385 // Caller wants to appear on home activity, so before starting
2386 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002387 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002388 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002389 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002390 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002391 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002392 if (isLockTaskModeViolation(sourceTask)) {
2393 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2394 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2395 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002396 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002397 if (doResume) {
2398 targetStack.moveToFront("sourceStackToFront");
2399 }
Craig Mautner737fae22014-10-15 12:52:10 -07002400 final TaskRecord topTask = targetStack.topTask();
2401 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002402 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002403 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002404 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002405 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002406 // In this case, we are adding the activity to an existing
2407 // task, but the caller has asked to clear that task if the
2408 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002409 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002410 keepCurTransition = true;
2411 if (top != null) {
2412 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002413 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002414 // For paranoia, make sure we have correctly
2415 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002416 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002417 if (doResume) {
2418 targetStack.resumeTopActivityLocked(null);
2419 }
2420 ActivityOptions.abort(options);
2421 return ActivityManager.START_DELIVERED_TO_TOP;
2422 }
2423 } else if (!addingToTask &&
2424 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2425 // In this case, we are launching an activity in our own task
2426 // that may already be running somewhere in the history, and
2427 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002428 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002429 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002430 final TaskRecord task = top.task;
2431 task.moveActivityToFrontLocked(top);
2432 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002433 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002434 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002435 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002436 if (doResume) {
2437 targetStack.resumeTopActivityLocked(null);
2438 }
2439 return ActivityManager.START_DELIVERED_TO_TOP;
2440 }
2441 }
2442 // An existing activity is starting this new activity, so we want
2443 // to keep the new one in the same task as the one that is starting
2444 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002445 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002446 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002447 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002448
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002449 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002450 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002451 // task it has provided to us.
2452 if (isLockTaskModeViolation(inTask)) {
2453 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2454 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2455 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002456 if (overrideBounds) {
2457 inTask.updateOverrideConfiguration(newBounds);
2458 int stackId = inTask.getLaunchStackId();
2459 if (stackId != inTask.stack.mStackId) {
2460 moveTaskToStackUncheckedLocked(
2461 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2462 }
2463 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002464 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002465 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2466 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002467
2468 // Check whether we should actually launch the new activity in to the task,
2469 // or just reuse the current activity on top.
2470 ActivityRecord top = inTask.getTopActivity();
2471 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2472 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2473 || launchSingleTop || launchSingleTask) {
2474 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2475 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2476 // We don't need to start a new activity, and
2477 // the client said not to do anything if that
2478 // is the case, so this is it!
2479 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2480 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002481 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002482 return ActivityManager.START_DELIVERED_TO_TOP;
2483 }
2484 }
2485
Dianne Hackborn962d5352014-08-29 16:27:40 -07002486 if (!addingToTask) {
2487 // We don't actually want to have this activity added to the task, so just
2488 // stop here but still tell the caller that we consumed the intent.
2489 ActivityOptions.abort(options);
2490 return ActivityManager.START_TASK_TO_FRONT;
2491 }
2492
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002493 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002494 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002495 + " in explicit task " + r.task);
2496
Craig Mautner8849a5e2013-04-02 16:41:03 -07002497 } else {
2498 // This not being started from an existing activity, and not part
2499 // of a new task... just put it in the top task, though these days
2500 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002501 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002502 if (doResume) {
2503 targetStack.moveToFront("addingToTopTask");
2504 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002505 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002506 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002507 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002508 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002509 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002510 + " in new guessed " + r.task);
2511 }
2512
2513 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002514 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002515
Craig Mautner76e2a762014-06-24 09:05:43 -07002516 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2517 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2518 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002519 if (newTask) {
2520 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2521 }
2522 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002523 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002524 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002525 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002526 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002527 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002528 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002529 return ActivityManager.START_SUCCESS;
2530 }
2531
Craig Mautneree36c772014-07-16 14:56:05 -07002532 final void doPendingActivityLaunchesLocked(boolean doResume) {
2533 while (!mPendingActivityLaunches.isEmpty()) {
2534 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002535
2536 try {
2537 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
2538 doResume && mPendingActivityLaunches.isEmpty(), null, null);
2539 } catch (Exception e) {
2540 Slog.w(TAG, "Exception during pending activity launch pal=" + pal, e);
2541 }
Craig Mautneree36c772014-07-16 14:56:05 -07002542 }
2543 }
2544
Craig Mautner7f13ed32014-07-28 14:00:35 -07002545 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002546 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2547 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002548 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002549 mPendingActivityLaunches.remove(palNdx);
2550 }
2551 }
2552 }
2553
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002554 void setLaunchSource(int uid) {
2555 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2556 }
2557
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002558 void acquireLaunchWakelock() {
2559 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2560 throw new IllegalStateException("Calling must be system uid");
2561 }
2562 mLaunchingActivity.acquire();
2563 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2564 // To be safe, don't allow the wake lock to be held for too long.
2565 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2566 }
2567 }
2568
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002569 /**
2570 * Called when the frontmost task is idle.
2571 * @return the state of mService.mBooting before this was called.
2572 */
2573 private boolean checkFinishBootingLocked() {
2574 final boolean booting = mService.mBooting;
2575 boolean enableScreen = false;
2576 mService.mBooting = false;
2577 if (!mService.mBooted) {
2578 mService.mBooted = true;
2579 enableScreen = true;
2580 }
2581 if (booting || enableScreen) {
2582 mService.postFinishBooting(booting, enableScreen);
2583 }
2584 return booting;
2585 }
2586
Craig Mautnerf3333272013-04-22 10:55:53 -07002587 // Checked.
2588 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2589 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002590 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002591
Craig Mautnerf3333272013-04-22 10:55:53 -07002592 ArrayList<ActivityRecord> stops = null;
2593 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002594 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002595 int NS = 0;
2596 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002597 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002598 boolean activityRemoved = false;
2599
Wale Ogunwale7d701172015-03-11 15:36:30 -07002600 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002601 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002602 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2603 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002604 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2605 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002606 if (fromTimeout) {
2607 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002608 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002609
2610 // This is a hack to semi-deal with a race condition
2611 // in the client where it can be constructed with a
2612 // newer configuration from when we asked it to launch.
2613 // We'll update with whatever configuration it now says
2614 // it used to launch.
2615 if (config != null) {
2616 r.configuration = config;
2617 }
2618
2619 // We are now idle. If someone is waiting for a thumbnail from
2620 // us, we can now deliver.
2621 r.idle = true;
2622
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002623 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002624 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002625 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002626 }
2627 }
2628
2629 if (allResumedActivitiesIdle()) {
2630 if (r != null) {
2631 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002632 }
2633
2634 if (mLaunchingActivity.isHeld()) {
2635 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2636 if (VALIDATE_WAKE_LOCK_CALLER &&
2637 Binder.getCallingUid() != Process.myUid()) {
2638 throw new IllegalStateException("Calling must be system uid");
2639 }
2640 mLaunchingActivity.release();
2641 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002642 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002643 }
2644
2645 // Atomically retrieve all of the other things to do.
2646 stops = processStoppingActivitiesLocked(true);
2647 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002648 if ((NF = mFinishingActivities.size()) > 0) {
2649 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002650 mFinishingActivities.clear();
2651 }
2652
Craig Mautnerf3333272013-04-22 10:55:53 -07002653 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002654 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002655 mStartingUsers.clear();
2656 }
2657
Craig Mautnerf3333272013-04-22 10:55:53 -07002658 // Stop any activities that are scheduled to do so but have been
2659 // waiting for the next one to start.
2660 for (int i = 0; i < NS; i++) {
2661 r = stops.get(i);
2662 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002663 if (stack != null) {
2664 if (r.finishing) {
2665 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2666 } else {
2667 stack.stopActivityLocked(r);
2668 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002669 }
2670 }
2671
2672 // Finish any activities that are scheduled to do so but have been
2673 // waiting for the next one to start.
2674 for (int i = 0; i < NF; i++) {
2675 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002676 final ActivityStack stack = r.task.stack;
2677 if (stack != null) {
2678 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2679 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002680 }
2681
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002682 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002683 // Complete user switch
2684 if (startingUsers != null) {
2685 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002686 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002687 }
2688 }
2689 // Complete starting up of background users
2690 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002691 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002692 mStartingBackgroundUsers.clear();
2693 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002694 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002695 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002696 }
2697 }
2698
2699 mService.trimApplications();
2700 //dump();
2701 //mWindowManager.dump();
2702
Craig Mautnerf3333272013-04-22 10:55:53 -07002703 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002704 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002705 }
2706
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002707 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002708 }
2709
Craig Mautner8e569572013-10-11 17:36:59 -07002710 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002711 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002712 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2713 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002714 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2715 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2716 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002717 }
Craig Mautner19091252013-10-05 00:03:53 -07002718 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002719 }
2720
2721 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2723 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2725 stacks.get(stackNdx).closeSystemDialogsLocked();
2726 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002727 }
2728 }
2729
Craig Mautner93529a42013-10-04 15:03:13 -07002730 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002731 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002732 }
2733
Craig Mautner8d341ef2013-03-26 09:03:27 -07002734 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002735 * Update the last used stack id for non-current user (current user's last
2736 * used stack is the focused stack)
2737 */
2738 void updateUserStackLocked(int userId, ActivityStack stack) {
2739 if (userId != mCurrentUser) {
2740 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2741 }
2742 }
2743
2744 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002745 * @return true if some activity was finished (or would have finished if doit were true).
2746 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002747 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2748 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002749 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2751 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002752 final int numStacks = stacks.size();
2753 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2754 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002755 if (stack.finishDisabledPackageActivitiesLocked(
2756 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002757 didSomething = true;
2758 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002759 }
2760 }
2761 return didSomething;
2762 }
2763
Dianne Hackborna413dc02013-07-12 12:02:55 -07002764 void updatePreviousProcessLocked(ActivityRecord r) {
2765 // Now that this process has stopped, we may want to consider
2766 // it to be the previous app to try to keep around in case
2767 // the user wants to return to it.
2768
2769 // First, found out what is currently the foreground app, so that
2770 // we don't blow away the previous app if this activity is being
2771 // hosted by the process that is actually still the foreground.
2772 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002773 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2774 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002775 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2776 final ActivityStack stack = stacks.get(stackNdx);
2777 if (isFrontStack(stack)) {
2778 if (stack.mResumedActivity != null) {
2779 fgApp = stack.mResumedActivity.app;
2780 } else if (stack.mPausingActivity != null) {
2781 fgApp = stack.mPausingActivity.app;
2782 }
2783 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002784 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002785 }
2786 }
2787
2788 // Now set this one as the previous process, only if that really
2789 // makes sense to.
2790 if (r.app != null && fgApp != null && r.app != fgApp
2791 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002792 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002793 mService.mPreviousProcess = r.app;
2794 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2795 }
2796 }
2797
Craig Mautner05d29032013-05-03 13:40:13 -07002798 boolean resumeTopActivitiesLocked() {
2799 return resumeTopActivitiesLocked(null, null, null);
2800 }
2801
2802 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2803 Bundle targetOptions) {
2804 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002805 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002806 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002807 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002808 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002809 if (isFrontStack(targetStack)) {
2810 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2811 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002812
Craig Mautnere0a38842013-12-16 16:14:02 -08002813 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2814 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002815 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2816 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002817 if (stack == targetStack) {
2818 // Already started above.
2819 continue;
2820 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002821 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002822 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002823 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002824 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002825 }
Craig Mautner05d29032013-05-03 13:40:13 -07002826 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002827 }
2828
Todd Kennedy539db512014-12-15 09:57:55 -08002829 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002830 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2831 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002832 final int numStacks = stacks.size();
2833 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2834 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002835 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002836 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002837 }
2838 }
2839
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002840 void finishVoiceTask(IVoiceInteractionSession session) {
2841 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2842 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2843 final int numStacks = stacks.size();
2844 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2845 final ActivityStack stack = stacks.get(stackNdx);
2846 stack.finishVoiceTask(session);
2847 }
2848 }
2849 }
2850
Craig Mautner299f9602015-01-26 09:47:33 -08002851 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002852 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2853 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002854 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002855 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2856 // Caller wants the home activity moved with it. To accomplish this,
2857 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002858 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002859 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002860 if (task.stack == null) {
2861 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2862 + task + " to front. Stack is null");
2863 return;
2864 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002865
2866 int stackId = task.stack.mStackId;
2867 if (task.mResizeable && options != null) {
2868 ActivityOptions opts = new ActivityOptions(options);
2869 if (opts.hasBounds()) {
2870 Rect bounds = opts.getBounds();
2871 task.updateOverrideConfiguration(bounds);
Chong Zhang87b21722015-09-21 15:39:51 -07002872 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2873 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002874 stackId = task.getLaunchStackId();
2875 }
2876 }
2877
2878 if (stackId != task.stack.mStackId) {
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002879 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2880
2881 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2882 // still need moveTaskToFrontLocked() below for any transition settings.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002883 }
2884
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002885 final ActivityRecord r = task.getTopActivity();
2886 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2887 r == null ? null : r.appTimeTracker, reason);
2888
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002889 if (DEBUG_STACK) Slog.d(TAG_STACK,
2890 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002891 }
2892
Craig Mautner967212c2013-04-13 21:10:58 -07002893 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002894 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002895 }
2896
2897 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002898 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2899 if (activityContainer != null) {
2900 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002901 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002902 if (!createStaticStackIfNeeded
2903 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2904 return null;
2905 }
2906 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 }
2908
Craig Mautner967212c2013-04-13 21:10:58 -07002909 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002910 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002911 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2912 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002913 }
2914 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002915 }
2916
Craig Mautner4a1cb222013-12-04 16:14:06 -08002917 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002918 ActivityRecord homeActivity = getHomeActivity();
2919 if (homeActivity != null) {
2920 return homeActivity.appToken;
2921 }
2922 return null;
2923 }
2924
2925 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002926 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002927 }
2928
2929 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002930 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2931 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2932 final TaskRecord task = tasks.get(taskNdx);
2933 if (task.isHomeTask()) {
2934 final ArrayList<ActivityRecord> activities = task.mActivities;
2935 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2936 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002937 if (r.isHomeActivity()
2938 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002939 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002940 }
2941 }
2942 }
2943 }
2944 return null;
2945 }
2946
Todd Kennedyca4d8422015-01-15 15:19:22 -08002947 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002948 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002949 ActivityContainer activityContainer =
2950 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002951 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002952 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2953 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002954 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002955 return activityContainer;
2956 }
2957
Craig Mautner34b73df2014-01-12 21:11:08 -08002958 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002959 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2960 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2961 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002962 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2963 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002964 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002965 }
2966 }
2967
Craig Mautner95da1082014-02-24 17:54:35 -08002968 void deleteActivityContainer(IActivityContainer container) {
2969 ActivityContainer activityContainer = (ActivityContainer)container;
2970 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002971 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2972 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002973 final int stackId = activityContainer.mStackId;
2974 mActivityContainers.remove(stackId);
2975 mWindowManager.removeStack(stackId);
2976 }
2977 }
2978
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002979 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
2980 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002981 final ActivityStack stack = getStack(stackId);
2982 if (stack == null) {
2983 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2984 return;
2985 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002986
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002987 if (!allowResizeInDockedMode
2988 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
2989 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2990 // stack size changing so things don't get out of sync.
2991 return;
2992 }
2993
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002994 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02002995 mWindowManager.deferSurfaceLayout();
2996 try {
2997 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07002998
Jorim Jaggic4025202015-10-22 16:43:34 +02002999 mTmpBounds.clear();
3000 mTmpConfigs.clear();
3001 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3002 for (int i = tasks.size() - 1; i >= 0; i--) {
3003 TaskRecord task = tasks.get(i);
3004 if (task.mResizeable) {
3005 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3006 // For freeform stack we don't adjust the size of the tasks to match that
3007 // of the stack, but we do try to make sure the tasks are still contained
3008 // with the bounds of the stack.
3009 tempRect2.set(task.mBounds);
3010 fitWithinBounds(tempRect2, bounds);
3011 task.updateOverrideConfiguration(tempRect2);
3012 } else {
3013 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003014 }
3015 }
3016
Jorim Jaggic4025202015-10-22 16:43:34 +02003017 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3018 mTmpBounds.put(task.taskId, task.mBounds);
3019 }
3020 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3021 if (stack.mStackId == DOCKED_STACK_ID) {
3022 // Dock stack funness...Yay!
3023 if (stack.mFullscreen) {
3024 // The dock stack went fullscreen which is kinda like dismissing it.
3025 // In this case we make all other static stacks fullscreen and move all
3026 // docked stack tasks to the fullscreen stack.
3027 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3028 if (i != DOCKED_STACK_ID && getStack(i) != null) {
3029 resizeStackLocked(i, null, preserveWindows, true);
3030 }
3031 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003032
Jorim Jaggic4025202015-10-22 16:43:34 +02003033 final int count = tasks.size();
3034 for (int i = 0; i < count; i++) {
3035 moveTaskToStackLocked(tasks.get(i).taskId,
3036 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
3037 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003038
Jorim Jaggic4025202015-10-22 16:43:34 +02003039 // stack shouldn't contain anymore activities, so nothing to resume.
3040 r = null;
3041 } else {
3042 // Docked stacks occupy a dedicated region on screen so the size of all other
3043 // static stacks need to be adjusted so they don't overlap with the docked stack.
3044 // We get the bounds to use from window manager which has been adjusted for any
3045 // screen controls and is also the same for all stacks.
3046 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3047
3048 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3049 if (i != DOCKED_STACK_ID) {
3050 ActivityStack otherStack = getStack(i);
3051 if (otherStack != null) {
3052 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3053 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003054 }
3055 }
3056 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003057 // Since we are resizing the stack, all other operations should strive to preserve
3058 // windows.
3059 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003060 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003061 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003062
Jorim Jaggic4025202015-10-22 16:43:34 +02003063 if (r != null) {
3064 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3065 // And we need to make sure at this point that all other activities
3066 // are made visible with the correct configuration.
3067 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3068 if (!updated) {
3069 resumeTopActivitiesLocked(stack, null, null);
3070 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003071 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003072 } finally {
3073 mWindowManager.continueSurfaceLayout();
3074 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003075 }
3076 }
3077
Chong Zhang6de2ae82015-09-30 18:25:21 -07003078 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003079 if (!task.mResizeable) {
3080 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3081 return;
3082 }
3083
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003084 adjustForMinimalTaskDimensions(task, bounds);
3085
Chong Zhang87b21722015-09-21 15:39:51 -07003086 // If this is a forced resize, let it go through even if the bounds is not changing,
3087 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003088 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003089 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003090 // Nothing to do here...
3091 return;
3092 }
3093
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003094 if (!mWindowManager.isValidTaskId(task.taskId)) {
3095 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003096 // All we can do for now is update the bounds so it can be used when the task is
3097 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003098 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003099 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3100 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003101 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003102 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003103 return;
3104 }
3105
Chong Zhang6de2ae82015-09-30 18:25:21 -07003106 // Do not move the task to another stack here.
3107 // This method assumes that the task is already placed in the right stack.
3108 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003109
Chong Zhang6de2ae82015-09-30 18:25:21 -07003110 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003111
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003112 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003113 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003114 // way and the activity was kept the way it was. If it's false, it means the activity had
3115 // to be relaunched due to configuration change.
3116 boolean kept = true;
3117 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003118 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003119 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003120 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003121 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003122 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003123 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003124 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003125 resumeTopActivitiesLocked(stack, null, null);
3126 }
3127 }
3128 }
Chong Zhang87b21722015-09-21 15:39:51 -07003129 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003130
3131 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003132 }
3133
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003134 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3135 if (bounds == null) {
3136 return;
3137 }
3138 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3139 : task.mMinimalSize;
3140 final boolean adjustWidth = minimalSize > bounds.width();
3141 final boolean adjustHeight = minimalSize > bounds.height();
3142 if (!(adjustWidth || adjustHeight)) {
3143 return;
3144 }
3145 Rect taskBounds = task.mBounds;
3146 if (adjustWidth) {
3147 if (taskBounds != null && bounds.right == taskBounds.right) {
3148 bounds.left = bounds.right - minimalSize;
3149 } else {
3150 // Either left bounds match, or neither match, or the previous bounds were
3151 // fullscreen and we default to keeping left.
3152 bounds.right = bounds.left + minimalSize;
3153 }
3154 }
3155 if (adjustHeight) {
3156 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3157 bounds.top = bounds.bottom - minimalSize;
3158 } else {
3159 // Either top bounds match, or neither match, or the previous bounds were
3160 // fullscreen and we default to keeping top.
3161 bounds.bottom = bounds.top + minimalSize;
3162 }
3163 }
3164 }
3165
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003166 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003167 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3168 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003169 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003170 }
3171
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003172 ActivityContainer activityContainer = new ActivityContainer(stackId);
3173 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003174 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003175 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003176 }
3177
3178 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003179 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003180 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3181 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003182 break;
3183 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003184 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003185 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003186 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003187 }
3188
Chong Zhang5dcb2752015-08-18 13:50:26 -07003189 /**
3190 * Restores a recent task to a stack
3191 * @param task The recent task to be restored.
3192 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003193 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003194 * @return true if the task has been restored successfully.
3195 */
3196 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3197 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003198 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003199 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003200 if (task.stack != null) {
3201 // Task has already been restored once. See if we need to do anything more
3202 if (task.stack.mStackId == stackId) {
3203 // Nothing else to do since it is already restored in the right stack.
3204 return true;
3205 }
3206 // Remove current stack association, so we can re-associate the task with the
3207 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003208 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003209 }
3210
3211 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003212 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003213
3214 if (stack == null) {
3215 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003216 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3217 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003218 return false;
3219 }
3220
3221 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003222 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3223 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003224 final ArrayList<ActivityRecord> activities = task.mActivities;
3225 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003226 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003227 }
3228 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003229 }
3230
Wale Ogunwale040b4702015-08-06 18:10:50 -07003231 /**
3232 * Moves the specified task record to the input stack id.
3233 * WARNING: This method performs an unchecked/raw move of the task and
3234 * can leave the system in an unstable state if used incorrectly.
3235 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3236 * to a stack.
3237 * @param task Task to move.
3238 * @param stackId Id of stack to move task to.
3239 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003240 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003241 * @param reason Reason the task is been moved.
3242 * @return The stack the task was moved to.
3243 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003244 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003245 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3246 final ActivityRecord r = task.getTopActivity();
3247 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3248 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3249
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003250 final boolean resizeable = task.mResizeable;
3251 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3252 // if a docked stack is created below which will lead to the stack we are moving from and
3253 // its resizeable tasks being resized.
3254 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003255 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003256 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003257 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3258 if (task.stack != null) {
3259 task.stack.removeTask(task, reason, MOVING);
3260 }
3261 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003262
3263 // If the task had focus before (or we're requested to move focus),
3264 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003265 stack.setFocusAndResumeStateIfNeeded(
3266 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003267
Wale Ogunwale040b4702015-08-06 18:10:50 -07003268 return stack;
3269 }
3270
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003271 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003272 final TaskRecord task = anyTaskForIdLocked(taskId);
3273 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003274 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003275 return;
3276 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003277 final String reason = "moveTaskToStack";
Wale Ogunwale99db1862015-10-23 20:08:22 -07003278 if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID
3279 || stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003280 // We are about to relaunch the activity because its configuration changed due to
3281 // being maximized, i.e. size change. The activity will first remove the old window
3282 // and then add a new one. This call will tell window manager about this, so it can
3283 // preserve the old window until the new one is drawn. This prevents having a gap
3284 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003285 // entrance of the new window to be properly animated.
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003286 ActivityRecord r = task.getTopActivity();
3287 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3288 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003289 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003290 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003291
3292 // Make sure the task has the appropriate bounds/size for the stack it is in.
3293 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003294 resizeTaskLocked(task, stack.mBounds,
3295 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003296 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3297 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003298 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3299 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003300 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003301 resizeTaskLocked(task, stack.mBounds,
3302 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003303 }
3304
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003305 // The task might have already been running and its visibility needs to be synchronized with
3306 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003307 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003308 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003309 }
3310
Wale Ogunwale079a0042015-10-24 11:44:07 -07003311 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3312 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3313 if (stack == null) {
3314 throw new IllegalArgumentException(
3315 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3316 }
3317
3318 final ActivityRecord r = stack.topRunningActivityLocked();
3319 if (r == null) {
3320 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3321 + " in stack=" + stack);
3322 return false;
3323 }
3324
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003325 if (!r.info.supportsPip) {
3326 Slog.w(TAG,
3327 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003328 + " r=" + r);
3329 return false;
3330 }
3331
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003332 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003333 if (task.mActivities.size() == 1) {
3334 // There is only one activity in the task. So, we can just move the task over to the
3335 // pinned stack without re-parenting the activity in a different task.
3336 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS);
3337 } else {
3338 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3339 pinnedStack.moveActivityToStack(r);
3340 }
3341
3342 resizeStackLocked(PINNED_STACK_ID, bounds, PRESERVE_WINDOWS, true);
3343
3344 // The task might have already been running and its visibility needs to be synchronized with
3345 // the visibility of the stack / windows.
3346 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3347 resumeTopActivitiesLocked();
3348 return true;
3349 }
3350
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003351 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3352 final TaskRecord task = anyTaskForIdLocked(taskId);
3353 if (task == null) {
3354 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3355 return;
3356 }
3357 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003358 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003359 mWindowManager.positionTaskInStack(taskId, stackId, position);
3360 final boolean stackChanged = task.stack != null && task.stack != stack;
3361 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003362 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003363 }
3364 stack.positionTask(task, position, stackChanged);
3365 // The task might have already been running and its visibility needs to be synchronized with
3366 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003367 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003368 resumeTopActivitiesLocked();
3369 }
3370
Craig Mautnerac6f8432013-07-17 13:24:59 -07003371 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003372 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003373 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3374 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003375 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3376 final ActivityStack stack = stacks.get(stackNdx);
3377 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003378 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003379 continue;
3380 }
3381 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003382 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3383 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 continue;
3385 }
3386 final ActivityRecord ar = stack.findTaskLocked(r);
3387 if (ar != null) {
3388 return ar;
3389 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003390 }
3391 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003392 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003393 return null;
3394 }
3395
3396 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003397 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3398 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003399 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3400 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3401 if (ar != null) {
3402 return ar;
3403 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003404 }
3405 }
3406 return null;
3407 }
3408
Craig Mautner8d341ef2013-03-26 09:03:27 -07003409 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003410 scheduleSleepTimeout();
3411 if (!mGoingToSleep.isHeld()) {
3412 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003413 if (mLaunchingActivity.isHeld()) {
3414 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3415 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003416 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003417 mLaunchingActivity.release();
3418 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003419 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003420 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003421 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003422 }
3423
3424 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003425 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003426
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003427 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003428 final long endTime = System.currentTimeMillis() + timeout;
3429 while (true) {
3430 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003431 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3432 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003433 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3434 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3435 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003436 }
3437 if (cantShutdown) {
3438 long timeRemaining = endTime - System.currentTimeMillis();
3439 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003440 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003441 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003442 } catch (InterruptedException e) {
3443 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003444 } else {
3445 Slog.w(TAG, "Activity manager shutdown timed out");
3446 timedout = true;
3447 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003448 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003449 } else {
3450 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003451 }
3452 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003453
3454 // Force checkReadyForSleep to complete.
3455 mSleepTimeout = true;
3456 checkReadyForSleepLocked();
3457
Craig Mautner8d341ef2013-03-26 09:03:27 -07003458 return timedout;
3459 }
3460
3461 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003462 removeSleepTimeouts();
3463 if (mGoingToSleep.isHeld()) {
3464 mGoingToSleep.release();
3465 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003466 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3467 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003468 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3469 final ActivityStack stack = stacks.get(stackNdx);
3470 stack.awakeFromSleepingLocked();
3471 if (isFrontStack(stack)) {
3472 resumeTopActivitiesLocked();
3473 }
Craig Mautner5314a402013-09-26 12:40:16 -07003474 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003475 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003476 mGoingToSleepActivities.clear();
3477 }
3478
3479 void activitySleptLocked(ActivityRecord r) {
3480 mGoingToSleepActivities.remove(r);
3481 checkReadyForSleepLocked();
3482 }
3483
3484 void checkReadyForSleepLocked() {
3485 if (!mService.isSleepingOrShuttingDown()) {
3486 // Do not care.
3487 return;
3488 }
3489
3490 if (!mSleepTimeout) {
3491 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003492 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3493 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003494 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3495 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3496 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003497 }
3498
3499 if (mStoppingActivities.size() > 0) {
3500 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003501 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003502 + mStoppingActivities.size() + " activities");
3503 scheduleIdleLocked();
3504 dontSleep = true;
3505 }
3506
3507 if (mGoingToSleepActivities.size() > 0) {
3508 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003509 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003510 + mGoingToSleepActivities.size() + " activities");
3511 dontSleep = true;
3512 }
3513
3514 if (dontSleep) {
3515 return;
3516 }
3517 }
3518
Craig Mautnere0a38842013-12-16 16:14:02 -08003519 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3520 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003521 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3522 stacks.get(stackNdx).goToSleep();
3523 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003524 }
3525
3526 removeSleepTimeouts();
3527
3528 if (mGoingToSleep.isHeld()) {
3529 mGoingToSleep.release();
3530 }
3531 if (mService.mShuttingDown) {
3532 mService.notifyAll();
3533 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003534 }
3535
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003536 boolean reportResumedActivityLocked(ActivityRecord r) {
3537 final ActivityStack stack = r.task.stack;
3538 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003539 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003540 }
3541 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003542 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003543 mWindowManager.executeAppTransition();
3544 return true;
3545 }
3546 return false;
3547 }
3548
Craig Mautner8d341ef2013-03-26 09:03:27 -07003549 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003550 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3551 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003552 int stackNdx = stacks.size() - 1;
3553 while (stackNdx >= 0) {
3554 stacks.get(stackNdx).handleAppCrashLocked(app);
3555 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003556 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003557 }
3558 }
3559
Jose Lima4b6c6692014-08-12 17:41:12 -07003560 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003561 final ActivityStack stack = r.task.stack;
3562 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003563 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3564 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003565 return false;
3566 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003567 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003568 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3569 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003570
3571 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003572 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003573 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003574 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003575 return true;
3576 }
3577
3578 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003579 if (visible && top.fullscreen) {
3580 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003581 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3582 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3583 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3584 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003585 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003586 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3587 // Only the activity set as currently visible behind should actively reset its
3588 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003589 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3590 "requestVisibleBehind: returning visible=" + visible
3591 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3592 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003593 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003594 }
3595
Jose Lima4b6c6692014-08-12 17:41:12 -07003596 stack.setVisibleBehindActivity(visible ? r : null);
3597 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003598 // Make the activity immediately above r opaque.
3599 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3600 if (next != null) {
3601 mService.convertFromTranslucent(next.appToken);
3602 }
3603 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003604 if (top.app != null && top.app.thread != null) {
3605 // Notify the top app of the change.
3606 try {
3607 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3608 } catch (RemoteException e) {
3609 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003610 }
3611 return true;
3612 }
3613
Craig Mautnerbb742462014-07-07 15:28:55 -07003614 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3615 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3616 r.mLaunchTaskBehind = false;
3617 final TaskRecord task = r.task;
3618 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003619 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003620 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003621 mWindowManager.setAppVisibility(r.appToken, false);
3622 }
3623
3624 void scheduleLaunchTaskBehindComplete(IBinder token) {
3625 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3626 }
3627
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003628 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3629 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003630 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003631 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3632 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003633 final int topStackNdx = stacks.size() - 1;
3634 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3635 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003636 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003637 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003638 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003639 }
3640
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003641 void invalidateTaskLayers() {
3642 mTaskLayersChanged = true;
3643 }
3644
3645 void rankTaskLayersIfNeeded() {
3646 if (!mTaskLayersChanged) {
3647 return;
3648 }
3649 mTaskLayersChanged = false;
3650 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3651 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3652 int baseLayer = 0;
3653 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3654 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3655 }
3656 }
3657 }
3658
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003659 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3660 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3661 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3662 final int topStackNdx = stacks.size() - 1;
3663 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3664 final ActivityStack stack = stacks.get(stackNdx);
3665 stack.clearOtherAppTimeTrackers(except);
3666 }
3667 }
3668 }
3669
Craig Mautner8d341ef2013-03-26 09:03:27 -07003670 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003671 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3672 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003673 final int numStacks = stacks.size();
3674 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3675 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003676 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003677 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003678 }
3679 }
3680
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003681 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3682 // Examine all activities currently running in the process.
3683 TaskRecord firstTask = null;
3684 // Tasks is non-null only if two or more tasks are found.
3685 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003686 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3687 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003688 ActivityRecord r = app.activities.get(i);
3689 // First, if we find an activity that is in the process of being destroyed,
3690 // then we just aren't going to do anything for now; we want things to settle
3691 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003692 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003693 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003694 return;
3695 }
3696 // Don't consider any activies that are currently not in a state where they
3697 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003698 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3699 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003700 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003701 continue;
3702 }
3703 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003704 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003705 + " from " + r);
3706 if (firstTask == null) {
3707 firstTask = r.task;
3708 } else if (firstTask != r.task) {
3709 if (tasks == null) {
3710 tasks = new ArraySet<>();
3711 tasks.add(firstTask);
3712 }
3713 tasks.add(r.task);
3714 }
3715 }
3716 }
3717 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003718 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003719 return;
3720 }
3721 // If we have activities in multiple tasks that are in a position to be destroyed,
3722 // let's iterate through the tasks and release the oldest one.
3723 final int numDisplays = mActivityDisplays.size();
3724 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3725 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3726 // Step through all stacks starting from behind, to hit the oldest things first.
3727 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3728 final ActivityStack stack = stacks.get(stackNdx);
3729 // Try to release activities in this stack; if we manage to, we are done.
3730 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3731 return;
3732 }
3733 }
3734 }
3735 }
3736
Amith Yamasani37a40c22015-06-17 13:25:42 -07003737 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003738 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003739 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003740 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003741
Craig Mautner858d8a62013-04-23 17:08:34 -07003742 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003743 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3744 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003745 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003746 final ActivityStack stack = stacks.get(stackNdx);
3747 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003748 TaskRecord task = stack.topTask();
3749 if (task != null) {
3750 mWindowManager.moveTaskToTop(task.taskId);
3751 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003752 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003753 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003754
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003755 ActivityStack stack = getStack(restoreStackId);
3756 if (stack == null) {
3757 stack = mHomeStack;
3758 }
3759 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003760 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003761 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003762 } else {
3763 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003764 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003765 }
Craig Mautner93529a42013-10-04 15:03:13 -07003766 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003767 }
3768
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003769 /**
3770 * Add background users to send boot completed events to.
3771 * @param userId The user being started in the background
3772 * @param uss The state object for the user.
3773 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003774 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003775 mStartingBackgroundUsers.add(uss);
3776 }
3777
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003778 /** Checks whether the userid is a profile of the current user. */
3779 boolean isCurrentProfileLocked(int userId) {
3780 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003781 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003782 }
3783
Chong Zhang45c25ce2015-08-10 22:18:26 -07003784 /** Checks whether the activity should be shown for current user. */
3785 boolean okToShowLocked(ActivityRecord r) {
3786 return r != null && (isCurrentProfileLocked(r.userId)
3787 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3788 }
3789
Craig Mautnerde4ef022013-04-07 19:01:33 -07003790 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003791 ArrayList<ActivityRecord> stops = null;
3792
3793 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003794 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3795 ActivityRecord s = mStoppingActivities.get(activityNdx);
3796 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003797 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003798 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3799 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003800 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003801 if (s.finishing) {
3802 // If this activity is finishing, it is sitting on top of
3803 // everyone else but we now know it is no longer needed...
3804 // so get rid of it. Otherwise, we need to go through the
3805 // normal flow and hide it once we determine that it is
3806 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003807 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003808 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003809 }
3810 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003811 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003812 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003813 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003814 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003815 }
3816 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003817 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003818 }
3819 }
3820
3821 return stops;
3822 }
3823
Craig Mautnercf910b02013-04-23 11:23:27 -07003824 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003825 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3826 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3827 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3828 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003829 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003830 final ActivityState state = r == null ? DESTROYED : r.state;
3831 if (isFrontStack(stack)) {
3832 if (r == null) Slog.e(TAG,
3833 "validateTop...: null top activity, stack=" + stack);
3834 else {
3835 final ActivityRecord pausing = stack.mPausingActivity;
3836 if (pausing != null && pausing == r) Slog.e(TAG,
3837 "validateTop...: top stack has pausing activity r=" + r
3838 + " state=" + state);
3839 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3840 "validateTop...: activity in front not resumed r=" + r
3841 + " state=" + state);
3842 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003843 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003844 final ActivityRecord resumed = stack.mResumedActivity;
3845 if (resumed != null && resumed == r) Slog.e(TAG,
3846 "validateTop...: back stack has resumed activity r=" + r
3847 + " state=" + state);
3848 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3849 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003850 }
3851 }
3852 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003853 }
3854
Craig Mautnere0570202015-05-13 13:06:11 -07003855 private String lockTaskModeToString() {
3856 switch (mLockTaskModeState) {
3857 case LOCK_TASK_MODE_LOCKED:
3858 return "LOCKED";
3859 case LOCK_TASK_MODE_PINNED:
3860 return "PINNED";
3861 case LOCK_TASK_MODE_NONE:
3862 return "NONE";
3863 default: return "unknown=" + mLockTaskModeState;
3864 }
3865 }
3866
Craig Mautner27084302013-03-25 08:05:25 -07003867 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003868 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003869 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003870 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3871 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3872 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003873 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003874 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3875 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3876 if (packages.size() > 0) {
3877 pw.println(" mLockTaskPackages (userId:packages)=");
3878 for (int i = 0; i < packages.size(); ++i) {
3879 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3880 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3881 }
3882 }
3883 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003884 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003885
Craig Mautner20e72272013-04-01 13:45:53 -07003886 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003887 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003888 }
3889
Dianne Hackborn390517b2013-05-30 15:03:32 -07003890 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3891 boolean needSep, String prefix) {
3892 if (activity != null) {
3893 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3894 if (needSep) {
3895 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003896 }
3897 pw.print(prefix);
3898 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003899 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003900 }
3901 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003902 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003903 }
3904
Craig Mautner8d341ef2013-03-26 09:03:27 -07003905 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3906 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003907 boolean printed = false;
3908 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003909 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3910 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003911 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003912 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003913 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003914 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003915 final ActivityStack stack = stacks.get(stackNdx);
3916 StringBuilder stackHeader = new StringBuilder(128);
3917 stackHeader.append(" Stack #");
3918 stackHeader.append(stack.mStackId);
3919 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003920 stackHeader.append("\n");
3921 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3922 stackHeader.append("\n");
3923 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3925 needSep, stackHeader.toString());
3926 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3927 !dumpAll, false, dumpPackage, true,
3928 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003929
Craig Mautner4a1cb222013-12-04 16:14:06 -08003930 needSep = printed;
3931 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3932 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003933 if (pr) {
3934 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003935 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003936 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003937 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3938 " mResumedActivity: ");
3939 if (pr) {
3940 printed = true;
3941 needSep = false;
3942 }
3943 if (dumpAll) {
3944 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3945 " mLastPausedActivity: ");
3946 if (pr) {
3947 printed = true;
3948 needSep = true;
3949 }
3950 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3951 needSep, " mLastNoHistoryActivity: ");
3952 }
3953 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003954 }
3955 }
3956
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003957 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3958 false, dumpPackage, true, " Activities waiting to finish:", null);
3959 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3960 false, dumpPackage, true, " Activities waiting to stop:", null);
3961 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3962 false, dumpPackage, true, " Activities waiting for another to become visible:",
3963 null);
3964 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3965 false, dumpPackage, true, " Activities waiting to sleep:", null);
3966 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3967 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003968
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003969 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003970 }
3971
Dianne Hackborn390517b2013-05-30 15:03:32 -07003972 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003973 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003974 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003975 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003976 String innerPrefix = null;
3977 String[] args = null;
3978 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003979 for (int i=list.size()-1; i>=0; i--) {
3980 final ActivityRecord r = list.get(i);
3981 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3982 continue;
3983 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003984 if (innerPrefix == null) {
3985 innerPrefix = prefix + " ";
3986 args = new String[0];
3987 }
3988 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003989 final boolean full = !brief && (complete || !r.isInHistory());
3990 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003991 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003992 needNL = false;
3993 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003994 if (header1 != null) {
3995 pw.println(header1);
3996 header1 = null;
3997 }
3998 if (header2 != null) {
3999 pw.println(header2);
4000 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004001 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004002 if (lastTask != r.task) {
4003 lastTask = r.task;
4004 pw.print(prefix);
4005 pw.print(full ? "* " : " ");
4006 pw.println(lastTask);
4007 if (full) {
4008 lastTask.dump(pw, prefix + " ");
4009 } else if (complete) {
4010 // Complete + brief == give a summary. Isn't that obvious?!?
4011 if (lastTask.intent != null) {
4012 pw.print(prefix); pw.print(" ");
4013 pw.println(lastTask.intent.toInsecureStringWithClip());
4014 }
4015 }
4016 }
4017 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4018 pw.print(" #"); pw.print(i); pw.print(": ");
4019 pw.println(r);
4020 if (full) {
4021 r.dump(pw, innerPrefix);
4022 } else if (complete) {
4023 // Complete + brief == give a summary. Isn't that obvious?!?
4024 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4025 if (r.app != null) {
4026 pw.print(innerPrefix); pw.println(r.app);
4027 }
4028 }
4029 if (client && r.app != null && r.app.thread != null) {
4030 // flush anything that is already in the PrintWriter since the thread is going
4031 // to write to the file descriptor directly
4032 pw.flush();
4033 try {
4034 TransferPipe tp = new TransferPipe();
4035 try {
4036 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4037 r.appToken, innerPrefix, args);
4038 // Short timeout, since blocking here can
4039 // deadlock with the application.
4040 tp.go(fd, 2000);
4041 } finally {
4042 tp.kill();
4043 }
4044 } catch (IOException e) {
4045 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4046 } catch (RemoteException e) {
4047 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4048 }
4049 needNL = true;
4050 }
4051 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004052 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004053 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004054
Craig Mautnerf3333272013-04-22 10:55:53 -07004055 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004056 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4057 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004058 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4059 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004060 }
4061
4062 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004063 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004064 }
4065
4066 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004067 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4068 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004069 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4070 }
4071
Craig Mautner05d29032013-05-03 13:40:13 -07004072 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004073 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4074 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4075 }
Craig Mautner05d29032013-05-03 13:40:13 -07004076 }
4077
Craig Mautner0eea92c2013-05-16 13:35:39 -07004078 void removeSleepTimeouts() {
4079 mSleepTimeout = false;
4080 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4081 }
4082
4083 final void scheduleSleepTimeout() {
4084 removeSleepTimeouts();
4085 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4086 }
4087
Craig Mautner4a1cb222013-12-04 16:14:06 -08004088 @Override
4089 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004090 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004091 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4092 }
4093
4094 @Override
4095 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004096 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004097 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4098 }
4099
4100 @Override
4101 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004102 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004103 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4104 }
4105
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004106 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004107 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004108 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004109 newDisplay = mActivityDisplays.get(displayId) == null;
4110 if (newDisplay) {
4111 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004112 if (activityDisplay.mDisplay == null) {
4113 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4114 return;
4115 }
Craig Mautner4504de52013-12-20 09:06:56 -08004116 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004117 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004118 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004119 }
Craig Mautner4504de52013-12-20 09:06:56 -08004120 if (newDisplay) {
4121 mWindowManager.onDisplayAdded(displayId);
4122 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004123 }
4124
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004125 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4126 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4127 return;
4128 }
4129 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4130 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4131 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4132 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4133 }
4134
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004135 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004136 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004137 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4138 if (activityDisplay != null) {
4139 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004140 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004141 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004142 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004143 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004144 }
4145 }
4146 mWindowManager.onDisplayRemoved(displayId);
4147 }
4148
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004149 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004150 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004151 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4152 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004153 // TODO: Update the bounds.
4154 }
4155 }
4156 mWindowManager.onDisplayChanged(displayId);
4157 }
4158
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004159 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004160 StackInfo info = new StackInfo();
4161 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4162 info.displayId = Display.DEFAULT_DISPLAY;
4163 info.stackId = stack.mStackId;
4164
4165 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4166 final int numTasks = tasks.size();
4167 int[] taskIds = new int[numTasks];
4168 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004169 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004170 for (int i = 0; i < numTasks; ++i) {
4171 final TaskRecord task = tasks.get(i);
4172 taskIds[i] = task.taskId;
4173 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4174 : task.realActivity != null ? task.realActivity.flattenToString()
4175 : task.getTopActivity() != null ? task.getTopActivity().packageName
4176 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004177 taskBounds[i] = new Rect();
4178 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004179 }
4180 info.taskIds = taskIds;
4181 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004182 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004183 return info;
4184 }
4185
4186 StackInfo getStackInfoLocked(int stackId) {
4187 ActivityStack stack = getStack(stackId);
4188 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004189 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004190 }
4191 return null;
4192 }
4193
4194 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004195 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004196 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4197 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004198 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004199 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004200 }
4201 }
4202 return list;
4203 }
4204
Craig Mautner15df08a2015-04-01 12:17:18 -07004205 TaskRecord getLockedTaskLocked() {
4206 final int top = mLockTaskModeTasks.size() - 1;
4207 if (top >= 0) {
4208 return mLockTaskModeTasks.get(top);
4209 }
4210 return null;
4211 }
4212
4213 boolean isLockedTask(TaskRecord task) {
4214 return mLockTaskModeTasks.contains(task);
4215 }
4216
4217 boolean isLastLockedTask(TaskRecord task) {
4218 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4219 }
4220
4221 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004222 if (!mLockTaskModeTasks.remove(task)) {
4223 return;
4224 }
4225 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4226 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004227 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004228 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4229 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004230 final Message lockTaskMsg = Message.obtain();
4231 lockTaskMsg.arg1 = task.userId;
4232 lockTaskMsg.what = LOCK_TASK_END_MSG;
4233 mHandler.sendMessage(lockTaskMsg);
4234 }
4235 }
4236
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004237 void showLockTaskToast() {
4238 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004239 }
4240
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004241 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4242 if (mLockTaskModeTasks.contains(task)) {
4243 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4244 }
4245 }
4246
Craig Mautner432f64e2015-05-20 14:59:57 -07004247 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4248 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004249 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004250 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004251 final TaskRecord lockedTask = getLockedTaskLocked();
4252 if (lockedTask != null) {
4253 removeLockedTaskLocked(lockedTask);
4254 if (!mLockTaskModeTasks.isEmpty()) {
4255 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004256 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4257 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004258 lockedTask.performClearTaskLocked();
4259 resumeTopActivitiesLocked();
4260 return;
4261 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004262 }
Craig Mautnere0570202015-05-13 13:06:11 -07004263 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4264 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004265 return;
4266 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004267
4268 // Should have already been checked, but do it again.
4269 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004270 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4271 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004272 return;
4273 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004274 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004275 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004276 return;
4277 }
4278
4279 if (mLockTaskModeTasks.isEmpty()) {
4280 // First locktask.
4281 final Message lockTaskMsg = Message.obtain();
4282 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4283 lockTaskMsg.arg1 = task.userId;
4284 lockTaskMsg.what = LOCK_TASK_START_MSG;
4285 lockTaskMsg.arg2 = lockTaskModeState;
4286 mHandler.sendMessage(lockTaskMsg);
4287 }
4288 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004289 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4290 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004291 mLockTaskModeTasks.remove(task);
4292 mLockTaskModeTasks.add(task);
4293
4294 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004295 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004296 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004297
4298 if (andResume) {
4299 findTaskToMoveToFrontLocked(task, 0, null, reason);
4300 resumeTopActivitiesLocked();
4301 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004302 }
4303
4304 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004305 return isLockTaskModeViolation(task, false);
4306 }
4307
4308 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4309 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004310 return false;
4311 }
4312 final int lockTaskAuth = task.mLockTaskAuth;
4313 switch (lockTaskAuth) {
4314 case LOCK_TASK_AUTH_DONT_LOCK:
4315 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004316 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004317 case LOCK_TASK_AUTH_LAUNCHABLE:
4318 case LOCK_TASK_AUTH_WHITELISTED:
4319 return false;
4320 case LOCK_TASK_AUTH_PINNABLE:
4321 // Pinnable tasks can't be launched on top of locktask tasks.
4322 return !mLockTaskModeTasks.isEmpty();
4323 default:
4324 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4325 return true;
4326 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004327 }
4328
Craig Mautner15df08a2015-04-01 12:17:18 -07004329 void onLockTaskPackagesUpdatedLocked() {
4330 boolean didSomething = false;
4331 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4332 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004333 final boolean wasWhitelisted =
4334 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4335 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004336 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004337 final boolean isWhitelisted =
4338 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4339 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4340 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004341 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004342 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4343 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004344 removeLockedTaskLocked(lockedTask);
4345 lockedTask.performClearTaskLocked();
4346 didSomething = true;
4347 }
4348 }
4349 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4350 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4351 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4352 final ActivityStack stack = stacks.get(stackNdx);
4353 stack.onLockTaskPackagesUpdatedLocked();
4354 }
4355 }
Craig Mautnere0570202015-05-13 13:06:11 -07004356 final ActivityRecord r = topRunningActivityLocked();
4357 final TaskRecord task = r != null ? r.task : null;
4358 if (mLockTaskModeTasks.isEmpty() && task != null
4359 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4360 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004361 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4362 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4363 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4364 false);
4365 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004366 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004367 if (didSomething) {
4368 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004369 }
4370 }
4371
Benjamin Franz43261142015-02-11 15:59:44 +00004372 int getLockTaskModeState() {
4373 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004374 }
4375
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004376 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004377
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004378 public ActivityStackSupervisorHandler(Looper looper) {
4379 super(looper);
4380 }
4381
Craig Mautnerf3333272013-04-22 10:55:53 -07004382 void activityIdleInternal(ActivityRecord r) {
4383 synchronized (mService) {
4384 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4385 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004386 }
4387
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004388 @Override
4389 public void handleMessage(Message msg) {
4390 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004391 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004392 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4393 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004394 if (mService.mDidDexOpt) {
4395 mService.mDidDexOpt = false;
4396 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4397 nmsg.obj = msg.obj;
4398 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4399 return;
4400 }
4401 // We don't at this point know if the activity is fullscreen,
4402 // so we need to be conservative and assume it isn't.
4403 activityIdleInternal((ActivityRecord)msg.obj);
4404 } break;
4405 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004406 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004407 activityIdleInternal((ActivityRecord)msg.obj);
4408 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004409 case RESUME_TOP_ACTIVITY_MSG: {
4410 synchronized (mService) {
4411 resumeTopActivitiesLocked();
4412 }
4413 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004414 case SLEEP_TIMEOUT_MSG: {
4415 synchronized (mService) {
4416 if (mService.isSleepingOrShuttingDown()) {
4417 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4418 mSleepTimeout = true;
4419 checkReadyForSleepLocked();
4420 }
4421 }
4422 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004423 case LAUNCH_TIMEOUT_MSG: {
4424 if (mService.mDidDexOpt) {
4425 mService.mDidDexOpt = false;
4426 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4427 return;
4428 }
4429 synchronized (mService) {
4430 if (mLaunchingActivity.isHeld()) {
4431 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4432 if (VALIDATE_WAKE_LOCK_CALLER
4433 && Binder.getCallingUid() != Process.myUid()) {
4434 throw new IllegalStateException("Calling must be system uid");
4435 }
4436 mLaunchingActivity.release();
4437 }
4438 }
4439 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004440 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004441 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004442 } break;
4443 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004444 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004445 } break;
4446 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004447 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004448 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004449 case CONTAINER_CALLBACK_VISIBILITY: {
4450 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004451 final IActivityContainerCallback callback = container.mCallback;
4452 if (callback != null) {
4453 try {
4454 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4455 } catch (RemoteException e) {
4456 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004457 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004458 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004459 case LOCK_TASK_START_MSG: {
4460 // When lock task starts, we disable the status bars.
4461 try {
Jason Monk62515be2014-05-21 16:06:19 -04004462 if (mLockTaskNotify == null) {
4463 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004464 }
Jason Monk62515be2014-05-21 16:06:19 -04004465 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004466 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004467 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004468 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004469 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004470 flags = StatusBarManager.DISABLE_MASK
4471 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004472 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004473 flags = StatusBarManager.DISABLE_MASK
4474 & (~StatusBarManager.DISABLE_BACK)
4475 & (~StatusBarManager.DISABLE_HOME)
4476 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004477 }
4478 getStatusBarService().disable(flags, mToken,
4479 mService.mContext.getPackageName());
4480 }
4481 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004482 if (getDevicePolicyManager() != null) {
4483 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004484 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004485 }
justinzhang5286d3f2014-05-12 17:06:01 -04004486 } catch (RemoteException ex) {
4487 throw new RuntimeException(ex);
4488 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004489 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004490 case LOCK_TASK_END_MSG: {
4491 // When lock task ends, we enable the status bars.
4492 try {
Jason Monk62515be2014-05-21 16:06:19 -04004493 if (getStatusBarService() != null) {
4494 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4495 mService.mContext.getPackageName());
4496 }
4497 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004498 if (getDevicePolicyManager() != null) {
4499 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4500 msg.arg1);
4501 }
Jason Monk62515be2014-05-21 16:06:19 -04004502 if (mLockTaskNotify == null) {
4503 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4504 }
4505 mLockTaskNotify.show(false);
4506 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004507 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004508 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004509 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004510 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004511 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004512 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004513 new LockPatternUtils(mService.mContext)
4514 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004515 }
4516 } catch (SettingNotFoundException e) {
4517 // No setting, don't lock.
4518 }
justinzhang5286d3f2014-05-12 17:06:01 -04004519 } catch (RemoteException ex) {
4520 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004521 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004522 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004523 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004524 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004525 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4526 if (mLockTaskNotify == null) {
4527 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4528 }
4529 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4530 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004531 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4532 final ActivityContainer container = (ActivityContainer) msg.obj;
4533 final IActivityContainerCallback callback = container.mCallback;
4534 if (callback != null) {
4535 try {
4536 callback.onAllActivitiesComplete(container.asBinder());
4537 } catch (RemoteException e) {
4538 }
4539 }
4540 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004541 case LAUNCH_TASK_BEHIND_COMPLETE: {
4542 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004543 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004544 if (r != null) {
4545 handleLaunchTaskBehindCompleteLocked(r);
4546 }
4547 }
4548 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004549 }
4550 }
4551 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004552
Ying Wangb081a592014-04-22 15:20:16 -07004553 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004554 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004555 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004556 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004557 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004558 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004559 ActivityRecord mParentActivity = null;
4560 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004561
Craig Mautnere3a00d72014-04-16 08:31:19 -07004562 boolean mVisible = true;
4563
Craig Mautner4a1cb222013-12-04 16:14:06 -08004564 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004565 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004566
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004567 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4568 final static int CONTAINER_STATE_NO_SURFACE = 1;
4569 final static int CONTAINER_STATE_FINISHING = 2;
4570 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4571
4572 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004573 synchronized (mService) {
4574 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004575 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004576 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004577 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004578 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004579 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004580
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004581 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004582 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004583 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004584 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004585 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004586 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004587 }
4588
4589 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004590 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004591 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004592 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4593 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004594 return;
4595 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004596 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004597 }
4598 }
4599
4600 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004601 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004602 synchronized (mService) {
4603 if (mActivityDisplay != null) {
4604 return mActivityDisplay.mDisplayId;
4605 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004606 }
4607 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004608 }
4609
Jeff Brownca9bc702014-02-11 14:32:56 -08004610 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004611 public int getStackId() {
4612 synchronized (mService) {
4613 return mStackId;
4614 }
4615 }
4616
4617 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004618 public boolean injectEvent(InputEvent event) {
4619 final long origId = Binder.clearCallingIdentity();
4620 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004621 synchronized (mService) {
4622 if (mActivityDisplay != null) {
4623 return mInputManagerInternal.injectInputEvent(event,
4624 mActivityDisplay.mDisplayId,
4625 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4626 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004627 }
4628 return false;
4629 } finally {
4630 Binder.restoreCallingIdentity(origId);
4631 }
4632 }
4633
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004634 @Override
4635 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004636 synchronized (mService) {
4637 if (mContainerState == CONTAINER_STATE_FINISHING) {
4638 return;
4639 }
4640 mContainerState = CONTAINER_STATE_FINISHING;
4641
Craig Mautnerd163e752014-06-13 17:18:47 -07004642 long origId = Binder.clearCallingIdentity();
4643 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004644 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004645 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004646 } finally {
4647 Binder.restoreCallingIdentity(origId);
4648 }
4649 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004650 }
4651
Craig Mautner60257702014-09-17 15:02:33 -07004652 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004653 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004654 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004655 if (mActivityDisplay != null) {
4656 mActivityDisplay.detachActivitiesLocked(mStack);
4657 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004658 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004659 }
4660 }
4661
4662 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004663 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004664 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004665 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004666 Binder.getCallingUid(), mCurrentUser, false,
4667 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004668
Craig Mautnere0a38842013-12-16 16:14:02 -08004669 // TODO: Switch to user app stacks here.
4670 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004671 final Uri data = intent.getData();
4672 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4673 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004674 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004675 checkEmbeddedAllowedInner(userId, intent, mimeType);
4676
4677 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4678 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004679 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004680 }
4681
4682 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004683 public final int startActivityIntentSender(IIntentSender intentSender)
4684 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004685 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4686
4687 if (!(intentSender instanceof PendingIntentRecord)) {
4688 throw new IllegalArgumentException("Bad PendingIntent object");
4689 }
4690
Craig Mautnerb9168362015-02-26 20:40:19 -08004691 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004692 Binder.getCallingUid(), mCurrentUser, false,
4693 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004694
4695 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4696 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4697 pendingIntent.key.requestResolvedType);
4698
4699 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4700 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4701 }
4702
4703 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004704 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004705 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004706 throw new SecurityException(
4707 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4708 }
4709 }
4710
Craig Mautnerdf88d732014-01-27 09:21:32 -08004711 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004712 public IBinder asBinder() {
4713 return this;
4714 }
4715
Craig Mautner4504de52013-12-20 09:06:56 -08004716 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004717 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004718 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004719 }
4720
Craig Mautner4a1cb222013-12-04 16:14:06 -08004721 ActivityStackSupervisor getOuter() {
4722 return ActivityStackSupervisor.this;
4723 }
4724
Craig Mautnerd163e752014-06-13 17:18:47 -07004725 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004726 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004727 }
4728
Craig Mautner6985bad2014-04-21 15:22:06 -07004729 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004730 void setVisible(boolean visible) {
4731 if (mVisible != visible) {
4732 mVisible = visible;
4733 if (mCallback != null) {
4734 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4735 0 /* unused */, this).sendToTarget();
4736 }
4737 }
4738 }
4739
Craig Mautner6985bad2014-04-21 15:22:06 -07004740 void setDrawn() {
4741 }
4742
Craig Mautner1b4bf852014-05-26 15:06:32 -07004743 // You can always start a new task on a regular ActivityStack.
4744 boolean isEligibleForNewTasks() {
4745 return true;
4746 }
4747
Craig Mautnerd163e752014-06-13 17:18:47 -07004748 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004749 detachLocked();
4750 deleteActivityContainer(this);
4751 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004752 }
4753
Craig Mautner34b73df2014-01-12 21:11:08 -08004754 @Override
4755 public String toString() {
4756 return mIdString + (mActivityDisplay == null ? "N" : "A");
4757 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004758 }
4759
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004760 private class VirtualActivityContainer extends ActivityContainer {
4761 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004762 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004763
4764 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4765 super(getNextStackId());
4766 mParentActivity = parent;
4767 mCallback = callback;
4768 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004769 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004770 }
4771
4772 @Override
4773 public void setSurface(Surface surface, int width, int height, int density) {
4774 super.setSurface(surface, width, height, density);
4775
4776 synchronized (mService) {
4777 final long origId = Binder.clearCallingIdentity();
4778 try {
4779 setSurfaceLocked(surface, width, height, density);
4780 } finally {
4781 Binder.restoreCallingIdentity(origId);
4782 }
4783 }
4784 }
4785
4786 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4787 if (mContainerState == CONTAINER_STATE_FINISHING) {
4788 return;
4789 }
4790 VirtualActivityDisplay virtualActivityDisplay =
4791 (VirtualActivityDisplay) mActivityDisplay;
4792 if (virtualActivityDisplay == null) {
4793 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004794 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004795 mActivityDisplay = virtualActivityDisplay;
4796 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004797 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004798 }
4799
4800 if (mSurface != null) {
4801 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004802 }
4803
Craig Mautner6985bad2014-04-21 15:22:06 -07004804 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004805 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004806 mStack.resumeTopActivityLocked(null);
4807 } else {
4808 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004809 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004810 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004811 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004812 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004813 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004814
Craig Mautnerd163e752014-06-13 17:18:47 -07004815 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004816
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004817 if (DEBUG_STACK) Slog.d(TAG_STACK,
4818 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004819 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004820
Craig Mautner6985bad2014-04-21 15:22:06 -07004821 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004822 boolean isAttachedLocked() {
4823 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004824 }
4825
4826 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004827 void setDrawn() {
4828 synchronized (mService) {
4829 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004830 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004831 }
4832 }
4833
Craig Mautner1b4bf852014-05-26 15:06:32 -07004834 // Never start a new task on an ActivityView if it isn't explicitly specified.
4835 @Override
4836 boolean isEligibleForNewTasks() {
4837 return false;
4838 }
4839
Craig Mautnerd163e752014-06-13 17:18:47 -07004840 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004841 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004842 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4843 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4844 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4845 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4846 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004847 }
4848 }
4849
Craig Mautner4a1cb222013-12-04 16:14:06 -08004850 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4851 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004852 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004853 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004854 int mDisplayId;
4855 Display mDisplay;
4856 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004857
Craig Mautner4a1cb222013-12-04 16:14:06 -08004858 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4859 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004860 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004861
Jose Lima4b6c6692014-08-12 17:41:12 -07004862 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004863
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004864 ActivityDisplay() {
4865 }
Craig Mautner4504de52013-12-20 09:06:56 -08004866
Craig Mautner1a70a162014-09-13 12:09:31 -07004867 // After instantiation, check that mDisplay is not null before using this. The alternative
4868 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004869 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004870 final Display display = mDisplayManager.getDisplay(displayId);
4871 if (display == null) {
4872 return;
4873 }
4874 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004875 }
4876
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004877 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004878 mDisplay = display;
4879 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004880 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004881 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004882
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004883 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004884 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004885 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4886 + " onTop=" + onTop);
4887 if (onTop) {
4888 mStacks.add(stack);
4889 } else {
4890 mStacks.add(0, stack);
4891 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004892 }
4893
Craig Mautnere0a38842013-12-16 16:14:02 -08004894 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004895 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004896 + " from displayId=" + mDisplayId);
4897 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004898 }
4899
Jose Lima4b6c6692014-08-12 17:41:12 -07004900 void setVisibleBehindActivity(ActivityRecord r) {
4901 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004902 }
4903
Jose Lima4b6c6692014-08-12 17:41:12 -07004904 boolean hasVisibleBehindActivity() {
4905 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004906 }
4907
Craig Mautner34b73df2014-01-12 21:11:08 -08004908 @Override
4909 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004910 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4911 }
4912 }
4913
4914 class VirtualActivityDisplay extends ActivityDisplay {
4915 VirtualDisplay mVirtualDisplay;
4916
Craig Mautner6985bad2014-04-21 15:22:06 -07004917 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004918 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004919 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4920 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4921 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4922 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004923
4924 init(mVirtualDisplay.getDisplay());
4925
4926 mWindowManager.handleDisplayAdded(mDisplayId);
4927 }
4928
4929 void setSurface(Surface surface) {
4930 if (mVirtualDisplay != null) {
4931 mVirtualDisplay.setSurface(surface);
4932 }
4933 }
4934
4935 @Override
4936 void detachActivitiesLocked(ActivityStack stack) {
4937 super.detachActivitiesLocked(stack);
4938 if (mVirtualDisplay != null) {
4939 mVirtualDisplay.release();
4940 mVirtualDisplay = null;
4941 }
4942 }
4943
4944 @Override
4945 public String toString() {
4946 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004947 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004948 }
Jose Lima58e66d62014-05-27 20:00:27 -07004949
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004950 /**
4951 * Adjust bounds to stay within stack bounds.
4952 *
4953 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4954 * that keep them unchanged, but be contained within the stack bounds.
4955 *
4956 * @param bounds Bounds to be adjusted.
4957 * @param stackBounds Bounds within which the other bounds should remain.
4958 */
4959 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4960 if (stackBounds == null || stackBounds.contains(bounds)) {
4961 return;
4962 }
4963
4964 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4965 final int maxRight = stackBounds.right
4966 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4967 int horizontalDiff = stackBounds.left - bounds.left;
4968 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4969 || (bounds.left + horizontalDiff >= maxRight)) {
4970 horizontalDiff = maxRight - bounds.left;
4971 }
4972 bounds.left += horizontalDiff;
4973 bounds.right += horizontalDiff;
4974 }
4975
4976 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4977 final int maxBottom = stackBounds.bottom
4978 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4979 int verticalDiff = stackBounds.top - bounds.top;
4980 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4981 || (bounds.top + verticalDiff >= maxBottom)) {
4982 verticalDiff = maxBottom - bounds.top;
4983 }
4984 bounds.top += verticalDiff;
4985 bounds.bottom += verticalDiff;
4986 }
4987 }
4988
Craig Mautner27084302013-03-25 08:05:25 -07004989}