blob: b025ce2b525d4006783f6f9a17288de219199f50 [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 Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
22import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
23import static android.app.ActivityManager.StackId.FIRST_STATIC_STACK_ID;
24import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
25import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
26import static android.app.ActivityManager.StackId.HOME_STACK_ID;
27import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
28import static android.app.ActivityManager.StackId.LAST_STATIC_STACK_ID;
29import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070030import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
31import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070032import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
33import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070034import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070035import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070036import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080037import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070038import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070039import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
40import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
41import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070042import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070043import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
44import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010045import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070046import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
47import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070048
Svetoslav7008b512015-06-24 18:47:07 -070049import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070050import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070052import android.app.ActivityManager.StackId;
Craig Mautnered6649f2013-12-02 14:08:25 -080053import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.app.ActivityOptions;
55import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070056import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080057import android.app.IActivityContainer;
58import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070059import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070062import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070063import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070064import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070065import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040066import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040067import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070068import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070069import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070071import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070073import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070074import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070075import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070076import android.content.pm.PackageManager;
77import android.content.pm.ResolveInfo;
78import android.content.res.Configuration;
Wale Ogunwale60454db2015-01-23 16:05:07 -080079import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080080import android.hardware.display.DisplayManager;
81import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080082import android.hardware.display.DisplayManagerGlobal;
83import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080084import android.hardware.input.InputManager;
85import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080086import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070087import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070088import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070089import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070090import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070091import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070092import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070093import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070094import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070095import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070096import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070097import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040098import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070099import android.os.SystemClock;
Wale Ogunwalecad05a02015-09-25 10:41:44 -0700100import android.os.Trace;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700101import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -0700102import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700103import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700104import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400105import android.provider.Settings;
106import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700107import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700108import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700109import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700110import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700111import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800112import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700113
Craig Mautner4a1cb222013-12-04 16:14:06 -0800114import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800115import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800116import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800117import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800118import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700119import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700120import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800121import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700122import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400123import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700124import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400125import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800126import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700127import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700128import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700129
justinzhang5286d3f2014-05-12 17:06:01 -0400130
Craig Mautner8d341ef2013-03-26 09:03:27 -0700131import java.io.FileDescriptor;
132import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700133import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700134import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700135import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700136import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700137import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700138
Craig Mautner4a1cb222013-12-04 16:14:06 -0800139public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800140 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700141 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700142 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700143 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700144 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700145 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700146 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700147 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700148 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
149 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700150 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700151 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700152 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
153 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700154 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700155 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800156
Craig Mautnerf3333272013-04-22 10:55:53 -0700157 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700158 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700159
Craig Mautner0eea92c2013-05-16 13:35:39 -0700160 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700161 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700162
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700163 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700164 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700165
Craig Mautner05d29032013-05-03 13:40:13 -0700166 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
167 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
168 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700169 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700170 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800171 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
172 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
173 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700174 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400175 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
176 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700177 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700178 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700179 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800180
Wale Ogunwale040b4702015-08-06 18:10:50 -0700181 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700182
Jason Monk62515be2014-05-21 16:06:19 -0400183 private static final String LOCK_TASK_TAG = "Lock-to-App";
184
Wale Ogunwale040b4702015-08-06 18:10:50 -0700185 // Used to indicate if an object (e.g. stack) that we are trying to get
186 // should be created if it doesn't exist already.
187 private static final boolean CREATE_IF_NEEDED = true;
188
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700189 // Used to indicate that windows of activities should be preserved during the resize.
190 static final boolean PRESERVE_WINDOWS = true;
191
Wale Ogunwale040b4702015-08-06 18:10:50 -0700192 // Used to indicate if an object (e.g. task) should be moved/created
193 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700194 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700195
196 // Used to indicate that an objects (e.g. task) removal from its container
197 // (e.g. stack) is due to it moving to another container.
198 static final boolean MOVING = true;
199
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700200 // Force the focus to change to the stack we are moving a task to..
201 static final boolean FORCE_FOCUS = true;
202
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700203 // Restore task from the saved recents if it can't be found in any live stack.
204 static final boolean RESTORE_FROM_RECENTS = true;
205
Svetoslav7008b512015-06-24 18:47:07 -0700206 // Activity actions an app cannot start if it uses a permission which is not granted.
207 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
208 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700209
Svetoslav7008b512015-06-24 18:47:07 -0700210 static {
211 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
212 Manifest.permission.CAMERA);
213 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
214 Manifest.permission.CAMERA);
215 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
216 Manifest.permission.CALL_PHONE);
217 }
218
Svet Ganov99b60432015-06-27 13:15:22 -0700219 /** Action restriction: launching the activity is not restricted. */
220 private static final int ACTIVITY_RESTRICTION_NONE = 0;
221 /** Action restriction: launching the activity is restricted by a permission. */
222 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
223 /** Action restriction: launching the activity is restricted by an app op. */
224 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700225
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700226 // The height/width divide used when fitting a task within a bounds with method
227 // {@link #fitWithinBounds}.
228 // We always want the task to to be visible in the bounds without affecting its size when
229 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
230 // the input bounds right or bottom side minus the width or height divided by this value.
231 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
232
justinzhang5286d3f2014-05-12 17:06:01 -0400233 /** Status Bar Service **/
234 private IBinder mToken = new Binder();
235 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400236 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400237
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700238 // For debugging to make sure the caller when acquiring/releasing our
239 // wake lock is the system process.
240 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700241
Craig Mautner27084302013-03-25 08:05:25 -0700242 final ActivityManagerService mService;
243
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800244 private final RecentTasks mRecentTasks;
245
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700246 final ActivityStackSupervisorHandler mHandler;
247
248 /** Short cut */
249 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800250 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700251
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700252 /** Counter for next free stack ID to use for dynamic activity stacks. */
253 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700254
255 /** Task identifier that activities are currently being started in. Incremented each time a
256 * new task is created. */
257 private int mCurTaskId = 0;
258
Craig Mautner2420ead2013-04-01 17:13:20 -0700259 /** The current user */
260 private int mCurrentUser;
261
Craig Mautnere0a38842013-12-16 16:14:02 -0800262 /** The stack containing the launcher app. Assumed to always be attached to
263 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700264 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700265
Craig Mautnere0a38842013-12-16 16:14:02 -0800266 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700267 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700268
Craig Mautner4a1cb222013-12-04 16:14:06 -0800269 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
270 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800271 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800272 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700273
274 /** List of activities that are waiting for a new activity to become visible before completing
275 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800276 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700277
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700278 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800279 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700280
281 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800282 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700283
Craig Mautnerde4ef022013-04-07 19:01:33 -0700284 /** List of activities that are ready to be stopped, but waiting for the next activity to
285 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800286 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700287
Craig Mautnerf3333272013-04-22 10:55:53 -0700288 /** List of activities that are ready to be finished, but waiting for the previous activity to
289 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800290 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700291
Craig Mautner0eea92c2013-05-16 13:35:39 -0700292 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800293 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700294
Craig Mautnerf3333272013-04-22 10:55:53 -0700295 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700296 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700297
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700298 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700299 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700300
Craig Mautnerde4ef022013-04-07 19:01:33 -0700301 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
302 * is being brought in front of us. */
303 boolean mUserLeaving = false;
304
Craig Mautner0eea92c2013-05-16 13:35:39 -0700305 /** Set when we have taken too long waiting to go to sleep. */
306 boolean mSleepTimeout = false;
307
308 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700309 * We don't want to allow the device to go to sleep while in the process
310 * of launching an activity. This is primarily to allow alarm intent
311 * receivers to launch an activity and get that to run before the device
312 * goes back to sleep.
313 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700314 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700315
316 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700317 * Set when the system is going to sleep, until we have
318 * successfully paused the current activity and released our wake lock.
319 * At that point the system is allowed to actually sleep.
320 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700321 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700322
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700323 /** Stack id of the front stack when user switched, indexed by userId. */
324 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700325
Craig Mautner4504de52013-12-20 09:06:56 -0800326 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800327 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700328 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800329
330 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700331 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800332
Jeff Brownca9bc702014-02-11 14:32:56 -0800333 InputManagerInternal mInputManagerInternal;
334
Craig Mautner15df08a2015-04-01 12:17:18 -0700335 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
336 * may be finished until there is only one entry left. If this is empty the system is not
337 * in lockTask mode. */
338 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000339 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700340 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
341 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000342 */
343 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400344 /**
345 * Notifies the user when entering/exiting lock-task.
346 */
347 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800348
Craig Mautner15df08a2015-04-01 12:17:18 -0700349 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700350
Craig Mautner42d04db2014-11-06 12:13:23 -0800351 /** Used to keep resumeTopActivityLocked() from being entered recursively */
352 boolean inResumeTopActivity;
353
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700354 // 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 -0700355 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700356 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700357
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700358 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
359 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
360
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700361 // The default minimal size that will be used if the activity doesn't specify its minimal size.
362 // It will be calculated when the default display gets added.
363 private int mDefaultMinimalSizeOfResizeableTask = -1;
364
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700365 // Whether tasks have moved and we need to rank the tasks before next OOM scoring
366 private boolean mTaskLayersChanged = true;
367
Craig Mautneree36c772014-07-16 14:56:05 -0700368 /**
369 * Description of a request to start a new activity, which has been held
370 * due to app switches being disabled.
371 */
372 static class PendingActivityLaunch {
373 final ActivityRecord r;
374 final ActivityRecord sourceRecord;
375 final int startFlags;
376 final ActivityStack stack;
377
378 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
379 int _startFlags, ActivityStack _stack) {
380 r = _r;
381 sourceRecord = _sourceRecord;
382 startFlags = _startFlags;
383 stack = _stack;
384 }
385 }
386
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800387 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700388 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800389 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700390 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
391 }
392
393 /**
394 * At the time when the constructor runs, the power manager has not yet been
395 * initialized. So we initialize our wakelocks afterwards.
396 */
397 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800398 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700399 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700400 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700401 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700402 }
403
justinzhang5286d3f2014-05-12 17:06:01 -0400404 // This function returns a IStatusBarService. The value is from ServiceManager.
405 // getService and is cached.
406 private IStatusBarService getStatusBarService() {
407 synchronized (mService) {
408 if (mStatusBarService == null) {
409 mStatusBarService = IStatusBarService.Stub.asInterface(
410 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
411 if (mStatusBarService == null) {
412 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
413 }
414 }
415 return mStatusBarService;
416 }
417 }
418
Jason Monk35c62a42014-06-17 10:24:47 -0400419 private IDevicePolicyManager getDevicePolicyManager() {
420 synchronized (mService) {
421 if (mDevicePolicyManager == null) {
422 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
423 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
424 if (mDevicePolicyManager == null) {
425 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
426 }
427 }
428 return mDevicePolicyManager;
429 }
430 }
431
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700432 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800433 synchronized (mService) {
434 mWindowManager = wm;
435
436 mDisplayManager =
437 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
438 mDisplayManager.registerDisplayListener(this, null);
439
440 Display[] displays = mDisplayManager.getDisplays();
441 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
442 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800443 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700444 if (activityDisplay.mDisplay == null) {
445 throw new IllegalStateException("Default Display does not exist");
446 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800447 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynski7be9a8c2015-10-15 18:20:30 -0700448 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800449 }
450
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700451 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800452 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800453
454 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800455 }
Craig Mautner27084302013-03-25 08:05:25 -0700456 }
457
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200458 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700459 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200460 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700461 }
462
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700463 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800464 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700465 }
466
Craig Mautnerde4ef022013-04-07 19:01:33 -0700467 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800468 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700469 }
470
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700471 boolean isFocusedStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700472 if (stack == null) {
473 return false;
474 }
475
Craig Mautnerdf88d732014-01-27 09:21:32 -0800476 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
477 if (parent != null) {
478 stack = parent.task.stack;
479 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800480 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700481 }
482
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700483 /** The top most stack. */
484 boolean isFrontStack(ActivityStack stack) {
485 if (stack == null) {
486 return false;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800487 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700488
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700489 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
490 if (parent != null) {
491 stack = parent.task.stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800492 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700493 return stack == mHomeStack.mStacks.get((mHomeStack.mStacks.size() - 1));
494 }
495
496 void setFocusStack(String reason, ActivityStack focusedStack) {
497 mLastFocusedStack = mFocusedStack;
498 mFocusedStack = focusedStack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800499
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700500 EventLogTags.writeAmFocusedStack(
501 mCurrentUser, mFocusedStack == null ? -1 : mFocusedStack.getStackId(),
502 mLastFocusedStack == null ? -1 : mLastFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800503
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800504 if (mService.mBooting || !mService.mBooted) {
505 final ActivityRecord r = topRunningActivityLocked();
506 if (r != null && r.idle) {
507 checkFinishBootingLocked();
508 }
509 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700510 }
511
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700512 void moveHomeStackToFront(String reason) {
513 mHomeStack.moveToFront(reason);
514 }
515
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700516 /** Returns true if the focus activity was adjusted to the home stack top activity. */
517 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700518 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
519 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700520 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700521 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700522
Craig Mautner84984fa2014-06-19 11:19:20 -0700523 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700524
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700525 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700526 if (top == null) {
527 return false;
528 }
529 mService.setFocusedActivityLocked(top, reason);
530 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700531 }
532
Craig Mautner299f9602015-01-26 09:47:33 -0800533 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700534 if (!mService.mBooting && !mService.mBooted) {
535 // Not ready yet!
536 return false;
537 }
538
Craig Mautner84984fa2014-06-19 11:19:20 -0700539 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
540 mWindowManager.showRecentApps();
541 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700542 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700543
Craig Mautner84984fa2014-06-19 11:19:20 -0700544 if (prev != null) {
545 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
546 }
547
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700548 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
549 ActivityRecord r = getHomeActivity();
Mark Lua56ea122015-10-08 13:31:01 +0800550 // Only resume home activity if isn't finishing.
551 if (r != null && !r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -0800552 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700553 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700554 }
Craig Mautner299f9602015-01-26 09:47:33 -0800555 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700556 }
557
Craig Mautner8d341ef2013-03-26 09:03:27 -0700558 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700559 return anyTaskForIdLocked(id, RESTORE_FROM_RECENTS, INVALID_STACK_ID);
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700560 }
561
562 /**
563 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
564 * @param id Id of the task we would like returned.
565 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
566 * restore the task from recents to the active list.
Wale Ogunwale3797c222015-10-27 14:21:58 -0700567 * @param stackId The stack to restore the task to (default launch stack will be used if
568 * stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700569 */
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700570 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents, int stackId) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800571 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800573 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800574 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
575 ActivityStack stack = stacks.get(stackNdx);
576 TaskRecord task = stack.taskForIdLocked(id);
577 if (task != null) {
578 return task;
579 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700580 }
581 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800582
583 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700584 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800585 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800586 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700587 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800588 return null;
589 }
590
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700591 if (!restoreFromRecents) {
592 return task;
593 }
594
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700595 if (!restoreRecentTaskLocked(task, stackId)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700596 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
597 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800598 return null;
599 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700600 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800601 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700602 }
603
Craig Mautner6170f732013-04-02 13:05:23 -0700604 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800605 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800606 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800607 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800608 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
609 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
610 if (r != null) {
611 return r;
612 }
Craig Mautner6170f732013-04-02 13:05:23 -0700613 }
614 }
615 return null;
616 }
617
Craig Mautneref73ee12014-04-23 11:45:37 -0700618 void setNextTaskId(int taskId) {
619 if (taskId > mCurTaskId) {
620 mCurTaskId = taskId;
621 }
622 }
623
Craig Mautner8d341ef2013-03-26 09:03:27 -0700624 int getNextTaskId() {
625 do {
626 mCurTaskId++;
627 if (mCurTaskId <= 0) {
628 mCurTaskId = 1;
629 }
Wale Ogunwale7e8184b2015-10-05 14:37:03 -0700630 } while (anyTaskForIdLocked(mCurTaskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700631 return mCurTaskId;
632 }
633
Craig Mautnerde4ef022013-04-07 19:01:33 -0700634 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800635 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700636 if (stack == null) {
637 return null;
638 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700639 ActivityRecord resumedActivity = stack.mResumedActivity;
640 if (resumedActivity == null || resumedActivity.app == null) {
641 resumedActivity = stack.mPausingActivity;
642 if (resumedActivity == null || resumedActivity.app == null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700643 resumedActivity = stack.topRunningActivityLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700644 }
645 }
646 return resumedActivity;
647 }
648
Dianne Hackbornff072722014-09-24 10:56:28 -0700649 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700650 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800652 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
653 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800654 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
655 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700656 if (!isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800657 continue;
658 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700659 ActivityRecord hr = stack.topRunningActivityLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800660 if (hr != null) {
661 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
662 && processName.equals(hr.processName)) {
663 try {
George Mount2c92c972014-03-20 09:38:23 -0700664 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800665 didSomething = true;
666 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700667 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800668 Slog.w(TAG, "Exception in new application when starting activity "
669 + hr.intent.getComponent().flattenToShortString(), e);
670 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700671 }
Craig Mautner20e72272013-04-01 13:45:53 -0700672 }
Craig Mautner20e72272013-04-01 13:45:53 -0700673 }
674 }
675 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700676 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700677 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700678 }
Craig Mautner20e72272013-04-01 13:45:53 -0700679 return didSomething;
680 }
681
682 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800683 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
684 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
686 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700687 if (!isFocusedStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 continue;
689 }
690 final ActivityRecord resumedActivity = stack.mResumedActivity;
691 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700692 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800693 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800694 return false;
695 }
Craig Mautner20e72272013-04-01 13:45:53 -0700696 }
697 }
698 return true;
699 }
700
Craig Mautnerde4ef022013-04-07 19:01:33 -0700701 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800702 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
703 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800704 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
705 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700706 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700708 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800709 return false;
710 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700711 }
712 }
713 }
714 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700715 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800716 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
717 mLastFocusedStack + " to=" + mFocusedStack);
718 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700719 return true;
720 }
721
722 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800723 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800724 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
725 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800726 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
727 final ActivityStack stack = stacks.get(stackNdx);
728 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800729 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700730 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800731 return false;
732 }
733 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800734 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700735 }
736 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800737 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700738 }
739
Craig Mautner2acc3892013-09-23 10:28:14 -0700740 /**
741 * Pause all activities in either all of the stacks or just the back stacks.
742 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700743 * @return true if any activity was paused as a result of this call.
744 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700745 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700746 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800747 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
748 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800749 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
750 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700751 if (!isFocusedStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700752 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800753 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700754 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
755 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700757 }
758 }
759 return someActivityPaused;
760 }
761
Craig Mautnerde4ef022013-04-07 19:01:33 -0700762 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700763 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
765 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800766 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
767 final ActivityStack stack = stacks.get(stackNdx);
768 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700769 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800770 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700771 Slog.d(TAG_STATES,
772 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800773 pausing = false;
774 } else {
775 return false;
776 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700777 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700778 }
779 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700780 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700781 }
782
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700783 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
784 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500785 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800786 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
787 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
788 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
789 final ActivityStack stack = stacks.get(stackNdx);
790 if (stack.mResumedActivity != null &&
791 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700792 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800793 }
794 }
795 }
796 }
797
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700798 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700799 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700800 }
801
802 void sendWaitingVisibleReportLocked(ActivityRecord r) {
803 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700804 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700805 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700806 if (w.who == null) {
807 changed = true;
808 w.timeout = false;
809 if (r != null) {
810 w.who = new ComponentName(r.info.packageName, r.info.name);
811 }
812 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
813 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700814 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700815 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700816 if (changed) {
817 mService.notifyAll();
818 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700819 }
820
821 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
822 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700823 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700824 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700825 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700826 if (w.who == null) {
827 changed = true;
828 w.timeout = timeout;
829 if (r != null) {
830 w.who = new ComponentName(r.info.packageName, r.info.name);
831 }
832 w.thisTime = thisTime;
833 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700834 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700835 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700836 if (changed) {
837 mService.notifyAll();
838 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700839 }
840
Craig Mautner29219d92013-04-16 20:19:12 -0700841 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800842 final ActivityStack focusedStack = mFocusedStack;
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700843 ActivityRecord r = focusedStack.topRunningActivityLocked();
Craig Mautner1602ec22013-05-12 10:24:27 -0700844 if (r != null) {
845 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700846 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700847
Craig Mautner4a1cb222013-12-04 16:14:06 -0800848 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800849 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800850 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
851 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700852 if (stack != focusedStack && isFrontStack(stack)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700853 r = stack.topRunningActivityLocked();
Craig Mautner29219d92013-04-16 20:19:12 -0700854 if (r != null) {
855 return r;
856 }
857 }
858 }
859 return null;
860 }
861
Dianne Hackborn09233282014-04-30 11:33:59 -0700862 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700863 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800864 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
865 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800866 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800867 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800868 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800869 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
870 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700871 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700873 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700874 }
875 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700876
877 // The lists are already sorted from most recent to oldest. Just pull the most recent off
878 // each list and add it to list. Stop when all lists are empty or maxNum reached.
879 while (maxNum > 0) {
880 long mostRecentActiveTime = Long.MIN_VALUE;
881 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800882 final int numTaskLists = runningTaskLists.size();
883 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
884 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700885 if (!stackTaskList.isEmpty()) {
886 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
887 if (lastActiveTime > mostRecentActiveTime) {
888 mostRecentActiveTime = lastActiveTime;
889 selectedStackList = stackTaskList;
890 }
891 }
892 }
893 if (selectedStackList != null) {
894 list.add(selectedStackList.remove(0));
895 --maxNum;
896 } else {
897 break;
898 }
899 }
Craig Mautner20e72272013-04-01 13:45:53 -0700900 }
901
Craig Mautner23ac33b2013-04-01 16:26:35 -0700902 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700903 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700904 // Collect information about the target of the Intent.
905 ActivityInfo aInfo;
906 try {
907 ResolveInfo rInfo =
908 AppGlobals.getPackageManager().resolveIntent(
909 intent, resolvedType,
910 PackageManager.MATCH_DEFAULT_ONLY
911 | ActivityManagerService.STOCK_PM_FLAGS, userId);
912 aInfo = rInfo != null ? rInfo.activityInfo : null;
913 } catch (RemoteException e) {
914 aInfo = null;
915 }
916
917 if (aInfo != null) {
918 // Store the found target back into the intent, because now that
919 // we have it we never want to do this again. For example, if the
920 // user navigates back to this point in the history, we should
921 // always restart the exact same activity.
922 intent.setComponent(new ComponentName(
923 aInfo.applicationInfo.packageName, aInfo.name));
924
925 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700926 if (!aInfo.processName.equals("system")) {
927 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700928 mService.setDebugApp(aInfo.processName, true, false);
929 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700930
Man Caocfa78b22015-06-11 20:14:34 -0700931 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
932 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
933 }
934
935 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700936 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700937 }
938 }
939 }
940 return aInfo;
941 }
942
Craig Mautner299f9602015-01-26 09:47:33 -0800943 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
944 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700945 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
946 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
947 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
948 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700949 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
950 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700951 null /* outActivity */, null /* container */, null /* inTask */);
952 if (inResumeTopActivity) {
953 // If we are in resume section already, home activity will be initialized, but not
954 // resumed (to avoid recursive resume) and will stay that way until something pokes it
955 // again. We need to schedule another resume.
956 scheduleResumeTopActivities();
957 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700958 }
959
Craig Mautner23ac33b2013-04-01 16:26:35 -0700960 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700961 String callingPackage, Intent intent, String resolvedType,
962 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700963 IBinder resultTo, String resultWho, int requestCode, int startFlags,
964 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700965 Bundle options, boolean ignoreTargetSecurity, int userId,
966 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700967 // Refuse possible leaked file descriptors
968 if (intent != null && intent.hasFileDescriptors()) {
969 throw new IllegalArgumentException("File descriptors passed in Intent");
970 }
971 boolean componentSpecified = intent.getComponent() != null;
972
973 // Don't modify the client's object!
974 intent = new Intent(intent);
975
976 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700977 ActivityInfo aInfo =
978 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700979
Craig Mautnere0a38842013-12-16 16:14:02 -0800980 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700981 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800982 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700983 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800984 // Cannot start a child activity if the parent is not resumed.
985 return ActivityManager.START_CANCELED;
986 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700987 final int realCallingPid = Binder.getCallingPid();
988 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700989 int callingPid;
990 if (callingUid >= 0) {
991 callingPid = -1;
992 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700993 callingPid = realCallingPid;
994 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700995 } else {
996 callingPid = callingUid = -1;
997 }
998
Craig Mautnere0a38842013-12-16 16:14:02 -0800999 final ActivityStack stack;
1000 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001001 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001002 } else {
1003 stack = container.mStack;
1004 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08001005 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001006 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001007 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001008
1009 final long origId = Binder.clearCallingIdentity();
1010
1011 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001012 (aInfo.applicationInfo.privateFlags
1013 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001014 // This may be a heavy-weight process! Check to see if we already
1015 // have another, different heavy-weight process running.
1016 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1017 if (mService.mHeavyWeightProcess != null &&
1018 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1019 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001020 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001021 if (caller != null) {
1022 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1023 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001024 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001025 } else {
1026 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001027 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001028 + intent.toString());
1029 ActivityOptions.abort(options);
1030 return ActivityManager.START_PERMISSION_DENIED;
1031 }
1032 }
1033
1034 IIntentSender target = mService.getIntentSenderLocked(
1035 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001036 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001037 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1038 | PendingIntent.FLAG_ONE_SHOT, null);
1039
1040 Intent newIntent = new Intent();
1041 if (requestCode >= 0) {
1042 // Caller is requesting a result.
1043 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1044 }
1045 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1046 new IntentSender(target));
1047 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1048 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1049 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1050 hist.packageName);
1051 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1052 hist.task.taskId);
1053 }
1054 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1055 aInfo.packageName);
1056 newIntent.setFlags(intent.getFlags());
1057 newIntent.setClassName("android",
1058 HeavyWeightSwitcherActivity.class.getName());
1059 intent = newIntent;
1060 resolvedType = null;
1061 caller = null;
1062 callingUid = Binder.getCallingUid();
1063 callingPid = Binder.getCallingPid();
1064 componentSpecified = true;
1065 try {
1066 ResolveInfo rInfo =
1067 AppGlobals.getPackageManager().resolveIntent(
1068 intent, null,
1069 PackageManager.MATCH_DEFAULT_ONLY
1070 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1071 aInfo = rInfo != null ? rInfo.activityInfo : null;
1072 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1073 } catch (RemoteException e) {
1074 aInfo = null;
1075 }
1076 }
1077 }
1078 }
1079
Dianne Hackborn91097de2014-04-04 18:02:06 -07001080 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1081 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001082 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001083 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001084 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001085
Craig Mautner85c11a82014-07-17 12:38:18 -07001086 Binder.restoreCallingIdentity(origId);
1087
Craig Mautnerde4ef022013-04-07 19:01:33 -07001088 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001089 // If the caller also wants to switch to a new configuration,
1090 // do so now. This allows a clean switch, as we are waiting
1091 // for the current activity to pause (so we will not destroy
1092 // it), and have not yet started the next activity.
1093 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1094 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001095 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001096 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001097 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001098 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001099 }
1100
Craig Mautner23ac33b2013-04-01 16:26:35 -07001101 if (outResult != null) {
1102 outResult.result = res;
1103 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001104 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001105 do {
1106 try {
1107 mService.wait();
1108 } catch (InterruptedException e) {
1109 }
1110 } while (!outResult.timeout && outResult.who == null);
1111 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001112 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001113 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001114 outResult.timeout = false;
1115 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1116 outResult.totalTime = 0;
1117 outResult.thisTime = 0;
1118 } else {
1119 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001120 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001121 do {
1122 try {
1123 mService.wait();
1124 } catch (InterruptedException e) {
1125 }
1126 } while (!outResult.timeout && outResult.who == null);
1127 }
1128 }
1129 }
1130
1131 return res;
1132 }
1133 }
1134
1135 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1136 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1137 Bundle options, int userId) {
1138 if (intents == null) {
1139 throw new NullPointerException("intents is null");
1140 }
1141 if (resolvedTypes == null) {
1142 throw new NullPointerException("resolvedTypes is null");
1143 }
1144 if (intents.length != resolvedTypes.length) {
1145 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1146 }
1147
Craig Mautner23ac33b2013-04-01 16:26:35 -07001148
1149 int callingPid;
1150 if (callingUid >= 0) {
1151 callingPid = -1;
1152 } else if (caller == null) {
1153 callingPid = Binder.getCallingPid();
1154 callingUid = Binder.getCallingUid();
1155 } else {
1156 callingPid = callingUid = -1;
1157 }
1158 final long origId = Binder.clearCallingIdentity();
1159 try {
1160 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001161 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001162 for (int i=0; i<intents.length; i++) {
1163 Intent intent = intents[i];
1164 if (intent == null) {
1165 continue;
1166 }
1167
1168 // Refuse possible leaked file descriptors
1169 if (intent != null && intent.hasFileDescriptors()) {
1170 throw new IllegalArgumentException("File descriptors passed in Intent");
1171 }
1172
1173 boolean componentSpecified = intent.getComponent() != null;
1174
1175 // Don't modify the client's object!
1176 intent = new Intent(intent);
1177
1178 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001179 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001180 // TODO: New, check if this is correct
1181 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1182
1183 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001184 (aInfo.applicationInfo.privateFlags
1185 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001186 throw new IllegalArgumentException(
1187 "FLAG_CANT_SAVE_STATE not supported here");
1188 }
1189
1190 Bundle theseOptions;
1191 if (options != null && i == intents.length-1) {
1192 theseOptions = options;
1193 } else {
1194 theseOptions = null;
1195 }
Craig Mautner6170f732013-04-02 13:05:23 -07001196 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001197 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1198 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001199 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001200 if (res < 0) {
1201 return res;
1202 }
1203
1204 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1205 }
1206 }
1207 } finally {
1208 Binder.restoreCallingIdentity(origId);
1209 }
1210
1211 return ActivityManager.START_SUCCESS;
1212 }
1213
Craig Mautner2420ead2013-04-01 17:13:20 -07001214 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001215 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001216 throws RemoteException {
1217
Craig Mautner2568c3a2015-03-26 14:22:34 -07001218 if (andResume) {
1219 r.startFreezingScreenLocked(app, 0);
1220 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001221
Craig Mautner2568c3a2015-03-26 14:22:34 -07001222 // schedule launch ticks to collect information about slow apps.
1223 r.startLaunchTickingLocked();
1224 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001225
1226 // Have the window manager re-evaluate the orientation of
1227 // the screen based on the new activity order. Note that
1228 // as a result of this, it can call back into the activity
1229 // manager with a new orientation. We don't care about that,
1230 // because the activity is not currently running so we are
1231 // just restarting it anyway.
1232 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001233 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001234 mService.mConfiguration,
1235 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001236 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001237 }
1238
1239 r.app = app;
1240 app.waitingToKill = null;
1241 r.launchCount++;
1242 r.lastLaunchTime = SystemClock.uptimeMillis();
1243
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001244 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001245
1246 int idx = app.activities.indexOf(r);
1247 if (idx < 0) {
1248 app.activities.add(r);
1249 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001250 mService.updateLruProcessLocked(app, true, null);
1251 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001252
Craig Mautner15df08a2015-04-01 12:17:18 -07001253 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001254 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1255 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001256 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001257 }
1258
1259 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001260 try {
1261 if (app.thread == null) {
1262 throw new RemoteException();
1263 }
1264 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001265 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001266 if (andResume) {
1267 results = r.results;
1268 newIntents = r.newIntents;
1269 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001270 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1271 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1272 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001273 if (andResume) {
1274 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1275 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001276 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001277 }
Chong Zhang85ee6542015-10-02 13:36:38 -07001278 if (r.isHomeActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001279 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001280 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001281 }
1282 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1283 r.sleeping = false;
1284 r.forceNewConfig = false;
1285 mService.showAskCompatModeDialogLocked(r);
1286 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001287 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001288 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1289 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1290 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001291 final String profileFile = mService.mProfileFile;
1292 if (profileFile != null) {
1293 ParcelFileDescriptor profileFd = mService.mProfileFd;
1294 if (profileFd != null) {
1295 try {
1296 profileFd = profileFd.dup();
1297 } catch (IOException e) {
1298 if (profileFd != null) {
1299 try {
1300 profileFd.close();
1301 } catch (IOException o) {
1302 }
1303 profileFd = null;
1304 }
1305 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001306 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001307
1308 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1309 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001310 }
1311 }
1312 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001313
Craig Mautner2568c3a2015-03-26 14:22:34 -07001314 if (andResume) {
1315 app.hasShownUi = true;
1316 app.pendingUiClean = true;
1317 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001318 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001319 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001320 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001321 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001322 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001323 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001324
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001325 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001326 // This may be a heavy-weight process! Note that the package
1327 // manager will ensure that only activity can run in the main
1328 // process of the .apk, which is the only thing that will be
1329 // considered heavy-weight.
1330 if (app.processName.equals(app.info.packageName)) {
1331 if (mService.mHeavyWeightProcess != null
1332 && mService.mHeavyWeightProcess != app) {
1333 Slog.w(TAG, "Starting new heavy weight process " + app
1334 + " when already running "
1335 + mService.mHeavyWeightProcess);
1336 }
1337 mService.mHeavyWeightProcess = app;
1338 Message msg = mService.mHandler.obtainMessage(
1339 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1340 msg.obj = r;
1341 mService.mHandler.sendMessage(msg);
1342 }
1343 }
1344
1345 } catch (RemoteException e) {
1346 if (r.launchFailed) {
1347 // This is the second time we failed -- finish activity
1348 // and give up.
1349 Slog.e(TAG, "Second failure launching "
1350 + r.intent.getComponent().flattenToShortString()
1351 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001352 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001353 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1354 "2nd-crash", false);
1355 return false;
1356 }
1357
1358 // This is the first time we failed -- restart process and
1359 // retry.
1360 app.activities.remove(r);
1361 throw e;
1362 }
1363
1364 r.launchFailed = false;
1365 if (stack.updateLRUListLocked(r)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07001366 Slog.w(TAG, "Activity " + r + " being launched, but already in LRU list");
Craig Mautner2420ead2013-04-01 17:13:20 -07001367 }
1368
1369 if (andResume) {
1370 // As part of the process of launching, ActivityThread also performs
1371 // a resume.
1372 stack.minimalResumeActivityLocked(r);
1373 } else {
1374 // This activity is not starting in the resumed state... which
1375 // should look like we asked it to pause+stop (but remain visible),
1376 // and it has done so and reported back the current icicle and
1377 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001378 if (DEBUG_STATES) Slog.v(TAG_STATES,
1379 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001380 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001381 r.stopped = true;
1382 }
1383
1384 // Launch the new version setup screen if needed. We do this -after-
1385 // launching the initial activity (that is, home), so that it can have
1386 // a chance to initialize itself while in the background, making the
1387 // switch back to it faster and look better.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001388 if (isFocusedStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001389 mService.startSetupActivityLocked();
1390 }
1391
Dianne Hackborn465fa392014-09-14 14:21:18 -07001392 // Update any services we are bound to that might care about whether
1393 // their client may have activities.
1394 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1395
Craig Mautner2420ead2013-04-01 17:13:20 -07001396 return true;
1397 }
1398
Craig Mautnere79d42682013-04-01 19:01:53 -07001399 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001400 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001401 // Is this activity's application already running?
1402 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001403 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001404
1405 r.task.stack.setLaunchTime(r);
1406
1407 if (app != null && app.thread != null) {
1408 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001409 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1410 || !"android".equals(r.info.packageName)) {
1411 // Don't add this if it is a platform component that is marked
1412 // to run in multiple processes, because this is actually
1413 // part of the framework so doesn't make sense to track as a
1414 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001415 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1416 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001417 }
George Mount2c92c972014-03-20 09:38:23 -07001418 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001419 return;
1420 } catch (RemoteException e) {
1421 Slog.w(TAG, "Exception when starting activity "
1422 + r.intent.getComponent().flattenToShortString(), e);
1423 }
1424
1425 // If a dead object exception was thrown -- fall through to
1426 // restart the application.
1427 }
1428
1429 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001430 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001431 }
1432
Craig Mautner6170f732013-04-02 13:05:23 -07001433 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001434 Intent intent, String resolvedType, ActivityInfo aInfo,
1435 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1436 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001437 int callingPid, int callingUid, String callingPackage,
1438 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001439 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1440 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001441 int err = ActivityManager.START_SUCCESS;
1442
1443 ProcessRecord callerApp = null;
1444 if (caller != null) {
1445 callerApp = mService.getRecordForAppLocked(caller);
1446 if (callerApp != null) {
1447 callingPid = callerApp.pid;
1448 callingUid = callerApp.info.uid;
1449 } else {
1450 Slog.w(TAG, "Unable to find app for caller " + caller
1451 + " (pid=" + callingPid + ") when starting: "
1452 + intent.toString());
1453 err = ActivityManager.START_PERMISSION_DENIED;
1454 }
1455 }
1456
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001457 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1458
Craig Mautner6170f732013-04-02 13:05:23 -07001459 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001460 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001461 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001462 + " on display " + (container == null ? (mFocusedStack == null ?
1463 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1464 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1465 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001466 }
1467
1468 ActivityRecord sourceRecord = null;
1469 ActivityRecord resultRecord = null;
1470 if (resultTo != null) {
1471 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001472 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1473 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001474 if (sourceRecord != null) {
1475 if (requestCode >= 0 && !sourceRecord.finishing) {
1476 resultRecord = sourceRecord;
1477 }
1478 }
1479 }
Craig Mautner6170f732013-04-02 13:05:23 -07001480
Dianne Hackborn91097de2014-04-04 18:02:06 -07001481 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001482
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001483 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001484 // Transfer the result target from the source activity to the new
1485 // one being started, including any failures.
1486 if (requestCode >= 0) {
1487 ActivityOptions.abort(options);
1488 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1489 }
1490 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001491 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1492 resultRecord = null;
1493 }
Craig Mautner6170f732013-04-02 13:05:23 -07001494 resultWho = sourceRecord.resultWho;
1495 requestCode = sourceRecord.requestCode;
1496 sourceRecord.resultTo = null;
1497 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001498 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001499 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001500 if (sourceRecord.launchedFromUid == callingUid) {
1501 // The new activity is being launched from the same uid as the previous
1502 // activity in the flow, and asking to forward its result back to the
1503 // previous. In this case the activity is serving as a trampoline between
1504 // the two, so we also want to update its launchedFromPackage to be the
1505 // same as the previous activity. Note that this is safe, since we know
1506 // these two packages come from the same uid; the caller could just as
1507 // well have supplied that same package name itself. This specifially
1508 // deals with the case of an intent picker/chooser being launched in the app
1509 // flow to redirect to an activity picked by the user, where we want the final
1510 // activity to consider it to have been launched by the previous app activity.
1511 callingPackage = sourceRecord.launchedFromPackage;
1512 }
Craig Mautner6170f732013-04-02 13:05:23 -07001513 }
1514
1515 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1516 // We couldn't find a class that can handle the given Intent.
1517 // That's the end of that!
1518 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1519 }
1520
1521 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1522 // We couldn't find the specific class specified in the Intent.
1523 // Also the end of the line.
1524 err = ActivityManager.START_CLASS_NOT_FOUND;
1525 }
1526
Dianne Hackborn91097de2014-04-04 18:02:06 -07001527 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1528 && sourceRecord.task.voiceSession != null) {
1529 // If this activity is being launched as part of a voice session, we need
1530 // to ensure that it is safe to do so. If the upcoming activity will also
1531 // be part of the voice session, we can only launch it if it has explicitly
1532 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001533 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001534 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1535 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001536 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001537 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1538 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001539 Slog.w(TAG,
1540 "Activity being started in current voice task does not support voice: "
1541 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001542 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1543 }
1544 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001545 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001546 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1547 }
1548 }
1549 }
1550
1551 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1552 // If the caller is starting a new voice session, just make sure the target
1553 // is actually allowing it to run this way.
1554 try {
1555 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1556 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001557 Slog.w(TAG,
1558 "Activity being started in new voice task does not support: "
1559 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001560 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1561 }
1562 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001563 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001564 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1565 }
1566 }
1567
louis_changcd5d1982014-08-01 10:09:08 +08001568 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1569
Craig Mautner6170f732013-04-02 13:05:23 -07001570 if (err != ActivityManager.START_SUCCESS) {
1571 if (resultRecord != null) {
1572 resultStack.sendActivityResultLocked(-1,
1573 resultRecord, resultWho, requestCode,
1574 Activity.RESULT_CANCELED, null);
1575 }
Craig Mautner6170f732013-04-02 13:05:23 -07001576 ActivityOptions.abort(options);
1577 return err;
1578 }
1579
Svetoslav7008b512015-06-24 18:47:07 -07001580 boolean abort = false;
1581
Svet Ganov99b60432015-06-27 13:15:22 -07001582 final int startAnyPerm = mService.checkPermission(
1583 START_ANY_ACTIVITY, callingPid, callingUid);
1584
1585 if (startAnyPerm != PERMISSION_GRANTED) {
1586 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001587 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001588 final int actionRestriction = getActionRestrictionForCallingPackage(
1589 intent.getAction(), callingPackage, callingPid, callingUid);
1590
1591 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1592 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1593 if (resultRecord != null) {
1594 resultStack.sendActivityResultLocked(-1,
1595 resultRecord, resultWho, requestCode,
1596 Activity.RESULT_CANCELED, null);
1597 }
1598 String msg;
1599 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1600 msg = "Permission Denial: starting " + intent.toString()
1601 + " from " + callerApp + " (pid=" + callingPid
1602 + ", uid=" + callingUid + ")" + " with revoked permission "
1603 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1604 } else if (!aInfo.exported) {
1605 msg = "Permission Denial: starting " + intent.toString()
1606 + " from " + callerApp + " (pid=" + callingPid
1607 + ", uid=" + callingUid + ")"
1608 + " not exported from uid " + aInfo.applicationInfo.uid;
1609 } else {
1610 msg = "Permission Denial: starting " + intent.toString()
1611 + " from " + callerApp + " (pid=" + callingPid
1612 + ", uid=" + callingUid + ")"
1613 + " requires " + aInfo.permission;
1614 }
1615 Slog.w(TAG, msg);
1616 throw new SecurityException(msg);
1617 }
1618
1619 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1620 String message = "Appop Denial: starting " + intent.toString()
1621 + " from " + callerApp + " (pid=" + callingPid
1622 + ", uid=" + callingUid + ")"
1623 + " requires " + AppOpsManager.permissionToOp(
1624 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1625 Slog.w(TAG, message);
1626 abort = true;
1627 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1628 String message = "Appop Denial: starting " + intent.toString()
1629 + " from " + callerApp + " (pid=" + callingPid
1630 + ", uid=" + callingUid + ")"
1631 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1632 Slog.w(TAG, message);
1633 abort = true;
1634 }
Svetoslav7008b512015-06-24 18:47:07 -07001635 }
1636
1637 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001638 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001639
Craig Mautner6170f732013-04-02 13:05:23 -07001640 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001641 try {
1642 // The Intent we give to the watcher has the extra data
1643 // stripped off, since it can contain private information.
1644 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001645 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001646 aInfo.applicationInfo.packageName);
1647 } catch (RemoteException e) {
1648 mService.mController = null;
1649 }
Ben Gruver5e207332013-04-03 17:41:37 -07001650 }
Craig Mautner6170f732013-04-02 13:05:23 -07001651
Ben Gruver5e207332013-04-03 17:41:37 -07001652 if (abort) {
1653 if (resultRecord != null) {
1654 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001655 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001656 }
Ben Gruver5e207332013-04-03 17:41:37 -07001657 // We pretend to the caller that it was really started, but
1658 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001659 ActivityOptions.abort(options);
1660 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001661 }
1662
1663 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001664 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001665 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001666 if (outActivity != null) {
1667 outActivity[0] = r;
1668 }
1669
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001670 if (r.appTimeTracker == null && sourceRecord != null) {
1671 // If the caller didn't specify an explicit time tracker, we want to continue
1672 // tracking under any it has.
1673 r.appTimeTracker = sourceRecord.appTimeTracker;
1674 }
1675
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001676 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001677 if (voiceSession == null && (stack.mResumedActivity == null
1678 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001679 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1680 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001681 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001682 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001683 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001684 ActivityOptions.abort(options);
1685 return ActivityManager.START_SWITCHES_CANCELED;
1686 }
1687 }
1688
1689 if (mService.mDidAppSwitch) {
1690 // This is the second allowed switch since we stopped switches,
1691 // so now just generally allow switches. Use case: user presses
1692 // home (switches disabled, switch to home, mDidAppSwitch now true);
1693 // user taps a home icon (coming from home so allowed, we hit here
1694 // and now allow anyone to switch again).
1695 mService.mAppSwitchesAllowedTime = 0;
1696 } else {
1697 mService.mDidAppSwitch = true;
1698 }
1699
Craig Mautneree36c772014-07-16 14:56:05 -07001700 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001701
Dianne Hackborn91097de2014-04-04 18:02:06 -07001702 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001703 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001704
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001705 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001706 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001707 // activity start, but we are not actually doing an activity
1708 // switch... just dismiss the keyguard now, because we
1709 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001710 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001711 }
1712 return err;
1713 }
1714
Svet Ganov99b60432015-06-27 13:15:22 -07001715 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001716 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001717 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1718 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001719 == PackageManager.PERMISSION_DENIED) {
1720 return ACTIVITY_RESTRICTION_PERMISSION;
1721 }
1722
Christopher Tateff7add02015-08-17 10:23:22 -07001723 if (activityInfo.permission == null) {
1724 return ACTIVITY_RESTRICTION_NONE;
1725 }
1726
Svet Ganov99b60432015-06-27 13:15:22 -07001727 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1728 if (opCode == AppOpsManager.OP_NONE) {
1729 return ACTIVITY_RESTRICTION_NONE;
1730 }
1731
1732 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1733 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001734 if (!ignoreTargetSecurity) {
1735 return ACTIVITY_RESTRICTION_APPOP;
1736 }
Svet Ganov99b60432015-06-27 13:15:22 -07001737 }
1738
1739 return ACTIVITY_RESTRICTION_NONE;
1740 }
1741
Svetoslav7008b512015-06-24 18:47:07 -07001742 private int getActionRestrictionForCallingPackage(String action,
1743 String callingPackage, int callingPid, int callingUid) {
1744 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001745 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001746 }
1747
1748 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1749 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001750 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001751 }
1752
1753 final PackageInfo packageInfo;
1754 try {
1755 packageInfo = mService.mContext.getPackageManager()
1756 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1757 } catch (PackageManager.NameNotFoundException e) {
1758 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001759 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001760 }
1761
1762 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001763 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001764 }
1765
1766 if (mService.checkPermission(permission, callingPid, callingUid) ==
1767 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001768 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001769 }
1770
1771 final int opCode = AppOpsManager.permissionToOpCode(permission);
1772 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001773 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001774 }
1775
1776 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1777 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001778 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001779 }
1780
Svet Ganov99b60432015-06-27 13:15:22 -07001781 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001782 }
1783
Chong Zhang0fa656b2015-08-31 15:17:21 -07001784 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001785 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001786
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001787 if (!(r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
1788 return mHomeStack;
1789 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001790
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001791 ActivityStack stack;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001792
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001793 if (task != null && task.stack != null) {
1794 stack = task.stack;
1795 if (stack.isOnHomeDisplay()) {
1796 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001797 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001798 "computeStackFocus: Setting " + "focused stack to r=" + r
1799 + " task=" + task);
1800 } else {
1801 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1802 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautner858d8a62013-04-23 17:08:34 -07001803 }
1804 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001805 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001806 }
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001807
1808 final ActivityContainer container = r.mInitialActivityContainer;
1809 if (container != null) {
1810 // The first time put it on the desired stack, after this put on task stack.
1811 r.mInitialActivityContainer = null;
1812 return container.mStack;
1813 }
1814
1815 // The fullscreen stack can contain any task regardless of if the task is resizeable
1816 // or not. So, we let the task go in the fullscreen task if it is the focus stack.
1817 // If the freeform stack has focus, and the activity to be launched is resizeable,
1818 // we can also put it in the focused stack.
1819 final boolean canUseFocusedStack =
1820 mFocusedStack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1821 || mFocusedStack.mStackId == FREEFORM_WORKSPACE_STACK_ID && r.info.resizeable;
1822 if (canUseFocusedStack
1823 && (!newTask || mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
1824 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1825 "computeStackFocus: Have a focused stack=" + mFocusedStack);
1826 return mFocusedStack;
1827 }
1828
1829 // We first try to put the task in the first dynamic stack.
1830 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1831 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1832 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001833 if (!StackId.isStaticStack(stack.mStackId)) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07001834 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1835 "computeStackFocus: Setting focused stack=" + stack);
1836 return stack;
1837 }
1838 }
1839
1840 // If there is no suitable dynamic stack then we figure out which static stack to use.
1841 final int stackId = task != null ? task.getLaunchStackId() :
1842 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1843 FULLSCREEN_WORKSPACE_STACK_ID;
1844 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
1845 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1846 + r + " stackId=" + stack.mStackId);
1847 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001848 }
1849
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001850 boolean setFocusedStack(ActivityRecord r, String reason) {
1851 if (r == null) {
1852 // Not sure what you are trying to do, but it is not going to work...
1853 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001854 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001855 final TaskRecord task = r.task;
1856 if (task == null || task.stack == null) {
1857 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1858 return false;
1859 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001860 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001861 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001862 }
1863
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001864 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001865 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001866 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001867 final Intent intent = r.intent;
1868 final int callingUid = r.launchedFromUid;
1869
Chong Zhang0fa656b2015-08-31 15:17:21 -07001870 boolean overrideBounds = false;
1871 Rect newBounds = null;
Chong Zhang56f171d2015-10-08 10:39:23 -07001872 if (options != null && (r.info.resizeable || (inTask != null && inTask.mResizeable))) {
1873 ActivityOptions opts = new ActivityOptions(options);
1874 if (opts.hasBounds()) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07001875 overrideBounds = true;
Chong Zhang56f171d2015-10-08 10:39:23 -07001876 newBounds = opts.getBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07001877 }
1878 }
1879
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001880 // In some flows in to this function, we retrieve the task record and hold on to it
1881 // without a lock before calling back in to here... so the task at this point may
1882 // not actually be in recents. Check for that, and if it isn't in recents just
1883 // consider it invalid.
1884 if (inTask != null && !inTask.inRecents) {
1885 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1886 inTask = null;
1887 }
1888
Craig Mautnerad400af2014-08-13 16:41:36 -07001889 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001890 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1891 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001892
Craig Mautnera228ae92014-07-09 05:44:55 -07001893 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001894 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001895 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001896 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1897 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1898 "\"singleInstance\" or \"singleTask\"");
1899 launchFlags &=
1900 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1901 } else {
1902 switch (r.info.documentLaunchMode) {
1903 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1904 break;
1905 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1906 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1907 break;
1908 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1909 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1910 break;
1911 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1912 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1913 break;
1914 }
1915 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001916
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001917 final boolean launchTaskBehind = r.mLaunchTaskBehind
1918 && !launchSingleTask && !launchSingleInstance
1919 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001920
Wale Ogunwale7d701172015-03-11 15:36:30 -07001921 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1922 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001923 // For whatever reason this activity is being launched into a new
1924 // task... yet the caller has requested a result back. Well, that
1925 // is pretty messed up, so instead immediately send back a cancel
1926 // and let the new task continue launched as normal without a
1927 // dependency on its originator.
1928 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1929 r.resultTo.task.stack.sendActivityResultLocked(-1,
1930 r.resultTo, r.resultWho, r.requestCode,
1931 Activity.RESULT_CANCELED, null);
1932 r.resultTo = null;
1933 }
1934
1935 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1936 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1937 }
1938
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001939 // If we are actually going to launch in to a new task, there are some cases where
1940 // we further want to do multiple task.
1941 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1942 if (launchTaskBehind
1943 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1944 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1945 }
1946 }
1947
Craig Mautner8849a5e2013-04-02 16:41:03 -07001948 // We'll invoke onUserLeaving before onPause only if the launching
1949 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001950 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001951 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1952 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001953
1954 // If the caller has asked not to resume at this point, we make note
1955 // of this in the record so that we can skip it when trying to find
1956 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001957 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001958 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001959 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001960 }
1961
Craig Mautnera254cd72014-05-25 16:47:39 -07001962 ActivityRecord notTop =
1963 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001964
1965 // If the onlyIfNeeded flag is set, then we can do this if the activity
1966 // being launched is the same as the one making the call... or, as
1967 // a special case, if we do not know the caller then we count the
1968 // current top activity as the caller.
1969 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1970 ActivityRecord checkedCaller = sourceRecord;
1971 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001972 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001973 }
1974 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1975 // Caller is not the same as launcher, so always needed.
1976 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1977 }
1978 }
1979
Craig Mautner8849a5e2013-04-02 16:41:03 -07001980 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001981 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001982
1983 // If the caller is not coming from another activity, but has given us an
1984 // explicit task into which they would like us to launch the new activity,
1985 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001986 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1987 final Intent baseIntent = inTask.getBaseIntent();
1988 final ActivityRecord root = inTask.getRootActivity();
1989 if (baseIntent == null) {
1990 ActivityOptions.abort(options);
1991 throw new IllegalArgumentException("Launching into task without base intent: "
1992 + inTask);
1993 }
1994
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001995 // If this task is empty, then we are adding the first activity -- it
1996 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001997 if (launchSingleInstance || launchSingleTask) {
1998 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1999 ActivityOptions.abort(options);
2000 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
2001 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002002 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002003 if (root != null) {
2004 ActivityOptions.abort(options);
2005 throw new IllegalArgumentException("Caller with inTask " + inTask
2006 + " has root " + root + " but target is singleInstance/Task");
2007 }
2008 }
2009
2010 // If task is empty, then adopt the interesting intent launch flags in to the
2011 // activity being started.
2012 if (root == null) {
2013 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2014 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2015 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2016 launchFlags = (launchFlags&~flagsOfInterest)
2017 | (baseIntent.getFlags()&flagsOfInterest);
2018 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002019 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002020 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002021
Dianne Hackborn962d5352014-08-29 16:27:40 -07002022 // If the task is not empty and the caller is asking to start it as the root
2023 // of a new task, then we don't actually want to start this on the task. We
2024 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002025 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002026 addingToTask = false;
2027
2028 } else {
2029 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002030 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002031
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002032 reuseTask = inTask;
2033 } else {
2034 inTask = null;
Chong Zhang85ee6542015-10-02 13:36:38 -07002035 // Launch ResolverActivity in the source task, so that it stays in the task
2036 // bounds when in freeform workspace.
2037 // Also put noDisplay activities in the source task. These by itself can
2038 // be placed in any task/stack, however it could launch other activities
2039 // like ResolverActivity, and we want those to stay in the original task.
2040 if (r.isResolverActivity() || r.noDisplay) {
2041 addingToTask = true;
2042 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002043 }
2044
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002045 if (inTask == null) {
2046 if (sourceRecord == null) {
2047 // This activity is not being started from another... in this
2048 // case we -always- start a new task.
2049 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2050 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2051 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2052 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2053 }
2054 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2055 // The original activity who is starting us is running as a single
2056 // instance... this new activity it is starting must go on its
2057 // own task.
2058 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2059 } else if (launchSingleInstance || launchSingleTask) {
2060 // The activity being started is a single instance... it always
2061 // gets launched into its own task.
2062 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2063 }
2064 }
2065
2066 ActivityInfo newTaskInfo = null;
2067 Intent newTaskIntent = null;
2068 ActivityStack sourceStack;
2069 if (sourceRecord != null) {
2070 if (sourceRecord.finishing) {
2071 // If the source is finishing, we can't further count it as our source. This
2072 // is because the task it is associated with may now be empty and on its way out,
2073 // so we don't want to blindly throw it in to that task. Instead we will take
2074 // the NEW_TASK flow and try to find a task for it. But save the task information
2075 // so it can be used when creating the new task.
2076 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2077 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2078 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2079 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2080 newTaskInfo = sourceRecord.info;
2081 newTaskIntent = sourceRecord.task.intent;
2082 }
2083 sourceRecord = null;
2084 sourceStack = null;
2085 } else {
2086 sourceStack = sourceRecord.task.stack;
2087 }
2088 } else {
2089 sourceStack = null;
2090 }
2091
2092 boolean movedHome = false;
2093 ActivityStack targetStack;
2094
2095 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002096 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002097
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002098 // We may want to try to place the new activity in to an existing task. We always
2099 // do this if the target activity is singleTask or singleInstance; we will also do
2100 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2101 // us to still place it in a new task: multi task, always doc mode, or being asked to
2102 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002103 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2104 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002105 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002106 // If bring to front is requested, and no result is requested and we have not
2107 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002108 // we can find a task that was started with this same
2109 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002110 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002111 // See if there is a task to bring to the front. If this is
2112 // a SINGLE_INSTANCE activity, there can be one and only one
2113 // instance of it in the history, and it is always in its own
2114 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002115 ActivityRecord intentActivity = !launchSingleInstance ?
2116 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002117 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002118 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2119 // but still needs to be a lock task mode violation since the task gets
2120 // cleared out and the device would otherwise leave the locked task.
2121 if (isLockTaskModeViolation(intentActivity.task,
2122 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2123 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002124 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002125 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002126 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2127 }
Craig Mautner29219d92013-04-16 20:19:12 -07002128 if (r.task == null) {
2129 r.task = intentActivity.task;
2130 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002131 if (intentActivity.task.intent == null) {
2132 // This task was started because of movement of
2133 // the activity based on affinity... now that we
2134 // are actually launching it, we can assign the
2135 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002136 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002137 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002138 targetStack = intentActivity.task.stack;
2139 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002140 // If the target task is not in the front, then we need
2141 // to bring it to the front... except... well, with
2142 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2143 // to have the same behavior as if a new instance was
2144 // being started, which means not bringing it to the front
2145 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002146 final ActivityStack focusStack = getFocusedStack();
2147 ActivityRecord curTop = (focusStack == null)
2148 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002149 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002150 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002151 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002152 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002153 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2154 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002155 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002156 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002157 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2158 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002159 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002160 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002161 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002162 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002163 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002164 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2165 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002166 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002167 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002168 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002169 options = null;
2170 }
2171 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002172 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002173 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002174 + " from " + intentActivity);
2175 targetStack.moveToFront("intentActivityFound");
2176 }
2177
Craig Mautner8849a5e2013-04-02 16:41:03 -07002178 // If the caller has requested that the target task be
2179 // reset, then do so.
2180 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2181 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2182 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002183 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002184 // We don't need to start a new activity, and
2185 // the client said not to do anything if that
2186 // is the case, so this is it! And for paranoia, make
2187 // sure we have correctly resumed the top activity.
2188 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002189 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002190
2191 // Make sure to notify Keyguard as well if we are not running an app
2192 // transition later.
2193 if (!movedToFront) {
2194 notifyActivityDrawnForKeyguard();
2195 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002196 } else {
2197 ActivityOptions.abort(options);
2198 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002199 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002200 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2201 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002202 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002203 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002204 // The caller has requested to completely replace any
2205 // existing task with its new activity. Well that should
2206 // not be too hard...
2207 reuseTask = intentActivity.task;
2208 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002209 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002210 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002211 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002212 // In this situation we want to remove all activities
2213 // from the task up to the one being started. In most
2214 // cases this means we are resetting the task to its
2215 // initial state.
2216 ActivityRecord top =
2217 intentActivity.task.performClearTaskLocked(r, launchFlags);
2218 if (top != null) {
2219 if (top.frontOfTask) {
2220 // Activity aliases may mean we use different
2221 // intents for the top activity, so make sure
2222 // the task now has the identity of the new
2223 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002224 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002225 }
2226 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2227 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002228 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002229 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002230 // A special case: we need to start the activity because it is not
2231 // currently running, and the caller has asked to clear the current
2232 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002233 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002234 // Now pretend like this activity is being started by the top of its
2235 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002236 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002237 TaskRecord task = sourceRecord.task;
2238 if (task != null && task.stack == null) {
2239 // Target stack got cleared when we all activities were removed
2240 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002241 targetStack = computeStackFocus(
2242 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002243 targetStack.addTask(
2244 task, !launchTaskBehind /* toTop */, false /* moving */);
2245 }
2246
Craig Mautner8849a5e2013-04-02 16:41:03 -07002247 }
2248 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2249 // In this case the top activity on the task is the
2250 // same as the one being launched, so we take that
2251 // as a request to bring the task to the foreground.
2252 // If the top activity in the task is the root
2253 // activity, deliver this new intent to it if it
2254 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002255 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002256 && intentActivity.realActivity.equals(r.realActivity)) {
2257 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2258 intentActivity.task);
2259 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002260 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002261 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002262 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2263 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002264 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2265 // In this case we are launching the root activity
2266 // of the task, but with a different intent. We
2267 // should start a new instance on top.
2268 addingToTask = true;
2269 sourceRecord = intentActivity;
2270 }
2271 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2272 // In this case an activity is being launched in to an
2273 // existing task, without resetting that task. This
2274 // is typically the situation of launching an activity
2275 // from a notification or shortcut. We want to place
2276 // the new activity on top of the current task.
2277 addingToTask = true;
2278 sourceRecord = intentActivity;
2279 } else if (!intentActivity.task.rootWasReset) {
2280 // In this case we are launching in to an existing task
2281 // that has not yet been started from its front door.
2282 // The current task has been brought to the front.
2283 // Ideally, we'd probably like to place this new task
2284 // at the bottom of its stack, but that's a little hard
2285 // to do with the current organization of the code so
2286 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002287 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002288 }
2289 if (!addingToTask && reuseTask == null) {
2290 // We didn't do anything... but it was needed (a.k.a., client
2291 // don't use that intent!) And for paranoia, make
2292 // sure we have correctly resumed the top activity.
2293 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002294 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002295 if (!movedToFront) {
2296 // Make sure to notify Keyguard as well if we are not running an app
2297 // transition later.
2298 notifyActivityDrawnForKeyguard();
2299 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002300 } else {
2301 ActivityOptions.abort(options);
2302 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002303 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002304 return ActivityManager.START_TASK_TO_FRONT;
2305 }
2306 }
2307 }
2308 }
2309
2310 //String uri = r.intent.toURI();
2311 //Intent intent2 = new Intent(uri);
2312 //Slog.i(TAG, "Given intent: " + r.intent);
2313 //Slog.i(TAG, "URI is: " + uri);
2314 //Slog.i(TAG, "To intent: " + intent2);
2315
2316 if (r.packageName != null) {
2317 // If the activity being launched is the same as the one currently
2318 // at the top, then we need to check if it should only be launched
2319 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002320 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002321 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002322 if (top != null && r.resultTo == null) {
2323 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2324 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002325 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002326 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002327 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2328 top.task);
2329 // For paranoia, make sure we have correctly
2330 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002331 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002332 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002333 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002334 }
2335 ActivityOptions.abort(options);
2336 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2337 // We don't need to start a new activity, and
2338 // the client said not to do anything if that
2339 // is the case, so this is it!
2340 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2341 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002342 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002343 return ActivityManager.START_DELIVERED_TO_TOP;
2344 }
2345 }
2346 }
2347 }
2348
2349 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002350 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002351 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2352 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002353 }
2354 ActivityOptions.abort(options);
2355 return ActivityManager.START_CLASS_NOT_FOUND;
2356 }
2357
2358 boolean newTask = false;
2359 boolean keepCurTransition = false;
2360
Craig Mautnerbb742462014-07-07 15:28:55 -07002361 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002362 sourceRecord.task : null;
2363
Craig Mautner8849a5e2013-04-02 16:41:03 -07002364 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002365 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002366 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002367 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002368 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002369 if (doResume) {
2370 targetStack.moveToFront("startingNewTask");
2371 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002372
Craig Mautner8849a5e2013-04-02 16:41:03 -07002373 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002374 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2375 newTaskInfo != null ? newTaskInfo : r.info,
2376 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002377 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2378 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002379 if (overrideBounds) {
2380 r.task.updateOverrideConfiguration(newBounds);
2381 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002382 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2383 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002384 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002385 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002386 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002387 if (isLockTaskModeViolation(r.task)) {
2388 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2389 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2390 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002391 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002392 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002393 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2394 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002395 // Caller wants to appear on home activity, so before starting
2396 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002397 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002398 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002399 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002400 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002401 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002402 if (isLockTaskModeViolation(sourceTask)) {
2403 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2404 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2405 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002406 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002407 if (doResume) {
2408 targetStack.moveToFront("sourceStackToFront");
2409 }
Craig Mautner737fae22014-10-15 12:52:10 -07002410 final TaskRecord topTask = targetStack.topTask();
2411 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002412 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002413 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002414 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002415 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002416 // In this case, we are adding the activity to an existing
2417 // task, but the caller has asked to clear that task if the
2418 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002419 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002420 keepCurTransition = true;
2421 if (top != null) {
2422 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002423 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002424 // For paranoia, make sure we have correctly
2425 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002426 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002427 if (doResume) {
2428 targetStack.resumeTopActivityLocked(null);
2429 }
2430 ActivityOptions.abort(options);
2431 return ActivityManager.START_DELIVERED_TO_TOP;
2432 }
2433 } else if (!addingToTask &&
2434 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2435 // In this case, we are launching an activity in our own task
2436 // that may already be running somewhere in the history, and
2437 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002438 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002439 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002440 final TaskRecord task = top.task;
2441 task.moveActivityToFrontLocked(top);
2442 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002443 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002444 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002445 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002446 if (doResume) {
2447 targetStack.resumeTopActivityLocked(null);
2448 }
2449 return ActivityManager.START_DELIVERED_TO_TOP;
2450 }
2451 }
2452 // An existing activity is starting this new activity, so we want
2453 // to keep the new one in the same task as the one that is starting
2454 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002455 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002456 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002457 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002458
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002459 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002460 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002461 // task it has provided to us.
2462 if (isLockTaskModeViolation(inTask)) {
2463 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2464 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2465 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002466 if (overrideBounds) {
2467 inTask.updateOverrideConfiguration(newBounds);
2468 int stackId = inTask.getLaunchStackId();
2469 if (stackId != inTask.stack.mStackId) {
2470 moveTaskToStackUncheckedLocked(
2471 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2472 }
2473 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002474 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002475 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2476 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002477
2478 // Check whether we should actually launch the new activity in to the task,
2479 // or just reuse the current activity on top.
2480 ActivityRecord top = inTask.getTopActivity();
2481 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2482 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2483 || launchSingleTop || launchSingleTask) {
2484 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2485 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2486 // We don't need to start a new activity, and
2487 // the client said not to do anything if that
2488 // is the case, so this is it!
2489 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2490 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002491 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002492 return ActivityManager.START_DELIVERED_TO_TOP;
2493 }
2494 }
2495
Dianne Hackborn962d5352014-08-29 16:27:40 -07002496 if (!addingToTask) {
2497 // We don't actually want to have this activity added to the task, so just
2498 // stop here but still tell the caller that we consumed the intent.
2499 ActivityOptions.abort(options);
2500 return ActivityManager.START_TASK_TO_FRONT;
2501 }
2502
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002503 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002504 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002505 + " in explicit task " + r.task);
2506
Craig Mautner8849a5e2013-04-02 16:41:03 -07002507 } else {
2508 // This not being started from an existing activity, and not part
2509 // of a new task... just put it in the top task, though these days
2510 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002511 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002512 if (doResume) {
2513 targetStack.moveToFront("addingToTopTask");
2514 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002515 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002516 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002517 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002518 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002519 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002520 + " in new guessed " + r.task);
2521 }
2522
2523 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002524 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002525
Craig Mautner76e2a762014-06-24 09:05:43 -07002526 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2527 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2528 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002529 if (newTask) {
2530 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2531 }
2532 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002533 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002534 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002535 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002536 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002537 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002538 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002539 return ActivityManager.START_SUCCESS;
2540 }
2541
Craig Mautneree36c772014-07-16 14:56:05 -07002542 final void doPendingActivityLaunchesLocked(boolean doResume) {
2543 while (!mPendingActivityLaunches.isEmpty()) {
2544 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Robert Carrf1ddb5e2015-10-20 14:44:45 -07002545
2546 try {
2547 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
2548 doResume && mPendingActivityLaunches.isEmpty(), null, null);
2549 } catch (Exception e) {
2550 Slog.w(TAG, "Exception during pending activity launch pal=" + pal, e);
2551 }
Craig Mautneree36c772014-07-16 14:56:05 -07002552 }
2553 }
2554
Craig Mautner7f13ed32014-07-28 14:00:35 -07002555 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002556 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2557 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002558 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002559 mPendingActivityLaunches.remove(palNdx);
2560 }
2561 }
2562 }
2563
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002564 void setLaunchSource(int uid) {
2565 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2566 }
2567
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002568 void acquireLaunchWakelock() {
2569 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2570 throw new IllegalStateException("Calling must be system uid");
2571 }
2572 mLaunchingActivity.acquire();
2573 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2574 // To be safe, don't allow the wake lock to be held for too long.
2575 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2576 }
2577 }
2578
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002579 /**
2580 * Called when the frontmost task is idle.
2581 * @return the state of mService.mBooting before this was called.
2582 */
2583 private boolean checkFinishBootingLocked() {
2584 final boolean booting = mService.mBooting;
2585 boolean enableScreen = false;
2586 mService.mBooting = false;
2587 if (!mService.mBooted) {
2588 mService.mBooted = true;
2589 enableScreen = true;
2590 }
2591 if (booting || enableScreen) {
2592 mService.postFinishBooting(booting, enableScreen);
2593 }
2594 return booting;
2595 }
2596
Craig Mautnerf3333272013-04-22 10:55:53 -07002597 // Checked.
2598 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2599 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002600 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002601
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 ArrayList<ActivityRecord> stops = null;
2603 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002604 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002605 int NS = 0;
2606 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002607 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002608 boolean activityRemoved = false;
2609
Wale Ogunwale7d701172015-03-11 15:36:30 -07002610 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002611 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002612 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2613 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002614 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2615 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002616 if (fromTimeout) {
2617 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002618 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002619
2620 // This is a hack to semi-deal with a race condition
2621 // in the client where it can be constructed with a
2622 // newer configuration from when we asked it to launch.
2623 // We'll update with whatever configuration it now says
2624 // it used to launch.
2625 if (config != null) {
2626 r.configuration = config;
2627 }
2628
2629 // We are now idle. If someone is waiting for a thumbnail from
2630 // us, we can now deliver.
2631 r.idle = true;
2632
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002633 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002634 if (isFocusedStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002635 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002636 }
2637 }
2638
2639 if (allResumedActivitiesIdle()) {
2640 if (r != null) {
2641 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002642 }
2643
2644 if (mLaunchingActivity.isHeld()) {
2645 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2646 if (VALIDATE_WAKE_LOCK_CALLER &&
2647 Binder.getCallingUid() != Process.myUid()) {
2648 throw new IllegalStateException("Calling must be system uid");
2649 }
2650 mLaunchingActivity.release();
2651 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002652 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002653 }
2654
2655 // Atomically retrieve all of the other things to do.
2656 stops = processStoppingActivitiesLocked(true);
2657 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002658 if ((NF = mFinishingActivities.size()) > 0) {
2659 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002660 mFinishingActivities.clear();
2661 }
2662
Craig Mautnerf3333272013-04-22 10:55:53 -07002663 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002664 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002665 mStartingUsers.clear();
2666 }
2667
Craig Mautnerf3333272013-04-22 10:55:53 -07002668 // Stop any activities that are scheduled to do so but have been
2669 // waiting for the next one to start.
2670 for (int i = 0; i < NS; i++) {
2671 r = stops.get(i);
2672 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002673 if (stack != null) {
2674 if (r.finishing) {
2675 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2676 } else {
2677 stack.stopActivityLocked(r);
2678 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002679 }
2680 }
2681
2682 // Finish any activities that are scheduled to do so but have been
2683 // waiting for the next one to start.
2684 for (int i = 0; i < NF; i++) {
2685 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002686 final ActivityStack stack = r.task.stack;
2687 if (stack != null) {
2688 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2689 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002690 }
2691
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002692 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002693 // Complete user switch
2694 if (startingUsers != null) {
2695 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002696 mService.mUserController.finishUserSwitch(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002697 }
2698 }
2699 // Complete starting up of background users
2700 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002701 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002702 mStartingBackgroundUsers.clear();
2703 for (int i = 0; i < startingUsers.size(); i++) {
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002704 mService.mUserController.finishUserBoot(startingUsers.get(i));
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002705 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002706 }
2707 }
2708
2709 mService.trimApplications();
2710 //dump();
2711 //mWindowManager.dump();
2712
Craig Mautnerf3333272013-04-22 10:55:53 -07002713 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002714 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002715 }
2716
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002717 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002718 }
2719
Craig Mautner8e569572013-10-11 17:36:59 -07002720 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002721 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002722 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2723 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2725 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2726 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002727 }
Craig Mautner19091252013-10-05 00:03:53 -07002728 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002729 }
2730
2731 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002732 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2733 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002734 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2735 stacks.get(stackNdx).closeSystemDialogsLocked();
2736 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002737 }
2738 }
2739
Craig Mautner93529a42013-10-04 15:03:13 -07002740 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002741 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002742 }
2743
Craig Mautner8d341ef2013-03-26 09:03:27 -07002744 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002745 * Update the last used stack id for non-current user (current user's last
2746 * used stack is the focused stack)
2747 */
2748 void updateUserStackLocked(int userId, ActivityStack stack) {
2749 if (userId != mCurrentUser) {
2750 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2751 }
2752 }
2753
2754 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002755 * @return true if some activity was finished (or would have finished if doit were true).
2756 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002757 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2758 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002759 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002760 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2761 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
louis_chang8f0555a2015-10-27 10:45:53 +08002762 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002763 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002764 if (stack.finishDisabledPackageActivitiesLocked(
2765 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002766 didSomething = true;
2767 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002768 }
2769 }
2770 return didSomething;
2771 }
2772
Dianne Hackborna413dc02013-07-12 12:02:55 -07002773 void updatePreviousProcessLocked(ActivityRecord r) {
2774 // Now that this process has stopped, we may want to consider
2775 // it to be the previous app to try to keep around in case
2776 // the user wants to return to it.
2777
2778 // First, found out what is currently the foreground app, so that
2779 // we don't blow away the previous app if this activity is being
2780 // hosted by the process that is actually still the foreground.
2781 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002782 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2783 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002784 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2785 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002786 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002787 if (stack.mResumedActivity != null) {
2788 fgApp = stack.mResumedActivity.app;
2789 } else if (stack.mPausingActivity != null) {
2790 fgApp = stack.mPausingActivity.app;
2791 }
2792 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002793 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002794 }
2795 }
2796
2797 // Now set this one as the previous process, only if that really
2798 // makes sense to.
2799 if (r.app != null && fgApp != null && r.app != fgApp
2800 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002801 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002802 mService.mPreviousProcess = r.app;
2803 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2804 }
2805 }
2806
Craig Mautner05d29032013-05-03 13:40:13 -07002807 boolean resumeTopActivitiesLocked() {
2808 return resumeTopActivitiesLocked(null, null, null);
2809 }
2810
2811 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2812 Bundle targetOptions) {
2813 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002814 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002815 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002816 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002817 boolean result = false;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002818 if (isFocusedStack(targetStack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002819 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2820 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002821
Craig Mautnere0a38842013-12-16 16:14:02 -08002822 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2823 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002824 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2825 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002826 if (stack == targetStack) {
2827 // Already started above.
2828 continue;
2829 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002830 if (isFocusedStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002831 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002832 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002833 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002834 }
Craig Mautner05d29032013-05-03 13:40:13 -07002835 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002836 }
2837
Todd Kennedy539db512014-12-15 09:57:55 -08002838 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002839 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2840 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002841 final int numStacks = stacks.size();
2842 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2843 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002844 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002845 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002846 }
2847 }
2848
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002849 void finishVoiceTask(IVoiceInteractionSession session) {
2850 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2851 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2852 final int numStacks = stacks.size();
2853 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2854 final ActivityStack stack = stacks.get(stackNdx);
2855 stack.finishVoiceTask(session);
2856 }
2857 }
2858 }
2859
Craig Mautner299f9602015-01-26 09:47:33 -08002860 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002861 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2862 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002863 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002864 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2865 // Caller wants the home activity moved with it. To accomplish this,
2866 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002867 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002868 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002869 if (task.stack == null) {
2870 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2871 + task + " to front. Stack is null");
2872 return;
2873 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002874
Chong Zhang0fa656b2015-08-31 15:17:21 -07002875 if (task.mResizeable && options != null) {
2876 ActivityOptions opts = new ActivityOptions(options);
2877 if (opts.hasBounds()) {
2878 Rect bounds = opts.getBounds();
2879 task.updateOverrideConfiguration(bounds);
Chong Zhang112eb8c2015-11-02 11:17:00 -08002880 final int stackId = task.getLaunchStackId();
2881 if (stackId != task.stack.mStackId) {
2882 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, reason);
2883 // moveTaskToStackUncheckedLocked() should already placed the task on top,
2884 // still need moveTaskToFrontLocked() below for any transition settings.
2885 }
2886 // WM resizeTask must be done after the task is moved to the correct stack,
2887 // because Task's setBounds() also updates dim layer's bounds, but that has
2888 // dependency on the stack.
Chong Zhang87b21722015-09-21 15:39:51 -07002889 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig,
2890 false /*relayout*/, false /*forced*/);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002891 }
2892 }
2893
Chong Zhangdb20b5f2015-10-23 14:01:43 -07002894 final ActivityRecord r = task.getTopActivity();
2895 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2896 r == null ? null : r.appTimeTracker, reason);
2897
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002898 if (DEBUG_STACK) Slog.d(TAG_STACK,
2899 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002900 }
2901
Craig Mautner967212c2013-04-13 21:10:58 -07002902 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002903 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002904 }
2905
2906 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002907 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2908 if (activityContainer != null) {
2909 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002910 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07002911 if (!createStaticStackIfNeeded || !StackId.isStaticStack(stackId)) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002912 return null;
2913 }
2914 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002915 }
2916
Craig Mautner967212c2013-04-13 21:10:58 -07002917 ArrayList<ActivityStack> getStacks() {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002918 ArrayList<ActivityStack> allStacks = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002919 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2920 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002921 }
2922 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002923 }
2924
Craig Mautner4a1cb222013-12-04 16:14:06 -08002925 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002926 ActivityRecord homeActivity = getHomeActivity();
2927 if (homeActivity != null) {
2928 return homeActivity.appToken;
2929 }
2930 return null;
2931 }
2932
2933 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002934 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002935 }
2936
2937 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002938 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2939 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2940 final TaskRecord task = tasks.get(taskNdx);
2941 if (task.isHomeTask()) {
2942 final ArrayList<ActivityRecord> activities = task.mActivities;
2943 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2944 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002945 if (r.isHomeActivity()
2946 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002947 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002948 }
2949 }
2950 }
2951 }
2952 return null;
2953 }
2954
Todd Kennedyca4d8422015-01-15 15:19:22 -08002955 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002956 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002957 ActivityContainer activityContainer =
2958 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002959 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002960 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2961 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002962 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002963 return activityContainer;
2964 }
2965
Craig Mautner34b73df2014-01-12 21:11:08 -08002966 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002967 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2968 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2969 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002970 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2971 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002972 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002973 }
2974 }
2975
Craig Mautner95da1082014-02-24 17:54:35 -08002976 void deleteActivityContainer(IActivityContainer container) {
2977 ActivityContainer activityContainer = (ActivityContainer)container;
2978 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002979 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2980 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002981 final int stackId = activityContainer.mStackId;
2982 mActivityContainers.remove(stackId);
2983 mWindowManager.removeStack(stackId);
2984 }
2985 }
2986
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002987 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows,
2988 boolean allowResizeInDockedMode) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002989 final ActivityStack stack = getStack(stackId);
2990 if (stack == null) {
2991 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2992 return;
2993 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002994
Wale Ogunwaleffc11bb2015-10-10 13:05:45 -07002995 if (!allowResizeInDockedMode
2996 && stackId != DOCKED_STACK_ID && getStack(DOCKED_STACK_ID) != null) {
2997 // If the docked stack exist we don't allow resizes of stacks not caused by the docked
2998 // stack size changing so things don't get out of sync.
2999 return;
3000 }
3001
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003002 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeStack_" + stackId);
Jorim Jaggic4025202015-10-22 16:43:34 +02003003 mWindowManager.deferSurfaceLayout();
3004 try {
3005 ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003006
Jorim Jaggic4025202015-10-22 16:43:34 +02003007 mTmpBounds.clear();
3008 mTmpConfigs.clear();
3009 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3010 for (int i = tasks.size() - 1; i >= 0; i--) {
3011 TaskRecord task = tasks.get(i);
3012 if (task.mResizeable) {
3013 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
3014 // For freeform stack we don't adjust the size of the tasks to match that
3015 // of the stack, but we do try to make sure the tasks are still contained
3016 // with the bounds of the stack.
3017 tempRect2.set(task.mBounds);
3018 fitWithinBounds(tempRect2, bounds);
3019 task.updateOverrideConfiguration(tempRect2);
3020 } else {
3021 task.updateOverrideConfiguration(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003022 }
3023 }
3024
Jorim Jaggic4025202015-10-22 16:43:34 +02003025 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
3026 mTmpBounds.put(task.taskId, task.mBounds);
3027 }
3028 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, mTmpConfigs, mTmpBounds);
3029 if (stack.mStackId == DOCKED_STACK_ID) {
3030 // Dock stack funness...Yay!
3031 if (stack.mFullscreen) {
3032 // The dock stack went fullscreen which is kinda like dismissing it.
3033 // In this case we make all other static stacks fullscreen and move all
3034 // docked stack tasks to the fullscreen stack.
3035 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003036 if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003037 resizeStackLocked(i, null, preserveWindows, true);
3038 }
3039 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003040
Jorim Jaggic4025202015-10-22 16:43:34 +02003041 final int count = tasks.size();
3042 for (int i = 0; i < count; i++) {
3043 moveTaskToStackLocked(tasks.get(i).taskId,
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003044 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS, "resizeStack");
Jorim Jaggic4025202015-10-22 16:43:34 +02003045 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003046
Jorim Jaggic4025202015-10-22 16:43:34 +02003047 // stack shouldn't contain anymore activities, so nothing to resume.
3048 r = null;
3049 } else {
3050 // Docked stacks occupy a dedicated region on screen so the size of all other
3051 // static stacks need to be adjusted so they don't overlap with the docked stack.
3052 // We get the bounds to use from window manager which has been adjusted for any
3053 // screen controls and is also the same for all stacks.
3054 mWindowManager.getStackDockedModeBounds(HOME_STACK_ID, tempRect);
3055
3056 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07003057 if (StackId.isResizeableByDockedStack(i)) {
Jorim Jaggic4025202015-10-22 16:43:34 +02003058 ActivityStack otherStack = getStack(i);
3059 if (otherStack != null) {
3060 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS, true);
3061 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003062 }
3063 }
3064 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003065 // Since we are resizing the stack, all other operations should strive to preserve
3066 // windows.
3067 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003068 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003069 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003070
Jorim Jaggic4025202015-10-22 16:43:34 +02003071 if (r != null) {
3072 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
3073 // And we need to make sure at this point that all other activities
3074 // are made visible with the correct configuration.
3075 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
3076 if (!updated) {
3077 resumeTopActivitiesLocked(stack, null, null);
3078 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003079 }
Jorim Jaggic4025202015-10-22 16:43:34 +02003080 } finally {
3081 mWindowManager.continueSurfaceLayout();
3082 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003083 }
3084 }
3085
Chong Zhang6de2ae82015-09-30 18:25:21 -07003086 void resizeTaskLocked(TaskRecord task, Rect bounds, int resizeMode, boolean preserveWindow) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003087 if (!task.mResizeable) {
3088 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3089 return;
3090 }
3091
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003092 adjustForMinimalTaskDimensions(task, bounds);
3093
Chong Zhang87b21722015-09-21 15:39:51 -07003094 // If this is a forced resize, let it go through even if the bounds is not changing,
3095 // as we might need a relayout due to surface size change (to/from fullscreen).
Chong Zhang6de2ae82015-09-30 18:25:21 -07003096 final boolean forced = (resizeMode & RESIZE_MODE_FORCED) != 0;
Chong Zhang87b21722015-09-21 15:39:51 -07003097 if (task.mBounds != null && task.mBounds.equals(bounds) && !forced) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003098 // Nothing to do here...
3099 return;
3100 }
3101
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003102 if (!mWindowManager.isValidTaskId(task.taskId)) {
3103 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003104 // All we can do for now is update the bounds so it can be used when the task is
3105 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003106 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003107 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3108 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003109 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003110 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003111 return;
3112 }
3113
Chong Zhang6de2ae82015-09-30 18:25:21 -07003114 // Do not move the task to another stack here.
3115 // This method assumes that the task is already placed in the right stack.
3116 // we do not mess with that decision and we only do the resize!
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003117
Chong Zhang6de2ae82015-09-30 18:25:21 -07003118 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "am.resizeTask_" + task.taskId);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003119
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003120 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -07003121 // This variable holds information whether the configuration didn't change in a significant
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003122 // way and the activity was kept the way it was. If it's false, it means the activity had
3123 // to be relaunched due to configuration change.
3124 boolean kept = true;
3125 if (overrideConfig != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003126 ActivityRecord r = task.topRunningActivityLocked();
Wale Ogunwale60454db2015-01-23 16:05:07 -08003127 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003128 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003129 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003130 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003131 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003132 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003133 resumeTopActivitiesLocked(stack, null, null);
3134 }
3135 }
3136 }
Chong Zhang87b21722015-09-21 15:39:51 -07003137 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept, forced);
Wale Ogunwalecad05a02015-09-25 10:41:44 -07003138
3139 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003140 }
3141
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003142 private void adjustForMinimalTaskDimensions(TaskRecord task, Rect bounds) {
3143 if (bounds == null) {
3144 return;
3145 }
3146 int minimalSize = task.mMinimalSize == -1 ? mDefaultMinimalSizeOfResizeableTask
3147 : task.mMinimalSize;
3148 final boolean adjustWidth = minimalSize > bounds.width();
3149 final boolean adjustHeight = minimalSize > bounds.height();
3150 if (!(adjustWidth || adjustHeight)) {
3151 return;
3152 }
3153 Rect taskBounds = task.mBounds;
3154 if (adjustWidth) {
3155 if (taskBounds != null && bounds.right == taskBounds.right) {
3156 bounds.left = bounds.right - minimalSize;
3157 } else {
3158 // Either left bounds match, or neither match, or the previous bounds were
3159 // fullscreen and we default to keeping left.
3160 bounds.right = bounds.left + minimalSize;
3161 }
3162 }
3163 if (adjustHeight) {
3164 if (taskBounds != null && bounds.bottom == taskBounds.bottom) {
3165 bounds.top = bounds.bottom - minimalSize;
3166 } else {
3167 // Either top bounds match, or neither match, or the previous bounds were
3168 // fullscreen and we default to keeping top.
3169 bounds.bottom = bounds.top + minimalSize;
3170 }
3171 }
3172 }
3173
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003174 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003175 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3176 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003177 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003178 }
3179
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003180 ActivityContainer activityContainer = new ActivityContainer(stackId);
3181 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003182 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003183 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003184 }
3185
3186 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003187 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003188 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3189 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003190 break;
3191 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003192 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003193 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003194 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003195 }
3196
Chong Zhang5dcb2752015-08-18 13:50:26 -07003197 /**
3198 * Restores a recent task to a stack
3199 * @param task The recent task to be restored.
3200 * @param stackId The stack to restore the task to (default launch stack will be used
Wale Ogunwale3797c222015-10-27 14:21:58 -07003201 * if stackId is {@link android.app.ActivityManager.StackId#INVALID_STACK_ID}).
Chong Zhang5dcb2752015-08-18 13:50:26 -07003202 * @return true if the task has been restored successfully.
3203 */
3204 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3205 if (stackId == INVALID_STACK_ID) {
Wale Ogunwale8b06a582015-10-22 14:38:21 -07003206 stackId = task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003207 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003208 if (task.stack != null) {
3209 // Task has already been restored once. See if we need to do anything more
3210 if (task.stack.mStackId == stackId) {
3211 // Nothing else to do since it is already restored in the right stack.
3212 return true;
3213 }
3214 // Remove current stack association, so we can re-associate the task with the
3215 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003216 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003217 }
3218
3219 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003220 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003221
3222 if (stack == null) {
3223 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003224 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3225 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003226 return false;
3227 }
3228
3229 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003230 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3231 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003232 final ArrayList<ActivityRecord> activities = task.mActivities;
3233 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003234 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003235 }
3236 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003237 }
3238
Wale Ogunwale040b4702015-08-06 18:10:50 -07003239 /**
3240 * Moves the specified task record to the input stack id.
3241 * WARNING: This method performs an unchecked/raw move of the task and
3242 * can leave the system in an unstable state if used incorrectly.
3243 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3244 * to a stack.
3245 * @param task Task to move.
3246 * @param stackId Id of stack to move task to.
3247 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003248 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003249 * @param reason Reason the task is been moved.
3250 * @return The stack the task was moved to.
3251 */
Chong Zhang6de2ae82015-09-30 18:25:21 -07003252 ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003253 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3254 final ActivityRecord r = task.getTopActivity();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003255 final boolean wasFocused = isFocusedStack(task.stack) && (topRunningActivityLocked() == r);
Chong Zhang02898352015-08-21 17:27:14 -07003256 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3257
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003258 final boolean resizeable = task.mResizeable;
3259 // Temporarily disable resizeablility of task we are moving. We don't want it to be resized
3260 // if a docked stack is created below which will lead to the stack we are moving from and
3261 // its resizeable tasks being resized.
3262 task.mResizeable = false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003263 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwaleeb8e56c2015-09-22 20:38:16 -07003264 task.mResizeable = resizeable;
Wale Ogunwale040b4702015-08-06 18:10:50 -07003265 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3266 if (task.stack != null) {
3267 task.stack.removeTask(task, reason, MOVING);
3268 }
3269 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003270
3271 // If the task had focus before (or we're requested to move focus),
3272 // move focus to the new stack.
Wale Ogunwale079a0042015-10-24 11:44:07 -07003273 stack.setFocusAndResumeStateIfNeeded(
3274 r, forceFocus || wasFocused, wasResumed, reason);
Chong Zhang02898352015-08-21 17:27:14 -07003275
Wale Ogunwale040b4702015-08-06 18:10:50 -07003276 return stack;
3277 }
3278
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003279 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus,
3280 String reason) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003281 final TaskRecord task = anyTaskForIdLocked(taskId);
3282 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003283 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003284 return;
3285 }
Wale Ogunwale3797c222015-10-27 14:21:58 -07003286 if (StackId.preserveWindowOnTaskMove(stackId)) {
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003287 // We are about to relaunch the activity because its configuration changed due to
3288 // being maximized, i.e. size change. The activity will first remove the old window
3289 // and then add a new one. This call will tell window manager about this, so it can
3290 // preserve the old window until the new one is drawn. This prevents having a gap
3291 // between the removal and addition, in which no window is visible. We also want the
Wale Ogunwale7e8184b2015-10-05 14:37:03 -07003292 // entrance of the new window to be properly animated.
Filip Gruszczynski49b80af2015-09-24 09:04:26 -07003293 ActivityRecord r = task.getTopActivity();
3294 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3295 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003296 final ActivityStack stack =
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003297 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus,
3298 "moveTaskToStack:" + reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003299
3300 // Make sure the task has the appropriate bounds/size for the stack it is in.
3301 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003302 resizeTaskLocked(task, stack.mBounds,
3303 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003304 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3305 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003306 resizeTaskLocked(task, task.mLastNonFullscreenBounds,
3307 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwale99db1862015-10-23 20:08:22 -07003308 } else if (stackId == DOCKED_STACK_ID || stackId == PINNED_STACK_ID) {
Chong Zhang6de2ae82015-09-30 18:25:21 -07003309 resizeTaskLocked(task, stack.mBounds,
3310 RESIZE_MODE_SYSTEM, !PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003311 }
3312
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003313 // The task might have already been running and its visibility needs to be synchronized with
3314 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003315 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003316 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003317 }
3318
Wale Ogunwale079a0042015-10-24 11:44:07 -07003319 boolean moveTopStackActivityToPinnedStackLocked(int stackId, Rect bounds) {
3320 final ActivityStack stack = getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
3321 if (stack == null) {
3322 throw new IllegalArgumentException(
3323 "moveTopStackActivityToPinnedStackLocked: Unknown stackId=" + stackId);
3324 }
3325
3326 final ActivityRecord r = stack.topRunningActivityLocked();
3327 if (r == null) {
3328 Slog.w(TAG, "moveTopStackActivityToPinnedStackLocked: No top running activity"
3329 + " in stack=" + stack);
3330 return false;
3331 }
3332
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003333 if (!r.info.supportsPip) {
3334 Slog.w(TAG,
3335 "moveTopStackActivityToPinnedStackLocked: Picture-In-Picture not supported for "
Wale Ogunwale079a0042015-10-24 11:44:07 -07003336 + " r=" + r);
3337 return false;
3338 }
3339
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003340 final TaskRecord task = r.task;
Wale Ogunwale079a0042015-10-24 11:44:07 -07003341 if (task.mActivities.size() == 1) {
3342 // There is only one activity in the task. So, we can just move the task over to the
3343 // pinned stack without re-parenting the activity in a different task.
Filip Gruszczynski90186c62015-10-26 14:07:00 -07003344 moveTaskToStackLocked(task.taskId, PINNED_STACK_ID, ON_TOP, FORCE_FOCUS,
3345 "moveTopActivityToPinnedStack");
Wale Ogunwale079a0042015-10-24 11:44:07 -07003346 } else {
3347 final ActivityStack pinnedStack = getStack(PINNED_STACK_ID, CREATE_IF_NEEDED, ON_TOP);
3348 pinnedStack.moveActivityToStack(r);
3349 }
3350
3351 resizeStackLocked(PINNED_STACK_ID, bounds, PRESERVE_WINDOWS, true);
3352
3353 // The task might have already been running and its visibility needs to be synchronized with
3354 // the visibility of the stack / windows.
3355 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
3356 resumeTopActivitiesLocked();
3357 return true;
3358 }
3359
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003360 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3361 final TaskRecord task = anyTaskForIdLocked(taskId);
3362 if (task == null) {
3363 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3364 return;
3365 }
3366 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003367 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003368 mWindowManager.positionTaskInStack(taskId, stackId, position);
3369 final boolean stackChanged = task.stack != null && task.stack != stack;
3370 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003371 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003372 }
3373 stack.positionTask(task, position, stackChanged);
3374 // The task might have already been running and its visibility needs to be synchronized with
3375 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003376 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003377 resumeTopActivitiesLocked();
3378 }
3379
Craig Mautnerac6f8432013-07-17 13:24:59 -07003380 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003381 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003382 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3383 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003384 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3385 final ActivityStack stack = stacks.get(stackNdx);
3386 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003387 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003388 continue;
3389 }
3390 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003391 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3392 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003393 continue;
3394 }
3395 final ActivityRecord ar = stack.findTaskLocked(r);
3396 if (ar != null) {
3397 return ar;
3398 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003399 }
3400 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003401 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003402 return null;
3403 }
3404
3405 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003406 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3407 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003408 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3409 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3410 if (ar != null) {
3411 return ar;
3412 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003413 }
3414 }
3415 return null;
3416 }
3417
Craig Mautner8d341ef2013-03-26 09:03:27 -07003418 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003419 scheduleSleepTimeout();
3420 if (!mGoingToSleep.isHeld()) {
3421 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003422 if (mLaunchingActivity.isHeld()) {
3423 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3424 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003425 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003426 mLaunchingActivity.release();
3427 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003428 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003429 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003430 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003431 }
3432
3433 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003434 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003435
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003436 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003437 final long endTime = System.currentTimeMillis() + timeout;
3438 while (true) {
3439 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003440 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3441 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003442 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3443 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3444 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003445 }
3446 if (cantShutdown) {
3447 long timeRemaining = endTime - System.currentTimeMillis();
3448 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003449 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003450 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003451 } catch (InterruptedException e) {
3452 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003453 } else {
3454 Slog.w(TAG, "Activity manager shutdown timed out");
3455 timedout = true;
3456 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003457 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003458 } else {
3459 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003460 }
3461 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003462
3463 // Force checkReadyForSleep to complete.
3464 mSleepTimeout = true;
3465 checkReadyForSleepLocked();
3466
Craig Mautner8d341ef2013-03-26 09:03:27 -07003467 return timedout;
3468 }
3469
3470 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003471 removeSleepTimeouts();
3472 if (mGoingToSleep.isHeld()) {
3473 mGoingToSleep.release();
3474 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003475 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3476 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003477 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3478 final ActivityStack stack = stacks.get(stackNdx);
3479 stack.awakeFromSleepingLocked();
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003480 if (isFocusedStack(stack)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003481 resumeTopActivitiesLocked();
3482 }
Craig Mautner5314a402013-09-26 12:40:16 -07003483 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003484 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003485 mGoingToSleepActivities.clear();
3486 }
3487
3488 void activitySleptLocked(ActivityRecord r) {
3489 mGoingToSleepActivities.remove(r);
3490 checkReadyForSleepLocked();
3491 }
3492
3493 void checkReadyForSleepLocked() {
3494 if (!mService.isSleepingOrShuttingDown()) {
3495 // Do not care.
3496 return;
3497 }
3498
3499 if (!mSleepTimeout) {
3500 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003501 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3502 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003503 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3504 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3505 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003506 }
3507
3508 if (mStoppingActivities.size() > 0) {
3509 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003510 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003511 + mStoppingActivities.size() + " activities");
3512 scheduleIdleLocked();
3513 dontSleep = true;
3514 }
3515
3516 if (mGoingToSleepActivities.size() > 0) {
3517 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003518 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003519 + mGoingToSleepActivities.size() + " activities");
3520 dontSleep = true;
3521 }
3522
3523 if (dontSleep) {
3524 return;
3525 }
3526 }
3527
Craig Mautnere0a38842013-12-16 16:14:02 -08003528 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3529 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3531 stacks.get(stackNdx).goToSleep();
3532 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003533 }
3534
3535 removeSleepTimeouts();
3536
3537 if (mGoingToSleep.isHeld()) {
3538 mGoingToSleep.release();
3539 }
3540 if (mService.mShuttingDown) {
3541 mService.notifyAll();
3542 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003543 }
3544
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003545 boolean reportResumedActivityLocked(ActivityRecord r) {
3546 final ActivityStack stack = r.task.stack;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003547 if (isFocusedStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003548 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003549 }
3550 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003551 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003552 mWindowManager.executeAppTransition();
3553 return true;
3554 }
3555 return false;
3556 }
3557
Craig Mautner8d341ef2013-03-26 09:03:27 -07003558 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003559 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3560 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003561 int stackNdx = stacks.size() - 1;
3562 while (stackNdx >= 0) {
3563 stacks.get(stackNdx).handleAppCrashLocked(app);
3564 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003565 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003566 }
3567 }
3568
Jose Lima4b6c6692014-08-12 17:41:12 -07003569 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003570 final ActivityStack stack = r.task.stack;
3571 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003572 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3573 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003574 return false;
3575 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003576 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003577 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3578 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003579
3580 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003581 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003582 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003583 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003584 return true;
3585 }
3586
3587 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003588 if (visible && top.fullscreen) {
3589 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003590 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3591 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3592 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3593 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003594 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003595 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3596 // Only the activity set as currently visible behind should actively reset its
3597 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003598 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3599 "requestVisibleBehind: returning visible=" + visible
3600 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3601 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003602 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003603 }
3604
Jose Lima4b6c6692014-08-12 17:41:12 -07003605 stack.setVisibleBehindActivity(visible ? r : null);
3606 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003607 // Make the activity immediately above r opaque.
3608 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3609 if (next != null) {
3610 mService.convertFromTranslucent(next.appToken);
3611 }
3612 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003613 if (top.app != null && top.app.thread != null) {
3614 // Notify the top app of the change.
3615 try {
3616 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3617 } catch (RemoteException e) {
3618 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003619 }
3620 return true;
3621 }
3622
Craig Mautnerbb742462014-07-07 15:28:55 -07003623 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3624 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3625 r.mLaunchTaskBehind = false;
3626 final TaskRecord task = r.task;
3627 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003628 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003629 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003630 mWindowManager.setAppVisibility(r.appToken, false);
3631 }
3632
3633 void scheduleLaunchTaskBehindComplete(IBinder token) {
3634 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3635 }
3636
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003637 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3638 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003639 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003640 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3641 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003642 final int topStackNdx = stacks.size() - 1;
3643 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3644 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003645 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003646 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003647 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003648 }
3649
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003650 void invalidateTaskLayers() {
3651 mTaskLayersChanged = true;
3652 }
3653
3654 void rankTaskLayersIfNeeded() {
3655 if (!mTaskLayersChanged) {
3656 return;
3657 }
3658 mTaskLayersChanged = false;
3659 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); displayNdx++) {
3660 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3661 int baseLayer = 0;
3662 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3663 baseLayer += stacks.get(stackNdx).rankTaskLayers(baseLayer);
3664 }
3665 }
3666 }
3667
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003668 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3669 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3670 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3671 final int topStackNdx = stacks.size() - 1;
3672 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3673 final ActivityStack stack = stacks.get(stackNdx);
3674 stack.clearOtherAppTimeTrackers(except);
3675 }
3676 }
3677 }
3678
Craig Mautner8d341ef2013-03-26 09:03:27 -07003679 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003680 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3681 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003682 final int numStacks = stacks.size();
3683 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3684 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003685 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003686 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003687 }
3688 }
3689
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003690 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3691 // Examine all activities currently running in the process.
3692 TaskRecord firstTask = null;
3693 // Tasks is non-null only if two or more tasks are found.
3694 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003695 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3696 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003697 ActivityRecord r = app.activities.get(i);
3698 // First, if we find an activity that is in the process of being destroyed,
3699 // then we just aren't going to do anything for now; we want things to settle
3700 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003701 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003702 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003703 return;
3704 }
3705 // Don't consider any activies that are currently not in a state where they
3706 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003707 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3708 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003709 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003710 continue;
3711 }
3712 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003713 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003714 + " from " + r);
3715 if (firstTask == null) {
3716 firstTask = r.task;
3717 } else if (firstTask != r.task) {
3718 if (tasks == null) {
3719 tasks = new ArraySet<>();
3720 tasks.add(firstTask);
3721 }
3722 tasks.add(r.task);
3723 }
3724 }
3725 }
3726 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003727 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003728 return;
3729 }
3730 // If we have activities in multiple tasks that are in a position to be destroyed,
3731 // let's iterate through the tasks and release the oldest one.
3732 final int numDisplays = mActivityDisplays.size();
3733 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3734 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3735 // Step through all stacks starting from behind, to hit the oldest things first.
3736 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3737 final ActivityStack stack = stacks.get(stackNdx);
3738 // Try to release activities in this stack; if we manage to, we are done.
3739 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3740 return;
3741 }
3742 }
3743 }
3744 }
3745
Amith Yamasani37a40c22015-06-17 13:25:42 -07003746 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003747 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003748 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003749 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003750
Craig Mautner858d8a62013-04-23 17:08:34 -07003751 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003752 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3753 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003754 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003755 final ActivityStack stack = stacks.get(stackNdx);
3756 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003757 TaskRecord task = stack.topTask();
3758 if (task != null) {
3759 mWindowManager.moveTaskToTop(task.taskId);
3760 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003761 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003762 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003763
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003764 ActivityStack stack = getStack(restoreStackId);
3765 if (stack == null) {
3766 stack = mHomeStack;
3767 }
3768 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003769 if (stack.isOnHomeDisplay()) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07003770 stack.moveToFront("switchUserOnHomeDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003771 } else {
3772 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003773 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003774 }
Craig Mautner93529a42013-10-04 15:03:13 -07003775 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003776 }
3777
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003778 /**
3779 * Add background users to send boot completed events to.
3780 * @param userId The user being started in the background
3781 * @param uss The state object for the user.
3782 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003783 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003784 mStartingBackgroundUsers.add(uss);
3785 }
3786
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003787 /** Checks whether the userid is a profile of the current user. */
3788 boolean isCurrentProfileLocked(int userId) {
3789 if (userId == mCurrentUser) return true;
Fyodor Kupolov610acda2015-10-19 18:44:07 -07003790 return mService.mUserController.isCurrentProfileLocked(userId);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003791 }
3792
Chong Zhang45c25ce2015-08-10 22:18:26 -07003793 /** Checks whether the activity should be shown for current user. */
3794 boolean okToShowLocked(ActivityRecord r) {
3795 return r != null && (isCurrentProfileLocked(r.userId)
3796 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3797 }
3798
Craig Mautnerde4ef022013-04-07 19:01:33 -07003799 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003800 ArrayList<ActivityRecord> stops = null;
3801
3802 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003803 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3804 ActivityRecord s = mStoppingActivities.get(activityNdx);
3805 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003806 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003807 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3808 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003809 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003810 if (s.finishing) {
3811 // If this activity is finishing, it is sitting on top of
3812 // everyone else but we now know it is no longer needed...
3813 // so get rid of it. Otherwise, we need to go through the
3814 // normal flow and hide it once we determine that it is
3815 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003816 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003817 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003818 }
3819 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003820 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003821 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003822 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003823 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003824 }
3825 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003826 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003827 }
3828 }
3829
3830 return stops;
3831 }
3832
Craig Mautnercf910b02013-04-23 11:23:27 -07003833 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003834 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3835 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3836 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3837 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003838 final ActivityRecord r = stack.topRunningActivityLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003839 final ActivityState state = r == null ? DESTROYED : r.state;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003840 if (isFocusedStack(stack)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003841 if (r == null) Slog.e(TAG,
3842 "validateTop...: null top activity, stack=" + stack);
3843 else {
3844 final ActivityRecord pausing = stack.mPausingActivity;
3845 if (pausing != null && pausing == r) Slog.e(TAG,
3846 "validateTop...: top stack has pausing activity r=" + r
3847 + " state=" + state);
3848 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3849 "validateTop...: activity in front not resumed r=" + r
3850 + " state=" + state);
3851 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003852 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003853 final ActivityRecord resumed = stack.mResumedActivity;
3854 if (resumed != null && resumed == r) Slog.e(TAG,
3855 "validateTop...: back stack has resumed activity r=" + r
3856 + " state=" + state);
3857 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3858 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003859 }
3860 }
3861 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003862 }
3863
Craig Mautnere0570202015-05-13 13:06:11 -07003864 private String lockTaskModeToString() {
3865 switch (mLockTaskModeState) {
3866 case LOCK_TASK_MODE_LOCKED:
3867 return "LOCKED";
3868 case LOCK_TASK_MODE_PINNED:
3869 return "PINNED";
3870 case LOCK_TASK_MODE_NONE:
3871 return "NONE";
3872 default: return "unknown=" + mLockTaskModeState;
3873 }
3874 }
3875
Craig Mautner27084302013-03-25 08:05:25 -07003876 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003877 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003878 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003879 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3880 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3881 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003882 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003883 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3884 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3885 if (packages.size() > 0) {
3886 pw.println(" mLockTaskPackages (userId:packages)=");
3887 for (int i = 0; i < packages.size(); ++i) {
3888 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3889 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3890 }
3891 }
3892 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003893 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003894
Craig Mautner20e72272013-04-01 13:45:53 -07003895 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003896 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003897 }
3898
Dianne Hackborn390517b2013-05-30 15:03:32 -07003899 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3900 boolean needSep, String prefix) {
3901 if (activity != null) {
3902 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3903 if (needSep) {
3904 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003905 }
3906 pw.print(prefix);
3907 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003908 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003909 }
3910 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003911 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003912 }
3913
Craig Mautner8d341ef2013-03-26 09:03:27 -07003914 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3915 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003916 boolean printed = false;
3917 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003918 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3919 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003920 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003921 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003922 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003923 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 final ActivityStack stack = stacks.get(stackNdx);
3925 StringBuilder stackHeader = new StringBuilder(128);
3926 stackHeader.append(" Stack #");
3927 stackHeader.append(stack.mStackId);
3928 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003929 stackHeader.append("\n");
3930 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3931 stackHeader.append("\n");
3932 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003933 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3934 needSep, stackHeader.toString());
3935 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3936 !dumpAll, false, dumpPackage, true,
3937 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003938
Craig Mautner4a1cb222013-12-04 16:14:06 -08003939 needSep = printed;
3940 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3941 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003942 if (pr) {
3943 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003944 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003945 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003946 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3947 " mResumedActivity: ");
3948 if (pr) {
3949 printed = true;
3950 needSep = false;
3951 }
3952 if (dumpAll) {
3953 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3954 " mLastPausedActivity: ");
3955 if (pr) {
3956 printed = true;
3957 needSep = true;
3958 }
3959 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3960 needSep, " mLastNoHistoryActivity: ");
3961 }
3962 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003963 }
3964 }
3965
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003966 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3967 false, dumpPackage, true, " Activities waiting to finish:", null);
3968 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3969 false, dumpPackage, true, " Activities waiting to stop:", null);
3970 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3971 false, dumpPackage, true, " Activities waiting for another to become visible:",
3972 null);
3973 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3974 false, dumpPackage, true, " Activities waiting to sleep:", null);
3975 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3976 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003977
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003978 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003979 }
3980
Dianne Hackborn390517b2013-05-30 15:03:32 -07003981 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003982 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003983 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003984 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003985 String innerPrefix = null;
3986 String[] args = null;
3987 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003988 for (int i=list.size()-1; i>=0; i--) {
3989 final ActivityRecord r = list.get(i);
3990 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3991 continue;
3992 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003993 if (innerPrefix == null) {
3994 innerPrefix = prefix + " ";
3995 args = new String[0];
3996 }
3997 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003998 final boolean full = !brief && (complete || !r.isInHistory());
3999 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004000 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07004001 needNL = false;
4002 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004003 if (header1 != null) {
4004 pw.println(header1);
4005 header1 = null;
4006 }
4007 if (header2 != null) {
4008 pw.println(header2);
4009 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07004010 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07004011 if (lastTask != r.task) {
4012 lastTask = r.task;
4013 pw.print(prefix);
4014 pw.print(full ? "* " : " ");
4015 pw.println(lastTask);
4016 if (full) {
4017 lastTask.dump(pw, prefix + " ");
4018 } else if (complete) {
4019 // Complete + brief == give a summary. Isn't that obvious?!?
4020 if (lastTask.intent != null) {
4021 pw.print(prefix); pw.print(" ");
4022 pw.println(lastTask.intent.toInsecureStringWithClip());
4023 }
4024 }
4025 }
4026 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
4027 pw.print(" #"); pw.print(i); pw.print(": ");
4028 pw.println(r);
4029 if (full) {
4030 r.dump(pw, innerPrefix);
4031 } else if (complete) {
4032 // Complete + brief == give a summary. Isn't that obvious?!?
4033 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
4034 if (r.app != null) {
4035 pw.print(innerPrefix); pw.println(r.app);
4036 }
4037 }
4038 if (client && r.app != null && r.app.thread != null) {
4039 // flush anything that is already in the PrintWriter since the thread is going
4040 // to write to the file descriptor directly
4041 pw.flush();
4042 try {
4043 TransferPipe tp = new TransferPipe();
4044 try {
4045 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
4046 r.appToken, innerPrefix, args);
4047 // Short timeout, since blocking here can
4048 // deadlock with the application.
4049 tp.go(fd, 2000);
4050 } finally {
4051 tp.kill();
4052 }
4053 } catch (IOException e) {
4054 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
4055 } catch (RemoteException e) {
4056 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
4057 }
4058 needNL = true;
4059 }
4060 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004061 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07004062 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004063
Craig Mautnerf3333272013-04-22 10:55:53 -07004064 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004065 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4066 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07004067 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
4068 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07004069 }
4070
4071 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07004072 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07004073 }
4074
4075 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004076 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
4077 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07004078 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4079 }
4080
Craig Mautner05d29032013-05-03 13:40:13 -07004081 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08004082 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
4083 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
4084 }
Craig Mautner05d29032013-05-03 13:40:13 -07004085 }
4086
Craig Mautner0eea92c2013-05-16 13:35:39 -07004087 void removeSleepTimeouts() {
4088 mSleepTimeout = false;
4089 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
4090 }
4091
4092 final void scheduleSleepTimeout() {
4093 removeSleepTimeouts();
4094 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
4095 }
4096
Craig Mautner4a1cb222013-12-04 16:14:06 -08004097 @Override
4098 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004099 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004100 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
4101 }
4102
4103 @Override
4104 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004105 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004106 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4107 }
4108
4109 @Override
4110 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004111 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004112 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4113 }
4114
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004115 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004116 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004117 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004118 newDisplay = mActivityDisplays.get(displayId) == null;
4119 if (newDisplay) {
4120 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004121 if (activityDisplay.mDisplay == null) {
4122 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4123 return;
4124 }
Craig Mautner4504de52013-12-20 09:06:56 -08004125 mActivityDisplays.put(displayId, activityDisplay);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004126 calculateDefaultMinimalSizeOfResizeableTasks(activityDisplay);
Craig Mautner4504de52013-12-20 09:06:56 -08004127 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004128 }
Craig Mautner4504de52013-12-20 09:06:56 -08004129 if (newDisplay) {
4130 mWindowManager.onDisplayAdded(displayId);
4131 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004132 }
4133
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004134 private void calculateDefaultMinimalSizeOfResizeableTasks(ActivityDisplay display) {
4135 if (display.mDisplayId != Display.DEFAULT_DISPLAY) {
4136 return;
4137 }
4138 final float fraction = mService.mContext.getResources().getFraction(com.android.internal.R.
4139 fraction.config_displayFractionForDefaultMinimalSizeOfResizeableTask, 1, 1);
4140 mDefaultMinimalSizeOfResizeableTask = (int) (fraction * Math.min(
4141 display.mDisplayInfo.logicalWidth, display.mDisplayInfo.logicalHeight));
4142 }
4143
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004144 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004145 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004146 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4147 if (activityDisplay != null) {
4148 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004149 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004150 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004151 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004152 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004153 }
4154 }
4155 mWindowManager.onDisplayRemoved(displayId);
4156 }
4157
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004158 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004159 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004160 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4161 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004162 // TODO: Update the bounds.
4163 }
4164 }
4165 mWindowManager.onDisplayChanged(displayId);
4166 }
4167
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004168 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004169 StackInfo info = new StackInfo();
4170 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4171 info.displayId = Display.DEFAULT_DISPLAY;
4172 info.stackId = stack.mStackId;
4173
4174 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4175 final int numTasks = tasks.size();
4176 int[] taskIds = new int[numTasks];
4177 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004178 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004179 for (int i = 0; i < numTasks; ++i) {
4180 final TaskRecord task = tasks.get(i);
4181 taskIds[i] = task.taskId;
4182 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4183 : task.realActivity != null ? task.realActivity.flattenToString()
4184 : task.getTopActivity() != null ? task.getTopActivity().packageName
4185 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004186 taskBounds[i] = new Rect();
4187 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004188 }
4189 info.taskIds = taskIds;
4190 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004191 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004192 return info;
4193 }
4194
4195 StackInfo getStackInfoLocked(int stackId) {
4196 ActivityStack stack = getStack(stackId);
4197 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004198 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004199 }
4200 return null;
4201 }
4202
4203 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004204 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004205 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4206 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004207 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004208 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004209 }
4210 }
4211 return list;
4212 }
4213
Craig Mautner15df08a2015-04-01 12:17:18 -07004214 TaskRecord getLockedTaskLocked() {
4215 final int top = mLockTaskModeTasks.size() - 1;
4216 if (top >= 0) {
4217 return mLockTaskModeTasks.get(top);
4218 }
4219 return null;
4220 }
4221
4222 boolean isLockedTask(TaskRecord task) {
4223 return mLockTaskModeTasks.contains(task);
4224 }
4225
4226 boolean isLastLockedTask(TaskRecord task) {
4227 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4228 }
4229
4230 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004231 if (!mLockTaskModeTasks.remove(task)) {
4232 return;
4233 }
4234 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4235 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004236 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004237 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4238 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004239 final Message lockTaskMsg = Message.obtain();
4240 lockTaskMsg.arg1 = task.userId;
4241 lockTaskMsg.what = LOCK_TASK_END_MSG;
4242 mHandler.sendMessage(lockTaskMsg);
4243 }
4244 }
4245
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004246 void showLockTaskToast() {
4247 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004248 }
4249
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004250 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4251 if (mLockTaskModeTasks.contains(task)) {
4252 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4253 }
4254 }
4255
Craig Mautner432f64e2015-05-20 14:59:57 -07004256 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4257 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004258 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004259 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004260 final TaskRecord lockedTask = getLockedTaskLocked();
4261 if (lockedTask != null) {
4262 removeLockedTaskLocked(lockedTask);
4263 if (!mLockTaskModeTasks.isEmpty()) {
4264 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004265 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4266 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004267 lockedTask.performClearTaskLocked();
4268 resumeTopActivitiesLocked();
4269 return;
4270 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004271 }
Craig Mautnere0570202015-05-13 13:06:11 -07004272 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4273 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004274 return;
4275 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004276
4277 // Should have already been checked, but do it again.
4278 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004279 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4280 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004281 return;
4282 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004283 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004284 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004285 return;
4286 }
4287
4288 if (mLockTaskModeTasks.isEmpty()) {
4289 // First locktask.
4290 final Message lockTaskMsg = Message.obtain();
4291 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4292 lockTaskMsg.arg1 = task.userId;
4293 lockTaskMsg.what = LOCK_TASK_START_MSG;
4294 lockTaskMsg.arg2 = lockTaskModeState;
4295 mHandler.sendMessage(lockTaskMsg);
4296 }
4297 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004298 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4299 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004300 mLockTaskModeTasks.remove(task);
4301 mLockTaskModeTasks.add(task);
4302
4303 if (task.mLockTaskUid == -1) {
Wale Ogunwale5c18d052015-10-12 10:34:14 -07004304 task.mLockTaskUid = task.effectiveUid;
Craig Mautner15df08a2015-04-01 12:17:18 -07004305 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004306
4307 if (andResume) {
4308 findTaskToMoveToFrontLocked(task, 0, null, reason);
4309 resumeTopActivitiesLocked();
4310 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004311 }
4312
4313 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004314 return isLockTaskModeViolation(task, false);
4315 }
4316
4317 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4318 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004319 return false;
4320 }
4321 final int lockTaskAuth = task.mLockTaskAuth;
4322 switch (lockTaskAuth) {
4323 case LOCK_TASK_AUTH_DONT_LOCK:
4324 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004325 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004326 case LOCK_TASK_AUTH_LAUNCHABLE:
4327 case LOCK_TASK_AUTH_WHITELISTED:
4328 return false;
4329 case LOCK_TASK_AUTH_PINNABLE:
4330 // Pinnable tasks can't be launched on top of locktask tasks.
4331 return !mLockTaskModeTasks.isEmpty();
4332 default:
4333 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4334 return true;
4335 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004336 }
4337
Craig Mautner15df08a2015-04-01 12:17:18 -07004338 void onLockTaskPackagesUpdatedLocked() {
4339 boolean didSomething = false;
4340 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4341 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004342 final boolean wasWhitelisted =
4343 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4344 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004345 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004346 final boolean isWhitelisted =
4347 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4348 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4349 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004350 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004351 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4352 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004353 removeLockedTaskLocked(lockedTask);
4354 lockedTask.performClearTaskLocked();
4355 didSomething = true;
4356 }
4357 }
4358 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4359 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4360 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4361 final ActivityStack stack = stacks.get(stackNdx);
4362 stack.onLockTaskPackagesUpdatedLocked();
4363 }
4364 }
Craig Mautnere0570202015-05-13 13:06:11 -07004365 final ActivityRecord r = topRunningActivityLocked();
4366 final TaskRecord task = r != null ? r.task : null;
4367 if (mLockTaskModeTasks.isEmpty() && task != null
4368 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4369 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004370 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4371 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4372 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4373 false);
4374 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004375 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004376 if (didSomething) {
4377 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004378 }
4379 }
4380
Benjamin Franz43261142015-02-11 15:59:44 +00004381 int getLockTaskModeState() {
4382 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004383 }
4384
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004385 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004386
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004387 public ActivityStackSupervisorHandler(Looper looper) {
4388 super(looper);
4389 }
4390
Craig Mautnerf3333272013-04-22 10:55:53 -07004391 void activityIdleInternal(ActivityRecord r) {
4392 synchronized (mService) {
4393 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4394 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004395 }
4396
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004397 @Override
4398 public void handleMessage(Message msg) {
4399 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004400 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004401 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4402 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004403 if (mService.mDidDexOpt) {
4404 mService.mDidDexOpt = false;
4405 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4406 nmsg.obj = msg.obj;
4407 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4408 return;
4409 }
4410 // We don't at this point know if the activity is fullscreen,
4411 // so we need to be conservative and assume it isn't.
4412 activityIdleInternal((ActivityRecord)msg.obj);
4413 } break;
4414 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004415 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004416 activityIdleInternal((ActivityRecord)msg.obj);
4417 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004418 case RESUME_TOP_ACTIVITY_MSG: {
4419 synchronized (mService) {
4420 resumeTopActivitiesLocked();
4421 }
4422 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004423 case SLEEP_TIMEOUT_MSG: {
4424 synchronized (mService) {
4425 if (mService.isSleepingOrShuttingDown()) {
4426 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4427 mSleepTimeout = true;
4428 checkReadyForSleepLocked();
4429 }
4430 }
4431 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004432 case LAUNCH_TIMEOUT_MSG: {
4433 if (mService.mDidDexOpt) {
4434 mService.mDidDexOpt = false;
4435 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4436 return;
4437 }
4438 synchronized (mService) {
4439 if (mLaunchingActivity.isHeld()) {
4440 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4441 if (VALIDATE_WAKE_LOCK_CALLER
4442 && Binder.getCallingUid() != Process.myUid()) {
4443 throw new IllegalStateException("Calling must be system uid");
4444 }
4445 mLaunchingActivity.release();
4446 }
4447 }
4448 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004449 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004450 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004451 } break;
4452 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004453 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004454 } break;
4455 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004456 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004457 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004458 case CONTAINER_CALLBACK_VISIBILITY: {
4459 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004460 final IActivityContainerCallback callback = container.mCallback;
4461 if (callback != null) {
4462 try {
4463 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4464 } catch (RemoteException e) {
4465 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004466 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004467 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004468 case LOCK_TASK_START_MSG: {
4469 // When lock task starts, we disable the status bars.
4470 try {
Jason Monk62515be2014-05-21 16:06:19 -04004471 if (mLockTaskNotify == null) {
4472 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004473 }
Jason Monk62515be2014-05-21 16:06:19 -04004474 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004475 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004476 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004477 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004478 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004479 flags = StatusBarManager.DISABLE_MASK
4480 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004481 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004482 flags = StatusBarManager.DISABLE_MASK
4483 & (~StatusBarManager.DISABLE_BACK)
4484 & (~StatusBarManager.DISABLE_HOME)
4485 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004486 }
4487 getStatusBarService().disable(flags, mToken,
4488 mService.mContext.getPackageName());
4489 }
4490 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004491 if (getDevicePolicyManager() != null) {
4492 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004493 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004494 }
justinzhang5286d3f2014-05-12 17:06:01 -04004495 } catch (RemoteException ex) {
4496 throw new RuntimeException(ex);
4497 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004498 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004499 case LOCK_TASK_END_MSG: {
4500 // When lock task ends, we enable the status bars.
4501 try {
Jason Monk62515be2014-05-21 16:06:19 -04004502 if (getStatusBarService() != null) {
4503 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4504 mService.mContext.getPackageName());
4505 }
4506 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004507 if (getDevicePolicyManager() != null) {
4508 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4509 msg.arg1);
4510 }
Jason Monk62515be2014-05-21 16:06:19 -04004511 if (mLockTaskNotify == null) {
4512 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4513 }
4514 mLockTaskNotify.show(false);
4515 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004516 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004517 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004518 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004519 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004520 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004521 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004522 new LockPatternUtils(mService.mContext)
4523 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004524 }
4525 } catch (SettingNotFoundException e) {
4526 // No setting, don't lock.
4527 }
justinzhang5286d3f2014-05-12 17:06:01 -04004528 } catch (RemoteException ex) {
4529 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004530 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004531 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004532 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004533 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004534 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4535 if (mLockTaskNotify == null) {
4536 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4537 }
4538 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4539 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004540 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4541 final ActivityContainer container = (ActivityContainer) msg.obj;
4542 final IActivityContainerCallback callback = container.mCallback;
4543 if (callback != null) {
4544 try {
4545 callback.onAllActivitiesComplete(container.asBinder());
4546 } catch (RemoteException e) {
4547 }
4548 }
4549 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004550 case LAUNCH_TASK_BEHIND_COMPLETE: {
4551 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004552 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004553 if (r != null) {
4554 handleLaunchTaskBehindCompleteLocked(r);
4555 }
4556 }
4557 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004558 }
4559 }
4560 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004561
Ying Wangb081a592014-04-22 15:20:16 -07004562 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004563 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004564 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004565 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004566 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004567 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004568 ActivityRecord mParentActivity = null;
4569 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004570
Craig Mautnere3a00d72014-04-16 08:31:19 -07004571 boolean mVisible = true;
4572
Craig Mautner4a1cb222013-12-04 16:14:06 -08004573 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004574 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004575
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004576 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4577 final static int CONTAINER_STATE_NO_SURFACE = 1;
4578 final static int CONTAINER_STATE_FINISHING = 2;
4579 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4580
4581 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004582 synchronized (mService) {
4583 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004584 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004585 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004586 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004587 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004588 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004589
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004590 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004591 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004592 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004593 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004594 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004595 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004596 }
4597
4598 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004599 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004600 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004601 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4602 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004603 return;
4604 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004605 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004606 }
4607 }
4608
4609 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004610 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004611 synchronized (mService) {
4612 if (mActivityDisplay != null) {
4613 return mActivityDisplay.mDisplayId;
4614 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004615 }
4616 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004617 }
4618
Jeff Brownca9bc702014-02-11 14:32:56 -08004619 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004620 public int getStackId() {
4621 synchronized (mService) {
4622 return mStackId;
4623 }
4624 }
4625
4626 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004627 public boolean injectEvent(InputEvent event) {
4628 final long origId = Binder.clearCallingIdentity();
4629 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004630 synchronized (mService) {
4631 if (mActivityDisplay != null) {
4632 return mInputManagerInternal.injectInputEvent(event,
4633 mActivityDisplay.mDisplayId,
4634 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4635 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004636 }
4637 return false;
4638 } finally {
4639 Binder.restoreCallingIdentity(origId);
4640 }
4641 }
4642
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004643 @Override
4644 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004645 synchronized (mService) {
4646 if (mContainerState == CONTAINER_STATE_FINISHING) {
4647 return;
4648 }
4649 mContainerState = CONTAINER_STATE_FINISHING;
4650
Craig Mautnerd163e752014-06-13 17:18:47 -07004651 long origId = Binder.clearCallingIdentity();
4652 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004653 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004654 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004655 } finally {
4656 Binder.restoreCallingIdentity(origId);
4657 }
4658 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004659 }
4660
Craig Mautner60257702014-09-17 15:02:33 -07004661 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004662 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004663 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004664 if (mActivityDisplay != null) {
4665 mActivityDisplay.detachActivitiesLocked(mStack);
4666 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004667 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004668 }
4669 }
4670
4671 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004672 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004673 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004674 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004675 Binder.getCallingUid(), mCurrentUser, false,
4676 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004677
Craig Mautnere0a38842013-12-16 16:14:02 -08004678 // TODO: Switch to user app stacks here.
4679 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004680 final Uri data = intent.getData();
4681 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4682 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004683 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004684 checkEmbeddedAllowedInner(userId, intent, mimeType);
4685
4686 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4687 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004688 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004689 }
4690
4691 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004692 public final int startActivityIntentSender(IIntentSender intentSender)
4693 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004694 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4695
4696 if (!(intentSender instanceof PendingIntentRecord)) {
4697 throw new IllegalArgumentException("Bad PendingIntent object");
4698 }
4699
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -07004700 final int userId = mService.mUserController.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004701 Binder.getCallingUid(), mCurrentUser, false,
4702 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004703
4704 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4705 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4706 pendingIntent.key.requestResolvedType);
4707
4708 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4709 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4710 }
4711
4712 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004713 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004714 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004715 throw new SecurityException(
4716 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4717 }
4718 }
4719
Craig Mautnerdf88d732014-01-27 09:21:32 -08004720 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004721 public IBinder asBinder() {
4722 return this;
4723 }
4724
Craig Mautner4504de52013-12-20 09:06:56 -08004725 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004726 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004727 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004728 }
4729
Craig Mautner4a1cb222013-12-04 16:14:06 -08004730 ActivityStackSupervisor getOuter() {
4731 return ActivityStackSupervisor.this;
4732 }
4733
Craig Mautnerd163e752014-06-13 17:18:47 -07004734 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004735 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004736 }
4737
Craig Mautner6985bad2014-04-21 15:22:06 -07004738 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004739 void setVisible(boolean visible) {
4740 if (mVisible != visible) {
4741 mVisible = visible;
4742 if (mCallback != null) {
4743 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4744 0 /* unused */, this).sendToTarget();
4745 }
4746 }
4747 }
4748
Craig Mautner6985bad2014-04-21 15:22:06 -07004749 void setDrawn() {
4750 }
4751
Craig Mautner1b4bf852014-05-26 15:06:32 -07004752 // You can always start a new task on a regular ActivityStack.
4753 boolean isEligibleForNewTasks() {
4754 return true;
4755 }
4756
Craig Mautnerd163e752014-06-13 17:18:47 -07004757 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004758 detachLocked();
4759 deleteActivityContainer(this);
4760 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004761 }
4762
Craig Mautner34b73df2014-01-12 21:11:08 -08004763 @Override
4764 public String toString() {
4765 return mIdString + (mActivityDisplay == null ? "N" : "A");
4766 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004767 }
4768
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004769 private class VirtualActivityContainer extends ActivityContainer {
4770 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004771 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004772
4773 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4774 super(getNextStackId());
4775 mParentActivity = parent;
4776 mCallback = callback;
4777 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004778 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004779 }
4780
4781 @Override
4782 public void setSurface(Surface surface, int width, int height, int density) {
4783 super.setSurface(surface, width, height, density);
4784
4785 synchronized (mService) {
4786 final long origId = Binder.clearCallingIdentity();
4787 try {
4788 setSurfaceLocked(surface, width, height, density);
4789 } finally {
4790 Binder.restoreCallingIdentity(origId);
4791 }
4792 }
4793 }
4794
4795 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4796 if (mContainerState == CONTAINER_STATE_FINISHING) {
4797 return;
4798 }
4799 VirtualActivityDisplay virtualActivityDisplay =
4800 (VirtualActivityDisplay) mActivityDisplay;
4801 if (virtualActivityDisplay == null) {
4802 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004803 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004804 mActivityDisplay = virtualActivityDisplay;
4805 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004806 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004807 }
4808
4809 if (mSurface != null) {
4810 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004811 }
4812
Craig Mautner6985bad2014-04-21 15:22:06 -07004813 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004814 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004815 mStack.resumeTopActivityLocked(null);
4816 } else {
4817 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004818 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004819 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004820 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004821 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004822 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004823
Craig Mautnerd163e752014-06-13 17:18:47 -07004824 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004825
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004826 if (DEBUG_STACK) Slog.d(TAG_STACK,
4827 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004828 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004829
Craig Mautner6985bad2014-04-21 15:22:06 -07004830 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004831 boolean isAttachedLocked() {
4832 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004833 }
4834
4835 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004836 void setDrawn() {
4837 synchronized (mService) {
4838 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004839 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004840 }
4841 }
4842
Craig Mautner1b4bf852014-05-26 15:06:32 -07004843 // Never start a new task on an ActivityView if it isn't explicitly specified.
4844 @Override
4845 boolean isEligibleForNewTasks() {
4846 return false;
4847 }
4848
Craig Mautnerd163e752014-06-13 17:18:47 -07004849 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004850 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004851 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4852 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4853 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4854 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4855 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004856 }
4857 }
4858
Craig Mautner4a1cb222013-12-04 16:14:06 -08004859 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4860 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004861 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004862 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004863 int mDisplayId;
4864 Display mDisplay;
4865 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004866
Craig Mautner4a1cb222013-12-04 16:14:06 -08004867 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4868 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004869 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004870
Jose Lima4b6c6692014-08-12 17:41:12 -07004871 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004872
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004873 ActivityDisplay() {
4874 }
Craig Mautner4504de52013-12-20 09:06:56 -08004875
Craig Mautner1a70a162014-09-13 12:09:31 -07004876 // After instantiation, check that mDisplay is not null before using this. The alternative
4877 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004878 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004879 final Display display = mDisplayManager.getDisplay(displayId);
4880 if (display == null) {
4881 return;
4882 }
4883 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004884 }
4885
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004886 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004887 mDisplay = display;
4888 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004889 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004890 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004891
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004892 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004893 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004894 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4895 + " onTop=" + onTop);
4896 if (onTop) {
4897 mStacks.add(stack);
4898 } else {
4899 mStacks.add(0, stack);
4900 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004901 }
4902
Craig Mautnere0a38842013-12-16 16:14:02 -08004903 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004904 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004905 + " from displayId=" + mDisplayId);
4906 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004907 }
4908
Jose Lima4b6c6692014-08-12 17:41:12 -07004909 void setVisibleBehindActivity(ActivityRecord r) {
4910 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004911 }
4912
Jose Lima4b6c6692014-08-12 17:41:12 -07004913 boolean hasVisibleBehindActivity() {
4914 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004915 }
4916
Craig Mautner34b73df2014-01-12 21:11:08 -08004917 @Override
4918 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004919 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4920 }
4921 }
4922
4923 class VirtualActivityDisplay extends ActivityDisplay {
4924 VirtualDisplay mVirtualDisplay;
4925
Craig Mautner6985bad2014-04-21 15:22:06 -07004926 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004927 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004928 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4929 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4930 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4931 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004932
4933 init(mVirtualDisplay.getDisplay());
4934
4935 mWindowManager.handleDisplayAdded(mDisplayId);
4936 }
4937
4938 void setSurface(Surface surface) {
4939 if (mVirtualDisplay != null) {
4940 mVirtualDisplay.setSurface(surface);
4941 }
4942 }
4943
4944 @Override
4945 void detachActivitiesLocked(ActivityStack stack) {
4946 super.detachActivitiesLocked(stack);
4947 if (mVirtualDisplay != null) {
4948 mVirtualDisplay.release();
4949 mVirtualDisplay = null;
4950 }
4951 }
4952
4953 @Override
4954 public String toString() {
4955 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004956 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004957 }
Jose Lima58e66d62014-05-27 20:00:27 -07004958
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004959 /**
4960 * Adjust bounds to stay within stack bounds.
4961 *
4962 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4963 * that keep them unchanged, but be contained within the stack bounds.
4964 *
4965 * @param bounds Bounds to be adjusted.
4966 * @param stackBounds Bounds within which the other bounds should remain.
4967 */
4968 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4969 if (stackBounds == null || stackBounds.contains(bounds)) {
4970 return;
4971 }
4972
4973 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4974 final int maxRight = stackBounds.right
4975 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4976 int horizontalDiff = stackBounds.left - bounds.left;
4977 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4978 || (bounds.left + horizontalDiff >= maxRight)) {
4979 horizontalDiff = maxRight - bounds.left;
4980 }
4981 bounds.left += horizontalDiff;
4982 bounds.right += horizontalDiff;
4983 }
4984
4985 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4986 final int maxBottom = stackBounds.bottom
4987 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4988 int verticalDiff = stackBounds.top - bounds.top;
4989 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4990 || (bounds.top + verticalDiff >= maxBottom)) {
4991 verticalDiff = maxBottom - bounds.top;
4992 }
4993 bounds.top += verticalDiff;
4994 bounds.bottom += verticalDiff;
4995 }
4996 }
4997
Craig Mautner27084302013-03-25 08:05:25 -07004998}