blob: b40ec7516c11d670c20db92b700b8a0446f76d90 [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
Jose Lima58e66d62014-05-27 20:00:27 -0700298 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
299 * setWindowManager is called. **/
300 private boolean mLeanbackOnlyDevice;
301
Craig Mautner0eea92c2013-05-16 13:35:39 -0700302 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700303 * We don't want to allow the device to go to sleep while in the process
304 * of launching an activity. This is primarily to allow alarm intent
305 * receivers to launch an activity and get that to run before the device
306 * goes back to sleep.
307 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700308 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700309
310 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700311 * Set when the system is going to sleep, until we have
312 * successfully paused the current activity and released our wake lock.
313 * At that point the system is allowed to actually sleep.
314 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700315 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700316
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700317 /** Stack id of the front stack when user switched, indexed by userId. */
318 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700319
Craig Mautner4504de52013-12-20 09:06:56 -0800320 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800321 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700322 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800323
324 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700325 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326
Jeff Brownca9bc702014-02-11 14:32:56 -0800327 InputManagerInternal mInputManagerInternal;
328
Craig Mautner15df08a2015-04-01 12:17:18 -0700329 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
330 * may be finished until there is only one entry left. If this is empty the system is not
331 * in lockTask mode. */
332 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000333 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700334 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
335 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000336 */
337 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400338 /**
339 * Notifies the user when entering/exiting lock-task.
340 */
341 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800342
Craig Mautner15df08a2015-04-01 12:17:18 -0700343 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700344
Craig Mautner42d04db2014-11-06 12:13:23 -0800345 /** Used to keep resumeTopActivityLocked() from being entered recursively */
346 boolean inResumeTopActivity;
347
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700348 // 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 -0700349 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700350 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700351
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700352 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
353 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
354
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700355 // The default minimal size that will be used if the activity doesn't specify its minimal size.
356 // It will be calculated when the default display gets added.
357 private int mDefaultMinimalSizeOfResizeableTask = -1;
358
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700359 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
360 private boolean mTaskLayersChanged = true;
361
Craig Mautneree36c772014-07-16 14:56:05 -0700362 /**
363 * Description of a request to start a new activity, which has been held
364 * due to app switches being disabled.
365 */
366 static class PendingActivityLaunch {
367 final ActivityRecord r;
368 final ActivityRecord sourceRecord;
369 final int startFlags;
370 final ActivityStack stack;
371
372 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
373 int _startFlags, ActivityStack _stack) {
374 r = _r;
375 sourceRecord = _sourceRecord;
376 startFlags = _startFlags;
377 stack = _stack;
378 }
379 }
380
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800381 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700382 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800383 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700384 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
385 }
386
387 /**
388 * At the time when the constructor runs, the power manager has not yet been
389 * initialized. So we initialize our wakelocks afterwards.
390 */
391 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700393 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700394 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700395 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700396 }
397
justinzhang5286d3f2014-05-12 17:06:01 -0400398 // This function returns a IStatusBarService. The value is from ServiceManager.
399 // getService and is cached.
400 private IStatusBarService getStatusBarService() {
401 synchronized (mService) {
402 if (mStatusBarService == null) {
403 mStatusBarService = IStatusBarService.Stub.asInterface(
404 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
405 if (mStatusBarService == null) {
406 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
407 }
408 }
409 return mStatusBarService;
410 }
411 }
412
Jason Monk35c62a42014-06-17 10:24:47 -0400413 private IDevicePolicyManager getDevicePolicyManager() {
414 synchronized (mService) {
415 if (mDevicePolicyManager == null) {
416 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
417 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
418 if (mDevicePolicyManager == null) {
419 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
420 }
421 }
422 return mDevicePolicyManager;
423 }
424 }
425
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700426 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800427 synchronized (mService) {
428 mWindowManager = wm;
429
430 mDisplayManager =
431 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
432 mDisplayManager.registerDisplayListener(this, null);
433
434 Display[] displays = mDisplayManager.getDisplays();
435 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
436 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800437 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700438 if (activityDisplay.mDisplay == null) {
439 throw new IllegalStateException("Default Display does not exist");
440 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800441 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700442 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800443 }
444
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700445 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800446 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800447
448 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700449
450 // Initialize this here, now that we can get a valid reference to PackageManager.
451 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800452 }
Craig Mautner27084302013-03-25 08:05:25 -0700453 }
454
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200455 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700456 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200457 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700458 }
459
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700460 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800461 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700462 }
463
Craig Mautnerde4ef022013-04-07 19:01:33 -0700464 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800465 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700466 }
467
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800468 /** Top of all visible stacks is/should always be equal to the focused stack.
469 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
470 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700471 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700472 if (stack == null) {
473 return false;
474 }
475
Craig Mautnerdf88d732014-01-27 09:21:32 -0800476 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
477 if (parent != null) {
478 stack = parent.task.stack;
479 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800480 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700481 }
482
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700483 void setFocusStack(String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800484 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800485 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800486 if (topNdx <= 0) {
487 return;
488 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700489
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700490 final ActivityStack topStack = stacks.get(topNdx);
491 mFocusedStack = topStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800492 if (lastFocusedStack != null) {
493 mLastFocusedStack = lastFocusedStack;
494 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800495
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700496 EventLogTags.writeAmFocusedStack(
497 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
498 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800499
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800500 if (mService.mBooting || !mService.mBooted) {
501 final ActivityRecord r = topRunningActivityLocked();
502 if (r != null && r.idle) {
503 checkFinishBootingLocked();
504 }
505 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700506 }
507
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700508 void moveHomeStackToFront(String reason) {
509 mHomeStack.moveToFront(reason);
510 }
511
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700512 /** Returns true if the focus activity was adjusted to the home stack top activity. */
513 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700514 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
515 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700516 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700517 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700518
Craig Mautner84984fa2014-06-19 11:19:20 -0700519 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700520
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700521 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700522 if (top == null) {
523 return false;
524 }
525 mService.setFocusedActivityLocked(top, reason);
526 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700527 }
528
Craig Mautner299f9602015-01-26 09:47:33 -0800529 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700530 if (!mService.mBooting && !mService.mBooted) {
531 // Not ready yet!
532 return false;
533 }
534
Craig Mautner84984fa2014-06-19 11:19:20 -0700535 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
536 mWindowManager.showRecentApps();
537 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700538 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700539
Craig Mautner84984fa2014-06-19 11:19:20 -0700540 if (prev != null) {
541 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
542 }
543
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700544 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
545 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800546 // Only resume home activity if isn't finishing.
547 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800548 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700549 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700550 }
Craig Mautner299f9602015-01-26 09:47:33 -0800551 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700552 }
553
Craig Mautner8d341ef2013-03-26 09:03:27 -0700554 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700555 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700556 }
557
558 /**
559 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
560 * @param id Id of the task we would like returned.
561 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
562 * restore the task from recents to the active list.
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700563 * @param stackId The stack to restore the task to (default launch stack will be used
564 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700565 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700566 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800567 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800569 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800570 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
571 ActivityStack stack = stacks.get(stackNdx);
572 TaskRecord task = stack.taskForIdLocked(id);
573 if (task != null) {
574 return task;
575 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700576 }
577 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800578
579 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700580 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800581 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800582 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800584 return null;
585 }
586
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700587 if (!restoreFromRecents) {
588 return task;
589 }
590
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700591 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700592 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
593 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800594 return null;
595 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700596 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800597 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700598 }
599
Craig Mautner6170f732013-04-02 13:05:23 -0700600 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800601 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800603 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800604 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
605 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
606 if (r != null) {
607 return r;
608 }
Craig Mautner6170f732013-04-02 13:05:23 -0700609 }
610 }
611 return null;
612 }
613
Craig Mautneref73ee12014-04-23 11:45:37 -0700614 void setNextTaskId(int taskId) {
615 if (taskId > mCurTaskId) {
616 mCurTaskId = taskId;
617 }
618 }
619
Craig Mautner8d341ef2013-03-26 09:03:27 -0700620 int getNextTaskId() {
621 do {
622 mCurTaskId++;
623 if (mCurTaskId <= 0) {
624 mCurTaskId = 1;
625 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700626 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700627 return mCurTaskId;
628 }
629
Craig Mautnerde4ef022013-04-07 19:01:33 -0700630 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800631 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700632 if (stack == null) {
633 return null;
634 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700635 ActivityRecord resumedActivity = stack.mResumedActivity;
636 if (resumedActivity == null || resumedActivity.app == null) {
637 resumedActivity = stack.mPausingActivity;
638 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700639 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700640 }
641 }
642 return resumedActivity;
643 }
644
Dianne Hackbornff072722014-09-24 10:56:28 -0700645 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700646 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800647 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800648 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
649 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800650 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
651 final ActivityStack stack = stacks.get(stackNdx);
652 if (!isFrontStack(stack)) {
653 continue;
654 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700655 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800656 if (hr != null) {
657 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
658 && processName.equals(hr.processName)) {
659 try {
George Mount2c92c972014-03-20 09:38:23 -0700660 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800661 didSomething = true;
662 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700663 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 Slog.w(TAG, "Exception in new application when starting activity "
665 + hr.intent.getComponent().flattenToShortString(), e);
666 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700667 }
Craig Mautner20e72272013-04-01 13:45:53 -0700668 }
Craig Mautner20e72272013-04-01 13:45:53 -0700669 }
670 }
671 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700672 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700673 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700674 }
Craig Mautner20e72272013-04-01 13:45:53 -0700675 return didSomething;
676 }
677
678 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800679 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
680 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800681 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
682 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800683 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800684 continue;
685 }
686 final ActivityRecord resumedActivity = stack.mResumedActivity;
687 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700688 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800689 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800690 return false;
691 }
Craig Mautner20e72272013-04-01 13:45:53 -0700692 }
693 }
694 return true;
695 }
696
Craig Mautnerde4ef022013-04-07 19:01:33 -0700697 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800698 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
699 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800700 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
701 final ActivityStack stack = stacks.get(stackNdx);
702 if (isFrontStack(stack)) {
703 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700704 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 return false;
706 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700707 }
708 }
709 }
710 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700711 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
713 mLastFocusedStack + " to=" + mFocusedStack);
714 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700715 return true;
716 }
717
718 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800719 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800720 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
721 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800722 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
723 final ActivityStack stack = stacks.get(stackNdx);
724 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800725 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700726 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800727 return false;
728 }
729 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800730 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700731 }
732 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800733 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700734 }
735
Craig Mautner2acc3892013-09-23 10:28:14 -0700736 /**
737 * Pause all activities in either all of the stacks or just the back stacks.
738 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700739 * @return true if any activity was paused as a result of this call.
740 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700741 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700742 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800743 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
744 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800745 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
746 final ActivityStack stack = stacks.get(stackNdx);
747 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700748 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800749 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700750 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
751 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800752 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700753 }
754 }
755 return someActivityPaused;
756 }
757
Craig Mautnerde4ef022013-04-07 19:01:33 -0700758 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700759 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800760 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
761 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800762 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
763 final ActivityStack stack = stacks.get(stackNdx);
764 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700765 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800766 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700767 Slog.d(TAG_STATES,
768 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 pausing = false;
770 } else {
771 return false;
772 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700773 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700774 }
775 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700776 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700777 }
778
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700779 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
780 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500781 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800782 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
783 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
784 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
785 final ActivityStack stack = stacks.get(stackNdx);
786 if (stack.mResumedActivity != null &&
787 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700788 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800789 }
790 }
791 }
792 }
793
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700794 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700795 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700796 }
797
798 void sendWaitingVisibleReportLocked(ActivityRecord r) {
799 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700800 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700801 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700802 if (w.who == null) {
803 changed = true;
804 w.timeout = false;
805 if (r != null) {
806 w.who = new ComponentName(r.info.packageName, r.info.name);
807 }
808 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
809 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700810 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700811 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700812 if (changed) {
813 mService.notifyAll();
814 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700815 }
816
817 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
818 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700819 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700820 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700821 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700822 if (w.who == null) {
823 changed = true;
824 w.timeout = timeout;
825 if (r != null) {
826 w.who = new ComponentName(r.info.packageName, r.info.name);
827 }
828 w.thisTime = thisTime;
829 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700830 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700831 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700832 if (changed) {
833 mService.notifyAll();
834 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700835 }
836
Craig Mautner29219d92013-04-16 20:19:12 -0700837 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800838 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700839 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700840 if (r != null) {
841 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700842 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700843
Craig Mautner4a1cb222013-12-04 16:14:06 -0800844 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800845 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800846 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
847 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700848 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700849 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700850 if (r != null) {
851 return r;
852 }
853 }
854 }
855 return null;
856 }
857
Dianne Hackborn09233282014-04-30 11:33:59 -0700858 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700859 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800860 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
861 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800862 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800863 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800864 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800865 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
866 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700867 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800868 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700869 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700870 }
871 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700872
873 // The lists are already sorted from most recent to oldest. Just pull the most recent off
874 // each list and add it to list. Stop when all lists are empty or maxNum reached.
875 while (maxNum > 0) {
876 long mostRecentActiveTime = Long.MIN_VALUE;
877 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800878 final int numTaskLists = runningTaskLists.size();
879 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
880 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700881 if (!stackTaskList.isEmpty()) {
882 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
883 if (lastActiveTime > mostRecentActiveTime) {
884 mostRecentActiveTime = lastActiveTime;
885 selectedStackList = stackTaskList;
886 }
887 }
888 }
889 if (selectedStackList != null) {
890 list.add(selectedStackList.remove(0));
891 --maxNum;
892 } else {
893 break;
894 }
895 }
Craig Mautner20e72272013-04-01 13:45:53 -0700896 }
897
Craig Mautner23ac33b2013-04-01 16:26:35 -0700898 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700899 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700900 // Collect information about the target of the Intent.
901 ActivityInfo aInfo;
902 try {
903 ResolveInfo rInfo =
904 AppGlobals.getPackageManager().resolveIntent(
905 intent, resolvedType,
906 PackageManager.MATCH_DEFAULT_ONLY
907 | ActivityManagerService.STOCK_PM_FLAGS, userId);
908 aInfo = rInfo != null ? rInfo.activityInfo : null;
909 } catch (RemoteException e) {
910 aInfo = null;
911 }
912
913 if (aInfo != null) {
914 // Store the found target back into the intent, because now that
915 // we have it we never want to do this again. For example, if the
916 // user navigates back to this point in the history, we should
917 // always restart the exact same activity.
918 intent.setComponent(new ComponentName(
919 aInfo.applicationInfo.packageName, aInfo.name));
920
921 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700922 if (!aInfo.processName.equals("system")) {
923 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924 mService.setDebugApp(aInfo.processName, true, false);
925 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926
Man Caocfa78b22015-06-11 20:14:34 -0700927 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
928 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
929 }
930
931 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700932 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700933 }
934 }
935 }
936 return aInfo;
937 }
938
Craig Mautner299f9602015-01-26 09:47:33 -0800939 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
940 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700941 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
942 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
943 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
944 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700945 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
946 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700947 null /* outActivity */, null /* container */, null /* inTask */);
948 if (inResumeTopActivity) {
949 // If we are in resume section already, home activity will be initialized, but not
950 // resumed (to avoid recursive resume) and will stay that way until something pokes it
951 // again. We need to schedule another resume.
952 scheduleResumeTopActivities();
953 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700954 }
955
Craig Mautner23ac33b2013-04-01 16:26:35 -0700956 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700957 String callingPackage, Intent intent, String resolvedType,
958 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700959 IBinder resultTo, String resultWho, int requestCode, int startFlags,
960 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700961 Bundle options, boolean ignoreTargetSecurity, int userId,
962 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700963 // Refuse possible leaked file descriptors
964 if (intent != null && intent.hasFileDescriptors()) {
965 throw new IllegalArgumentException("File descriptors passed in Intent");
966 }
967 boolean componentSpecified = intent.getComponent() != null;
968
969 // Don't modify the client's object!
970 intent = new Intent(intent);
971
972 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700973 ActivityInfo aInfo =
974 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700975
Craig Mautnere0a38842013-12-16 16:14:02 -0800976 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700977 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800978 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700979 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800980 // Cannot start a child activity if the parent is not resumed.
981 return ActivityManager.START_CANCELED;
982 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700983 final int realCallingPid = Binder.getCallingPid();
984 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700985 int callingPid;
986 if (callingUid >= 0) {
987 callingPid = -1;
988 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700989 callingPid = realCallingPid;
990 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700991 } else {
992 callingPid = callingUid = -1;
993 }
994
Craig Mautnere0a38842013-12-16 16:14:02 -0800995 final ActivityStack stack;
996 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800997 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800998 } else {
999 stack = container.mStack;
1000 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001001 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001002 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001003 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001004
1005 final long origId = Binder.clearCallingIdentity();
1006
1007 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001008 (aInfo.applicationInfo.privateFlags
1009 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001010 // This may be a heavy-weight process! Check to see if we already
1011 // have another, different heavy-weight process running.
1012 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1013 if (mService.mHeavyWeightProcess != null &&
1014 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1015 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001016 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 if (caller != null) {
1018 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1019 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001020 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001021 } else {
1022 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001023 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001024 + intent.toString());
1025 ActivityOptions.abort(options);
1026 return ActivityManager.START_PERMISSION_DENIED;
1027 }
1028 }
1029
1030 IIntentSender target = mService.getIntentSenderLocked(
1031 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001032 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001033 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1034 | PendingIntent.FLAG_ONE_SHOT, null);
1035
1036 Intent newIntent = new Intent();
1037 if (requestCode >= 0) {
1038 // Caller is requesting a result.
1039 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1040 }
1041 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1042 new IntentSender(target));
1043 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1044 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1045 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1046 hist.packageName);
1047 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1048 hist.task.taskId);
1049 }
1050 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1051 aInfo.packageName);
1052 newIntent.setFlags(intent.getFlags());
1053 newIntent.setClassName("android",
1054 HeavyWeightSwitcherActivity.class.getName());
1055 intent = newIntent;
1056 resolvedType = null;
1057 caller = null;
1058 callingUid = Binder.getCallingUid();
1059 callingPid = Binder.getCallingPid();
1060 componentSpecified = true;
1061 try {
1062 ResolveInfo rInfo =
1063 AppGlobals.getPackageManager().resolveIntent(
1064 intent, null,
1065 PackageManager.MATCH_DEFAULT_ONLY
1066 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1067 aInfo = rInfo != null ? rInfo.activityInfo : null;
1068 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1069 } catch (RemoteException e) {
1070 aInfo = null;
1071 }
1072 }
1073 }
1074 }
1075
Dianne Hackborn91097de2014-04-04 18:02:06 -07001076 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1077 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001078 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001079 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001080 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001081
Craig Mautner85c11a82014-07-17 12:38:18 -07001082 Binder.restoreCallingIdentity(origId);
1083
Craig Mautnerde4ef022013-04-07 19:01:33 -07001084 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001085 // If the caller also wants to switch to a new configuration,
1086 // do so now. This allows a clean switch, as we are waiting
1087 // for the current activity to pause (so we will not destroy
1088 // it), and have not yet started the next activity.
1089 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1090 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001091 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001092 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001093 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001094 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 }
1096
Craig Mautner23ac33b2013-04-01 16:26:35 -07001097 if (outResult != null) {
1098 outResult.result = res;
1099 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001100 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001101 do {
1102 try {
1103 mService.wait();
1104 } catch (InterruptedException e) {
1105 }
1106 } while (!outResult.timeout && outResult.who == null);
1107 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001108 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001109 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001110 outResult.timeout = false;
1111 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1112 outResult.totalTime = 0;
1113 outResult.thisTime = 0;
1114 } else {
1115 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001116 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001117 do {
1118 try {
1119 mService.wait();
1120 } catch (InterruptedException e) {
1121 }
1122 } while (!outResult.timeout && outResult.who == null);
1123 }
1124 }
1125 }
1126
1127 return res;
1128 }
1129 }
1130
1131 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1132 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1133 Bundle options, int userId) {
1134 if (intents == null) {
1135 throw new NullPointerException("intents is null");
1136 }
1137 if (resolvedTypes == null) {
1138 throw new NullPointerException("resolvedTypes is null");
1139 }
1140 if (intents.length != resolvedTypes.length) {
1141 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1142 }
1143
Craig Mautner23ac33b2013-04-01 16:26:35 -07001144
1145 int callingPid;
1146 if (callingUid >= 0) {
1147 callingPid = -1;
1148 } else if (caller == null) {
1149 callingPid = Binder.getCallingPid();
1150 callingUid = Binder.getCallingUid();
1151 } else {
1152 callingPid = callingUid = -1;
1153 }
1154 final long origId = Binder.clearCallingIdentity();
1155 try {
1156 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001157 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001158 for (int i=0; i<intents.length; i++) {
1159 Intent intent = intents[i];
1160 if (intent == null) {
1161 continue;
1162 }
1163
1164 // Refuse possible leaked file descriptors
1165 if (intent != null && intent.hasFileDescriptors()) {
1166 throw new IllegalArgumentException("File descriptors passed in Intent");
1167 }
1168
1169 boolean componentSpecified = intent.getComponent() != null;
1170
1171 // Don't modify the client's object!
1172 intent = new Intent(intent);
1173
1174 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001175 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001176 // TODO: New, check if this is correct
1177 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1178
1179 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001180 (aInfo.applicationInfo.privateFlags
1181 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001182 throw new IllegalArgumentException(
1183 "FLAG_CANT_SAVE_STATE not supported here");
1184 }
1185
1186 Bundle theseOptions;
1187 if (options != null && i == intents.length-1) {
1188 theseOptions = options;
1189 } else {
1190 theseOptions = null;
1191 }
Craig Mautner6170f732013-04-02 13:05:23 -07001192 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001193 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1194 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001195 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001196 if (res < 0) {
1197 return res;
1198 }
1199
1200 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1201 }
1202 }
1203 } finally {
1204 Binder.restoreCallingIdentity(origId);
1205 }
1206
1207 return ActivityManager.START_SUCCESS;
1208 }
1209
Craig Mautner2420ead2013-04-01 17:13:20 -07001210 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001211 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001212 throws RemoteException {
1213
Craig Mautner2568c3a2015-03-26 14:22:34 -07001214 if (andResume) {
1215 r.startFreezingScreenLocked(app, 0);
1216 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001217
Craig Mautner2568c3a2015-03-26 14:22:34 -07001218 // schedule launch ticks to collect information about slow apps.
1219 r.startLaunchTickingLocked();
1220 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001221
1222 // Have the window manager re-evaluate the orientation of
1223 // the screen based on the new activity order. Note that
1224 // as a result of this, it can call back into the activity
1225 // manager with a new orientation. We don't care about that,
1226 // because the activity is not currently running so we are
1227 // just restarting it anyway.
1228 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001229 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001230 mService.mConfiguration,
1231 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001232 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001233 }
1234
1235 r.app = app;
1236 app.waitingToKill = null;
1237 r.launchCount++;
1238 r.lastLaunchTime = SystemClock.uptimeMillis();
1239
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001240 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001241
1242 int idx = app.activities.indexOf(r);
1243 if (idx < 0) {
1244 app.activities.add(r);
1245 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001246 mService.updateLruProcessLocked(app, true, null);
1247 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001248
Craig Mautner15df08a2015-04-01 12:17:18 -07001249 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001250 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1251 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001252 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001253 }
1254
1255 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001256 try {
1257 if (app.thread == null) {
1258 throw new RemoteException();
1259 }
1260 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001261 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001262 if (andResume) {
1263 results = r.results;
1264 newIntents = r.newIntents;
1265 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001266 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1267 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1268 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 if (andResume) {
1270 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1271 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001272 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001273 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001274 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001275 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001276 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 }
1278 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1279 r.sleeping = false;
1280 r.forceNewConfig = false;
1281 mService.showAskCompatModeDialogLocked(r);
1282 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001283 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1285 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1286 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001287 final String profileFile = mService.mProfileFile;
1288 if (profileFile != null) {
1289 ParcelFileDescriptor profileFd = mService.mProfileFd;
1290 if (profileFd != null) {
1291 try {
1292 profileFd = profileFd.dup();
1293 } catch (IOException e) {
1294 if (profileFd != null) {
1295 try {
1296 profileFd.close();
1297 } catch (IOException o) {
1298 }
1299 profileFd = null;
1300 }
1301 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001302 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001303
1304 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1305 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001306 }
1307 }
1308 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001309
Craig Mautner2568c3a2015-03-26 14:22:34 -07001310 if (andResume) {
1311 app.hasShownUi = true;
1312 app.pendingUiClean = true;
1313 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001314 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001315 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001316 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001317 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001318 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001319 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001320
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001321 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001322 // This may be a heavy-weight process! Note that the package
1323 // manager will ensure that only activity can run in the main
1324 // process of the .apk, which is the only thing that will be
1325 // considered heavy-weight.
1326 if (app.processName.equals(app.info.packageName)) {
1327 if (mService.mHeavyWeightProcess != null
1328 && mService.mHeavyWeightProcess != app) {
1329 Slog.w(TAG, "Starting new heavy weight process " + app
1330 + " when already running "
1331 + mService.mHeavyWeightProcess);
1332 }
1333 mService.mHeavyWeightProcess = app;
1334 Message msg = mService.mHandler.obtainMessage(
1335 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1336 msg.obj = r;
1337 mService.mHandler.sendMessage(msg);
1338 }
1339 }
1340
1341 } catch (RemoteException e) {
1342 if (r.launchFailed) {
1343 // This is the second time we failed -- finish activity
1344 // and give up.
1345 Slog.e(TAG, "Second failure launching "
1346 + r.intent.getComponent().flattenToShortString()
1347 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001348 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001349 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1350 "2nd-crash", false);
1351 return false;
1352 }
1353
1354 // This is the first time we failed -- restart process and
1355 // retry.
1356 app.activities.remove(r);
1357 throw e;
1358 }
1359
1360 r.launchFailed = false;
1361 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001362 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001363 }
1364
1365 if (andResume) {
1366 // As part of the process of launching, ActivityThread also performs
1367 // a resume.
1368 stack.minimalResumeActivityLocked(r);
1369 } else {
1370 // This activity is not starting in the resumed state... which
1371 // should look like we asked it to pause+stop (but remain visible),
1372 // and it has done so and reported back the current icicle and
1373 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001374 if (DEBUG_STATES) Slog.v(TAG_STATES,
1375 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001376 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001377 r.stopped = true;
1378 }
1379
1380 // Launch the new version setup screen if needed. We do this -after-
1381 // launching the initial activity (that is, home), so that it can have
1382 // a chance to initialize itself while in the background, making the
1383 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001384 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001385 mService.startSetupActivityLocked();
1386 }
1387
Dianne Hackborn465fa392014-09-14 14:21:18 -07001388 // Update any services we are bound to that might care about whether
1389 // their client may have activities.
1390 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1391
Craig Mautner2420ead2013-04-01 17:13:20 -07001392 return true;
1393 }
1394
Craig Mautnere79d42682013-04-01 19:01:53 -07001395 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001396 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001397 // Is this activity's application already running?
1398 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001399 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001400
1401 r.task.stack.setLaunchTime(r);
1402
1403 if (app != null && app.thread != null) {
1404 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001405 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1406 || !"android".equals(r.info.packageName)) {
1407 // Don't add this if it is a platform component that is marked
1408 // to run in multiple processes, because this is actually
1409 // part of the framework so doesn't make sense to track as a
1410 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001411 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1412 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001413 }
George Mount2c92c972014-03-20 09:38:23 -07001414 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001415 return;
1416 } catch (RemoteException e) {
1417 Slog.w(TAG, "Exception when starting activity "
1418 + r.intent.getComponent().flattenToShortString(), e);
1419 }
1420
1421 // If a dead object exception was thrown -- fall through to
1422 // restart the application.
1423 }
1424
1425 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001426 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001427 }
1428
Craig Mautner6170f732013-04-02 13:05:23 -07001429 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001430 Intent intent, String resolvedType, ActivityInfo aInfo,
1431 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1432 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001433 int callingPid, int callingUid, String callingPackage,
1434 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001435 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1436 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001437 int err = ActivityManager.START_SUCCESS;
1438
1439 ProcessRecord callerApp = null;
1440 if (caller != null) {
1441 callerApp = mService.getRecordForAppLocked(caller);
1442 if (callerApp != null) {
1443 callingPid = callerApp.pid;
1444 callingUid = callerApp.info.uid;
1445 } else {
1446 Slog.w(TAG, "Unable to find app for caller " + caller
1447 + " (pid=" + callingPid + ") when starting: "
1448 + intent.toString());
1449 err = ActivityManager.START_PERMISSION_DENIED;
1450 }
1451 }
1452
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001453 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1454
Craig Mautner6170f732013-04-02 13:05:23 -07001455 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001456 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001457 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001458 + " on display " + (container == null ? (mFocusedStack == null ?
1459 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1460 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1461 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001462 }
1463
1464 ActivityRecord sourceRecord = null;
1465 ActivityRecord resultRecord = null;
1466 if (resultTo != null) {
1467 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001468 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1469 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001470 if (sourceRecord != null) {
1471 if (requestCode >= 0 && !sourceRecord.finishing) {
1472 resultRecord = sourceRecord;
1473 }
1474 }
1475 }
Craig Mautner6170f732013-04-02 13:05:23 -07001476
Dianne Hackborn91097de2014-04-04 18:02:06 -07001477 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001478
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001479 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001480 // Transfer the result target from the source activity to the new
1481 // one being started, including any failures.
1482 if (requestCode >= 0) {
1483 ActivityOptions.abort(options);
1484 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1485 }
1486 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001487 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1488 resultRecord = null;
1489 }
Craig Mautner6170f732013-04-02 13:05:23 -07001490 resultWho = sourceRecord.resultWho;
1491 requestCode = sourceRecord.requestCode;
1492 sourceRecord.resultTo = null;
1493 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001494 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001495 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001496 if (sourceRecord.launchedFromUid == callingUid) {
1497 // The new activity is being launched from the same uid as the previous
1498 // activity in the flow, and asking to forward its result back to the
1499 // previous. In this case the activity is serving as a trampoline between
1500 // the two, so we also want to update its launchedFromPackage to be the
1501 // same as the previous activity. Note that this is safe, since we know
1502 // these two packages come from the same uid; the caller could just as
1503 // well have supplied that same package name itself. This specifially
1504 // deals with the case of an intent picker/chooser being launched in the app
1505 // flow to redirect to an activity picked by the user, where we want the final
1506 // activity to consider it to have been launched by the previous app activity.
1507 callingPackage = sourceRecord.launchedFromPackage;
1508 }
Craig Mautner6170f732013-04-02 13:05:23 -07001509 }
1510
1511 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1512 // We couldn't find a class that can handle the given Intent.
1513 // That's the end of that!
1514 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1515 }
1516
1517 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1518 // We couldn't find the specific class specified in the Intent.
1519 // Also the end of the line.
1520 err = ActivityManager.START_CLASS_NOT_FOUND;
1521 }
1522
Dianne Hackborn91097de2014-04-04 18:02:06 -07001523 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1524 && sourceRecord.task.voiceSession != null) {
1525 // If this activity is being launched as part of a voice session, we need
1526 // to ensure that it is safe to do so. If the upcoming activity will also
1527 // be part of the voice session, we can only launch it if it has explicitly
1528 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001529 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001530 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1531 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001532 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001533 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1534 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001535 Slog.w(TAG,
1536 "Activity being started in current voice task does not support voice: "
1537 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001538 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1539 }
1540 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001541 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001542 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1543 }
1544 }
1545 }
1546
1547 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1548 // If the caller is starting a new voice session, just make sure the target
1549 // is actually allowing it to run this way.
1550 try {
1551 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1552 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001553 Slog.w(TAG,
1554 "Activity being started in new voice task does not support: "
1555 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001556 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1557 }
1558 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001559 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001560 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1561 }
1562 }
1563
louis_changcd5d1982014-08-01 10:09:08 +08001564 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1565
Craig Mautner6170f732013-04-02 13:05:23 -07001566 if (err != ActivityManager.START_SUCCESS) {
1567 if (resultRecord != null) {
1568 resultStack.sendActivityResultLocked(-1,
1569 resultRecord, resultWho, requestCode,
1570 Activity.RESULT_CANCELED, null);
1571 }
Craig Mautner6170f732013-04-02 13:05:23 -07001572 ActivityOptions.abort(options);
1573 return err;
1574 }
1575
Svetoslav7008b512015-06-24 18:47:07 -07001576 boolean abort = false;
1577
Svet Ganov99b60432015-06-27 13:15:22 -07001578 final int startAnyPerm = mService.checkPermission(
1579 START_ANY_ACTIVITY, callingPid, callingUid);
1580
1581 if (startAnyPerm != PERMISSION_GRANTED) {
1582 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001583 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001584 final int actionRestriction = getActionRestrictionForCallingPackage(
1585 intent.getAction(), callingPackage, callingPid, callingUid);
1586
1587 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1588 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1589 if (resultRecord != null) {
1590 resultStack.sendActivityResultLocked(-1,
1591 resultRecord, resultWho, requestCode,
1592 Activity.RESULT_CANCELED, null);
1593 }
1594 String msg;
1595 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1596 msg = "Permission Denial: starting " + intent.toString()
1597 + " from " + callerApp + " (pid=" + callingPid
1598 + ", uid=" + callingUid + ")" + " with revoked permission "
1599 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1600 } else if (!aInfo.exported) {
1601 msg = "Permission Denial: starting " + intent.toString()
1602 + " from " + callerApp + " (pid=" + callingPid
1603 + ", uid=" + callingUid + ")"
1604 + " not exported from uid " + aInfo.applicationInfo.uid;
1605 } else {
1606 msg = "Permission Denial: starting " + intent.toString()
1607 + " from " + callerApp + " (pid=" + callingPid
1608 + ", uid=" + callingUid + ")"
1609 + " requires " + aInfo.permission;
1610 }
1611 Slog.w(TAG, msg);
1612 throw new SecurityException(msg);
1613 }
1614
1615 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1616 String message = "Appop Denial: starting " + intent.toString()
1617 + " from " + callerApp + " (pid=" + callingPid
1618 + ", uid=" + callingUid + ")"
1619 + " requires " + AppOpsManager.permissionToOp(
1620 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1621 Slog.w(TAG, message);
1622 abort = true;
1623 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1624 String message = "Appop Denial: starting " + intent.toString()
1625 + " from " + callerApp + " (pid=" + callingPid
1626 + ", uid=" + callingUid + ")"
1627 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1628 Slog.w(TAG, message);
1629 abort = true;
1630 }
Svetoslav7008b512015-06-24 18:47:07 -07001631 }
1632
1633 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001634 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001635
Craig Mautner6170f732013-04-02 13:05:23 -07001636 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001637 try {
1638 // The Intent we give to the watcher has the extra data
1639 // stripped off, since it can contain private information.
1640 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001641 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001642 aInfo.applicationInfo.packageName);
1643 } catch (RemoteException e) {
1644 mService.mController = null;
1645 }
Ben Gruver5e207332013-04-03 17:41:37 -07001646 }
Craig Mautner6170f732013-04-02 13:05:23 -07001647
Ben Gruver5e207332013-04-03 17:41:37 -07001648 if (abort) {
1649 if (resultRecord != null) {
1650 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001651 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001652 }
Ben Gruver5e207332013-04-03 17:41:37 -07001653 // We pretend to the caller that it was really started, but
1654 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001655 ActivityOptions.abort(options);
1656 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001657 }
1658
1659 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001660 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001661 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001662 if (outActivity != null) {
1663 outActivity[0] = r;
1664 }
1665
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001666 if (r.appTimeTracker == null && sourceRecord != null) {
1667 // If the caller didn't specify an explicit time tracker, we want to continue
1668 // tracking under any it has.
1669 r.appTimeTracker = sourceRecord.appTimeTracker;
1670 }
1671
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001672 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001673 if (voiceSession == null && (stack.mResumedActivity == null
1674 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001675 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1676 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001677 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001678 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001679 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001680 ActivityOptions.abort(options);
1681 return ActivityManager.START_SWITCHES_CANCELED;
1682 }
1683 }
1684
1685 if (mService.mDidAppSwitch) {
1686 // This is the second allowed switch since we stopped switches,
1687 // so now just generally allow switches. Use case: user presses
1688 // home (switches disabled, switch to home, mDidAppSwitch now true);
1689 // user taps a home icon (coming from home so allowed, we hit here
1690 // and now allow anyone to switch again).
1691 mService.mAppSwitchesAllowedTime = 0;
1692 } else {
1693 mService.mDidAppSwitch = true;
1694 }
1695
Craig Mautneree36c772014-07-16 14:56:05 -07001696 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001697
Dianne Hackborn91097de2014-04-04 18:02:06 -07001698 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001699 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001700
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001701 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001702 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001703 // activity start, but we are not actually doing an activity
1704 // switch... just dismiss the keyguard now, because we
1705 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001706 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001707 }
1708 return err;
1709 }
1710
Svet Ganov99b60432015-06-27 13:15:22 -07001711 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001712 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001713 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1714 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001715 == PackageManager.PERMISSION_DENIED) {
1716 return ACTIVITY_RESTRICTION_PERMISSION;
1717 }
1718
Christopher Tateff7add02015-08-17 10:23:22 -07001719 if (activityInfo.permission == null) {
1720 return ACTIVITY_RESTRICTION_NONE;
1721 }
1722
Svet Ganov99b60432015-06-27 13:15:22 -07001723 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1724 if (opCode == AppOpsManager.OP_NONE) {
1725 return ACTIVITY_RESTRICTION_NONE;
1726 }
1727
1728 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1729 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001730 if (!ignoreTargetSecurity) {
1731 return ACTIVITY_RESTRICTION_APPOP;
1732 }
Svet Ganov99b60432015-06-27 13:15:22 -07001733 }
1734
1735 return ACTIVITY_RESTRICTION_NONE;
1736 }
1737
Svetoslav7008b512015-06-24 18:47:07 -07001738 private int getActionRestrictionForCallingPackage(String action,
1739 String callingPackage, int callingPid, int callingUid) {
1740 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001741 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001742 }
1743
1744 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1745 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001746 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001747 }
1748
1749 final PackageInfo packageInfo;
1750 try {
1751 packageInfo = mService.mContext.getPackageManager()
1752 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1753 } catch (PackageManager.NameNotFoundException e) {
1754 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001755 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001756 }
1757
1758 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001759 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001760 }
1761
1762 if (mService.checkPermission(permission, callingPid, callingUid) ==
1763 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001764 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001765 }
1766
1767 final int opCode = AppOpsManager.permissionToOpCode(permission);
1768 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001769 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001770 }
1771
1772 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1773 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001774 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001775 }
1776
Svet Ganov99b60432015-06-27 13:15:22 -07001777 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001778 }
1779
Chong Zhang0fa656b2015-08-31 15:17:21 -07001780 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001781 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001782
1783 // On leanback only devices we should keep all activities in the same stack.
1784 if (!mLeanbackOnlyDevice &&
1785 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001786
1787 ActivityStack stack;
1788
Wale Ogunwale7d701172015-03-11 15:36:30 -07001789 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001790 stack = task.stack;
1791 if (stack.isOnHomeDisplay()) {
1792 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001793 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1794 "computeStackFocus: Setting " + "focused stack to r=" + r
1795 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001796 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001797 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001798 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001799 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001800 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001801 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001802 }
1803
Craig Mautnere0a38842013-12-16 16:14:02 -08001804 final ActivityContainer container = r.mInitialActivityContainer;
1805 if (container != null) {
1806 // The first time put it on the desired stack, after this put on task stack.
1807 r.mInitialActivityContainer = null;
1808 return container.mStack;
1809 }
1810
Chong Zhanga8be5e52015-10-05 17:06:28 -07001811 // The fullscreen stack can contain any task regardless of if the task is resizeable
1812 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1813 // If the freeform stack has focus, and the activity to be launched is resizeable,
1814 // we can also put it in the focused stack.
1815 final boolean canUseFocusedStack =
1816 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1817 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1818 if (canUseFocusedStack
Wale Ogunwale13a9b162015-09-22 21:38:51 -07001819 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001820 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001821 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001822 return mFocusedStack;
1823 }
1824
Wale Ogunwale706ed792015-08-02 10:29:44 -07001825 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001826 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1827 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001828 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001829 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1830 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001831 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001832 "computeStackFocus: Setting focused stack=" + stack);
1833 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001834 }
1835 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001836
Wale Ogunwale706ed792015-08-02 10:29:44 -07001837 // If there is no suitable dynamic stack then we figure out which static stack to use.
Wale Ogunwale13a9b162015-09-22 21:38:51 -07001838 final int stackId = task != null ? task.getLaunchStackId() :
Chong Zhang0fa656b2015-08-31 15:17:21 -07001839 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1840 FULLSCREEN_WORKSPACE_STACK_ID;
1841 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001842 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1843 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001844 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001845 }
1846 return mHomeStack;
1847 }
1848
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001849 boolean setFocusedStack(ActivityRecord r, String reason) {
1850 if (r == null) {
1851 // Not sure what you are trying to do, but it is not going to work...
1852 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001853 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001854 final TaskRecord task = r.task;
1855 if (task == null || task.stack == null) {
1856 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1857 return false;
1858 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001859 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001860 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001861 }
1862
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001863 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001864 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001865 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001866 final Intent intent = r.intent;
1867 final int callingUid = r.launchedFromUid;
1868
Chong Zhang0fa656b2015-08-31 15:17:21 -07001869 boolean overrideBounds = false;
1870 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001871 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
1872 ActivityOptions opts = new ActivityOptions(options);
1873 if (opts.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001874 overrideBounds = true;
Chong Zhang56f171d2015-10-08 10:39:23 -07001875 newBounds = opts.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001876 }
1877 }
1878
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001879 // In some flows in to this function, we retrieve the task record and hold on to it
1880 // without a lock before calling back in to here... so the task at this point may
1881 // not actually be in recents. Check for that, and if it isn't in recents just
1882 // consider it invalid.
1883 if (inTask != null && !inTask.inRecents) {
1884 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1885 inTask = null;
1886 }
1887
Craig Mautnerad400af2014-08-13 16:41:36 -07001888 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001889 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1890 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001891
Craig Mautnera228ae92014-07-09 05:44:55 -07001892 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001893 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001894 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001895 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1896 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1897 "\"singleInstance\" or \"singleTask\"");
1898 launchFlags &=
1899 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1900 } else {
1901 switch (r.info.documentLaunchMode) {
1902 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1903 break;
1904 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1905 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1906 break;
1907 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1908 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1909 break;
1910 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1911 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1912 break;
1913 }
1914 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001915
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001916 final boolean launchTaskBehind = r.mLaunchTaskBehind
1917 && !launchSingleTask && !launchSingleInstance
1918 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001919
Wale Ogunwale7d701172015-03-11 15:36:30 -07001920 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1921 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001922 // For whatever reason this activity is being launched into a new
1923 // task... yet the caller has requested a result back. Well, that
1924 // is pretty messed up, so instead immediately send back a cancel
1925 // and let the new task continue launched as normal without a
1926 // dependency on its originator.
1927 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1928 r.resultTo.task.stack.sendActivityResultLocked(-1,
1929 r.resultTo, r.resultWho, r.requestCode,
1930 Activity.RESULT_CANCELED, null);
1931 r.resultTo = null;
1932 }
1933
1934 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1935 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1936 }
1937
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001938 // If we are actually going to launch in to a new task, there are some cases where
1939 // we further want to do multiple task.
1940 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1941 if (launchTaskBehind
1942 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1943 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1944 }
1945 }
1946
Craig Mautner8849a5e2013-04-02 16:41:03 -07001947 // We'll invoke onUserLeaving before onPause only if the launching
1948 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001949 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001950 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1951 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001952
1953 // If the caller has asked not to resume at this point, we make note
1954 // of this in the record so that we can skip it when trying to find
1955 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001956 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001957 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001958 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001959 }
1960
Craig Mautnera254cd72014-05-25 16:47:39 -07001961 ActivityRecord notTop =
1962 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963
1964 // If the onlyIfNeeded flag is set, then we can do this if the activity
1965 // being launched is the same as the one making the call... or, as
1966 // a special case, if we do not know the caller then we count the
1967 // current top activity as the caller.
1968 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1969 ActivityRecord checkedCaller = sourceRecord;
1970 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001971 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001972 }
1973 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1974 // Caller is not the same as launcher, so always needed.
1975 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1976 }
1977 }
1978
Craig Mautner8849a5e2013-04-02 16:41:03 -07001979 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001980 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001981
1982 // If the caller is not coming from another activity, but has given us an
1983 // explicit task into which they would like us to launch the new activity,
1984 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001985 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1986 final Intent baseIntent = inTask.getBaseIntent();
1987 final ActivityRecord root = inTask.getRootActivity();
1988 if (baseIntent == null) {
1989 ActivityOptions.abort(options);
1990 throw new IllegalArgumentException("Launching into task without base intent: "
1991 + inTask);
1992 }
1993
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001994 // If this task is empty, then we are adding the first activity -- it
1995 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001996 if (launchSingleInstance || launchSingleTask) {
1997 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1998 ActivityOptions.abort(options);
1999 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2000 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002001 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002002 if (root != null) {
2003 ActivityOptions.abort(options);
2004 throw new IllegalArgumentException("Caller with inTask " + inTask
2005 + " has root " + root + " but target is singleInstance/Task");
2006 }
2007 }
2008
2009 // If task is empty, then adopt the interesting intent launch flags in to the
2010 // activity being started.
2011 if (root == null) {
2012 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2013 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2014 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2015 launchFlags = (launchFlags&~flagsOfInterest)
2016 | (baseIntent.getFlags()&flagsOfInterest);
2017 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002018 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002019 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002020
Dianne Hackborn962d5352014-08-29 16:27:40 -07002021 // If the task is not empty and the caller is asking to start it as the root
2022 // of a new task, then we don't actually want to start this on the task. We
2023 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002024 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002025 addingToTask = false;
2026
2027 } else {
2028 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002029 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002030
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002031 reuseTask = inTask;
2032 } else {
2033 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002034 // Launch ResolverActivity in the source task, so that it stays in the task
2035 // bounds when in freeform workspace.
2036 // Also put noDisplay activities in the source task. These by itself can
2037 // be placed in any task/stack, however it could launch other activities
2038 // like ResolverActivity, and we want those to stay in the original task.
2039 if (r.isResolverActivity() || r.noDisplay) {
2040 addingToTask = true;
2041 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002042 }
2043
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002044 if (inTask == null) {
2045 if (sourceRecord == null) {
2046 // This activity is not being started from another... in this
2047 // case we -always- start a new task.
2048 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2049 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2050 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2051 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2052 }
2053 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2054 // The original activity who is starting us is running as a single
2055 // instance... this new activity it is starting must go on its
2056 // own task.
2057 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2058 } else if (launchSingleInstance || launchSingleTask) {
2059 // The activity being started is a single instance... it always
2060 // gets launched into its own task.
2061 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2062 }
2063 }
2064
2065 ActivityInfo newTaskInfo = null;
2066 Intent newTaskIntent = null;
2067 ActivityStack sourceStack;
2068 if (sourceRecord != null) {
2069 if (sourceRecord.finishing) {
2070 // If the source is finishing, we can't further count it as our source. This
2071 // is because the task it is associated with may now be empty and on its way out,
2072 // so we don't want to blindly throw it in to that task. Instead we will take
2073 // the NEW_TASK flow and try to find a task for it. But save the task information
2074 // so it can be used when creating the new task.
2075 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2076 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2077 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2078 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2079 newTaskInfo = sourceRecord.info;
2080 newTaskIntent = sourceRecord.task.intent;
2081 }
2082 sourceRecord = null;
2083 sourceStack = null;
2084 } else {
2085 sourceStack = sourceRecord.task.stack;
2086 }
2087 } else {
2088 sourceStack = null;
2089 }
2090
2091 boolean movedHome = false;
2092 ActivityStack targetStack;
2093
2094 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002095 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002096
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002097 // We may want to try to place the new activity in to an existing task. We always
2098 // do this if the target activity is singleTask or singleInstance; we will also do
2099 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2100 // us to still place it in a new task: multi task, always doc mode, or being asked to
2101 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002102 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2103 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002104 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002105 // If bring to front is requested, and no result is requested and we have not
2106 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002107 // we can find a task that was started with this same
2108 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002109 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002110 // See if there is a task to bring to the front. If this is
2111 // a SINGLE_INSTANCE activity, there can be one and only one
2112 // instance of it in the history, and it is always in its own
2113 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002114 ActivityRecord intentActivity = !launchSingleInstance ?
2115 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002116 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002117 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2118 // but still needs to be a lock task mode violation since the task gets
2119 // cleared out and the device would otherwise leave the locked task.
2120 if (isLockTaskModeViolation(intentActivity.task,
2121 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2122 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002123 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002124 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002125 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2126 }
Craig Mautner29219d92013-04-16 20:19:12 -07002127 if (r.task == null) {
2128 r.task = intentActivity.task;
2129 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002130 if (intentActivity.task.intent == null) {
2131 // This task was started because of movement of
2132 // the activity based on affinity... now that we
2133 // are actually launching it, we can assign the
2134 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002135 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002136 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002137 targetStack = intentActivity.task.stack;
2138 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002139 // If the target task is not in the front, then we need
2140 // to bring it to the front... except... well, with
2141 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2142 // to have the same behavior as if a new instance was
2143 // being started, which means not bringing it to the front
2144 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002145 final ActivityStack focusStack = getFocusedStack();
2146 ActivityRecord curTop = (focusStack == null)
2147 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002148 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002149 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002150 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002151 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002152 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2153 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002154 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002155 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002156 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2157 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002158 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002159 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002160 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002161 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002162 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002163 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2164 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002165 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002166 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002167 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002168 options = null;
2169 }
2170 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002171 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002172 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002173 + " from " + intentActivity);
2174 targetStack.moveToFront("intentActivityFound");
2175 }
2176
Craig Mautner8849a5e2013-04-02 16:41:03 -07002177 // If the caller has requested that the target task be
2178 // reset, then do so.
2179 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2180 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2181 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002182 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002183 // We don't need to start a new activity, and
2184 // the client said not to do anything if that
2185 // is the case, so this is it! And for paranoia, make
2186 // sure we have correctly resumed the top activity.
2187 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002188 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002189
2190 // Make sure to notify Keyguard as well if we are not running an app
2191 // transition later.
2192 if (!movedToFront) {
2193 notifyActivityDrawnForKeyguard();
2194 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002195 } else {
2196 ActivityOptions.abort(options);
2197 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002198 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002199 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2200 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002201 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002202 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002203 // The caller has requested to completely replace any
2204 // existing task with its new activity. Well that should
2205 // not be too hard...
2206 reuseTask = intentActivity.task;
2207 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002208 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002209 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002210 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002211 // In this situation we want to remove all activities
2212 // from the task up to the one being started. In most
2213 // cases this means we are resetting the task to its
2214 // initial state.
2215 ActivityRecord top =
2216 intentActivity.task.performClearTaskLocked(r, launchFlags);
2217 if (top != null) {
2218 if (top.frontOfTask) {
2219 // Activity aliases may mean we use different
2220 // intents for the top activity, so make sure
2221 // the task now has the identity of the new
2222 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002223 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002224 }
2225 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2226 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002227 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002228 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002229 // A special case: we need to start the activity because it is not
2230 // currently running, and the caller has asked to clear the current
2231 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002232 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002233 // Now pretend like this activity is being started by the top of its
2234 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002235 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002236 TaskRecord task = sourceRecord.task;
2237 if (task != null && task.stack == null) {
2238 // Target stack got cleared when we all activities were removed
2239 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002240 targetStack = computeStackFocus(
2241 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002242 targetStack.addTask(
2243 task, !launchTaskBehind /* toTop */, false /* moving */);
2244 }
2245
Craig Mautner8849a5e2013-04-02 16:41:03 -07002246 }
2247 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2248 // In this case the top activity on the task is the
2249 // same as the one being launched, so we take that
2250 // as a request to bring the task to the foreground.
2251 // If the top activity in the task is the root
2252 // activity, deliver this new intent to it if it
2253 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002254 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002255 && intentActivity.realActivity.equals(r.realActivity)) {
2256 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2257 intentActivity.task);
2258 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002259 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002260 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002261 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2262 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002263 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2264 // In this case we are launching the root activity
2265 // of the task, but with a different intent. We
2266 // should start a new instance on top.
2267 addingToTask = true;
2268 sourceRecord = intentActivity;
2269 }
2270 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2271 // In this case an activity is being launched in to an
2272 // existing task, without resetting that task. This
2273 // is typically the situation of launching an activity
2274 // from a notification or shortcut. We want to place
2275 // the new activity on top of the current task.
2276 addingToTask = true;
2277 sourceRecord = intentActivity;
2278 } else if (!intentActivity.task.rootWasReset) {
2279 // In this case we are launching in to an existing task
2280 // that has not yet been started from its front door.
2281 // The current task has been brought to the front.
2282 // Ideally, we'd probably like to place this new task
2283 // at the bottom of its stack, but that's a little hard
2284 // to do with the current organization of the code so
2285 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002286 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002287 }
2288 if (!addingToTask && reuseTask == null) {
2289 // We didn't do anything... but it was needed (a.k.a., client
2290 // don't use that intent!) And for paranoia, make
2291 // sure we have correctly resumed the top activity.
2292 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002293 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002294 if (!movedToFront) {
2295 // Make sure to notify Keyguard as well if we are not running an app
2296 // transition later.
2297 notifyActivityDrawnForKeyguard();
2298 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002299 } else {
2300 ActivityOptions.abort(options);
2301 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002302 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002303 return ActivityManager.START_TASK_TO_FRONT;
2304 }
2305 }
2306 }
2307 }
2308
2309 //String uri = r.intent.toURI();
2310 //Intent intent2 = new Intent(uri);
2311 //Slog.i(TAG, "Given intent: " + r.intent);
2312 //Slog.i(TAG, "URI is: " + uri);
2313 //Slog.i(TAG, "To intent: " + intent2);
2314
2315 if (r.packageName != null) {
2316 // If the activity being launched is the same as the one currently
2317 // at the top, then we need to check if it should only be launched
2318 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002319 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002320 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002321 if (top != null && r.resultTo == null) {
2322 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2323 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002324 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002325 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002326 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2327 top.task);
2328 // For paranoia, make sure we have correctly
2329 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002330 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002331 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002332 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002333 }
2334 ActivityOptions.abort(options);
2335 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2336 // We don't need to start a new activity, and
2337 // the client said not to do anything if that
2338 // is the case, so this is it!
2339 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2340 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002341 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002342 return ActivityManager.START_DELIVERED_TO_TOP;
2343 }
2344 }
2345 }
2346 }
2347
2348 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002349 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002350 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2351 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002352 }
2353 ActivityOptions.abort(options);
2354 return ActivityManager.START_CLASS_NOT_FOUND;
2355 }
2356
2357 boolean newTask = false;
2358 boolean keepCurTransition = false;
2359
Craig Mautnerbb742462014-07-07 15:28:55 -07002360 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002361 sourceRecord.task : null;
2362
Craig Mautner8849a5e2013-04-02 16:41:03 -07002363 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002364 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002365 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002366 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002367 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002368 if (doResume) {
2369 targetStack.moveToFront("startingNewTask");
2370 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002371
Craig Mautner8849a5e2013-04-02 16:41:03 -07002372 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002373 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2374 newTaskInfo != null ? newTaskInfo : r.info,
2375 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002376 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2377 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002378 if (overrideBounds) {
2379 r.task.updateOverrideConfiguration(newBounds);
2380 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002381 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2382 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002383 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002384 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002385 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002386 if (isLockTaskModeViolation(r.task)) {
2387 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2388 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2389 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002390 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002391 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002392 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2393 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002394 // Caller wants to appear on home activity, so before starting
2395 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002396 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002397 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002398 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002399 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002400 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002401 if (isLockTaskModeViolation(sourceTask)) {
2402 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2403 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2404 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002405 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002406 if (doResume) {
2407 targetStack.moveToFront("sourceStackToFront");
2408 }
Craig Mautner737fae22014-10-15 12:52:10 -07002409 final TaskRecord topTask = targetStack.topTask();
2410 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002411 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002412 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002413 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002414 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002415 // In this case, we are adding the activity to an existing
2416 // task, but the caller has asked to clear that task if the
2417 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002418 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002419 keepCurTransition = true;
2420 if (top != null) {
2421 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002422 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002423 // For paranoia, make sure we have correctly
2424 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002425 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002426 if (doResume) {
2427 targetStack.resumeTopActivityLocked(null);
2428 }
2429 ActivityOptions.abort(options);
2430 return ActivityManager.START_DELIVERED_TO_TOP;
2431 }
2432 } else if (!addingToTask &&
2433 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2434 // In this case, we are launching an activity in our own task
2435 // that may already be running somewhere in the history, and
2436 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002437 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002438 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002439 final TaskRecord task = top.task;
2440 task.moveActivityToFrontLocked(top);
2441 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002442 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002443 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002444 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002445 if (doResume) {
2446 targetStack.resumeTopActivityLocked(null);
2447 }
2448 return ActivityManager.START_DELIVERED_TO_TOP;
2449 }
2450 }
2451 // An existing activity is starting this new activity, so we want
2452 // to keep the new one in the same task as the one that is starting
2453 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002454 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002455 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002456 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002457
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002458 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002459 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002460 // task it has provided to us.
2461 if (isLockTaskModeViolation(inTask)) {
2462 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2463 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2464 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002465 if (overrideBounds) {
2466 inTask.updateOverrideConfiguration(newBounds);
2467 int stackId = inTask.getLaunchStackId();
2468 if (stackId != inTask.stack.mStackId) {
2469 moveTaskToStackUncheckedLocked(
2470 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2471 }
2472 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002473 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002474 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2475 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002476
2477 // Check whether we should actually launch the new activity in to the task,
2478 // or just reuse the current activity on top.
2479 ActivityRecord top = inTask.getTopActivity();
2480 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2481 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2482 || launchSingleTop || launchSingleTask) {
2483 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2484 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2485 // We don't need to start a new activity, and
2486 // the client said not to do anything if that
2487 // is the case, so this is it!
2488 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2489 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002490 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002491 return ActivityManager.START_DELIVERED_TO_TOP;
2492 }
2493 }
2494
Dianne Hackborn962d5352014-08-29 16:27:40 -07002495 if (!addingToTask) {
2496 // We don't actually want to have this activity added to the task, so just
2497 // stop here but still tell the caller that we consumed the intent.
2498 ActivityOptions.abort(options);
2499 return ActivityManager.START_TASK_TO_FRONT;
2500 }
2501
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002502 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002503 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002504 + " in explicit task " + r.task);
2505
Craig Mautner8849a5e2013-04-02 16:41:03 -07002506 } else {
2507 // This not being started from an existing activity, and not part
2508 // of a new task... just put it in the top task, though these days
2509 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002510 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002511 if (doResume) {
2512 targetStack.moveToFront("addingToTopTask");
2513 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002514 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002515 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002516 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002517 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002518 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002519 + " in new guessed " + r.task);
2520 }
2521
2522 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002523 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002524
Craig Mautner76e2a762014-06-24 09:05:43 -07002525 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2526 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2527 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002528 if (newTask) {
2529 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2530 }
2531 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002532 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002533 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002534 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002535 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002536 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002537 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002538 return ActivityManager.START_SUCCESS;
2539 }
2540
Craig Mautneree36c772014-07-16 14:56:05 -07002541 final void doPendingActivityLaunchesLocked(boolean doResume) {
2542 while (!mPendingActivityLaunches.isEmpty()) {
2543 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002544
2545 try {
2546 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
2547 doResume && mPendingActivityLaunches.isEmpty(), null, null);
2548 } catch (Exception e) {
2549 Slog.w(TAG, "Exception during pending activity launch pal=" + pal, e);
2550 }
Craig Mautneree36c772014-07-16 14:56:05 -07002551 }
2552 }
2553
Craig Mautner7f13ed32014-07-28 14:00:35 -07002554 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002555 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2556 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002557 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002558 mPendingActivityLaunches.remove(palNdx);
2559 }
2560 }
2561 }
2562
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002563 void setLaunchSource(int uid) {
2564 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2565 }
2566
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002567 void acquireLaunchWakelock() {
2568 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2569 throw new IllegalStateException("Calling must be system uid");
2570 }
2571 mLaunchingActivity.acquire();
2572 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2573 // To be safe, don't allow the wake lock to be held for too long.
2574 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2575 }
2576 }
2577
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002578 /**
2579 * Called when the frontmost task is idle.
2580 * @return the state of mService.mBooting before this was called.
2581 */
2582 private boolean checkFinishBootingLocked() {
2583 final boolean booting = mService.mBooting;
2584 boolean enableScreen = false;
2585 mService.mBooting = false;
2586 if (!mService.mBooted) {
2587 mService.mBooted = true;
2588 enableScreen = true;
2589 }
2590 if (booting || enableScreen) {
2591 mService.postFinishBooting(booting, enableScreen);
2592 }
2593 return booting;
2594 }
2595
Craig Mautnerf3333272013-04-22 10:55:53 -07002596 // Checked.
2597 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2598 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002599 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002600
Craig Mautnerf3333272013-04-22 10:55:53 -07002601 ArrayList<ActivityRecord> stops = null;
2602 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002603 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002604 int NS = 0;
2605 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002606 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002607 boolean activityRemoved = false;
2608
Wale Ogunwale7d701172015-03-11 15:36:30 -07002609 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002610 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002611 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2612 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002613 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2614 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002615 if (fromTimeout) {
2616 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002617 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002618
2619 // This is a hack to semi-deal with a race condition
2620 // in the client where it can be constructed with a
2621 // newer configuration from when we asked it to launch.
2622 // We'll update with whatever configuration it now says
2623 // it used to launch.
2624 if (config != null) {
2625 r.configuration = config;
2626 }
2627
2628 // We are now idle. If someone is waiting for a thumbnail from
2629 // us, we can now deliver.
2630 r.idle = true;
2631
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002632 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002633 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002634 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002635 }
2636 }
2637
2638 if (allResumedActivitiesIdle()) {
2639 if (r != null) {
2640 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002641 }
2642
2643 if (mLaunchingActivity.isHeld()) {
2644 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2645 if (VALIDATE_WAKE_LOCK_CALLER &&
2646 Binder.getCallingUid() != Process.myUid()) {
2647 throw new IllegalStateException("Calling must be system uid");
2648 }
2649 mLaunchingActivity.release();
2650 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002651 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002652 }
2653
2654 // Atomically retrieve all of the other things to do.
2655 stops = processStoppingActivitiesLocked(true);
2656 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002657 if ((NF = mFinishingActivities.size()) > 0) {
2658 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002659 mFinishingActivities.clear();
2660 }
2661
Craig Mautnerf3333272013-04-22 10:55:53 -07002662 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002663 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002664 mStartingUsers.clear();
2665 }
2666
Craig Mautnerf3333272013-04-22 10:55:53 -07002667 // Stop any activities that are scheduled to do so but have been
2668 // waiting for the next one to start.
2669 for (int i = 0; i < NS; i++) {
2670 r = stops.get(i);
2671 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002672 if (stack != null) {
2673 if (r.finishing) {
2674 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2675 } else {
2676 stack.stopActivityLocked(r);
2677 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002678 }
2679 }
2680
2681 // Finish any activities that are scheduled to do so but have been
2682 // waiting for the next one to start.
2683 for (int i = 0; i < NF; i++) {
2684 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002685 final ActivityStack stack = r.task.stack;
2686 if (stack != null) {
2687 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2688 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002689 }
2690
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002691 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002692 // Complete user switch
2693 if (startingUsers != null) {
2694 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002695 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002696 }
2697 }
2698 // Complete starting up of background users
2699 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002700 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002701 mStartingBackgroundUsers.clear();
2702 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002703 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002704 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002705 }
2706 }
2707
2708 mService.trimApplications();
2709 //dump();
2710 //mWindowManager.dump();
2711
Craig Mautnerf3333272013-04-22 10:55:53 -07002712 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002713 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002714 }
2715
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002716 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002717 }
2718
Craig Mautner8e569572013-10-11 17:36:59 -07002719 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002720 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002721 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2722 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002723 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2724 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2725 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002726 }
Craig Mautner19091252013-10-05 00:03:53 -07002727 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002728 }
2729
2730 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002731 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2732 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002733 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2734 stacks.get(stackNdx).closeSystemDialogsLocked();
2735 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002736 }
2737 }
2738
Craig Mautner93529a42013-10-04 15:03:13 -07002739 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002740 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002741 }
2742
Craig Mautner8d341ef2013-03-26 09:03:27 -07002743 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002744 * Update the last used stack id for non-current user (current user's last
2745 * used stack is the focused stack)
2746 */
2747 void updateUserStackLocked(int userId, ActivityStack stack) {
2748 if (userId != mCurrentUser) {
2749 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2750 }
2751 }
2752
2753 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002754 * @return true if some activity was finished (or would have finished if doit were true).
2755 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002756 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2757 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002758 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002759 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2760 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002761 final int numStacks = stacks.size();
2762 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2763 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002764 if (stack.finishDisabledPackageActivitiesLocked(
2765 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002766 didSomething = true;
2767 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002768 }
2769 }
2770 return didSomething;
2771 }
2772
Dianne Hackborna413dc02013-07-12 12:02:55 -07002773 void updatePreviousProcessLocked(ActivityRecord r) {
2774 // Now that this process has stopped, we may want to consider
2775 // it to be the previous app to try to keep around in case
2776 // the user wants to return to it.
2777
2778 // First, found out what is currently the foreground app, so that
2779 // we don't blow away the previous app if this activity is being
2780 // hosted by the process that is actually still the foreground.
2781 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002782 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2783 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002784 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2785 final ActivityStack stack = stacks.get(stackNdx);
2786 if (isFrontStack(stack)) {
2787 if (stack.mResumedActivity != null) {
2788 fgApp = stack.mResumedActivity.app;
2789 } else if (stack.mPausingActivity != null) {
2790 fgApp = stack.mPausingActivity.app;
2791 }
2792 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002793 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002794 }
2795 }
2796
2797 // Now set this one as the previous process, only if that really
2798 // makes sense to.
2799 if (r.app != null && fgApp != null && r.app != fgApp
2800 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002801 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002802 mService.mPreviousProcess = r.app;
2803 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2804 }
2805 }
2806
Craig Mautner05d29032013-05-03 13:40:13 -07002807 boolean resumeTopActivitiesLocked() {
2808 return resumeTopActivitiesLocked(null, null, null);
2809 }
2810
2811 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2812 Bundle targetOptions) {
2813 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002814 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002815 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002816 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002817 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002818 if (isFrontStack(targetStack)) {
2819 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2820 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002821
Craig Mautnere0a38842013-12-16 16:14:02 -08002822 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2823 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002824 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2825 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002826 if (stack == targetStack) {
2827 // Already started above.
2828 continue;
2829 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002830 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002831 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002832 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002833 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002834 }
Craig Mautner05d29032013-05-03 13:40:13 -07002835 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002836 }
2837
Todd Kennedy539db512014-12-15 09:57:55 -08002838 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002839 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2840 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002841 final int numStacks = stacks.size();
2842 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2843 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002844 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002845 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002846 }
2847 }
2848
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002849 void finishVoiceTask(IVoiceInteractionSession session) {
2850 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2851 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2852 final int numStacks = stacks.size();
2853 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2854 final ActivityStack stack = stacks.get(stackNdx);
2855 stack.finishVoiceTask(session);
2856 }
2857 }
2858 }
2859
Craig Mautner299f9602015-01-26 09:47:33 -08002860 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002861 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2862 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002863 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002864 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2865 // Caller wants the home activity moved with it. To accomplish this,
2866 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002867 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002868 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002869 if (task.stack == null) {
2870 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2871 + task + " to front. Stack is null");
2872 return;
2873 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002874
2875 int stackId = task.stack.mStackId;
2876 if (task.mResizeable && options != null) {
2877 ActivityOptions opts = new ActivityOptions(options);
2878 if (opts.hasBounds()) {
2879 Rect bounds = opts.getBounds();
2880 task.updateOverrideConfiguration(bounds);
Chong Zhang87b21722015-09-21 15:39:51 -07002881 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2882 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002883 stackId = task.getLaunchStackId();
2884 }
2885 }
2886
2887 if (stackId != task.stack.mStackId) {
2888 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, FORCE_FOCUS, reason);
2889 } else {
2890 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002891 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2892 reason);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002893 }
2894
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002895 if (DEBUG_STACK) Slog.d(TAG_STACK,
2896 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002897 }
2898
Craig Mautner967212c2013-04-13 21:10:58 -07002899 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002900 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002901 }
2902
2903 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002904 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2905 if (activityContainer != null) {
2906 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002907 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002908 if (!createStaticStackIfNeeded
2909 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2910 return null;
2911 }
2912 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002913 }
2914
Craig Mautner967212c2013-04-13 21:10:58 -07002915 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002916 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002917 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2918 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002919 }
2920 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002921 }
2922
Craig Mautner4a1cb222013-12-04 16:14:06 -08002923 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002924 ActivityRecord homeActivity = getHomeActivity();
2925 if (homeActivity != null) {
2926 return homeActivity.appToken;
2927 }
2928 return null;
2929 }
2930
2931 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002932 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002933 }
2934
2935 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002936 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2937 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2938 final TaskRecord task = tasks.get(taskNdx);
2939 if (task.isHomeTask()) {
2940 final ArrayList<ActivityRecord> activities = task.mActivities;
2941 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2942 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002943 if (r.isHomeActivity()
2944 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002945 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 }
2947 }
2948 }
2949 }
2950 return null;
2951 }
2952
Todd Kennedyca4d8422015-01-15 15:19:22 -08002953 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002954 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002955 ActivityContainer activityContainer =
2956 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002957 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002958 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2959 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002960 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002961 return activityContainer;
2962 }
2963
Craig Mautner34b73df2014-01-12 21:11:08 -08002964 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002965 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2966 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2967 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002968 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2969 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002970 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002971 }
2972 }
2973
Craig Mautner95da1082014-02-24 17:54:35 -08002974 void deleteActivityContainer(IActivityContainer container) {
2975 ActivityContainer activityContainer = (ActivityContainer)container;
2976 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002977 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2978 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002979 final int stackId = activityContainer.mStackId;
2980 mActivityContainers.remove(stackId);
2981 mWindowManager.removeStack(stackId);
2982 }
2983 }
2984
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002985 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
2986 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002987 final ActivityStack stack = getStack(stackId);
2988 if (stack == null) {
2989 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2990 return;
2991 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002992
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002993 if (!allowResizeInDockedMode
2994 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
2995 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2996 // stack size changing so things don't get out of sync.
2997 return;
2998 }
2999
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003000 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
3001
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003002 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003003
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003004 mTmpBounds.clear();
3005 mTmpConfigs.clear();
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003006 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3007 for (int i = tasks.size() - 1; i >= 0; i--) {
3008 TaskRecord task = tasks.get(i);
3009 if (task.mResizeable) {
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003010 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003011 // For freeform stack we don't adjust the size of the tasks to match that
3012 // of the stack, but we do try to make sure the tasks are still contained
3013 // with the bounds of the stack.
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07003014 tempRect2.set(task.mBounds);
3015 fitWithinBounds(tempRect2, bounds);
3016 task.updateOverrideConfiguration(tempRect2);
3017 } else {
3018 task.updateOverrideConfiguration(bounds);
3019 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003020 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003021
3022 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3023 mTmpBounds.put(task.taskId, task.mBounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003024 }
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07003025 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003026 if (stack.mStackId == DOCKED_STACK_ID) {
3027 // Dock stack funness...Yay!
3028 if (stack.mFullscreen) {
3029 // The dock stack went fullscreen which is kinda like dismissing it.
3030 // In this case we make all other static stacks fullscreen and move all
3031 // docked stack tasks to the fullscreen stack.
3032 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwalea4e86602015-10-08 18:09:02 -07003033 if (i != DOCKED_STACK_ID && getStack(i) != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003034 resizeStackLocked(i, null, preserveWindows, true);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003035 }
3036 }
3037
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003038 final int count = tasks.size();
3039 for (int i = 0; i < count; i++) {
3040 moveTaskToStackLocked(tasks.get(i).taskId,
3041 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
3042 }
3043
3044 // stack shouldn't contain anymore activities, so nothing to resume.
3045 r = null;
3046 } else {
3047 // Docked stacks occupy a dedicated region on screen so the size of all other
3048 // static stacks need to be adjusted so they don't overlap with the docked stack.
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003049 // We get the bounds to use from window manager which has been adjusted for any
3050 // screen controls and is also the same for all stacks.
3051 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003052
3053 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3054 if (i != DOCKED_STACK_ID) {
3055 ActivityStack otherStack = getStack(i);
3056 if (otherStack != null) {
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003057 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003058 }
3059 }
3060 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003061 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003062 // Since we are resizing the stack, all other operations should strive to preserve
3063 // windows.
3064 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003065 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003066 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003067
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003068 if (r != null) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003069 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003070 // And we need to make sure at this point that all other activities
3071 // are made visible with the correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003072 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003073 if (!updated) {
3074 resumeTopActivitiesLocked(stack, null, null);
3075 }
3076 }
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003077
3078 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003079 }
3080
Chong Zhang6de2ae82015-09-30 18:25:21 -07003081 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003082 if (!task.mResizeable) {
3083 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3084 return;
3085 }
3086
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003087 adjustForMinimalTaskDimensions(task, bounds);
3088
Chong Zhang87b21722015-09-21 15:39:51 -07003089 // If this is a forced resize, let it go through even if the bounds is not changing,
3090 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003091 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003092 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003093 // Nothing to do here...
3094 return;
3095 }
3096
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003097 if (!mWindowManager.isValidTaskId(task.taskId)) {
3098 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003099 // All we can do for now is update the bounds so it can be used when the task is
3100 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003101 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003102 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3103 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003104 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003105 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003106 return;
3107 }
3108
Chong Zhang6de2ae82015-09-30 18:25:21 -07003109 // Do not move the task to another stack here.
3110 // This method assumes that the task is already placed in the right stack.
3111 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003112
Chong Zhang6de2ae82015-09-30 18:25:21 -07003113 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003114
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003115 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003116 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003117 // way and the activity was kept the way it was. If it's false, it means the activity had
3118 // to be relaunched due to configuration change.
3119 boolean kept = true;
3120 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003121 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003122 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003123 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003124 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003125 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003126 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003127 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003128 resumeTopActivitiesLocked(stack, null, null);
3129 }
3130 }
3131 }
Chong Zhang87b21722015-09-21 15:39:51 -07003132 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003133
3134 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003135 }
3136
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003137 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3138 if (bounds == null) {
3139 return;
3140 }
3141 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3142 : task.mMinimalSize;
3143 final boolean adjustWidth = minimalSize > bounds.width();
3144 final boolean adjustHeight = minimalSize > bounds.height();
3145 if (!(adjustWidth || adjustHeight)) {
3146 return;
3147 }
3148 Rect taskBounds = task.mBounds;
3149 if (adjustWidth) {
3150 if (taskBounds != null && bounds.right == taskBounds.right) {
3151 bounds.left = bounds.right - minimalSize;
3152 } else {
3153 // Either left bounds match, or neither match, or the previous bounds were
3154 // fullscreen and we default to keeping left.
3155 bounds.right = bounds.left + minimalSize;
3156 }
3157 }
3158 if (adjustHeight) {
3159 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3160 bounds.top = bounds.bottom - minimalSize;
3161 } else {
3162 // Either top bounds match, or neither match, or the previous bounds were
3163 // fullscreen and we default to keeping top.
3164 bounds.bottom = bounds.top + minimalSize;
3165 }
3166 }
3167 }
3168
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003169 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003170 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3171 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003172 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003173 }
3174
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003175 ActivityContainer activityContainer = new ActivityContainer(stackId);
3176 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003177 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003178 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003179 }
3180
3181 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003182 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003183 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3184 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003185 break;
3186 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003187 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003188 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003189 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003190 }
3191
Chong Zhang5dcb2752015-08-18 13:50:26 -07003192 /**
3193 * Restores a recent task to a stack
3194 * @param task The recent task to be restored.
3195 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003196 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003197 * @return true if the task has been restored successfully.
3198 */
3199 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3200 if (stackId == INVALID_STACK_ID) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07003201 stackId = mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003202 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003203 if (task.stack != null) {
3204 // Task has already been restored once. See if we need to do anything more
3205 if (task.stack.mStackId == stackId) {
3206 // Nothing else to do since it is already restored in the right stack.
3207 return true;
3208 }
3209 // Remove current stack association, so we can re-associate the task with the
3210 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003211 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003212 }
3213
3214 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003215 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003216
3217 if (stack == null) {
3218 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003219 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3220 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003221 return false;
3222 }
3223
3224 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003225 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3226 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003227 final ArrayList<ActivityRecord> activities = task.mActivities;
3228 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003229 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003230 }
3231 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003232 }
3233
Wale Ogunwale040b4702015-08-06 18:10:50 -07003234 /**
3235 * Moves the specified task record to the input stack id.
3236 * WARNING: This method performs an unchecked/raw move of the task and
3237 * can leave the system in an unstable state if used incorrectly.
3238 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3239 * to a stack.
3240 * @param task Task to move.
3241 * @param stackId Id of stack to move task to.
3242 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003243 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003244 * @param reason Reason the task is been moved.
3245 * @return The stack the task was moved to.
3246 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003247 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003248 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3249 final ActivityRecord r = task.getTopActivity();
3250 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3251 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3252
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003253 final boolean resizeable = task.mResizeable;
3254 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3255 // if a docked stack is created below which will lead to the stack we are moving from and
3256 // its resizeable tasks being resized.
3257 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003258 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003259 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003260 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3261 if (task.stack != null) {
3262 task.stack.removeTask(task, reason, MOVING);
3263 }
3264 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003265
3266 // If the task had focus before (or we're requested to move focus),
3267 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003268 stack.setFocusAndResumeStateIfNeeded(
3269 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003270
Wale Ogunwale040b4702015-08-06 18:10:50 -07003271 return stack;
3272 }
3273
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003274 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003275 final TaskRecord task = anyTaskForIdLocked(taskId);
3276 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003277 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003278 return;
3279 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003280 final String reason = "moveTaskToStack";
Wale Ogunwale99db1862015-10-23 20:08:22 -07003281 if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID
3282 || stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003283 // We are about to relaunch the activity because its configuration changed due to
3284 // being maximized, i.e. size change. The activity will first remove the old window
3285 // and then add a new one. This call will tell window manager about this, so it can
3286 // preserve the old window until the new one is drawn. This prevents having a gap
3287 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003288 // entrance of the new window to be properly animated.
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003289 ActivityRecord r = task.getTopActivity();
3290 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3291 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003292 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003293 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003294
3295 // Make sure the task has the appropriate bounds/size for the stack it is in.
3296 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003297 resizeTaskLocked(task, stack.mBounds,
3298 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003299 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3300 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003301 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3302 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003303 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003304 resizeTaskLocked(task, stack.mBounds,
3305 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003306 }
3307
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003308 // The task might have already been running and its visibility needs to be synchronized with
3309 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003310 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003311 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003312 }
3313
Wale Ogunwale079a0042015-10-24 11:44:07 -07003314 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3315 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3316 if (stack == null) {
3317 throw new IllegalArgumentException(
3318 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3319 }
3320
3321 final ActivityRecord r = stack.topRunningActivityLocked();
3322 if (r == null) {
3323 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3324 + " in stack=" + stack);
3325 return false;
3326 }
3327
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003328 if (!r.info.supportsPip) {
3329 Slog.w(TAG,
3330 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003331 + " r=" + r);
3332 return false;
3333 }
3334
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003335 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003336 if (task.mActivities.size() == 1) {
3337 // There is only one activity in the task. So, we can just move the task over to the
3338 // pinned stack without re-parenting the activity in a different task.
3339 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS);
3340 } else {
3341 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3342 pinnedStack.moveActivityToStack(r);
3343 }
3344
3345 resizeStackLocked(PINNED_STACK_ID, bounds, PRESERVE_WINDOWS, true);
3346
3347 // The task might have already been running and its visibility needs to be synchronized with
3348 // the visibility of the stack / windows.
3349 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3350 resumeTopActivitiesLocked();
3351 return true;
3352 }
3353
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003354 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3355 final TaskRecord task = anyTaskForIdLocked(taskId);
3356 if (task == null) {
3357 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3358 return;
3359 }
3360 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003361 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003362 mWindowManager.positionTaskInStack(taskId, stackId, position);
3363 final boolean stackChanged = task.stack != null && task.stack != stack;
3364 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003365 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003366 }
3367 stack.positionTask(task, position, stackChanged);
3368 // The task might have already been running and its visibility needs to be synchronized with
3369 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003370 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003371 resumeTopActivitiesLocked();
3372 }
3373
Craig Mautnerac6f8432013-07-17 13:24:59 -07003374 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003375 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003376 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3377 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003378 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3379 final ActivityStack stack = stacks.get(stackNdx);
3380 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003381 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003382 continue;
3383 }
3384 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003385 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3386 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003387 continue;
3388 }
3389 final ActivityRecord ar = stack.findTaskLocked(r);
3390 if (ar != null) {
3391 return ar;
3392 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003393 }
3394 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003395 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003396 return null;
3397 }
3398
3399 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003400 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3401 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003402 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3403 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3404 if (ar != null) {
3405 return ar;
3406 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003407 }
3408 }
3409 return null;
3410 }
3411
Craig Mautner8d341ef2013-03-26 09:03:27 -07003412 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003413 scheduleSleepTimeout();
3414 if (!mGoingToSleep.isHeld()) {
3415 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003416 if (mLaunchingActivity.isHeld()) {
3417 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3418 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003419 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003420 mLaunchingActivity.release();
3421 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003422 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003423 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003424 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003425 }
3426
3427 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003428 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003429
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003430 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003431 final long endTime = System.currentTimeMillis() + timeout;
3432 while (true) {
3433 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003434 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3435 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003436 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3437 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3438 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003439 }
3440 if (cantShutdown) {
3441 long timeRemaining = endTime - System.currentTimeMillis();
3442 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003443 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003444 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003445 } catch (InterruptedException e) {
3446 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003447 } else {
3448 Slog.w(TAG, "Activity manager shutdown timed out");
3449 timedout = true;
3450 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003451 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003452 } else {
3453 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003454 }
3455 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003456
3457 // Force checkReadyForSleep to complete.
3458 mSleepTimeout = true;
3459 checkReadyForSleepLocked();
3460
Craig Mautner8d341ef2013-03-26 09:03:27 -07003461 return timedout;
3462 }
3463
3464 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003465 removeSleepTimeouts();
3466 if (mGoingToSleep.isHeld()) {
3467 mGoingToSleep.release();
3468 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003469 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3470 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3472 final ActivityStack stack = stacks.get(stackNdx);
3473 stack.awakeFromSleepingLocked();
3474 if (isFrontStack(stack)) {
3475 resumeTopActivitiesLocked();
3476 }
Craig Mautner5314a402013-09-26 12:40:16 -07003477 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003478 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003479 mGoingToSleepActivities.clear();
3480 }
3481
3482 void activitySleptLocked(ActivityRecord r) {
3483 mGoingToSleepActivities.remove(r);
3484 checkReadyForSleepLocked();
3485 }
3486
3487 void checkReadyForSleepLocked() {
3488 if (!mService.isSleepingOrShuttingDown()) {
3489 // Do not care.
3490 return;
3491 }
3492
3493 if (!mSleepTimeout) {
3494 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003495 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3496 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003497 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3498 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3499 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003500 }
3501
3502 if (mStoppingActivities.size() > 0) {
3503 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003504 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003505 + mStoppingActivities.size() + " activities");
3506 scheduleIdleLocked();
3507 dontSleep = true;
3508 }
3509
3510 if (mGoingToSleepActivities.size() > 0) {
3511 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003512 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003513 + mGoingToSleepActivities.size() + " activities");
3514 dontSleep = true;
3515 }
3516
3517 if (dontSleep) {
3518 return;
3519 }
3520 }
3521
Craig Mautnere0a38842013-12-16 16:14:02 -08003522 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3523 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003524 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3525 stacks.get(stackNdx).goToSleep();
3526 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003527 }
3528
3529 removeSleepTimeouts();
3530
3531 if (mGoingToSleep.isHeld()) {
3532 mGoingToSleep.release();
3533 }
3534 if (mService.mShuttingDown) {
3535 mService.notifyAll();
3536 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003537 }
3538
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003539 boolean reportResumedActivityLocked(ActivityRecord r) {
3540 final ActivityStack stack = r.task.stack;
3541 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003542 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003543 }
3544 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003545 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003546 mWindowManager.executeAppTransition();
3547 return true;
3548 }
3549 return false;
3550 }
3551
Craig Mautner8d341ef2013-03-26 09:03:27 -07003552 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003553 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3554 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003555 int stackNdx = stacks.size() - 1;
3556 while (stackNdx >= 0) {
3557 stacks.get(stackNdx).handleAppCrashLocked(app);
3558 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003559 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003560 }
3561 }
3562
Jose Lima4b6c6692014-08-12 17:41:12 -07003563 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003564 final ActivityStack stack = r.task.stack;
3565 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003566 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3567 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003568 return false;
3569 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003570 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003571 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3572 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003573
3574 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003575 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003576 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003577 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003578 return true;
3579 }
3580
3581 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003582 if (visible && top.fullscreen) {
3583 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003584 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3585 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3586 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3587 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003588 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003589 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3590 // Only the activity set as currently visible behind should actively reset its
3591 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003592 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3593 "requestVisibleBehind: returning visible=" + visible
3594 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3595 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003596 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003597 }
3598
Jose Lima4b6c6692014-08-12 17:41:12 -07003599 stack.setVisibleBehindActivity(visible ? r : null);
3600 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003601 // Make the activity immediately above r opaque.
3602 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3603 if (next != null) {
3604 mService.convertFromTranslucent(next.appToken);
3605 }
3606 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003607 if (top.app != null && top.app.thread != null) {
3608 // Notify the top app of the change.
3609 try {
3610 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3611 } catch (RemoteException e) {
3612 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003613 }
3614 return true;
3615 }
3616
Craig Mautnerbb742462014-07-07 15:28:55 -07003617 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3618 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3619 r.mLaunchTaskBehind = false;
3620 final TaskRecord task = r.task;
3621 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003622 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003623 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003624 mWindowManager.setAppVisibility(r.appToken, false);
3625 }
3626
3627 void scheduleLaunchTaskBehindComplete(IBinder token) {
3628 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3629 }
3630
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003631 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3632 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003633 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003634 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3635 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003636 final int topStackNdx = stacks.size() - 1;
3637 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3638 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003639 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003640 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003641 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003642 }
3643
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003644 void invalidateTaskLayers() {
3645 mTaskLayersChanged = true;
3646 }
3647
3648 void rankTaskLayersIfNeeded() {
3649 if (!mTaskLayersChanged) {
3650 return;
3651 }
3652 mTaskLayersChanged = false;
3653 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3654 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3655 int baseLayer = 0;
3656 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3657 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3658 }
3659 }
3660 }
3661
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003662 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3663 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3664 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3665 final int topStackNdx = stacks.size() - 1;
3666 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3667 final ActivityStack stack = stacks.get(stackNdx);
3668 stack.clearOtherAppTimeTrackers(except);
3669 }
3670 }
3671 }
3672
Craig Mautner8d341ef2013-03-26 09:03:27 -07003673 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3675 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003676 final int numStacks = stacks.size();
3677 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3678 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003679 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003680 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003681 }
3682 }
3683
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003684 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3685 // Examine all activities currently running in the process.
3686 TaskRecord firstTask = null;
3687 // Tasks is non-null only if two or more tasks are found.
3688 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003689 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3690 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003691 ActivityRecord r = app.activities.get(i);
3692 // First, if we find an activity that is in the process of being destroyed,
3693 // then we just aren't going to do anything for now; we want things to settle
3694 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003695 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003696 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003697 return;
3698 }
3699 // Don't consider any activies that are currently not in a state where they
3700 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003701 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3702 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003703 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003704 continue;
3705 }
3706 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003707 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003708 + " from " + r);
3709 if (firstTask == null) {
3710 firstTask = r.task;
3711 } else if (firstTask != r.task) {
3712 if (tasks == null) {
3713 tasks = new ArraySet<>();
3714 tasks.add(firstTask);
3715 }
3716 tasks.add(r.task);
3717 }
3718 }
3719 }
3720 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003721 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003722 return;
3723 }
3724 // If we have activities in multiple tasks that are in a position to be destroyed,
3725 // let's iterate through the tasks and release the oldest one.
3726 final int numDisplays = mActivityDisplays.size();
3727 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3728 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3729 // Step through all stacks starting from behind, to hit the oldest things first.
3730 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3731 final ActivityStack stack = stacks.get(stackNdx);
3732 // Try to release activities in this stack; if we manage to, we are done.
3733 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3734 return;
3735 }
3736 }
3737 }
3738 }
3739
Amith Yamasani37a40c22015-06-17 13:25:42 -07003740 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003741 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003742 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003743 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003744
Craig Mautner858d8a62013-04-23 17:08:34 -07003745 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003746 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3747 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003748 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003749 final ActivityStack stack = stacks.get(stackNdx);
3750 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003751 TaskRecord task = stack.topTask();
3752 if (task != null) {
3753 mWindowManager.moveTaskToTop(task.taskId);
3754 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003755 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003756 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003757
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003758 ActivityStack stack = getStack(restoreStackId);
3759 if (stack == null) {
3760 stack = mHomeStack;
3761 }
3762 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003763 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003764 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003765 } else {
3766 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003767 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003768 }
Craig Mautner93529a42013-10-04 15:03:13 -07003769 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003770 }
3771
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003772 /**
3773 * Add background users to send boot completed events to.
3774 * @param userId The user being started in the background
3775 * @param uss The state object for the user.
3776 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003777 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003778 mStartingBackgroundUsers.add(uss);
3779 }
3780
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003781 /** Checks whether the userid is a profile of the current user. */
3782 boolean isCurrentProfileLocked(int userId) {
3783 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003784 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003785 }
3786
Chong Zhang45c25ce2015-08-10 22:18:26 -07003787 /** Checks whether the activity should be shown for current user. */
3788 boolean okToShowLocked(ActivityRecord r) {
3789 return r != null && (isCurrentProfileLocked(r.userId)
3790 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3791 }
3792
Craig Mautnerde4ef022013-04-07 19:01:33 -07003793 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003794 ArrayList<ActivityRecord> stops = null;
3795
3796 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003797 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3798 ActivityRecord s = mStoppingActivities.get(activityNdx);
3799 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003800 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003801 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3802 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003803 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003804 if (s.finishing) {
3805 // If this activity is finishing, it is sitting on top of
3806 // everyone else but we now know it is no longer needed...
3807 // so get rid of it. Otherwise, we need to go through the
3808 // normal flow and hide it once we determine that it is
3809 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003810 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003811 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003812 }
3813 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003814 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003815 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003816 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003817 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003818 }
3819 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003820 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003821 }
3822 }
3823
3824 return stops;
3825 }
3826
Craig Mautnercf910b02013-04-23 11:23:27 -07003827 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003828 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3829 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3830 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3831 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003832 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003833 final ActivityState state = r == null ? DESTROYED : r.state;
3834 if (isFrontStack(stack)) {
3835 if (r == null) Slog.e(TAG,
3836 "validateTop...: null top activity, stack=" + stack);
3837 else {
3838 final ActivityRecord pausing = stack.mPausingActivity;
3839 if (pausing != null && pausing == r) Slog.e(TAG,
3840 "validateTop...: top stack has pausing activity r=" + r
3841 + " state=" + state);
3842 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3843 "validateTop...: activity in front not resumed r=" + r
3844 + " state=" + state);
3845 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003846 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003847 final ActivityRecord resumed = stack.mResumedActivity;
3848 if (resumed != null && resumed == r) Slog.e(TAG,
3849 "validateTop...: back stack has resumed activity r=" + r
3850 + " state=" + state);
3851 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3852 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003853 }
3854 }
3855 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003856 }
3857
Craig Mautnere0570202015-05-13 13:06:11 -07003858 private String lockTaskModeToString() {
3859 switch (mLockTaskModeState) {
3860 case LOCK_TASK_MODE_LOCKED:
3861 return "LOCKED";
3862 case LOCK_TASK_MODE_PINNED:
3863 return "PINNED";
3864 case LOCK_TASK_MODE_NONE:
3865 return "NONE";
3866 default: return "unknown=" + mLockTaskModeState;
3867 }
3868 }
3869
Craig Mautner27084302013-03-25 08:05:25 -07003870 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003871 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003872 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003873 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3874 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3875 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003876 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003877 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3878 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3879 if (packages.size() > 0) {
3880 pw.println(" mLockTaskPackages (userId:packages)=");
3881 for (int i = 0; i < packages.size(); ++i) {
3882 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3883 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3884 }
3885 }
3886 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003887 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003888
Craig Mautner20e72272013-04-01 13:45:53 -07003889 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003890 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003891 }
3892
Dianne Hackborn390517b2013-05-30 15:03:32 -07003893 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3894 boolean needSep, String prefix) {
3895 if (activity != null) {
3896 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3897 if (needSep) {
3898 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003899 }
3900 pw.print(prefix);
3901 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003902 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003903 }
3904 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003905 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003906 }
3907
Craig Mautner8d341ef2013-03-26 09:03:27 -07003908 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3909 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003910 boolean printed = false;
3911 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003912 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3913 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003914 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003915 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003916 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003917 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003918 final ActivityStack stack = stacks.get(stackNdx);
3919 StringBuilder stackHeader = new StringBuilder(128);
3920 stackHeader.append(" Stack #");
3921 stackHeader.append(stack.mStackId);
3922 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003923 stackHeader.append("\n");
3924 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3925 stackHeader.append("\n");
3926 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003927 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3928 needSep, stackHeader.toString());
3929 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3930 !dumpAll, false, dumpPackage, true,
3931 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003932
Craig Mautner4a1cb222013-12-04 16:14:06 -08003933 needSep = printed;
3934 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3935 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003936 if (pr) {
3937 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003938 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003939 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003940 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3941 " mResumedActivity: ");
3942 if (pr) {
3943 printed = true;
3944 needSep = false;
3945 }
3946 if (dumpAll) {
3947 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3948 " mLastPausedActivity: ");
3949 if (pr) {
3950 printed = true;
3951 needSep = true;
3952 }
3953 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3954 needSep, " mLastNoHistoryActivity: ");
3955 }
3956 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003957 }
3958 }
3959
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003960 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3961 false, dumpPackage, true, " Activities waiting to finish:", null);
3962 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3963 false, dumpPackage, true, " Activities waiting to stop:", null);
3964 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3965 false, dumpPackage, true, " Activities waiting for another to become visible:",
3966 null);
3967 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3968 false, dumpPackage, true, " Activities waiting to sleep:", null);
3969 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3970 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003971
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003972 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003973 }
3974
Dianne Hackborn390517b2013-05-30 15:03:32 -07003975 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003976 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003977 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003978 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003979 String innerPrefix = null;
3980 String[] args = null;
3981 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003982 for (int i=list.size()-1; i>=0; i--) {
3983 final ActivityRecord r = list.get(i);
3984 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3985 continue;
3986 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003987 if (innerPrefix == null) {
3988 innerPrefix = prefix + " ";
3989 args = new String[0];
3990 }
3991 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003992 final boolean full = !brief && (complete || !r.isInHistory());
3993 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003994 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003995 needNL = false;
3996 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003997 if (header1 != null) {
3998 pw.println(header1);
3999 header1 = null;
4000 }
4001 if (header2 != null) {
4002 pw.println(header2);
4003 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004004 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004005 if (lastTask != r.task) {
4006 lastTask = r.task;
4007 pw.print(prefix);
4008 pw.print(full ? "* " : " ");
4009 pw.println(lastTask);
4010 if (full) {
4011 lastTask.dump(pw, prefix + " ");
4012 } else if (complete) {
4013 // Complete + brief == give a summary. Isn't that obvious?!?
4014 if (lastTask.intent != null) {
4015 pw.print(prefix); pw.print(" ");
4016 pw.println(lastTask.intent.toInsecureStringWithClip());
4017 }
4018 }
4019 }
4020 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4021 pw.print(" #"); pw.print(i); pw.print(": ");
4022 pw.println(r);
4023 if (full) {
4024 r.dump(pw, innerPrefix);
4025 } else if (complete) {
4026 // Complete + brief == give a summary. Isn't that obvious?!?
4027 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4028 if (r.app != null) {
4029 pw.print(innerPrefix); pw.println(r.app);
4030 }
4031 }
4032 if (client && r.app != null && r.app.thread != null) {
4033 // flush anything that is already in the PrintWriter since the thread is going
4034 // to write to the file descriptor directly
4035 pw.flush();
4036 try {
4037 TransferPipe tp = new TransferPipe();
4038 try {
4039 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4040 r.appToken, innerPrefix, args);
4041 // Short timeout, since blocking here can
4042 // deadlock with the application.
4043 tp.go(fd, 2000);
4044 } finally {
4045 tp.kill();
4046 }
4047 } catch (IOException e) {
4048 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4049 } catch (RemoteException e) {
4050 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4051 }
4052 needNL = true;
4053 }
4054 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004055 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004056 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004057
Craig Mautnerf3333272013-04-22 10:55:53 -07004058 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004059 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4060 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004061 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4062 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004063 }
4064
4065 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004066 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004067 }
4068
4069 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004070 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4071 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004072 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4073 }
4074
Craig Mautner05d29032013-05-03 13:40:13 -07004075 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004076 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4077 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4078 }
Craig Mautner05d29032013-05-03 13:40:13 -07004079 }
4080
Craig Mautner0eea92c2013-05-16 13:35:39 -07004081 void removeSleepTimeouts() {
4082 mSleepTimeout = false;
4083 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4084 }
4085
4086 final void scheduleSleepTimeout() {
4087 removeSleepTimeouts();
4088 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4089 }
4090
Craig Mautner4a1cb222013-12-04 16:14:06 -08004091 @Override
4092 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004093 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004094 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4095 }
4096
4097 @Override
4098 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004099 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4101 }
4102
4103 @Override
4104 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004105 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004106 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4107 }
4108
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004109 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004110 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004111 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004112 newDisplay = mActivityDisplays.get(displayId) == null;
4113 if (newDisplay) {
4114 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004115 if (activityDisplay.mDisplay == null) {
4116 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4117 return;
4118 }
Craig Mautner4504de52013-12-20 09:06:56 -08004119 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004120 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004121 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004122 }
Craig Mautner4504de52013-12-20 09:06:56 -08004123 if (newDisplay) {
4124 mWindowManager.onDisplayAdded(displayId);
4125 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004126 }
4127
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004128 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4129 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4130 return;
4131 }
4132 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4133 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4134 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4135 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4136 }
4137
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004138 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004139 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004140 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4141 if (activityDisplay != null) {
4142 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004143 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004144 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004145 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004146 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004147 }
4148 }
4149 mWindowManager.onDisplayRemoved(displayId);
4150 }
4151
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004152 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004153 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004154 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4155 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004156 // TODO: Update the bounds.
4157 }
4158 }
4159 mWindowManager.onDisplayChanged(displayId);
4160 }
4161
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004162 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004163 StackInfo info = new StackInfo();
4164 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4165 info.displayId = Display.DEFAULT_DISPLAY;
4166 info.stackId = stack.mStackId;
4167
4168 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4169 final int numTasks = tasks.size();
4170 int[] taskIds = new int[numTasks];
4171 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004172 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004173 for (int i = 0; i < numTasks; ++i) {
4174 final TaskRecord task = tasks.get(i);
4175 taskIds[i] = task.taskId;
4176 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4177 : task.realActivity != null ? task.realActivity.flattenToString()
4178 : task.getTopActivity() != null ? task.getTopActivity().packageName
4179 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004180 taskBounds[i] = new Rect();
4181 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004182 }
4183 info.taskIds = taskIds;
4184 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004185 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004186 return info;
4187 }
4188
4189 StackInfo getStackInfoLocked(int stackId) {
4190 ActivityStack stack = getStack(stackId);
4191 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004192 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004193 }
4194 return null;
4195 }
4196
4197 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004198 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004199 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4200 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004201 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004202 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004203 }
4204 }
4205 return list;
4206 }
4207
Craig Mautner15df08a2015-04-01 12:17:18 -07004208 TaskRecord getLockedTaskLocked() {
4209 final int top = mLockTaskModeTasks.size() - 1;
4210 if (top >= 0) {
4211 return mLockTaskModeTasks.get(top);
4212 }
4213 return null;
4214 }
4215
4216 boolean isLockedTask(TaskRecord task) {
4217 return mLockTaskModeTasks.contains(task);
4218 }
4219
4220 boolean isLastLockedTask(TaskRecord task) {
4221 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4222 }
4223
4224 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004225 if (!mLockTaskModeTasks.remove(task)) {
4226 return;
4227 }
4228 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4229 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004230 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004231 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4232 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004233 final Message lockTaskMsg = Message.obtain();
4234 lockTaskMsg.arg1 = task.userId;
4235 lockTaskMsg.what = LOCK_TASK_END_MSG;
4236 mHandler.sendMessage(lockTaskMsg);
4237 }
4238 }
4239
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004240 void showLockTaskToast() {
4241 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004242 }
4243
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004244 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4245 if (mLockTaskModeTasks.contains(task)) {
4246 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4247 }
4248 }
4249
Craig Mautner432f64e2015-05-20 14:59:57 -07004250 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4251 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004252 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004253 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004254 final TaskRecord lockedTask = getLockedTaskLocked();
4255 if (lockedTask != null) {
4256 removeLockedTaskLocked(lockedTask);
4257 if (!mLockTaskModeTasks.isEmpty()) {
4258 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004259 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4260 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004261 lockedTask.performClearTaskLocked();
4262 resumeTopActivitiesLocked();
4263 return;
4264 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004265 }
Craig Mautnere0570202015-05-13 13:06:11 -07004266 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4267 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004268 return;
4269 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004270
4271 // Should have already been checked, but do it again.
4272 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004273 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4274 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004275 return;
4276 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004277 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004278 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004279 return;
4280 }
4281
4282 if (mLockTaskModeTasks.isEmpty()) {
4283 // First locktask.
4284 final Message lockTaskMsg = Message.obtain();
4285 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4286 lockTaskMsg.arg1 = task.userId;
4287 lockTaskMsg.what = LOCK_TASK_START_MSG;
4288 lockTaskMsg.arg2 = lockTaskModeState;
4289 mHandler.sendMessage(lockTaskMsg);
4290 }
4291 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004292 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4293 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004294 mLockTaskModeTasks.remove(task);
4295 mLockTaskModeTasks.add(task);
4296
4297 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004298 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004299 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004300
4301 if (andResume) {
4302 findTaskToMoveToFrontLocked(task, 0, null, reason);
4303 resumeTopActivitiesLocked();
4304 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004305 }
4306
4307 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004308 return isLockTaskModeViolation(task, false);
4309 }
4310
4311 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4312 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004313 return false;
4314 }
4315 final int lockTaskAuth = task.mLockTaskAuth;
4316 switch (lockTaskAuth) {
4317 case LOCK_TASK_AUTH_DONT_LOCK:
4318 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004319 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004320 case LOCK_TASK_AUTH_LAUNCHABLE:
4321 case LOCK_TASK_AUTH_WHITELISTED:
4322 return false;
4323 case LOCK_TASK_AUTH_PINNABLE:
4324 // Pinnable tasks can't be launched on top of locktask tasks.
4325 return !mLockTaskModeTasks.isEmpty();
4326 default:
4327 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4328 return true;
4329 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004330 }
4331
Craig Mautner15df08a2015-04-01 12:17:18 -07004332 void onLockTaskPackagesUpdatedLocked() {
4333 boolean didSomething = false;
4334 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4335 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004336 final boolean wasWhitelisted =
4337 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4338 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004339 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004340 final boolean isWhitelisted =
4341 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4342 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4343 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004344 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004345 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4346 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004347 removeLockedTaskLocked(lockedTask);
4348 lockedTask.performClearTaskLocked();
4349 didSomething = true;
4350 }
4351 }
4352 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4353 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4354 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4355 final ActivityStack stack = stacks.get(stackNdx);
4356 stack.onLockTaskPackagesUpdatedLocked();
4357 }
4358 }
Craig Mautnere0570202015-05-13 13:06:11 -07004359 final ActivityRecord r = topRunningActivityLocked();
4360 final TaskRecord task = r != null ? r.task : null;
4361 if (mLockTaskModeTasks.isEmpty() && task != null
4362 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4363 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004364 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4365 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4366 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4367 false);
4368 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004369 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004370 if (didSomething) {
4371 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004372 }
4373 }
4374
Benjamin Franz43261142015-02-11 15:59:44 +00004375 int getLockTaskModeState() {
4376 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004377 }
4378
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004379 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004380
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004381 public ActivityStackSupervisorHandler(Looper looper) {
4382 super(looper);
4383 }
4384
Craig Mautnerf3333272013-04-22 10:55:53 -07004385 void activityIdleInternal(ActivityRecord r) {
4386 synchronized (mService) {
4387 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4388 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004389 }
4390
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004391 @Override
4392 public void handleMessage(Message msg) {
4393 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004394 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004395 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4396 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004397 if (mService.mDidDexOpt) {
4398 mService.mDidDexOpt = false;
4399 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4400 nmsg.obj = msg.obj;
4401 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4402 return;
4403 }
4404 // We don't at this point know if the activity is fullscreen,
4405 // so we need to be conservative and assume it isn't.
4406 activityIdleInternal((ActivityRecord)msg.obj);
4407 } break;
4408 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004409 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004410 activityIdleInternal((ActivityRecord)msg.obj);
4411 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004412 case RESUME_TOP_ACTIVITY_MSG: {
4413 synchronized (mService) {
4414 resumeTopActivitiesLocked();
4415 }
4416 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004417 case SLEEP_TIMEOUT_MSG: {
4418 synchronized (mService) {
4419 if (mService.isSleepingOrShuttingDown()) {
4420 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4421 mSleepTimeout = true;
4422 checkReadyForSleepLocked();
4423 }
4424 }
4425 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004426 case LAUNCH_TIMEOUT_MSG: {
4427 if (mService.mDidDexOpt) {
4428 mService.mDidDexOpt = false;
4429 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4430 return;
4431 }
4432 synchronized (mService) {
4433 if (mLaunchingActivity.isHeld()) {
4434 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4435 if (VALIDATE_WAKE_LOCK_CALLER
4436 && Binder.getCallingUid() != Process.myUid()) {
4437 throw new IllegalStateException("Calling must be system uid");
4438 }
4439 mLaunchingActivity.release();
4440 }
4441 }
4442 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004443 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004444 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004445 } break;
4446 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004447 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004448 } break;
4449 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004450 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004451 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004452 case CONTAINER_CALLBACK_VISIBILITY: {
4453 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004454 final IActivityContainerCallback callback = container.mCallback;
4455 if (callback != null) {
4456 try {
4457 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4458 } catch (RemoteException e) {
4459 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004460 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004461 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004462 case LOCK_TASK_START_MSG: {
4463 // When lock task starts, we disable the status bars.
4464 try {
Jason Monk62515be2014-05-21 16:06:19 -04004465 if (mLockTaskNotify == null) {
4466 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004467 }
Jason Monk62515be2014-05-21 16:06:19 -04004468 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004469 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004470 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004471 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004472 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004473 flags = StatusBarManager.DISABLE_MASK
4474 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004475 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004476 flags = StatusBarManager.DISABLE_MASK
4477 & (~StatusBarManager.DISABLE_BACK)
4478 & (~StatusBarManager.DISABLE_HOME)
4479 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004480 }
4481 getStatusBarService().disable(flags, mToken,
4482 mService.mContext.getPackageName());
4483 }
4484 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004485 if (getDevicePolicyManager() != null) {
4486 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004487 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004488 }
justinzhang5286d3f2014-05-12 17:06:01 -04004489 } catch (RemoteException ex) {
4490 throw new RuntimeException(ex);
4491 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004492 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004493 case LOCK_TASK_END_MSG: {
4494 // When lock task ends, we enable the status bars.
4495 try {
Jason Monk62515be2014-05-21 16:06:19 -04004496 if (getStatusBarService() != null) {
4497 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4498 mService.mContext.getPackageName());
4499 }
4500 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004501 if (getDevicePolicyManager() != null) {
4502 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4503 msg.arg1);
4504 }
Jason Monk62515be2014-05-21 16:06:19 -04004505 if (mLockTaskNotify == null) {
4506 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4507 }
4508 mLockTaskNotify.show(false);
4509 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004510 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004511 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004512 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004513 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004514 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004515 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004516 new LockPatternUtils(mService.mContext)
4517 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004518 }
4519 } catch (SettingNotFoundException e) {
4520 // No setting, don't lock.
4521 }
justinzhang5286d3f2014-05-12 17:06:01 -04004522 } catch (RemoteException ex) {
4523 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004524 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004525 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004526 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004527 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004528 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4529 if (mLockTaskNotify == null) {
4530 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4531 }
4532 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4533 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004534 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4535 final ActivityContainer container = (ActivityContainer) msg.obj;
4536 final IActivityContainerCallback callback = container.mCallback;
4537 if (callback != null) {
4538 try {
4539 callback.onAllActivitiesComplete(container.asBinder());
4540 } catch (RemoteException e) {
4541 }
4542 }
4543 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004544 case LAUNCH_TASK_BEHIND_COMPLETE: {
4545 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004546 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004547 if (r != null) {
4548 handleLaunchTaskBehindCompleteLocked(r);
4549 }
4550 }
4551 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004552 }
4553 }
4554 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004555
Ying Wangb081a592014-04-22 15:20:16 -07004556 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004557 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004558 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004559 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004560 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004561 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004562 ActivityRecord mParentActivity = null;
4563 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004564
Craig Mautnere3a00d72014-04-16 08:31:19 -07004565 boolean mVisible = true;
4566
Craig Mautner4a1cb222013-12-04 16:14:06 -08004567 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004568 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004569
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004570 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4571 final static int CONTAINER_STATE_NO_SURFACE = 1;
4572 final static int CONTAINER_STATE_FINISHING = 2;
4573 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4574
4575 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004576 synchronized (mService) {
4577 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004578 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004579 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004580 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004581 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004582 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004583
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004584 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004585 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004586 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004587 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004588 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004589 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004590 }
4591
4592 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004593 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004594 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004595 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4596 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004597 return;
4598 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004599 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004600 }
4601 }
4602
4603 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004604 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004605 synchronized (mService) {
4606 if (mActivityDisplay != null) {
4607 return mActivityDisplay.mDisplayId;
4608 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004609 }
4610 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004611 }
4612
Jeff Brownca9bc702014-02-11 14:32:56 -08004613 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004614 public int getStackId() {
4615 synchronized (mService) {
4616 return mStackId;
4617 }
4618 }
4619
4620 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004621 public boolean injectEvent(InputEvent event) {
4622 final long origId = Binder.clearCallingIdentity();
4623 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004624 synchronized (mService) {
4625 if (mActivityDisplay != null) {
4626 return mInputManagerInternal.injectInputEvent(event,
4627 mActivityDisplay.mDisplayId,
4628 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4629 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004630 }
4631 return false;
4632 } finally {
4633 Binder.restoreCallingIdentity(origId);
4634 }
4635 }
4636
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004637 @Override
4638 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004639 synchronized (mService) {
4640 if (mContainerState == CONTAINER_STATE_FINISHING) {
4641 return;
4642 }
4643 mContainerState = CONTAINER_STATE_FINISHING;
4644
Craig Mautnerd163e752014-06-13 17:18:47 -07004645 long origId = Binder.clearCallingIdentity();
4646 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004647 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004648 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004649 } finally {
4650 Binder.restoreCallingIdentity(origId);
4651 }
4652 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004653 }
4654
Craig Mautner60257702014-09-17 15:02:33 -07004655 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004656 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004657 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004658 if (mActivityDisplay != null) {
4659 mActivityDisplay.detachActivitiesLocked(mStack);
4660 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004661 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004662 }
4663 }
4664
4665 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004666 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004667 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004668 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004669 Binder.getCallingUid(), mCurrentUser, false,
4670 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004671
Craig Mautnere0a38842013-12-16 16:14:02 -08004672 // TODO: Switch to user app stacks here.
4673 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004674 final Uri data = intent.getData();
4675 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4676 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004677 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004678 checkEmbeddedAllowedInner(userId, intent, mimeType);
4679
4680 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4681 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004682 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004683 }
4684
4685 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004686 public final int startActivityIntentSender(IIntentSender intentSender)
4687 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004688 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4689
4690 if (!(intentSender instanceof PendingIntentRecord)) {
4691 throw new IllegalArgumentException("Bad PendingIntent object");
4692 }
4693
Craig Mautnerb9168362015-02-26 20:40:19 -08004694 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004695 Binder.getCallingUid(), mCurrentUser, false,
4696 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004697
4698 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4699 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4700 pendingIntent.key.requestResolvedType);
4701
4702 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4703 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4704 }
4705
4706 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004707 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004708 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004709 throw new SecurityException(
4710 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4711 }
4712 }
4713
Craig Mautnerdf88d732014-01-27 09:21:32 -08004714 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004715 public IBinder asBinder() {
4716 return this;
4717 }
4718
Craig Mautner4504de52013-12-20 09:06:56 -08004719 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004720 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004721 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004722 }
4723
Craig Mautner4a1cb222013-12-04 16:14:06 -08004724 ActivityStackSupervisor getOuter() {
4725 return ActivityStackSupervisor.this;
4726 }
4727
Craig Mautnerd163e752014-06-13 17:18:47 -07004728 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004729 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004730 }
4731
Craig Mautner6985bad2014-04-21 15:22:06 -07004732 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004733 void setVisible(boolean visible) {
4734 if (mVisible != visible) {
4735 mVisible = visible;
4736 if (mCallback != null) {
4737 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4738 0 /* unused */, this).sendToTarget();
4739 }
4740 }
4741 }
4742
Craig Mautner6985bad2014-04-21 15:22:06 -07004743 void setDrawn() {
4744 }
4745
Craig Mautner1b4bf852014-05-26 15:06:32 -07004746 // You can always start a new task on a regular ActivityStack.
4747 boolean isEligibleForNewTasks() {
4748 return true;
4749 }
4750
Craig Mautnerd163e752014-06-13 17:18:47 -07004751 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004752 detachLocked();
4753 deleteActivityContainer(this);
4754 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004755 }
4756
Craig Mautner34b73df2014-01-12 21:11:08 -08004757 @Override
4758 public String toString() {
4759 return mIdString + (mActivityDisplay == null ? "N" : "A");
4760 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004761 }
4762
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004763 private class VirtualActivityContainer extends ActivityContainer {
4764 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004765 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004766
4767 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4768 super(getNextStackId());
4769 mParentActivity = parent;
4770 mCallback = callback;
4771 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004772 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004773 }
4774
4775 @Override
4776 public void setSurface(Surface surface, int width, int height, int density) {
4777 super.setSurface(surface, width, height, density);
4778
4779 synchronized (mService) {
4780 final long origId = Binder.clearCallingIdentity();
4781 try {
4782 setSurfaceLocked(surface, width, height, density);
4783 } finally {
4784 Binder.restoreCallingIdentity(origId);
4785 }
4786 }
4787 }
4788
4789 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4790 if (mContainerState == CONTAINER_STATE_FINISHING) {
4791 return;
4792 }
4793 VirtualActivityDisplay virtualActivityDisplay =
4794 (VirtualActivityDisplay) mActivityDisplay;
4795 if (virtualActivityDisplay == null) {
4796 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004797 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004798 mActivityDisplay = virtualActivityDisplay;
4799 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004800 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004801 }
4802
4803 if (mSurface != null) {
4804 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004805 }
4806
Craig Mautner6985bad2014-04-21 15:22:06 -07004807 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004808 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004809 mStack.resumeTopActivityLocked(null);
4810 } else {
4811 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004812 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004813 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004814 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004815 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004816 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004817
Craig Mautnerd163e752014-06-13 17:18:47 -07004818 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004819
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004820 if (DEBUG_STACK) Slog.d(TAG_STACK,
4821 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004822 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004823
Craig Mautner6985bad2014-04-21 15:22:06 -07004824 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004825 boolean isAttachedLocked() {
4826 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004827 }
4828
4829 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004830 void setDrawn() {
4831 synchronized (mService) {
4832 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004833 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004834 }
4835 }
4836
Craig Mautner1b4bf852014-05-26 15:06:32 -07004837 // Never start a new task on an ActivityView if it isn't explicitly specified.
4838 @Override
4839 boolean isEligibleForNewTasks() {
4840 return false;
4841 }
4842
Craig Mautnerd163e752014-06-13 17:18:47 -07004843 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004844 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004845 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4846 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4847 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4848 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4849 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004850 }
4851 }
4852
Craig Mautner4a1cb222013-12-04 16:14:06 -08004853 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4854 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004855 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004856 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004857 int mDisplayId;
4858 Display mDisplay;
4859 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004860
Craig Mautner4a1cb222013-12-04 16:14:06 -08004861 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4862 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004863 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004864
Jose Lima4b6c6692014-08-12 17:41:12 -07004865 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004866
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004867 ActivityDisplay() {
4868 }
Craig Mautner4504de52013-12-20 09:06:56 -08004869
Craig Mautner1a70a162014-09-13 12:09:31 -07004870 // After instantiation, check that mDisplay is not null before using this. The alternative
4871 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004872 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004873 final Display display = mDisplayManager.getDisplay(displayId);
4874 if (display == null) {
4875 return;
4876 }
4877 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004878 }
4879
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004880 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004881 mDisplay = display;
4882 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004883 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004884 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004885
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004886 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004887 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004888 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4889 + " onTop=" + onTop);
4890 if (onTop) {
4891 mStacks.add(stack);
4892 } else {
4893 mStacks.add(0, stack);
4894 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004895 }
4896
Craig Mautnere0a38842013-12-16 16:14:02 -08004897 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004898 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004899 + " from displayId=" + mDisplayId);
4900 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004901 }
4902
Jose Lima4b6c6692014-08-12 17:41:12 -07004903 void setVisibleBehindActivity(ActivityRecord r) {
4904 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004905 }
4906
Jose Lima4b6c6692014-08-12 17:41:12 -07004907 boolean hasVisibleBehindActivity() {
4908 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004909 }
4910
Craig Mautner34b73df2014-01-12 21:11:08 -08004911 @Override
4912 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004913 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4914 }
4915 }
4916
4917 class VirtualActivityDisplay extends ActivityDisplay {
4918 VirtualDisplay mVirtualDisplay;
4919
Craig Mautner6985bad2014-04-21 15:22:06 -07004920 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004921 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004922 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4923 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4924 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4925 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004926
4927 init(mVirtualDisplay.getDisplay());
4928
4929 mWindowManager.handleDisplayAdded(mDisplayId);
4930 }
4931
4932 void setSurface(Surface surface) {
4933 if (mVirtualDisplay != null) {
4934 mVirtualDisplay.setSurface(surface);
4935 }
4936 }
4937
4938 @Override
4939 void detachActivitiesLocked(ActivityStack stack) {
4940 super.detachActivitiesLocked(stack);
4941 if (mVirtualDisplay != null) {
4942 mVirtualDisplay.release();
4943 mVirtualDisplay = null;
4944 }
4945 }
4946
4947 @Override
4948 public String toString() {
4949 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004950 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004951 }
Jose Lima58e66d62014-05-27 20:00:27 -07004952
4953 private boolean isLeanbackOnlyDevice() {
4954 boolean onLeanbackOnly = false;
4955 try {
4956 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4957 PackageManager.FEATURE_LEANBACK_ONLY);
4958 } catch (RemoteException e) {
4959 // noop
4960 }
4961
4962 return onLeanbackOnly;
4963 }
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004964
4965 /**
4966 * Adjust bounds to stay within stack bounds.
4967 *
4968 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4969 * that keep them unchanged, but be contained within the stack bounds.
4970 *
4971 * @param bounds Bounds to be adjusted.
4972 * @param stackBounds Bounds within which the other bounds should remain.
4973 */
4974 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4975 if (stackBounds == null || stackBounds.contains(bounds)) {
4976 return;
4977 }
4978
4979 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4980 final int maxRight = stackBounds.right
4981 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4982 int horizontalDiff = stackBounds.left - bounds.left;
4983 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4984 || (bounds.left + horizontalDiff >= maxRight)) {
4985 horizontalDiff = maxRight - bounds.left;
4986 }
4987 bounds.left += horizontalDiff;
4988 bounds.right += horizontalDiff;
4989 }
4990
4991 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4992 final int maxBottom = stackBounds.bottom
4993 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4994 int verticalDiff = stackBounds.top - bounds.top;
4995 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4996 || (bounds.top + verticalDiff >= maxBottom)) {
4997 verticalDiff = maxBottom - bounds.top;
4998 }
4999 bounds.top += verticalDiff;
5000 bounds.bottom += verticalDiff;
5001 }
5002 }
5003
Craig Mautner27084302013-03-25 08:05:25 -07005004}