blob: 9c227335f23666b586e64b53ba026983dd86cdcb [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Wale Ogunwale868a5e12015-08-02 16:19:20 -070020import static android.app.ActivityManager.*;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070021import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
22import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070023import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070026import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080027import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070028import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070029import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
30import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
31import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070032import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070033import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
34import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010035import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070036import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
37import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070038
Svetoslav7008b512015-06-24 18:47:07 -070039import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070040import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080042import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.ActivityOptions;
44import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070045import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080046import android.app.IActivityContainer;
47import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070048import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070051import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070052import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070054import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040055import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040056import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070060import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070062import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070063import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070064import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.content.pm.PackageManager;
66import android.content.pm.ResolveInfo;
67import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080068import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080069import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080070import android.hardware.display.DisplayManager;
71import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.hardware.display.DisplayManagerGlobal;
73import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080074import android.hardware.input.InputManager;
75import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080076import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070079import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070080import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070081import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070082import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070084import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070085import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070086import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070087import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040088import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070090import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070091import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -070092import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -070093import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -040094import android.provider.Settings;
95import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070096import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -070097import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070098import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070099import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700100import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800101import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700102
Craig Mautner4a1cb222013-12-04 16:14:06 -0800103import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800104import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800105import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800106import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800107import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700108import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700109import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800110import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700111import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400112import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700113import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400114import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800115import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700116import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700117import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700118
justinzhang5286d3f2014-05-12 17:06:01 -0400119
Craig Mautner8d341ef2013-03-26 09:03:27 -0700120import java.io.FileDescriptor;
121import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700122import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700123import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700124import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700125import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700126import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700127
Craig Mautner4a1cb222013-12-04 16:14:06 -0800128public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800129 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700130 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700131 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700132 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700133 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700134 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700135 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700136 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700137 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
138 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700139 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700140 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700141 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
142 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700143 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700144 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800145
Craig Mautnerf3333272013-04-22 10:55:53 -0700146 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700147 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700148
Craig Mautner0eea92c2013-05-16 13:35:39 -0700149 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700150 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700151
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700152 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700153 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700154
Craig Mautner05d29032013-05-03 13:40:13 -0700155 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
156 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
157 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700158 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700159 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
161 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
162 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700163 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400164 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
165 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700166 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700167 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700168 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169
Wale Ogunwale040b4702015-08-06 18:10:50 -0700170 private static final String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700171
Jason Monk62515be2014-05-21 16:06:19 -0400172 private static final String LOCK_TASK_TAG = "Lock-to-App";
173
Wale Ogunwale040b4702015-08-06 18:10:50 -0700174 // Used to indicate if an object (e.g. stack) that we are trying to get
175 // should be created if it doesn't exist already.
176 private static final boolean CREATE_IF_NEEDED = true;
177
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700178 // Used to indicate that windows of activities should be preserved during the resize.
179 static final boolean PRESERVE_WINDOWS = true;
180
Wale Ogunwale040b4702015-08-06 18:10:50 -0700181 // Used to indicate if an object (e.g. task) should be moved/created
182 // at the top of its container (e.g. stack).
Wale Ogunwaleb30daaa2015-08-07 21:50:49 -0700183 static final boolean ON_TOP = true;
Wale Ogunwale040b4702015-08-06 18:10:50 -0700184
185 // Used to indicate that an objects (e.g. task) removal from its container
186 // (e.g. stack) is due to it moving to another container.
187 static final boolean MOVING = true;
188
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700189 // Force the focus to change to the stack we are moving a task to..
190 static final boolean FORCE_FOCUS = true;
191
Svetoslav7008b512015-06-24 18:47:07 -0700192 // Activity actions an app cannot start if it uses a permission which is not granted.
193 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
194 new ArrayMap<>();
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700195
Svetoslav7008b512015-06-24 18:47:07 -0700196 static {
197 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
198 Manifest.permission.CAMERA);
199 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
200 Manifest.permission.CAMERA);
201 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
202 Manifest.permission.CALL_PHONE);
203 }
204
Svet Ganov99b60432015-06-27 13:15:22 -0700205 /** Action restriction: launching the activity is not restricted. */
206 private static final int ACTIVITY_RESTRICTION_NONE = 0;
207 /** Action restriction: launching the activity is restricted by a permission. */
208 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
209 /** Action restriction: launching the activity is restricted by an app op. */
210 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700211
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700212 // The height/width divide used when fitting a task within a bounds with method
213 // {@link #fitWithinBounds}.
214 // We always want the task to to be visible in the bounds without affecting its size when
215 // fitting. To make sure this is the case, we don't adjust the task left or top side pass
216 // the input bounds right or bottom side minus the width or height divided by this value.
217 private static final int FIT_WITHIN_BOUNDS_DIVIDER = 3;
218
justinzhang5286d3f2014-05-12 17:06:01 -0400219 /** Status Bar Service **/
220 private IBinder mToken = new Binder();
221 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400222 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400223
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700224 // For debugging to make sure the caller when acquiring/releasing our
225 // wake lock is the system process.
226 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700227
Craig Mautner27084302013-03-25 08:05:25 -0700228 final ActivityManagerService mService;
229
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800230 private final RecentTasks mRecentTasks;
231
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700232 final ActivityStackSupervisorHandler mHandler;
233
234 /** Short cut */
235 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800236 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700237
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700238 /** Counter for next free stack ID to use for dynamic activity stacks. */
239 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700240
241 /** Task identifier that activities are currently being started in. Incremented each time a
242 * new task is created. */
243 private int mCurTaskId = 0;
244
Craig Mautner2420ead2013-04-01 17:13:20 -0700245 /** The current user */
246 private int mCurrentUser;
247
Craig Mautnere0a38842013-12-16 16:14:02 -0800248 /** The stack containing the launcher app. Assumed to always be attached to
249 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700250 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700251
Craig Mautnere0a38842013-12-16 16:14:02 -0800252 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700253 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700254
Craig Mautner4a1cb222013-12-04 16:14:06 -0800255 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
256 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800257 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800258 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700259
260 /** List of activities that are waiting for a new activity to become visible before completing
261 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800262 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700263
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700264 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800265 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700266
267 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800268 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700269
Craig Mautnerde4ef022013-04-07 19:01:33 -0700270 /** List of activities that are ready to be stopped, but waiting for the next activity to
271 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800272 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700273
Craig Mautnerf3333272013-04-22 10:55:53 -0700274 /** List of activities that are ready to be finished, but waiting for the previous activity to
275 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800276 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700277
Craig Mautner0eea92c2013-05-16 13:35:39 -0700278 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800279 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700280
Craig Mautnerf3333272013-04-22 10:55:53 -0700281 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700282 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700283
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700284 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700285 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700286
Craig Mautnerde4ef022013-04-07 19:01:33 -0700287 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
288 * is being brought in front of us. */
289 boolean mUserLeaving = false;
290
Craig Mautner0eea92c2013-05-16 13:35:39 -0700291 /** Set when we have taken too long waiting to go to sleep. */
292 boolean mSleepTimeout = false;
293
Jose Lima58e66d62014-05-27 20:00:27 -0700294 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
295 * setWindowManager is called. **/
296 private boolean mLeanbackOnlyDevice;
297
Craig Mautner0eea92c2013-05-16 13:35:39 -0700298 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700299 * We don't want to allow the device to go to sleep while in the process
300 * of launching an activity. This is primarily to allow alarm intent
301 * receivers to launch an activity and get that to run before the device
302 * goes back to sleep.
303 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700304 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700305
306 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700307 * Set when the system is going to sleep, until we have
308 * successfully paused the current activity and released our wake lock.
309 * At that point the system is allowed to actually sleep.
310 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700311 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700312
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700313 /** Stack id of the front stack when user switched, indexed by userId. */
314 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700315
Craig Mautner4504de52013-12-20 09:06:56 -0800316 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700318 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319
320 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700321 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322
Jeff Brownca9bc702014-02-11 14:32:56 -0800323 InputManagerInternal mInputManagerInternal;
324
Craig Mautner15df08a2015-04-01 12:17:18 -0700325 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
326 * may be finished until there is only one entry left. If this is empty the system is not
327 * in lockTask mode. */
328 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000329 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700330 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
331 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000332 */
333 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400334 /**
335 * Notifies the user when entering/exiting lock-task.
336 */
337 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800338
Craig Mautner15df08a2015-04-01 12:17:18 -0700339 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700340
Craig Mautner42d04db2014-11-06 12:13:23 -0800341 /** Used to keep resumeTopActivityLocked() from being entered recursively */
342 boolean inResumeTopActivity;
343
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700344 // temp. rects used during resize calculation so we don't need to create a new object each time.
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700345 private final Rect tempRect = new Rect();
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -0700346 private final Rect tempRect2 = new Rect();
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700347
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -0700348 private final SparseArray<Configuration> mTmpConfigs = new SparseArray<>();
349 private final SparseArray<Rect> mTmpBounds = new SparseArray<>();
350
Craig Mautneree36c772014-07-16 14:56:05 -0700351 /**
352 * Description of a request to start a new activity, which has been held
353 * due to app switches being disabled.
354 */
355 static class PendingActivityLaunch {
356 final ActivityRecord r;
357 final ActivityRecord sourceRecord;
358 final int startFlags;
359 final ActivityStack stack;
360
361 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
362 int _startFlags, ActivityStack _stack) {
363 r = _r;
364 sourceRecord = _sourceRecord;
365 startFlags = _startFlags;
366 stack = _stack;
367 }
368 }
369
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800370 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700371 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800372 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700373 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
374 }
375
376 /**
377 * At the time when the constructor runs, the power manager has not yet been
378 * initialized. So we initialize our wakelocks afterwards.
379 */
380 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700382 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700383 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700384 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700385 }
386
justinzhang5286d3f2014-05-12 17:06:01 -0400387 // This function returns a IStatusBarService. The value is from ServiceManager.
388 // getService and is cached.
389 private IStatusBarService getStatusBarService() {
390 synchronized (mService) {
391 if (mStatusBarService == null) {
392 mStatusBarService = IStatusBarService.Stub.asInterface(
393 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
394 if (mStatusBarService == null) {
395 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
396 }
397 }
398 return mStatusBarService;
399 }
400 }
401
Jason Monk35c62a42014-06-17 10:24:47 -0400402 private IDevicePolicyManager getDevicePolicyManager() {
403 synchronized (mService) {
404 if (mDevicePolicyManager == null) {
405 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
406 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
407 if (mDevicePolicyManager == null) {
408 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
409 }
410 }
411 return mDevicePolicyManager;
412 }
413 }
414
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700415 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416 synchronized (mService) {
417 mWindowManager = wm;
418
419 mDisplayManager =
420 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
421 mDisplayManager.registerDisplayListener(this, null);
422
423 Display[] displays = mDisplayManager.getDisplays();
424 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
425 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800426 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700427 if (activityDisplay.mDisplay == null) {
428 throw new IllegalStateException("Default Display does not exist");
429 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800430 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800431 }
432
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700433 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800434 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800435
436 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700437
438 // Initialize this here, now that we can get a valid reference to PackageManager.
439 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800440 }
Craig Mautner27084302013-03-25 08:05:25 -0700441 }
442
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200443 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700444 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200445 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700446 }
447
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700448 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800449 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700450 }
451
Craig Mautnerde4ef022013-04-07 19:01:33 -0700452 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800453 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700454 }
455
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800456 /** Top of all visible stacks is/should always be equal to the focused stack.
457 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
458 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700459 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700460 if (stack == null) {
461 return false;
462 }
463
Craig Mautnerdf88d732014-01-27 09:21:32 -0800464 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
465 if (parent != null) {
466 stack = parent.task.stack;
467 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800468 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700469 }
470
Craig Mautner299f9602015-01-26 09:47:33 -0800471 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800472 moveHomeStack(toFront, reason, null);
473 }
474
475 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800476 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800477 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800478 if (topNdx <= 0) {
479 return;
480 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700481
482 // The home stack should either be at the top or bottom of the stack list.
483 if ((toFront && (stacks.get(topNdx) != mHomeStack))
484 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700485 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700486 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
487 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800488 stacks.remove(mHomeStack);
489 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700490 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800491
492 if (lastFocusedStack != null) {
493 mLastFocusedStack = lastFocusedStack;
494 }
495 mFocusedStack = stacks.get(topNdx);
496
Craig Mautnerde313752015-01-22 14:28:03 -0800497 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
498 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800499 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800500
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800501 if (mService.mBooting || !mService.mBooted) {
502 final ActivityRecord r = topRunningActivityLocked();
503 if (r != null && r.idle) {
504 checkFinishBootingLocked();
505 }
506 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700507 }
508
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700509 /** Returns true if the focus activity was adjusted to the home stack top activity. */
510 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700511 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
512 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700513 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700514 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700515
Craig Mautner84984fa2014-06-19 11:19:20 -0700516 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700517
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700518 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700519 if (top == null) {
520 return false;
521 }
522 mService.setFocusedActivityLocked(top, reason);
523 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700524 }
525
Craig Mautner299f9602015-01-26 09:47:33 -0800526 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700527 if (!mService.mBooting && !mService.mBooted) {
528 // Not ready yet!
529 return false;
530 }
531
Craig Mautner84984fa2014-06-19 11:19:20 -0700532 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
533 mWindowManager.showRecentApps();
534 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700535 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700536
Craig Mautner84984fa2014-06-19 11:19:20 -0700537 if (prev != null) {
538 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
539 }
540
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700541 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
542 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700543 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800544 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700545 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700546 }
Craig Mautner299f9602015-01-26 09:47:33 -0800547 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700548 }
549
Craig Mautner8d341ef2013-03-26 09:03:27 -0700550 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700551 return anyTaskForIdLocked(id, true);
552 }
553
554 /**
555 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
556 * @param id Id of the task we would like returned.
557 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
558 * restore the task from recents to the active list.
559 */
560 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800561 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800562 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800563 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
565 ActivityStack stack = stacks.get(stackNdx);
566 TaskRecord task = stack.taskForIdLocked(id);
567 if (task != null) {
568 return task;
569 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700570 }
571 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800572
573 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700574 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800575 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800576 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700577 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800578 return null;
579 }
580
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700581 if (!restoreFromRecents) {
582 return task;
583 }
584
Chong Zhang5dcb2752015-08-18 13:50:26 -0700585 if (!restoreRecentTaskLocked(task, INVALID_STACK_ID)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700586 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
587 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800588 return null;
589 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800591 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700592 }
593
Craig Mautner6170f732013-04-02 13:05:23 -0700594 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800595 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800596 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800597 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800598 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
599 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
600 if (r != null) {
601 return r;
602 }
Craig Mautner6170f732013-04-02 13:05:23 -0700603 }
604 }
605 return null;
606 }
607
Craig Mautneref73ee12014-04-23 11:45:37 -0700608 void setNextTaskId(int taskId) {
609 if (taskId > mCurTaskId) {
610 mCurTaskId = taskId;
611 }
612 }
613
Craig Mautner8d341ef2013-03-26 09:03:27 -0700614 int getNextTaskId() {
615 do {
616 mCurTaskId++;
617 if (mCurTaskId <= 0) {
618 mCurTaskId = 1;
619 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700620 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700621 return mCurTaskId;
622 }
623
Craig Mautnerde4ef022013-04-07 19:01:33 -0700624 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800625 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700626 if (stack == null) {
627 return null;
628 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700629 ActivityRecord resumedActivity = stack.mResumedActivity;
630 if (resumedActivity == null || resumedActivity.app == null) {
631 resumedActivity = stack.mPausingActivity;
632 if (resumedActivity == null || resumedActivity.app == null) {
633 resumedActivity = stack.topRunningActivityLocked(null);
634 }
635 }
636 return resumedActivity;
637 }
638
Dianne Hackbornff072722014-09-24 10:56:28 -0700639 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700640 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800641 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800642 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
643 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800644 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
645 final ActivityStack stack = stacks.get(stackNdx);
646 if (!isFrontStack(stack)) {
647 continue;
648 }
649 ActivityRecord hr = stack.topRunningActivityLocked(null);
650 if (hr != null) {
651 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
652 && processName.equals(hr.processName)) {
653 try {
George Mount2c92c972014-03-20 09:38:23 -0700654 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 didSomething = true;
656 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700657 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800658 Slog.w(TAG, "Exception in new application when starting activity "
659 + hr.intent.getComponent().flattenToShortString(), e);
660 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700661 }
Craig Mautner20e72272013-04-01 13:45:53 -0700662 }
Craig Mautner20e72272013-04-01 13:45:53 -0700663 }
664 }
665 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700666 if (!didSomething) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700667 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700668 }
Craig Mautner20e72272013-04-01 13:45:53 -0700669 return didSomething;
670 }
671
672 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
674 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
676 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800677 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800678 continue;
679 }
680 final ActivityRecord resumedActivity = stack.mResumedActivity;
681 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700682 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800683 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800684 return false;
685 }
Craig Mautner20e72272013-04-01 13:45:53 -0700686 }
687 }
688 return true;
689 }
690
Craig Mautnerde4ef022013-04-07 19:01:33 -0700691 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800692 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
693 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800694 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
695 final ActivityStack stack = stacks.get(stackNdx);
696 if (isFrontStack(stack)) {
697 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700698 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800699 return false;
700 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700701 }
702 }
703 }
704 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700705 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800706 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
707 mLastFocusedStack + " to=" + mFocusedStack);
708 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700709 return true;
710 }
711
712 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800713 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800714 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
715 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800716 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
717 final ActivityStack stack = stacks.get(stackNdx);
718 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800719 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700720 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800721 return false;
722 }
723 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700725 }
726 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800727 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700728 }
729
Craig Mautner2acc3892013-09-23 10:28:14 -0700730 /**
731 * Pause all activities in either all of the stacks or just the back stacks.
732 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700733 * @return true if any activity was paused as a result of this call.
734 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700735 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700736 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800737 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
738 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800739 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
740 final ActivityStack stack = stacks.get(stackNdx);
741 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700742 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800743 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700744 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
745 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800746 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700747 }
748 }
749 return someActivityPaused;
750 }
751
Craig Mautnerde4ef022013-04-07 19:01:33 -0700752 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700753 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800754 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
755 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
757 final ActivityStack stack = stacks.get(stackNdx);
758 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700759 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800760 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700761 Slog.d(TAG_STATES,
762 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800763 pausing = false;
764 } else {
765 return false;
766 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700767 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700768 }
769 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700770 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700771 }
772
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700773 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
774 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500775 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800776 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
777 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
778 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
779 final ActivityStack stack = stacks.get(stackNdx);
780 if (stack.mResumedActivity != null &&
781 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700782 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800783 }
784 }
785 }
786 }
787
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700788 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700789 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700790 }
791
792 void sendWaitingVisibleReportLocked(ActivityRecord r) {
793 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700794 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700795 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700796 if (w.who == null) {
797 changed = true;
798 w.timeout = false;
799 if (r != null) {
800 w.who = new ComponentName(r.info.packageName, r.info.name);
801 }
802 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
803 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700804 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700805 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700806 if (changed) {
807 mService.notifyAll();
808 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700809 }
810
811 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
812 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700813 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700814 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700815 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700816 if (w.who == null) {
817 changed = true;
818 w.timeout = timeout;
819 if (r != null) {
820 w.who = new ComponentName(r.info.packageName, r.info.name);
821 }
822 w.thisTime = thisTime;
823 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700824 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700825 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700826 if (changed) {
827 mService.notifyAll();
828 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700829 }
830
Craig Mautner29219d92013-04-16 20:19:12 -0700831 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800832 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700833 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
834 if (r != null) {
835 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700836 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700837
Craig Mautner4a1cb222013-12-04 16:14:06 -0800838 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800839 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800840 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
841 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700842 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700843 r = stack.topRunningActivityLocked(null);
844 if (r != null) {
845 return r;
846 }
847 }
848 }
849 return null;
850 }
851
Dianne Hackborn09233282014-04-30 11:33:59 -0700852 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700853 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800854 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
855 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800856 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800857 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800858 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800859 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
860 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700861 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800862 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700863 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700864 }
865 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700866
867 // The lists are already sorted from most recent to oldest. Just pull the most recent off
868 // each list and add it to list. Stop when all lists are empty or maxNum reached.
869 while (maxNum > 0) {
870 long mostRecentActiveTime = Long.MIN_VALUE;
871 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800872 final int numTaskLists = runningTaskLists.size();
873 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
874 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700875 if (!stackTaskList.isEmpty()) {
876 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
877 if (lastActiveTime > mostRecentActiveTime) {
878 mostRecentActiveTime = lastActiveTime;
879 selectedStackList = stackTaskList;
880 }
881 }
882 }
883 if (selectedStackList != null) {
884 list.add(selectedStackList.remove(0));
885 --maxNum;
886 } else {
887 break;
888 }
889 }
Craig Mautner20e72272013-04-01 13:45:53 -0700890 }
891
Craig Mautner23ac33b2013-04-01 16:26:35 -0700892 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700893 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894 // Collect information about the target of the Intent.
895 ActivityInfo aInfo;
896 try {
897 ResolveInfo rInfo =
898 AppGlobals.getPackageManager().resolveIntent(
899 intent, resolvedType,
900 PackageManager.MATCH_DEFAULT_ONLY
901 | ActivityManagerService.STOCK_PM_FLAGS, userId);
902 aInfo = rInfo != null ? rInfo.activityInfo : null;
903 } catch (RemoteException e) {
904 aInfo = null;
905 }
906
907 if (aInfo != null) {
908 // Store the found target back into the intent, because now that
909 // we have it we never want to do this again. For example, if the
910 // user navigates back to this point in the history, we should
911 // always restart the exact same activity.
912 intent.setComponent(new ComponentName(
913 aInfo.applicationInfo.packageName, aInfo.name));
914
915 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700916 if (!aInfo.processName.equals("system")) {
917 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700918 mService.setDebugApp(aInfo.processName, true, false);
919 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700920
Man Caocfa78b22015-06-11 20:14:34 -0700921 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
923 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700924
Man Caocfa78b22015-06-11 20:14:34 -0700925 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
926 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
927 }
928
929 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700930 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700931 }
932 }
933 }
934 return aInfo;
935 }
936
Craig Mautner299f9602015-01-26 09:47:33 -0800937 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
938 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700939 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
940 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
941 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
942 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700943 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
944 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700945 null /* outActivity */, null /* container */, null /* inTask */);
946 if (inResumeTopActivity) {
947 // If we are in resume section already, home activity will be initialized, but not
948 // resumed (to avoid recursive resume) and will stay that way until something pokes it
949 // again. We need to schedule another resume.
950 scheduleResumeTopActivities();
951 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700952 }
953
Craig Mautner23ac33b2013-04-01 16:26:35 -0700954 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700955 String callingPackage, Intent intent, String resolvedType,
956 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700957 IBinder resultTo, String resultWho, int requestCode, int startFlags,
958 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700959 Bundle options, boolean ignoreTargetSecurity, int userId,
960 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700961 // Refuse possible leaked file descriptors
962 if (intent != null && intent.hasFileDescriptors()) {
963 throw new IllegalArgumentException("File descriptors passed in Intent");
964 }
965 boolean componentSpecified = intent.getComponent() != null;
966
967 // Don't modify the client's object!
968 intent = new Intent(intent);
969
970 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700971 ActivityInfo aInfo =
972 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700973
Craig Mautnere0a38842013-12-16 16:14:02 -0800974 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700975 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800976 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700977 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800978 // Cannot start a child activity if the parent is not resumed.
979 return ActivityManager.START_CANCELED;
980 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700981 final int realCallingPid = Binder.getCallingPid();
982 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700983 int callingPid;
984 if (callingUid >= 0) {
985 callingPid = -1;
986 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700987 callingPid = realCallingPid;
988 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700989 } else {
990 callingPid = callingUid = -1;
991 }
992
Craig Mautnere0a38842013-12-16 16:14:02 -0800993 final ActivityStack stack;
994 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800995 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800996 } else {
997 stack = container.mStack;
998 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800999 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001000 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -07001001 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001002
1003 final long origId = Binder.clearCallingIdentity();
1004
1005 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001006 (aInfo.applicationInfo.privateFlags
1007 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001008 // This may be a heavy-weight process! Check to see if we already
1009 // have another, different heavy-weight process running.
1010 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
1011 if (mService.mHeavyWeightProcess != null &&
1012 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
1013 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001014 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001015 if (caller != null) {
1016 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
1017 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001018 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -07001019 } else {
1020 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -07001021 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -07001022 + intent.toString());
1023 ActivityOptions.abort(options);
1024 return ActivityManager.START_PERMISSION_DENIED;
1025 }
1026 }
1027
1028 IIntentSender target = mService.getIntentSenderLocked(
1029 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001030 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001031 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1032 | PendingIntent.FLAG_ONE_SHOT, null);
1033
1034 Intent newIntent = new Intent();
1035 if (requestCode >= 0) {
1036 // Caller is requesting a result.
1037 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1038 }
1039 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1040 new IntentSender(target));
1041 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1042 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1043 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1044 hist.packageName);
1045 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1046 hist.task.taskId);
1047 }
1048 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1049 aInfo.packageName);
1050 newIntent.setFlags(intent.getFlags());
1051 newIntent.setClassName("android",
1052 HeavyWeightSwitcherActivity.class.getName());
1053 intent = newIntent;
1054 resolvedType = null;
1055 caller = null;
1056 callingUid = Binder.getCallingUid();
1057 callingPid = Binder.getCallingPid();
1058 componentSpecified = true;
1059 try {
1060 ResolveInfo rInfo =
1061 AppGlobals.getPackageManager().resolveIntent(
1062 intent, null,
1063 PackageManager.MATCH_DEFAULT_ONLY
1064 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1065 aInfo = rInfo != null ? rInfo.activityInfo : null;
1066 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1067 } catch (RemoteException e) {
1068 aInfo = null;
1069 }
1070 }
1071 }
1072 }
1073
Dianne Hackborn91097de2014-04-04 18:02:06 -07001074 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1075 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001076 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001077 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001078 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001079
Craig Mautner85c11a82014-07-17 12:38:18 -07001080 Binder.restoreCallingIdentity(origId);
1081
Craig Mautnerde4ef022013-04-07 19:01:33 -07001082 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001083 // If the caller also wants to switch to a new configuration,
1084 // do so now. This allows a clean switch, as we are waiting
1085 // for the current activity to pause (so we will not destroy
1086 // it), and have not yet started the next activity.
1087 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1088 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001089 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001090 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001091 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001092 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001093 }
1094
Craig Mautner23ac33b2013-04-01 16:26:35 -07001095 if (outResult != null) {
1096 outResult.result = res;
1097 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001098 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001099 do {
1100 try {
1101 mService.wait();
1102 } catch (InterruptedException e) {
1103 }
1104 } while (!outResult.timeout && outResult.who == null);
1105 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001106 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001107 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001108 outResult.timeout = false;
1109 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1110 outResult.totalTime = 0;
1111 outResult.thisTime = 0;
1112 } else {
1113 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001114 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001115 do {
1116 try {
1117 mService.wait();
1118 } catch (InterruptedException e) {
1119 }
1120 } while (!outResult.timeout && outResult.who == null);
1121 }
1122 }
1123 }
1124
1125 return res;
1126 }
1127 }
1128
1129 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1130 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1131 Bundle options, int userId) {
1132 if (intents == null) {
1133 throw new NullPointerException("intents is null");
1134 }
1135 if (resolvedTypes == null) {
1136 throw new NullPointerException("resolvedTypes is null");
1137 }
1138 if (intents.length != resolvedTypes.length) {
1139 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1140 }
1141
Craig Mautner23ac33b2013-04-01 16:26:35 -07001142
1143 int callingPid;
1144 if (callingUid >= 0) {
1145 callingPid = -1;
1146 } else if (caller == null) {
1147 callingPid = Binder.getCallingPid();
1148 callingUid = Binder.getCallingUid();
1149 } else {
1150 callingPid = callingUid = -1;
1151 }
1152 final long origId = Binder.clearCallingIdentity();
1153 try {
1154 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001155 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001156 for (int i=0; i<intents.length; i++) {
1157 Intent intent = intents[i];
1158 if (intent == null) {
1159 continue;
1160 }
1161
1162 // Refuse possible leaked file descriptors
1163 if (intent != null && intent.hasFileDescriptors()) {
1164 throw new IllegalArgumentException("File descriptors passed in Intent");
1165 }
1166
1167 boolean componentSpecified = intent.getComponent() != null;
1168
1169 // Don't modify the client's object!
1170 intent = new Intent(intent);
1171
1172 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001173 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001174 // TODO: New, check if this is correct
1175 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1176
1177 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001178 (aInfo.applicationInfo.privateFlags
1179 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001180 throw new IllegalArgumentException(
1181 "FLAG_CANT_SAVE_STATE not supported here");
1182 }
1183
1184 Bundle theseOptions;
1185 if (options != null && i == intents.length-1) {
1186 theseOptions = options;
1187 } else {
1188 theseOptions = null;
1189 }
Craig Mautner6170f732013-04-02 13:05:23 -07001190 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001191 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1192 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001193 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001194 if (res < 0) {
1195 return res;
1196 }
1197
1198 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1199 }
1200 }
1201 } finally {
1202 Binder.restoreCallingIdentity(origId);
1203 }
1204
1205 return ActivityManager.START_SUCCESS;
1206 }
1207
Craig Mautner2420ead2013-04-01 17:13:20 -07001208 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001209 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001210 throws RemoteException {
1211
Craig Mautner2568c3a2015-03-26 14:22:34 -07001212 if (andResume) {
1213 r.startFreezingScreenLocked(app, 0);
1214 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001215
Craig Mautner2568c3a2015-03-26 14:22:34 -07001216 // schedule launch ticks to collect information about slow apps.
1217 r.startLaunchTickingLocked();
1218 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001219
1220 // Have the window manager re-evaluate the orientation of
1221 // the screen based on the new activity order. Note that
1222 // as a result of this, it can call back into the activity
1223 // manager with a new orientation. We don't care about that,
1224 // because the activity is not currently running so we are
1225 // just restarting it anyway.
1226 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001227 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001228 mService.mConfiguration,
1229 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001230 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001231 }
1232
1233 r.app = app;
1234 app.waitingToKill = null;
1235 r.launchCount++;
1236 r.lastLaunchTime = SystemClock.uptimeMillis();
1237
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001238 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001239
1240 int idx = app.activities.indexOf(r);
1241 if (idx < 0) {
1242 app.activities.add(r);
1243 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001244 mService.updateLruProcessLocked(app, true, null);
1245 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001246
Craig Mautner15df08a2015-04-01 12:17:18 -07001247 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001248 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1249 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001250 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001251 }
1252
1253 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001254 try {
1255 if (app.thread == null) {
1256 throw new RemoteException();
1257 }
1258 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001259 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001260 if (andResume) {
1261 results = r.results;
1262 newIntents = r.newIntents;
1263 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001264 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1265 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1266 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001267 if (andResume) {
1268 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1269 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001270 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001271 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001272 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001273 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001274 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001275 }
1276 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1277 r.sleeping = false;
1278 r.forceNewConfig = false;
1279 mService.showAskCompatModeDialogLocked(r);
1280 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001281 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001282 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1283 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1284 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001285 final String profileFile = mService.mProfileFile;
1286 if (profileFile != null) {
1287 ParcelFileDescriptor profileFd = mService.mProfileFd;
1288 if (profileFd != null) {
1289 try {
1290 profileFd = profileFd.dup();
1291 } catch (IOException e) {
1292 if (profileFd != null) {
1293 try {
1294 profileFd.close();
1295 } catch (IOException o) {
1296 }
1297 profileFd = null;
1298 }
1299 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001300 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001301
1302 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1303 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001304 }
1305 }
1306 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001307
Craig Mautner2568c3a2015-03-26 14:22:34 -07001308 if (andResume) {
1309 app.hasShownUi = true;
1310 app.pendingUiClean = true;
1311 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001312 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001313 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001314 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001315 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001316 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001317 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001318
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001319 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001320 // This may be a heavy-weight process! Note that the package
1321 // manager will ensure that only activity can run in the main
1322 // process of the .apk, which is the only thing that will be
1323 // considered heavy-weight.
1324 if (app.processName.equals(app.info.packageName)) {
1325 if (mService.mHeavyWeightProcess != null
1326 && mService.mHeavyWeightProcess != app) {
1327 Slog.w(TAG, "Starting new heavy weight process " + app
1328 + " when already running "
1329 + mService.mHeavyWeightProcess);
1330 }
1331 mService.mHeavyWeightProcess = app;
1332 Message msg = mService.mHandler.obtainMessage(
1333 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1334 msg.obj = r;
1335 mService.mHandler.sendMessage(msg);
1336 }
1337 }
1338
1339 } catch (RemoteException e) {
1340 if (r.launchFailed) {
1341 // This is the second time we failed -- finish activity
1342 // and give up.
1343 Slog.e(TAG, "Second failure launching "
1344 + r.intent.getComponent().flattenToShortString()
1345 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001346 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001347 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1348 "2nd-crash", false);
1349 return false;
1350 }
1351
1352 // This is the first time we failed -- restart process and
1353 // retry.
1354 app.activities.remove(r);
1355 throw e;
1356 }
1357
1358 r.launchFailed = false;
1359 if (stack.updateLRUListLocked(r)) {
1360 Slog.w(TAG, "Activity " + r
1361 + " being launched, but already in LRU list");
1362 }
1363
1364 if (andResume) {
1365 // As part of the process of launching, ActivityThread also performs
1366 // a resume.
1367 stack.minimalResumeActivityLocked(r);
1368 } else {
1369 // This activity is not starting in the resumed state... which
1370 // should look like we asked it to pause+stop (but remain visible),
1371 // and it has done so and reported back the current icicle and
1372 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001373 if (DEBUG_STATES) Slog.v(TAG_STATES,
1374 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001375 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001376 r.stopped = true;
1377 }
1378
1379 // Launch the new version setup screen if needed. We do this -after-
1380 // launching the initial activity (that is, home), so that it can have
1381 // a chance to initialize itself while in the background, making the
1382 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001383 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001384 mService.startSetupActivityLocked();
1385 }
1386
Dianne Hackborn465fa392014-09-14 14:21:18 -07001387 // Update any services we are bound to that might care about whether
1388 // their client may have activities.
1389 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1390
Craig Mautner2420ead2013-04-01 17:13:20 -07001391 return true;
1392 }
1393
Craig Mautnere79d42682013-04-01 19:01:53 -07001394 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001395 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001396 // Is this activity's application already running?
1397 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001398 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001399
1400 r.task.stack.setLaunchTime(r);
1401
1402 if (app != null && app.thread != null) {
1403 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001404 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1405 || !"android".equals(r.info.packageName)) {
1406 // Don't add this if it is a platform component that is marked
1407 // to run in multiple processes, because this is actually
1408 // part of the framework so doesn't make sense to track as a
1409 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001410 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1411 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001412 }
George Mount2c92c972014-03-20 09:38:23 -07001413 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001414 return;
1415 } catch (RemoteException e) {
1416 Slog.w(TAG, "Exception when starting activity "
1417 + r.intent.getComponent().flattenToShortString(), e);
1418 }
1419
1420 // If a dead object exception was thrown -- fall through to
1421 // restart the application.
1422 }
1423
1424 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001425 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001426 }
1427
Craig Mautner6170f732013-04-02 13:05:23 -07001428 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001429 Intent intent, String resolvedType, ActivityInfo aInfo,
1430 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1431 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001432 int callingPid, int callingUid, String callingPackage,
1433 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001434 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1435 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001436 int err = ActivityManager.START_SUCCESS;
1437
1438 ProcessRecord callerApp = null;
1439 if (caller != null) {
1440 callerApp = mService.getRecordForAppLocked(caller);
1441 if (callerApp != null) {
1442 callingPid = callerApp.pid;
1443 callingUid = callerApp.info.uid;
1444 } else {
1445 Slog.w(TAG, "Unable to find app for caller " + caller
1446 + " (pid=" + callingPid + ") when starting: "
1447 + intent.toString());
1448 err = ActivityManager.START_PERMISSION_DENIED;
1449 }
1450 }
1451
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001452 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1453
Craig Mautner6170f732013-04-02 13:05:23 -07001454 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001455 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001456 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001457 + " on display " + (container == null ? (mFocusedStack == null ?
1458 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1459 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1460 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001461 }
1462
1463 ActivityRecord sourceRecord = null;
1464 ActivityRecord resultRecord = null;
1465 if (resultTo != null) {
1466 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001467 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1468 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001469 if (sourceRecord != null) {
1470 if (requestCode >= 0 && !sourceRecord.finishing) {
1471 resultRecord = sourceRecord;
1472 }
1473 }
1474 }
Craig Mautner6170f732013-04-02 13:05:23 -07001475
Dianne Hackborn91097de2014-04-04 18:02:06 -07001476 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001477
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001478 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001479 // Transfer the result target from the source activity to the new
1480 // one being started, including any failures.
1481 if (requestCode >= 0) {
1482 ActivityOptions.abort(options);
1483 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1484 }
1485 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001486 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1487 resultRecord = null;
1488 }
Craig Mautner6170f732013-04-02 13:05:23 -07001489 resultWho = sourceRecord.resultWho;
1490 requestCode = sourceRecord.requestCode;
1491 sourceRecord.resultTo = null;
1492 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001493 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001494 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001495 if (sourceRecord.launchedFromUid == callingUid) {
1496 // The new activity is being launched from the same uid as the previous
1497 // activity in the flow, and asking to forward its result back to the
1498 // previous. In this case the activity is serving as a trampoline between
1499 // the two, so we also want to update its launchedFromPackage to be the
1500 // same as the previous activity. Note that this is safe, since we know
1501 // these two packages come from the same uid; the caller could just as
1502 // well have supplied that same package name itself. This specifially
1503 // deals with the case of an intent picker/chooser being launched in the app
1504 // flow to redirect to an activity picked by the user, where we want the final
1505 // activity to consider it to have been launched by the previous app activity.
1506 callingPackage = sourceRecord.launchedFromPackage;
1507 }
Craig Mautner6170f732013-04-02 13:05:23 -07001508 }
1509
1510 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1511 // We couldn't find a class that can handle the given Intent.
1512 // That's the end of that!
1513 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1514 }
1515
1516 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1517 // We couldn't find the specific class specified in the Intent.
1518 // Also the end of the line.
1519 err = ActivityManager.START_CLASS_NOT_FOUND;
1520 }
1521
Dianne Hackborn91097de2014-04-04 18:02:06 -07001522 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1523 && sourceRecord.task.voiceSession != null) {
1524 // If this activity is being launched as part of a voice session, we need
1525 // to ensure that it is safe to do so. If the upcoming activity will also
1526 // be part of the voice session, we can only launch it if it has explicitly
1527 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001528 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001529 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1530 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001531 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001532 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1533 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001534 Slog.w(TAG,
1535 "Activity being started in current voice task does not support voice: "
1536 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001537 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1538 }
1539 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001540 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001541 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1542 }
1543 }
1544 }
1545
1546 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1547 // If the caller is starting a new voice session, just make sure the target
1548 // is actually allowing it to run this way.
1549 try {
1550 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1551 intent, resolvedType)) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001552 Slog.w(TAG,
1553 "Activity being started in new voice task does not support: "
1554 + intent);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001555 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1556 }
1557 } catch (RemoteException e) {
Dianne Hackborn74aefdf2015-08-07 10:37:21 -07001558 Slog.w(TAG, "Failure checking voice capabilities", e);
Dianne Hackborn91097de2014-04-04 18:02:06 -07001559 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1560 }
1561 }
1562
louis_changcd5d1982014-08-01 10:09:08 +08001563 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1564
Craig Mautner6170f732013-04-02 13:05:23 -07001565 if (err != ActivityManager.START_SUCCESS) {
1566 if (resultRecord != null) {
1567 resultStack.sendActivityResultLocked(-1,
1568 resultRecord, resultWho, requestCode,
1569 Activity.RESULT_CANCELED, null);
1570 }
Craig Mautner6170f732013-04-02 13:05:23 -07001571 ActivityOptions.abort(options);
1572 return err;
1573 }
1574
Svetoslav7008b512015-06-24 18:47:07 -07001575 boolean abort = false;
1576
Svet Ganov99b60432015-06-27 13:15:22 -07001577 final int startAnyPerm = mService.checkPermission(
1578 START_ANY_ACTIVITY, callingPid, callingUid);
1579
1580 if (startAnyPerm != PERMISSION_GRANTED) {
1581 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001582 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001583 final int actionRestriction = getActionRestrictionForCallingPackage(
1584 intent.getAction(), callingPackage, callingPid, callingUid);
1585
1586 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1587 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1588 if (resultRecord != null) {
1589 resultStack.sendActivityResultLocked(-1,
1590 resultRecord, resultWho, requestCode,
1591 Activity.RESULT_CANCELED, null);
1592 }
1593 String msg;
1594 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1595 msg = "Permission Denial: starting " + intent.toString()
1596 + " from " + callerApp + " (pid=" + callingPid
1597 + ", uid=" + callingUid + ")" + " with revoked permission "
1598 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1599 } else if (!aInfo.exported) {
1600 msg = "Permission Denial: starting " + intent.toString()
1601 + " from " + callerApp + " (pid=" + callingPid
1602 + ", uid=" + callingUid + ")"
1603 + " not exported from uid " + aInfo.applicationInfo.uid;
1604 } else {
1605 msg = "Permission Denial: starting " + intent.toString()
1606 + " from " + callerApp + " (pid=" + callingPid
1607 + ", uid=" + callingUid + ")"
1608 + " requires " + aInfo.permission;
1609 }
1610 Slog.w(TAG, msg);
1611 throw new SecurityException(msg);
1612 }
1613
1614 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1615 String message = "Appop Denial: starting " + intent.toString()
1616 + " from " + callerApp + " (pid=" + callingPid
1617 + ", uid=" + callingUid + ")"
1618 + " requires " + AppOpsManager.permissionToOp(
1619 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1620 Slog.w(TAG, message);
1621 abort = true;
1622 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1623 String message = "Appop Denial: starting " + intent.toString()
1624 + " from " + callerApp + " (pid=" + callingPid
1625 + ", uid=" + callingUid + ")"
1626 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1627 Slog.w(TAG, message);
1628 abort = true;
1629 }
Svetoslav7008b512015-06-24 18:47:07 -07001630 }
1631
1632 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001633 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001634
Craig Mautner6170f732013-04-02 13:05:23 -07001635 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001636 try {
1637 // The Intent we give to the watcher has the extra data
1638 // stripped off, since it can contain private information.
1639 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001640 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001641 aInfo.applicationInfo.packageName);
1642 } catch (RemoteException e) {
1643 mService.mController = null;
1644 }
Ben Gruver5e207332013-04-03 17:41:37 -07001645 }
Craig Mautner6170f732013-04-02 13:05:23 -07001646
Ben Gruver5e207332013-04-03 17:41:37 -07001647 if (abort) {
1648 if (resultRecord != null) {
1649 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001650 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001651 }
Ben Gruver5e207332013-04-03 17:41:37 -07001652 // We pretend to the caller that it was really started, but
1653 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001654 ActivityOptions.abort(options);
1655 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001656 }
1657
1658 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001659 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001660 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001661 if (outActivity != null) {
1662 outActivity[0] = r;
1663 }
1664
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001665 if (r.appTimeTracker == null && sourceRecord != null) {
1666 // If the caller didn't specify an explicit time tracker, we want to continue
1667 // tracking under any it has.
1668 r.appTimeTracker = sourceRecord.appTimeTracker;
1669 }
1670
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001671 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001672 if (voiceSession == null && (stack.mResumedActivity == null
1673 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001674 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1675 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001676 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001677 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001678 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001679 ActivityOptions.abort(options);
1680 return ActivityManager.START_SWITCHES_CANCELED;
1681 }
1682 }
1683
1684 if (mService.mDidAppSwitch) {
1685 // This is the second allowed switch since we stopped switches,
1686 // so now just generally allow switches. Use case: user presses
1687 // home (switches disabled, switch to home, mDidAppSwitch now true);
1688 // user taps a home icon (coming from home so allowed, we hit here
1689 // and now allow anyone to switch again).
1690 mService.mAppSwitchesAllowedTime = 0;
1691 } else {
1692 mService.mDidAppSwitch = true;
1693 }
1694
Craig Mautneree36c772014-07-16 14:56:05 -07001695 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001696
Dianne Hackborn91097de2014-04-04 18:02:06 -07001697 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001698 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001699
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001700 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001701 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001702 // activity start, but we are not actually doing an activity
1703 // switch... just dismiss the keyguard now, because we
1704 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001705 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001706 }
1707 return err;
1708 }
1709
Svet Ganov99b60432015-06-27 13:15:22 -07001710 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001711 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001712 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1713 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001714 == PackageManager.PERMISSION_DENIED) {
1715 return ACTIVITY_RESTRICTION_PERMISSION;
1716 }
1717
Christopher Tateff7add02015-08-17 10:23:22 -07001718 if (activityInfo.permission == null) {
1719 return ACTIVITY_RESTRICTION_NONE;
1720 }
1721
Svet Ganov99b60432015-06-27 13:15:22 -07001722 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1723 if (opCode == AppOpsManager.OP_NONE) {
1724 return ACTIVITY_RESTRICTION_NONE;
1725 }
1726
1727 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1728 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001729 if (!ignoreTargetSecurity) {
1730 return ACTIVITY_RESTRICTION_APPOP;
1731 }
Svet Ganov99b60432015-06-27 13:15:22 -07001732 }
1733
1734 return ACTIVITY_RESTRICTION_NONE;
1735 }
1736
Svetoslav7008b512015-06-24 18:47:07 -07001737 private int getActionRestrictionForCallingPackage(String action,
1738 String callingPackage, int callingPid, int callingUid) {
1739 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001740 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001741 }
1742
1743 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1744 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001745 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001746 }
1747
1748 final PackageInfo packageInfo;
1749 try {
1750 packageInfo = mService.mContext.getPackageManager()
1751 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1752 } catch (PackageManager.NameNotFoundException e) {
1753 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001754 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001755 }
1756
1757 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001758 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001759 }
1760
1761 if (mService.checkPermission(permission, callingPid, callingUid) ==
1762 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001763 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001764 }
1765
1766 final int opCode = AppOpsManager.permissionToOpCode(permission);
1767 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001768 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001769 }
1770
1771 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1772 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001773 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001774 }
1775
Svet Ganov99b60432015-06-27 13:15:22 -07001776 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001777 }
1778
Chong Zhang0fa656b2015-08-31 15:17:21 -07001779 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask, Rect bounds) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001780 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001781
1782 // On leanback only devices we should keep all activities in the same stack.
1783 if (!mLeanbackOnlyDevice &&
1784 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001785
1786 ActivityStack stack;
1787
Wale Ogunwale7d701172015-03-11 15:36:30 -07001788 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001789 stack = task.stack;
1790 if (stack.isOnHomeDisplay()) {
1791 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001792 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1793 "computeStackFocus: Setting " + "focused stack to r=" + r
1794 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001795 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001796 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001797 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001798 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001799 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001800 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001801 }
1802
Craig Mautnere0a38842013-12-16 16:14:02 -08001803 final ActivityContainer container = r.mInitialActivityContainer;
1804 if (container != null) {
1805 // The first time put it on the desired stack, after this put on task stack.
1806 r.mInitialActivityContainer = null;
1807 return container.mStack;
1808 }
1809
Craig Mautner1b4bf852014-05-26 15:06:32 -07001810 if (mFocusedStack != mHomeStack && (!newTask ||
1811 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001812 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001813 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001814 return mFocusedStack;
1815 }
1816
Wale Ogunwale706ed792015-08-02 10:29:44 -07001817 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001818 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1819 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001820 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001821 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1822 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001823 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001824 "computeStackFocus: Setting focused stack=" + stack);
1825 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001826 }
1827 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001828
Wale Ogunwale706ed792015-08-02 10:29:44 -07001829 // If there is no suitable dynamic stack then we figure out which static stack to use.
Chong Zhang0fa656b2015-08-31 15:17:21 -07001830 int stackId = task != null ? task.getLaunchStackId() :
1831 bounds != null ? FREEFORM_WORKSPACE_STACK_ID :
1832 FULLSCREEN_WORKSPACE_STACK_ID;
1833 stack = getStack(stackId, CREATE_IF_NEEDED, ON_TOP);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001834 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1835 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001836 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001837 }
1838 return mHomeStack;
1839 }
1840
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001841 boolean setFocusedStack(ActivityRecord r, String reason) {
1842 if (r == null) {
1843 // Not sure what you are trying to do, but it is not going to work...
1844 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001845 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001846 final TaskRecord task = r.task;
1847 if (task == null || task.stack == null) {
1848 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1849 return false;
1850 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001851 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001852 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001853 }
1854
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001855 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001856 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001857 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001858 final Intent intent = r.intent;
1859 final int callingUid = r.launchedFromUid;
1860
Chong Zhang0fa656b2015-08-31 15:17:21 -07001861 boolean overrideBounds = false;
1862 Rect newBounds = null;
1863 if (r.info.resizeable || (inTask != null && inTask.mResizeable)) {
1864 if (intent.hasExtra(ActivityOptions.KEY_BOUNDS)) {
1865 overrideBounds = true;
1866 newBounds = Rect.unflattenFromString(
1867 intent.getStringExtra(ActivityOptions.KEY_BOUNDS));
1868 } else if (options != null) {
1869 ActivityOptions opts = new ActivityOptions(options);
1870 if (opts.hasBounds()) {
1871 overrideBounds = true;
1872 newBounds = opts.getBounds();
1873 }
1874 }
1875 }
1876
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001877 // In some flows in to this function, we retrieve the task record and hold on to it
1878 // without a lock before calling back in to here... so the task at this point may
1879 // not actually be in recents. Check for that, and if it isn't in recents just
1880 // consider it invalid.
1881 if (inTask != null && !inTask.inRecents) {
1882 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1883 inTask = null;
1884 }
1885
Craig Mautnerad400af2014-08-13 16:41:36 -07001886 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001887 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1888 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001889
Craig Mautnera228ae92014-07-09 05:44:55 -07001890 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001891 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001892 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001893 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1894 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1895 "\"singleInstance\" or \"singleTask\"");
1896 launchFlags &=
1897 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1898 } else {
1899 switch (r.info.documentLaunchMode) {
1900 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1901 break;
1902 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1903 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1904 break;
1905 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1906 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1907 break;
1908 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1909 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1910 break;
1911 }
1912 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001913
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001914 final boolean launchTaskBehind = r.mLaunchTaskBehind
1915 && !launchSingleTask && !launchSingleInstance
1916 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001917
Wale Ogunwale7d701172015-03-11 15:36:30 -07001918 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1919 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001920 // For whatever reason this activity is being launched into a new
1921 // task... yet the caller has requested a result back. Well, that
1922 // is pretty messed up, so instead immediately send back a cancel
1923 // and let the new task continue launched as normal without a
1924 // dependency on its originator.
1925 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1926 r.resultTo.task.stack.sendActivityResultLocked(-1,
1927 r.resultTo, r.resultWho, r.requestCode,
1928 Activity.RESULT_CANCELED, null);
1929 r.resultTo = null;
1930 }
1931
1932 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1933 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1934 }
1935
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001936 // If we are actually going to launch in to a new task, there are some cases where
1937 // we further want to do multiple task.
1938 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1939 if (launchTaskBehind
1940 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1941 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1942 }
1943 }
1944
Craig Mautner8849a5e2013-04-02 16:41:03 -07001945 // We'll invoke onUserLeaving before onPause only if the launching
1946 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001947 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001948 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1949 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001950
1951 // If the caller has asked not to resume at this point, we make note
1952 // of this in the record so that we can skip it when trying to find
1953 // the top running activity.
Chong Zhang45c25ce2015-08-10 22:18:26 -07001954 if (!doResume || !okToShowLocked(r)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001955 r.delayedResume = true;
Chong Zhang45c25ce2015-08-10 22:18:26 -07001956 doResume = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001957 }
1958
Craig Mautnera254cd72014-05-25 16:47:39 -07001959 ActivityRecord notTop =
1960 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001961
1962 // If the onlyIfNeeded flag is set, then we can do this if the activity
1963 // being launched is the same as the one making the call... or, as
1964 // a special case, if we do not know the caller then we count the
1965 // current top activity as the caller.
1966 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1967 ActivityRecord checkedCaller = sourceRecord;
1968 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001969 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001970 }
1971 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1972 // Caller is not the same as launcher, so always needed.
1973 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1974 }
1975 }
1976
Craig Mautner8849a5e2013-04-02 16:41:03 -07001977 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001978 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001979
1980 // If the caller is not coming from another activity, but has given us an
1981 // explicit task into which they would like us to launch the new activity,
1982 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001983 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1984 final Intent baseIntent = inTask.getBaseIntent();
1985 final ActivityRecord root = inTask.getRootActivity();
1986 if (baseIntent == null) {
1987 ActivityOptions.abort(options);
1988 throw new IllegalArgumentException("Launching into task without base intent: "
1989 + inTask);
1990 }
1991
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001992 // If this task is empty, then we are adding the first activity -- it
1993 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001994 if (launchSingleInstance || launchSingleTask) {
1995 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1996 ActivityOptions.abort(options);
1997 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1998 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001999 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002000 if (root != null) {
2001 ActivityOptions.abort(options);
2002 throw new IllegalArgumentException("Caller with inTask " + inTask
2003 + " has root " + root + " but target is singleInstance/Task");
2004 }
2005 }
2006
2007 // If task is empty, then adopt the interesting intent launch flags in to the
2008 // activity being started.
2009 if (root == null) {
2010 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
2011 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
2012 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
2013 launchFlags = (launchFlags&~flagsOfInterest)
2014 | (baseIntent.getFlags()&flagsOfInterest);
2015 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002016 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07002017 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002018
Dianne Hackborn962d5352014-08-29 16:27:40 -07002019 // If the task is not empty and the caller is asking to start it as the root
2020 // of a new task, then we don't actually want to start this on the task. We
2021 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002022 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07002023 addingToTask = false;
2024
2025 } else {
2026 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002027 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07002028
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002029 reuseTask = inTask;
2030 } else {
2031 inTask = null;
2032 }
2033
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002034 if (inTask == null) {
2035 if (sourceRecord == null) {
2036 // This activity is not being started from another... in this
2037 // case we -always- start a new task.
2038 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
2039 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
2040 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2041 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2042 }
2043 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2044 // The original activity who is starting us is running as a single
2045 // instance... this new activity it is starting must go on its
2046 // own task.
2047 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2048 } else if (launchSingleInstance || launchSingleTask) {
2049 // The activity being started is a single instance... it always
2050 // gets launched into its own task.
2051 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2052 }
2053 }
2054
2055 ActivityInfo newTaskInfo = null;
2056 Intent newTaskIntent = null;
2057 ActivityStack sourceStack;
2058 if (sourceRecord != null) {
2059 if (sourceRecord.finishing) {
2060 // If the source is finishing, we can't further count it as our source. This
2061 // is because the task it is associated with may now be empty and on its way out,
2062 // so we don't want to blindly throw it in to that task. Instead we will take
2063 // the NEW_TASK flow and try to find a task for it. But save the task information
2064 // so it can be used when creating the new task.
2065 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2066 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2067 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2068 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2069 newTaskInfo = sourceRecord.info;
2070 newTaskIntent = sourceRecord.task.intent;
2071 }
2072 sourceRecord = null;
2073 sourceStack = null;
2074 } else {
2075 sourceStack = sourceRecord.task.stack;
2076 }
2077 } else {
2078 sourceStack = null;
2079 }
2080
2081 boolean movedHome = false;
2082 ActivityStack targetStack;
2083
2084 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002085 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002086
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002087 // We may want to try to place the new activity in to an existing task. We always
2088 // do this if the target activity is singleTask or singleInstance; we will also do
2089 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2090 // us to still place it in a new task: multi task, always doc mode, or being asked to
2091 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002092 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2093 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002094 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002095 // If bring to front is requested, and no result is requested and we have not
2096 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002097 // we can find a task that was started with this same
2098 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002099 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002100 // See if there is a task to bring to the front. If this is
2101 // a SINGLE_INSTANCE activity, there can be one and only one
2102 // instance of it in the history, and it is always in its own
2103 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002104 ActivityRecord intentActivity = !launchSingleInstance ?
2105 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002106 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002107 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2108 // but still needs to be a lock task mode violation since the task gets
2109 // cleared out and the device would otherwise leave the locked task.
2110 if (isLockTaskModeViolation(intentActivity.task,
2111 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2112 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002113 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002114 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002115 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2116 }
Craig Mautner29219d92013-04-16 20:19:12 -07002117 if (r.task == null) {
2118 r.task = intentActivity.task;
2119 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002120 if (intentActivity.task.intent == null) {
2121 // This task was started because of movement of
2122 // the activity based on affinity... now that we
2123 // are actually launching it, we can assign the
2124 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002125 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002126 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002127 targetStack = intentActivity.task.stack;
2128 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002129 // If the target task is not in the front, then we need
2130 // to bring it to the front... except... well, with
2131 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2132 // to have the same behavior as if a new instance was
2133 // being started, which means not bringing it to the front
2134 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002135 final ActivityStack focusStack = getFocusedStack();
2136 ActivityRecord curTop = (focusStack == null)
2137 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002138 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002139 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002140 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002141 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002142 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2143 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002144 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002145 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002146 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2147 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002148 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002149 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002150 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002151 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002152 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002153 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2154 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002155 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002156 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002157 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002158 options = null;
2159 }
2160 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002161 if (!movedToFront && doResume) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002162 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002163 + " from " + intentActivity);
2164 targetStack.moveToFront("intentActivityFound");
2165 }
2166
Craig Mautner8849a5e2013-04-02 16:41:03 -07002167 // If the caller has requested that the target task be
2168 // reset, then do so.
2169 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2170 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2171 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002172 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002173 // We don't need to start a new activity, and
2174 // the client said not to do anything if that
2175 // is the case, so this is it! And for paranoia, make
2176 // sure we have correctly resumed the top activity.
2177 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002178 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002179
2180 // Make sure to notify Keyguard as well if we are not running an app
2181 // transition later.
2182 if (!movedToFront) {
2183 notifyActivityDrawnForKeyguard();
2184 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002185 } else {
2186 ActivityOptions.abort(options);
2187 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002188 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002189 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2190 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002191 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002192 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002193 // The caller has requested to completely replace any
2194 // existing task with its new activity. Well that should
2195 // not be too hard...
2196 reuseTask = intentActivity.task;
2197 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002198 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002199 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002200 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002201 // In this situation we want to remove all activities
2202 // from the task up to the one being started. In most
2203 // cases this means we are resetting the task to its
2204 // initial state.
2205 ActivityRecord top =
2206 intentActivity.task.performClearTaskLocked(r, launchFlags);
2207 if (top != null) {
2208 if (top.frontOfTask) {
2209 // Activity aliases may mean we use different
2210 // intents for the top activity, so make sure
2211 // the task now has the identity of the new
2212 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002213 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002214 }
2215 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2216 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002217 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002218 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002219 // A special case: we need to start the activity because it is not
2220 // currently running, and the caller has asked to clear the current
2221 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002222 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002223 // Now pretend like this activity is being started by the top of its
2224 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002225 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002226 TaskRecord task = sourceRecord.task;
2227 if (task != null && task.stack == null) {
2228 // Target stack got cleared when we all activities were removed
2229 // above. Go ahead and reset it.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002230 targetStack = computeStackFocus(
2231 sourceRecord, false /* newTask */, null /* bounds */);
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002232 targetStack.addTask(
2233 task, !launchTaskBehind /* toTop */, false /* moving */);
2234 }
2235
Craig Mautner8849a5e2013-04-02 16:41:03 -07002236 }
2237 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2238 // In this case the top activity on the task is the
2239 // same as the one being launched, so we take that
2240 // as a request to bring the task to the foreground.
2241 // If the top activity in the task is the root
2242 // activity, deliver this new intent to it if it
2243 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002244 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002245 && intentActivity.realActivity.equals(r.realActivity)) {
2246 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2247 intentActivity.task);
2248 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002249 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002250 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002251 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2252 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002253 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2254 // In this case we are launching the root activity
2255 // of the task, but with a different intent. We
2256 // should start a new instance on top.
2257 addingToTask = true;
2258 sourceRecord = intentActivity;
2259 }
2260 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2261 // In this case an activity is being launched in to an
2262 // existing task, without resetting that task. This
2263 // is typically the situation of launching an activity
2264 // from a notification or shortcut. We want to place
2265 // the new activity on top of the current task.
2266 addingToTask = true;
2267 sourceRecord = intentActivity;
2268 } else if (!intentActivity.task.rootWasReset) {
2269 // In this case we are launching in to an existing task
2270 // that has not yet been started from its front door.
2271 // The current task has been brought to the front.
2272 // Ideally, we'd probably like to place this new task
2273 // at the bottom of its stack, but that's a little hard
2274 // to do with the current organization of the code so
2275 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002276 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002277 }
2278 if (!addingToTask && reuseTask == null) {
2279 // We didn't do anything... but it was needed (a.k.a., client
2280 // don't use that intent!) And for paranoia, make
2281 // sure we have correctly resumed the top activity.
2282 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002283 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002284 if (!movedToFront) {
2285 // Make sure to notify Keyguard as well if we are not running an app
2286 // transition later.
2287 notifyActivityDrawnForKeyguard();
2288 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002289 } else {
2290 ActivityOptions.abort(options);
2291 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002292 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002293 return ActivityManager.START_TASK_TO_FRONT;
2294 }
2295 }
2296 }
2297 }
2298
2299 //String uri = r.intent.toURI();
2300 //Intent intent2 = new Intent(uri);
2301 //Slog.i(TAG, "Given intent: " + r.intent);
2302 //Slog.i(TAG, "URI is: " + uri);
2303 //Slog.i(TAG, "To intent: " + intent2);
2304
2305 if (r.packageName != null) {
2306 // If the activity being launched is the same as the one currently
2307 // at the top, then we need to check if it should only be launched
2308 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002309 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002310 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002311 if (top != null && r.resultTo == null) {
2312 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2313 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002314 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002315 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002316 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2317 top.task);
2318 // For paranoia, make sure we have correctly
2319 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002320 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002321 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002322 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002323 }
2324 ActivityOptions.abort(options);
2325 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2326 // We don't need to start a new activity, and
2327 // the client said not to do anything if that
2328 // is the case, so this is it!
2329 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2330 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002331 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002332 return ActivityManager.START_DELIVERED_TO_TOP;
2333 }
2334 }
2335 }
2336 }
2337
2338 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002339 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002340 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2341 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002342 }
2343 ActivityOptions.abort(options);
2344 return ActivityManager.START_CLASS_NOT_FOUND;
2345 }
2346
2347 boolean newTask = false;
2348 boolean keepCurTransition = false;
2349
Craig Mautnerbb742462014-07-07 15:28:55 -07002350 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002351 sourceRecord.task : null;
2352
Craig Mautner8849a5e2013-04-02 16:41:03 -07002353 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002354 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002355 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002356 newTask = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -07002357 targetStack = computeStackFocus(r, newTask, newBounds);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002358 if (doResume) {
2359 targetStack.moveToFront("startingNewTask");
2360 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002361
Craig Mautner8849a5e2013-04-02 16:41:03 -07002362 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002363 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2364 newTaskInfo != null ? newTaskInfo : r.info,
2365 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002366 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2367 taskToAffiliate);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002368 if (overrideBounds) {
2369 r.task.updateOverrideConfiguration(newBounds);
2370 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002371 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2372 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002373 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002374 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002375 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002376 if (isLockTaskModeViolation(r.task)) {
2377 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2378 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2379 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002380 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002381 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002382 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2383 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002384 // Caller wants to appear on home activity, so before starting
2385 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002386 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002387 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002388 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002389 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002390 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002391 if (isLockTaskModeViolation(sourceTask)) {
2392 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2393 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2394 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002395 targetStack = sourceTask.stack;
Chong Zhang45c25ce2015-08-10 22:18:26 -07002396 if (doResume) {
2397 targetStack.moveToFront("sourceStackToFront");
2398 }
Craig Mautner737fae22014-10-15 12:52:10 -07002399 final TaskRecord topTask = targetStack.topTask();
2400 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002401 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002402 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002403 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002404 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002405 // In this case, we are adding the activity to an existing
2406 // task, but the caller has asked to clear that task if the
2407 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002408 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002409 keepCurTransition = true;
2410 if (top != null) {
2411 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002412 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002413 // For paranoia, make sure we have correctly
2414 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002415 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002416 if (doResume) {
2417 targetStack.resumeTopActivityLocked(null);
2418 }
2419 ActivityOptions.abort(options);
2420 return ActivityManager.START_DELIVERED_TO_TOP;
2421 }
2422 } else if (!addingToTask &&
2423 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2424 // In this case, we are launching an activity in our own task
2425 // that may already be running somewhere in the history, and
2426 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002427 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002428 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002429 final TaskRecord task = top.task;
2430 task.moveActivityToFrontLocked(top);
2431 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002432 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002433 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002434 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002435 if (doResume) {
2436 targetStack.resumeTopActivityLocked(null);
2437 }
2438 return ActivityManager.START_DELIVERED_TO_TOP;
2439 }
2440 }
2441 // An existing activity is starting this new activity, so we want
2442 // to keep the new one in the same task as the one that is starting
2443 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002444 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002445 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002446 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002447
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002448 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002449 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002450 // task it has provided to us.
2451 if (isLockTaskModeViolation(inTask)) {
2452 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2453 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2454 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002455 if (overrideBounds) {
2456 inTask.updateOverrideConfiguration(newBounds);
2457 int stackId = inTask.getLaunchStackId();
2458 if (stackId != inTask.stack.mStackId) {
2459 moveTaskToStackUncheckedLocked(
2460 inTask, stackId, ON_TOP, !FORCE_FOCUS, "inTaskToFront");
2461 }
2462 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002463 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002464 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2465 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002466
2467 // Check whether we should actually launch the new activity in to the task,
2468 // or just reuse the current activity on top.
2469 ActivityRecord top = inTask.getTopActivity();
2470 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2471 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2472 || launchSingleTop || launchSingleTask) {
2473 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2474 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2475 // We don't need to start a new activity, and
2476 // the client said not to do anything if that
2477 // is the case, so this is it!
2478 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2479 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002480 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002481 return ActivityManager.START_DELIVERED_TO_TOP;
2482 }
2483 }
2484
Dianne Hackborn962d5352014-08-29 16:27:40 -07002485 if (!addingToTask) {
2486 // We don't actually want to have this activity added to the task, so just
2487 // stop here but still tell the caller that we consumed the intent.
2488 ActivityOptions.abort(options);
2489 return ActivityManager.START_TASK_TO_FRONT;
2490 }
2491
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002492 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002493 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002494 + " in explicit task " + r.task);
2495
Craig Mautner8849a5e2013-04-02 16:41:03 -07002496 } else {
2497 // This not being started from an existing activity, and not part
2498 // of a new task... just put it in the top task, though these days
2499 // this case should never happen.
Chong Zhang0fa656b2015-08-31 15:17:21 -07002500 targetStack = computeStackFocus(r, newTask, null /* bounds */);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002501 if (doResume) {
2502 targetStack.moveToFront("addingToTopTask");
2503 }
Craig Mautner1602ec22013-05-12 10:24:27 -07002504 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002505 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002506 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002507 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002508 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002509 + " in new guessed " + r.task);
2510 }
2511
2512 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002513 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002514
Craig Mautner76e2a762014-06-24 09:05:43 -07002515 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2516 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2517 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002518 if (newTask) {
2519 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2520 }
2521 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002522 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002523 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002524 if (!launchTaskBehind && doResume) {
Craig Mautner299f9602015-01-26 09:47:33 -08002525 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002526 }
Chong Zhang45c25ce2015-08-10 22:18:26 -07002527 updateUserStackLocked(r.userId, targetStack);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002528 return ActivityManager.START_SUCCESS;
2529 }
2530
Craig Mautneree36c772014-07-16 14:56:05 -07002531 final void doPendingActivityLaunchesLocked(boolean doResume) {
2532 while (!mPendingActivityLaunches.isEmpty()) {
2533 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002534 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002535 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002536 }
2537 }
2538
Craig Mautner7f13ed32014-07-28 14:00:35 -07002539 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002540 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2541 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002542 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002543 mPendingActivityLaunches.remove(palNdx);
2544 }
2545 }
2546 }
2547
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002548 void setLaunchSource(int uid) {
2549 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2550 }
2551
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002552 void acquireLaunchWakelock() {
2553 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2554 throw new IllegalStateException("Calling must be system uid");
2555 }
2556 mLaunchingActivity.acquire();
2557 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2558 // To be safe, don't allow the wake lock to be held for too long.
2559 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2560 }
2561 }
2562
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002563 /**
2564 * Called when the frontmost task is idle.
2565 * @return the state of mService.mBooting before this was called.
2566 */
2567 private boolean checkFinishBootingLocked() {
2568 final boolean booting = mService.mBooting;
2569 boolean enableScreen = false;
2570 mService.mBooting = false;
2571 if (!mService.mBooted) {
2572 mService.mBooted = true;
2573 enableScreen = true;
2574 }
2575 if (booting || enableScreen) {
2576 mService.postFinishBooting(booting, enableScreen);
2577 }
2578 return booting;
2579 }
2580
Craig Mautnerf3333272013-04-22 10:55:53 -07002581 // Checked.
2582 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2583 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002584 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002585
Craig Mautnerf3333272013-04-22 10:55:53 -07002586 ArrayList<ActivityRecord> stops = null;
2587 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002588 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002589 int NS = 0;
2590 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002591 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002592 boolean activityRemoved = false;
2593
Wale Ogunwale7d701172015-03-11 15:36:30 -07002594 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002595 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002596 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2597 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002598 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2599 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002600 if (fromTimeout) {
2601 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002603
2604 // This is a hack to semi-deal with a race condition
2605 // in the client where it can be constructed with a
2606 // newer configuration from when we asked it to launch.
2607 // We'll update with whatever configuration it now says
2608 // it used to launch.
2609 if (config != null) {
2610 r.configuration = config;
2611 }
2612
2613 // We are now idle. If someone is waiting for a thumbnail from
2614 // us, we can now deliver.
2615 r.idle = true;
2616
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002617 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002618 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002619 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002620 }
2621 }
2622
2623 if (allResumedActivitiesIdle()) {
2624 if (r != null) {
2625 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002626 }
2627
2628 if (mLaunchingActivity.isHeld()) {
2629 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2630 if (VALIDATE_WAKE_LOCK_CALLER &&
2631 Binder.getCallingUid() != Process.myUid()) {
2632 throw new IllegalStateException("Calling must be system uid");
2633 }
2634 mLaunchingActivity.release();
2635 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002636 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerf3333272013-04-22 10:55:53 -07002637 }
2638
2639 // Atomically retrieve all of the other things to do.
2640 stops = processStoppingActivitiesLocked(true);
2641 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002642 if ((NF = mFinishingActivities.size()) > 0) {
2643 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002644 mFinishingActivities.clear();
2645 }
2646
Craig Mautnerf3333272013-04-22 10:55:53 -07002647 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002648 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002649 mStartingUsers.clear();
2650 }
2651
Craig Mautnerf3333272013-04-22 10:55:53 -07002652 // Stop any activities that are scheduled to do so but have been
2653 // waiting for the next one to start.
2654 for (int i = 0; i < NS; i++) {
2655 r = stops.get(i);
2656 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002657 if (stack != null) {
2658 if (r.finishing) {
2659 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2660 } else {
2661 stack.stopActivityLocked(r);
2662 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002663 }
2664 }
2665
2666 // Finish any activities that are scheduled to do so but have been
2667 // waiting for the next one to start.
2668 for (int i = 0; i < NF; i++) {
2669 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002670 final ActivityStack stack = r.task.stack;
2671 if (stack != null) {
2672 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2673 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002674 }
2675
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002676 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002677 // Complete user switch
2678 if (startingUsers != null) {
2679 for (int i = 0; i < startingUsers.size(); i++) {
2680 mService.finishUserSwitch(startingUsers.get(i));
2681 }
2682 }
2683 // Complete starting up of background users
2684 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002685 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002686 mStartingBackgroundUsers.clear();
2687 for (int i = 0; i < startingUsers.size(); i++) {
2688 mService.finishUserBoot(startingUsers.get(i));
2689 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002690 }
2691 }
2692
2693 mService.trimApplications();
2694 //dump();
2695 //mWindowManager.dump();
2696
Craig Mautnerf3333272013-04-22 10:55:53 -07002697 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002698 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002699 }
2700
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002701 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002702 }
2703
Craig Mautner8e569572013-10-11 17:36:59 -07002704 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002705 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002706 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2707 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002708 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2709 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2710 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002711 }
Craig Mautner19091252013-10-05 00:03:53 -07002712 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002713 }
2714
2715 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002716 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2717 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002718 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2719 stacks.get(stackNdx).closeSystemDialogsLocked();
2720 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002721 }
2722 }
2723
Craig Mautner93529a42013-10-04 15:03:13 -07002724 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002725 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002726 }
2727
Craig Mautner8d341ef2013-03-26 09:03:27 -07002728 /**
Chong Zhang45c25ce2015-08-10 22:18:26 -07002729 * Update the last used stack id for non-current user (current user's last
2730 * used stack is the focused stack)
2731 */
2732 void updateUserStackLocked(int userId, ActivityStack stack) {
2733 if (userId != mCurrentUser) {
2734 mUserStackInFront.put(userId, stack != null ? stack.getStackId() : HOME_STACK_ID);
2735 }
2736 }
2737
2738 /**
Craig Mautner8d341ef2013-03-26 09:03:27 -07002739 * @return true if some activity was finished (or would have finished if doit were true).
2740 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002741 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2742 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002743 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002744 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2745 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002746 final int numStacks = stacks.size();
2747 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2748 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002749 if (stack.finishDisabledPackageActivitiesLocked(
2750 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002751 didSomething = true;
2752 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002753 }
2754 }
2755 return didSomething;
2756 }
2757
Dianne Hackborna413dc02013-07-12 12:02:55 -07002758 void updatePreviousProcessLocked(ActivityRecord r) {
2759 // Now that this process has stopped, we may want to consider
2760 // it to be the previous app to try to keep around in case
2761 // the user wants to return to it.
2762
2763 // First, found out what is currently the foreground app, so that
2764 // we don't blow away the previous app if this activity is being
2765 // hosted by the process that is actually still the foreground.
2766 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002767 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2768 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002769 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2770 final ActivityStack stack = stacks.get(stackNdx);
2771 if (isFrontStack(stack)) {
2772 if (stack.mResumedActivity != null) {
2773 fgApp = stack.mResumedActivity.app;
2774 } else if (stack.mPausingActivity != null) {
2775 fgApp = stack.mPausingActivity.app;
2776 }
2777 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002778 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002779 }
2780 }
2781
2782 // Now set this one as the previous process, only if that really
2783 // makes sense to.
2784 if (r.app != null && fgApp != null && r.app != fgApp
2785 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002786 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002787 mService.mPreviousProcess = r.app;
2788 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2789 }
2790 }
2791
Craig Mautner05d29032013-05-03 13:40:13 -07002792 boolean resumeTopActivitiesLocked() {
2793 return resumeTopActivitiesLocked(null, null, null);
2794 }
2795
2796 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2797 Bundle targetOptions) {
2798 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002799 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002800 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002801 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002802 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002803 if (isFrontStack(targetStack)) {
2804 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2805 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002806
Craig Mautnere0a38842013-12-16 16:14:02 -08002807 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2808 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002809 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2810 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002811 if (stack == targetStack) {
2812 // Already started above.
2813 continue;
2814 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002815 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002816 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002817 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002818 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002819 }
Craig Mautner05d29032013-05-03 13:40:13 -07002820 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002821 }
2822
Todd Kennedy539db512014-12-15 09:57:55 -08002823 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002824 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2825 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002826 final int numStacks = stacks.size();
2827 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2828 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002829 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002830 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002831 }
2832 }
2833
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002834 void finishVoiceTask(IVoiceInteractionSession session) {
2835 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2836 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2837 final int numStacks = stacks.size();
2838 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2839 final ActivityStack stack = stacks.get(stackNdx);
2840 stack.finishVoiceTask(session);
2841 }
2842 }
2843 }
2844
Craig Mautner299f9602015-01-26 09:47:33 -08002845 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002846 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2847 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002848 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002849 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2850 // Caller wants the home activity moved with it. To accomplish this,
2851 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002852 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002853 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002854 if (task.stack == null) {
2855 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2856 + task + " to front. Stack is null");
2857 return;
2858 }
Chong Zhang0fa656b2015-08-31 15:17:21 -07002859
2860 int stackId = task.stack.mStackId;
2861 if (task.mResizeable && options != null) {
2862 ActivityOptions opts = new ActivityOptions(options);
2863 if (opts.hasBounds()) {
2864 Rect bounds = opts.getBounds();
2865 task.updateOverrideConfiguration(bounds);
2866 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, false);
2867 stackId = task.getLaunchStackId();
2868 }
2869 }
2870
2871 if (stackId != task.stack.mStackId) {
2872 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, FORCE_FOCUS, reason);
2873 } else {
2874 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002875 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2876 reason);
Chong Zhang0fa656b2015-08-31 15:17:21 -07002877 }
2878
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002879 if (DEBUG_STACK) Slog.d(TAG_STACK,
2880 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002881 }
2882
Craig Mautner967212c2013-04-13 21:10:58 -07002883 ActivityStack getStack(int stackId) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07002884 return getStack(stackId, !CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002885 }
2886
2887 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002888 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2889 if (activityContainer != null) {
2890 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002891 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002892 if (!createStaticStackIfNeeded
2893 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2894 return null;
2895 }
2896 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002897 }
2898
Craig Mautner967212c2013-04-13 21:10:58 -07002899 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002900 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002901 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2902 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002903 }
2904 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002905 }
2906
Craig Mautner4a1cb222013-12-04 16:14:06 -08002907 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002908 ActivityRecord homeActivity = getHomeActivity();
2909 if (homeActivity != null) {
2910 return homeActivity.appToken;
2911 }
2912 return null;
2913 }
2914
2915 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002916 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002917 }
2918
2919 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002920 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2921 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2922 final TaskRecord task = tasks.get(taskNdx);
2923 if (task.isHomeTask()) {
2924 final ArrayList<ActivityRecord> activities = task.mActivities;
2925 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2926 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002927 if (r.isHomeActivity()
2928 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002929 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002930 }
2931 }
2932 }
2933 }
2934 return null;
2935 }
2936
Todd Kennedyca4d8422015-01-15 15:19:22 -08002937 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002938 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002939 ActivityContainer activityContainer =
2940 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002941 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002942 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2943 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002944 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002945 return activityContainer;
2946 }
2947
Craig Mautner34b73df2014-01-12 21:11:08 -08002948 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002949 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2950 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2951 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002952 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2953 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002954 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002955 }
2956 }
2957
Craig Mautner95da1082014-02-24 17:54:35 -08002958 void deleteActivityContainer(IActivityContainer container) {
2959 ActivityContainer activityContainer = (ActivityContainer)container;
2960 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002961 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2962 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002963 final int stackId = activityContainer.mStackId;
2964 mActivityContainers.remove(stackId);
2965 mWindowManager.removeStack(stackId);
2966 }
2967 }
2968
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002969 void resizeStackLocked(int stackId, Rect bounds, boolean preserveWindows) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002970 final ActivityStack stack = getStack(stackId);
2971 if (stack == null) {
2972 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2973 return;
2974 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002975
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002976 ActivityRecord r = stack.topRunningActivityLocked(null);
2977 final boolean resizeTasks = r != null && r.task.mResizeable;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002978
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002979 mTmpBounds.clear();
2980 mTmpConfigs.clear();
2981 if (resizeTasks) {
2982 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2983 for (int i = tasks.size() - 1; i >= 0; i--) {
2984 TaskRecord task = tasks.get(i);
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07002985 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
2986 // For freeform stack we don't adjust the size of the tasks to match that of
2987 // the stack, but we do try to make sure the tasks are still contained with the
2988 // bounds of the stack.
2989 tempRect2.set(task.mBounds);
2990 fitWithinBounds(tempRect2, bounds);
2991 task.updateOverrideConfiguration(tempRect2);
2992 } else {
2993 task.updateOverrideConfiguration(bounds);
2994 }
2995
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07002996 mTmpConfigs.put(task.taskId, task.mOverrideConfig);
2997 mTmpBounds.put(task.taskId, task.mBounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002998 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002999 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003000 stack.mFullscreen = mWindowManager.resizeStack(stackId, bounds, resizeTasks, mTmpConfigs,
3001 mTmpBounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003002 if (stack.mStackId == DOCKED_STACK_ID) {
3003 // Dock stack funness...Yay!
3004 if (stack.mFullscreen) {
3005 // The dock stack went fullscreen which is kinda like dismissing it.
3006 // In this case we make all other static stacks fullscreen and move all
3007 // docked stack tasks to the fullscreen stack.
3008 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3009 if (i != DOCKED_STACK_ID) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003010 resizeStackLocked(i, null, preserveWindows);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003011 }
3012 }
3013
3014 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
3015 final int count = tasks.size();
3016 for (int i = 0; i < count; i++) {
3017 moveTaskToStackLocked(tasks.get(i).taskId,
3018 FULLSCREEN_WORKSPACE_STACK_ID, ON_TOP, FORCE_FOCUS);
3019 }
3020
3021 // stack shouldn't contain anymore activities, so nothing to resume.
3022 r = null;
3023 } else {
3024 // Docked stacks occupy a dedicated region on screen so the size of all other
3025 // static stacks need to be adjusted so they don't overlap with the docked stack.
3026 final int leftChange = stack.mBounds.left - bounds.left;
3027 final int rightChange = stack.mBounds.right - bounds.right;
3028 final int topChange = stack.mBounds.top - bounds.top;
3029 final int bottomChange = stack.mBounds.bottom - bounds.bottom;
3030
3031 for (int i = FIRST_STATIC_STACK_ID; i <= LAST_STATIC_STACK_ID; i++) {
3032 if (i != DOCKED_STACK_ID) {
3033 ActivityStack otherStack = getStack(i);
3034 if (otherStack != null) {
3035 tempRect.set(otherStack.mBounds);
3036 // We adjust the opposing sides of the other stacks to
3037 // the side in the dock stack that changed.
3038 tempRect.left -= rightChange;
3039 tempRect.right -= leftChange;
3040 tempRect.top -= bottomChange;
3041 tempRect.bottom -= topChange;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003042 resizeStackLocked(i, tempRect, PRESERVE_WINDOWS);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003043 }
3044 }
3045 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003046 }
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003047 // Since we are resizing the stack, all other operations should strive to preserve
3048 // windows.
3049 preserveWindows = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003050 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003051 stack.setBounds(bounds);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003052
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003053 if (r != null) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003054 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003055 // And we need to make sure at this point that all other activities
3056 // are made visible with the correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003057 ensureActivitiesVisibleLocked(r, 0, preserveWindows);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003058 if (!updated) {
3059 resumeTopActivitiesLocked(stack, null, null);
3060 }
3061 }
3062 }
3063
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003064 void resizeTaskLocked(TaskRecord task, Rect bounds, boolean resizedByUser) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003065 if (!task.mResizeable) {
3066 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
3067 return;
3068 }
3069
3070 if (task.mBounds != null && task.mBounds.equals(bounds)) {
3071 // Nothing to do here...
3072 return;
3073 }
3074
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003075 if (!mWindowManager.isValidTaskId(task.taskId)) {
3076 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07003077 // All we can do for now is update the bounds so it can be used when the task is
3078 // added to window manager.
Chong Zhang0fa656b2015-08-31 15:17:21 -07003079 task.updateOverrideConfiguration(bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003080 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
3081 // re-restore the task so it can have the proper stack association.
Chong Zhang5dcb2752015-08-18 13:50:26 -07003082 restoreRecentTaskLocked(task, FREEFORM_WORKSPACE_STACK_ID);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003083 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003084 return;
3085 }
3086
Wale Ogunwale040b4702015-08-06 18:10:50 -07003087 // The stack of a task is determined by its size (fullscreen vs non-fullscreen).
3088 // Place the task in the right stack if it isn't there already based on the requested
3089 // bounds.
3090 int stackId = task.stack.mStackId;
3091 if (bounds == null && stackId != FULLSCREEN_WORKSPACE_STACK_ID) {
3092 stackId = FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003093 } else if (bounds != null
3094 && stackId != FREEFORM_WORKSPACE_STACK_ID && stackId != DOCKED_STACK_ID) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003095 stackId = FREEFORM_WORKSPACE_STACK_ID;
3096 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003097 final boolean changedStacks = stackId != task.stack.mStackId;
3098 if (changedStacks) {
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003099 moveTaskToStackUncheckedLocked(task, stackId, ON_TOP, !FORCE_FOCUS, "resizeTask");
Wale Ogunwale040b4702015-08-06 18:10:50 -07003100 }
3101
Filip Gruszczynskiaff7f132015-09-02 17:21:21 -07003102 final Configuration overrideConfig = task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003103 // This variable holds information whether the configuration didn't change in a signficant
3104 // way and the activity was kept the way it was. If it's false, it means the activity had
3105 // to be relaunched due to configuration change.
3106 boolean kept = true;
3107 if (overrideConfig != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003108 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003109 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003110 final ActivityStack stack = task.stack;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003111 final boolean preserveWindow = resizedByUser && !changedStacks;
3112 kept = stack.ensureActivityConfigurationLocked(r, 0, preserveWindow);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003113 // All other activities must be made visible with their correct configuration.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003114 ensureActivitiesVisibleLocked(r, 0, !PRESERVE_WINDOWS);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003115 if (!kept) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08003116 resumeTopActivitiesLocked(stack, null, null);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003117 if (changedStacks && stackId == FULLSCREEN_WORKSPACE_STACK_ID) {
3118 // We are about to relaunch the activity because its configuration changed
3119 // due to being maximized, i.e. size change. The activity will first
3120 // remove the old window and then add a new one. This call will tell window
3121 // manager about this, so it can preserve the old window until the new
3122 // one is drawn. This prevents having a gap between the removal and
3123 // addition, in which no window is visible. We also want the entrace of the
3124 // new window to be properly animated.
3125 mWindowManager.setReplacingWindow(r.appToken, true /* animate */);
3126 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08003127 }
3128 }
3129 }
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07003130 mWindowManager.resizeTask(task.taskId, bounds, task.mOverrideConfig, kept);
Wale Ogunwale60454db2015-01-23 16:05:07 -08003131 }
3132
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003133 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003134 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3135 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08003136 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003137 }
3138
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003139 ActivityContainer activityContainer = new ActivityContainer(stackId);
3140 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003141 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08003142 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003143 }
3144
3145 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07003146 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003147 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
3148 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07003149 break;
3150 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003151 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003152 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003153 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003154 }
3155
Chong Zhang5dcb2752015-08-18 13:50:26 -07003156 /**
3157 * Restores a recent task to a stack
3158 * @param task The recent task to be restored.
3159 * @param stackId The stack to restore the task to (default launch stack will be used
3160 * if stackId is invalid).
3161 * @return true if the task has been restored successfully.
3162 */
3163 private boolean restoreRecentTaskLocked(TaskRecord task, int stackId) {
3164 if (stackId == INVALID_STACK_ID) {
Chong Zhang0fa656b2015-08-31 15:17:21 -07003165 stackId = mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId();
Chong Zhang5dcb2752015-08-18 13:50:26 -07003166 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07003167 if (task.stack != null) {
3168 // Task has already been restored once. See if we need to do anything more
3169 if (task.stack.mStackId == stackId) {
3170 // Nothing else to do since it is already restored in the right stack.
3171 return true;
3172 }
3173 // Remove current stack association, so we can re-associate the task with the
3174 // right stack below.
Wale Ogunwale040b4702015-08-06 18:10:50 -07003175 task.stack.removeTask(task, "restoreRecentTaskLocked", MOVING);
Wale Ogunwale706ed792015-08-02 10:29:44 -07003176 }
3177
3178 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003179 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003180
3181 if (stack == null) {
3182 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003183 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3184 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003185 return false;
3186 }
3187
3188 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003189 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3190 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003191 final ArrayList<ActivityRecord> activities = task.mActivities;
3192 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07003193 stack.addConfigOverride(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003194 }
3195 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003196 }
3197
Wale Ogunwale040b4702015-08-06 18:10:50 -07003198 /**
3199 * Moves the specified task record to the input stack id.
3200 * WARNING: This method performs an unchecked/raw move of the task and
3201 * can leave the system in an unstable state if used incorrectly.
3202 * Use {@link #moveTaskToStackLocked} to perform safe task movement
3203 * to a stack.
3204 * @param task Task to move.
3205 * @param stackId Id of stack to move task to.
3206 * @param toTop True if the task should be placed at the top of the stack.
Chong Zhang02898352015-08-21 17:27:14 -07003207 * @param forceFocus if focus should be moved to the new stack
Wale Ogunwale040b4702015-08-06 18:10:50 -07003208 * @param reason Reason the task is been moved.
3209 * @return The stack the task was moved to.
3210 */
3211 private ActivityStack moveTaskToStackUncheckedLocked(
Chong Zhang02898352015-08-21 17:27:14 -07003212 TaskRecord task, int stackId, boolean toTop, boolean forceFocus, String reason) {
3213 final ActivityRecord r = task.getTopActivity();
3214 final boolean wasFocused = isFrontStack(task.stack) && (topRunningActivityLocked() == r);
3215 final boolean wasResumed = wasFocused && (task.stack.mResumedActivity == r);
3216
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003217 final ActivityStack stack = getStack(stackId, CREATE_IF_NEEDED, toTop);
Wale Ogunwale040b4702015-08-06 18:10:50 -07003218 mWindowManager.moveTaskToStack(task.taskId, stack.mStackId, toTop);
3219 if (task.stack != null) {
3220 task.stack.removeTask(task, reason, MOVING);
3221 }
3222 stack.addTask(task, toTop, MOVING);
Chong Zhang02898352015-08-21 17:27:14 -07003223
3224 // If the task had focus before (or we're requested to move focus),
3225 // move focus to the new stack.
3226 if (forceFocus || wasFocused) {
3227 // If the task owns the last resumed activity, transfer that together,
3228 // so that we don't resume the same activity again in the new stack.
3229 // Apps may depend on onResume()/onPause() being called in pairs.
3230 if (wasResumed) {
3231 stack.mResumedActivity = r;
3232 }
3233 // move the stack in which we are placing the task to the front.
3234 stack.moveToFront(reason);
3235 }
3236
Wale Ogunwale040b4702015-08-06 18:10:50 -07003237 return stack;
3238 }
3239
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003240 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop, boolean forceFocus) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003241 final TaskRecord task = anyTaskForIdLocked(taskId);
3242 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003243 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003244 return;
3245 }
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003246 final String reason = "moveTaskToStack";
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003247 final ActivityStack stack =
Chong Zhang02898352015-08-21 17:27:14 -07003248 moveTaskToStackUncheckedLocked(task, stackId, toTop, forceFocus, reason);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003249
3250 // Make sure the task has the appropriate bounds/size for the stack it is in.
3251 if (stackId == FULLSCREEN_WORKSPACE_STACK_ID && task.mBounds != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003252 resizeTaskLocked(task, stack.mBounds, false);
Wale Ogunwale868a5e12015-08-02 16:19:20 -07003253 } else if (stackId == FREEFORM_WORKSPACE_STACK_ID
3254 && task.mBounds == null && task.mLastNonFullscreenBounds != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003255 resizeTaskLocked(task, task.mLastNonFullscreenBounds, false);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003256 } else if (stackId == DOCKED_STACK_ID) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003257 resizeTaskLocked(task, stack.mBounds, false);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003258 }
3259
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003260 // The task might have already been running and its visibility needs to be synchronized with
3261 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003262 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautner05d29032013-05-03 13:40:13 -07003263 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003264 }
3265
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003266 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3267 final TaskRecord task = anyTaskForIdLocked(taskId);
3268 if (task == null) {
3269 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3270 return;
3271 }
3272 ActivityStack stack =
Wale Ogunwale040b4702015-08-06 18:10:50 -07003273 getStack(stackId, CREATE_IF_NEEDED, !ON_TOP);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003274 mWindowManager.positionTaskInStack(taskId, stackId, position);
3275 final boolean stackChanged = task.stack != null && task.stack != stack;
3276 if (stackChanged) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07003277 task.stack.removeTask(task, "moveTaskToStack", MOVING);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003278 }
3279 stack.positionTask(task, position, stackChanged);
3280 // The task might have already been running and its visibility needs to be synchronized with
3281 // the visibility of the stack / windows.
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003282 stack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003283 resumeTopActivitiesLocked();
3284 }
3285
Craig Mautnerac6f8432013-07-17 13:24:59 -07003286 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003287 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003288 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3289 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003290 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3291 final ActivityStack stack = stacks.get(stackNdx);
3292 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003293 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003294 continue;
3295 }
3296 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003297 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3298 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003299 continue;
3300 }
3301 final ActivityRecord ar = stack.findTaskLocked(r);
3302 if (ar != null) {
3303 return ar;
3304 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003305 }
3306 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003307 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003308 return null;
3309 }
3310
3311 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003312 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3313 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003314 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3315 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3316 if (ar != null) {
3317 return ar;
3318 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003319 }
3320 }
3321 return null;
3322 }
3323
Craig Mautner8d341ef2013-03-26 09:03:27 -07003324 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003325 scheduleSleepTimeout();
3326 if (!mGoingToSleep.isHeld()) {
3327 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003328 if (mLaunchingActivity.isHeld()) {
3329 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3330 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003331 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003332 mLaunchingActivity.release();
3333 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003334 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003335 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003336 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003337 }
3338
3339 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003340 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003341
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003342 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003343 final long endTime = System.currentTimeMillis() + timeout;
3344 while (true) {
3345 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003346 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3347 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003348 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3349 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3350 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003351 }
3352 if (cantShutdown) {
3353 long timeRemaining = endTime - System.currentTimeMillis();
3354 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003355 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003356 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003357 } catch (InterruptedException e) {
3358 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003359 } else {
3360 Slog.w(TAG, "Activity manager shutdown timed out");
3361 timedout = true;
3362 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003363 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003364 } else {
3365 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003366 }
3367 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003368
3369 // Force checkReadyForSleep to complete.
3370 mSleepTimeout = true;
3371 checkReadyForSleepLocked();
3372
Craig Mautner8d341ef2013-03-26 09:03:27 -07003373 return timedout;
3374 }
3375
3376 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003377 removeSleepTimeouts();
3378 if (mGoingToSleep.isHeld()) {
3379 mGoingToSleep.release();
3380 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003381 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3382 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003383 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3384 final ActivityStack stack = stacks.get(stackNdx);
3385 stack.awakeFromSleepingLocked();
3386 if (isFrontStack(stack)) {
3387 resumeTopActivitiesLocked();
3388 }
Craig Mautner5314a402013-09-26 12:40:16 -07003389 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003390 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003391 mGoingToSleepActivities.clear();
3392 }
3393
3394 void activitySleptLocked(ActivityRecord r) {
3395 mGoingToSleepActivities.remove(r);
3396 checkReadyForSleepLocked();
3397 }
3398
3399 void checkReadyForSleepLocked() {
3400 if (!mService.isSleepingOrShuttingDown()) {
3401 // Do not care.
3402 return;
3403 }
3404
3405 if (!mSleepTimeout) {
3406 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003407 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3408 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003409 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3410 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3411 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003412 }
3413
3414 if (mStoppingActivities.size() > 0) {
3415 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003416 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003417 + mStoppingActivities.size() + " activities");
3418 scheduleIdleLocked();
3419 dontSleep = true;
3420 }
3421
3422 if (mGoingToSleepActivities.size() > 0) {
3423 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003424 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003425 + mGoingToSleepActivities.size() + " activities");
3426 dontSleep = true;
3427 }
3428
3429 if (dontSleep) {
3430 return;
3431 }
3432 }
3433
Craig Mautnere0a38842013-12-16 16:14:02 -08003434 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3435 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003436 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3437 stacks.get(stackNdx).goToSleep();
3438 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003439 }
3440
3441 removeSleepTimeouts();
3442
3443 if (mGoingToSleep.isHeld()) {
3444 mGoingToSleep.release();
3445 }
3446 if (mService.mShuttingDown) {
3447 mService.notifyAll();
3448 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003449 }
3450
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003451 boolean reportResumedActivityLocked(ActivityRecord r) {
3452 final ActivityStack stack = r.task.stack;
3453 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003454 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003455 }
3456 if (allResumedActivitiesComplete()) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003457 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003458 mWindowManager.executeAppTransition();
3459 return true;
3460 }
3461 return false;
3462 }
3463
Craig Mautner8d341ef2013-03-26 09:03:27 -07003464 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003465 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3466 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003467 int stackNdx = stacks.size() - 1;
3468 while (stackNdx >= 0) {
3469 stacks.get(stackNdx).handleAppCrashLocked(app);
3470 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003471 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003472 }
3473 }
3474
Jose Lima4b6c6692014-08-12 17:41:12 -07003475 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003476 final ActivityStack stack = r.task.stack;
3477 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003478 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3479 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003480 return false;
3481 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003482 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003483 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3484 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003485
3486 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003487 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003488 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003489 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003490 return true;
3491 }
3492
3493 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003494 if (visible && top.fullscreen) {
3495 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003496 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3497 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3498 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3499 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003500 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003501 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3502 // Only the activity set as currently visible behind should actively reset its
3503 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003504 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3505 "requestVisibleBehind: returning visible=" + visible
3506 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3507 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003508 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003509 }
3510
Jose Lima4b6c6692014-08-12 17:41:12 -07003511 stack.setVisibleBehindActivity(visible ? r : null);
3512 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003513 // Make the activity immediately above r opaque.
3514 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3515 if (next != null) {
3516 mService.convertFromTranslucent(next.appToken);
3517 }
3518 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003519 if (top.app != null && top.app.thread != null) {
3520 // Notify the top app of the change.
3521 try {
3522 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3523 } catch (RemoteException e) {
3524 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003525 }
3526 return true;
3527 }
3528
Craig Mautnerbb742462014-07-07 15:28:55 -07003529 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3530 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3531 r.mLaunchTaskBehind = false;
3532 final TaskRecord task = r.task;
3533 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003534 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003535 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003536 mWindowManager.setAppVisibility(r.appToken, false);
3537 }
3538
3539 void scheduleLaunchTaskBehindComplete(IBinder token) {
3540 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3541 }
3542
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003543 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
3544 boolean preserveWindows) {
Craig Mautner580ea812013-04-25 12:58:38 -07003545 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08003546 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3547 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003548 final int topStackNdx = stacks.size() - 1;
3549 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3550 final ActivityStack stack = stacks.get(stackNdx);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07003551 stack.ensureActivitiesVisibleLocked(starting, configChanges, preserveWindows);
Craig Mautner580ea812013-04-25 12:58:38 -07003552 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003553 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003554 }
3555
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003556 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3557 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3558 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3559 final int topStackNdx = stacks.size() - 1;
3560 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3561 final ActivityStack stack = stacks.get(stackNdx);
3562 stack.clearOtherAppTimeTrackers(except);
3563 }
3564 }
3565 }
3566
Craig Mautner8d341ef2013-03-26 09:03:27 -07003567 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003568 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3569 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003570 final int numStacks = stacks.size();
3571 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3572 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003573 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003574 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003575 }
3576 }
3577
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003578 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3579 // Examine all activities currently running in the process.
3580 TaskRecord firstTask = null;
3581 // Tasks is non-null only if two or more tasks are found.
3582 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003583 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3584 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003585 ActivityRecord r = app.activities.get(i);
3586 // First, if we find an activity that is in the process of being destroyed,
3587 // then we just aren't going to do anything for now; we want things to settle
3588 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003589 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003590 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003591 return;
3592 }
3593 // Don't consider any activies that are currently not in a state where they
3594 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003595 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3596 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003597 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003598 continue;
3599 }
3600 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003601 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003602 + " from " + r);
3603 if (firstTask == null) {
3604 firstTask = r.task;
3605 } else if (firstTask != r.task) {
3606 if (tasks == null) {
3607 tasks = new ArraySet<>();
3608 tasks.add(firstTask);
3609 }
3610 tasks.add(r.task);
3611 }
3612 }
3613 }
3614 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003615 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003616 return;
3617 }
3618 // If we have activities in multiple tasks that are in a position to be destroyed,
3619 // let's iterate through the tasks and release the oldest one.
3620 final int numDisplays = mActivityDisplays.size();
3621 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3622 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3623 // Step through all stacks starting from behind, to hit the oldest things first.
3624 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3625 final ActivityStack stack = stacks.get(stackNdx);
3626 // Try to release activities in this stack; if we manage to, we are done.
3627 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3628 return;
3629 }
3630 }
3631 }
3632 }
3633
Amith Yamasani37a40c22015-06-17 13:25:42 -07003634 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003635 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003636 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003637 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003638
Craig Mautner858d8a62013-04-23 17:08:34 -07003639 mStartingUsers.add(uss);
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 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003643 final ActivityStack stack = stacks.get(stackNdx);
3644 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003645 TaskRecord task = stack.topTask();
3646 if (task != null) {
3647 mWindowManager.moveTaskToTop(task.taskId);
3648 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003649 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003650 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003651
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003652 ActivityStack stack = getStack(restoreStackId);
3653 if (stack == null) {
3654 stack = mHomeStack;
3655 }
3656 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003657 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003658 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003659 TaskRecord task = stack.topTask();
3660 if (task != null) {
3661 mWindowManager.moveTaskToTop(task.taskId);
3662 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003663 } else {
3664 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003665 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003666 }
Craig Mautner93529a42013-10-04 15:03:13 -07003667 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003668 }
3669
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003670 /**
3671 * Add background users to send boot completed events to.
3672 * @param userId The user being started in the background
3673 * @param uss The state object for the user.
3674 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003675 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003676 mStartingBackgroundUsers.add(uss);
3677 }
3678
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003679 /** Checks whether the userid is a profile of the current user. */
3680 boolean isCurrentProfileLocked(int userId) {
3681 if (userId == mCurrentUser) return true;
3682 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3683 if (mService.mCurrentProfileIds[i] == userId) return true;
3684 }
3685 return false;
3686 }
3687
Chong Zhang45c25ce2015-08-10 22:18:26 -07003688 /** Checks whether the activity should be shown for current user. */
3689 boolean okToShowLocked(ActivityRecord r) {
3690 return r != null && (isCurrentProfileLocked(r.userId)
3691 || (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0);
3692 }
3693
Craig Mautnerde4ef022013-04-07 19:01:33 -07003694 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003695 ArrayList<ActivityRecord> stops = null;
3696
3697 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003698 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3699 ActivityRecord s = mStoppingActivities.get(activityNdx);
3700 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003701 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003702 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3703 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003704 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003705 if (s.finishing) {
3706 // If this activity is finishing, it is sitting on top of
3707 // everyone else but we now know it is no longer needed...
3708 // so get rid of it. Otherwise, we need to go through the
3709 // normal flow and hide it once we determine that it is
3710 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003711 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003712 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003713 }
3714 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003715 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003716 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003717 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003718 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003719 }
3720 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003721 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003722 }
3723 }
3724
3725 return stops;
3726 }
3727
Craig Mautnercf910b02013-04-23 11:23:27 -07003728 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003729 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3730 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3731 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3732 final ActivityStack stack = stacks.get(stackNdx);
3733 final ActivityRecord r = stack.topRunningActivityLocked(null);
3734 final ActivityState state = r == null ? DESTROYED : r.state;
3735 if (isFrontStack(stack)) {
3736 if (r == null) Slog.e(TAG,
3737 "validateTop...: null top activity, stack=" + stack);
3738 else {
3739 final ActivityRecord pausing = stack.mPausingActivity;
3740 if (pausing != null && pausing == r) Slog.e(TAG,
3741 "validateTop...: top stack has pausing activity r=" + r
3742 + " state=" + state);
3743 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3744 "validateTop...: activity in front not resumed r=" + r
3745 + " state=" + state);
3746 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003747 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003748 final ActivityRecord resumed = stack.mResumedActivity;
3749 if (resumed != null && resumed == r) Slog.e(TAG,
3750 "validateTop...: back stack has resumed activity r=" + r
3751 + " state=" + state);
3752 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3753 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003754 }
3755 }
3756 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003757 }
3758
Craig Mautnere0570202015-05-13 13:06:11 -07003759 private String lockTaskModeToString() {
3760 switch (mLockTaskModeState) {
3761 case LOCK_TASK_MODE_LOCKED:
3762 return "LOCKED";
3763 case LOCK_TASK_MODE_PINNED:
3764 return "PINNED";
3765 case LOCK_TASK_MODE_NONE:
3766 return "NONE";
3767 default: return "unknown=" + mLockTaskModeState;
3768 }
3769 }
3770
Craig Mautner27084302013-03-25 08:05:25 -07003771 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003772 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003773 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003774 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3775 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3776 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003777 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003778 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3779 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3780 if (packages.size() > 0) {
3781 pw.println(" mLockTaskPackages (userId:packages)=");
3782 for (int i = 0; i < packages.size(); ++i) {
3783 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3784 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3785 }
3786 }
3787 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003788 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003789
Craig Mautner20e72272013-04-01 13:45:53 -07003790 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003791 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003792 }
3793
Dianne Hackborn390517b2013-05-30 15:03:32 -07003794 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3795 boolean needSep, String prefix) {
3796 if (activity != null) {
3797 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3798 if (needSep) {
3799 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003800 }
3801 pw.print(prefix);
3802 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003803 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003804 }
3805 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003806 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003807 }
3808
Craig Mautner8d341ef2013-03-26 09:03:27 -07003809 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3810 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003811 boolean printed = false;
3812 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003813 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3814 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003815 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003816 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003817 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003818 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 final ActivityStack stack = stacks.get(stackNdx);
3820 StringBuilder stackHeader = new StringBuilder(128);
3821 stackHeader.append(" Stack #");
3822 stackHeader.append(stack.mStackId);
3823 stackHeader.append(":");
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07003824 stackHeader.append("\n");
3825 stackHeader.append(" mFullscreen=" + stack.mFullscreen);
3826 stackHeader.append("\n");
3827 stackHeader.append(" mBounds=" + stack.mBounds);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3829 needSep, stackHeader.toString());
3830 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3831 !dumpAll, false, dumpPackage, true,
3832 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003833
Craig Mautner4a1cb222013-12-04 16:14:06 -08003834 needSep = printed;
3835 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3836 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003837 if (pr) {
3838 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003839 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003840 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003841 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3842 " mResumedActivity: ");
3843 if (pr) {
3844 printed = true;
3845 needSep = false;
3846 }
3847 if (dumpAll) {
3848 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3849 " mLastPausedActivity: ");
3850 if (pr) {
3851 printed = true;
3852 needSep = true;
3853 }
3854 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3855 needSep, " mLastNoHistoryActivity: ");
3856 }
3857 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003858 }
3859 }
3860
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003861 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3862 false, dumpPackage, true, " Activities waiting to finish:", null);
3863 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3864 false, dumpPackage, true, " Activities waiting to stop:", null);
3865 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3866 false, dumpPackage, true, " Activities waiting for another to become visible:",
3867 null);
3868 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3869 false, dumpPackage, true, " Activities waiting to sleep:", null);
3870 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3871 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003872
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003873 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003874 }
3875
Dianne Hackborn390517b2013-05-30 15:03:32 -07003876 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003877 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003878 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003879 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003880 String innerPrefix = null;
3881 String[] args = null;
3882 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003883 for (int i=list.size()-1; i>=0; i--) {
3884 final ActivityRecord r = list.get(i);
3885 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3886 continue;
3887 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003888 if (innerPrefix == null) {
3889 innerPrefix = prefix + " ";
3890 args = new String[0];
3891 }
3892 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003893 final boolean full = !brief && (complete || !r.isInHistory());
3894 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003895 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003896 needNL = false;
3897 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003898 if (header1 != null) {
3899 pw.println(header1);
3900 header1 = null;
3901 }
3902 if (header2 != null) {
3903 pw.println(header2);
3904 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003905 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003906 if (lastTask != r.task) {
3907 lastTask = r.task;
3908 pw.print(prefix);
3909 pw.print(full ? "* " : " ");
3910 pw.println(lastTask);
3911 if (full) {
3912 lastTask.dump(pw, prefix + " ");
3913 } else if (complete) {
3914 // Complete + brief == give a summary. Isn't that obvious?!?
3915 if (lastTask.intent != null) {
3916 pw.print(prefix); pw.print(" ");
3917 pw.println(lastTask.intent.toInsecureStringWithClip());
3918 }
3919 }
3920 }
3921 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3922 pw.print(" #"); pw.print(i); pw.print(": ");
3923 pw.println(r);
3924 if (full) {
3925 r.dump(pw, innerPrefix);
3926 } else if (complete) {
3927 // Complete + brief == give a summary. Isn't that obvious?!?
3928 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3929 if (r.app != null) {
3930 pw.print(innerPrefix); pw.println(r.app);
3931 }
3932 }
3933 if (client && r.app != null && r.app.thread != null) {
3934 // flush anything that is already in the PrintWriter since the thread is going
3935 // to write to the file descriptor directly
3936 pw.flush();
3937 try {
3938 TransferPipe tp = new TransferPipe();
3939 try {
3940 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3941 r.appToken, innerPrefix, args);
3942 // Short timeout, since blocking here can
3943 // deadlock with the application.
3944 tp.go(fd, 2000);
3945 } finally {
3946 tp.kill();
3947 }
3948 } catch (IOException e) {
3949 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3950 } catch (RemoteException e) {
3951 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3952 }
3953 needNL = true;
3954 }
3955 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003956 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003957 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003958
Craig Mautnerf3333272013-04-22 10:55:53 -07003959 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003960 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3961 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003962 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3963 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003964 }
3965
3966 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003967 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003968 }
3969
3970 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003971 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3972 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003973 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3974 }
3975
Craig Mautner05d29032013-05-03 13:40:13 -07003976 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003977 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3978 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3979 }
Craig Mautner05d29032013-05-03 13:40:13 -07003980 }
3981
Craig Mautner0eea92c2013-05-16 13:35:39 -07003982 void removeSleepTimeouts() {
3983 mSleepTimeout = false;
3984 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3985 }
3986
3987 final void scheduleSleepTimeout() {
3988 removeSleepTimeouts();
3989 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3990 }
3991
Craig Mautner4a1cb222013-12-04 16:14:06 -08003992 @Override
3993 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003994 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003995 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3996 }
3997
3998 @Override
3999 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004000 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004001 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
4002 }
4003
4004 @Override
4005 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07004006 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004007 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
4008 }
4009
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004010 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08004011 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004012 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08004013 newDisplay = mActivityDisplays.get(displayId) == null;
4014 if (newDisplay) {
4015 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07004016 if (activityDisplay.mDisplay == null) {
4017 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
4018 return;
4019 }
Craig Mautner4504de52013-12-20 09:06:56 -08004020 mActivityDisplays.put(displayId, activityDisplay);
4021 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004022 }
Craig Mautner4504de52013-12-20 09:06:56 -08004023 if (newDisplay) {
4024 mWindowManager.onDisplayAdded(displayId);
4025 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004026 }
4027
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004028 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004029 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004030 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4031 if (activityDisplay != null) {
4032 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004033 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004034 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004035 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004036 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004037 }
4038 }
4039 mWindowManager.onDisplayRemoved(displayId);
4040 }
4041
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004042 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004043 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004044 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4045 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004046 // TODO: Update the bounds.
4047 }
4048 }
4049 mWindowManager.onDisplayChanged(displayId);
4050 }
4051
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004052 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004053 StackInfo info = new StackInfo();
4054 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
4055 info.displayId = Display.DEFAULT_DISPLAY;
4056 info.stackId = stack.mStackId;
4057
4058 ArrayList<TaskRecord> tasks = stack.getAllTasks();
4059 final int numTasks = tasks.size();
4060 int[] taskIds = new int[numTasks];
4061 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004062 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08004063 for (int i = 0; i < numTasks; ++i) {
4064 final TaskRecord task = tasks.get(i);
4065 taskIds[i] = task.taskId;
4066 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
4067 : task.realActivity != null ? task.realActivity.flattenToString()
4068 : task.getTopActivity() != null ? task.getTopActivity().packageName
4069 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004070 taskBounds[i] = new Rect();
4071 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004072 }
4073 info.taskIds = taskIds;
4074 info.taskNames = taskNames;
Wale Ogunwale868a5e12015-08-02 16:19:20 -07004075 info.taskBounds = taskBounds;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004076 return info;
4077 }
4078
4079 StackInfo getStackInfoLocked(int stackId) {
4080 ActivityStack stack = getStack(stackId);
4081 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004082 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004083 }
4084 return null;
4085 }
4086
4087 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004088 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08004089 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
4090 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004091 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004092 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08004093 }
4094 }
4095 return list;
4096 }
4097
Craig Mautner15df08a2015-04-01 12:17:18 -07004098 TaskRecord getLockedTaskLocked() {
4099 final int top = mLockTaskModeTasks.size() - 1;
4100 if (top >= 0) {
4101 return mLockTaskModeTasks.get(top);
4102 }
4103 return null;
4104 }
4105
4106 boolean isLockedTask(TaskRecord task) {
4107 return mLockTaskModeTasks.contains(task);
4108 }
4109
4110 boolean isLastLockedTask(TaskRecord task) {
4111 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
4112 }
4113
4114 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07004115 if (!mLockTaskModeTasks.remove(task)) {
4116 return;
4117 }
4118 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
4119 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004120 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07004121 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
4122 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07004123 final Message lockTaskMsg = Message.obtain();
4124 lockTaskMsg.arg1 = task.userId;
4125 lockTaskMsg.what = LOCK_TASK_END_MSG;
4126 mHandler.sendMessage(lockTaskMsg);
4127 }
4128 }
4129
Craig Mautner6cd6cec2015-04-01 00:02:12 -07004130 void showLockTaskToast() {
4131 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04004132 }
4133
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004134 void showLockTaskEscapeMessageLocked(TaskRecord task) {
4135 if (mLockTaskModeTasks.contains(task)) {
4136 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
4137 }
4138 }
4139
Craig Mautner432f64e2015-05-20 14:59:57 -07004140 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
4141 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004142 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07004143 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07004144 final TaskRecord lockedTask = getLockedTaskLocked();
4145 if (lockedTask != null) {
4146 removeLockedTaskLocked(lockedTask);
4147 if (!mLockTaskModeTasks.isEmpty()) {
4148 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07004149 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4150 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07004151 lockedTask.performClearTaskLocked();
4152 resumeTopActivitiesLocked();
4153 return;
4154 }
Christopher Tate3bd90612014-06-18 16:37:52 -07004155 }
Craig Mautnere0570202015-05-13 13:06:11 -07004156 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4157 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08004158 return;
4159 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004160
4161 // Should have already been checked, but do it again.
4162 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07004163 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
4164 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08004165 return;
4166 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004167 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07004168 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07004169 return;
4170 }
4171
4172 if (mLockTaskModeTasks.isEmpty()) {
4173 // First locktask.
4174 final Message lockTaskMsg = Message.obtain();
4175 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
4176 lockTaskMsg.arg1 = task.userId;
4177 lockTaskMsg.what = LOCK_TASK_START_MSG;
4178 lockTaskMsg.arg2 = lockTaskModeState;
4179 mHandler.sendMessage(lockTaskMsg);
4180 }
4181 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07004182 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
4183 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07004184 mLockTaskModeTasks.remove(task);
4185 mLockTaskModeTasks.add(task);
4186
4187 if (task.mLockTaskUid == -1) {
4188 task.mLockTaskUid = task.mCallingUid;
4189 }
Craig Mautner432f64e2015-05-20 14:59:57 -07004190
4191 if (andResume) {
4192 findTaskToMoveToFrontLocked(task, 0, null, reason);
4193 resumeTopActivitiesLocked();
4194 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004195 }
4196
4197 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04004198 return isLockTaskModeViolation(task, false);
4199 }
4200
4201 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
4202 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004203 return false;
4204 }
4205 final int lockTaskAuth = task.mLockTaskAuth;
4206 switch (lockTaskAuth) {
4207 case LOCK_TASK_AUTH_DONT_LOCK:
4208 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01004209 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07004210 case LOCK_TASK_AUTH_LAUNCHABLE:
4211 case LOCK_TASK_AUTH_WHITELISTED:
4212 return false;
4213 case LOCK_TASK_AUTH_PINNABLE:
4214 // Pinnable tasks can't be launched on top of locktask tasks.
4215 return !mLockTaskModeTasks.isEmpty();
4216 default:
4217 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
4218 return true;
4219 }
Craig Mautneraea74a52014-03-08 14:23:10 -08004220 }
4221
Craig Mautner15df08a2015-04-01 12:17:18 -07004222 void onLockTaskPackagesUpdatedLocked() {
4223 boolean didSomething = false;
4224 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
4225 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01004226 final boolean wasWhitelisted =
4227 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4228 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07004229 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01004230 final boolean isWhitelisted =
4231 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
4232 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
4233 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004234 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07004235 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
4236 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07004237 removeLockedTaskLocked(lockedTask);
4238 lockedTask.performClearTaskLocked();
4239 didSomething = true;
4240 }
4241 }
4242 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
4243 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
4244 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4245 final ActivityStack stack = stacks.get(stackNdx);
4246 stack.onLockTaskPackagesUpdatedLocked();
4247 }
4248 }
Craig Mautnere0570202015-05-13 13:06:11 -07004249 final ActivityRecord r = topRunningActivityLocked();
4250 final TaskRecord task = r != null ? r.task : null;
4251 if (mLockTaskModeTasks.isEmpty() && task != null
4252 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4253 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004254 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4255 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4256 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4257 false);
4258 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004259 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004260 if (didSomething) {
4261 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004262 }
4263 }
4264
Benjamin Franz43261142015-02-11 15:59:44 +00004265 int getLockTaskModeState() {
4266 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004267 }
4268
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004269 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004270
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004271 public ActivityStackSupervisorHandler(Looper looper) {
4272 super(looper);
4273 }
4274
Craig Mautnerf3333272013-04-22 10:55:53 -07004275 void activityIdleInternal(ActivityRecord r) {
4276 synchronized (mService) {
4277 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4278 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004279 }
4280
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004281 @Override
4282 public void handleMessage(Message msg) {
4283 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004284 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004285 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4286 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004287 if (mService.mDidDexOpt) {
4288 mService.mDidDexOpt = false;
4289 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4290 nmsg.obj = msg.obj;
4291 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4292 return;
4293 }
4294 // We don't at this point know if the activity is fullscreen,
4295 // so we need to be conservative and assume it isn't.
4296 activityIdleInternal((ActivityRecord)msg.obj);
4297 } break;
4298 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004299 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004300 activityIdleInternal((ActivityRecord)msg.obj);
4301 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004302 case RESUME_TOP_ACTIVITY_MSG: {
4303 synchronized (mService) {
4304 resumeTopActivitiesLocked();
4305 }
4306 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004307 case SLEEP_TIMEOUT_MSG: {
4308 synchronized (mService) {
4309 if (mService.isSleepingOrShuttingDown()) {
4310 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4311 mSleepTimeout = true;
4312 checkReadyForSleepLocked();
4313 }
4314 }
4315 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004316 case LAUNCH_TIMEOUT_MSG: {
4317 if (mService.mDidDexOpt) {
4318 mService.mDidDexOpt = false;
4319 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4320 return;
4321 }
4322 synchronized (mService) {
4323 if (mLaunchingActivity.isHeld()) {
4324 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4325 if (VALIDATE_WAKE_LOCK_CALLER
4326 && Binder.getCallingUid() != Process.myUid()) {
4327 throw new IllegalStateException("Calling must be system uid");
4328 }
4329 mLaunchingActivity.release();
4330 }
4331 }
4332 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004333 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004334 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004335 } break;
4336 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004337 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004338 } break;
4339 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004340 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004341 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004342 case CONTAINER_CALLBACK_VISIBILITY: {
4343 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004344 final IActivityContainerCallback callback = container.mCallback;
4345 if (callback != null) {
4346 try {
4347 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4348 } catch (RemoteException e) {
4349 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004350 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004351 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004352 case LOCK_TASK_START_MSG: {
4353 // When lock task starts, we disable the status bars.
4354 try {
Jason Monk62515be2014-05-21 16:06:19 -04004355 if (mLockTaskNotify == null) {
4356 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004357 }
Jason Monk62515be2014-05-21 16:06:19 -04004358 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004359 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004360 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004361 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004362 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004363 flags = StatusBarManager.DISABLE_MASK
4364 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004365 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004366 flags = StatusBarManager.DISABLE_MASK
4367 & (~StatusBarManager.DISABLE_BACK)
4368 & (~StatusBarManager.DISABLE_HOME)
4369 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004370 }
4371 getStatusBarService().disable(flags, mToken,
4372 mService.mContext.getPackageName());
4373 }
4374 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004375 if (getDevicePolicyManager() != null) {
4376 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004377 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004378 }
justinzhang5286d3f2014-05-12 17:06:01 -04004379 } catch (RemoteException ex) {
4380 throw new RuntimeException(ex);
4381 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004382 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004383 case LOCK_TASK_END_MSG: {
4384 // When lock task ends, we enable the status bars.
4385 try {
Jason Monk62515be2014-05-21 16:06:19 -04004386 if (getStatusBarService() != null) {
4387 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4388 mService.mContext.getPackageName());
4389 }
4390 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004391 if (getDevicePolicyManager() != null) {
4392 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4393 msg.arg1);
4394 }
Jason Monk62515be2014-05-21 16:06:19 -04004395 if (mLockTaskNotify == null) {
4396 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4397 }
4398 mLockTaskNotify.show(false);
4399 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004400 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004401 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004402 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004403 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004404 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004405 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004406 new LockPatternUtils(mService.mContext)
4407 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004408 }
4409 } catch (SettingNotFoundException e) {
4410 // No setting, don't lock.
4411 }
justinzhang5286d3f2014-05-12 17:06:01 -04004412 } catch (RemoteException ex) {
4413 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004414 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004415 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004416 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004417 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004418 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4419 if (mLockTaskNotify == null) {
4420 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4421 }
4422 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4423 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004424 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4425 final ActivityContainer container = (ActivityContainer) msg.obj;
4426 final IActivityContainerCallback callback = container.mCallback;
4427 if (callback != null) {
4428 try {
4429 callback.onAllActivitiesComplete(container.asBinder());
4430 } catch (RemoteException e) {
4431 }
4432 }
4433 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004434 case LAUNCH_TASK_BEHIND_COMPLETE: {
4435 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004436 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004437 if (r != null) {
4438 handleLaunchTaskBehindCompleteLocked(r);
4439 }
4440 }
4441 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004442 }
4443 }
4444 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004445
Ying Wangb081a592014-04-22 15:20:16 -07004446 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004447 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004448 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004449 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004450 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004451 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004452 ActivityRecord mParentActivity = null;
4453 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004454
Craig Mautnere3a00d72014-04-16 08:31:19 -07004455 boolean mVisible = true;
4456
Craig Mautner4a1cb222013-12-04 16:14:06 -08004457 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004458 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004459
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004460 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4461 final static int CONTAINER_STATE_NO_SURFACE = 1;
4462 final static int CONTAINER_STATE_FINISHING = 2;
4463 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4464
4465 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004466 synchronized (mService) {
4467 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004468 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004469 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004470 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004471 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004472 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004473
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004474 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004475 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004476 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004477 mActivityDisplay = activityDisplay;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004478 mStack.attachDisplay(activityDisplay, onTop);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004479 activityDisplay.attachActivities(mStack, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004480 }
4481
4482 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004483 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004484 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004485 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4486 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004487 return;
4488 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004489 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004490 }
4491 }
4492
4493 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004494 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004495 synchronized (mService) {
4496 if (mActivityDisplay != null) {
4497 return mActivityDisplay.mDisplayId;
4498 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004499 }
4500 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004501 }
4502
Jeff Brownca9bc702014-02-11 14:32:56 -08004503 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004504 public int getStackId() {
4505 synchronized (mService) {
4506 return mStackId;
4507 }
4508 }
4509
4510 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004511 public boolean injectEvent(InputEvent event) {
4512 final long origId = Binder.clearCallingIdentity();
4513 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004514 synchronized (mService) {
4515 if (mActivityDisplay != null) {
4516 return mInputManagerInternal.injectInputEvent(event,
4517 mActivityDisplay.mDisplayId,
4518 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4519 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004520 }
4521 return false;
4522 } finally {
4523 Binder.restoreCallingIdentity(origId);
4524 }
4525 }
4526
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004527 @Override
4528 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004529 synchronized (mService) {
4530 if (mContainerState == CONTAINER_STATE_FINISHING) {
4531 return;
4532 }
4533 mContainerState = CONTAINER_STATE_FINISHING;
4534
Craig Mautnerd163e752014-06-13 17:18:47 -07004535 long origId = Binder.clearCallingIdentity();
4536 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004537 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004538 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004539 } finally {
4540 Binder.restoreCallingIdentity(origId);
4541 }
4542 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004543 }
4544
Craig Mautner60257702014-09-17 15:02:33 -07004545 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004546 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004547 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004548 if (mActivityDisplay != null) {
4549 mActivityDisplay.detachActivitiesLocked(mStack);
4550 mActivityDisplay = null;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004551 mStack.detachDisplay();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004552 }
4553 }
4554
4555 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004556 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004557 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004558 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004559 Binder.getCallingUid(), mCurrentUser, false,
4560 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004561
Craig Mautnere0a38842013-12-16 16:14:02 -08004562 // TODO: Switch to user app stacks here.
4563 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004564 final Uri data = intent.getData();
4565 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4566 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004567 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004568 checkEmbeddedAllowedInner(userId, intent, mimeType);
4569
4570 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4571 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004572 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004573 }
4574
4575 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004576 public final int startActivityIntentSender(IIntentSender intentSender)
4577 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004578 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4579
4580 if (!(intentSender instanceof PendingIntentRecord)) {
4581 throw new IllegalArgumentException("Bad PendingIntent object");
4582 }
4583
Craig Mautnerb9168362015-02-26 20:40:19 -08004584 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004585 Binder.getCallingUid(), mCurrentUser, false,
4586 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004587
4588 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4589 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4590 pendingIntent.key.requestResolvedType);
4591
4592 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4593 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4594 }
4595
4596 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004597 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004598 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004599 throw new SecurityException(
4600 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4601 }
4602 }
4603
Craig Mautnerdf88d732014-01-27 09:21:32 -08004604 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004605 public IBinder asBinder() {
4606 return this;
4607 }
4608
Craig Mautner4504de52013-12-20 09:06:56 -08004609 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004610 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004611 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004612 }
4613
Craig Mautner4a1cb222013-12-04 16:14:06 -08004614 ActivityStackSupervisor getOuter() {
4615 return ActivityStackSupervisor.this;
4616 }
4617
Craig Mautnerd163e752014-06-13 17:18:47 -07004618 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004619 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004620 }
4621
4622 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004623 synchronized (mService) {
4624 if (mActivityDisplay != null) {
4625 mActivityDisplay.getBounds(outBounds);
4626 } else {
4627 outBounds.set(0, 0);
4628 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004629 }
4630 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004631
Craig Mautner6985bad2014-04-21 15:22:06 -07004632 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004633 void setVisible(boolean visible) {
4634 if (mVisible != visible) {
4635 mVisible = visible;
4636 if (mCallback != null) {
4637 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4638 0 /* unused */, this).sendToTarget();
4639 }
4640 }
4641 }
4642
Craig Mautner6985bad2014-04-21 15:22:06 -07004643 void setDrawn() {
4644 }
4645
Craig Mautner1b4bf852014-05-26 15:06:32 -07004646 // You can always start a new task on a regular ActivityStack.
4647 boolean isEligibleForNewTasks() {
4648 return true;
4649 }
4650
Craig Mautnerd163e752014-06-13 17:18:47 -07004651 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004652 detachLocked();
4653 deleteActivityContainer(this);
4654 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004655 }
4656
Craig Mautner34b73df2014-01-12 21:11:08 -08004657 @Override
4658 public String toString() {
4659 return mIdString + (mActivityDisplay == null ? "N" : "A");
4660 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004661 }
4662
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004663 private class VirtualActivityContainer extends ActivityContainer {
4664 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004665 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004666
4667 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4668 super(getNextStackId());
4669 mParentActivity = parent;
4670 mCallback = callback;
4671 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004672 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004673 }
4674
4675 @Override
4676 public void setSurface(Surface surface, int width, int height, int density) {
4677 super.setSurface(surface, width, height, density);
4678
4679 synchronized (mService) {
4680 final long origId = Binder.clearCallingIdentity();
4681 try {
4682 setSurfaceLocked(surface, width, height, density);
4683 } finally {
4684 Binder.restoreCallingIdentity(origId);
4685 }
4686 }
4687 }
4688
4689 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4690 if (mContainerState == CONTAINER_STATE_FINISHING) {
4691 return;
4692 }
4693 VirtualActivityDisplay virtualActivityDisplay =
4694 (VirtualActivityDisplay) mActivityDisplay;
4695 if (virtualActivityDisplay == null) {
4696 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004697 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004698 mActivityDisplay = virtualActivityDisplay;
4699 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004700 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004701 }
4702
4703 if (mSurface != null) {
4704 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004705 }
4706
Craig Mautner6985bad2014-04-21 15:22:06 -07004707 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004708 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004709 mStack.resumeTopActivityLocked(null);
4710 } else {
4711 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004712 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004713 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004714 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004715 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004716 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004717
Craig Mautnerd163e752014-06-13 17:18:47 -07004718 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004719
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004720 if (DEBUG_STACK) Slog.d(TAG_STACK,
4721 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004722 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004723
Craig Mautner6985bad2014-04-21 15:22:06 -07004724 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004725 boolean isAttachedLocked() {
4726 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004727 }
4728
4729 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004730 void setDrawn() {
4731 synchronized (mService) {
4732 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004733 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004734 }
4735 }
4736
Craig Mautner1b4bf852014-05-26 15:06:32 -07004737 // Never start a new task on an ActivityView if it isn't explicitly specified.
4738 @Override
4739 boolean isEligibleForNewTasks() {
4740 return false;
4741 }
4742
Craig Mautnerd163e752014-06-13 17:18:47 -07004743 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004744 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004745 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4746 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4747 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4748 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4749 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004750 }
4751 }
4752
Craig Mautner4a1cb222013-12-04 16:14:06 -08004753 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4754 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004755 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004756 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004757 int mDisplayId;
4758 Display mDisplay;
4759 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004760
Craig Mautner4a1cb222013-12-04 16:14:06 -08004761 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4762 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004763 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004764
Jose Lima4b6c6692014-08-12 17:41:12 -07004765 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004766
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004767 ActivityDisplay() {
4768 }
Craig Mautner4504de52013-12-20 09:06:56 -08004769
Craig Mautner1a70a162014-09-13 12:09:31 -07004770 // After instantiation, check that mDisplay is not null before using this. The alternative
4771 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004772 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004773 final Display display = mDisplayManager.getDisplay(displayId);
4774 if (display == null) {
4775 return;
4776 }
4777 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004778 }
4779
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004780 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004781 mDisplay = display;
4782 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004783 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004784 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004785
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004786 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004787 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004788 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4789 + " onTop=" + onTop);
4790 if (onTop) {
4791 mStacks.add(stack);
4792 } else {
4793 mStacks.add(0, stack);
4794 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004795 }
4796
Craig Mautnere0a38842013-12-16 16:14:02 -08004797 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004798 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004799 + " from displayId=" + mDisplayId);
4800 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004801 }
4802
4803 void getBounds(Point bounds) {
4804 mDisplay.getDisplayInfo(mDisplayInfo);
4805 bounds.x = mDisplayInfo.appWidth;
4806 bounds.y = mDisplayInfo.appHeight;
4807 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004808
Jose Lima4b6c6692014-08-12 17:41:12 -07004809 void setVisibleBehindActivity(ActivityRecord r) {
4810 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004811 }
4812
Jose Lima4b6c6692014-08-12 17:41:12 -07004813 boolean hasVisibleBehindActivity() {
4814 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004815 }
4816
Craig Mautner34b73df2014-01-12 21:11:08 -08004817 @Override
4818 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004819 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4820 }
4821 }
4822
4823 class VirtualActivityDisplay extends ActivityDisplay {
4824 VirtualDisplay mVirtualDisplay;
4825
Craig Mautner6985bad2014-04-21 15:22:06 -07004826 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004827 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004828 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4829 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4830 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4831 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004832
4833 init(mVirtualDisplay.getDisplay());
4834
4835 mWindowManager.handleDisplayAdded(mDisplayId);
4836 }
4837
4838 void setSurface(Surface surface) {
4839 if (mVirtualDisplay != null) {
4840 mVirtualDisplay.setSurface(surface);
4841 }
4842 }
4843
4844 @Override
4845 void detachActivitiesLocked(ActivityStack stack) {
4846 super.detachActivitiesLocked(stack);
4847 if (mVirtualDisplay != null) {
4848 mVirtualDisplay.release();
4849 mVirtualDisplay = null;
4850 }
4851 }
4852
4853 @Override
4854 public String toString() {
4855 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004856 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004857 }
Jose Lima58e66d62014-05-27 20:00:27 -07004858
4859 private boolean isLeanbackOnlyDevice() {
4860 boolean onLeanbackOnly = false;
4861 try {
4862 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4863 PackageManager.FEATURE_LEANBACK_ONLY);
4864 } catch (RemoteException e) {
4865 // noop
4866 }
4867
4868 return onLeanbackOnly;
4869 }
Wale Ogunwale3e3e66f2015-09-10 15:40:19 -07004870
4871 /**
4872 * Adjust bounds to stay within stack bounds.
4873 *
4874 * Since bounds might be outside of stack bounds, this method tries to move the bounds in a way
4875 * that keep them unchanged, but be contained within the stack bounds.
4876 *
4877 * @param bounds Bounds to be adjusted.
4878 * @param stackBounds Bounds within which the other bounds should remain.
4879 */
4880 private static void fitWithinBounds(Rect bounds, Rect stackBounds) {
4881 if (stackBounds == null || stackBounds.contains(bounds)) {
4882 return;
4883 }
4884
4885 if (bounds.left < stackBounds.left || bounds.right > stackBounds.right) {
4886 final int maxRight = stackBounds.right
4887 - (stackBounds.width() / FIT_WITHIN_BOUNDS_DIVIDER);
4888 int horizontalDiff = stackBounds.left - bounds.left;
4889 if ((horizontalDiff < 0 && bounds.left >= maxRight)
4890 || (bounds.left + horizontalDiff >= maxRight)) {
4891 horizontalDiff = maxRight - bounds.left;
4892 }
4893 bounds.left += horizontalDiff;
4894 bounds.right += horizontalDiff;
4895 }
4896
4897 if (bounds.top < stackBounds.top || bounds.bottom > stackBounds.bottom) {
4898 final int maxBottom = stackBounds.bottom
4899 - (stackBounds.height() / FIT_WITHIN_BOUNDS_DIVIDER);
4900 int verticalDiff = stackBounds.top - bounds.top;
4901 if ((verticalDiff < 0 && bounds.top >= maxBottom)
4902 || (bounds.top + verticalDiff >= maxBottom)) {
4903 verticalDiff = maxBottom - bounds.top;
4904 }
4905 bounds.top += verticalDiff;
4906 bounds.bottom += verticalDiff;
4907 }
4908 }
4909
Craig Mautner27084302013-03-25 08:05:25 -07004910}