blob: ca3aaa61bd581740db2976f032c1acf86eb881f2 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Wale Ogunwale868a5e12015-08-02 16:19:20 -070020import static android.app.ActivityManager.*;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070021import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
22import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070023import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070026import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070027import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080028import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070029import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070030import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
31import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
32import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070033import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070034import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
35import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070037import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
38import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070039
Svetoslav7008b512015-06-24 18:47:07 -070040import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070041import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070042import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080043import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.ActivityOptions;
45import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070046import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080047import android.app.IActivityContainer;
48import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070049import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070052import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070053import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070055import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040056import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040057import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070061import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070062import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070063import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070064import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070065import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070066import android.content.pm.PackageManager;
67import android.content.pm.ResolveInfo;
68import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080069import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.hardware.display.DisplayManager;
71import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.hardware.display.DisplayManagerGlobal;
73import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080074import android.hardware.input.InputManager;
75import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080076import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070079import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070080import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070085import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070086import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070087import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040088import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070090import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070091import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070092import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070093import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070094import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040095import android.provider.Settings;
96import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070097import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070098import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070099import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700100import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700101import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800102import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700103
Craig Mautner4a1cb222013-12-04 16:14:06 -0800104import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800105import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800106import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800107import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800108import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700109import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700110import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800111import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700112import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400113import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700114import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400115import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800116import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700118import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119
justinzhang5286d3f2014-05-12 17:06:01 -0400120
Craig Mautner8d341ef2013-03-26 09:03:27 -0700121import java.io.FileDescriptor;
122import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700123import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700124import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700125import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700126import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700127import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700128
Craig Mautner4a1cb222013-12-04 16:14:06 -0800129public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800130 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700131 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700132 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700133 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700134 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700135 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700137 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700138 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
139 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700140 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700141 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700142 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
143 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700144 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700145 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800146
Craig Mautnerf3333272013-04-22 10:55:53 -0700147 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700149
Craig Mautner0eea92c2013-05-16 13:35:39 -0700150 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700152
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700153 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700155
Craig Mautner05d29032013-05-03 13:40:13 -0700156 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
157 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
158 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700159 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700160 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800161 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
162 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
163 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700164 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400165 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
166 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700167 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700168 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700169 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800170
Wale Ogunwale040b4702015-08-06 18:10:50 -0700171 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700172
Jason Monk62515be2014-05-21 16:06:19 -0400173 private static final String LOCK_TASK_TAG = "Lock-to-App";
174
Wale Ogunwale040b4702015-08-06 18:10:50 -0700175 // Used to indicate if an object (e.g. stack) that we are trying to get
176 // should be created if it doesn't exist already.
177 private static final boolean CREATE_IF_NEEDED = true;
178
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700179 // Used to indicate that windows of activities should be preserved during the resize.
180 static final boolean PRESERVE_WINDOWS = true;
181
Wale Ogunwale040b4702015-08-06 18:10:50 -0700182 // Used to indicate if an object (e.g. task) should be moved/created
183 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700184 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700185
186 // Used to indicate that an objects (e.g. task) removal from its container
187 // (e.g. stack) is due to it moving to another container.
188 static final boolean MOVING = true;
189
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700190 // Force the focus to change to the stack we are moving a task to..
191 static final boolean FORCE_FOCUS = true;
192
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700193 // Restore task from the saved recents if it can't be found in any live stack.
194 static final boolean RESTORE_FROM_RECENTS = true;
195
Svetoslav7008b512015-06-24 18:47:07 -0700196 // Activity actions an app cannot start if it uses a permission which is not granted.
197 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
198 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700199
Svetoslav7008b512015-06-24 18:47:07 -0700200 static {
201 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
202 Manifest.permission.CAMERA);
203 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
204 Manifest.permission.CAMERA);
205 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
206 Manifest.permission.CALL_PHONE);
207 }
208
Svet Ganov99b60432015-06-27 13:15:22 -0700209 /** Action restriction: launching the activity is not restricted. */
210 private static final int ACTIVITY_RESTRICTION_NONE = 0;
211 /** Action restriction: launching the activity is restricted by a permission. */
212 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
213 /** Action restriction: launching the activity is restricted by an app op. */
214 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700215
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700216 // The height/width divide used when fitting a task within a bounds with method
217 // {@link #fitWithinBounds}.
218 // We always want the task to to be visible in the bounds without affecting its size when
219 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
220 // the input bounds right or bottom side minus the width or height divided by this value.
221 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
222
justinzhang5286d3f2014-05-12 17:06:01 -0400223 /** Status Bar Service **/
224 private IBinder mToken = new Binder();
225 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400226 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400227
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700228 // For debugging to make sure the caller when acquiring/releasing our
229 // wake lock is the system process.
230 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700231
Craig Mautner27084302013-03-25 08:05:25 -0700232 final ActivityManagerService mService;
233
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800234 private final RecentTasks mRecentTasks;
235
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700236 final ActivityStackSupervisorHandler mHandler;
237
238 /** Short cut */
239 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800240 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700241
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700242 /** Counter for next free stack ID to use for dynamic activity stacks. */
243 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700244
245 /** Task identifier that activities are currently being started in. Incremented each time a
246 * new task is created. */
247 private int mCurTaskId = 0;
248
Craig Mautner2420ead2013-04-01 17:13:20 -0700249 /** The current user */
250 private int mCurrentUser;
251
Craig Mautnere0a38842013-12-16 16:14:02 -0800252 /** The stack containing the launcher app. Assumed to always be attached to
253 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700254 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700255
Craig Mautnere0a38842013-12-16 16:14:02 -0800256 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700257 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700258
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
260 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800261 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800262 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700263
264 /** List of activities that are waiting for a new activity to become visible before completing
265 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800266 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700267
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700268 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800269 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700270
271 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800272 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700273
Craig Mautnerde4ef022013-04-07 19:01:33 -0700274 /** List of activities that are ready to be stopped, but waiting for the next activity to
275 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800276 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700277
Craig Mautnerf3333272013-04-22 10:55:53 -0700278 /** List of activities that are ready to be finished, but waiting for the previous activity to
279 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800280 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700281
Craig Mautner0eea92c2013-05-16 13:35:39 -0700282 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800283 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700284
Craig Mautnerf3333272013-04-22 10:55:53 -0700285 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700286 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700287
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700288 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700289 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700290
Craig Mautnerde4ef022013-04-07 19:01:33 -0700291 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
292 * is being brought in front of us. */
293 boolean mUserLeaving = false;
294
Craig Mautner0eea92c2013-05-16 13:35:39 -0700295 /** Set when we have taken too long waiting to go to sleep. */
296 boolean mSleepTimeout = false;
297
298 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700299 * We don't want to allow the device to go to sleep while in the process
300 * of launching an activity. This is primarily to allow alarm intent
301 * receivers to launch an activity and get that to run before the device
302 * goes back to sleep.
303 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700304 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700305
306 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700307 * Set when the system is going to sleep, until we have
308 * successfully paused the current activity and released our wake lock.
309 * At that point the system is allowed to actually sleep.
310 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700311 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700312
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700313 /** Stack id of the front stack when user switched, indexed by userId. */
314 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700315
Craig Mautner4504de52013-12-20 09:06:56 -0800316 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700318 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319
320 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700321 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322
Jeff Brownca9bc702014-02-11 14:32:56 -0800323 InputManagerInternal mInputManagerInternal;
324
Craig Mautner15df08a2015-04-01 12:17:18 -0700325 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
326 * may be finished until there is only one entry left. If this is empty the system is not
327 * in lockTask mode. */
328 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000329 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700330 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
331 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000332 */
333 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400334 /**
335 * Notifies the user when entering/exiting lock-task.
336 */
337 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800338
Craig Mautner15df08a2015-04-01 12:17:18 -0700339 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700340
Craig Mautner42d04db2014-11-06 12:13:23 -0800341 /** Used to keep resumeTopActivityLocked() from being entered recursively */
342 boolean inResumeTopActivity;
343
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700344 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700345 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700346 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700347
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700348 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
349 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
350
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700351 // The default minimal size that will be used if the activity doesn't specify its minimal size.
352 // It will be calculated when the default display gets added.
353 private int mDefaultMinimalSizeOfResizeableTask = -1;
354
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700355 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
356 private boolean mTaskLayersChanged = true;
357
Craig Mautneree36c772014-07-16 14:56:05 -0700358 /**
359 * Description of a request to start a new activity, which has been held
360 * due to app switches being disabled.
361 */
362 static class PendingActivityLaunch {
363 final ActivityRecord r;
364 final ActivityRecord sourceRecord;
365 final int startFlags;
366 final ActivityStack stack;
Robert Carr13997f52015-10-23 13:13:39 -0700367 final ProcessRecord callerApp;
Craig Mautneree36c772014-07-16 14:56:05 -0700368
369 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
Robert Carr13997f52015-10-23 13:13:39 -0700370 int _startFlags, ActivityStack _stack, ProcessRecord _callerApp) {
Craig Mautneree36c772014-07-16 14:56:05 -0700371 r = _r;
372 sourceRecord = _sourceRecord;
373 startFlags = _startFlags;
374 stack = _stack;
Robert Carr13997f52015-10-23 13:13:39 -0700375 callerApp = _callerApp;
376 }
377
378 void sendErrorResult(String message) {
379 try {
380 if (callerApp.thread != null) {
381 callerApp.thread.scheduleCrash(message);
382 }
383 } catch (RemoteException e) {
384 Slog.e(TAG, "Exception scheduling crash of failed "
385 + "activity launcher sourceRecord=" + sourceRecord, e);
386 }
Craig Mautneree36c772014-07-16 14:56:05 -0700387 }
388 }
389
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800390 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700391 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800392 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700393 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
394 }
395
396 /**
397 * At the time when the constructor runs, the power manager has not yet been
398 * initialized. So we initialize our wakelocks afterwards.
399 */
400 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800401 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700402 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700403 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700404 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700405 }
406
justinzhang5286d3f2014-05-12 17:06:01 -0400407 // This function returns a IStatusBarService. The value is from ServiceManager.
408 // getService and is cached.
409 private IStatusBarService getStatusBarService() {
410 synchronized (mService) {
411 if (mStatusBarService == null) {
412 mStatusBarService = IStatusBarService.Stub.asInterface(
413 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
414 if (mStatusBarService == null) {
415 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
416 }
417 }
418 return mStatusBarService;
419 }
420 }
421
Jason Monk35c62a42014-06-17 10:24:47 -0400422 private IDevicePolicyManager getDevicePolicyManager() {
423 synchronized (mService) {
424 if (mDevicePolicyManager == null) {
425 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
426 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
427 if (mDevicePolicyManager == null) {
428 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
429 }
430 }
431 return mDevicePolicyManager;
432 }
433 }
434
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700435 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800436 synchronized (mService) {
437 mWindowManager = wm;
438
439 mDisplayManager =
440 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
441 mDisplayManager.registerDisplayListener(this, null);
442
443 Display[] displays = mDisplayManager.getDisplays();
444 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
445 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800446 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700447 if (activityDisplay.mDisplay == null) {
448 throw new IllegalStateException("Default Display does not exist");
449 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800450 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700451 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800452 }
453
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700454 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800455 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800456
457 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800458 }
Craig Mautner27084302013-03-25 08:05:25 -0700459 }
460
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200461 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700462 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200463 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700464 }
465
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700466 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800467 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700468 }
469
Craig Mautnerde4ef022013-04-07 19:01:33 -0700470 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800471 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700472 }
473
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700474 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700475 if (stack == null) {
476 return false;
477 }
478
Craig Mautnerdf88d732014-01-27 09:21:32 -0800479 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
480 if (parent != null) {
481 stack = parent.task.stack;
482 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800483 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700484 }
485
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700486 /** The top most stack. */
487 boolean isFrontStack(ActivityStack stack) {
488 if (stack == null) {
489 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800490 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700491
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700492 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
493 if (parent != null) {
494 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800495 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700496 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
497 }
498
499 void setFocusStack(String reason, ActivityStack focusedStack) {
500 mLastFocusedStack = mFocusedStack;
501 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800502
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700503 EventLogTags.writeAmFocusedStack(
504 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
505 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800506
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800507 if (mService.mBooting || !mService.mBooted) {
508 final ActivityRecord r = topRunningActivityLocked();
509 if (r != null && r.idle) {
510 checkFinishBootingLocked();
511 }
512 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700513 }
514
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700515 void moveHomeStackToFront(String reason) {
516 mHomeStack.moveToFront(reason);
517 }
518
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700519 /** Returns true if the focus activity was adjusted to the home stack top activity. */
520 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700521 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
522 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700523 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700524 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700525
Craig Mautner84984fa2014-06-19 11:19:20 -0700526 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700527
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700528 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700529 if (top == null) {
530 return false;
531 }
532 mService.setFocusedActivityLocked(top, reason);
533 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700534 }
535
Craig Mautner299f9602015-01-26 09:47:33 -0800536 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700537 if (!mService.mBooting && !mService.mBooted) {
538 // Not ready yet!
539 return false;
540 }
541
Craig Mautner84984fa2014-06-19 11:19:20 -0700542 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
543 mWindowManager.showRecentApps();
544 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700545 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700546
Craig Mautner84984fa2014-06-19 11:19:20 -0700547 if (prev != null) {
548 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
549 }
550
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700551 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
552 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800553 // Only resume home activity if isn't finishing.
554 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800555 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700556 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700557 }
Craig Mautner299f9602015-01-26 09:47:33 -0800558 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700559 }
560
Craig Mautner8d341ef2013-03-26 09:03:27 -0700561 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700562 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700563 }
564
565 /**
566 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
567 * @param id Id of the task we would like returned.
568 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
569 * restore the task from recents to the active list.
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700570 * @param stackId The stack to restore the task to (default launch stack will be used
571 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700572 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700573 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800574 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800575 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800576 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800577 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
578 ActivityStack stack = stacks.get(stackNdx);
579 TaskRecord task = stack.taskForIdLocked(id);
580 if (task != null) {
581 return task;
582 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700583 }
584 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800585
586 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700587 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800588 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800589 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800591 return null;
592 }
593
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700594 if (!restoreFromRecents) {
595 return task;
596 }
597
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700598 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700599 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
600 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800601 return null;
602 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700603 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800604 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700605 }
606
Craig Mautner6170f732013-04-02 13:05:23 -0700607 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800608 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800610 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800611 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
612 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
613 if (r != null) {
614 return r;
615 }
Craig Mautner6170f732013-04-02 13:05:23 -0700616 }
617 }
618 return null;
619 }
620
Craig Mautneref73ee12014-04-23 11:45:37 -0700621 void setNextTaskId(int taskId) {
622 if (taskId > mCurTaskId) {
623 mCurTaskId = taskId;
624 }
625 }
626
Craig Mautner8d341ef2013-03-26 09:03:27 -0700627 int getNextTaskId() {
628 do {
629 mCurTaskId++;
630 if (mCurTaskId <= 0) {
631 mCurTaskId = 1;
632 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700633 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700634 return mCurTaskId;
635 }
636
Craig Mautnerde4ef022013-04-07 19:01:33 -0700637 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800638 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700639 if (stack == null) {
640 return null;
641 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700642 ActivityRecord resumedActivity = stack.mResumedActivity;
643 if (resumedActivity == null || resumedActivity.app == null) {
644 resumedActivity = stack.mPausingActivity;
645 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700646 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700647 }
648 }
649 return resumedActivity;
650 }
651
Dianne Hackbornff072722014-09-24 10:56:28 -0700652 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700653 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800654 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800655 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
656 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800657 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
658 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700659 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800660 continue;
661 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700662 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800663 if (hr != null) {
664 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
665 && processName.equals(hr.processName)) {
666 try {
George Mount2c92c972014-03-20 09:38:23 -0700667 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 didSomething = true;
669 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700670 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800671 Slog.w(TAG, "Exception in new application when starting activity "
672 + hr.intent.getComponent().flattenToShortString(), e);
673 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700674 }
Craig Mautner20e72272013-04-01 13:45:53 -0700675 }
Craig Mautner20e72272013-04-01 13:45:53 -0700676 }
677 }
678 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700679 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700680 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700681 }
Craig Mautner20e72272013-04-01 13:45:53 -0700682 return didSomething;
683 }
684
685 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800686 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
687 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
689 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700690 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800691 continue;
692 }
693 final ActivityRecord resumedActivity = stack.mResumedActivity;
694 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700695 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800696 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800697 return false;
698 }
Craig Mautner20e72272013-04-01 13:45:53 -0700699 }
700 }
701 return true;
702 }
703
Craig Mautnerde4ef022013-04-07 19:01:33 -0700704 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800705 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
706 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
708 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700709 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800710 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700711 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800712 return false;
713 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700714 }
715 }
716 }
717 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700718 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800719 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
720 mLastFocusedStack + " to=" + mFocusedStack);
721 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700722 return true;
723 }
724
725 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800726 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800727 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
728 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
730 final ActivityStack stack = stacks.get(stackNdx);
731 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800732 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700733 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800734 return false;
735 }
736 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800737 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700738 }
739 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800740 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700741 }
742
Craig Mautner2acc3892013-09-23 10:28:14 -0700743 /**
744 * Pause all activities in either all of the stacks or just the back stacks.
745 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700746 * @return true if any activity was paused as a result of this call.
747 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700748 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700749 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800750 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
751 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800752 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
753 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700754 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700755 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700757 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
758 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800759 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700760 }
761 }
762 return someActivityPaused;
763 }
764
Craig Mautnerde4ef022013-04-07 19:01:33 -0700765 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700766 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
768 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
770 final ActivityStack stack = stacks.get(stackNdx);
771 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700772 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700774 Slog.d(TAG_STATES,
775 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800776 pausing = false;
777 } else {
778 return false;
779 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700780 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700781 }
782 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700783 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700784 }
785
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700786 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
787 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500788 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800789 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
790 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
791 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
792 final ActivityStack stack = stacks.get(stackNdx);
793 if (stack.mResumedActivity != null &&
794 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700795 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800796 }
797 }
798 }
799 }
800
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700801 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700802 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700803 }
804
805 void sendWaitingVisibleReportLocked(ActivityRecord r) {
806 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700807 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700808 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700809 if (w.who == null) {
810 changed = true;
811 w.timeout = false;
812 if (r != null) {
813 w.who = new ComponentName(r.info.packageName, r.info.name);
814 }
815 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
816 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700817 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700818 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700819 if (changed) {
820 mService.notifyAll();
821 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700822 }
823
824 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
825 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700826 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700827 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700828 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700829 if (w.who == null) {
830 changed = true;
831 w.timeout = timeout;
832 if (r != null) {
833 w.who = new ComponentName(r.info.packageName, r.info.name);
834 }
835 w.thisTime = thisTime;
836 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700837 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700838 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700839 if (changed) {
840 mService.notifyAll();
841 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700842 }
843
Craig Mautner29219d92013-04-16 20:19:12 -0700844 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800845 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700846 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700847 if (r != null) {
848 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700849 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700850
Craig Mautner4a1cb222013-12-04 16:14:06 -0800851 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800852 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800853 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
854 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700855 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700856 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700857 if (r != null) {
858 return r;
859 }
860 }
861 }
862 return null;
863 }
864
Dianne Hackborn09233282014-04-30 11:33:59 -0700865 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700866 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800867 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
868 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800869 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800870 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800871 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
873 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700874 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800875 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700876 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700877 }
878 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700879
880 // The lists are already sorted from most recent to oldest. Just pull the most recent off
881 // each list and add it to list. Stop when all lists are empty or maxNum reached.
882 while (maxNum > 0) {
883 long mostRecentActiveTime = Long.MIN_VALUE;
884 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800885 final int numTaskLists = runningTaskLists.size();
886 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
887 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700888 if (!stackTaskList.isEmpty()) {
889 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
890 if (lastActiveTime > mostRecentActiveTime) {
891 mostRecentActiveTime = lastActiveTime;
892 selectedStackList = stackTaskList;
893 }
894 }
895 }
896 if (selectedStackList != null) {
897 list.add(selectedStackList.remove(0));
898 --maxNum;
899 } else {
900 break;
901 }
902 }
Craig Mautner20e72272013-04-01 13:45:53 -0700903 }
904
Craig Mautner23ac33b2013-04-01 16:26:35 -0700905 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700906 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700907 // Collect information about the target of the Intent.
908 ActivityInfo aInfo;
909 try {
910 ResolveInfo rInfo =
911 AppGlobals.getPackageManager().resolveIntent(
912 intent, resolvedType,
913 PackageManager.MATCH_DEFAULT_ONLY
914 | ActivityManagerService.STOCK_PM_FLAGS, userId);
915 aInfo = rInfo != null ? rInfo.activityInfo : null;
916 } catch (RemoteException e) {
917 aInfo = null;
918 }
919
920 if (aInfo != null) {
921 // Store the found target back into the intent, because now that
922 // we have it we never want to do this again. For example, if the
923 // user navigates back to this point in the history, we should
924 // always restart the exact same activity.
925 intent.setComponent(new ComponentName(
926 aInfo.applicationInfo.packageName, aInfo.name));
927
928 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700929 if (!aInfo.processName.equals("system")) {
930 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700931 mService.setDebugApp(aInfo.processName, true, false);
932 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700933
Man Caocfa78b22015-06-11 20:14:34 -0700934 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
935 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
936 }
937
938 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700939 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700940 }
941 }
942 }
943 return aInfo;
944 }
945
Craig Mautner299f9602015-01-26 09:47:33 -0800946 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
947 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700948 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
949 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
950 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
951 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700952 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
953 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700954 null /* outActivity */, null /* container */, null /* inTask */);
955 if (inResumeTopActivity) {
956 // If we are in resume section already, home activity will be initialized, but not
957 // resumed (to avoid recursive resume) and will stay that way until something pokes it
958 // again. We need to schedule another resume.
959 scheduleResumeTopActivities();
960 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700961 }
962
Craig Mautner23ac33b2013-04-01 16:26:35 -0700963 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700964 String callingPackage, Intent intent, String resolvedType,
965 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700966 IBinder resultTo, String resultWho, int requestCode, int startFlags,
967 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700968 Bundle options, boolean ignoreTargetSecurity, int userId,
969 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700970 // Refuse possible leaked file descriptors
971 if (intent != null && intent.hasFileDescriptors()) {
972 throw new IllegalArgumentException("File descriptors passed in Intent");
973 }
974 boolean componentSpecified = intent.getComponent() != null;
975
976 // Don't modify the client's object!
977 intent = new Intent(intent);
978
979 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700980 ActivityInfo aInfo =
981 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700982
Craig Mautnere0a38842013-12-16 16:14:02 -0800983 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700984 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800985 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700986 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800987 // Cannot start a child activity if the parent is not resumed.
988 return ActivityManager.START_CANCELED;
989 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700990 final int realCallingPid = Binder.getCallingPid();
991 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700992 int callingPid;
993 if (callingUid >= 0) {
994 callingPid = -1;
995 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700996 callingPid = realCallingPid;
997 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700998 } else {
999 callingPid = callingUid = -1;
1000 }
1001
Craig Mautnere0a38842013-12-16 16:14:02 -08001002 final ActivityStack stack;
1003 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001004 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001005 } else {
1006 stack = container.mStack;
1007 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001008 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001009 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001010 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001011
1012 final long origId = Binder.clearCallingIdentity();
1013
1014 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001015 (aInfo.applicationInfo.privateFlags
1016 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001017 // This may be a heavy-weight process! Check to see if we already
1018 // have another, different heavy-weight process running.
1019 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1020 if (mService.mHeavyWeightProcess != null &&
1021 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1022 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001023 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001024 if (caller != null) {
1025 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1026 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001027 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001028 } else {
1029 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001030 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001031 + intent.toString());
1032 ActivityOptions.abort(options);
1033 return ActivityManager.START_PERMISSION_DENIED;
1034 }
1035 }
1036
1037 IIntentSender target = mService.getIntentSenderLocked(
1038 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001039 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001040 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1041 | PendingIntent.FLAG_ONE_SHOT, null);
1042
1043 Intent newIntent = new Intent();
1044 if (requestCode >= 0) {
1045 // Caller is requesting a result.
1046 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1047 }
1048 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1049 new IntentSender(target));
1050 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1051 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1052 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1053 hist.packageName);
1054 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1055 hist.task.taskId);
1056 }
1057 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1058 aInfo.packageName);
1059 newIntent.setFlags(intent.getFlags());
1060 newIntent.setClassName("android",
1061 HeavyWeightSwitcherActivity.class.getName());
1062 intent = newIntent;
1063 resolvedType = null;
1064 caller = null;
1065 callingUid = Binder.getCallingUid();
1066 callingPid = Binder.getCallingPid();
1067 componentSpecified = true;
1068 try {
1069 ResolveInfo rInfo =
1070 AppGlobals.getPackageManager().resolveIntent(
1071 intent, null,
1072 PackageManager.MATCH_DEFAULT_ONLY
1073 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1074 aInfo = rInfo != null ? rInfo.activityInfo : null;
1075 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1076 } catch (RemoteException e) {
1077 aInfo = null;
1078 }
1079 }
1080 }
1081 }
1082
Dianne Hackborn91097de2014-04-04 18:02:06 -07001083 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1084 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001085 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001086 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001087 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001088
Craig Mautner85c11a82014-07-17 12:38:18 -07001089 Binder.restoreCallingIdentity(origId);
1090
Craig Mautnerde4ef022013-04-07 19:01:33 -07001091 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001092 // If the caller also wants to switch to a new configuration,
1093 // do so now. This allows a clean switch, as we are waiting
1094 // for the current activity to pause (so we will not destroy
1095 // it), and have not yet started the next activity.
1096 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1097 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001098 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001099 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001100 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001101 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001102 }
1103
Craig Mautner23ac33b2013-04-01 16:26:35 -07001104 if (outResult != null) {
1105 outResult.result = res;
1106 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001107 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001108 do {
1109 try {
1110 mService.wait();
1111 } catch (InterruptedException e) {
1112 }
1113 } while (!outResult.timeout && outResult.who == null);
1114 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001115 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001116 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001117 outResult.timeout = false;
1118 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1119 outResult.totalTime = 0;
1120 outResult.thisTime = 0;
1121 } else {
1122 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001123 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001124 do {
1125 try {
1126 mService.wait();
1127 } catch (InterruptedException e) {
1128 }
1129 } while (!outResult.timeout && outResult.who == null);
1130 }
1131 }
1132 }
1133
1134 return res;
1135 }
1136 }
1137
1138 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1139 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1140 Bundle options, int userId) {
1141 if (intents == null) {
1142 throw new NullPointerException("intents is null");
1143 }
1144 if (resolvedTypes == null) {
1145 throw new NullPointerException("resolvedTypes is null");
1146 }
1147 if (intents.length != resolvedTypes.length) {
1148 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1149 }
1150
Craig Mautner23ac33b2013-04-01 16:26:35 -07001151
1152 int callingPid;
1153 if (callingUid >= 0) {
1154 callingPid = -1;
1155 } else if (caller == null) {
1156 callingPid = Binder.getCallingPid();
1157 callingUid = Binder.getCallingUid();
1158 } else {
1159 callingPid = callingUid = -1;
1160 }
1161 final long origId = Binder.clearCallingIdentity();
1162 try {
1163 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001164 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001165 for (int i=0; i<intents.length; i++) {
1166 Intent intent = intents[i];
1167 if (intent == null) {
1168 continue;
1169 }
1170
1171 // Refuse possible leaked file descriptors
1172 if (intent != null && intent.hasFileDescriptors()) {
1173 throw new IllegalArgumentException("File descriptors passed in Intent");
1174 }
1175
1176 boolean componentSpecified = intent.getComponent() != null;
1177
1178 // Don't modify the client's object!
1179 intent = new Intent(intent);
1180
1181 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001182 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001183 // TODO: New, check if this is correct
1184 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1185
1186 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001187 (aInfo.applicationInfo.privateFlags
1188 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001189 throw new IllegalArgumentException(
1190 "FLAG_CANT_SAVE_STATE not supported here");
1191 }
1192
1193 Bundle theseOptions;
1194 if (options != null && i == intents.length-1) {
1195 theseOptions = options;
1196 } else {
1197 theseOptions = null;
1198 }
Craig Mautner6170f732013-04-02 13:05:23 -07001199 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001200 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1201 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001202 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001203 if (res < 0) {
1204 return res;
1205 }
1206
1207 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1208 }
1209 }
1210 } finally {
1211 Binder.restoreCallingIdentity(origId);
1212 }
1213
1214 return ActivityManager.START_SUCCESS;
1215 }
1216
Craig Mautner2420ead2013-04-01 17:13:20 -07001217 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001218 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001219 throws RemoteException {
1220
Craig Mautner2568c3a2015-03-26 14:22:34 -07001221 if (andResume) {
1222 r.startFreezingScreenLocked(app, 0);
1223 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001224
Craig Mautner2568c3a2015-03-26 14:22:34 -07001225 // schedule launch ticks to collect information about slow apps.
1226 r.startLaunchTickingLocked();
1227 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001228
1229 // Have the window manager re-evaluate the orientation of
1230 // the screen based on the new activity order. Note that
1231 // as a result of this, it can call back into the activity
1232 // manager with a new orientation. We don't care about that,
1233 // because the activity is not currently running so we are
1234 // just restarting it anyway.
1235 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001236 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 mService.mConfiguration,
1238 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001239 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001240 }
1241
1242 r.app = app;
1243 app.waitingToKill = null;
1244 r.launchCount++;
1245 r.lastLaunchTime = SystemClock.uptimeMillis();
1246
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001247 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001248
1249 int idx = app.activities.indexOf(r);
1250 if (idx < 0) {
1251 app.activities.add(r);
1252 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001253 mService.updateLruProcessLocked(app, true, null);
1254 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001255
Craig Mautner15df08a2015-04-01 12:17:18 -07001256 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001257 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1258 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001259 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001260 }
1261
1262 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001263 try {
1264 if (app.thread == null) {
1265 throw new RemoteException();
1266 }
1267 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001268 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001269 if (andResume) {
1270 results = r.results;
1271 newIntents = r.newIntents;
1272 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001273 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1274 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1275 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 if (andResume) {
1277 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1278 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001279 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001281 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001282 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001283 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001284 }
1285 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1286 r.sleeping = false;
1287 r.forceNewConfig = false;
1288 mService.showAskCompatModeDialogLocked(r);
1289 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001290 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001291 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1292 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1293 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001294 final String profileFile = mService.mProfileFile;
1295 if (profileFile != null) {
1296 ParcelFileDescriptor profileFd = mService.mProfileFd;
1297 if (profileFd != null) {
1298 try {
1299 profileFd = profileFd.dup();
1300 } catch (IOException e) {
1301 if (profileFd != null) {
1302 try {
1303 profileFd.close();
1304 } catch (IOException o) {
1305 }
1306 profileFd = null;
1307 }
1308 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001309 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001310
1311 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1312 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001313 }
1314 }
1315 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001316
Craig Mautner2568c3a2015-03-26 14:22:34 -07001317 if (andResume) {
1318 app.hasShownUi = true;
1319 app.pendingUiClean = true;
1320 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001321 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001322 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001323 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001324 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001325 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001326 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001327
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001328 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001329 // This may be a heavy-weight process! Note that the package
1330 // manager will ensure that only activity can run in the main
1331 // process of the .apk, which is the only thing that will be
1332 // considered heavy-weight.
1333 if (app.processName.equals(app.info.packageName)) {
1334 if (mService.mHeavyWeightProcess != null
1335 && mService.mHeavyWeightProcess != app) {
1336 Slog.w(TAG, "Starting new heavy weight process " + app
1337 + " when already running "
1338 + mService.mHeavyWeightProcess);
1339 }
1340 mService.mHeavyWeightProcess = app;
1341 Message msg = mService.mHandler.obtainMessage(
1342 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1343 msg.obj = r;
1344 mService.mHandler.sendMessage(msg);
1345 }
1346 }
1347
1348 } catch (RemoteException e) {
1349 if (r.launchFailed) {
1350 // This is the second time we failed -- finish activity
1351 // and give up.
1352 Slog.e(TAG, "Second failure launching "
1353 + r.intent.getComponent().flattenToShortString()
1354 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001355 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001356 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1357 "2nd-crash", false);
1358 return false;
1359 }
1360
1361 // This is the first time we failed -- restart process and
1362 // retry.
1363 app.activities.remove(r);
1364 throw e;
1365 }
1366
1367 r.launchFailed = false;
1368 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001369 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001370 }
1371
1372 if (andResume) {
1373 // As part of the process of launching, ActivityThread also performs
1374 // a resume.
1375 stack.minimalResumeActivityLocked(r);
1376 } else {
1377 // This activity is not starting in the resumed state... which
1378 // should look like we asked it to pause+stop (but remain visible),
1379 // and it has done so and reported back the current icicle and
1380 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001381 if (DEBUG_STATES) Slog.v(TAG_STATES,
1382 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001383 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001384 r.stopped = true;
1385 }
1386
1387 // Launch the new version setup screen if needed. We do this -after-
1388 // launching the initial activity (that is, home), so that it can have
1389 // a chance to initialize itself while in the background, making the
1390 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001391 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001392 mService.startSetupActivityLocked();
1393 }
1394
Dianne Hackborn465fa392014-09-14 14:21:18 -07001395 // Update any services we are bound to that might care about whether
1396 // their client may have activities.
1397 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1398
Craig Mautner2420ead2013-04-01 17:13:20 -07001399 return true;
1400 }
1401
Craig Mautnere79d42682013-04-01 19:01:53 -07001402 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001403 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001404 // Is this activity's application already running?
1405 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001406 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001407
1408 r.task.stack.setLaunchTime(r);
1409
1410 if (app != null && app.thread != null) {
1411 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001412 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1413 || !"android".equals(r.info.packageName)) {
1414 // Don't add this if it is a platform component that is marked
1415 // to run in multiple processes, because this is actually
1416 // part of the framework so doesn't make sense to track as a
1417 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001418 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1419 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001420 }
George Mount2c92c972014-03-20 09:38:23 -07001421 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001422 return;
1423 } catch (RemoteException e) {
1424 Slog.w(TAG, "Exception when starting activity "
1425 + r.intent.getComponent().flattenToShortString(), e);
1426 }
1427
1428 // If a dead object exception was thrown -- fall through to
1429 // restart the application.
1430 }
1431
1432 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001433 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001434 }
1435
Craig Mautner6170f732013-04-02 13:05:23 -07001436 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001437 Intent intent, String resolvedType, ActivityInfo aInfo,
1438 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1439 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001440 int callingPid, int callingUid, String callingPackage,
1441 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001442 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1443 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001444 int err = ActivityManager.START_SUCCESS;
1445
1446 ProcessRecord callerApp = null;
1447 if (caller != null) {
1448 callerApp = mService.getRecordForAppLocked(caller);
1449 if (callerApp != null) {
1450 callingPid = callerApp.pid;
1451 callingUid = callerApp.info.uid;
1452 } else {
1453 Slog.w(TAG, "Unable to find app for caller " + caller
1454 + " (pid=" + callingPid + ") when starting: "
1455 + intent.toString());
1456 err = ActivityManager.START_PERMISSION_DENIED;
1457 }
1458 }
1459
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001460 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1461
Craig Mautner6170f732013-04-02 13:05:23 -07001462 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001463 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001464 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001465 + " on display " + (container == null ? (mFocusedStack == null ?
1466 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1467 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1468 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001469 }
1470
1471 ActivityRecord sourceRecord = null;
1472 ActivityRecord resultRecord = null;
1473 if (resultTo != null) {
1474 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001475 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1476 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001477 if (sourceRecord != null) {
1478 if (requestCode >= 0 && !sourceRecord.finishing) {
1479 resultRecord = sourceRecord;
1480 }
1481 }
1482 }
Craig Mautner6170f732013-04-02 13:05:23 -07001483
Dianne Hackborn91097de2014-04-04 18:02:06 -07001484 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001485
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001486 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001487 // Transfer the result target from the source activity to the new
1488 // one being started, including any failures.
1489 if (requestCode >= 0) {
1490 ActivityOptions.abort(options);
1491 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1492 }
1493 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001494 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1495 resultRecord = null;
1496 }
Craig Mautner6170f732013-04-02 13:05:23 -07001497 resultWho = sourceRecord.resultWho;
1498 requestCode = sourceRecord.requestCode;
1499 sourceRecord.resultTo = null;
1500 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001501 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001502 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001503 if (sourceRecord.launchedFromUid == callingUid) {
1504 // The new activity is being launched from the same uid as the previous
1505 // activity in the flow, and asking to forward its result back to the
1506 // previous. In this case the activity is serving as a trampoline between
1507 // the two, so we also want to update its launchedFromPackage to be the
1508 // same as the previous activity. Note that this is safe, since we know
1509 // these two packages come from the same uid; the caller could just as
1510 // well have supplied that same package name itself. This specifially
1511 // deals with the case of an intent picker/chooser being launched in the app
1512 // flow to redirect to an activity picked by the user, where we want the final
1513 // activity to consider it to have been launched by the previous app activity.
1514 callingPackage = sourceRecord.launchedFromPackage;
1515 }
Craig Mautner6170f732013-04-02 13:05:23 -07001516 }
1517
1518 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1519 // We couldn't find a class that can handle the given Intent.
1520 // That's the end of that!
1521 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1522 }
1523
1524 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1525 // We couldn't find the specific class specified in the Intent.
1526 // Also the end of the line.
1527 err = ActivityManager.START_CLASS_NOT_FOUND;
1528 }
1529
Dianne Hackborn91097de2014-04-04 18:02:06 -07001530 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1531 && sourceRecord.task.voiceSession != null) {
1532 // If this activity is being launched as part of a voice session, we need
1533 // to ensure that it is safe to do so. If the upcoming activity will also
1534 // be part of the voice session, we can only launch it if it has explicitly
1535 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001536 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001537 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1538 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001539 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001540 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1541 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001542 Slog.w(TAG,
1543 "Activity being started in current voice task does not support voice: "
1544 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001545 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1546 }
1547 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001548 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001549 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1550 }
1551 }
1552 }
1553
1554 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1555 // If the caller is starting a new voice session, just make sure the target
1556 // is actually allowing it to run this way.
1557 try {
1558 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1559 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001560 Slog.w(TAG,
1561 "Activity being started in new voice task does not support: "
1562 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001563 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1564 }
1565 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001566 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001567 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1568 }
1569 }
1570
louis_changcd5d1982014-08-01 10:09:08 +08001571 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1572
Craig Mautner6170f732013-04-02 13:05:23 -07001573 if (err != ActivityManager.START_SUCCESS) {
1574 if (resultRecord != null) {
1575 resultStack.sendActivityResultLocked(-1,
1576 resultRecord, resultWho, requestCode,
1577 Activity.RESULT_CANCELED, null);
1578 }
Craig Mautner6170f732013-04-02 13:05:23 -07001579 ActivityOptions.abort(options);
1580 return err;
1581 }
1582
Svetoslav7008b512015-06-24 18:47:07 -07001583 boolean abort = false;
1584
Svet Ganov99b60432015-06-27 13:15:22 -07001585 final int startAnyPerm = mService.checkPermission(
1586 START_ANY_ACTIVITY, callingPid, callingUid);
1587
1588 if (startAnyPerm != PERMISSION_GRANTED) {
1589 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001590 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001591 final int actionRestriction = getActionRestrictionForCallingPackage(
1592 intent.getAction(), callingPackage, callingPid, callingUid);
1593
1594 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1595 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1596 if (resultRecord != null) {
1597 resultStack.sendActivityResultLocked(-1,
1598 resultRecord, resultWho, requestCode,
1599 Activity.RESULT_CANCELED, null);
1600 }
1601 String msg;
1602 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1603 msg = "Permission Denial: starting " + intent.toString()
1604 + " from " + callerApp + " (pid=" + callingPid
1605 + ", uid=" + callingUid + ")" + " with revoked permission "
1606 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1607 } else if (!aInfo.exported) {
1608 msg = "Permission Denial: starting " + intent.toString()
1609 + " from " + callerApp + " (pid=" + callingPid
1610 + ", uid=" + callingUid + ")"
1611 + " not exported from uid " + aInfo.applicationInfo.uid;
1612 } else {
1613 msg = "Permission Denial: starting " + intent.toString()
1614 + " from " + callerApp + " (pid=" + callingPid
1615 + ", uid=" + callingUid + ")"
1616 + " requires " + aInfo.permission;
1617 }
1618 Slog.w(TAG, msg);
1619 throw new SecurityException(msg);
1620 }
1621
1622 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1623 String message = "Appop Denial: starting " + intent.toString()
1624 + " from " + callerApp + " (pid=" + callingPid
1625 + ", uid=" + callingUid + ")"
1626 + " requires " + AppOpsManager.permissionToOp(
1627 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1628 Slog.w(TAG, message);
1629 abort = true;
1630 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1631 String message = "Appop Denial: starting " + intent.toString()
1632 + " from " + callerApp + " (pid=" + callingPid
1633 + ", uid=" + callingUid + ")"
1634 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1635 Slog.w(TAG, message);
1636 abort = true;
1637 }
Svetoslav7008b512015-06-24 18:47:07 -07001638 }
1639
1640 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001641 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001642
Craig Mautner6170f732013-04-02 13:05:23 -07001643 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001644 try {
1645 // The Intent we give to the watcher has the extra data
1646 // stripped off, since it can contain private information.
1647 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001648 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001649 aInfo.applicationInfo.packageName);
1650 } catch (RemoteException e) {
1651 mService.mController = null;
1652 }
Ben Gruver5e207332013-04-03 17:41:37 -07001653 }
Craig Mautner6170f732013-04-02 13:05:23 -07001654
Ben Gruver5e207332013-04-03 17:41:37 -07001655 if (abort) {
1656 if (resultRecord != null) {
1657 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001658 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001659 }
Ben Gruver5e207332013-04-03 17:41:37 -07001660 // We pretend to the caller that it was really started, but
1661 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001662 ActivityOptions.abort(options);
1663 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001664 }
1665
1666 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001667 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001668 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001669 if (outActivity != null) {
1670 outActivity[0] = r;
1671 }
1672
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001673 if (r.appTimeTracker == null && sourceRecord != null) {
1674 // If the caller didn't specify an explicit time tracker, we want to continue
1675 // tracking under any it has.
1676 r.appTimeTracker = sourceRecord.appTimeTracker;
1677 }
1678
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001679 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001680 if (voiceSession == null && (stack.mResumedActivity == null
1681 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001682 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1683 realCallingPid, realCallingUid, "Activity start")) {
Robert Carr13997f52015-10-23 13:13:39 -07001684 PendingActivityLaunch pal = new PendingActivityLaunch(r,
1685 sourceRecord, startFlags, stack, callerApp);
Craig Mautneree36c772014-07-16 14:56:05 -07001686 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001687 ActivityOptions.abort(options);
1688 return ActivityManager.START_SWITCHES_CANCELED;
1689 }
1690 }
1691
1692 if (mService.mDidAppSwitch) {
1693 // This is the second allowed switch since we stopped switches,
1694 // so now just generally allow switches. Use case: user presses
1695 // home (switches disabled, switch to home, mDidAppSwitch now true);
1696 // user taps a home icon (coming from home so allowed, we hit here
1697 // and now allow anyone to switch again).
1698 mService.mAppSwitchesAllowedTime = 0;
1699 } else {
1700 mService.mDidAppSwitch = true;
1701 }
1702
Craig Mautneree36c772014-07-16 14:56:05 -07001703 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001704
Dianne Hackborn91097de2014-04-04 18:02:06 -07001705 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001706 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001707
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001708 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001709 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001710 // activity start, but we are not actually doing an activity
1711 // switch... just dismiss the keyguard now, because we
1712 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001713 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001714 }
1715 return err;
1716 }
1717
Svet Ganov99b60432015-06-27 13:15:22 -07001718 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001719 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001720 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1721 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001722 == PackageManager.PERMISSION_DENIED) {
1723 return ACTIVITY_RESTRICTION_PERMISSION;
1724 }
1725
Christopher Tateff7add02015-08-17 10:23:22 -07001726 if (activityInfo.permission == null) {
1727 return ACTIVITY_RESTRICTION_NONE;
1728 }
1729
Svet Ganov99b60432015-06-27 13:15:22 -07001730 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1731 if (opCode == AppOpsManager.OP_NONE) {
1732 return ACTIVITY_RESTRICTION_NONE;
1733 }
1734
1735 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1736 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001737 if (!ignoreTargetSecurity) {
1738 return ACTIVITY_RESTRICTION_APPOP;
1739 }
Svet Ganov99b60432015-06-27 13:15:22 -07001740 }
1741
1742 return ACTIVITY_RESTRICTION_NONE;
1743 }
1744
Svetoslav7008b512015-06-24 18:47:07 -07001745 private int getActionRestrictionForCallingPackage(String action,
1746 String callingPackage, int callingPid, int callingUid) {
1747 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001748 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001749 }
1750
1751 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1752 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001753 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001754 }
1755
1756 final PackageInfo packageInfo;
1757 try {
1758 packageInfo = mService.mContext.getPackageManager()
1759 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1760 } catch (PackageManager.NameNotFoundException e) {
1761 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001762 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001763 }
1764
1765 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001766 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001767 }
1768
1769 if (mService.checkPermission(permission, callingPid, callingUid) ==
1770 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001771 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001772 }
1773
1774 final int opCode = AppOpsManager.permissionToOpCode(permission);
1775 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001776 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001777 }
1778
1779 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1780 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001781 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001782 }
1783
Svet Ganov99b60432015-06-27 13:15:22 -07001784 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001785 }
1786
Chong Zhang0fa656b2015-08-31 15:17:21 -07001787 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001788 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001789
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001790 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1791 return mHomeStack;
1792 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001793
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001794 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001795
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001796 if (task != null && task.stack != null) {
1797 stack = task.stack;
1798 if (stack.isOnHomeDisplay()) {
1799 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001800 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001801 "computeStackFocus: Setting " + "focused stack to r=" + r
1802 + " task=" + task);
1803 } else {
1804 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1805 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001806 }
1807 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001808 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001809 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001810
1811 final ActivityContainer container = r.mInitialActivityContainer;
1812 if (container != null) {
1813 // The first time put it on the desired stack, after this put on task stack.
1814 r.mInitialActivityContainer = null;
1815 return container.mStack;
1816 }
1817
1818 // The fullscreen stack can contain any task regardless of if the task is resizeable
1819 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1820 // If the freeform stack has focus, and the activity to be launched is resizeable,
1821 // we can also put it in the focused stack.
1822 final boolean canUseFocusedStack =
1823 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1824 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1825 if (canUseFocusedStack
1826 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1827 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1828 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1829 return mFocusedStack;
1830 }
1831
1832 // We first try to put the task in the first dynamic stack.
1833 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1834 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1835 stack = homeDisplayStacks.get(stackNdx);
1836 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1837 if (isDynamicStack) {
1838 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1839 "computeStackFocus: Setting focused stack=" + stack);
1840 return stack;
1841 }
1842 }
1843
1844 // If there is no suitable dynamic stack then we figure out which static stack to use.
1845 final int stackId = task != null ? task.getLaunchStackId() :
1846 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1847 FULLSCREEN_WORKSPACE_STACK_ID;
1848 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1849 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1850 + r + " stackId=" + stack.mStackId);
1851 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001852 }
1853
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001854 boolean setFocusedStack(ActivityRecord r, String reason) {
1855 if (r == null) {
1856 // Not sure what you are trying to do, but it is not going to work...
1857 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001858 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001859 final TaskRecord task = r.task;
1860 if (task == null || task.stack == null) {
1861 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1862 return false;
1863 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001864 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001865 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001866 }
1867
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001868 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001869 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001870 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001871 final Intent intent = r.intent;
1872 final int callingUid = r.launchedFromUid;
1873
Chong Zhang0fa656b2015-08-31 15:17:21 -07001874 boolean overrideBounds = false;
1875 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001876 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
1877 ActivityOptions opts = new ActivityOptions(options);
1878 if (opts.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001879 overrideBounds = true;
Chong Zhang56f171d2015-10-08 10:39:23 -07001880 newBounds = opts.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001881 }
1882 }
1883
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001884 // In some flows in to this function, we retrieve the task record and hold on to it
1885 // without a lock before calling back in to here... so the task at this point may
1886 // not actually be in recents. Check for that, and if it isn't in recents just
1887 // consider it invalid.
1888 if (inTask != null && !inTask.inRecents) {
1889 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1890 inTask = null;
1891 }
1892
Craig Mautnerad400af2014-08-13 16:41:36 -07001893 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001894 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1895 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001896
Craig Mautnera228ae92014-07-09 05:44:55 -07001897 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001898 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001899 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001900 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1901 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1902 "\"singleInstance\" or \"singleTask\"");
1903 launchFlags &=
1904 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1905 } else {
1906 switch (r.info.documentLaunchMode) {
1907 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1908 break;
1909 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1910 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1911 break;
1912 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1913 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1914 break;
1915 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1916 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1917 break;
1918 }
1919 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001920
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001921 final boolean launchTaskBehind = r.mLaunchTaskBehind
1922 && !launchSingleTask && !launchSingleInstance
1923 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001924
Wale Ogunwale7d701172015-03-11 15:36:30 -07001925 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1926 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001927 // For whatever reason this activity is being launched into a new
1928 // task... yet the caller has requested a result back. Well, that
1929 // is pretty messed up, so instead immediately send back a cancel
1930 // and let the new task continue launched as normal without a
1931 // dependency on its originator.
1932 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1933 r.resultTo.task.stack.sendActivityResultLocked(-1,
1934 r.resultTo, r.resultWho, r.requestCode,
1935 Activity.RESULT_CANCELED, null);
1936 r.resultTo = null;
1937 }
1938
1939 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1940 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1941 }
1942
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001943 // If we are actually going to launch in to a new task, there are some cases where
1944 // we further want to do multiple task.
1945 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1946 if (launchTaskBehind
1947 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1948 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1949 }
1950 }
1951
Craig Mautner8849a5e2013-04-02 16:41:03 -07001952 // We'll invoke onUserLeaving before onPause only if the launching
1953 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001954 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001955 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1956 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001957
1958 // If the caller has asked not to resume at this point, we make note
1959 // of this in the record so that we can skip it when trying to find
1960 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001961 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001962 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001963 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001964 }
1965
Craig Mautnera254cd72014-05-25 16:47:39 -07001966 ActivityRecord notTop =
1967 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001968
1969 // If the onlyIfNeeded flag is set, then we can do this if the activity
1970 // being launched is the same as the one making the call... or, as
1971 // a special case, if we do not know the caller then we count the
1972 // current top activity as the caller.
1973 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1974 ActivityRecord checkedCaller = sourceRecord;
1975 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001976 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001977 }
1978 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1979 // Caller is not the same as launcher, so always needed.
1980 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1981 }
1982 }
1983
Craig Mautner8849a5e2013-04-02 16:41:03 -07001984 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001985 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001986
1987 // If the caller is not coming from another activity, but has given us an
1988 // explicit task into which they would like us to launch the new activity,
1989 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001990 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1991 final Intent baseIntent = inTask.getBaseIntent();
1992 final ActivityRecord root = inTask.getRootActivity();
1993 if (baseIntent == null) {
1994 ActivityOptions.abort(options);
1995 throw new IllegalArgumentException("Launching into task without base intent: "
1996 + inTask);
1997 }
1998
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001999 // If this task is empty, then we are adding the first activity -- it
2000 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002001 if (launchSingleInstance || launchSingleTask) {
2002 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
2003 ActivityOptions.abort(options);
2004 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2005 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002006 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002007 if (root != null) {
2008 ActivityOptions.abort(options);
2009 throw new IllegalArgumentException("Caller with inTask " + inTask
2010 + " has root " + root + " but target is singleInstance/Task");
2011 }
2012 }
2013
2014 // If task is empty, then adopt the interesting intent launch flags in to the
2015 // activity being started.
2016 if (root == null) {
2017 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2018 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2019 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2020 launchFlags = (launchFlags&~flagsOfInterest)
2021 | (baseIntent.getFlags()&flagsOfInterest);
2022 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002023 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002024 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002025
Dianne Hackborn962d5352014-08-29 16:27:40 -07002026 // If the task is not empty and the caller is asking to start it as the root
2027 // of a new task, then we don't actually want to start this on the task. We
2028 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002029 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002030 addingToTask = false;
2031
2032 } else {
2033 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002034 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002035
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002036 reuseTask = inTask;
2037 } else {
2038 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002039 // Launch ResolverActivity in the source task, so that it stays in the task
2040 // bounds when in freeform workspace.
2041 // Also put noDisplay activities in the source task. These by itself can
2042 // be placed in any task/stack, however it could launch other activities
2043 // like ResolverActivity, and we want those to stay in the original task.
2044 if (r.isResolverActivity() || r.noDisplay) {
2045 addingToTask = true;
2046 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002047 }
2048
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002049 if (inTask == null) {
2050 if (sourceRecord == null) {
2051 // This activity is not being started from another... in this
2052 // case we -always- start a new task.
2053 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2054 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2055 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2056 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2057 }
2058 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2059 // The original activity who is starting us is running as a single
2060 // instance... this new activity it is starting must go on its
2061 // own task.
2062 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2063 } else if (launchSingleInstance || launchSingleTask) {
2064 // The activity being started is a single instance... it always
2065 // gets launched into its own task.
2066 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2067 }
2068 }
2069
2070 ActivityInfo newTaskInfo = null;
2071 Intent newTaskIntent = null;
2072 ActivityStack sourceStack;
2073 if (sourceRecord != null) {
2074 if (sourceRecord.finishing) {
2075 // If the source is finishing, we can't further count it as our source. This
2076 // is because the task it is associated with may now be empty and on its way out,
2077 // so we don't want to blindly throw it in to that task. Instead we will take
2078 // the NEW_TASK flow and try to find a task for it. But save the task information
2079 // so it can be used when creating the new task.
2080 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2081 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2082 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2083 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2084 newTaskInfo = sourceRecord.info;
2085 newTaskIntent = sourceRecord.task.intent;
2086 }
2087 sourceRecord = null;
2088 sourceStack = null;
2089 } else {
2090 sourceStack = sourceRecord.task.stack;
2091 }
2092 } else {
2093 sourceStack = null;
2094 }
2095
2096 boolean movedHome = false;
2097 ActivityStack targetStack;
2098
2099 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002100 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002101
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002102 // We may want to try to place the new activity in to an existing task. We always
2103 // do this if the target activity is singleTask or singleInstance; we will also do
2104 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2105 // us to still place it in a new task: multi task, always doc mode, or being asked to
2106 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002107 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2108 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002109 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002110 // If bring to front is requested, and no result is requested and we have not
2111 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002112 // we can find a task that was started with this same
2113 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002114 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002115 // See if there is a task to bring to the front. If this is
2116 // a SINGLE_INSTANCE activity, there can be one and only one
2117 // instance of it in the history, and it is always in its own
2118 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002119 ActivityRecord intentActivity = !launchSingleInstance ?
2120 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002121 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002122 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2123 // but still needs to be a lock task mode violation since the task gets
2124 // cleared out and the device would otherwise leave the locked task.
2125 if (isLockTaskModeViolation(intentActivity.task,
2126 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2127 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002128 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002129 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002130 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2131 }
Craig Mautner29219d92013-04-16 20:19:12 -07002132 if (r.task == null) {
2133 r.task = intentActivity.task;
2134 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002135 if (intentActivity.task.intent == null) {
2136 // This task was started because of movement of
2137 // the activity based on affinity... now that we
2138 // are actually launching it, we can assign the
2139 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002140 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002141 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002142 targetStack = intentActivity.task.stack;
2143 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002144 // If the target task is not in the front, then we need
2145 // to bring it to the front... except... well, with
2146 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2147 // to have the same behavior as if a new instance was
2148 // being started, which means not bringing it to the front
2149 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002150 final ActivityStack focusStack = getFocusedStack();
2151 ActivityRecord curTop = (focusStack == null)
2152 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002153 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002154 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002155 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002156 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002157 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2158 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002159 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002160 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002161 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2162 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002164 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002165 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002166 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002167 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002168 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2169 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002170 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002171 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002172 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002173 options = null;
2174 }
2175 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002176 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002177 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002178 + " from " + intentActivity);
2179 targetStack.moveToFront("intentActivityFound");
2180 }
2181
Craig Mautner8849a5e2013-04-02 16:41:03 -07002182 // If the caller has requested that the target task be
2183 // reset, then do so.
2184 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2185 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2186 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002187 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002188 // We don't need to start a new activity, and
2189 // the client said not to do anything if that
2190 // is the case, so this is it! And for paranoia, make
2191 // sure we have correctly resumed the top activity.
2192 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002193 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002194
2195 // Make sure to notify Keyguard as well if we are not running an app
2196 // transition later.
2197 if (!movedToFront) {
2198 notifyActivityDrawnForKeyguard();
2199 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002200 } else {
2201 ActivityOptions.abort(options);
2202 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002203 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002204 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2205 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002206 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002207 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002208 // The caller has requested to completely replace any
2209 // existing task with its new activity. Well that should
2210 // not be too hard...
2211 reuseTask = intentActivity.task;
2212 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002213 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002214 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002215 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002216 // In this situation we want to remove all activities
2217 // from the task up to the one being started. In most
2218 // cases this means we are resetting the task to its
2219 // initial state.
2220 ActivityRecord top =
2221 intentActivity.task.performClearTaskLocked(r, launchFlags);
2222 if (top != null) {
2223 if (top.frontOfTask) {
2224 // Activity aliases may mean we use different
2225 // intents for the top activity, so make sure
2226 // the task now has the identity of the new
2227 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002228 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002229 }
2230 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2231 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002232 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002233 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002234 // A special case: we need to start the activity because it is not
2235 // currently running, and the caller has asked to clear the current
2236 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002237 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002238 // Now pretend like this activity is being started by the top of its
2239 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002240 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002241 TaskRecord task = sourceRecord.task;
2242 if (task != null && task.stack == null) {
2243 // Target stack got cleared when we all activities were removed
2244 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002245 targetStack = computeStackFocus(
2246 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002247 targetStack.addTask(
2248 task, !launchTaskBehind /* toTop */, false /* moving */);
2249 }
2250
Craig Mautner8849a5e2013-04-02 16:41:03 -07002251 }
2252 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2253 // In this case the top activity on the task is the
2254 // same as the one being launched, so we take that
2255 // as a request to bring the task to the foreground.
2256 // If the top activity in the task is the root
2257 // activity, deliver this new intent to it if it
2258 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002259 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002260 && intentActivity.realActivity.equals(r.realActivity)) {
2261 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2262 intentActivity.task);
2263 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002264 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002265 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002266 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2267 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002268 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2269 // In this case we are launching the root activity
2270 // of the task, but with a different intent. We
2271 // should start a new instance on top.
2272 addingToTask = true;
2273 sourceRecord = intentActivity;
2274 }
2275 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2276 // In this case an activity is being launched in to an
2277 // existing task, without resetting that task. This
2278 // is typically the situation of launching an activity
2279 // from a notification or shortcut. We want to place
2280 // the new activity on top of the current task.
2281 addingToTask = true;
2282 sourceRecord = intentActivity;
2283 } else if (!intentActivity.task.rootWasReset) {
2284 // In this case we are launching in to an existing task
2285 // that has not yet been started from its front door.
2286 // The current task has been brought to the front.
2287 // Ideally, we'd probably like to place this new task
2288 // at the bottom of its stack, but that's a little hard
2289 // to do with the current organization of the code so
2290 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002291 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002292 }
2293 if (!addingToTask && reuseTask == null) {
2294 // We didn't do anything... but it was needed (a.k.a., client
2295 // don't use that intent!) And for paranoia, make
2296 // sure we have correctly resumed the top activity.
2297 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002298 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002299 if (!movedToFront) {
2300 // Make sure to notify Keyguard as well if we are not running an app
2301 // transition later.
2302 notifyActivityDrawnForKeyguard();
2303 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002304 } else {
2305 ActivityOptions.abort(options);
2306 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002307 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002308 return ActivityManager.START_TASK_TO_FRONT;
2309 }
2310 }
2311 }
2312 }
2313
2314 //String uri = r.intent.toURI();
2315 //Intent intent2 = new Intent(uri);
2316 //Slog.i(TAG, "Given intent: " + r.intent);
2317 //Slog.i(TAG, "URI is: " + uri);
2318 //Slog.i(TAG, "To intent: " + intent2);
2319
2320 if (r.packageName != null) {
2321 // If the activity being launched is the same as the one currently
2322 // at the top, then we need to check if it should only be launched
2323 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002324 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002325 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002326 if (top != null && r.resultTo == null) {
2327 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2328 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002329 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002330 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002331 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2332 top.task);
2333 // For paranoia, make sure we have correctly
2334 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002335 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002336 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002337 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002338 }
2339 ActivityOptions.abort(options);
2340 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2341 // We don't need to start a new activity, and
2342 // the client said not to do anything if that
2343 // is the case, so this is it!
2344 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2345 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002346 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002347 return ActivityManager.START_DELIVERED_TO_TOP;
2348 }
2349 }
2350 }
2351 }
2352
2353 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002354 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002355 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2356 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002357 }
2358 ActivityOptions.abort(options);
2359 return ActivityManager.START_CLASS_NOT_FOUND;
2360 }
2361
2362 boolean newTask = false;
2363 boolean keepCurTransition = false;
2364
Craig Mautnerbb742462014-07-07 15:28:55 -07002365 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002366 sourceRecord.task : null;
2367
Craig Mautner8849a5e2013-04-02 16:41:03 -07002368 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002369 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002370 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002371 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002372 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002373 if (doResume) {
2374 targetStack.moveToFront("startingNewTask");
2375 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002376
Craig Mautner8849a5e2013-04-02 16:41:03 -07002377 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002378 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2379 newTaskInfo != null ? newTaskInfo : r.info,
2380 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002381 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2382 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002383 if (overrideBounds) {
2384 r.task.updateOverrideConfiguration(newBounds);
2385 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002386 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2387 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002388 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002389 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002390 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002391 if (isLockTaskModeViolation(r.task)) {
2392 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2393 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2394 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002395 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002396 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002397 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2398 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002399 // Caller wants to appear on home activity, so before starting
2400 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002401 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002402 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002403 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002404 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002405 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002406 if (isLockTaskModeViolation(sourceTask)) {
2407 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2408 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2409 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002410 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002411 if (doResume) {
2412 targetStack.moveToFront("sourceStackToFront");
2413 }
Craig Mautner737fae22014-10-15 12:52:10 -07002414 final TaskRecord topTask = targetStack.topTask();
2415 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002416 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002417 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002418 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002419 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002420 // In this case, we are adding the activity to an existing
2421 // task, but the caller has asked to clear that task if the
2422 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002423 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002424 keepCurTransition = true;
2425 if (top != null) {
2426 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002427 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002428 // For paranoia, make sure we have correctly
2429 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002430 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002431 if (doResume) {
2432 targetStack.resumeTopActivityLocked(null);
2433 }
2434 ActivityOptions.abort(options);
2435 return ActivityManager.START_DELIVERED_TO_TOP;
2436 }
2437 } else if (!addingToTask &&
2438 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2439 // In this case, we are launching an activity in our own task
2440 // that may already be running somewhere in the history, and
2441 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002442 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002443 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002444 final TaskRecord task = top.task;
2445 task.moveActivityToFrontLocked(top);
2446 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002447 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002448 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002449 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002450 if (doResume) {
2451 targetStack.resumeTopActivityLocked(null);
2452 }
2453 return ActivityManager.START_DELIVERED_TO_TOP;
2454 }
2455 }
2456 // An existing activity is starting this new activity, so we want
2457 // to keep the new one in the same task as the one that is starting
2458 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002459 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002460 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002461 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002462
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002463 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002464 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002465 // task it has provided to us.
2466 if (isLockTaskModeViolation(inTask)) {
2467 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2468 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2469 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002470 if (overrideBounds) {
2471 inTask.updateOverrideConfiguration(newBounds);
2472 int stackId = inTask.getLaunchStackId();
2473 if (stackId != inTask.stack.mStackId) {
2474 moveTaskToStackUncheckedLocked(
2475 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2476 }
2477 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002478 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002479 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2480 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002481
2482 // Check whether we should actually launch the new activity in to the task,
2483 // or just reuse the current activity on top.
2484 ActivityRecord top = inTask.getTopActivity();
2485 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2486 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2487 || launchSingleTop || launchSingleTask) {
2488 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2489 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2490 // We don't need to start a new activity, and
2491 // the client said not to do anything if that
2492 // is the case, so this is it!
2493 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2494 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002495 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002496 return ActivityManager.START_DELIVERED_TO_TOP;
2497 }
2498 }
2499
Dianne Hackborn962d5352014-08-29 16:27:40 -07002500 if (!addingToTask) {
2501 // We don't actually want to have this activity added to the task, so just
2502 // stop here but still tell the caller that we consumed the intent.
2503 ActivityOptions.abort(options);
2504 return ActivityManager.START_TASK_TO_FRONT;
2505 }
2506
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002507 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002508 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002509 + " in explicit task " + r.task);
2510
Craig Mautner8849a5e2013-04-02 16:41:03 -07002511 } else {
2512 // This not being started from an existing activity, and not part
2513 // of a new task... just put it in the top task, though these days
2514 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002515 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002516 if (doResume) {
2517 targetStack.moveToFront("addingToTopTask");
2518 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002519 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002520 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002521 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002522 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002523 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002524 + " in new guessed " + r.task);
2525 }
2526
2527 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002528 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002529
Craig Mautner76e2a762014-06-24 09:05:43 -07002530 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2531 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2532 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002533 if (newTask) {
2534 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2535 }
2536 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002537 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002538 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002539 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002540 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002541 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002542 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002543 return ActivityManager.START_SUCCESS;
2544 }
2545
Craig Mautneree36c772014-07-16 14:56:05 -07002546 final void doPendingActivityLaunchesLocked(boolean doResume) {
2547 while (!mPendingActivityLaunches.isEmpty()) {
2548 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002549
2550 try {
2551 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Robert Carr13997f52015-10-23 13:13:39 -07002552 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002553 } catch (Exception e) {
Robert Carr13997f52015-10-23 13:13:39 -07002554 Slog.e(TAG, "Exception during pending activity launch pal=" + pal, e);
2555 pal.sendErrorResult(e.getMessage());
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002556 }
Craig Mautneree36c772014-07-16 14:56:05 -07002557 }
2558 }
2559
Craig Mautner7f13ed32014-07-28 14:00:35 -07002560 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002561 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2562 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002563 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002564 mPendingActivityLaunches.remove(palNdx);
2565 }
2566 }
2567 }
2568
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002569 void setLaunchSource(int uid) {
2570 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2571 }
2572
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002573 void acquireLaunchWakelock() {
2574 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2575 throw new IllegalStateException("Calling must be system uid");
2576 }
2577 mLaunchingActivity.acquire();
2578 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2579 // To be safe, don't allow the wake lock to be held for too long.
2580 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2581 }
2582 }
2583
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002584 /**
2585 * Called when the frontmost task is idle.
2586 * @return the state of mService.mBooting before this was called.
2587 */
2588 private boolean checkFinishBootingLocked() {
2589 final boolean booting = mService.mBooting;
2590 boolean enableScreen = false;
2591 mService.mBooting = false;
2592 if (!mService.mBooted) {
2593 mService.mBooted = true;
2594 enableScreen = true;
2595 }
2596 if (booting || enableScreen) {
2597 mService.postFinishBooting(booting, enableScreen);
2598 }
2599 return booting;
2600 }
2601
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 // Checked.
2603 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2604 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002605 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002606
Craig Mautnerf3333272013-04-22 10:55:53 -07002607 ArrayList<ActivityRecord> stops = null;
2608 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002609 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002610 int NS = 0;
2611 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002612 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002613 boolean activityRemoved = false;
2614
Wale Ogunwale7d701172015-03-11 15:36:30 -07002615 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002616 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002617 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2618 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002619 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2620 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002621 if (fromTimeout) {
2622 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002623 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002624
2625 // This is a hack to semi-deal with a race condition
2626 // in the client where it can be constructed with a
2627 // newer configuration from when we asked it to launch.
2628 // We'll update with whatever configuration it now says
2629 // it used to launch.
2630 if (config != null) {
2631 r.configuration = config;
2632 }
2633
2634 // We are now idle. If someone is waiting for a thumbnail from
2635 // us, we can now deliver.
2636 r.idle = true;
2637
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002638 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002639 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002640 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002641 }
2642 }
2643
2644 if (allResumedActivitiesIdle()) {
2645 if (r != null) {
2646 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002647 }
2648
2649 if (mLaunchingActivity.isHeld()) {
2650 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2651 if (VALIDATE_WAKE_LOCK_CALLER &&
2652 Binder.getCallingUid() != Process.myUid()) {
2653 throw new IllegalStateException("Calling must be system uid");
2654 }
2655 mLaunchingActivity.release();
2656 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002657 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002658 }
2659
2660 // Atomically retrieve all of the other things to do.
2661 stops = processStoppingActivitiesLocked(true);
2662 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002663 if ((NF = mFinishingActivities.size()) > 0) {
2664 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002665 mFinishingActivities.clear();
2666 }
2667
Craig Mautnerf3333272013-04-22 10:55:53 -07002668 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002669 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002670 mStartingUsers.clear();
2671 }
2672
Craig Mautnerf3333272013-04-22 10:55:53 -07002673 // Stop any activities that are scheduled to do so but have been
2674 // waiting for the next one to start.
2675 for (int i = 0; i < NS; i++) {
2676 r = stops.get(i);
2677 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002678 if (stack != null) {
2679 if (r.finishing) {
2680 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2681 } else {
2682 stack.stopActivityLocked(r);
2683 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002684 }
2685 }
2686
2687 // Finish any activities that are scheduled to do so but have been
2688 // waiting for the next one to start.
2689 for (int i = 0; i < NF; i++) {
2690 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002691 final ActivityStack stack = r.task.stack;
2692 if (stack != null) {
2693 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2694 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002695 }
2696
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002697 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002698 // Complete user switch
2699 if (startingUsers != null) {
2700 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002701 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002702 }
2703 }
2704 // Complete starting up of background users
2705 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002706 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002707 mStartingBackgroundUsers.clear();
2708 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002709 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002710 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002711 }
2712 }
2713
2714 mService.trimApplications();
2715 //dump();
2716 //mWindowManager.dump();
2717
Craig Mautnerf3333272013-04-22 10:55:53 -07002718 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002719 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002720 }
2721
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002722 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002723 }
2724
Craig Mautner8e569572013-10-11 17:36:59 -07002725 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002726 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002727 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2728 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002729 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2730 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2731 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002732 }
Craig Mautner19091252013-10-05 00:03:53 -07002733 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002734 }
2735
2736 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2738 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002739 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2740 stacks.get(stackNdx).closeSystemDialogsLocked();
2741 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002742 }
2743 }
2744
Craig Mautner93529a42013-10-04 15:03:13 -07002745 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002746 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002747 }
2748
Craig Mautner8d341ef2013-03-26 09:03:27 -07002749 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002750 * Update the last used stack id for non-current user (current user's last
2751 * used stack is the focused stack)
2752 */
2753 void updateUserStackLocked(int userId, ActivityStack stack) {
2754 if (userId != mCurrentUser) {
2755 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2756 }
2757 }
2758
2759 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002760 * @return true if some activity was finished (or would have finished if doit were true).
2761 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002762 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2763 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002764 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002765 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2766 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002767 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002768 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002769 if (stack.finishDisabledPackageActivitiesLocked(
2770 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002771 didSomething = true;
2772 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002773 }
2774 }
2775 return didSomething;
2776 }
2777
Dianne Hackborna413dc02013-07-12 12:02:55 -07002778 void updatePreviousProcessLocked(ActivityRecord r) {
2779 // Now that this process has stopped, we may want to consider
2780 // it to be the previous app to try to keep around in case
2781 // the user wants to return to it.
2782
2783 // First, found out what is currently the foreground app, so that
2784 // we don't blow away the previous app if this activity is being
2785 // hosted by the process that is actually still the foreground.
2786 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002787 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2788 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002789 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2790 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002791 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002792 if (stack.mResumedActivity != null) {
2793 fgApp = stack.mResumedActivity.app;
2794 } else if (stack.mPausingActivity != null) {
2795 fgApp = stack.mPausingActivity.app;
2796 }
2797 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002798 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002799 }
2800 }
2801
2802 // Now set this one as the previous process, only if that really
2803 // makes sense to.
2804 if (r.app != null && fgApp != null && r.app != fgApp
2805 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002806 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002807 mService.mPreviousProcess = r.app;
2808 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2809 }
2810 }
2811
Craig Mautner05d29032013-05-03 13:40:13 -07002812 boolean resumeTopActivitiesLocked() {
2813 return resumeTopActivitiesLocked(null, null, null);
2814 }
2815
2816 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2817 Bundle targetOptions) {
2818 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002819 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002820 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002821 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002822 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002823 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002824 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2825 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002826
Craig Mautnere0a38842013-12-16 16:14:02 -08002827 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2828 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002829 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2830 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002831 if (stack == targetStack) {
2832 // Already started above.
2833 continue;
2834 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002835 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002836 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002837 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002838 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002839 }
Craig Mautner05d29032013-05-03 13:40:13 -07002840 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002841 }
2842
Todd Kennedy539db512014-12-15 09:57:55 -08002843 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002844 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2845 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002846 final int numStacks = stacks.size();
2847 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2848 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002849 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002850 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002851 }
2852 }
2853
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002854 void finishVoiceTask(IVoiceInteractionSession session) {
2855 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2856 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2857 final int numStacks = stacks.size();
2858 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2859 final ActivityStack stack = stacks.get(stackNdx);
2860 stack.finishVoiceTask(session);
2861 }
2862 }
2863 }
2864
Craig Mautner299f9602015-01-26 09:47:33 -08002865 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002866 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2867 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002868 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002869 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2870 // Caller wants the home activity moved with it. To accomplish this,
2871 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002872 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002873 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002874 if (task.stack == null) {
2875 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2876 + task + " to front. Stack is null");
2877 return;
2878 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002879
2880 int stackId = task.stack.mStackId;
2881 if (task.mResizeable && options != null) {
2882 ActivityOptions opts = new ActivityOptions(options);
2883 if (opts.hasBounds()) {
2884 Rect bounds = opts.getBounds();
2885 task.updateOverrideConfiguration(bounds);
Chong Zhang87b21722015-09-21 15:39:51 -07002886 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2887 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002888 stackId = task.getLaunchStackId();
2889 }
2890 }
2891
2892 if (stackId != task.stack.mStackId) {
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002893 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2894
2895 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2896 // still need moveTaskToFrontLocked() below for any transition settings.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002897 }
2898
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002899 final ActivityRecord r = task.getTopActivity();
2900 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2901 r == null ? null : r.appTimeTracker, reason);
2902
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002903 if (DEBUG_STACK) Slog.d(TAG_STACK,
2904 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002905 }
2906
Craig Mautner967212c2013-04-13 21:10:58 -07002907 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002908 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002909 }
2910
2911 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002912 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2913 if (activityContainer != null) {
2914 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002915 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002916 if (!createStaticStackIfNeeded
2917 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2918 return null;
2919 }
2920 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002921 }
2922
Craig Mautner967212c2013-04-13 21:10:58 -07002923 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002924 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002925 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2926 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002927 }
2928 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002929 }
2930
Craig Mautner4a1cb222013-12-04 16:14:06 -08002931 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002932 ActivityRecord homeActivity = getHomeActivity();
2933 if (homeActivity != null) {
2934 return homeActivity.appToken;
2935 }
2936 return null;
2937 }
2938
2939 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002940 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002941 }
2942
2943 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002944 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2945 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2946 final TaskRecord task = tasks.get(taskNdx);
2947 if (task.isHomeTask()) {
2948 final ArrayList<ActivityRecord> activities = task.mActivities;
2949 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2950 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002951 if (r.isHomeActivity()
2952 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002953 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002954 }
2955 }
2956 }
2957 }
2958 return null;
2959 }
2960
Todd Kennedyca4d8422015-01-15 15:19:22 -08002961 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002962 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002963 ActivityContainer activityContainer =
2964 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002965 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002966 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2967 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002968 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002969 return activityContainer;
2970 }
2971
Craig Mautner34b73df2014-01-12 21:11:08 -08002972 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002973 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2974 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2975 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002976 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2977 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002978 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002979 }
2980 }
2981
Craig Mautner95da1082014-02-24 17:54:35 -08002982 void deleteActivityContainer(IActivityContainer container) {
2983 ActivityContainer activityContainer = (ActivityContainer)container;
2984 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002985 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2986 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002987 final int stackId = activityContainer.mStackId;
2988 mActivityContainers.remove(stackId);
2989 mWindowManager.removeStack(stackId);
2990 }
2991 }
2992
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002993 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
2994 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002995 final ActivityStack stack = getStack(stackId);
2996 if (stack == null) {
2997 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2998 return;
2999 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003000
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07003001 if (!allowResizeInDockedMode
3002 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
3003 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
3004 // stack size changing so things don't get out of sync.
3005 return;
3006 }
3007
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003008 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003009 mWindowManager.deferSurfaceLayout();
3010 try {
3011 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003012
Jorim Jaggic4025202015-10-22 16:43:34 +02003013 mTmpBounds.clear();
3014 mTmpConfigs.clear();
3015 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3016 for (int i = tasks.size() - 1; i >= 0; i--) {
3017 TaskRecord task = tasks.get(i);
3018 if (task.mResizeable) {
3019 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3020 // For freeform stack we don't adjust the size of the tasks to match that
3021 // of the stack, but we do try to make sure the tasks are still contained
3022 // with the bounds of the stack.
3023 tempRect2.set(task.mBounds);
3024 fitWithinBounds(tempRect2, bounds);
3025 task.updateOverrideConfiguration(tempRect2);
3026 } else {
3027 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003028 }
3029 }
3030
Jorim Jaggic4025202015-10-22 16:43:34 +02003031 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3032 mTmpBounds.put(task.taskId, task.mBounds);
3033 }
3034 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3035 if (stack.mStackId == DOCKED_STACK_ID) {
3036 // Dock stack funness...Yay!
3037 if (stack.mFullscreen) {
3038 // The dock stack went fullscreen which is kinda like dismissing it.
3039 // In this case we make all other static stacks fullscreen and move all
3040 // docked stack tasks to the fullscreen stack.
3041 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3042 if (i != DOCKED_STACK_ID && getStack(i) != null) {
3043 resizeStackLocked(i, null, preserveWindows, true);
3044 }
3045 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003046
Jorim Jaggic4025202015-10-22 16:43:34 +02003047 final int count = tasks.size();
3048 for (int i = 0; i < count; i++) {
3049 moveTaskToStackLocked(tasks.get(i).taskId,
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003050 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack");
Jorim Jaggic4025202015-10-22 16:43:34 +02003051 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003052
Jorim Jaggic4025202015-10-22 16:43:34 +02003053 // stack shouldn't contain anymore activities, so nothing to resume.
3054 r = null;
3055 } else {
3056 // Docked stacks occupy a dedicated region on screen so the size of all other
3057 // static stacks need to be adjusted so they don't overlap with the docked stack.
3058 // We get the bounds to use from window manager which has been adjusted for any
3059 // screen controls and is also the same for all stacks.
3060 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3061
3062 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3063 if (i != DOCKED_STACK_ID) {
3064 ActivityStack otherStack = getStack(i);
3065 if (otherStack != null) {
3066 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3067 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003068 }
3069 }
3070 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003071 // Since we are resizing the stack, all other operations should strive to preserve
3072 // windows.
3073 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003074 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003075 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003076
Jorim Jaggic4025202015-10-22 16:43:34 +02003077 if (r != null) {
3078 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3079 // And we need to make sure at this point that all other activities
3080 // are made visible with the correct configuration.
3081 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3082 if (!updated) {
3083 resumeTopActivitiesLocked(stack, null, null);
3084 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003085 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003086 } finally {
3087 mWindowManager.continueSurfaceLayout();
3088 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003089 }
3090 }
3091
Chong Zhang6de2ae82015-09-30 18:25:21 -07003092 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003093 if (!task.mResizeable) {
3094 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3095 return;
3096 }
3097
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003098 adjustForMinimalTaskDimensions(task, bounds);
3099
Chong Zhang87b21722015-09-21 15:39:51 -07003100 // If this is a forced resize, let it go through even if the bounds is not changing,
3101 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003102 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003103 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003104 // Nothing to do here...
3105 return;
3106 }
3107
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003108 if (!mWindowManager.isValidTaskId(task.taskId)) {
3109 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003110 // All we can do for now is update the bounds so it can be used when the task is
3111 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003112 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003113 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3114 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003115 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003116 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003117 return;
3118 }
3119
Chong Zhang6de2ae82015-09-30 18:25:21 -07003120 // Do not move the task to another stack here.
3121 // This method assumes that the task is already placed in the right stack.
3122 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003123
Chong Zhang6de2ae82015-09-30 18:25:21 -07003124 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003125
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003126 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003127 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003128 // way and the activity was kept the way it was. If it's false, it means the activity had
3129 // to be relaunched due to configuration change.
3130 boolean kept = true;
3131 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003132 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003133 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003134 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003135 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003136 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003137 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003138 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003139 resumeTopActivitiesLocked(stack, null, null);
3140 }
3141 }
3142 }
Chong Zhang87b21722015-09-21 15:39:51 -07003143 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003144
3145 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003146 }
3147
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003148 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3149 if (bounds == null) {
3150 return;
3151 }
3152 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3153 : task.mMinimalSize;
3154 final boolean adjustWidth = minimalSize > bounds.width();
3155 final boolean adjustHeight = minimalSize > bounds.height();
3156 if (!(adjustWidth || adjustHeight)) {
3157 return;
3158 }
3159 Rect taskBounds = task.mBounds;
3160 if (adjustWidth) {
3161 if (taskBounds != null && bounds.right == taskBounds.right) {
3162 bounds.left = bounds.right - minimalSize;
3163 } else {
3164 // Either left bounds match, or neither match, or the previous bounds were
3165 // fullscreen and we default to keeping left.
3166 bounds.right = bounds.left + minimalSize;
3167 }
3168 }
3169 if (adjustHeight) {
3170 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3171 bounds.top = bounds.bottom - minimalSize;
3172 } else {
3173 // Either top bounds match, or neither match, or the previous bounds were
3174 // fullscreen and we default to keeping top.
3175 bounds.bottom = bounds.top + minimalSize;
3176 }
3177 }
3178 }
3179
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003180 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003181 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3182 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003183 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003184 }
3185
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003186 ActivityContainer activityContainer = new ActivityContainer(stackId);
3187 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003188 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003189 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003190 }
3191
3192 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003193 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003194 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3195 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003196 break;
3197 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003198 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003199 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003200 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003201 }
3202
Chong Zhang5dcb2752015-08-18 13:50:26 -07003203 /**
3204 * Restores a recent task to a stack
3205 * @param task The recent task to be restored.
3206 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003207 * if stackId is {@link android.app.ActivityManager#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003208 * @return true if the task has been restored successfully.
3209 */
3210 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3211 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003212 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003213 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003214 if (task.stack != null) {
3215 // Task has already been restored once. See if we need to do anything more
3216 if (task.stack.mStackId == stackId) {
3217 // Nothing else to do since it is already restored in the right stack.
3218 return true;
3219 }
3220 // Remove current stack association, so we can re-associate the task with the
3221 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003222 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003223 }
3224
3225 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003226 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003227
3228 if (stack == null) {
3229 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003230 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3231 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003232 return false;
3233 }
3234
3235 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003236 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3237 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003238 final ArrayList<ActivityRecord> activities = task.mActivities;
3239 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003240 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003241 }
3242 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003243 }
3244
Wale Ogunwale040b4702015-08-06 18:10:50 -07003245 /**
3246 * Moves the specified task record to the input stack id.
3247 * WARNING: This method performs an unchecked/raw move of the task and
3248 * can leave the system in an unstable state if used incorrectly.
3249 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3250 * to a stack.
3251 * @param task Task to move.
3252 * @param stackId Id of stack to move task to.
3253 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003254 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003255 * @param reason Reason the task is been moved.
3256 * @return The stack the task was moved to.
3257 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003258 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003259 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3260 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003261 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003262 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3263
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003264 final boolean resizeable = task.mResizeable;
3265 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3266 // if a docked stack is created below which will lead to the stack we are moving from and
3267 // its resizeable tasks being resized.
3268 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003269 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003270 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003271 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3272 if (task.stack != null) {
3273 task.stack.removeTask(task, reason, MOVING);
3274 }
3275 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003276
3277 // If the task had focus before (or we're requested to move focus),
3278 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003279 stack.setFocusAndResumeStateIfNeeded(
3280 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003281
Wale Ogunwale040b4702015-08-06 18:10:50 -07003282 return stack;
3283 }
3284
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003285 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
3286 String reason) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003287 final TaskRecord task = anyTaskForIdLocked(taskId);
3288 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003289 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003290 return;
3291 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07003292 if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID
3293 || stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003294 // We are about to relaunch the activity because its configuration changed due to
3295 // being maximized, i.e. size change. The activity will first remove the old window
3296 // and then add a new one. This call will tell window manager about this, so it can
3297 // preserve the old window until the new one is drawn. This prevents having a gap
3298 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003299 // entrance of the new window to be properly animated.
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003300 ActivityRecord r = task.getTopActivity();
3301 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3302 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003303 final ActivityStack stack =
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003304 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus,
3305 "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003306
3307 // Make sure the task has the appropriate bounds/size for the stack it is in.
3308 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003309 resizeTaskLocked(task, stack.mBounds,
3310 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003311 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3312 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003313 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3314 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003315 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003316 resizeTaskLocked(task, stack.mBounds,
3317 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003318 }
3319
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003320 // The task might have already been running and its visibility needs to be synchronized with
3321 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003322 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003323 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003324 }
3325
Wale Ogunwale079a0042015-10-24 11:44:07 -07003326 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3327 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3328 if (stack == null) {
3329 throw new IllegalArgumentException(
3330 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3331 }
3332
3333 final ActivityRecord r = stack.topRunningActivityLocked();
3334 if (r == null) {
3335 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3336 + " in stack=" + stack);
3337 return false;
3338 }
3339
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003340 if (!r.info.supportsPip) {
3341 Slog.w(TAG,
3342 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003343 + " r=" + r);
3344 return false;
3345 }
3346
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003347 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003348 if (task.mActivities.size() == 1) {
3349 // There is only one activity in the task. So, we can just move the task over to the
3350 // pinned stack without re-parenting the activity in a different task.
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003351 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS,
3352 "moveTopActivityToPinnedStack");
Wale Ogunwale079a0042015-10-24 11:44:07 -07003353 } else {
3354 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3355 pinnedStack.moveActivityToStack(r);
3356 }
3357
3358 resizeStackLocked(PINNED_STACK_ID, bounds, PRESERVE_WINDOWS, true);
3359
3360 // The task might have already been running and its visibility needs to be synchronized with
3361 // the visibility of the stack / windows.
3362 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3363 resumeTopActivitiesLocked();
3364 return true;
3365 }
3366
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003367 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3368 final TaskRecord task = anyTaskForIdLocked(taskId);
3369 if (task == null) {
3370 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3371 return;
3372 }
3373 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003374 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003375 mWindowManager.positionTaskInStack(taskId, stackId, position);
3376 final boolean stackChanged = task.stack != null && task.stack != stack;
3377 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003378 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003379 }
3380 stack.positionTask(task, position, stackChanged);
3381 // The task might have already been running and its visibility needs to be synchronized with
3382 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003383 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003384 resumeTopActivitiesLocked();
3385 }
3386
Craig Mautnerac6f8432013-07-17 13:24:59 -07003387 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003388 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003389 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3390 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003391 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3392 final ActivityStack stack = stacks.get(stackNdx);
3393 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003394 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003395 continue;
3396 }
3397 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003398 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3399 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003400 continue;
3401 }
3402 final ActivityRecord ar = stack.findTaskLocked(r);
3403 if (ar != null) {
3404 return ar;
3405 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003406 }
3407 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003408 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003409 return null;
3410 }
3411
3412 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003413 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3414 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003415 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3416 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3417 if (ar != null) {
3418 return ar;
3419 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003420 }
3421 }
3422 return null;
3423 }
3424
Craig Mautner8d341ef2013-03-26 09:03:27 -07003425 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003426 scheduleSleepTimeout();
3427 if (!mGoingToSleep.isHeld()) {
3428 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003429 if (mLaunchingActivity.isHeld()) {
3430 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3431 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003432 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003433 mLaunchingActivity.release();
3434 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003435 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003436 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003437 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003438 }
3439
3440 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003441 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003442
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003443 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003444 final long endTime = System.currentTimeMillis() + timeout;
3445 while (true) {
3446 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003447 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3448 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003449 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3450 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3451 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003452 }
3453 if (cantShutdown) {
3454 long timeRemaining = endTime - System.currentTimeMillis();
3455 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003456 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003457 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003458 } catch (InterruptedException e) {
3459 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003460 } else {
3461 Slog.w(TAG, "Activity manager shutdown timed out");
3462 timedout = true;
3463 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003464 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003465 } else {
3466 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003467 }
3468 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003469
3470 // Force checkReadyForSleep to complete.
3471 mSleepTimeout = true;
3472 checkReadyForSleepLocked();
3473
Craig Mautner8d341ef2013-03-26 09:03:27 -07003474 return timedout;
3475 }
3476
3477 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003478 removeSleepTimeouts();
3479 if (mGoingToSleep.isHeld()) {
3480 mGoingToSleep.release();
3481 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003482 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3483 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003484 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3485 final ActivityStack stack = stacks.get(stackNdx);
3486 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003487 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003488 resumeTopActivitiesLocked();
3489 }
Craig Mautner5314a402013-09-26 12:40:16 -07003490 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003491 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003492 mGoingToSleepActivities.clear();
3493 }
3494
3495 void activitySleptLocked(ActivityRecord r) {
3496 mGoingToSleepActivities.remove(r);
3497 checkReadyForSleepLocked();
3498 }
3499
3500 void checkReadyForSleepLocked() {
3501 if (!mService.isSleepingOrShuttingDown()) {
3502 // Do not care.
3503 return;
3504 }
3505
3506 if (!mSleepTimeout) {
3507 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003508 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3509 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003510 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3511 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3512 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003513 }
3514
3515 if (mStoppingActivities.size() > 0) {
3516 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003517 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003518 + mStoppingActivities.size() + " activities");
3519 scheduleIdleLocked();
3520 dontSleep = true;
3521 }
3522
3523 if (mGoingToSleepActivities.size() > 0) {
3524 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003525 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003526 + mGoingToSleepActivities.size() + " activities");
3527 dontSleep = true;
3528 }
3529
3530 if (dontSleep) {
3531 return;
3532 }
3533 }
3534
Craig Mautnere0a38842013-12-16 16:14:02 -08003535 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3536 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003537 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3538 stacks.get(stackNdx).goToSleep();
3539 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003540 }
3541
3542 removeSleepTimeouts();
3543
3544 if (mGoingToSleep.isHeld()) {
3545 mGoingToSleep.release();
3546 }
3547 if (mService.mShuttingDown) {
3548 mService.notifyAll();
3549 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003550 }
3551
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003552 boolean reportResumedActivityLocked(ActivityRecord r) {
3553 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003554 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003555 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003556 }
3557 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003558 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003559 mWindowManager.executeAppTransition();
3560 return true;
3561 }
3562 return false;
3563 }
3564
Craig Mautner8d341ef2013-03-26 09:03:27 -07003565 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003566 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3567 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003568 int stackNdx = stacks.size() - 1;
3569 while (stackNdx >= 0) {
3570 stacks.get(stackNdx).handleAppCrashLocked(app);
3571 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003572 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003573 }
3574 }
3575
Jose Lima4b6c6692014-08-12 17:41:12 -07003576 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003577 final ActivityStack stack = r.task.stack;
3578 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003579 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3580 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003581 return false;
3582 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003583 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003584 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3585 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003586
3587 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003588 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003589 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003590 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003591 return true;
3592 }
3593
3594 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003595 if (visible && top.fullscreen) {
3596 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003597 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3598 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3599 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3600 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003601 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003602 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3603 // Only the activity set as currently visible behind should actively reset its
3604 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003605 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3606 "requestVisibleBehind: returning visible=" + visible
3607 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3608 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003609 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003610 }
3611
Jose Lima4b6c6692014-08-12 17:41:12 -07003612 stack.setVisibleBehindActivity(visible ? r : null);
3613 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003614 // Make the activity immediately above r opaque.
3615 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3616 if (next != null) {
3617 mService.convertFromTranslucent(next.appToken);
3618 }
3619 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003620 if (top.app != null && top.app.thread != null) {
3621 // Notify the top app of the change.
3622 try {
3623 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3624 } catch (RemoteException e) {
3625 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003626 }
3627 return true;
3628 }
3629
Craig Mautnerbb742462014-07-07 15:28:55 -07003630 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3631 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3632 r.mLaunchTaskBehind = false;
3633 final TaskRecord task = r.task;
3634 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003635 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003636 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003637 mWindowManager.setAppVisibility(r.appToken, false);
3638 }
3639
3640 void scheduleLaunchTaskBehindComplete(IBinder token) {
3641 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3642 }
3643
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003644 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3645 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003646 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003647 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3648 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003649 final int topStackNdx = stacks.size() - 1;
3650 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3651 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003652 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003653 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003654 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003655 }
3656
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003657 void invalidateTaskLayers() {
3658 mTaskLayersChanged = true;
3659 }
3660
3661 void rankTaskLayersIfNeeded() {
3662 if (!mTaskLayersChanged) {
3663 return;
3664 }
3665 mTaskLayersChanged = false;
3666 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3667 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3668 int baseLayer = 0;
3669 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3670 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3671 }
3672 }
3673 }
3674
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003675 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3676 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3677 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3678 final int topStackNdx = stacks.size() - 1;
3679 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3680 final ActivityStack stack = stacks.get(stackNdx);
3681 stack.clearOtherAppTimeTrackers(except);
3682 }
3683 }
3684 }
3685
Craig Mautner8d341ef2013-03-26 09:03:27 -07003686 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003687 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3688 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003689 final int numStacks = stacks.size();
3690 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3691 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003692 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003693 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003694 }
3695 }
3696
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003697 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3698 // Examine all activities currently running in the process.
3699 TaskRecord firstTask = null;
3700 // Tasks is non-null only if two or more tasks are found.
3701 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003702 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3703 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003704 ActivityRecord r = app.activities.get(i);
3705 // First, if we find an activity that is in the process of being destroyed,
3706 // then we just aren't going to do anything for now; we want things to settle
3707 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003708 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003709 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003710 return;
3711 }
3712 // Don't consider any activies that are currently not in a state where they
3713 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003714 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3715 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003716 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003717 continue;
3718 }
3719 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003720 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003721 + " from " + r);
3722 if (firstTask == null) {
3723 firstTask = r.task;
3724 } else if (firstTask != r.task) {
3725 if (tasks == null) {
3726 tasks = new ArraySet<>();
3727 tasks.add(firstTask);
3728 }
3729 tasks.add(r.task);
3730 }
3731 }
3732 }
3733 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003734 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003735 return;
3736 }
3737 // If we have activities in multiple tasks that are in a position to be destroyed,
3738 // let's iterate through the tasks and release the oldest one.
3739 final int numDisplays = mActivityDisplays.size();
3740 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3741 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3742 // Step through all stacks starting from behind, to hit the oldest things first.
3743 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3744 final ActivityStack stack = stacks.get(stackNdx);
3745 // Try to release activities in this stack; if we manage to, we are done.
3746 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3747 return;
3748 }
3749 }
3750 }
3751 }
3752
Amith Yamasani37a40c22015-06-17 13:25:42 -07003753 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003754 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003755 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003756 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003757
Craig Mautner858d8a62013-04-23 17:08:34 -07003758 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003759 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3760 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003761 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003762 final ActivityStack stack = stacks.get(stackNdx);
3763 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003764 TaskRecord task = stack.topTask();
3765 if (task != null) {
3766 mWindowManager.moveTaskToTop(task.taskId);
3767 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003768 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003769 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003770
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003771 ActivityStack stack = getStack(restoreStackId);
3772 if (stack == null) {
3773 stack = mHomeStack;
3774 }
3775 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003776 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003777 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003778 } else {
3779 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003780 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003781 }
Craig Mautner93529a42013-10-04 15:03:13 -07003782 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003783 }
3784
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003785 /**
3786 * Add background users to send boot completed events to.
3787 * @param userId The user being started in the background
3788 * @param uss The state object for the user.
3789 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003790 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003791 mStartingBackgroundUsers.add(uss);
3792 }
3793
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003794 /** Checks whether the userid is a profile of the current user. */
3795 boolean isCurrentProfileLocked(int userId) {
3796 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003797 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003798 }
3799
Chong Zhang45c25ce2015-08-10 22:18:26 -07003800 /** Checks whether the activity should be shown for current user. */
3801 boolean okToShowLocked(ActivityRecord r) {
3802 return r != null && (isCurrentProfileLocked(r.userId)
3803 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3804 }
3805
Craig Mautnerde4ef022013-04-07 19:01:33 -07003806 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003807 ArrayList<ActivityRecord> stops = null;
3808
3809 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003810 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3811 ActivityRecord s = mStoppingActivities.get(activityNdx);
3812 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003813 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003814 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3815 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003816 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003817 if (s.finishing) {
3818 // If this activity is finishing, it is sitting on top of
3819 // everyone else but we now know it is no longer needed...
3820 // so get rid of it. Otherwise, we need to go through the
3821 // normal flow and hide it once we determine that it is
3822 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003823 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003824 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003825 }
3826 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003827 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003828 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003829 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003830 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003831 }
3832 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003833 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003834 }
3835 }
3836
3837 return stops;
3838 }
3839
Craig Mautnercf910b02013-04-23 11:23:27 -07003840 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003841 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3842 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3843 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3844 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003845 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003846 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003847 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003848 if (r == null) Slog.e(TAG,
3849 "validateTop...: null top activity, stack=" + stack);
3850 else {
3851 final ActivityRecord pausing = stack.mPausingActivity;
3852 if (pausing != null && pausing == r) Slog.e(TAG,
3853 "validateTop...: top stack has pausing activity r=" + r
3854 + " state=" + state);
3855 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3856 "validateTop...: activity in front not resumed r=" + r
3857 + " state=" + state);
3858 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003859 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003860 final ActivityRecord resumed = stack.mResumedActivity;
3861 if (resumed != null && resumed == r) Slog.e(TAG,
3862 "validateTop...: back stack has resumed activity r=" + r
3863 + " state=" + state);
3864 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3865 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003866 }
3867 }
3868 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003869 }
3870
Craig Mautnere0570202015-05-13 13:06:11 -07003871 private String lockTaskModeToString() {
3872 switch (mLockTaskModeState) {
3873 case LOCK_TASK_MODE_LOCKED:
3874 return "LOCKED";
3875 case LOCK_TASK_MODE_PINNED:
3876 return "PINNED";
3877 case LOCK_TASK_MODE_NONE:
3878 return "NONE";
3879 default: return "unknown=" + mLockTaskModeState;
3880 }
3881 }
3882
Craig Mautner27084302013-03-25 08:05:25 -07003883 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003884 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003885 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003886 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3887 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3888 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003889 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003890 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3891 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3892 if (packages.size() > 0) {
3893 pw.println(" mLockTaskPackages (userId:packages)=");
3894 for (int i = 0; i < packages.size(); ++i) {
3895 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3896 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3897 }
3898 }
3899 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003900 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003901
Craig Mautner20e72272013-04-01 13:45:53 -07003902 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003903 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003904 }
3905
Dianne Hackborn390517b2013-05-30 15:03:32 -07003906 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3907 boolean needSep, String prefix) {
3908 if (activity != null) {
3909 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3910 if (needSep) {
3911 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003912 }
3913 pw.print(prefix);
3914 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003915 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003916 }
3917 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003918 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003919 }
3920
Craig Mautner8d341ef2013-03-26 09:03:27 -07003921 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3922 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003923 boolean printed = false;
3924 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003925 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3926 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003927 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003928 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003929 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003930 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003931 final ActivityStack stack = stacks.get(stackNdx);
3932 StringBuilder stackHeader = new StringBuilder(128);
3933 stackHeader.append(" Stack #");
3934 stackHeader.append(stack.mStackId);
3935 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003936 stackHeader.append("\n");
3937 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3938 stackHeader.append("\n");
3939 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003940 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3941 needSep, stackHeader.toString());
3942 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3943 !dumpAll, false, dumpPackage, true,
3944 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003945
Craig Mautner4a1cb222013-12-04 16:14:06 -08003946 needSep = printed;
3947 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3948 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003949 if (pr) {
3950 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003951 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003952 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003953 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3954 " mResumedActivity: ");
3955 if (pr) {
3956 printed = true;
3957 needSep = false;
3958 }
3959 if (dumpAll) {
3960 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3961 " mLastPausedActivity: ");
3962 if (pr) {
3963 printed = true;
3964 needSep = true;
3965 }
3966 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3967 needSep, " mLastNoHistoryActivity: ");
3968 }
3969 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003970 }
3971 }
3972
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003973 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3974 false, dumpPackage, true, " Activities waiting to finish:", null);
3975 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3976 false, dumpPackage, true, " Activities waiting to stop:", null);
3977 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3978 false, dumpPackage, true, " Activities waiting for another to become visible:",
3979 null);
3980 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3981 false, dumpPackage, true, " Activities waiting to sleep:", null);
3982 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3983 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003984
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003985 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003986 }
3987
Dianne Hackborn390517b2013-05-30 15:03:32 -07003988 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003989 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003990 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003991 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003992 String innerPrefix = null;
3993 String[] args = null;
3994 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003995 for (int i=list.size()-1; i>=0; i--) {
3996 final ActivityRecord r = list.get(i);
3997 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3998 continue;
3999 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004000 if (innerPrefix == null) {
4001 innerPrefix = prefix + " ";
4002 args = new String[0];
4003 }
4004 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004005 final boolean full = !brief && (complete || !r.isInHistory());
4006 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004007 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004008 needNL = false;
4009 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004010 if (header1 != null) {
4011 pw.println(header1);
4012 header1 = null;
4013 }
4014 if (header2 != null) {
4015 pw.println(header2);
4016 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004017 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004018 if (lastTask != r.task) {
4019 lastTask = r.task;
4020 pw.print(prefix);
4021 pw.print(full ? "* " : " ");
4022 pw.println(lastTask);
4023 if (full) {
4024 lastTask.dump(pw, prefix + " ");
4025 } else if (complete) {
4026 // Complete + brief == give a summary. Isn't that obvious?!?
4027 if (lastTask.intent != null) {
4028 pw.print(prefix); pw.print(" ");
4029 pw.println(lastTask.intent.toInsecureStringWithClip());
4030 }
4031 }
4032 }
4033 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4034 pw.print(" #"); pw.print(i); pw.print(": ");
4035 pw.println(r);
4036 if (full) {
4037 r.dump(pw, innerPrefix);
4038 } else if (complete) {
4039 // Complete + brief == give a summary. Isn't that obvious?!?
4040 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4041 if (r.app != null) {
4042 pw.print(innerPrefix); pw.println(r.app);
4043 }
4044 }
4045 if (client && r.app != null && r.app.thread != null) {
4046 // flush anything that is already in the PrintWriter since the thread is going
4047 // to write to the file descriptor directly
4048 pw.flush();
4049 try {
4050 TransferPipe tp = new TransferPipe();
4051 try {
4052 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4053 r.appToken, innerPrefix, args);
4054 // Short timeout, since blocking here can
4055 // deadlock with the application.
4056 tp.go(fd, 2000);
4057 } finally {
4058 tp.kill();
4059 }
4060 } catch (IOException e) {
4061 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4062 } catch (RemoteException e) {
4063 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4064 }
4065 needNL = true;
4066 }
4067 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004068 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004069 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004070
Craig Mautnerf3333272013-04-22 10:55:53 -07004071 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004072 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4073 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004074 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4075 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004076 }
4077
4078 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004079 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004080 }
4081
4082 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004083 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4084 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004085 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4086 }
4087
Craig Mautner05d29032013-05-03 13:40:13 -07004088 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004089 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4090 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4091 }
Craig Mautner05d29032013-05-03 13:40:13 -07004092 }
4093
Craig Mautner0eea92c2013-05-16 13:35:39 -07004094 void removeSleepTimeouts() {
4095 mSleepTimeout = false;
4096 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4097 }
4098
4099 final void scheduleSleepTimeout() {
4100 removeSleepTimeouts();
4101 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4102 }
4103
Craig Mautner4a1cb222013-12-04 16:14:06 -08004104 @Override
4105 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004106 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004107 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4108 }
4109
4110 @Override
4111 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004112 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004113 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4114 }
4115
4116 @Override
4117 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004118 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004119 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4120 }
4121
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004122 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004123 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004124 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004125 newDisplay = mActivityDisplays.get(displayId) == null;
4126 if (newDisplay) {
4127 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004128 if (activityDisplay.mDisplay == null) {
4129 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4130 return;
4131 }
Craig Mautner4504de52013-12-20 09:06:56 -08004132 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004133 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004134 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004135 }
Craig Mautner4504de52013-12-20 09:06:56 -08004136 if (newDisplay) {
4137 mWindowManager.onDisplayAdded(displayId);
4138 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004139 }
4140
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004141 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4142 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4143 return;
4144 }
4145 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4146 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4147 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4148 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4149 }
4150
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004151 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004152 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004153 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4154 if (activityDisplay != null) {
4155 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004156 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004157 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004158 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004159 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004160 }
4161 }
4162 mWindowManager.onDisplayRemoved(displayId);
4163 }
4164
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004165 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004166 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004167 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4168 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004169 // TODO: Update the bounds.
4170 }
4171 }
4172 mWindowManager.onDisplayChanged(displayId);
4173 }
4174
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004175 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004176 StackInfo info = new StackInfo();
4177 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4178 info.displayId = Display.DEFAULT_DISPLAY;
4179 info.stackId = stack.mStackId;
4180
4181 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4182 final int numTasks = tasks.size();
4183 int[] taskIds = new int[numTasks];
4184 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004185 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004186 for (int i = 0; i < numTasks; ++i) {
4187 final TaskRecord task = tasks.get(i);
4188 taskIds[i] = task.taskId;
4189 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4190 : task.realActivity != null ? task.realActivity.flattenToString()
4191 : task.getTopActivity() != null ? task.getTopActivity().packageName
4192 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004193 taskBounds[i] = new Rect();
4194 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004195 }
4196 info.taskIds = taskIds;
4197 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004198 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004199 return info;
4200 }
4201
4202 StackInfo getStackInfoLocked(int stackId) {
4203 ActivityStack stack = getStack(stackId);
4204 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004205 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004206 }
4207 return null;
4208 }
4209
4210 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004211 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004212 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4213 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004214 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004215 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004216 }
4217 }
4218 return list;
4219 }
4220
Craig Mautner15df08a2015-04-01 12:17:18 -07004221 TaskRecord getLockedTaskLocked() {
4222 final int top = mLockTaskModeTasks.size() - 1;
4223 if (top >= 0) {
4224 return mLockTaskModeTasks.get(top);
4225 }
4226 return null;
4227 }
4228
4229 boolean isLockedTask(TaskRecord task) {
4230 return mLockTaskModeTasks.contains(task);
4231 }
4232
4233 boolean isLastLockedTask(TaskRecord task) {
4234 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4235 }
4236
4237 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004238 if (!mLockTaskModeTasks.remove(task)) {
4239 return;
4240 }
4241 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4242 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004243 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004244 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4245 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004246 final Message lockTaskMsg = Message.obtain();
4247 lockTaskMsg.arg1 = task.userId;
4248 lockTaskMsg.what = LOCK_TASK_END_MSG;
4249 mHandler.sendMessage(lockTaskMsg);
4250 }
4251 }
4252
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004253 void showLockTaskToast() {
4254 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004255 }
4256
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004257 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4258 if (mLockTaskModeTasks.contains(task)) {
4259 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4260 }
4261 }
4262
Craig Mautner432f64e2015-05-20 14:59:57 -07004263 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4264 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004265 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004266 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004267 final TaskRecord lockedTask = getLockedTaskLocked();
4268 if (lockedTask != null) {
4269 removeLockedTaskLocked(lockedTask);
4270 if (!mLockTaskModeTasks.isEmpty()) {
4271 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004272 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4273 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004274 lockedTask.performClearTaskLocked();
4275 resumeTopActivitiesLocked();
4276 return;
4277 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004278 }
Craig Mautnere0570202015-05-13 13:06:11 -07004279 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4280 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004281 return;
4282 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004283
4284 // Should have already been checked, but do it again.
4285 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004286 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4287 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004288 return;
4289 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004290 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004291 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004292 return;
4293 }
4294
4295 if (mLockTaskModeTasks.isEmpty()) {
4296 // First locktask.
4297 final Message lockTaskMsg = Message.obtain();
4298 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4299 lockTaskMsg.arg1 = task.userId;
4300 lockTaskMsg.what = LOCK_TASK_START_MSG;
4301 lockTaskMsg.arg2 = lockTaskModeState;
4302 mHandler.sendMessage(lockTaskMsg);
4303 }
4304 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004305 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4306 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004307 mLockTaskModeTasks.remove(task);
4308 mLockTaskModeTasks.add(task);
4309
4310 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004311 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004312 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004313
4314 if (andResume) {
4315 findTaskToMoveToFrontLocked(task, 0, null, reason);
4316 resumeTopActivitiesLocked();
4317 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004318 }
4319
4320 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004321 return isLockTaskModeViolation(task, false);
4322 }
4323
4324 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4325 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004326 return false;
4327 }
4328 final int lockTaskAuth = task.mLockTaskAuth;
4329 switch (lockTaskAuth) {
4330 case LOCK_TASK_AUTH_DONT_LOCK:
4331 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004332 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004333 case LOCK_TASK_AUTH_LAUNCHABLE:
4334 case LOCK_TASK_AUTH_WHITELISTED:
4335 return false;
4336 case LOCK_TASK_AUTH_PINNABLE:
4337 // Pinnable tasks can't be launched on top of locktask tasks.
4338 return !mLockTaskModeTasks.isEmpty();
4339 default:
4340 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4341 return true;
4342 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004343 }
4344
Craig Mautner15df08a2015-04-01 12:17:18 -07004345 void onLockTaskPackagesUpdatedLocked() {
4346 boolean didSomething = false;
4347 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4348 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004349 final boolean wasWhitelisted =
4350 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4351 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004352 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004353 final boolean isWhitelisted =
4354 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4355 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4356 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004357 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004358 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4359 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004360 removeLockedTaskLocked(lockedTask);
4361 lockedTask.performClearTaskLocked();
4362 didSomething = true;
4363 }
4364 }
4365 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4366 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4367 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4368 final ActivityStack stack = stacks.get(stackNdx);
4369 stack.onLockTaskPackagesUpdatedLocked();
4370 }
4371 }
Craig Mautnere0570202015-05-13 13:06:11 -07004372 final ActivityRecord r = topRunningActivityLocked();
4373 final TaskRecord task = r != null ? r.task : null;
4374 if (mLockTaskModeTasks.isEmpty() && task != null
4375 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4376 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004377 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4378 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4379 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4380 false);
4381 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004382 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004383 if (didSomething) {
4384 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004385 }
4386 }
4387
Benjamin Franz43261142015-02-11 15:59:44 +00004388 int getLockTaskModeState() {
4389 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004390 }
4391
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004392 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004393
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004394 public ActivityStackSupervisorHandler(Looper looper) {
4395 super(looper);
4396 }
4397
Craig Mautnerf3333272013-04-22 10:55:53 -07004398 void activityIdleInternal(ActivityRecord r) {
4399 synchronized (mService) {
4400 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4401 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004402 }
4403
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004404 @Override
4405 public void handleMessage(Message msg) {
4406 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004407 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004408 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4409 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004410 if (mService.mDidDexOpt) {
4411 mService.mDidDexOpt = false;
4412 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4413 nmsg.obj = msg.obj;
4414 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4415 return;
4416 }
4417 // We don't at this point know if the activity is fullscreen,
4418 // so we need to be conservative and assume it isn't.
4419 activityIdleInternal((ActivityRecord)msg.obj);
4420 } break;
4421 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004422 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004423 activityIdleInternal((ActivityRecord)msg.obj);
4424 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004425 case RESUME_TOP_ACTIVITY_MSG: {
4426 synchronized (mService) {
4427 resumeTopActivitiesLocked();
4428 }
4429 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004430 case SLEEP_TIMEOUT_MSG: {
4431 synchronized (mService) {
4432 if (mService.isSleepingOrShuttingDown()) {
4433 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4434 mSleepTimeout = true;
4435 checkReadyForSleepLocked();
4436 }
4437 }
4438 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004439 case LAUNCH_TIMEOUT_MSG: {
4440 if (mService.mDidDexOpt) {
4441 mService.mDidDexOpt = false;
4442 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4443 return;
4444 }
4445 synchronized (mService) {
4446 if (mLaunchingActivity.isHeld()) {
4447 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4448 if (VALIDATE_WAKE_LOCK_CALLER
4449 && Binder.getCallingUid() != Process.myUid()) {
4450 throw new IllegalStateException("Calling must be system uid");
4451 }
4452 mLaunchingActivity.release();
4453 }
4454 }
4455 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004456 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004457 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004458 } break;
4459 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004460 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004461 } break;
4462 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004463 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004464 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004465 case CONTAINER_CALLBACK_VISIBILITY: {
4466 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004467 final IActivityContainerCallback callback = container.mCallback;
4468 if (callback != null) {
4469 try {
4470 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4471 } catch (RemoteException e) {
4472 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004473 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004474 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004475 case LOCK_TASK_START_MSG: {
4476 // When lock task starts, we disable the status bars.
4477 try {
Jason Monk62515be2014-05-21 16:06:19 -04004478 if (mLockTaskNotify == null) {
4479 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004480 }
Jason Monk62515be2014-05-21 16:06:19 -04004481 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004482 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004483 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004484 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004485 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004486 flags = StatusBarManager.DISABLE_MASK
4487 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004488 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004489 flags = StatusBarManager.DISABLE_MASK
4490 & (~StatusBarManager.DISABLE_BACK)
4491 & (~StatusBarManager.DISABLE_HOME)
4492 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004493 }
4494 getStatusBarService().disable(flags, mToken,
4495 mService.mContext.getPackageName());
4496 }
4497 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004498 if (getDevicePolicyManager() != null) {
4499 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004500 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004501 }
justinzhang5286d3f2014-05-12 17:06:01 -04004502 } catch (RemoteException ex) {
4503 throw new RuntimeException(ex);
4504 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004505 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004506 case LOCK_TASK_END_MSG: {
4507 // When lock task ends, we enable the status bars.
4508 try {
Jason Monk62515be2014-05-21 16:06:19 -04004509 if (getStatusBarService() != null) {
4510 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4511 mService.mContext.getPackageName());
4512 }
4513 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004514 if (getDevicePolicyManager() != null) {
4515 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4516 msg.arg1);
4517 }
Jason Monk62515be2014-05-21 16:06:19 -04004518 if (mLockTaskNotify == null) {
4519 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4520 }
4521 mLockTaskNotify.show(false);
4522 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004523 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004524 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004525 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004526 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004527 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004528 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004529 new LockPatternUtils(mService.mContext)
4530 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004531 }
4532 } catch (SettingNotFoundException e) {
4533 // No setting, don't lock.
4534 }
justinzhang5286d3f2014-05-12 17:06:01 -04004535 } catch (RemoteException ex) {
4536 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004537 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004538 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004539 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004540 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004541 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4542 if (mLockTaskNotify == null) {
4543 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4544 }
4545 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4546 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004547 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4548 final ActivityContainer container = (ActivityContainer) msg.obj;
4549 final IActivityContainerCallback callback = container.mCallback;
4550 if (callback != null) {
4551 try {
4552 callback.onAllActivitiesComplete(container.asBinder());
4553 } catch (RemoteException e) {
4554 }
4555 }
4556 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004557 case LAUNCH_TASK_BEHIND_COMPLETE: {
4558 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004559 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004560 if (r != null) {
4561 handleLaunchTaskBehindCompleteLocked(r);
4562 }
4563 }
4564 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004565 }
4566 }
4567 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004568
Ying Wangb081a592014-04-22 15:20:16 -07004569 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004570 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004571 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004572 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004573 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004574 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004575 ActivityRecord mParentActivity = null;
4576 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004577
Craig Mautnere3a00d72014-04-16 08:31:19 -07004578 boolean mVisible = true;
4579
Craig Mautner4a1cb222013-12-04 16:14:06 -08004580 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004581 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004582
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004583 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4584 final static int CONTAINER_STATE_NO_SURFACE = 1;
4585 final static int CONTAINER_STATE_FINISHING = 2;
4586 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4587
4588 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004589 synchronized (mService) {
4590 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004591 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004592 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004593 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004594 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004595 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004596
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004597 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004598 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004599 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004600 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004601 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004602 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004603 }
4604
4605 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004606 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004607 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004608 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4609 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004610 return;
4611 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004612 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004613 }
4614 }
4615
4616 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004617 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004618 synchronized (mService) {
4619 if (mActivityDisplay != null) {
4620 return mActivityDisplay.mDisplayId;
4621 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004622 }
4623 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004624 }
4625
Jeff Brownca9bc702014-02-11 14:32:56 -08004626 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004627 public int getStackId() {
4628 synchronized (mService) {
4629 return mStackId;
4630 }
4631 }
4632
4633 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004634 public boolean injectEvent(InputEvent event) {
4635 final long origId = Binder.clearCallingIdentity();
4636 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004637 synchronized (mService) {
4638 if (mActivityDisplay != null) {
4639 return mInputManagerInternal.injectInputEvent(event,
4640 mActivityDisplay.mDisplayId,
4641 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4642 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004643 }
4644 return false;
4645 } finally {
4646 Binder.restoreCallingIdentity(origId);
4647 }
4648 }
4649
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004650 @Override
4651 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004652 synchronized (mService) {
4653 if (mContainerState == CONTAINER_STATE_FINISHING) {
4654 return;
4655 }
4656 mContainerState = CONTAINER_STATE_FINISHING;
4657
Craig Mautnerd163e752014-06-13 17:18:47 -07004658 long origId = Binder.clearCallingIdentity();
4659 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004660 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004661 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004662 } finally {
4663 Binder.restoreCallingIdentity(origId);
4664 }
4665 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004666 }
4667
Craig Mautner60257702014-09-17 15:02:33 -07004668 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004669 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004670 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004671 if (mActivityDisplay != null) {
4672 mActivityDisplay.detachActivitiesLocked(mStack);
4673 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004674 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004675 }
4676 }
4677
4678 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004679 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004680 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004681 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004682 Binder.getCallingUid(), mCurrentUser, false,
4683 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004684
Craig Mautnere0a38842013-12-16 16:14:02 -08004685 // TODO: Switch to user app stacks here.
4686 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004687 final Uri data = intent.getData();
4688 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4689 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004690 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004691 checkEmbeddedAllowedInner(userId, intent, mimeType);
4692
4693 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4694 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004695 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004696 }
4697
4698 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004699 public final int startActivityIntentSender(IIntentSender intentSender)
4700 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004701 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4702
4703 if (!(intentSender instanceof PendingIntentRecord)) {
4704 throw new IllegalArgumentException("Bad PendingIntent object");
4705 }
4706
Craig Mautnerb9168362015-02-26 20:40:19 -08004707 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004708 Binder.getCallingUid(), mCurrentUser, false,
4709 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004710
4711 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4712 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4713 pendingIntent.key.requestResolvedType);
4714
4715 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4716 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4717 }
4718
4719 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004720 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004721 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004722 throw new SecurityException(
4723 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4724 }
4725 }
4726
Craig Mautnerdf88d732014-01-27 09:21:32 -08004727 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004728 public IBinder asBinder() {
4729 return this;
4730 }
4731
Craig Mautner4504de52013-12-20 09:06:56 -08004732 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004733 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004734 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004735 }
4736
Craig Mautner4a1cb222013-12-04 16:14:06 -08004737 ActivityStackSupervisor getOuter() {
4738 return ActivityStackSupervisor.this;
4739 }
4740
Craig Mautnerd163e752014-06-13 17:18:47 -07004741 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004742 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004743 }
4744
Craig Mautner6985bad2014-04-21 15:22:06 -07004745 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004746 void setVisible(boolean visible) {
4747 if (mVisible != visible) {
4748 mVisible = visible;
4749 if (mCallback != null) {
4750 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4751 0 /* unused */, this).sendToTarget();
4752 }
4753 }
4754 }
4755
Craig Mautner6985bad2014-04-21 15:22:06 -07004756 void setDrawn() {
4757 }
4758
Craig Mautner1b4bf852014-05-26 15:06:32 -07004759 // You can always start a new task on a regular ActivityStack.
4760 boolean isEligibleForNewTasks() {
4761 return true;
4762 }
4763
Craig Mautnerd163e752014-06-13 17:18:47 -07004764 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004765 detachLocked();
4766 deleteActivityContainer(this);
4767 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004768 }
4769
Craig Mautner34b73df2014-01-12 21:11:08 -08004770 @Override
4771 public String toString() {
4772 return mIdString + (mActivityDisplay == null ? "N" : "A");
4773 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004774 }
4775
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004776 private class VirtualActivityContainer extends ActivityContainer {
4777 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004778 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004779
4780 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4781 super(getNextStackId());
4782 mParentActivity = parent;
4783 mCallback = callback;
4784 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004785 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004786 }
4787
4788 @Override
4789 public void setSurface(Surface surface, int width, int height, int density) {
4790 super.setSurface(surface, width, height, density);
4791
4792 synchronized (mService) {
4793 final long origId = Binder.clearCallingIdentity();
4794 try {
4795 setSurfaceLocked(surface, width, height, density);
4796 } finally {
4797 Binder.restoreCallingIdentity(origId);
4798 }
4799 }
4800 }
4801
4802 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4803 if (mContainerState == CONTAINER_STATE_FINISHING) {
4804 return;
4805 }
4806 VirtualActivityDisplay virtualActivityDisplay =
4807 (VirtualActivityDisplay) mActivityDisplay;
4808 if (virtualActivityDisplay == null) {
4809 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004810 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004811 mActivityDisplay = virtualActivityDisplay;
4812 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004813 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004814 }
4815
4816 if (mSurface != null) {
4817 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004818 }
4819
Craig Mautner6985bad2014-04-21 15:22:06 -07004820 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004821 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004822 mStack.resumeTopActivityLocked(null);
4823 } else {
4824 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004825 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004826 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004827 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004828 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004829 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004830
Craig Mautnerd163e752014-06-13 17:18:47 -07004831 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004832
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004833 if (DEBUG_STACK) Slog.d(TAG_STACK,
4834 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004835 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004836
Craig Mautner6985bad2014-04-21 15:22:06 -07004837 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004838 boolean isAttachedLocked() {
4839 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004840 }
4841
4842 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004843 void setDrawn() {
4844 synchronized (mService) {
4845 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004846 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004847 }
4848 }
4849
Craig Mautner1b4bf852014-05-26 15:06:32 -07004850 // Never start a new task on an ActivityView if it isn't explicitly specified.
4851 @Override
4852 boolean isEligibleForNewTasks() {
4853 return false;
4854 }
4855
Craig Mautnerd163e752014-06-13 17:18:47 -07004856 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004857 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004858 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4859 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4860 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4861 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4862 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004863 }
4864 }
4865
Craig Mautner4a1cb222013-12-04 16:14:06 -08004866 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4867 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004868 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004869 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004870 int mDisplayId;
4871 Display mDisplay;
4872 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004873
Craig Mautner4a1cb222013-12-04 16:14:06 -08004874 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4875 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004876 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004877
Jose Lima4b6c6692014-08-12 17:41:12 -07004878 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004879
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004880 ActivityDisplay() {
4881 }
Craig Mautner4504de52013-12-20 09:06:56 -08004882
Craig Mautner1a70a162014-09-13 12:09:31 -07004883 // After instantiation, check that mDisplay is not null before using this. The alternative
4884 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004885 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004886 final Display display = mDisplayManager.getDisplay(displayId);
4887 if (display == null) {
4888 return;
4889 }
4890 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004891 }
4892
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004893 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004894 mDisplay = display;
4895 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004896 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004897 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004898
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004899 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004900 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004901 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4902 + " onTop=" + onTop);
4903 if (onTop) {
4904 mStacks.add(stack);
4905 } else {
4906 mStacks.add(0, stack);
4907 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004908 }
4909
Craig Mautnere0a38842013-12-16 16:14:02 -08004910 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004911 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004912 + " from displayId=" + mDisplayId);
4913 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004914 }
4915
Jose Lima4b6c6692014-08-12 17:41:12 -07004916 void setVisibleBehindActivity(ActivityRecord r) {
4917 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004918 }
4919
Jose Lima4b6c6692014-08-12 17:41:12 -07004920 boolean hasVisibleBehindActivity() {
4921 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004922 }
4923
Craig Mautner34b73df2014-01-12 21:11:08 -08004924 @Override
4925 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004926 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4927 }
4928 }
4929
4930 class VirtualActivityDisplay extends ActivityDisplay {
4931 VirtualDisplay mVirtualDisplay;
4932
Craig Mautner6985bad2014-04-21 15:22:06 -07004933 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004934 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004935 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4936 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4937 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4938 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004939
4940 init(mVirtualDisplay.getDisplay());
4941
4942 mWindowManager.handleDisplayAdded(mDisplayId);
4943 }
4944
4945 void setSurface(Surface surface) {
4946 if (mVirtualDisplay != null) {
4947 mVirtualDisplay.setSurface(surface);
4948 }
4949 }
4950
4951 @Override
4952 void detachActivitiesLocked(ActivityStack stack) {
4953 super.detachActivitiesLocked(stack);
4954 if (mVirtualDisplay != null) {
4955 mVirtualDisplay.release();
4956 mVirtualDisplay = null;
4957 }
4958 }
4959
4960 @Override
4961 public String toString() {
4962 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004963 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004964 }
Jose Lima58e66d62014-05-27 20:00:27 -07004965
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004966 /**
4967 * Adjust bounds to stay within stack bounds.
4968 *
4969 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4970 * that keep them unchanged, but be contained within the stack bounds.
4971 *
4972 * @param bounds Bounds to be adjusted.
4973 * @param stackBounds Bounds within which the other bounds should remain.
4974 */
4975 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4976 if (stackBounds == null || stackBounds.contains(bounds)) {
4977 return;
4978 }
4979
4980 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4981 final int maxRight = stackBounds.right
4982 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4983 int horizontalDiff = stackBounds.left - bounds.left;
4984 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4985 || (bounds.left + horizontalDiff >= maxRight)) {
4986 horizontalDiff = maxRight - bounds.left;
4987 }
4988 bounds.left += horizontalDiff;
4989 bounds.right += horizontalDiff;
4990 }
4991
4992 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4993 final int maxBottom = stackBounds.bottom
4994 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4995 int verticalDiff = stackBounds.top - bounds.top;
4996 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4997 || (bounds.top + verticalDiff >= maxBottom)) {
4998 verticalDiff = maxBottom - bounds.top;
4999 }
5000 bounds.top += verticalDiff;
5001 bounds.bottom += verticalDiff;
5002 }
5003 }
5004
Craig Mautner27084302013-03-25 08:05:25 -07005005}