blob: 296f16c8722752824efb1b64ee42f0d6c433cc6d [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 Ogunwaleddc1cb22015-07-25 19:23:04 -070020import static android.app.ActivityManager.FIRST_DYNAMIC_STACK_ID;
21import static android.app.ActivityManager.FIRST_STATIC_STACK_ID;
22import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
23import static android.app.ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID;
24import static android.app.ActivityManager.HOME_STACK_ID;
25import static android.app.ActivityManager.LAST_STATIC_STACK_ID;
Craig Mautner15df08a2015-04-01 12:17:18 -070026import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
27import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
28import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
Wale Ogunwaled80c2632015-03-13 10:26:26 -070029import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
30import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
Craig Mautner29219d92013-04-16 20:19:12 -070031import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
32import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -070033import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner6170f732013-04-02 13:05:23 -070034import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Wale Ogunwaled57969f2014-11-15 19:37:29 -080035import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner05d29032013-05-03 13:40:13 -070036import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner84984fa2014-06-19 11:19:20 -070037import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
38import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
39import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070040import static com.android.server.am.ActivityStack.ActivityState.*;
Craig Mautner15df08a2015-04-01 12:17:18 -070041import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
42import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
Benjamin Franz469dd582015-06-09 14:24:36 +010043import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
Craig Mautner15df08a2015-04-01 12:17:18 -070044import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
45import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
Craig Mautner8d341ef2013-03-26 09:03:27 -070046
Svetoslav7008b512015-06-24 18:47:07 -070047import android.Manifest;
Craig Mautner2420ead2013-04-01 17:13:20 -070048import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080050import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.app.ActivityOptions;
52import android.app.AppGlobals;
Svetoslav7008b512015-06-24 18:47:07 -070053import android.app.AppOpsManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -080054import android.app.IActivityContainer;
55import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070056import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070059import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070060import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070061import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070062import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040063import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040064import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070065import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070066import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070067import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070068import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070069import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070070import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070071import android.content.pm.ApplicationInfo;
Svetoslav7008b512015-06-24 18:47:07 -070072import android.content.pm.PackageInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070073import android.content.pm.PackageManager;
74import android.content.pm.ResolveInfo;
75import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080076import android.graphics.Point;
Wale Ogunwale60454db2015-01-23 16:05:07 -080077import android.graphics.Rect;
Craig Mautner4a1cb222013-12-04 16:14:06 -080078import android.hardware.display.DisplayManager;
79import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080080import android.hardware.display.DisplayManagerGlobal;
81import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080082import android.hardware.input.InputManager;
83import android.hardware.input.InputManagerInternal;
Craig Mautnerb9168362015-02-26 20:40:19 -080084import android.net.Uri;
Craig Mautner23ac33b2013-04-01 16:26:35 -070085import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070086import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070087import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070088import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070089import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070090import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070091import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070092import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070093import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070094import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070095import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040096import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070097import android.os.SystemClock;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070098import android.os.TransactionTooLargeException;
Craig Mautner6170f732013-04-02 13:05:23 -070099import android.os.UserHandle;
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700100import android.os.WorkSource;
Svetoslav7008b512015-06-24 18:47:07 -0700101import android.provider.MediaStore;
Jason Monk62515be2014-05-21 16:06:19 -0400102import android.provider.Settings;
103import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700104import android.service.voice.IVoiceInteractionSession;
Svetoslav7008b512015-06-24 18:47:07 -0700105import android.util.ArrayMap;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700106import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -0700107import android.util.EventLog;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700108import android.util.IntArray;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700109import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800110import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700111
Craig Mautner4a1cb222013-12-04 16:14:06 -0800112import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -0800113import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800114import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -0800115import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -0800116import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700117import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700118import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800119import com.android.internal.content.ReferrerIntent;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700120import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400121import com.android.internal.statusbar.IStatusBarService;
Svetoslav7008b512015-06-24 18:47:07 -0700122import com.android.internal.util.ArrayUtils;
Jason Monke0697792014-08-04 16:28:09 -0400123import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800124import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700125import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700126import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700127
justinzhang5286d3f2014-05-12 17:06:01 -0400128
Craig Mautner8d341ef2013-03-26 09:03:27 -0700129import java.io.FileDescriptor;
130import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700131import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700132import java.util.ArrayList;
Craig Mautnere0570202015-05-13 13:06:11 -0700133import java.util.Arrays;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700134import java.util.List;
Wale Ogunwale540e1232015-05-01 15:35:39 -0700135import java.util.Set;
Craig Mautner27084302013-03-25 08:05:25 -0700136
Craig Mautner4a1cb222013-12-04 16:14:06 -0800137public final class ActivityStackSupervisor implements DisplayListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800138 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStackSupervisor" : TAG_AM;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700139 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700140 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700141 private static final String TAG_FOCUS = TAG + POSTFIX_FOCUS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700142 private static final String TAG_IDLE = TAG + POSTFIX_IDLE;
Craig Mautnere0570202015-05-13 13:06:11 -0700143 private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700144 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700145 private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700146 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
147 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700148 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700149 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700150 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
151 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700152 private static final String TAG_VISIBLE_BEHIND = TAG + POSTFIX_VISIBLE_BEHIND;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700153 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800154
Craig Mautnerf3333272013-04-22 10:55:53 -0700155 /** How long we wait until giving up on the last activity telling us it is idle. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700156 static final int IDLE_TIMEOUT = 10 * 1000;
Craig Mautnerf3333272013-04-22 10:55:53 -0700157
Craig Mautner0eea92c2013-05-16 13:35:39 -0700158 /** How long we can hold the sleep wake lock before giving up. */
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700159 static final int SLEEP_TIMEOUT = 5 * 1000;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700160
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700161 // How long we can hold the launch wake lock before giving up.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700162 static final int LAUNCH_TIMEOUT = 10 * 1000;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700163
Craig Mautner05d29032013-05-03 13:40:13 -0700164 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
165 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
166 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700167 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700168 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800169 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
170 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
171 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700172 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400173 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
174 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautner6cd6cec2015-04-01 00:02:12 -0700175 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Wale Ogunwale73eba742015-04-07 14:23:14 -0700176 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -0700177 static final int SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800178
Craig Mautner4504de52013-12-20 09:06:56 -0800179 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700180
Jason Monk62515be2014-05-21 16:06:19 -0400181 private static final String LOCK_TASK_TAG = "Lock-to-App";
182
Svetoslav7008b512015-06-24 18:47:07 -0700183 // Activity actions an app cannot start if it uses a permission which is not granted.
184 private static final ArrayMap<String, String> ACTION_TO_RUNTIME_PERMISSION =
185 new ArrayMap<>();
186 static {
187 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_IMAGE_CAPTURE,
188 Manifest.permission.CAMERA);
189 ACTION_TO_RUNTIME_PERMISSION.put(MediaStore.ACTION_VIDEO_CAPTURE,
190 Manifest.permission.CAMERA);
191 ACTION_TO_RUNTIME_PERMISSION.put(Intent.ACTION_CALL,
192 Manifest.permission.CALL_PHONE);
193 }
194
Svet Ganov99b60432015-06-27 13:15:22 -0700195 /** Action restriction: launching the activity is not restricted. */
196 private static final int ACTIVITY_RESTRICTION_NONE = 0;
197 /** Action restriction: launching the activity is restricted by a permission. */
198 private static final int ACTIVITY_RESTRICTION_PERMISSION = 1;
199 /** Action restriction: launching the activity is restricted by an app op. */
200 private static final int ACTIVITY_RESTRICTION_APPOP = 2;
Svetoslav7008b512015-06-24 18:47:07 -0700201
justinzhang5286d3f2014-05-12 17:06:01 -0400202 /** Status Bar Service **/
203 private IBinder mToken = new Binder();
204 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400205 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400206
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700207 // For debugging to make sure the caller when acquiring/releasing our
208 // wake lock is the system process.
209 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700210
Craig Mautner27084302013-03-25 08:05:25 -0700211 final ActivityManagerService mService;
212
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800213 private final RecentTasks mRecentTasks;
214
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700215 final ActivityStackSupervisorHandler mHandler;
216
217 /** Short cut */
218 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800219 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700220
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700221 /** Counter for next free stack ID to use for dynamic activity stacks. */
222 private int mNextFreeStackId = FIRST_DYNAMIC_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700223
224 /** Task identifier that activities are currently being started in. Incremented each time a
225 * new task is created. */
226 private int mCurTaskId = 0;
227
Craig Mautner2420ead2013-04-01 17:13:20 -0700228 /** The current user */
229 private int mCurrentUser;
230
Craig Mautnere0a38842013-12-16 16:14:02 -0800231 /** The stack containing the launcher app. Assumed to always be attached to
232 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700233 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700234
Craig Mautnere0a38842013-12-16 16:14:02 -0800235 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700236 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700237
Craig Mautner4a1cb222013-12-04 16:14:06 -0800238 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
239 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800240 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800241 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700242
243 /** List of activities that are waiting for a new activity to become visible before completing
244 * whatever operation they are supposed to do. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800245 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700246
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700247 /** List of processes waiting to find out about the next visible activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800248 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700249
250 /** List of processes waiting to find out about the next launched activity. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800251 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched = new ArrayList<>();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700252
Craig Mautnerde4ef022013-04-07 19:01:33 -0700253 /** List of activities that are ready to be stopped, but waiting for the next activity to
254 * settle down before doing so. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800255 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -0700256
Craig Mautnerf3333272013-04-22 10:55:53 -0700257 /** List of activities that are ready to be finished, but waiting for the previous activity to
258 * settle down before doing so. It contains ActivityRecord objects. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800259 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700260
Craig Mautner0eea92c2013-05-16 13:35:39 -0700261 /** List of activities that are in the process of going to sleep. */
Craig Mautner8c14c152015-01-15 17:32:07 -0800262 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<>();
Craig Mautner0eea92c2013-05-16 13:35:39 -0700263
Craig Mautnerf3333272013-04-22 10:55:53 -0700264 /** Used on user changes */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700265 final ArrayList<UserState> mStartingUsers = new ArrayList<>();
Craig Mautnerf3333272013-04-22 10:55:53 -0700266
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700267 /** Used to queue up any background users being started */
Amith Yamasani37a40c22015-06-17 13:25:42 -0700268 final ArrayList<UserState> mStartingBackgroundUsers = new ArrayList<>();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700269
Craig Mautnerde4ef022013-04-07 19:01:33 -0700270 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
271 * is being brought in front of us. */
272 boolean mUserLeaving = false;
273
Craig Mautner0eea92c2013-05-16 13:35:39 -0700274 /** Set when we have taken too long waiting to go to sleep. */
275 boolean mSleepTimeout = false;
276
Jose Lima58e66d62014-05-27 20:00:27 -0700277 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
278 * setWindowManager is called. **/
279 private boolean mLeanbackOnlyDevice;
280
Craig Mautner0eea92c2013-05-16 13:35:39 -0700281 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700282 * We don't want to allow the device to go to sleep while in the process
283 * of launching an activity. This is primarily to allow alarm intent
284 * receivers to launch an activity and get that to run before the device
285 * goes back to sleep.
286 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700287 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700288
289 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700290 * Set when the system is going to sleep, until we have
291 * successfully paused the current activity and released our wake lock.
292 * At that point the system is allowed to actually sleep.
293 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700294 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700295
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700296 /** Stack id of the front stack when user switched, indexed by userId. */
297 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700298
Craig Mautner4504de52013-12-20 09:06:56 -0800299 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800300 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700301 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800302
303 /** Mapping from displayId to display current state */
Craig Mautner15df08a2015-04-01 12:17:18 -0700304 private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800305
Jeff Brownca9bc702014-02-11 14:32:56 -0800306 InputManagerInternal mInputManagerInternal;
307
Craig Mautner15df08a2015-04-01 12:17:18 -0700308 /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
309 * may be finished until there is only one entry left. If this is empty the system is not
310 * in lockTask mode. */
311 ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
Benjamin Franz43261142015-02-11 15:59:44 +0000312 /** Store the current lock task mode. Possible values:
Craig Mautner2568c3a2015-03-26 14:22:34 -0700313 * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
314 * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
Benjamin Franz43261142015-02-11 15:59:44 +0000315 */
316 private int mLockTaskModeState;
Jason Monk62515be2014-05-21 16:06:19 -0400317 /**
318 * Notifies the user when entering/exiting lock-task.
319 */
320 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800321
Craig Mautner15df08a2015-04-01 12:17:18 -0700322 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
Craig Mautneree36c772014-07-16 14:56:05 -0700323
Craig Mautner42d04db2014-11-06 12:13:23 -0800324 /** Used to keep resumeTopActivityLocked() from being entered recursively */
325 boolean inResumeTopActivity;
326
Craig Mautneree36c772014-07-16 14:56:05 -0700327 /**
328 * Description of a request to start a new activity, which has been held
329 * due to app switches being disabled.
330 */
331 static class PendingActivityLaunch {
332 final ActivityRecord r;
333 final ActivityRecord sourceRecord;
334 final int startFlags;
335 final ActivityStack stack;
336
337 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
338 int _startFlags, ActivityStack _stack) {
339 r = _r;
340 sourceRecord = _sourceRecord;
341 startFlags = _startFlags;
342 stack = _stack;
343 }
344 }
345
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800346 public ActivityStackSupervisor(ActivityManagerService service, RecentTasks recentTasks) {
Craig Mautner27084302013-03-25 08:05:25 -0700347 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800348 mRecentTasks = recentTasks;
Jeff Brown2c43c332014-06-12 22:38:59 -0700349 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
350 }
351
352 /**
353 * At the time when the constructor runs, the power manager has not yet been
354 * initialized. So we initialize our wakelocks afterwards.
355 */
356 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800357 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700358 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700359 mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700360 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700361 }
362
justinzhang5286d3f2014-05-12 17:06:01 -0400363 // This function returns a IStatusBarService. The value is from ServiceManager.
364 // getService and is cached.
365 private IStatusBarService getStatusBarService() {
366 synchronized (mService) {
367 if (mStatusBarService == null) {
368 mStatusBarService = IStatusBarService.Stub.asInterface(
369 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
370 if (mStatusBarService == null) {
371 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
372 }
373 }
374 return mStatusBarService;
375 }
376 }
377
Jason Monk35c62a42014-06-17 10:24:47 -0400378 private IDevicePolicyManager getDevicePolicyManager() {
379 synchronized (mService) {
380 if (mDevicePolicyManager == null) {
381 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
382 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
383 if (mDevicePolicyManager == null) {
384 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
385 }
386 }
387 return mDevicePolicyManager;
388 }
389 }
390
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700391 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800392 synchronized (mService) {
393 mWindowManager = wm;
394
395 mDisplayManager =
396 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
397 mDisplayManager.registerDisplayListener(this, null);
398
399 Display[] displays = mDisplayManager.getDisplays();
400 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
401 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800402 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700403 if (activityDisplay.mDisplay == null) {
404 throw new IllegalStateException("Default Display does not exist");
405 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800406 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800407 }
408
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700409 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800410 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800411
412 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700413
414 // Initialize this here, now that we can get a valid reference to PackageManager.
415 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800416 }
Craig Mautner27084302013-03-25 08:05:25 -0700417 }
418
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200419 void notifyActivityDrawnForKeyguard() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700420 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200421 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700422 }
423
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700424 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800425 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700426 }
427
Craig Mautnerde4ef022013-04-07 19:01:33 -0700428 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800429 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700430 }
431
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800432 /** Top of all visible stacks is/should always be equal to the focused stack.
433 * Use {@link ActivityStack#isStackVisibleLocked} to determine if a specific
434 * stack is visible or not. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700435 boolean isFrontStack(ActivityStack stack) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700436 if (stack == null) {
437 return false;
438 }
439
Craig Mautnerdf88d732014-01-27 09:21:32 -0800440 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
441 if (parent != null) {
442 stack = parent.task.stack;
443 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800444 return stack == mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700445 }
446
Craig Mautner299f9602015-01-26 09:47:33 -0800447 void moveHomeStack(boolean toFront, String reason) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800448 moveHomeStack(toFront, reason, null);
449 }
450
451 void moveHomeStack(boolean toFront, String reason, ActivityStack lastFocusedStack) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800452 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautnerde313752015-01-22 14:28:03 -0800453 final int topNdx = stacks.size() - 1;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800454 if (topNdx <= 0) {
455 return;
456 }
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700457
458 // The home stack should either be at the top or bottom of the stack list.
459 if ((toFront && (stacks.get(topNdx) != mHomeStack))
460 || (!toFront && (stacks.get(0) != mHomeStack))) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700461 if (DEBUG_STACK) Slog.d(TAG_STACK, "moveHomeTask: topStack old="
Wale Ogunwale92acaf22015-03-18 14:43:41 -0700462 + ((lastFocusedStack != null) ? lastFocusedStack : stacks.get(topNdx))
463 + " new=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800464 stacks.remove(mHomeStack);
465 stacks.add(toFront ? topNdx : 0, mHomeStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700466 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800467
468 if (lastFocusedStack != null) {
469 mLastFocusedStack = lastFocusedStack;
470 }
471 mFocusedStack = stacks.get(topNdx);
472
Craig Mautnerde313752015-01-22 14:28:03 -0800473 EventLog.writeEvent(EventLogTags.AM_HOME_STACK_MOVED,
474 mCurrentUser, toFront ? 1 : 0, stacks.get(topNdx).getStackId(),
Craig Mautner299f9602015-01-26 09:47:33 -0800475 mFocusedStack == null ? -1 : mFocusedStack.getStackId(), reason);
Craig Mautnerde313752015-01-22 14:28:03 -0800476
Craig Mautnerf3ea23a2015-01-13 09:37:08 -0800477 if (mService.mBooting || !mService.mBooted) {
478 final ActivityRecord r = topRunningActivityLocked();
479 if (r != null && r.idle) {
480 checkFinishBootingLocked();
481 }
482 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700483 }
484
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700485 /** Returns true if the focus activity was adjusted to the home stack top activity. */
486 boolean moveHomeStackTaskToTop(int homeStackTaskType, String reason) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700487 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
488 mWindowManager.showRecentApps();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700489 return false;
Craig Mautner84984fa2014-06-19 11:19:20 -0700490 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700491
Craig Mautner84984fa2014-06-19 11:19:20 -0700492 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700493
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700494 final ActivityRecord top = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700495 if (top == null) {
496 return false;
497 }
498 mService.setFocusedActivityLocked(top, reason);
499 return true;
Craig Mautner8e569572013-10-11 17:36:59 -0700500 }
501
Craig Mautner299f9602015-01-26 09:47:33 -0800502 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700503 if (!mService.mBooting && !mService.mBooted) {
504 // Not ready yet!
505 return false;
506 }
507
Craig Mautner84984fa2014-06-19 11:19:20 -0700508 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
509 mWindowManager.showRecentApps();
510 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700511 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700512
Craig Mautner84984fa2014-06-19 11:19:20 -0700513 if (prev != null) {
514 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
515 }
516
Wale Ogunwale2d0f39b2015-04-17 15:35:39 -0700517 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
518 ActivityRecord r = getHomeActivity();
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700519 if (r != null) {
Craig Mautner299f9602015-01-26 09:47:33 -0800520 mService.setFocusedActivityLocked(r, reason);
Craig Mautner05d29032013-05-03 13:40:13 -0700521 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700522 }
Craig Mautner299f9602015-01-26 09:47:33 -0800523 return mService.startHomeActivityLocked(mCurrentUser, reason);
Craig Mautner69ada552013-04-18 13:51:51 -0700524 }
525
Craig Mautner8d341ef2013-03-26 09:03:27 -0700526 TaskRecord anyTaskForIdLocked(int id) {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700527 return anyTaskForIdLocked(id, true);
528 }
529
530 /**
531 * Returns a {@link TaskRecord} for the input id if available. Null otherwise.
532 * @param id Id of the task we would like returned.
533 * @param restoreFromRecents If the id was not in the active list, but was found in recents,
534 * restore the task from recents to the active list.
535 */
536 TaskRecord anyTaskForIdLocked(int id, boolean restoreFromRecents) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800537 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800538 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800539 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800540 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
541 ActivityStack stack = stacks.get(stackNdx);
542 TaskRecord task = stack.taskForIdLocked(id);
543 if (task != null) {
544 return task;
545 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700546 }
547 }
Wale Ogunwale7de05352014-12-12 15:21:33 -0800548
549 // Don't give up! Look in recents.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700550 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS, "Looking for task id=" + id + " in recents");
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800551 TaskRecord task = mRecentTasks.taskForIdLocked(id);
Wale Ogunwale7de05352014-12-12 15:21:33 -0800552 if (task == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700553 if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "\tDidn't find task id=" + id + " in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800554 return null;
555 }
556
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700557 if (!restoreFromRecents) {
558 return task;
559 }
560
Wale Ogunwale7de05352014-12-12 15:21:33 -0800561 if (!restoreRecentTaskLocked(task)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700562 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS,
563 "Couldn't restore task id=" + id + " found in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800564 return null;
565 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700566 if (DEBUG_RECENTS) Slog.w(TAG_RECENTS, "Restored task id=" + id + " from in recents");
Wale Ogunwale7de05352014-12-12 15:21:33 -0800567 return task;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700568 }
569
Craig Mautner6170f732013-04-02 13:05:23 -0700570 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800571 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800573 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800574 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
575 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
576 if (r != null) {
577 return r;
578 }
Craig Mautner6170f732013-04-02 13:05:23 -0700579 }
580 }
581 return null;
582 }
583
Craig Mautneref73ee12014-04-23 11:45:37 -0700584 void setNextTaskId(int taskId) {
585 if (taskId > mCurTaskId) {
586 mCurTaskId = taskId;
587 }
588 }
589
Craig Mautner8d341ef2013-03-26 09:03:27 -0700590 int getNextTaskId() {
591 do {
592 mCurTaskId++;
593 if (mCurTaskId <= 0) {
594 mCurTaskId = 1;
595 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -0700596 } while (anyTaskForIdLocked(mCurTaskId, false) != null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700597 return mCurTaskId;
598 }
599
Craig Mautnerde4ef022013-04-07 19:01:33 -0700600 ActivityRecord resumedAppLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800601 ActivityStack stack = mFocusedStack;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700602 if (stack == null) {
603 return null;
604 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700605 ActivityRecord resumedActivity = stack.mResumedActivity;
606 if (resumedActivity == null || resumedActivity.app == null) {
607 resumedActivity = stack.mPausingActivity;
608 if (resumedActivity == null || resumedActivity.app == null) {
609 resumedActivity = stack.topRunningActivityLocked(null);
610 }
611 }
612 return resumedActivity;
613 }
614
Dianne Hackbornff072722014-09-24 10:56:28 -0700615 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700616 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800617 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800618 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
619 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800620 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
621 final ActivityStack stack = stacks.get(stackNdx);
622 if (!isFrontStack(stack)) {
623 continue;
624 }
625 ActivityRecord hr = stack.topRunningActivityLocked(null);
626 if (hr != null) {
627 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
628 && processName.equals(hr.processName)) {
629 try {
George Mount2c92c972014-03-20 09:38:23 -0700630 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800631 didSomething = true;
632 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700633 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800634 Slog.w(TAG, "Exception in new application when starting activity "
635 + hr.intent.getComponent().flattenToShortString(), e);
636 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700637 }
Craig Mautner20e72272013-04-01 13:45:53 -0700638 }
Craig Mautner20e72272013-04-01 13:45:53 -0700639 }
640 }
641 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700642 if (!didSomething) {
643 ensureActivitiesVisibleLocked(null, 0);
644 }
Craig Mautner20e72272013-04-01 13:45:53 -0700645 return didSomething;
646 }
647
648 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800649 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
650 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800651 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
652 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800653 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800654 continue;
655 }
656 final ActivityRecord resumedActivity = stack.mResumedActivity;
657 if (resumedActivity == null || !resumedActivity.idle) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700658 if (DEBUG_STATES) Slog.d(TAG_STATES, "allResumedActivitiesIdle: stack="
Craig Mautner34b73df2014-01-12 21:11:08 -0800659 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800660 return false;
661 }
Craig Mautner20e72272013-04-01 13:45:53 -0700662 }
663 }
664 return true;
665 }
666
Craig Mautnerde4ef022013-04-07 19:01:33 -0700667 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800668 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
669 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
671 final ActivityStack stack = stacks.get(stackNdx);
672 if (isFrontStack(stack)) {
673 final ActivityRecord r = stack.mResumedActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700674 if (r != null && r.state != RESUMED) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800675 return false;
676 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700677 }
678 }
679 }
680 // TODO: Not sure if this should check if all Paused are complete too.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700681 if (DEBUG_STACK) Slog.d(TAG_STACK,
Craig Mautner4a1cb222013-12-04 16:14:06 -0800682 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
683 mLastFocusedStack + " to=" + mFocusedStack);
684 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700685 return true;
686 }
687
688 boolean allResumedActivitiesVisible() {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800689 boolean foundResumed = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800690 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
691 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800692 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
693 final ActivityStack stack = stacks.get(stackNdx);
694 final ActivityRecord r = stack.mResumedActivity;
riddle_hsudb46d6b2015-04-01 18:58:07 +0800695 if (r != null) {
Wale Ogunwale356c6282015-04-01 12:32:32 -0700696 if (!r.nowVisible || mWaitingVisibleActivities.contains(r)) {
riddle_hsudb46d6b2015-04-01 18:58:07 +0800697 return false;
698 }
699 foundResumed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800700 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700701 }
702 }
riddle_hsudb46d6b2015-04-01 18:58:07 +0800703 return foundResumed;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700704 }
705
Craig Mautner2acc3892013-09-23 10:28:14 -0700706 /**
707 * Pause all activities in either all of the stacks or just the back stacks.
708 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700709 * @return true if any activity was paused as a result of this call.
710 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700711 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700712 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800713 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
714 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800715 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
716 final ActivityStack stack = stacks.get(stackNdx);
717 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700718 if (DEBUG_STATES) Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack +
Craig Mautner4a1cb222013-12-04 16:14:06 -0800719 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700720 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
721 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800722 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700723 }
724 }
725 return someActivityPaused;
726 }
727
Craig Mautnerde4ef022013-04-07 19:01:33 -0700728 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700729 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800730 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
731 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800732 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
733 final ActivityStack stack = stacks.get(stackNdx);
734 final ActivityRecord r = stack.mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700735 if (r != null && r.state != PAUSED && r.state != STOPPED && r.state != STOPPING) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800736 if (DEBUG_STATES) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700737 Slog.d(TAG_STATES,
738 "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800739 pausing = false;
740 } else {
741 return false;
742 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700743 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700744 }
745 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700746 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700747 }
748
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700749 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
750 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500751 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800752 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
753 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
754 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
755 final ActivityStack stack = stacks.get(stackNdx);
756 if (stack.mResumedActivity != null &&
757 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700758 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800759 }
760 }
761 }
762 }
763
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700764 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700765 sendWaitingVisibleReportLocked(r);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700766 }
767
768 void sendWaitingVisibleReportLocked(ActivityRecord r) {
769 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700770 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700771 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700772 if (w.who == null) {
773 changed = true;
774 w.timeout = false;
775 if (r != null) {
776 w.who = new ComponentName(r.info.packageName, r.info.name);
777 }
778 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
779 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700780 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700781 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700782 if (changed) {
783 mService.notifyAll();
784 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700785 }
786
787 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
788 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700789 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700790 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700791 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700792 if (w.who == null) {
793 changed = true;
794 w.timeout = timeout;
795 if (r != null) {
796 w.who = new ComponentName(r.info.packageName, r.info.name);
797 }
798 w.thisTime = thisTime;
799 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700800 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700801 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700802 if (changed) {
803 mService.notifyAll();
804 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700805 }
806
Craig Mautner29219d92013-04-16 20:19:12 -0700807 ActivityRecord topRunningActivityLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800808 final ActivityStack focusedStack = mFocusedStack;
Craig Mautner1602ec22013-05-12 10:24:27 -0700809 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
810 if (r != null) {
811 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700812 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700813
Craig Mautner4a1cb222013-12-04 16:14:06 -0800814 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800815 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800816 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
817 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700818 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700819 r = stack.topRunningActivityLocked(null);
820 if (r != null) {
821 return r;
822 }
823 }
824 }
825 return null;
826 }
827
Dianne Hackborn09233282014-04-30 11:33:59 -0700828 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700829 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800830 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
831 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800832 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800833 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800834 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800835 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
836 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner15df08a2015-04-01 12:17:18 -0700837 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800838 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700839 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700840 }
841 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700842
843 // The lists are already sorted from most recent to oldest. Just pull the most recent off
844 // each list and add it to list. Stop when all lists are empty or maxNum reached.
845 while (maxNum > 0) {
846 long mostRecentActiveTime = Long.MIN_VALUE;
847 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800848 final int numTaskLists = runningTaskLists.size();
849 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
850 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700851 if (!stackTaskList.isEmpty()) {
852 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
853 if (lastActiveTime > mostRecentActiveTime) {
854 mostRecentActiveTime = lastActiveTime;
855 selectedStackList = stackTaskList;
856 }
857 }
858 }
859 if (selectedStackList != null) {
860 list.add(selectedStackList.remove(0));
861 --maxNum;
862 } else {
863 break;
864 }
865 }
Craig Mautner20e72272013-04-01 13:45:53 -0700866 }
867
Craig Mautner23ac33b2013-04-01 16:26:35 -0700868 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700869 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700870 // Collect information about the target of the Intent.
871 ActivityInfo aInfo;
872 try {
873 ResolveInfo rInfo =
874 AppGlobals.getPackageManager().resolveIntent(
875 intent, resolvedType,
876 PackageManager.MATCH_DEFAULT_ONLY
877 | ActivityManagerService.STOCK_PM_FLAGS, userId);
878 aInfo = rInfo != null ? rInfo.activityInfo : null;
879 } catch (RemoteException e) {
880 aInfo = null;
881 }
882
883 if (aInfo != null) {
884 // Store the found target back into the intent, because now that
885 // we have it we never want to do this again. For example, if the
886 // user navigates back to this point in the history, we should
887 // always restart the exact same activity.
888 intent.setComponent(new ComponentName(
889 aInfo.applicationInfo.packageName, aInfo.name));
890
891 // Don't debug things in the system process
Man Caocfa78b22015-06-11 20:14:34 -0700892 if (!aInfo.processName.equals("system")) {
893 if ((startFlags & ActivityManager.START_FLAG_DEBUG) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700894 mService.setDebugApp(aInfo.processName, true, false);
895 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700896
Man Caocfa78b22015-06-11 20:14:34 -0700897 if ((startFlags & ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700898 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
899 }
Craig Mautner23ac33b2013-04-01 16:26:35 -0700900
Man Caocfa78b22015-06-11 20:14:34 -0700901 if ((startFlags & ActivityManager.START_FLAG_TRACK_ALLOCATION) != 0) {
902 mService.setTrackAllocationApp(aInfo.applicationInfo, aInfo.processName);
903 }
904
905 if (profilerInfo != null) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700906 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700907 }
908 }
909 }
910 return aInfo;
911 }
912
Craig Mautner299f9602015-01-26 09:47:33 -0800913 void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
914 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700915 startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
916 null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
917 null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
918 null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700919 0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
920 false /* componentSpecified */,
Filip Gruszczynskidd913622015-06-19 15:14:53 -0700921 null /* outActivity */, null /* container */, null /* inTask */);
922 if (inResumeTopActivity) {
923 // If we are in resume section already, home activity will be initialized, but not
924 // resumed (to avoid recursive resume) and will stay that way until something pokes it
925 // again. We need to schedule another resume.
926 scheduleResumeTopActivities();
927 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700928 }
929
Craig Mautner23ac33b2013-04-01 16:26:35 -0700930 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700931 String callingPackage, Intent intent, String resolvedType,
932 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700933 IBinder resultTo, String resultWho, int requestCode, int startFlags,
934 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -0700935 Bundle options, boolean ignoreTargetSecurity, int userId,
936 IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700937 // Refuse possible leaked file descriptors
938 if (intent != null && intent.hasFileDescriptors()) {
939 throw new IllegalArgumentException("File descriptors passed in Intent");
940 }
941 boolean componentSpecified = intent.getComponent() != null;
942
943 // Don't modify the client's object!
944 intent = new Intent(intent);
945
946 // Collect information about the target of the Intent.
Craig Mautner15df08a2015-04-01 12:17:18 -0700947 ActivityInfo aInfo =
948 resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700949
Craig Mautnere0a38842013-12-16 16:14:02 -0800950 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700951 synchronized (mService) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800952 if (container != null && container.mParentActivity != null &&
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700953 container.mParentActivity.state != RESUMED) {
Craig Mautnerb9168362015-02-26 20:40:19 -0800954 // Cannot start a child activity if the parent is not resumed.
955 return ActivityManager.START_CANCELED;
956 }
Dianne Hackborn95465202014-09-15 16:21:55 -0700957 final int realCallingPid = Binder.getCallingPid();
958 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700959 int callingPid;
960 if (callingUid >= 0) {
961 callingPid = -1;
962 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700963 callingPid = realCallingPid;
964 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700965 } else {
966 callingPid = callingUid = -1;
967 }
968
Craig Mautnere0a38842013-12-16 16:14:02 -0800969 final ActivityStack stack;
970 if (container == null || container.mStack.isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -0800971 stack = mFocusedStack;
Craig Mautnere0a38842013-12-16 16:14:02 -0800972 } else {
973 stack = container.mStack;
974 }
Wale Ogunwale60454db2015-01-23 16:05:07 -0800975 stack.mConfigWillChange = config != null && mService.mConfiguration.diff(config) != 0;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700976 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700977 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978
979 final long origId = Binder.clearCallingIdentity();
980
981 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800982 (aInfo.applicationInfo.privateFlags
983 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700984 // This may be a heavy-weight process! Check to see if we already
985 // have another, different heavy-weight process running.
986 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
987 if (mService.mHeavyWeightProcess != null &&
988 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
989 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700990 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700991 if (caller != null) {
992 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
993 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700994 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700995 } else {
996 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700997 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700998 + intent.toString());
999 ActivityOptions.abort(options);
1000 return ActivityManager.START_PERMISSION_DENIED;
1001 }
1002 }
1003
1004 IIntentSender target = mService.getIntentSenderLocked(
1005 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -07001006 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -07001007 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
1008 | PendingIntent.FLAG_ONE_SHOT, null);
1009
1010 Intent newIntent = new Intent();
1011 if (requestCode >= 0) {
1012 // Caller is requesting a result.
1013 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
1014 }
1015 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
1016 new IntentSender(target));
1017 if (mService.mHeavyWeightProcess.activities.size() > 0) {
1018 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
1019 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
1020 hist.packageName);
1021 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
1022 hist.task.taskId);
1023 }
1024 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
1025 aInfo.packageName);
1026 newIntent.setFlags(intent.getFlags());
1027 newIntent.setClassName("android",
1028 HeavyWeightSwitcherActivity.class.getName());
1029 intent = newIntent;
1030 resolvedType = null;
1031 caller = null;
1032 callingUid = Binder.getCallingUid();
1033 callingPid = Binder.getCallingPid();
1034 componentSpecified = true;
1035 try {
1036 ResolveInfo rInfo =
1037 AppGlobals.getPackageManager().resolveIntent(
1038 intent, null,
1039 PackageManager.MATCH_DEFAULT_ONLY
1040 | ActivityManagerService.STOCK_PM_FLAGS, userId);
1041 aInfo = rInfo != null ? rInfo.activityInfo : null;
1042 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1043 } catch (RemoteException e) {
1044 aInfo = null;
1045 }
1046 }
1047 }
1048 }
1049
Dianne Hackborn91097de2014-04-04 18:02:06 -07001050 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
1051 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -07001052 requestCode, callingPid, callingUid, callingPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001053 realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001054 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001055
Craig Mautner85c11a82014-07-17 12:38:18 -07001056 Binder.restoreCallingIdentity(origId);
1057
Craig Mautnerde4ef022013-04-07 19:01:33 -07001058 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001059 // If the caller also wants to switch to a new configuration,
1060 // do so now. This allows a clean switch, as we are waiting
1061 // for the current activity to pause (so we will not destroy
1062 // it), and have not yet started the next activity.
1063 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
1064 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -07001065 stack.mConfigWillChange = false;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001066 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Craig Mautner23ac33b2013-04-01 16:26:35 -07001067 "Updating to new configuration after starting activity.");
Maxim Bogatov05075302015-05-19 18:33:08 -07001068 mService.updateConfigurationLocked(config, null, false);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001069 }
1070
Craig Mautner23ac33b2013-04-01 16:26:35 -07001071 if (outResult != null) {
1072 outResult.result = res;
1073 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001074 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001075 do {
1076 try {
1077 mService.wait();
1078 } catch (InterruptedException e) {
1079 }
1080 } while (!outResult.timeout && outResult.who == null);
1081 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001082 ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001083 if (r.nowVisible && r.state == RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001084 outResult.timeout = false;
1085 outResult.who = new ComponentName(r.info.packageName, r.info.name);
1086 outResult.totalTime = 0;
1087 outResult.thisTime = 0;
1088 } else {
1089 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001090 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001091 do {
1092 try {
1093 mService.wait();
1094 } catch (InterruptedException e) {
1095 }
1096 } while (!outResult.timeout && outResult.who == null);
1097 }
1098 }
1099 }
1100
1101 return res;
1102 }
1103 }
1104
1105 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
1106 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
1107 Bundle options, int userId) {
1108 if (intents == null) {
1109 throw new NullPointerException("intents is null");
1110 }
1111 if (resolvedTypes == null) {
1112 throw new NullPointerException("resolvedTypes is null");
1113 }
1114 if (intents.length != resolvedTypes.length) {
1115 throw new IllegalArgumentException("intents are length different than resolvedTypes");
1116 }
1117
Craig Mautner23ac33b2013-04-01 16:26:35 -07001118
1119 int callingPid;
1120 if (callingUid >= 0) {
1121 callingPid = -1;
1122 } else if (caller == null) {
1123 callingPid = Binder.getCallingPid();
1124 callingUid = Binder.getCallingUid();
1125 } else {
1126 callingPid = callingUid = -1;
1127 }
1128 final long origId = Binder.clearCallingIdentity();
1129 try {
1130 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001131 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001132 for (int i=0; i<intents.length; i++) {
1133 Intent intent = intents[i];
1134 if (intent == null) {
1135 continue;
1136 }
1137
1138 // Refuse possible leaked file descriptors
1139 if (intent != null && intent.hasFileDescriptors()) {
1140 throw new IllegalArgumentException("File descriptors passed in Intent");
1141 }
1142
1143 boolean componentSpecified = intent.getComponent() != null;
1144
1145 // Don't modify the client's object!
1146 intent = new Intent(intent);
1147
1148 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001149 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001150 // TODO: New, check if this is correct
1151 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1152
1153 if (aInfo != null &&
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001154 (aInfo.applicationInfo.privateFlags
1155 & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner23ac33b2013-04-01 16:26:35 -07001156 throw new IllegalArgumentException(
1157 "FLAG_CANT_SAVE_STATE not supported here");
1158 }
1159
1160 Bundle theseOptions;
1161 if (options != null && i == intents.length-1) {
1162 theseOptions = options;
1163 } else {
1164 theseOptions = null;
1165 }
Craig Mautner6170f732013-04-02 13:05:23 -07001166 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001167 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1168 callingPackage, callingPid, callingUid,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001169 0, theseOptions, false, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001170 if (res < 0) {
1171 return res;
1172 }
1173
1174 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1175 }
1176 }
1177 } finally {
1178 Binder.restoreCallingIdentity(origId);
1179 }
1180
1181 return ActivityManager.START_SUCCESS;
1182 }
1183
Craig Mautner2420ead2013-04-01 17:13:20 -07001184 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001185 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001186 throws RemoteException {
1187
Craig Mautner2568c3a2015-03-26 14:22:34 -07001188 if (andResume) {
1189 r.startFreezingScreenLocked(app, 0);
1190 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001191
Craig Mautner2568c3a2015-03-26 14:22:34 -07001192 // schedule launch ticks to collect information about slow apps.
1193 r.startLaunchTickingLocked();
1194 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001195
1196 // Have the window manager re-evaluate the orientation of
1197 // the screen based on the new activity order. Note that
1198 // as a result of this, it can call back into the activity
1199 // manager with a new orientation. We don't care about that,
1200 // because the activity is not currently running so we are
1201 // just restarting it anyway.
1202 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001203 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001204 mService.mConfiguration,
1205 r.mayFreezeScreenLocked(app) ? r.appToken : null);
Maxim Bogatov05075302015-05-19 18:33:08 -07001206 mService.updateConfigurationLocked(config, r, false);
Craig Mautner2420ead2013-04-01 17:13:20 -07001207 }
1208
1209 r.app = app;
1210 app.waitingToKill = null;
1211 r.launchCount++;
1212 r.lastLaunchTime = SystemClock.uptimeMillis();
1213
Wale Ogunwalee23149f2015-03-06 15:39:44 -08001214 if (DEBUG_ALL) Slog.v(TAG, "Launching: " + r);
Craig Mautner2420ead2013-04-01 17:13:20 -07001215
1216 int idx = app.activities.indexOf(r);
1217 if (idx < 0) {
1218 app.activities.add(r);
1219 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001220 mService.updateLruProcessLocked(app, true, null);
1221 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001222
Craig Mautner15df08a2015-04-01 12:17:18 -07001223 final TaskRecord task = r.task;
Benjamin Franz469dd582015-06-09 14:24:36 +01001224 if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE ||
1225 task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE_PRIV) {
Craig Mautner432f64e2015-05-20 14:59:57 -07001226 setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "mLockTaskAuth==LAUNCHABLE", false);
Craig Mautner15df08a2015-04-01 12:17:18 -07001227 }
1228
1229 final ActivityStack stack = task.stack;
Craig Mautner2420ead2013-04-01 17:13:20 -07001230 try {
1231 if (app.thread == null) {
1232 throw new RemoteException();
1233 }
1234 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08001235 List<ReferrerIntent> newIntents = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001236 if (andResume) {
1237 results = r.results;
1238 newIntents = r.newIntents;
1239 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001240 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1241 "Launching: " + r + " icicle=" + r.icicle + " with results=" + results
1242 + " newIntents=" + newIntents + " andResume=" + andResume);
Craig Mautner2420ead2013-04-01 17:13:20 -07001243 if (andResume) {
1244 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1245 r.userId, System.identityHashCode(r),
Craig Mautner15df08a2015-04-01 12:17:18 -07001246 task.taskId, r.shortComponentName);
Craig Mautner2420ead2013-04-01 17:13:20 -07001247 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001248 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001249 // Home process is the root process of the task.
Craig Mautner15df08a2015-04-01 12:17:18 -07001250 mService.mHomeProcess = task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001251 }
1252 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1253 r.sleeping = false;
1254 r.forceNewConfig = false;
1255 mService.showAskCompatModeDialogLocked(r);
1256 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001257 ProfilerInfo profilerInfo = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001258 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1259 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1260 mService.mProfileProc = app;
Craig Mautner2568c3a2015-03-26 14:22:34 -07001261 final String profileFile = mService.mProfileFile;
1262 if (profileFile != null) {
1263 ParcelFileDescriptor profileFd = mService.mProfileFd;
1264 if (profileFd != null) {
1265 try {
1266 profileFd = profileFd.dup();
1267 } catch (IOException e) {
1268 if (profileFd != null) {
1269 try {
1270 profileFd.close();
1271 } catch (IOException o) {
1272 }
1273 profileFd = null;
1274 }
1275 }
Craig Mautner2420ead2013-04-01 17:13:20 -07001276 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001277
1278 profilerInfo = new ProfilerInfo(profileFile, profileFd,
1279 mService.mSamplingInterval, mService.mAutoStopProfiler);
Craig Mautner2420ead2013-04-01 17:13:20 -07001280 }
1281 }
1282 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001283
Craig Mautner2568c3a2015-03-26 14:22:34 -07001284 if (andResume) {
1285 app.hasShownUi = true;
1286 app.pendingUiClean = true;
1287 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001288 app.forceProcessStateUpTo(mService.mTopProcessState);
Craig Mautner2420ead2013-04-01 17:13:20 -07001289 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001290 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001291 new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,
Craig Mautner15df08a2015-04-01 12:17:18 -07001292 task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001293 newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001294
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001295 if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001296 // This may be a heavy-weight process! Note that the package
1297 // manager will ensure that only activity can run in the main
1298 // process of the .apk, which is the only thing that will be
1299 // considered heavy-weight.
1300 if (app.processName.equals(app.info.packageName)) {
1301 if (mService.mHeavyWeightProcess != null
1302 && mService.mHeavyWeightProcess != app) {
1303 Slog.w(TAG, "Starting new heavy weight process " + app
1304 + " when already running "
1305 + mService.mHeavyWeightProcess);
1306 }
1307 mService.mHeavyWeightProcess = app;
1308 Message msg = mService.mHandler.obtainMessage(
1309 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1310 msg.obj = r;
1311 mService.mHandler.sendMessage(msg);
1312 }
1313 }
1314
1315 } catch (RemoteException e) {
1316 if (r.launchFailed) {
1317 // This is the second time we failed -- finish activity
1318 // and give up.
1319 Slog.e(TAG, "Second failure launching "
1320 + r.intent.getComponent().flattenToShortString()
1321 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001322 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001323 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1324 "2nd-crash", false);
1325 return false;
1326 }
1327
1328 // This is the first time we failed -- restart process and
1329 // retry.
1330 app.activities.remove(r);
1331 throw e;
1332 }
1333
1334 r.launchFailed = false;
1335 if (stack.updateLRUListLocked(r)) {
1336 Slog.w(TAG, "Activity " + r
1337 + " being launched, but already in LRU list");
1338 }
1339
1340 if (andResume) {
1341 // As part of the process of launching, ActivityThread also performs
1342 // a resume.
1343 stack.minimalResumeActivityLocked(r);
1344 } else {
1345 // This activity is not starting in the resumed state... which
1346 // should look like we asked it to pause+stop (but remain visible),
1347 // and it has done so and reported back the current icicle and
1348 // other state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001349 if (DEBUG_STATES) Slog.v(TAG_STATES,
1350 "Moving to STOPPED: " + r + " (starting in stopped state)");
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001351 r.state = STOPPED;
Craig Mautner2420ead2013-04-01 17:13:20 -07001352 r.stopped = true;
1353 }
1354
1355 // Launch the new version setup screen if needed. We do this -after-
1356 // launching the initial activity (that is, home), so that it can have
1357 // a chance to initialize itself while in the background, making the
1358 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001359 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001360 mService.startSetupActivityLocked();
1361 }
1362
Dianne Hackborn465fa392014-09-14 14:21:18 -07001363 // Update any services we are bound to that might care about whether
1364 // their client may have activities.
1365 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1366
Craig Mautner2420ead2013-04-01 17:13:20 -07001367 return true;
1368 }
1369
Craig Mautnere79d42682013-04-01 19:01:53 -07001370 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001371 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001372 // Is this activity's application already running?
1373 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001374 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001375
1376 r.task.stack.setLaunchTime(r);
1377
1378 if (app != null && app.thread != null) {
1379 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001380 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1381 || !"android".equals(r.info.packageName)) {
1382 // Don't add this if it is a platform component that is marked
1383 // to run in multiple processes, because this is actually
1384 // part of the framework so doesn't make sense to track as a
1385 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001386 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1387 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001388 }
George Mount2c92c972014-03-20 09:38:23 -07001389 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001390 return;
1391 } catch (RemoteException e) {
1392 Slog.w(TAG, "Exception when starting activity "
1393 + r.intent.getComponent().flattenToShortString(), e);
1394 }
1395
1396 // If a dead object exception was thrown -- fall through to
1397 // restart the application.
1398 }
1399
1400 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001401 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001402 }
1403
Craig Mautner6170f732013-04-02 13:05:23 -07001404 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001405 Intent intent, String resolvedType, ActivityInfo aInfo,
1406 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1407 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001408 int callingPid, int callingUid, String callingPackage,
1409 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001410 boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
1411 ActivityContainer container, TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001412 int err = ActivityManager.START_SUCCESS;
1413
1414 ProcessRecord callerApp = null;
1415 if (caller != null) {
1416 callerApp = mService.getRecordForAppLocked(caller);
1417 if (callerApp != null) {
1418 callingPid = callerApp.pid;
1419 callingUid = callerApp.info.uid;
1420 } else {
1421 Slog.w(TAG, "Unable to find app for caller " + caller
1422 + " (pid=" + callingPid + ") when starting: "
1423 + intent.toString());
1424 err = ActivityManager.START_PERMISSION_DENIED;
1425 }
1426 }
1427
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001428 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1429
Craig Mautner6170f732013-04-02 13:05:23 -07001430 if (err == ActivityManager.START_SUCCESS) {
Craig Mautner6170f732013-04-02 13:05:23 -07001431 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001432 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001433 + " on display " + (container == null ? (mFocusedStack == null ?
1434 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1435 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1436 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001437 }
1438
1439 ActivityRecord sourceRecord = null;
1440 ActivityRecord resultRecord = null;
1441 if (resultTo != null) {
1442 sourceRecord = isInAnyStackLocked(resultTo);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001443 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
1444 "Will send result to " + resultTo + " " + sourceRecord);
Craig Mautner6170f732013-04-02 13:05:23 -07001445 if (sourceRecord != null) {
1446 if (requestCode >= 0 && !sourceRecord.finishing) {
1447 resultRecord = sourceRecord;
1448 }
1449 }
1450 }
Craig Mautner6170f732013-04-02 13:05:23 -07001451
Dianne Hackborn91097de2014-04-04 18:02:06 -07001452 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001453
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001454 if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001455 // Transfer the result target from the source activity to the new
1456 // one being started, including any failures.
1457 if (requestCode >= 0) {
1458 ActivityOptions.abort(options);
1459 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1460 }
1461 resultRecord = sourceRecord.resultTo;
Wale Ogunwale7d701172015-03-11 15:36:30 -07001462 if (resultRecord != null && !resultRecord.isInStackLocked()) {
1463 resultRecord = null;
1464 }
Craig Mautner6170f732013-04-02 13:05:23 -07001465 resultWho = sourceRecord.resultWho;
1466 requestCode = sourceRecord.requestCode;
1467 sourceRecord.resultTo = null;
1468 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001469 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001470 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001471 if (sourceRecord.launchedFromUid == callingUid) {
1472 // The new activity is being launched from the same uid as the previous
1473 // activity in the flow, and asking to forward its result back to the
1474 // previous. In this case the activity is serving as a trampoline between
1475 // the two, so we also want to update its launchedFromPackage to be the
1476 // same as the previous activity. Note that this is safe, since we know
1477 // these two packages come from the same uid; the caller could just as
1478 // well have supplied that same package name itself. This specifially
1479 // deals with the case of an intent picker/chooser being launched in the app
1480 // flow to redirect to an activity picked by the user, where we want the final
1481 // activity to consider it to have been launched by the previous app activity.
1482 callingPackage = sourceRecord.launchedFromPackage;
1483 }
Craig Mautner6170f732013-04-02 13:05:23 -07001484 }
1485
1486 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1487 // We couldn't find a class that can handle the given Intent.
1488 // That's the end of that!
1489 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1490 }
1491
1492 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1493 // We couldn't find the specific class specified in the Intent.
1494 // Also the end of the line.
1495 err = ActivityManager.START_CLASS_NOT_FOUND;
1496 }
1497
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07001498 if (err == ActivityManager.START_SUCCESS
1499 && !isCurrentProfileLocked(userId)
1500 && (aInfo.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
1501 // Trying to launch a background activity that doesn't show for all users.
1502 err = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY;
1503 }
1504
Dianne Hackborn91097de2014-04-04 18:02:06 -07001505 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1506 && sourceRecord.task.voiceSession != null) {
1507 // If this activity is being launched as part of a voice session, we need
1508 // to ensure that it is safe to do so. If the upcoming activity will also
1509 // be part of the voice session, we can only launch it if it has explicitly
1510 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001511 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001512 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1513 try {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001514 intent.addCategory(Intent.CATEGORY_VOICE);
Dianne Hackborn95465202014-09-15 16:21:55 -07001515 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1516 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001517 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1518 }
1519 } catch (RemoteException e) {
1520 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1521 }
1522 }
1523 }
1524
1525 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1526 // If the caller is starting a new voice session, just make sure the target
1527 // is actually allowing it to run this way.
1528 try {
1529 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1530 intent, resolvedType)) {
1531 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1532 }
1533 } catch (RemoteException e) {
1534 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1535 }
1536 }
1537
louis_changcd5d1982014-08-01 10:09:08 +08001538 final ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1539
Craig Mautner6170f732013-04-02 13:05:23 -07001540 if (err != ActivityManager.START_SUCCESS) {
1541 if (resultRecord != null) {
1542 resultStack.sendActivityResultLocked(-1,
1543 resultRecord, resultWho, requestCode,
1544 Activity.RESULT_CANCELED, null);
1545 }
Craig Mautner6170f732013-04-02 13:05:23 -07001546 ActivityOptions.abort(options);
1547 return err;
1548 }
1549
Svetoslav7008b512015-06-24 18:47:07 -07001550 boolean abort = false;
1551
Svet Ganov99b60432015-06-27 13:15:22 -07001552 final int startAnyPerm = mService.checkPermission(
1553 START_ANY_ACTIVITY, callingPid, callingUid);
1554
1555 if (startAnyPerm != PERMISSION_GRANTED) {
1556 final int componentRestriction = getComponentRestrictionForCallingPackage(
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001557 aInfo, callingPackage, callingPid, callingUid, ignoreTargetSecurity);
Svet Ganov99b60432015-06-27 13:15:22 -07001558 final int actionRestriction = getActionRestrictionForCallingPackage(
1559 intent.getAction(), callingPackage, callingPid, callingUid);
1560
1561 if (componentRestriction == ACTIVITY_RESTRICTION_PERMISSION
1562 || actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1563 if (resultRecord != null) {
1564 resultStack.sendActivityResultLocked(-1,
1565 resultRecord, resultWho, requestCode,
1566 Activity.RESULT_CANCELED, null);
1567 }
1568 String msg;
1569 if (actionRestriction == ACTIVITY_RESTRICTION_PERMISSION) {
1570 msg = "Permission Denial: starting " + intent.toString()
1571 + " from " + callerApp + " (pid=" + callingPid
1572 + ", uid=" + callingUid + ")" + " with revoked permission "
1573 + ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction());
1574 } else if (!aInfo.exported) {
1575 msg = "Permission Denial: starting " + intent.toString()
1576 + " from " + callerApp + " (pid=" + callingPid
1577 + ", uid=" + callingUid + ")"
1578 + " not exported from uid " + aInfo.applicationInfo.uid;
1579 } else {
1580 msg = "Permission Denial: starting " + intent.toString()
1581 + " from " + callerApp + " (pid=" + callingPid
1582 + ", uid=" + callingUid + ")"
1583 + " requires " + aInfo.permission;
1584 }
1585 Slog.w(TAG, msg);
1586 throw new SecurityException(msg);
1587 }
1588
1589 if (actionRestriction == ACTIVITY_RESTRICTION_APPOP) {
1590 String message = "Appop Denial: starting " + intent.toString()
1591 + " from " + callerApp + " (pid=" + callingPid
1592 + ", uid=" + callingUid + ")"
1593 + " requires " + AppOpsManager.permissionToOp(
1594 ACTION_TO_RUNTIME_PERMISSION.get(intent.getAction()));
1595 Slog.w(TAG, message);
1596 abort = true;
1597 } else if (componentRestriction == ACTIVITY_RESTRICTION_APPOP) {
1598 String message = "Appop Denial: starting " + intent.toString()
1599 + " from " + callerApp + " (pid=" + callingPid
1600 + ", uid=" + callingUid + ")"
1601 + " requires appop " + AppOpsManager.permissionToOp(aInfo.permission);
1602 Slog.w(TAG, message);
1603 abort = true;
1604 }
Svetoslav7008b512015-06-24 18:47:07 -07001605 }
1606
1607 abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001608 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001609
Craig Mautner6170f732013-04-02 13:05:23 -07001610 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001611 try {
1612 // The Intent we give to the watcher has the extra data
1613 // stripped off, since it can contain private information.
1614 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001615 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001616 aInfo.applicationInfo.packageName);
1617 } catch (RemoteException e) {
1618 mService.mController = null;
1619 }
Ben Gruver5e207332013-04-03 17:41:37 -07001620 }
Craig Mautner6170f732013-04-02 13:05:23 -07001621
Ben Gruver5e207332013-04-03 17:41:37 -07001622 if (abort) {
1623 if (resultRecord != null) {
1624 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001625 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001626 }
Ben Gruver5e207332013-04-03 17:41:37 -07001627 // We pretend to the caller that it was really started, but
1628 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001629 ActivityOptions.abort(options);
1630 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001631 }
1632
1633 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001634 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Dianne Hackbornfb81d092015-08-03 17:14:46 -07001635 requestCode, componentSpecified, voiceSession != null, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001636 if (outActivity != null) {
1637 outActivity[0] = r;
1638 }
1639
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001640 if (r.appTimeTracker == null && sourceRecord != null) {
1641 // If the caller didn't specify an explicit time tracker, we want to continue
1642 // tracking under any it has.
1643 r.appTimeTracker = sourceRecord.appTimeTracker;
1644 }
1645
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001646 final ActivityStack stack = mFocusedStack;
Dianne Hackborn91097de2014-04-04 18:02:06 -07001647 if (voiceSession == null && (stack.mResumedActivity == null
1648 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001649 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1650 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001651 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001652 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001653 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001654 ActivityOptions.abort(options);
1655 return ActivityManager.START_SWITCHES_CANCELED;
1656 }
1657 }
1658
1659 if (mService.mDidAppSwitch) {
1660 // This is the second allowed switch since we stopped switches,
1661 // so now just generally allow switches. Use case: user presses
1662 // home (switches disabled, switch to home, mDidAppSwitch now true);
1663 // user taps a home icon (coming from home so allowed, we hit here
1664 // and now allow anyone to switch again).
1665 mService.mAppSwitchesAllowedTime = 0;
1666 } else {
1667 mService.mDidAppSwitch = true;
1668 }
1669
Craig Mautneree36c772014-07-16 14:56:05 -07001670 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001671
Dianne Hackborn91097de2014-04-04 18:02:06 -07001672 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001673 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001674
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001675 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001676 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001677 // activity start, but we are not actually doing an activity
1678 // switch... just dismiss the keyguard now, because we
1679 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001680 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001681 }
1682 return err;
1683 }
1684
Svet Ganov99b60432015-06-27 13:15:22 -07001685 private int getComponentRestrictionForCallingPackage(ActivityInfo activityInfo,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001686 String callingPackage, int callingPid, int callingUid, boolean ignoreTargetSecurity) {
Svet Ganov99b60432015-06-27 13:15:22 -07001687 if (activityInfo.permission == null) {
1688 return ACTIVITY_RESTRICTION_NONE;
1689 }
1690
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001691 if (!ignoreTargetSecurity && mService.checkComponentPermission(activityInfo.permission,
1692 callingPid, callingUid, activityInfo.applicationInfo.uid, activityInfo.exported)
Svet Ganov99b60432015-06-27 13:15:22 -07001693 == PackageManager.PERMISSION_DENIED) {
1694 return ACTIVITY_RESTRICTION_PERMISSION;
1695 }
1696
1697 final int opCode = AppOpsManager.permissionToOpCode(activityInfo.permission);
1698 if (opCode == AppOpsManager.OP_NONE) {
1699 return ACTIVITY_RESTRICTION_NONE;
1700 }
1701
1702 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1703 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001704 if (!ignoreTargetSecurity) {
1705 return ACTIVITY_RESTRICTION_APPOP;
1706 }
Svet Ganov99b60432015-06-27 13:15:22 -07001707 }
1708
1709 return ACTIVITY_RESTRICTION_NONE;
1710 }
1711
Svetoslav7008b512015-06-24 18:47:07 -07001712 private int getActionRestrictionForCallingPackage(String action,
1713 String callingPackage, int callingPid, int callingUid) {
1714 if (action == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001715 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001716 }
1717
1718 String permission = ACTION_TO_RUNTIME_PERMISSION.get(action);
1719 if (permission == null) {
Svet Ganov99b60432015-06-27 13:15:22 -07001720 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001721 }
1722
1723 final PackageInfo packageInfo;
1724 try {
1725 packageInfo = mService.mContext.getPackageManager()
1726 .getPackageInfo(callingPackage, PackageManager.GET_PERMISSIONS);
1727 } catch (PackageManager.NameNotFoundException e) {
1728 Slog.i(TAG, "Cannot find package info for " + callingPackage);
Svet Ganov99b60432015-06-27 13:15:22 -07001729 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001730 }
1731
1732 if (!ArrayUtils.contains(packageInfo.requestedPermissions, permission)) {
Svet Ganov99b60432015-06-27 13:15:22 -07001733 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001734 }
1735
1736 if (mService.checkPermission(permission, callingPid, callingUid) ==
1737 PackageManager.PERMISSION_DENIED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001738 return ACTIVITY_RESTRICTION_PERMISSION;
Svetoslav7008b512015-06-24 18:47:07 -07001739 }
1740
1741 final int opCode = AppOpsManager.permissionToOpCode(permission);
1742 if (opCode == AppOpsManager.OP_NONE) {
Svet Ganov99b60432015-06-27 13:15:22 -07001743 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001744 }
1745
1746 if (mService.mAppOpsService.noteOperation(opCode, callingUid,
1747 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Svet Ganov99b60432015-06-27 13:15:22 -07001748 return ACTIVITY_RESTRICTION_APPOP;
Svetoslav7008b512015-06-24 18:47:07 -07001749 }
1750
Svet Ganov99b60432015-06-27 13:15:22 -07001751 return ACTIVITY_RESTRICTION_NONE;
Svetoslav7008b512015-06-24 18:47:07 -07001752 }
1753
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001754 ActivityStack computeStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001755 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001756
1757 // On leanback only devices we should keep all activities in the same stack.
1758 if (!mLeanbackOnlyDevice &&
1759 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001760
1761 ActivityStack stack;
1762
Wale Ogunwale7d701172015-03-11 15:36:30 -07001763 if (task != null && task.stack != null) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001764 stack = task.stack;
1765 if (stack.isOnHomeDisplay()) {
1766 if (mFocusedStack != stack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001767 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
1768 "computeStackFocus: Setting " + "focused stack to r=" + r
1769 + " task=" + task);
Craig Mautnere0a38842013-12-16 16:14:02 -08001770 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001771 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001772 "computeStackFocus: Focused stack already=" + mFocusedStack);
Craig Mautnere0a38842013-12-16 16:14:02 -08001773 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001774 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001775 return stack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001776 }
1777
Craig Mautnere0a38842013-12-16 16:14:02 -08001778 final ActivityContainer container = r.mInitialActivityContainer;
1779 if (container != null) {
1780 // The first time put it on the desired stack, after this put on task stack.
1781 r.mInitialActivityContainer = null;
1782 return container.mStack;
1783 }
1784
Craig Mautner1b4bf852014-05-26 15:06:32 -07001785 if (mFocusedStack != mHomeStack && (!newTask ||
1786 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001787 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001788 "computeStackFocus: Have a focused stack=" + mFocusedStack);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001789 return mFocusedStack;
1790 }
1791
Wale Ogunwale706ed792015-08-02 10:29:44 -07001792 // We first try to put the task in the first dynamic stack.
Craig Mautnere0a38842013-12-16 16:14:02 -08001793 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1794 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001795 stack = homeDisplayStacks.get(stackNdx);
Wale Ogunwale706ed792015-08-02 10:29:44 -07001796 final boolean isDynamicStack = stack.mStackId >= FIRST_DYNAMIC_STACK_ID;
1797 if (isDynamicStack) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001798 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS,
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001799 "computeStackFocus: Setting focused stack=" + stack);
1800 return stack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001801 }
1802 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001803
Wale Ogunwale706ed792015-08-02 10:29:44 -07001804 // If there is no suitable dynamic stack then we figure out which static stack to use.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001805 stack = getStack(
Wale Ogunwale706ed792015-08-02 10:29:44 -07001806 task != null
1807 ? task.getLaunchStackId(mFocusedStack) : FULLSCREEN_WORKSPACE_STACK_ID,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001808 true /*createStaticStackIfNeeded*/,
1809 true /*createOnTop*/);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07001810 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG_FOCUS, "computeStackFocus: New stack r="
1811 + r + " stackId=" + stack.mStackId);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001812 return stack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001813 }
1814 return mHomeStack;
1815 }
1816
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001817 boolean setFocusedStack(ActivityRecord r, String reason) {
1818 if (r == null) {
1819 // Not sure what you are trying to do, but it is not going to work...
1820 return false;
Craig Mautner29219d92013-04-16 20:19:12 -07001821 }
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001822 final TaskRecord task = r.task;
1823 if (task == null || task.stack == null) {
1824 Slog.w(TAG, "Can't set focus stack for r=" + r + " task=" + task);
1825 return false;
1826 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -07001827 task.stack.moveToFront(reason, task);
Wale Ogunwalecb82f302015-02-25 07:53:40 -08001828 return true;
Craig Mautner29219d92013-04-16 20:19:12 -07001829 }
1830
Craig Mautner8f5f7e92015-01-26 18:03:13 -08001831 final int startActivityUncheckedLocked(final ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001832 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001833 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001834 final Intent intent = r.intent;
1835 final int callingUid = r.launchedFromUid;
1836
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001837 // In some flows in to this function, we retrieve the task record and hold on to it
1838 // without a lock before calling back in to here... so the task at this point may
1839 // not actually be in recents. Check for that, and if it isn't in recents just
1840 // consider it invalid.
1841 if (inTask != null && !inTask.inRecents) {
1842 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1843 inTask = null;
1844 }
1845
Craig Mautnerad400af2014-08-13 16:41:36 -07001846 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001847 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1848 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001849
Craig Mautnera228ae92014-07-09 05:44:55 -07001850 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001851 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001852 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001853 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1854 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1855 "\"singleInstance\" or \"singleTask\"");
1856 launchFlags &=
1857 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1858 } else {
1859 switch (r.info.documentLaunchMode) {
1860 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1861 break;
1862 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1863 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1864 break;
1865 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1866 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1867 break;
1868 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1869 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1870 break;
1871 }
1872 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001873
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001874 final boolean launchTaskBehind = r.mLaunchTaskBehind
1875 && !launchSingleTask && !launchSingleInstance
1876 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001877
Wale Ogunwale7d701172015-03-11 15:36:30 -07001878 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0
1879 && r.resultTo.task.stack != null) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001880 // For whatever reason this activity is being launched into a new
1881 // task... yet the caller has requested a result back. Well, that
1882 // is pretty messed up, so instead immediately send back a cancel
1883 // and let the new task continue launched as normal without a
1884 // dependency on its originator.
1885 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1886 r.resultTo.task.stack.sendActivityResultLocked(-1,
1887 r.resultTo, r.resultWho, r.requestCode,
1888 Activity.RESULT_CANCELED, null);
1889 r.resultTo = null;
1890 }
1891
1892 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1893 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1894 }
1895
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001896 // If we are actually going to launch in to a new task, there are some cases where
1897 // we further want to do multiple task.
1898 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1899 if (launchTaskBehind
1900 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1901 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1902 }
1903 }
1904
Craig Mautner8849a5e2013-04-02 16:41:03 -07001905 // We'll invoke onUserLeaving before onPause only if the launching
1906 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001907 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001908 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
1909 "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001910
1911 // If the caller has asked not to resume at this point, we make note
1912 // of this in the record so that we can skip it when trying to find
1913 // the top running activity.
1914 if (!doResume) {
1915 r.delayedResume = true;
1916 }
1917
Craig Mautnera254cd72014-05-25 16:47:39 -07001918 ActivityRecord notTop =
1919 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920
1921 // If the onlyIfNeeded flag is set, then we can do this if the activity
1922 // being launched is the same as the one making the call... or, as
1923 // a special case, if we do not know the caller then we count the
1924 // current top activity as the caller.
1925 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1926 ActivityRecord checkedCaller = sourceRecord;
1927 if (checkedCaller == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001928 checkedCaller = mFocusedStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001929 }
1930 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1931 // Caller is not the same as launcher, so always needed.
1932 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1933 }
1934 }
1935
Craig Mautner8849a5e2013-04-02 16:41:03 -07001936 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001937 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001938
1939 // If the caller is not coming from another activity, but has given us an
1940 // explicit task into which they would like us to launch the new activity,
1941 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001942 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1943 final Intent baseIntent = inTask.getBaseIntent();
1944 final ActivityRecord root = inTask.getRootActivity();
1945 if (baseIntent == null) {
1946 ActivityOptions.abort(options);
1947 throw new IllegalArgumentException("Launching into task without base intent: "
1948 + inTask);
1949 }
1950
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001951 // If this task is empty, then we are adding the first activity -- it
1952 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001953 if (launchSingleInstance || launchSingleTask) {
1954 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1955 ActivityOptions.abort(options);
1956 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1957 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001958 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001959 if (root != null) {
1960 ActivityOptions.abort(options);
1961 throw new IllegalArgumentException("Caller with inTask " + inTask
1962 + " has root " + root + " but target is singleInstance/Task");
1963 }
1964 }
1965
1966 // If task is empty, then adopt the interesting intent launch flags in to the
1967 // activity being started.
1968 if (root == null) {
1969 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1970 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1971 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1972 launchFlags = (launchFlags&~flagsOfInterest)
1973 | (baseIntent.getFlags()&flagsOfInterest);
1974 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001975 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001976 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001977
Dianne Hackborn962d5352014-08-29 16:27:40 -07001978 // If the task is not empty and the caller is asking to start it as the root
1979 // of a new task, then we don't actually want to start this on the task. We
1980 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001981 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001982 addingToTask = false;
1983
1984 } else {
1985 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001986 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001987
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001988 reuseTask = inTask;
1989 } else {
1990 inTask = null;
1991 }
1992
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001993 if (inTask == null) {
1994 if (sourceRecord == null) {
1995 // This activity is not being started from another... in this
1996 // case we -always- start a new task.
1997 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1998 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1999 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2000 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2001 }
2002 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
2003 // The original activity who is starting us is running as a single
2004 // instance... this new activity it is starting must go on its
2005 // own task.
2006 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2007 } else if (launchSingleInstance || launchSingleTask) {
2008 // The activity being started is a single instance... it always
2009 // gets launched into its own task.
2010 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2011 }
2012 }
2013
2014 ActivityInfo newTaskInfo = null;
2015 Intent newTaskIntent = null;
2016 ActivityStack sourceStack;
2017 if (sourceRecord != null) {
2018 if (sourceRecord.finishing) {
2019 // If the source is finishing, we can't further count it as our source. This
2020 // is because the task it is associated with may now be empty and on its way out,
2021 // so we don't want to blindly throw it in to that task. Instead we will take
2022 // the NEW_TASK flow and try to find a task for it. But save the task information
2023 // so it can be used when creating the new task.
2024 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
2025 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
2026 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
2027 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
2028 newTaskInfo = sourceRecord.info;
2029 newTaskIntent = sourceRecord.task.intent;
2030 }
2031 sourceRecord = null;
2032 sourceStack = null;
2033 } else {
2034 sourceStack = sourceRecord.task.stack;
2035 }
2036 } else {
2037 sourceStack = null;
2038 }
2039
2040 boolean movedHome = false;
2041 ActivityStack targetStack;
2042
2043 intent.setFlags(launchFlags);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002044 final boolean noAnimation = (launchFlags & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002045
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002046 // We may want to try to place the new activity in to an existing task. We always
2047 // do this if the target activity is singleTask or singleInstance; we will also do
2048 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
2049 // us to still place it in a new task: multi task, always doc mode, or being asked to
2050 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07002051 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
2052 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07002053 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002054 // If bring to front is requested, and no result is requested and we have not
2055 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07002056 // we can find a task that was started with this same
2057 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002058 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002059 // See if there is a task to bring to the front. If this is
2060 // a SINGLE_INSTANCE activity, there can be one and only one
2061 // instance of it in the history, and it is always in its own
2062 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07002063 ActivityRecord intentActivity = !launchSingleInstance ?
2064 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002065 if (intentActivity != null) {
Jason Monk25d237b2015-06-19 10:39:39 -04002066 // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused
2067 // but still needs to be a lock task mode violation since the task gets
2068 // cleared out and the device would otherwise leave the locked task.
2069 if (isLockTaskModeViolation(intentActivity.task,
2070 (launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
2071 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
Craig Mautner6cd6cec2015-04-01 00:02:12 -07002072 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07002073 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08002074 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2075 }
Craig Mautner29219d92013-04-16 20:19:12 -07002076 if (r.task == null) {
2077 r.task = intentActivity.task;
2078 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002079 if (intentActivity.task.intent == null) {
2080 // This task was started because of movement of
2081 // the activity based on affinity... now that we
2082 // are actually launching it, we can assign the
2083 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07002084 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002085 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002086 targetStack = intentActivity.task.stack;
2087 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002088 // If the target task is not in the front, then we need
2089 // to bring it to the front... except... well, with
2090 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
2091 // to have the same behavior as if a new instance was
2092 // being started, which means not bringing it to the front
2093 // if the caller is not itself in the front.
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002094 final ActivityStack focusStack = getFocusedStack();
2095 ActivityRecord curTop = (focusStack == null)
2096 ? null : focusStack.topRunningNonDelayedActivityLocked(notTop);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002097 boolean movedToFront = false;
Craig Mautner7504d7b2013-09-17 10:50:53 -07002098 if (curTop != null && (curTop.task != intentActivity.task ||
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002099 curTop.task != focusStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002100 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07002101 if (sourceRecord == null || (sourceStack.topActivity() != null &&
2102 sourceStack.topActivity().task == sourceRecord.task)) {
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002103 // We really do want to push this one into the user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07002104 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002105 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
2106 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002107 movedHome = true;
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002108 targetStack.moveTaskToFrontLocked(intentActivity.task, noAnimation,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002109 options, r.appTimeTracker, "bringingFoundTaskToFront");
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002110 movedToFront = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002111 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07002112 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2113 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07002114 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07002115 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002116 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002117 options = null;
2118 }
2119 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002120 if (!movedToFront) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002121 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Bring to front target: " + targetStack
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002122 + " from " + intentActivity);
2123 targetStack.moveToFront("intentActivityFound");
2124 }
2125
Craig Mautner8849a5e2013-04-02 16:41:03 -07002126 // If the caller has requested that the target task be
2127 // reset, then do so.
2128 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2129 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
2130 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002131 if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002132 // We don't need to start a new activity, and
2133 // the client said not to do anything if that
2134 // is the case, so this is it! And for paranoia, make
2135 // sure we have correctly resumed the top activity.
2136 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002137 resumeTopActivitiesLocked(targetStack, null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002138
2139 // Make sure to notify Keyguard as well if we are not running an app
2140 // transition later.
2141 if (!movedToFront) {
2142 notifyActivityDrawnForKeyguard();
2143 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002144 } else {
2145 ActivityOptions.abort(options);
2146 }
2147 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2148 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002149 if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002150 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002151 // The caller has requested to completely replace any
2152 // existing task with its new activity. Well that should
2153 // not be too hard...
2154 reuseTask = intentActivity.task;
2155 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07002156 reuseTask.setIntent(r);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002157 } else if ((launchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07002158 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002159 // In this situation we want to remove all activities
2160 // from the task up to the one being started. In most
2161 // cases this means we are resetting the task to its
2162 // initial state.
2163 ActivityRecord top =
2164 intentActivity.task.performClearTaskLocked(r, launchFlags);
2165 if (top != null) {
2166 if (top.frontOfTask) {
2167 // Activity aliases may mean we use different
2168 // intents for the top activity, so make sure
2169 // the task now has the identity of the new
2170 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07002171 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 }
2173 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
2174 r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002175 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002176 } else {
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002177 // A special case: we need to start the activity because it is not
2178 // currently running, and the caller has asked to clear the current
2179 // task to have this activity at the top.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002180 addingToTask = true;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002181 // Now pretend like this activity is being started by the top of its
2182 // task, so it is put in the right place.
Craig Mautner8849a5e2013-04-02 16:41:03 -07002183 sourceRecord = intentActivity;
Wale Ogunwale6eeed832015-06-10 12:03:23 -07002184 TaskRecord task = sourceRecord.task;
2185 if (task != null && task.stack == null) {
2186 // Target stack got cleared when we all activities were removed
2187 // above. Go ahead and reset it.
2188 targetStack = computeStackFocus(sourceRecord, false /* newTask */);
2189 targetStack.addTask(
2190 task, !launchTaskBehind /* toTop */, false /* moving */);
2191 }
2192
Craig Mautner8849a5e2013-04-02 16:41:03 -07002193 }
2194 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
2195 // In this case the top activity on the task is the
2196 // same as the one being launched, so we take that
2197 // as a request to bring the task to the foreground.
2198 // If the top activity in the task is the root
2199 // activity, deliver this new intent to it if it
2200 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07002201 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07002202 && intentActivity.realActivity.equals(r.realActivity)) {
2203 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
2204 intentActivity.task);
2205 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07002206 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002207 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002208 intentActivity.deliverNewIntentLocked(callingUid, r.intent,
2209 r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002210 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
2211 // In this case we are launching the root activity
2212 // of the task, but with a different intent. We
2213 // should start a new instance on top.
2214 addingToTask = true;
2215 sourceRecord = intentActivity;
2216 }
2217 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
2218 // In this case an activity is being launched in to an
2219 // existing task, without resetting that task. This
2220 // is typically the situation of launching an activity
2221 // from a notification or shortcut. We want to place
2222 // the new activity on top of the current task.
2223 addingToTask = true;
2224 sourceRecord = intentActivity;
2225 } else if (!intentActivity.task.rootWasReset) {
2226 // In this case we are launching in to an existing task
2227 // that has not yet been started from its front door.
2228 // The current task has been brought to the front.
2229 // Ideally, we'd probably like to place this new task
2230 // at the bottom of its stack, but that's a little hard
2231 // to do with the current organization of the code so
2232 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07002233 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002234 }
2235 if (!addingToTask && reuseTask == null) {
2236 // We didn't do anything... but it was needed (a.k.a., client
2237 // don't use that intent!) And for paranoia, make
2238 // sure we have correctly resumed the top activity.
2239 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002240 targetStack.resumeTopActivityLocked(null, options);
Jorim Jaggi44f60cc2014-11-07 20:33:51 +01002241 if (!movedToFront) {
2242 // Make sure to notify Keyguard as well if we are not running an app
2243 // transition later.
2244 notifyActivityDrawnForKeyguard();
2245 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002246 } else {
2247 ActivityOptions.abort(options);
2248 }
2249 return ActivityManager.START_TASK_TO_FRONT;
2250 }
2251 }
2252 }
2253 }
2254
2255 //String uri = r.intent.toURI();
2256 //Intent intent2 = new Intent(uri);
2257 //Slog.i(TAG, "Given intent: " + r.intent);
2258 //Slog.i(TAG, "URI is: " + uri);
2259 //Slog.i(TAG, "To intent: " + intent2);
2260
2261 if (r.packageName != null) {
2262 // If the activity being launched is the same as the one currently
2263 // at the top, then we need to check if it should only be launched
2264 // once.
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002265 ActivityStack topStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002266 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002267 if (top != null && r.resultTo == null) {
2268 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2269 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07002270 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07002271 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002272 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
2273 top.task);
2274 // For paranoia, make sure we have correctly
2275 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002276 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002277 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07002278 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07002279 }
2280 ActivityOptions.abort(options);
2281 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2282 // We don't need to start a new activity, and
2283 // the client said not to do anything if that
2284 // is the case, so this is it!
2285 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2286 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002287 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002288 return ActivityManager.START_DELIVERED_TO_TOP;
2289 }
2290 }
2291 }
2292 }
2293
2294 } else {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002295 if (r.resultTo != null && r.resultTo.task.stack != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002296 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2297 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002298 }
2299 ActivityOptions.abort(options);
2300 return ActivityManager.START_CLASS_NOT_FOUND;
2301 }
2302
2303 boolean newTask = false;
2304 boolean keepCurTransition = false;
2305
Craig Mautnerbb742462014-07-07 15:28:55 -07002306 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002307 sourceRecord.task : null;
2308
Craig Mautner8849a5e2013-04-02 16:41:03 -07002309 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002310 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002311 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002312 newTask = true;
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002313 targetStack = computeStackFocus(r, newTask);
2314 targetStack.moveToFront("startingNewTask");
2315
Craig Mautner8849a5e2013-04-02 16:41:03 -07002316 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002317 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2318 newTaskInfo != null ? newTaskInfo : r.info,
2319 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002320 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2321 taskToAffiliate);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002322 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2323 "Starting new activity " + r + " in new task " + r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002324 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002325 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002326 }
Craig Mautner15df08a2015-04-01 12:17:18 -07002327 if (isLockTaskModeViolation(r.task)) {
2328 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2329 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2330 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002331 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002332 if ((launchFlags &
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002333 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
2334 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002335 // Caller wants to appear on home activity, so before starting
2336 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002337 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002338 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002339 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002340 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002341 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002342 if (isLockTaskModeViolation(sourceTask)) {
2343 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2344 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2345 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002346 targetStack = sourceTask.stack;
Craig Mautner299f9602015-01-26 09:47:33 -08002347 targetStack.moveToFront("sourceStackToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002348 final TaskRecord topTask = targetStack.topTask();
2349 if (topTask != sourceTask) {
Craig Mautner8f5f7e92015-01-26 18:03:13 -08002350 targetStack.moveTaskToFrontLocked(sourceTask, noAnimation, options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002351 r.appTimeTracker, "sourceTaskToFront");
Craig Mautner737fae22014-10-15 12:52:10 -07002352 }
Craig Mautnera228ae92014-07-09 05:44:55 -07002353 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002354 // In this case, we are adding the activity to an existing
2355 // task, but the caller has asked to clear that task if the
2356 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002357 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002358 keepCurTransition = true;
2359 if (top != null) {
2360 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002361 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002362 // For paranoia, make sure we have correctly
2363 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002364 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002365 if (doResume) {
2366 targetStack.resumeTopActivityLocked(null);
2367 }
2368 ActivityOptions.abort(options);
2369 return ActivityManager.START_DELIVERED_TO_TOP;
2370 }
2371 } else if (!addingToTask &&
2372 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2373 // In this case, we are launching an activity in our own task
2374 // that may already be running somewhere in the history, and
2375 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002376 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002377 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002378 final TaskRecord task = top.task;
2379 task.moveActivityToFrontLocked(top);
2380 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002381 top.updateOptionsLocked(options);
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002382 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Craig Mautner0f922742013-08-06 08:44:42 -07002383 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002384 if (doResume) {
2385 targetStack.resumeTopActivityLocked(null);
2386 }
2387 return ActivityManager.START_DELIVERED_TO_TOP;
2388 }
2389 }
2390 // An existing activity is starting this new activity, so we want
2391 // to keep the new one in the same task as the one that is starting
2392 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002393 r.setTask(sourceTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002394 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002395 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002396
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002397 } else if (inTask != null) {
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002398 // The caller is asking that the new activity be started in an explicit
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002399 // task it has provided to us.
2400 if (isLockTaskModeViolation(inTask)) {
2401 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2402 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2403 }
2404 targetStack = inTask.stack;
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002405 targetStack.moveTaskToFrontLocked(inTask, noAnimation, options, r.appTimeTracker,
2406 "inTaskToFront");
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002407
2408 // Check whether we should actually launch the new activity in to the task,
2409 // or just reuse the current activity on top.
2410 ActivityRecord top = inTask.getTopActivity();
2411 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2412 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2413 || launchSingleTop || launchSingleTask) {
2414 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2415 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2416 // We don't need to start a new activity, and
2417 // the client said not to do anything if that
2418 // is the case, so this is it!
2419 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2420 }
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002421 top.deliverNewIntentLocked(callingUid, r.intent, r.launchedFromPackage);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002422 return ActivityManager.START_DELIVERED_TO_TOP;
2423 }
2424 }
2425
Dianne Hackborn962d5352014-08-29 16:27:40 -07002426 if (!addingToTask) {
2427 // We don't actually want to have this activity added to the task, so just
2428 // stop here but still tell the caller that we consumed the intent.
2429 ActivityOptions.abort(options);
2430 return ActivityManager.START_TASK_TO_FRONT;
2431 }
2432
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002433 r.setTask(inTask, null);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002434 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002435 + " in explicit task " + r.task);
2436
Craig Mautner8849a5e2013-04-02 16:41:03 -07002437 } else {
2438 // This not being started from an existing activity, and not part
2439 // of a new task... just put it in the top task, though these days
2440 // this case should never happen.
Wale Ogunwalecb82f302015-02-25 07:53:40 -08002441 targetStack = computeStackFocus(r, newTask);
Craig Mautner299f9602015-01-26 09:47:33 -08002442 targetStack.moveToFront("addingToTopTask");
Craig Mautner1602ec22013-05-12 10:24:27 -07002443 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002444 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002445 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002446 mWindowManager.moveTaskToTop(r.task.taskId);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002447 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Starting new activity " + r
Craig Mautner8849a5e2013-04-02 16:41:03 -07002448 + " in new guessed " + r.task);
2449 }
2450
2451 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002452 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002453
Craig Mautner76e2a762014-06-24 09:05:43 -07002454 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2455 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2456 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002457 if (newTask) {
2458 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2459 }
2460 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002461 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002462 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002463 if (!launchTaskBehind) {
2464 // Don't set focus on an activity that's going to the back.
Craig Mautner299f9602015-01-26 09:47:33 -08002465 mService.setFocusedActivityLocked(r, "startedActivity");
Craig Mautnerbb742462014-07-07 15:28:55 -07002466 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002467 return ActivityManager.START_SUCCESS;
2468 }
2469
Craig Mautneree36c772014-07-16 14:56:05 -07002470 final void doPendingActivityLaunchesLocked(boolean doResume) {
2471 while (!mPendingActivityLaunches.isEmpty()) {
2472 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002473 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002474 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002475 }
2476 }
2477
Craig Mautner7f13ed32014-07-28 14:00:35 -07002478 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002479 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2480 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002481 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002482 mPendingActivityLaunches.remove(palNdx);
2483 }
2484 }
2485 }
2486
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002487 void setLaunchSource(int uid) {
2488 mLaunchingActivity.setWorkSource(new WorkSource(uid));
2489 }
2490
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002491 void acquireLaunchWakelock() {
2492 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2493 throw new IllegalStateException("Calling must be system uid");
2494 }
2495 mLaunchingActivity.acquire();
2496 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2497 // To be safe, don't allow the wake lock to be held for too long.
2498 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2499 }
2500 }
2501
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002502 /**
2503 * Called when the frontmost task is idle.
2504 * @return the state of mService.mBooting before this was called.
2505 */
2506 private boolean checkFinishBootingLocked() {
2507 final boolean booting = mService.mBooting;
2508 boolean enableScreen = false;
2509 mService.mBooting = false;
2510 if (!mService.mBooted) {
2511 mService.mBooted = true;
2512 enableScreen = true;
2513 }
2514 if (booting || enableScreen) {
2515 mService.postFinishBooting(booting, enableScreen);
2516 }
2517 return booting;
2518 }
2519
Craig Mautnerf3333272013-04-22 10:55:53 -07002520 // Checked.
2521 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2522 Configuration config) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08002523 if (DEBUG_ALL) Slog.v(TAG, "Activity idle: " + token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002524
Craig Mautnerf3333272013-04-22 10:55:53 -07002525 ArrayList<ActivityRecord> stops = null;
2526 ArrayList<ActivityRecord> finishes = null;
Amith Yamasani37a40c22015-06-17 13:25:42 -07002527 ArrayList<UserState> startingUsers = null;
Craig Mautnerf3333272013-04-22 10:55:53 -07002528 int NS = 0;
2529 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002530 boolean booting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002531 boolean activityRemoved = false;
2532
Wale Ogunwale7d701172015-03-11 15:36:30 -07002533 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerf3333272013-04-22 10:55:53 -07002534 if (r != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002535 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternalLocked: Callers="
2536 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002537 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2538 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002539 if (fromTimeout) {
2540 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002541 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002542
2543 // This is a hack to semi-deal with a race condition
2544 // in the client where it can be constructed with a
2545 // newer configuration from when we asked it to launch.
2546 // We'll update with whatever configuration it now says
2547 // it used to launch.
2548 if (config != null) {
2549 r.configuration = config;
2550 }
2551
2552 // We are now idle. If someone is waiting for a thumbnail from
2553 // us, we can now deliver.
2554 r.idle = true;
2555
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002556 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
Dianne Hackborn9449a612014-10-01 15:53:28 -07002557 if (isFrontStack(r.task.stack) || fromTimeout) {
Craig Mautnerf3ea23a2015-01-13 09:37:08 -08002558 booting = checkFinishBootingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002559 }
2560 }
2561
2562 if (allResumedActivitiesIdle()) {
2563 if (r != null) {
2564 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002565 }
2566
2567 if (mLaunchingActivity.isHeld()) {
2568 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2569 if (VALIDATE_WAKE_LOCK_CALLER &&
2570 Binder.getCallingUid() != Process.myUid()) {
2571 throw new IllegalStateException("Calling must be system uid");
2572 }
2573 mLaunchingActivity.release();
2574 }
2575 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002576 }
2577
2578 // Atomically retrieve all of the other things to do.
2579 stops = processStoppingActivitiesLocked(true);
2580 NS = stops != null ? stops.size() : 0;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002581 if ((NF = mFinishingActivities.size()) > 0) {
2582 finishes = new ArrayList<>(mFinishingActivities);
Craig Mautnerf3333272013-04-22 10:55:53 -07002583 mFinishingActivities.clear();
2584 }
2585
Craig Mautnerf3333272013-04-22 10:55:53 -07002586 if (mStartingUsers.size() > 0) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07002587 startingUsers = new ArrayList<>(mStartingUsers);
Craig Mautnerf3333272013-04-22 10:55:53 -07002588 mStartingUsers.clear();
2589 }
2590
Craig Mautnerf3333272013-04-22 10:55:53 -07002591 // Stop any activities that are scheduled to do so but have been
2592 // waiting for the next one to start.
2593 for (int i = 0; i < NS; i++) {
2594 r = stops.get(i);
2595 final ActivityStack stack = r.task.stack;
Wale Ogunwale7d701172015-03-11 15:36:30 -07002596 if (stack != null) {
2597 if (r.finishing) {
2598 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2599 } else {
2600 stack.stopActivityLocked(r);
2601 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002602 }
2603 }
2604
2605 // Finish any activities that are scheduled to do so but have been
2606 // waiting for the next one to start.
2607 for (int i = 0; i < NF; i++) {
2608 r = finishes.get(i);
Wale Ogunwale7d701172015-03-11 15:36:30 -07002609 final ActivityStack stack = r.task.stack;
2610 if (stack != null) {
2611 activityRemoved |= stack.destroyActivityLocked(r, true, "finish-idle");
2612 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002613 }
2614
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002615 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002616 // Complete user switch
2617 if (startingUsers != null) {
2618 for (int i = 0; i < startingUsers.size(); i++) {
2619 mService.finishUserSwitch(startingUsers.get(i));
2620 }
2621 }
2622 // Complete starting up of background users
2623 if (mStartingBackgroundUsers.size() > 0) {
Amith Yamasani37a40c22015-06-17 13:25:42 -07002624 startingUsers = new ArrayList<UserState>(mStartingBackgroundUsers);
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002625 mStartingBackgroundUsers.clear();
2626 for (int i = 0; i < startingUsers.size(); i++) {
2627 mService.finishUserBoot(startingUsers.get(i));
2628 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002629 }
2630 }
2631
2632 mService.trimApplications();
2633 //dump();
2634 //mWindowManager.dump();
2635
Craig Mautnerf3333272013-04-22 10:55:53 -07002636 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002637 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002638 }
2639
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002640 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002641 }
2642
Craig Mautner8e569572013-10-11 17:36:59 -07002643 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002644 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002645 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2646 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002647 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2648 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2649 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002650 }
Craig Mautner19091252013-10-05 00:03:53 -07002651 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002652 }
2653
2654 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002655 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2656 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002657 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2658 stacks.get(stackNdx).closeSystemDialogsLocked();
2659 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002660 }
2661 }
2662
Craig Mautner93529a42013-10-04 15:03:13 -07002663 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002664 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002665 }
2666
Craig Mautner8d341ef2013-03-26 09:03:27 -07002667 /**
2668 * @return true if some activity was finished (or would have finished if doit were true).
2669 */
Wale Ogunwale540e1232015-05-01 15:35:39 -07002670 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
2671 boolean doit, boolean evenPersistent, int userId) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002672 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002673 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2674 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002675 final int numStacks = stacks.size();
2676 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2677 final ActivityStack stack = stacks.get(stackNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07002678 if (stack.finishDisabledPackageActivitiesLocked(
2679 packageName, filterByClasses, doit, evenPersistent, userId)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002680 didSomething = true;
2681 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002682 }
2683 }
2684 return didSomething;
2685 }
2686
Dianne Hackborna413dc02013-07-12 12:02:55 -07002687 void updatePreviousProcessLocked(ActivityRecord r) {
2688 // Now that this process has stopped, we may want to consider
2689 // it to be the previous app to try to keep around in case
2690 // the user wants to return to it.
2691
2692 // First, found out what is currently the foreground app, so that
2693 // we don't blow away the previous app if this activity is being
2694 // hosted by the process that is actually still the foreground.
2695 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002696 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2697 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002698 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2699 final ActivityStack stack = stacks.get(stackNdx);
2700 if (isFrontStack(stack)) {
2701 if (stack.mResumedActivity != null) {
2702 fgApp = stack.mResumedActivity.app;
2703 } else if (stack.mPausingActivity != null) {
2704 fgApp = stack.mPausingActivity.app;
2705 }
2706 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002707 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002708 }
2709 }
2710
2711 // Now set this one as the previous process, only if that really
2712 // makes sense to.
2713 if (r.app != null && fgApp != null && r.app != fgApp
2714 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002715 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002716 mService.mPreviousProcess = r.app;
2717 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2718 }
2719 }
2720
Craig Mautner05d29032013-05-03 13:40:13 -07002721 boolean resumeTopActivitiesLocked() {
2722 return resumeTopActivitiesLocked(null, null, null);
2723 }
2724
2725 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2726 Bundle targetOptions) {
2727 if (targetStack == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002728 targetStack = mFocusedStack;
Craig Mautner05d29032013-05-03 13:40:13 -07002729 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002730 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002731 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002732 if (isFrontStack(targetStack)) {
2733 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2734 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002735
Craig Mautnere0a38842013-12-16 16:14:02 -08002736 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2737 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002738 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2739 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002740 if (stack == targetStack) {
2741 // Already started above.
2742 continue;
2743 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002744 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002745 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002746 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002747 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002748 }
Craig Mautner05d29032013-05-03 13:40:13 -07002749 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002750 }
2751
Todd Kennedy539db512014-12-15 09:57:55 -08002752 void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002753 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2754 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002755 final int numStacks = stacks.size();
2756 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2757 final ActivityStack stack = stacks.get(stackNdx);
Todd Kennedy539db512014-12-15 09:57:55 -08002758 stack.finishTopRunningActivityLocked(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002759 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002760 }
2761 }
2762
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002763 void finishVoiceTask(IVoiceInteractionSession session) {
2764 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2765 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2766 final int numStacks = stacks.size();
2767 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2768 final ActivityStack stack = stacks.get(stackNdx);
2769 stack.finishVoiceTask(session);
2770 }
2771 }
2772 }
2773
Craig Mautner299f9602015-01-26 09:47:33 -08002774 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options, String reason) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002775 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2776 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002777 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002778 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2779 // Caller wants the home activity moved with it. To accomplish this,
2780 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002781 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002782 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07002783 if (task.stack == null) {
2784 Slog.e(TAG, "findTaskToMoveToFrontLocked: can't move task="
2785 + task + " to front. Stack is null");
2786 return;
2787 }
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07002788 task.stack.moveTaskToFrontLocked(task, false /* noAnimation */, options,
2789 task.getTopActivity() == null ? null : task.getTopActivity().appTimeTracker,
2790 reason);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002791 if (DEBUG_STACK) Slog.d(TAG_STACK,
2792 "findTaskToMoveToFront: moved to front of stack=" + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002793 }
2794
Craig Mautner967212c2013-04-13 21:10:58 -07002795 ActivityStack getStack(int stackId) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002796 return getStack(stackId, false /*createStaticStackIfNeeded*/, false /*createOnTop*/);
2797 }
2798
2799 ActivityStack getStack(int stackId, boolean createStaticStackIfNeeded, boolean createOnTop) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002800 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2801 if (activityContainer != null) {
2802 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002803 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002804 if (!createStaticStackIfNeeded
2805 || (stackId < FIRST_STATIC_STACK_ID || stackId > LAST_STATIC_STACK_ID)) {
2806 return null;
2807 }
2808 return createStackOnDisplay(stackId, Display.DEFAULT_DISPLAY, createOnTop);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002809 }
2810
Craig Mautner967212c2013-04-13 21:10:58 -07002811 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002812 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002813 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2814 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002815 }
2816 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002817 }
2818
Craig Mautner4a1cb222013-12-04 16:14:06 -08002819 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002820 ActivityRecord homeActivity = getHomeActivity();
2821 if (homeActivity != null) {
2822 return homeActivity.appToken;
2823 }
2824 return null;
2825 }
2826
2827 ActivityRecord getHomeActivity() {
Craig Mautnerdb49fec2015-05-21 15:33:30 -07002828 return getHomeActivityForUser(mCurrentUser);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002829 }
2830
2831 ActivityRecord getHomeActivityForUser(int userId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002832 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2833 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2834 final TaskRecord task = tasks.get(taskNdx);
2835 if (task.isHomeTask()) {
2836 final ArrayList<ActivityRecord> activities = task.mActivities;
2837 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2838 final ActivityRecord r = activities.get(activityNdx);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07002839 if (r.isHomeActivity()
2840 && ((userId == UserHandle.USER_ALL) || (r.userId == userId))) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002841 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002842 }
2843 }
2844 }
2845 }
2846 return null;
2847 }
2848
Todd Kennedyca4d8422015-01-15 15:19:22 -08002849 ActivityContainer createVirtualActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002850 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002851 ActivityContainer activityContainer =
2852 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002853 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002854 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2855 "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002856 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002857 return activityContainer;
2858 }
2859
Craig Mautner34b73df2014-01-12 21:11:08 -08002860 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002861 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2862 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2863 ActivityContainer container = childStacks.remove(containerNdx);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002864 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "removeChildActivityContainers: removing "
2865 + container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002866 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002867 }
2868 }
2869
Craig Mautner95da1082014-02-24 17:54:35 -08002870 void deleteActivityContainer(IActivityContainer container) {
2871 ActivityContainer activityContainer = (ActivityContainer)container;
2872 if (activityContainer != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002873 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
2874 "deleteActivityContainer: callers=" + Debug.getCallers(4));
Craig Mautner95da1082014-02-24 17:54:35 -08002875 final int stackId = activityContainer.mStackId;
2876 mActivityContainers.remove(stackId);
2877 mWindowManager.removeStack(stackId);
2878 }
2879 }
2880
Wale Ogunwale60454db2015-01-23 16:05:07 -08002881 void resizeStackLocked(int stackId, Rect bounds) {
2882 final ActivityStack stack = getStack(stackId);
2883 if (stack == null) {
2884 Slog.w(TAG, "resizeStack: stackId " + stackId + " not found.");
2885 return;
2886 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002887
2888 final ActivityRecord r = stack.topRunningActivityLocked(null);
Wale Ogunwale0e162182015-02-05 08:48:34 -08002889 if (r != null && !r.task.mResizeable) {
2890 Slog.w(TAG, "resizeStack: top task " + r.task + " not resizeable.");
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08002891 return;
2892 }
2893
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002894 final IntArray changedTaskIds = new IntArray(stack.numTasks());
2895 final List<Configuration> newTaskConfigs = new ArrayList<>(stack.numTasks());
2896 stack.mFullscreen =
2897 mWindowManager.resizeStack(stackId, bounds, changedTaskIds, newTaskConfigs);
2898 for (int i = changedTaskIds.size() - 1; i >= 0; i--) {
2899 final TaskRecord task = anyTaskForIdLocked(changedTaskIds.get(i), false);
2900 if (task == null) {
2901 Slog.wtf(TAG, "Task in WindowManager, but not in ActivityManager???");
2902 continue;
2903 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002904 task.updateOverrideConfiguration(newTaskConfigs.get(i), bounds);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002905 }
2906
2907 if (r != null) {
2908 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2909 // And we need to make sure at this point that all other activities
2910 // are made visible with the correct configuration.
2911 ensureActivitiesVisibleLocked(r, 0);
2912 if (!updated) {
2913 resumeTopActivitiesLocked(stack, null, null);
2914 }
2915 }
2916 }
2917
2918 void resizeTaskLocked(TaskRecord task, Rect bounds) {
2919 if (!task.mResizeable) {
2920 Slog.w(TAG, "resizeTask: task " + task + " not resizeable.");
2921 return;
2922 }
2923
2924 if (task.mBounds != null && task.mBounds.equals(bounds)) {
2925 // Nothing to do here...
2926 return;
2927 }
2928
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002929 if (!mWindowManager.isValidTaskId(task.taskId)) {
2930 // Task doesn't exist in window manager yet (e.g. was restored from recents).
Wale Ogunwale706ed792015-08-02 10:29:44 -07002931 // All we can do for now is update the bounds so it can be used when the task is
2932 // added to window manager.
2933 task.mBounds = task.mLastNonFullscreenBounds = new Rect(bounds);
2934 if (task.stack != null && task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID) {
2935 // re-restore the task so it can have the proper stack association.
2936 restoreRecentTaskLocked(task);
2937 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002938 return;
2939 }
2940
2941 final Configuration overrideConfig = mWindowManager.resizeTask(task.taskId, bounds);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002942 if (task.updateOverrideConfiguration(overrideConfig, bounds)) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002943 ActivityRecord r = task.topRunningActivityLocked(null);
Wale Ogunwale60454db2015-01-23 16:05:07 -08002944 if (r != null) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002945 final ActivityStack stack = task.stack;
Wale Ogunwale60454db2015-01-23 16:05:07 -08002946 final boolean updated = stack.ensureActivityConfigurationLocked(r, 0);
2947 // And we need to make sure at this point that all other activities
2948 // are made visible with the correct configuration.
2949 ensureActivitiesVisibleLocked(r, 0);
2950 if (!updated) {
2951 resumeTopActivitiesLocked(stack, null, null);
2952 }
2953 }
2954 }
2955 }
2956
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002957 ActivityStack createStackOnDisplay(int stackId, int displayId, boolean onTop) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002958 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2959 if (activityDisplay == null) {
Todd Kennedy4900bf92015-01-16 16:05:14 -08002960 return null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002961 }
2962
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002963 ActivityContainer activityContainer = new ActivityContainer(stackId);
2964 mActivityContainers.put(stackId, activityContainer);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002965 activityContainer.attachToDisplayLocked(activityDisplay, onTop);
Todd Kennedy4900bf92015-01-16 16:05:14 -08002966 return activityContainer.mStack;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002967 }
2968
2969 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002970 while (true) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002971 if (mNextFreeStackId >= FIRST_DYNAMIC_STACK_ID
2972 && getStack(mNextFreeStackId) == null) {
Craig Mautner858d8a62013-04-23 17:08:34 -07002973 break;
2974 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002975 mNextFreeStackId++;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002976 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002977 return mNextFreeStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002978 }
2979
Wale Ogunwale7de05352014-12-12 15:21:33 -08002980 private boolean restoreRecentTaskLocked(TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07002981 final int stackId =
2982 mLeanbackOnlyDevice ? mHomeStack.mStackId : task.getLaunchStackId(mFocusedStack);
2983 if (task.stack != null) {
2984 // Task has already been restored once. See if we need to do anything more
2985 if (task.stack.mStackId == stackId) {
2986 // Nothing else to do since it is already restored in the right stack.
2987 return true;
2988 }
2989 // Remove current stack association, so we can re-associate the task with the
2990 // right stack below.
2991 task.stack.removeTask(task, "restoreRecentTaskLocked", false /*notMoving*/);
2992 }
2993
2994 ActivityStack stack =
2995 getStack(stackId, true /*createStaticStackIfNeeded*/, false /*createOnTop*/);
Wale Ogunwale7de05352014-12-12 15:21:33 -08002996
2997 if (stack == null) {
2998 // What does this mean??? Not sure how we would get here...
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002999 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3000 "Unable to find/create stack to restore recent task=" + task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003001 return false;
3002 }
3003
3004 stack.addTask(task, false, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003005 if (DEBUG_RECENTS) Slog.v(TAG_RECENTS,
3006 "Added restored task=" + task + " to stack=" + stack);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003007 final ArrayList<ActivityRecord> activities = task.mActivities;
3008 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07003009 stack.addAppToken(activities.get(activityNdx), task);
Wale Ogunwale7de05352014-12-12 15:21:33 -08003010 }
3011 return true;
Craig Mautneref73ee12014-04-23 11:45:37 -07003012 }
3013
Craig Mautner299f9602015-01-26 09:47:33 -08003014 void moveTaskToStackLocked(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003015 final TaskRecord task = anyTaskForIdLocked(taskId);
3016 if (task == null) {
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003017 Slog.w(TAG, "moveTaskToStack: no task for id=" + taskId);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07003018 return;
3019 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003020 ActivityStack stack =
3021 getStack(stackId, true /*createStaticStackIfNeeded*/, toTop /*createOnTop*/);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003022 mWindowManager.moveTaskToStack(taskId, stackId, toTop);
3023 if (task.stack != null) {
Wale Ogunwale000957c2015-04-03 08:19:12 -07003024 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08003025 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07003026 stack.addTask(task, toTop, true);
Stefan Kuhne54714cd2015-05-12 13:42:18 -07003027 // The task might have already been running and its visibility needs to be synchronized with
3028 // the visibility of the stack / windows.
3029 stack.ensureActivitiesVisibleLocked(null, 0);
Craig Mautner05d29032013-05-03 13:40:13 -07003030 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003031 }
3032
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07003033 void positionTaskInStackLocked(int taskId, int stackId, int position) {
3034 final TaskRecord task = anyTaskForIdLocked(taskId);
3035 if (task == null) {
3036 Slog.w(TAG, "positionTaskInStackLocked: no task for id=" + taskId);
3037 return;
3038 }
3039 ActivityStack stack =
3040 getStack(stackId, true /*createStaticStackIfNeeded*/, false /*createOnTop*/);
3041 mWindowManager.positionTaskInStack(taskId, stackId, position);
3042 final boolean stackChanged = task.stack != null && task.stack != stack;
3043 if (stackChanged) {
3044 task.stack.removeTask(task, "moveTaskToStack", false /* notMoving */);
3045 }
3046 stack.positionTask(task, position, stackChanged);
3047 // The task might have already been running and its visibility needs to be synchronized with
3048 // the visibility of the stack / windows.
3049 stack.ensureActivitiesVisibleLocked(null, 0);
3050 resumeTopActivitiesLocked();
3051 }
3052
Craig Mautnerac6f8432013-07-17 13:24:59 -07003053 ActivityRecord findTaskLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003054 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08003055 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3056 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003057 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3058 final ActivityStack stack = stacks.get(stackNdx);
3059 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003060 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping stack: (home activity) " + stack);
Craig Mautner1b4bf852014-05-26 15:06:32 -07003061 continue;
3062 }
3063 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003064 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
3065 "Skipping stack: (new task not allowed) " + stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003066 continue;
3067 }
3068 final ActivityRecord ar = stack.findTaskLocked(r);
3069 if (ar != null) {
3070 return ar;
3071 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003072 }
3073 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003074 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07003075 return null;
3076 }
3077
3078 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003079 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3080 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003081 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3082 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
3083 if (ar != null) {
3084 return ar;
3085 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07003086 }
3087 }
3088 return null;
3089 }
3090
Craig Mautner8d341ef2013-03-26 09:03:27 -07003091 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003092 scheduleSleepTimeout();
3093 if (!mGoingToSleep.isHeld()) {
3094 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003095 if (mLaunchingActivity.isHeld()) {
3096 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
3097 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003098 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003099 mLaunchingActivity.release();
3100 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003101 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003102 }
Amith Yamasanice15e152013-09-19 12:30:32 -07003103 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07003104 }
3105
3106 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003107 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07003108
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003109 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003110 final long endTime = System.currentTimeMillis() + timeout;
3111 while (true) {
3112 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003113 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3114 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003115 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3116 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
3117 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003118 }
3119 if (cantShutdown) {
3120 long timeRemaining = endTime - System.currentTimeMillis();
3121 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003122 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003123 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003124 } catch (InterruptedException e) {
3125 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003126 } else {
3127 Slog.w(TAG, "Activity manager shutdown timed out");
3128 timedout = true;
3129 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003130 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003131 } else {
3132 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003133 }
3134 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003135
3136 // Force checkReadyForSleep to complete.
3137 mSleepTimeout = true;
3138 checkReadyForSleepLocked();
3139
Craig Mautner8d341ef2013-03-26 09:03:27 -07003140 return timedout;
3141 }
3142
3143 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003144 removeSleepTimeouts();
3145 if (mGoingToSleep.isHeld()) {
3146 mGoingToSleep.release();
3147 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003148 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3149 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003150 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3151 final ActivityStack stack = stacks.get(stackNdx);
3152 stack.awakeFromSleepingLocked();
3153 if (isFrontStack(stack)) {
3154 resumeTopActivitiesLocked();
3155 }
Craig Mautner5314a402013-09-26 12:40:16 -07003156 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003157 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003158 mGoingToSleepActivities.clear();
3159 }
3160
3161 void activitySleptLocked(ActivityRecord r) {
3162 mGoingToSleepActivities.remove(r);
3163 checkReadyForSleepLocked();
3164 }
3165
3166 void checkReadyForSleepLocked() {
3167 if (!mService.isSleepingOrShuttingDown()) {
3168 // Do not care.
3169 return;
3170 }
3171
3172 if (!mSleepTimeout) {
3173 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003174 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3175 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003176 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3177 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
3178 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003179 }
3180
3181 if (mStoppingActivities.size() > 0) {
3182 // Still need to tell some activities to stop; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003183 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to stop "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003184 + mStoppingActivities.size() + " activities");
3185 scheduleIdleLocked();
3186 dontSleep = true;
3187 }
3188
3189 if (mGoingToSleepActivities.size() > 0) {
3190 // Still need to tell some activities to sleep; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003191 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still need to sleep "
Craig Mautner0eea92c2013-05-16 13:35:39 -07003192 + mGoingToSleepActivities.size() + " activities");
3193 dontSleep = true;
3194 }
3195
3196 if (dontSleep) {
3197 return;
3198 }
3199 }
3200
Craig Mautnere0a38842013-12-16 16:14:02 -08003201 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3202 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003203 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3204 stacks.get(stackNdx).goToSleep();
3205 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07003206 }
3207
3208 removeSleepTimeouts();
3209
3210 if (mGoingToSleep.isHeld()) {
3211 mGoingToSleep.release();
3212 }
3213 if (mService.mShuttingDown) {
3214 mService.notifyAll();
3215 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003216 }
3217
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003218 boolean reportResumedActivityLocked(ActivityRecord r) {
3219 final ActivityStack stack = r.task.stack;
3220 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07003221 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003222 }
3223 if (allResumedActivitiesComplete()) {
3224 ensureActivitiesVisibleLocked(null, 0);
3225 mWindowManager.executeAppTransition();
3226 return true;
3227 }
3228 return false;
3229 }
3230
Craig Mautner8d341ef2013-03-26 09:03:27 -07003231 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003232 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3233 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Wale Ogunwale28b23972015-07-29 16:01:50 -07003234 int stackNdx = stacks.size() - 1;
3235 while (stackNdx >= 0) {
3236 stacks.get(stackNdx).handleAppCrashLocked(app);
3237 stackNdx--;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003238 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003239 }
3240 }
3241
Jose Lima4b6c6692014-08-12 17:41:12 -07003242 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003243 final ActivityStack stack = r.task.stack;
3244 if (stack == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003245 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3246 "requestVisibleBehind: r=" + r + " visible=" + visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003247 return false;
3248 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003249 final boolean isVisible = stack.hasVisibleBehindActivity();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003250 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3251 "requestVisibleBehind r=" + r + " visible=" + visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003252
3253 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07003254 if (top == null || top == r || (visible == isVisible)) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003255 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND, "requestVisibleBehind: quick return");
Jose Lima4b6c6692014-08-12 17:41:12 -07003256 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003257 return true;
3258 }
3259
3260 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07003261 if (visible && top.fullscreen) {
3262 // Let the caller know that it can't be seen.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003263 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3264 "requestVisibleBehind: returning top.fullscreen=" + top.fullscreen
3265 + " top.state=" + top.state + " top.app=" + top.app + " top.app.thread="
3266 + top.app.thread);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003267 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07003268 } else if (!visible && stack.getVisibleBehindActivity() != r) {
3269 // Only the activity set as currently visible behind should actively reset its
3270 // visible behind state.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003271 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG_VISIBLE_BEHIND,
3272 "requestVisibleBehind: returning visible=" + visible
3273 + " stack.getVisibleBehindActivity()=" + stack.getVisibleBehindActivity()
3274 + " r=" + r);
Jose Lima34ff4922014-08-18 15:19:41 -07003275 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003276 }
3277
Jose Lima4b6c6692014-08-12 17:41:12 -07003278 stack.setVisibleBehindActivity(visible ? r : null);
3279 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07003280 // Make the activity immediately above r opaque.
3281 final ActivityRecord next = stack.findNextTranslucentActivity(r);
3282 if (next != null) {
3283 mService.convertFromTranslucent(next.appToken);
3284 }
3285 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07003286 if (top.app != null && top.app.thread != null) {
3287 // Notify the top app of the change.
3288 try {
3289 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
3290 } catch (RemoteException e) {
3291 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07003292 }
3293 return true;
3294 }
3295
Craig Mautnerbb742462014-07-07 15:28:55 -07003296 // Called when WindowManager has finished animating the launchingBehind activity to the back.
3297 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
3298 r.mLaunchTaskBehind = false;
3299 final TaskRecord task = r.task;
3300 task.setLastThumbnail(task.stack.screenshotActivities(r));
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08003301 mRecentTasks.addLocked(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08003302 mService.notifyTaskStackChangedLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07003303 mWindowManager.setAppVisibility(r.appToken, false);
3304 }
3305
3306 void scheduleLaunchTaskBehindComplete(IBinder token) {
3307 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
3308 }
3309
Craig Mautnerde4ef022013-04-07 19:01:33 -07003310 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07003311 // First the front stacks. In case any are not fullscreen and are in front of home.
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 final int topStackNdx = stacks.size() - 1;
3315 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3316 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07003317 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07003318 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003319 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003320 }
3321
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003322 void clearOtherAppTimeTrackers(AppTimeTracker except) {
3323 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3324 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3325 final int topStackNdx = stacks.size() - 1;
3326 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
3327 final ActivityStack stack = stacks.get(stackNdx);
3328 stack.clearOtherAppTimeTrackers(except);
3329 }
3330 }
3331 }
3332
Craig Mautner8d341ef2013-03-26 09:03:27 -07003333 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003334 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3335 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003336 final int numStacks = stacks.size();
3337 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3338 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003339 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003340 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003341 }
3342 }
3343
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003344 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
3345 // Examine all activities currently running in the process.
3346 TaskRecord firstTask = null;
3347 // Tasks is non-null only if two or more tasks are found.
3348 ArraySet<TaskRecord> tasks = null;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003349 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
3350 for (int i = 0; i < app.activities.size(); i++) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003351 ActivityRecord r = app.activities.get(i);
3352 // First, if we find an activity that is in the process of being destroyed,
3353 // then we just aren't going to do anything for now; we want things to settle
3354 // down before we try to prune more activities.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003355 if (r.finishing || r.state == DESTROYING || r.state == DESTROYED) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003356 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003357 return;
3358 }
3359 // Don't consider any activies that are currently not in a state where they
3360 // can be destroyed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003361 if (r.visible || !r.stopped || !r.haveState || r.state == RESUMED || r.state == PAUSING
3362 || r.state == PAUSED || r.state == STOPPING) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003363 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003364 continue;
3365 }
3366 if (r.task != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003367 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Collecting release task " + r.task
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003368 + " from " + r);
3369 if (firstTask == null) {
3370 firstTask = r.task;
3371 } else if (firstTask != r.task) {
3372 if (tasks == null) {
3373 tasks = new ArraySet<>();
3374 tasks.add(firstTask);
3375 }
3376 tasks.add(r.task);
3377 }
3378 }
3379 }
3380 if (tasks == null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003381 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Didn't find two or more tasks to release");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003382 return;
3383 }
3384 // If we have activities in multiple tasks that are in a position to be destroyed,
3385 // let's iterate through the tasks and release the oldest one.
3386 final int numDisplays = mActivityDisplays.size();
3387 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
3388 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3389 // Step through all stacks starting from behind, to hit the oldest things first.
3390 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
3391 final ActivityStack stack = stacks.get(stackNdx);
3392 // Try to release activities in this stack; if we manage to, we are done.
3393 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
3394 return;
3395 }
3396 }
3397 }
3398 }
3399
Amith Yamasani37a40c22015-06-17 13:25:42 -07003400 boolean switchUserLocked(int userId, UserState uss) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003401 mUserStackInFront.put(mCurrentUser, mFocusedStack.getStackId());
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003402 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07003403 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07003404
Craig Mautner858d8a62013-04-23 17:08:34 -07003405 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08003406 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3407 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003408 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003409 final ActivityStack stack = stacks.get(stackNdx);
3410 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003411 TaskRecord task = stack.topTask();
3412 if (task != null) {
3413 mWindowManager.moveTaskToTop(task.taskId);
3414 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003415 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07003416 }
Craig Mautner858d8a62013-04-23 17:08:34 -07003417
Craig Mautner4f1df4f2013-10-15 15:44:14 -07003418 ActivityStack stack = getStack(restoreStackId);
3419 if (stack == null) {
3420 stack = mHomeStack;
3421 }
3422 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08003423 if (stack.isOnHomeDisplay()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003424 moveHomeStack(homeInFront, "switchUserOnHomeDisplay");
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00003425 TaskRecord task = stack.topTask();
3426 if (task != null) {
3427 mWindowManager.moveTaskToTop(task.taskId);
3428 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003429 } else {
3430 // Stack was moved to another display while user was swapped out.
Craig Mautner299f9602015-01-26 09:47:33 -08003431 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null, "switchUserOnOtherDisplay");
Craig Mautnere0a38842013-12-16 16:14:02 -08003432 }
Craig Mautner93529a42013-10-04 15:03:13 -07003433 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07003434 }
3435
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003436 /**
3437 * Add background users to send boot completed events to.
3438 * @param userId The user being started in the background
3439 * @param uss The state object for the user.
3440 */
Amith Yamasani37a40c22015-06-17 13:25:42 -07003441 public void startBackgroundUserLocked(int userId, UserState uss) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07003442 mStartingBackgroundUsers.add(uss);
3443 }
3444
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07003445 /** Checks whether the userid is a profile of the current user. */
3446 boolean isCurrentProfileLocked(int userId) {
3447 if (userId == mCurrentUser) return true;
3448 for (int i = 0; i < mService.mCurrentProfileIds.length; i++) {
3449 if (mService.mCurrentProfileIds[i] == userId) return true;
3450 }
3451 return false;
3452 }
3453
Craig Mautnerde4ef022013-04-07 19:01:33 -07003454 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003455 ArrayList<ActivityRecord> stops = null;
3456
3457 final boolean nowVisible = allResumedActivitiesVisible();
Craig Mautner8c14c152015-01-15 17:32:07 -08003458 for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3459 ActivityRecord s = mStoppingActivities.get(activityNdx);
3460 final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003461 if (DEBUG_ALL) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
Craig Mautner8c14c152015-01-15 17:32:07 -08003462 + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
3463 if (waitingVisible && nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003464 mWaitingVisibleActivities.remove(s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003465 if (s.finishing) {
3466 // If this activity is finishing, it is sitting on top of
3467 // everyone else but we now know it is no longer needed...
3468 // so get rid of it. Otherwise, we need to go through the
3469 // normal flow and hide it once we determine that it is
3470 // hidden by the activities in front of it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003471 if (DEBUG_ALL) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003472 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003473 }
3474 }
Craig Mautner8c14c152015-01-15 17:32:07 -08003475 if ((!waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003476 if (DEBUG_ALL) Slog.v(TAG, "Ready to stop: " + s);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003477 if (stops == null) {
Craig Mautner8c14c152015-01-15 17:32:07 -08003478 stops = new ArrayList<>();
Craig Mautnerde4ef022013-04-07 19:01:33 -07003479 }
3480 stops.add(s);
Craig Mautner8c14c152015-01-15 17:32:07 -08003481 mStoppingActivities.remove(activityNdx);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003482 }
3483 }
3484
3485 return stops;
3486 }
3487
Craig Mautnercf910b02013-04-23 11:23:27 -07003488 void validateTopActivitiesLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003489 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3490 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3491 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3492 final ActivityStack stack = stacks.get(stackNdx);
3493 final ActivityRecord r = stack.topRunningActivityLocked(null);
3494 final ActivityState state = r == null ? DESTROYED : r.state;
3495 if (isFrontStack(stack)) {
3496 if (r == null) Slog.e(TAG,
3497 "validateTop...: null top activity, stack=" + stack);
3498 else {
3499 final ActivityRecord pausing = stack.mPausingActivity;
3500 if (pausing != null && pausing == r) Slog.e(TAG,
3501 "validateTop...: top stack has pausing activity r=" + r
3502 + " state=" + state);
3503 if (state != INITIALIZING && state != RESUMED) Slog.e(TAG,
3504 "validateTop...: activity in front not resumed r=" + r
3505 + " state=" + state);
3506 }
Craig Mautnercf910b02013-04-23 11:23:27 -07003507 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003508 final ActivityRecord resumed = stack.mResumedActivity;
3509 if (resumed != null && resumed == r) Slog.e(TAG,
3510 "validateTop...: back stack has resumed activity r=" + r
3511 + " state=" + state);
3512 if (r != null && (state == INITIALIZING || state == RESUMED)) Slog.e(TAG,
3513 "validateTop...: activity in back resumed r=" + r + " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003514 }
3515 }
3516 }
Craig Mautner76ea2242013-05-15 11:40:05 -07003517 }
3518
Craig Mautnere0570202015-05-13 13:06:11 -07003519 private String lockTaskModeToString() {
3520 switch (mLockTaskModeState) {
3521 case LOCK_TASK_MODE_LOCKED:
3522 return "LOCKED";
3523 case LOCK_TASK_MODE_PINNED:
3524 return "PINNED";
3525 case LOCK_TASK_MODE_NONE:
3526 return "NONE";
3527 default: return "unknown=" + mLockTaskModeState;
3528 }
3529 }
3530
Craig Mautner27084302013-03-25 08:05:25 -07003531 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003532 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003533 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003534 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3535 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3536 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003537 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautnere0570202015-05-13 13:06:11 -07003538 pw.print(prefix); pw.print("mLockTaskModeState=" + lockTaskModeToString());
3539 final SparseArray<String[]> packages = mService.mLockTaskPackages;
3540 if (packages.size() > 0) {
3541 pw.println(" mLockTaskPackages (userId:packages)=");
3542 for (int i = 0; i < packages.size(); ++i) {
3543 pw.print(prefix); pw.print(prefix); pw.print(packages.keyAt(i));
3544 pw.print(":"); pw.println(Arrays.toString(packages.valueAt(i)));
3545 }
3546 }
3547 pw.println(" mLockTaskModeTasks" + mLockTaskModeTasks);
Craig Mautner27084302013-03-25 08:05:25 -07003548 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003549
Craig Mautner20e72272013-04-01 13:45:53 -07003550 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003551 return mFocusedStack.getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003552 }
3553
Dianne Hackborn390517b2013-05-30 15:03:32 -07003554 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3555 boolean needSep, String prefix) {
3556 if (activity != null) {
3557 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3558 if (needSep) {
3559 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003560 }
3561 pw.print(prefix);
3562 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003563 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003564 }
3565 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003566 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003567 }
3568
Craig Mautner8d341ef2013-03-26 09:03:27 -07003569 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3570 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003571 boolean printed = false;
3572 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003573 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3574 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003575 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
Craig Mautner737fae22014-10-15 12:52:10 -07003576 pw.println(" (activities from top to bottom):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003577 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner737fae22014-10-15 12:52:10 -07003578 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003579 final ActivityStack stack = stacks.get(stackNdx);
3580 StringBuilder stackHeader = new StringBuilder(128);
3581 stackHeader.append(" Stack #");
3582 stackHeader.append(stack.mStackId);
3583 stackHeader.append(":");
3584 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3585 needSep, stackHeader.toString());
3586 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3587 !dumpAll, false, dumpPackage, true,
3588 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003589
Craig Mautner4a1cb222013-12-04 16:14:06 -08003590 needSep = printed;
3591 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3592 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003593 if (pr) {
3594 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003595 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003596 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003597 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3598 " mResumedActivity: ");
3599 if (pr) {
3600 printed = true;
3601 needSep = false;
3602 }
3603 if (dumpAll) {
3604 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3605 " mLastPausedActivity: ");
3606 if (pr) {
3607 printed = true;
3608 needSep = true;
3609 }
3610 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3611 needSep, " mLastNoHistoryActivity: ");
3612 }
3613 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003614 }
3615 }
3616
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003617 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3618 false, dumpPackage, true, " Activities waiting to finish:", null);
3619 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3620 false, dumpPackage, true, " Activities waiting to stop:", null);
3621 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3622 false, dumpPackage, true, " Activities waiting for another to become visible:",
3623 null);
3624 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3625 false, dumpPackage, true, " Activities waiting to sleep:", null);
3626 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3627 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003628
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003629 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003630 }
3631
Dianne Hackborn390517b2013-05-30 15:03:32 -07003632 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003633 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003634 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003635 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003636 String innerPrefix = null;
3637 String[] args = null;
3638 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003639 for (int i=list.size()-1; i>=0; i--) {
3640 final ActivityRecord r = list.get(i);
3641 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3642 continue;
3643 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003644 if (innerPrefix == null) {
3645 innerPrefix = prefix + " ";
3646 args = new String[0];
3647 }
3648 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003649 final boolean full = !brief && (complete || !r.isInHistory());
3650 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003651 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003652 needNL = false;
3653 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003654 if (header1 != null) {
3655 pw.println(header1);
3656 header1 = null;
3657 }
3658 if (header2 != null) {
3659 pw.println(header2);
3660 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003661 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003662 if (lastTask != r.task) {
3663 lastTask = r.task;
3664 pw.print(prefix);
3665 pw.print(full ? "* " : " ");
3666 pw.println(lastTask);
3667 if (full) {
3668 lastTask.dump(pw, prefix + " ");
3669 } else if (complete) {
3670 // Complete + brief == give a summary. Isn't that obvious?!?
3671 if (lastTask.intent != null) {
3672 pw.print(prefix); pw.print(" ");
3673 pw.println(lastTask.intent.toInsecureStringWithClip());
3674 }
3675 }
3676 }
3677 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3678 pw.print(" #"); pw.print(i); pw.print(": ");
3679 pw.println(r);
3680 if (full) {
3681 r.dump(pw, innerPrefix);
3682 } else if (complete) {
3683 // Complete + brief == give a summary. Isn't that obvious?!?
3684 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3685 if (r.app != null) {
3686 pw.print(innerPrefix); pw.println(r.app);
3687 }
3688 }
3689 if (client && r.app != null && r.app.thread != null) {
3690 // flush anything that is already in the PrintWriter since the thread is going
3691 // to write to the file descriptor directly
3692 pw.flush();
3693 try {
3694 TransferPipe tp = new TransferPipe();
3695 try {
3696 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3697 r.appToken, innerPrefix, args);
3698 // Short timeout, since blocking here can
3699 // deadlock with the application.
3700 tp.go(fd, 2000);
3701 } finally {
3702 tp.kill();
3703 }
3704 } catch (IOException e) {
3705 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3706 } catch (RemoteException e) {
3707 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3708 }
3709 needNL = true;
3710 }
3711 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003712 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003713 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003714
Craig Mautnerf3333272013-04-22 10:55:53 -07003715 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003716 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
3717 "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003718 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3719 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003720 }
3721
3722 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003723 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003724 }
3725
3726 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003727 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers="
3728 + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003729 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3730 }
3731
Craig Mautner05d29032013-05-03 13:40:13 -07003732 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003733 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3734 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3735 }
Craig Mautner05d29032013-05-03 13:40:13 -07003736 }
3737
Craig Mautner0eea92c2013-05-16 13:35:39 -07003738 void removeSleepTimeouts() {
3739 mSleepTimeout = false;
3740 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3741 }
3742
3743 final void scheduleSleepTimeout() {
3744 removeSleepTimeouts();
3745 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3746 }
3747
Craig Mautner4a1cb222013-12-04 16:14:06 -08003748 @Override
3749 public void onDisplayAdded(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003750 if (DEBUG_STACK) Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003751 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3752 }
3753
3754 @Override
3755 public void onDisplayRemoved(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003756 if (DEBUG_STACK) Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003757 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3758 }
3759
3760 @Override
3761 public void onDisplayChanged(int displayId) {
Dianne Hackbornfb81d092015-08-03 17:14:46 -07003762 if (DEBUG_STACK) Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003763 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3764 }
3765
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003766 private void handleDisplayAdded(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003767 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003768 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003769 newDisplay = mActivityDisplays.get(displayId) == null;
3770 if (newDisplay) {
3771 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003772 if (activityDisplay.mDisplay == null) {
3773 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3774 return;
3775 }
Craig Mautner4504de52013-12-20 09:06:56 -08003776 mActivityDisplays.put(displayId, activityDisplay);
3777 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003778 }
Craig Mautner4504de52013-12-20 09:06:56 -08003779 if (newDisplay) {
3780 mWindowManager.onDisplayAdded(displayId);
3781 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003782 }
3783
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003784 private void handleDisplayRemoved(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003785 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003786 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3787 if (activityDisplay != null) {
3788 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003789 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003790 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003791 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003792 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003793 }
3794 }
3795 mWindowManager.onDisplayRemoved(displayId);
3796 }
3797
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003798 private void handleDisplayChanged(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003799 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003800 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3801 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003802 // TODO: Update the bounds.
3803 }
3804 }
3805 mWindowManager.onDisplayChanged(displayId);
3806 }
3807
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003808 private StackInfo getStackInfoLocked(ActivityStack stack) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003809 StackInfo info = new StackInfo();
3810 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3811 info.displayId = Display.DEFAULT_DISPLAY;
3812 info.stackId = stack.mStackId;
3813
3814 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3815 final int numTasks = tasks.size();
3816 int[] taskIds = new int[numTasks];
3817 String[] taskNames = new String[numTasks];
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003818 Rect[] taskBounds = new Rect[numTasks];
Craig Mautner4a1cb222013-12-04 16:14:06 -08003819 for (int i = 0; i < numTasks; ++i) {
3820 final TaskRecord task = tasks.get(i);
3821 taskIds[i] = task.taskId;
3822 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3823 : task.realActivity != null ? task.realActivity.flattenToString()
3824 : task.getTopActivity() != null ? task.getTopActivity().packageName
3825 : "unknown";
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003826 taskBounds[i] = new Rect();
3827 mWindowManager.getTaskBounds(task.taskId, taskBounds[i]);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003828 }
3829 info.taskIds = taskIds;
3830 info.taskNames = taskNames;
3831 return info;
3832 }
3833
3834 StackInfo getStackInfoLocked(int stackId) {
3835 ActivityStack stack = getStack(stackId);
3836 if (stack != null) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003837 return getStackInfoLocked(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003838 }
3839 return null;
3840 }
3841
3842 ArrayList<StackInfo> getAllStackInfosLocked() {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003843 ArrayList<StackInfo> list = new ArrayList<>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003844 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3845 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003846 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07003847 list.add(getStackInfoLocked(stacks.get(ndx)));
Craig Mautner4a1cb222013-12-04 16:14:06 -08003848 }
3849 }
3850 return list;
3851 }
3852
Craig Mautner15df08a2015-04-01 12:17:18 -07003853 TaskRecord getLockedTaskLocked() {
3854 final int top = mLockTaskModeTasks.size() - 1;
3855 if (top >= 0) {
3856 return mLockTaskModeTasks.get(top);
3857 }
3858 return null;
3859 }
3860
3861 boolean isLockedTask(TaskRecord task) {
3862 return mLockTaskModeTasks.contains(task);
3863 }
3864
3865 boolean isLastLockedTask(TaskRecord task) {
3866 return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
3867 }
3868
3869 void removeLockedTaskLocked(final TaskRecord task) {
Craig Mautner432f64e2015-05-20 14:59:57 -07003870 if (!mLockTaskModeTasks.remove(task)) {
3871 return;
3872 }
3873 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "removeLockedTaskLocked: removed " + task);
3874 if (mLockTaskModeTasks.isEmpty()) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003875 // Last one.
Craig Mautnere0570202015-05-13 13:06:11 -07003876 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task +
3877 " last task, reverting locktask mode. Callers=" + Debug.getCallers(3));
Craig Mautner15df08a2015-04-01 12:17:18 -07003878 final Message lockTaskMsg = Message.obtain();
3879 lockTaskMsg.arg1 = task.userId;
3880 lockTaskMsg.what = LOCK_TASK_END_MSG;
3881 mHandler.sendMessage(lockTaskMsg);
3882 }
3883 }
3884
Craig Mautner6cd6cec2015-04-01 00:02:12 -07003885 void showLockTaskToast() {
3886 mLockTaskNotify.showToast(mLockTaskModeState);
Jason Monka8f569c2014-07-07 12:15:21 -04003887 }
3888
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07003889 void showLockTaskEscapeMessageLocked(TaskRecord task) {
3890 if (mLockTaskModeTasks.contains(task)) {
3891 mHandler.sendEmptyMessage(SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG);
3892 }
3893 }
3894
Craig Mautner432f64e2015-05-20 14:59:57 -07003895 void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason,
3896 boolean andResume) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003897 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003898 // Take out of lock task mode if necessary
Craig Mautner15df08a2015-04-01 12:17:18 -07003899 final TaskRecord lockedTask = getLockedTaskLocked();
3900 if (lockedTask != null) {
3901 removeLockedTaskLocked(lockedTask);
3902 if (!mLockTaskModeTasks.isEmpty()) {
3903 // There are locked tasks remaining, can only finish this task, not unlock it.
Craig Mautnere0570202015-05-13 13:06:11 -07003904 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3905 "setLockTaskModeLocked: Tasks remaining, can't unlock");
Craig Mautner15df08a2015-04-01 12:17:18 -07003906 lockedTask.performClearTaskLocked();
3907 resumeTopActivitiesLocked();
3908 return;
3909 }
Christopher Tate3bd90612014-06-18 16:37:52 -07003910 }
Craig Mautnere0570202015-05-13 13:06:11 -07003911 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3912 "setLockTaskModeLocked: No tasks to unlock. Callers=" + Debug.getCallers(4));
Craig Mautneraea74a52014-03-08 14:23:10 -08003913 return;
3914 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003915
3916 // Should have already been checked, but do it again.
3917 if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
Craig Mautnere0570202015-05-13 13:06:11 -07003918 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK,
3919 "setLockTaskModeLocked: Can't lock due to auth");
Craig Mautneraea74a52014-03-08 14:23:10 -08003920 return;
3921 }
Craig Mautner15df08a2015-04-01 12:17:18 -07003922 if (isLockTaskModeViolation(task)) {
Craig Mautnere0570202015-05-13 13:06:11 -07003923 Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task.");
Craig Mautner15df08a2015-04-01 12:17:18 -07003924 return;
3925 }
3926
3927 if (mLockTaskModeTasks.isEmpty()) {
3928 // First locktask.
3929 final Message lockTaskMsg = Message.obtain();
3930 lockTaskMsg.obj = task.intent.getComponent().getPackageName();
3931 lockTaskMsg.arg1 = task.userId;
3932 lockTaskMsg.what = LOCK_TASK_START_MSG;
3933 lockTaskMsg.arg2 = lockTaskModeState;
3934 mHandler.sendMessage(lockTaskMsg);
3935 }
3936 // Add it or move it to the top.
Craig Mautnere0570202015-05-13 13:06:11 -07003937 if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskModeLocked: Locking to " + task +
3938 " Callers=" + Debug.getCallers(4));
Craig Mautner15df08a2015-04-01 12:17:18 -07003939 mLockTaskModeTasks.remove(task);
3940 mLockTaskModeTasks.add(task);
3941
3942 if (task.mLockTaskUid == -1) {
3943 task.mLockTaskUid = task.mCallingUid;
3944 }
Craig Mautner432f64e2015-05-20 14:59:57 -07003945
3946 if (andResume) {
3947 findTaskToMoveToFrontLocked(task, 0, null, reason);
3948 resumeTopActivitiesLocked();
3949 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003950 }
3951
3952 boolean isLockTaskModeViolation(TaskRecord task) {
Jason Monk25d237b2015-06-19 10:39:39 -04003953 return isLockTaskModeViolation(task, false);
3954 }
3955
3956 boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) {
3957 if (getLockedTaskLocked() == task && !isNewClearTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003958 return false;
3959 }
3960 final int lockTaskAuth = task.mLockTaskAuth;
3961 switch (lockTaskAuth) {
3962 case LOCK_TASK_AUTH_DONT_LOCK:
3963 return !mLockTaskModeTasks.isEmpty();
Benjamin Franz469dd582015-06-09 14:24:36 +01003964 case LOCK_TASK_AUTH_LAUNCHABLE_PRIV:
Craig Mautner15df08a2015-04-01 12:17:18 -07003965 case LOCK_TASK_AUTH_LAUNCHABLE:
3966 case LOCK_TASK_AUTH_WHITELISTED:
3967 return false;
3968 case LOCK_TASK_AUTH_PINNABLE:
3969 // Pinnable tasks can't be launched on top of locktask tasks.
3970 return !mLockTaskModeTasks.isEmpty();
3971 default:
3972 Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
3973 return true;
3974 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003975 }
3976
Craig Mautner15df08a2015-04-01 12:17:18 -07003977 void onLockTaskPackagesUpdatedLocked() {
3978 boolean didSomething = false;
3979 for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
3980 final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
Benjamin Franz469dd582015-06-09 14:24:36 +01003981 final boolean wasWhitelisted =
3982 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3983 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
Craig Mautner15df08a2015-04-01 12:17:18 -07003984 lockedTask.setLockTaskAuth();
Benjamin Franz469dd582015-06-09 14:24:36 +01003985 final boolean isWhitelisted =
3986 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) ||
3987 (lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED);
3988 if (wasWhitelisted && !isWhitelisted) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003989 // Lost whitelisting authorization. End it now.
Craig Mautner432f64e2015-05-20 14:59:57 -07003990 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " +
3991 lockedTask + " mLockTaskAuth=" + lockedTask.lockTaskAuthToString());
Craig Mautner15df08a2015-04-01 12:17:18 -07003992 removeLockedTaskLocked(lockedTask);
3993 lockedTask.performClearTaskLocked();
3994 didSomething = true;
3995 }
3996 }
3997 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
3998 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
3999 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
4000 final ActivityStack stack = stacks.get(stackNdx);
4001 stack.onLockTaskPackagesUpdatedLocked();
4002 }
4003 }
Craig Mautnere0570202015-05-13 13:06:11 -07004004 final ActivityRecord r = topRunningActivityLocked();
4005 final TaskRecord task = r != null ? r.task : null;
4006 if (mLockTaskModeTasks.isEmpty() && task != null
4007 && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
4008 // This task must have just been authorized.
Craig Mautner432f64e2015-05-20 14:59:57 -07004009 if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK,
4010 "onLockTaskPackagesUpdated: starting new locktask task=" + task);
4011 setLockTaskModeLocked(task, ActivityManager.LOCK_TASK_MODE_LOCKED, "package updated",
4012 false);
4013 didSomething = true;
Craig Mautnere0570202015-05-13 13:06:11 -07004014 }
Craig Mautner15df08a2015-04-01 12:17:18 -07004015 if (didSomething) {
4016 resumeTopActivitiesLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08004017 }
4018 }
4019
Benjamin Franz43261142015-02-11 15:59:44 +00004020 int getLockTaskModeState() {
4021 return mLockTaskModeState;
Craig Mautneraea74a52014-03-08 14:23:10 -08004022 }
4023
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004024 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07004025
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004026 public ActivityStackSupervisorHandler(Looper looper) {
4027 super(looper);
4028 }
4029
Craig Mautnerf3333272013-04-22 10:55:53 -07004030 void activityIdleInternal(ActivityRecord r) {
4031 synchronized (mService) {
4032 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
4033 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004034 }
4035
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004036 @Override
4037 public void handleMessage(Message msg) {
4038 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07004039 case IDLE_TIMEOUT_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004040 if (DEBUG_IDLE) Slog.d(TAG_IDLE,
4041 "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004042 if (mService.mDidDexOpt) {
4043 mService.mDidDexOpt = false;
4044 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
4045 nmsg.obj = msg.obj;
4046 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
4047 return;
4048 }
4049 // We don't at this point know if the activity is fullscreen,
4050 // so we need to be conservative and assume it isn't.
4051 activityIdleInternal((ActivityRecord)msg.obj);
4052 } break;
4053 case IDLE_NOW_MSG: {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004054 if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07004055 activityIdleInternal((ActivityRecord)msg.obj);
4056 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07004057 case RESUME_TOP_ACTIVITY_MSG: {
4058 synchronized (mService) {
4059 resumeTopActivitiesLocked();
4060 }
4061 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07004062 case SLEEP_TIMEOUT_MSG: {
4063 synchronized (mService) {
4064 if (mService.isSleepingOrShuttingDown()) {
4065 Slog.w(TAG, "Sleep timeout! Sleeping now.");
4066 mSleepTimeout = true;
4067 checkReadyForSleepLocked();
4068 }
4069 }
4070 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07004071 case LAUNCH_TIMEOUT_MSG: {
4072 if (mService.mDidDexOpt) {
4073 mService.mDidDexOpt = false;
4074 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
4075 return;
4076 }
4077 synchronized (mService) {
4078 if (mLaunchingActivity.isHeld()) {
4079 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
4080 if (VALIDATE_WAKE_LOCK_CALLER
4081 && Binder.getCallingUid() != Process.myUid()) {
4082 throw new IllegalStateException("Calling must be system uid");
4083 }
4084 mLaunchingActivity.release();
4085 }
4086 }
4087 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004088 case HANDLE_DISPLAY_ADDED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004089 handleDisplayAdded(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004090 } break;
4091 case HANDLE_DISPLAY_CHANGED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004092 handleDisplayChanged(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004093 } break;
4094 case HANDLE_DISPLAY_REMOVED: {
Wale Ogunwalef16a2812015-04-01 11:23:15 -07004095 handleDisplayRemoved(msg.arg1);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004096 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07004097 case CONTAINER_CALLBACK_VISIBILITY: {
4098 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004099 final IActivityContainerCallback callback = container.mCallback;
4100 if (callback != null) {
4101 try {
4102 callback.setVisible(container.asBinder(), msg.arg1 == 1);
4103 } catch (RemoteException e) {
4104 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07004105 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004106 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004107 case LOCK_TASK_START_MSG: {
4108 // When lock task starts, we disable the status bars.
4109 try {
Jason Monk62515be2014-05-21 16:06:19 -04004110 if (mLockTaskNotify == null) {
4111 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04004112 }
Jason Monk62515be2014-05-21 16:06:19 -04004113 mLockTaskNotify.show(true);
Benjamin Franz43261142015-02-11 15:59:44 +00004114 mLockTaskModeState = msg.arg2;
Jason Monk62515be2014-05-21 16:06:19 -04004115 if (getStatusBarService() != null) {
Benjamin Franz43261142015-02-11 15:59:44 +00004116 int flags = 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004117 if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004118 flags = StatusBarManager.DISABLE_MASK
4119 & (~StatusBarManager.DISABLE_BACK);
Craig Mautner15df08a2015-04-01 12:17:18 -07004120 } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
Benjamin Franz43261142015-02-11 15:59:44 +00004121 flags = StatusBarManager.DISABLE_MASK
4122 & (~StatusBarManager.DISABLE_BACK)
4123 & (~StatusBarManager.DISABLE_HOME)
4124 & (~StatusBarManager.DISABLE_RECENT);
Jason Monk62515be2014-05-21 16:06:19 -04004125 }
4126 getStatusBarService().disable(flags, mToken,
4127 mService.mContext.getPackageName());
4128 }
4129 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04004130 if (getDevicePolicyManager() != null) {
4131 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04004132 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04004133 }
justinzhang5286d3f2014-05-12 17:06:01 -04004134 } catch (RemoteException ex) {
4135 throw new RuntimeException(ex);
4136 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004137 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04004138 case LOCK_TASK_END_MSG: {
4139 // When lock task ends, we enable the status bars.
4140 try {
Jason Monk62515be2014-05-21 16:06:19 -04004141 if (getStatusBarService() != null) {
4142 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
4143 mService.mContext.getPackageName());
4144 }
4145 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04004146 if (getDevicePolicyManager() != null) {
4147 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
4148 msg.arg1);
4149 }
Jason Monk62515be2014-05-21 16:06:19 -04004150 if (mLockTaskNotify == null) {
4151 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4152 }
4153 mLockTaskNotify.show(false);
4154 try {
Jason Monk94cfd9d2014-10-31 13:18:21 -04004155 boolean shouldLockKeyguard = Settings.Secure.getInt(
Jason Monk62515be2014-05-21 16:06:19 -04004156 mService.mContext.getContentResolver(),
Jason Monk94cfd9d2014-10-31 13:18:21 -04004157 Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
Craig Mautner15df08a2015-04-01 12:17:18 -07004158 if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04004159 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04004160 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04004161 new LockPatternUtils(mService.mContext)
4162 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04004163 }
4164 } catch (SettingNotFoundException e) {
4165 // No setting, don't lock.
4166 }
justinzhang5286d3f2014-05-12 17:06:01 -04004167 } catch (RemoteException ex) {
4168 throw new RuntimeException(ex);
Benjamin Franz43261142015-02-11 15:59:44 +00004169 } finally {
Craig Mautner15df08a2015-04-01 12:17:18 -07004170 mLockTaskModeState = LOCK_TASK_MODE_NONE;
justinzhang5286d3f2014-05-12 17:06:01 -04004171 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07004172 } break;
Craig Mautnerc21ae9e2015-04-15 09:45:42 -07004173 case SHOW_LOCK_TASK_ESCAPE_MESSAGE_MSG: {
4174 if (mLockTaskNotify == null) {
4175 mLockTaskNotify = new LockTaskNotify(mService.mContext);
4176 }
4177 mLockTaskNotify.showToast(LOCK_TASK_MODE_PINNED);
4178 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004179 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
4180 final ActivityContainer container = (ActivityContainer) msg.obj;
4181 final IActivityContainerCallback callback = container.mCallback;
4182 if (callback != null) {
4183 try {
4184 callback.onAllActivitiesComplete(container.asBinder());
4185 } catch (RemoteException e) {
4186 }
4187 }
4188 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07004189 case LAUNCH_TASK_BEHIND_COMPLETE: {
4190 synchronized (mService) {
Wale Ogunwale7d701172015-03-11 15:36:30 -07004191 ActivityRecord r = ActivityRecord.forTokenLocked((IBinder) msg.obj);
Craig Mautnerbb742462014-07-07 15:28:55 -07004192 if (r != null) {
4193 handleLaunchTaskBehindCompleteLocked(r);
4194 }
4195 }
4196 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004197 }
4198 }
4199 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004200
Ying Wangb081a592014-04-22 15:20:16 -07004201 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07004202 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07004203 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004204 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004205 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004206 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004207 ActivityRecord mParentActivity = null;
4208 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08004209
Craig Mautnere3a00d72014-04-16 08:31:19 -07004210 boolean mVisible = true;
4211
Craig Mautner4a1cb222013-12-04 16:14:06 -08004212 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08004213 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004214
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004215 final static int CONTAINER_STATE_HAS_SURFACE = 0;
4216 final static int CONTAINER_STATE_NO_SURFACE = 1;
4217 final static int CONTAINER_STATE_FINISHING = 2;
4218 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
4219
4220 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004221 synchronized (mService) {
4222 mStackId = stackId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004223 mStack = new ActivityStack(this, mRecentTasks);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004224 mIdString = "ActivtyContainer{" + mStackId + "}";
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004225 if (DEBUG_STACK) Slog.d(TAG_STACK, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004226 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004227 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004228
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004229 void attachToDisplayLocked(ActivityDisplay activityDisplay, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004230 if (DEBUG_STACK) Slog.d(TAG_STACK, "attachToDisplayLocked: " + this
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004231 + " to display=" + activityDisplay + " onTop=" + onTop);
Craig Mautnere0a38842013-12-16 16:14:02 -08004232 mActivityDisplay = activityDisplay;
4233 mStack.mDisplayId = activityDisplay.mDisplayId;
4234 mStack.mStacks = activityDisplay.mStacks;
4235
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004236 activityDisplay.attachActivities(mStack, onTop);
4237 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004238 }
4239
4240 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004241 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004242 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08004243 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
4244 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004245 return;
4246 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004247 attachToDisplayLocked(activityDisplay, true);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004248 }
4249 }
4250
4251 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004252 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004253 synchronized (mService) {
4254 if (mActivityDisplay != null) {
4255 return mActivityDisplay.mDisplayId;
4256 }
Craig Mautnere0a38842013-12-16 16:14:02 -08004257 }
4258 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004259 }
4260
Jeff Brownca9bc702014-02-11 14:32:56 -08004261 @Override
Winson Chungd16c5652015-01-26 16:11:07 -08004262 public int getStackId() {
4263 synchronized (mService) {
4264 return mStackId;
4265 }
4266 }
4267
4268 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08004269 public boolean injectEvent(InputEvent event) {
4270 final long origId = Binder.clearCallingIdentity();
4271 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07004272 synchronized (mService) {
4273 if (mActivityDisplay != null) {
4274 return mInputManagerInternal.injectInputEvent(event,
4275 mActivityDisplay.mDisplayId,
4276 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
4277 }
Jeff Brownca9bc702014-02-11 14:32:56 -08004278 }
4279 return false;
4280 } finally {
4281 Binder.restoreCallingIdentity(origId);
4282 }
4283 }
4284
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004285 @Override
4286 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07004287 synchronized (mService) {
4288 if (mContainerState == CONTAINER_STATE_FINISHING) {
4289 return;
4290 }
4291 mContainerState = CONTAINER_STATE_FINISHING;
4292
Craig Mautnerd163e752014-06-13 17:18:47 -07004293 long origId = Binder.clearCallingIdentity();
4294 try {
Craig Mautneree36c772014-07-16 14:56:05 -07004295 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07004296 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07004297 } finally {
4298 Binder.restoreCallingIdentity(origId);
4299 }
4300 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004301 }
4302
Craig Mautner60257702014-09-17 15:02:33 -07004303 protected void detachLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004304 if (DEBUG_STACK) Slog.d(TAG_STACK, "detachLocked: " + this + " from display="
Craig Mautner34b73df2014-01-12 21:11:08 -08004305 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08004306 if (mActivityDisplay != null) {
4307 mActivityDisplay.detachActivitiesLocked(mStack);
4308 mActivityDisplay = null;
4309 mStack.mDisplayId = -1;
4310 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08004311 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004312 }
4313 }
4314
4315 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08004316 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004317 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnerb9168362015-02-26 20:40:19 -08004318 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004319 Binder.getCallingUid(), mCurrentUser, false,
4320 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004321
Craig Mautnere0a38842013-12-16 16:14:02 -08004322 // TODO: Switch to user app stacks here.
4323 String mimeType = intent.getType();
Craig Mautnerb9168362015-02-26 20:40:19 -08004324 final Uri data = intent.getData();
4325 if (mimeType == null && data != null && "content".equals(data.getScheme())) {
4326 mimeType = mService.getProviderMimeType(data, userId);
Craig Mautnere0a38842013-12-16 16:14:02 -08004327 }
Craig Mautnerb9168362015-02-26 20:40:19 -08004328 checkEmbeddedAllowedInner(userId, intent, mimeType);
4329
4330 intent.addFlags(FORCE_NEW_TASK_FLAGS);
4331 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07004332 0, 0, null, null, null, null, false, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004333 }
4334
4335 @Override
Craig Mautner5f2bb4c2015-03-12 16:10:27 -07004336 public final int startActivityIntentSender(IIntentSender intentSender)
4337 throws TransactionTooLargeException {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004338 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
4339
4340 if (!(intentSender instanceof PendingIntentRecord)) {
4341 throw new IllegalArgumentException("Bad PendingIntent object");
4342 }
4343
Craig Mautnerb9168362015-02-26 20:40:19 -08004344 final int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07004345 Binder.getCallingUid(), mCurrentUser, false,
4346 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnerb9168362015-02-26 20:40:19 -08004347
4348 final PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
4349 checkEmbeddedAllowedInner(userId, pendingIntent.key.requestIntent,
4350 pendingIntent.key.requestResolvedType);
4351
4352 return pendingIntent.sendInner(0, null, null, null, null, null, null, 0,
4353 FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
4354 }
4355
4356 private void checkEmbeddedAllowedInner(int userId, Intent intent, String resolvedType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004357 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07004358 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07004359 throw new SecurityException(
4360 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
4361 }
4362 }
4363
Craig Mautnerdf88d732014-01-27 09:21:32 -08004364 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08004365 public IBinder asBinder() {
4366 return this;
4367 }
4368
Craig Mautner4504de52013-12-20 09:06:56 -08004369 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004370 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08004371 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08004372 }
4373
Craig Mautner4a1cb222013-12-04 16:14:06 -08004374 ActivityStackSupervisor getOuter() {
4375 return ActivityStackSupervisor.this;
4376 }
4377
Craig Mautnerd163e752014-06-13 17:18:47 -07004378 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08004379 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08004380 }
4381
4382 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07004383 synchronized (mService) {
4384 if (mActivityDisplay != null) {
4385 mActivityDisplay.getBounds(outBounds);
4386 } else {
4387 outBounds.set(0, 0);
4388 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004389 }
4390 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004391
Craig Mautner6985bad2014-04-21 15:22:06 -07004392 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07004393 void setVisible(boolean visible) {
4394 if (mVisible != visible) {
4395 mVisible = visible;
4396 if (mCallback != null) {
4397 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
4398 0 /* unused */, this).sendToTarget();
4399 }
4400 }
4401 }
4402
Craig Mautner6985bad2014-04-21 15:22:06 -07004403 void setDrawn() {
4404 }
4405
Craig Mautner1b4bf852014-05-26 15:06:32 -07004406 // You can always start a new task on a regular ActivityStack.
4407 boolean isEligibleForNewTasks() {
4408 return true;
4409 }
4410
Craig Mautnerd163e752014-06-13 17:18:47 -07004411 void onTaskListEmptyLocked() {
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004412 detachLocked();
4413 deleteActivityContainer(this);
4414 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
Craig Mautnerd94b47f2014-06-02 15:06:40 -07004415 }
4416
Craig Mautner34b73df2014-01-12 21:11:08 -08004417 @Override
4418 public String toString() {
4419 return mIdString + (mActivityDisplay == null ? "N" : "A");
4420 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004421 }
4422
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004423 private class VirtualActivityContainer extends ActivityContainer {
4424 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07004425 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004426
4427 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
4428 super(getNextStackId());
4429 mParentActivity = parent;
4430 mCallback = callback;
4431 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07004432 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004433 }
4434
4435 @Override
4436 public void setSurface(Surface surface, int width, int height, int density) {
4437 super.setSurface(surface, width, height, density);
4438
4439 synchronized (mService) {
4440 final long origId = Binder.clearCallingIdentity();
4441 try {
4442 setSurfaceLocked(surface, width, height, density);
4443 } finally {
4444 Binder.restoreCallingIdentity(origId);
4445 }
4446 }
4447 }
4448
4449 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
4450 if (mContainerState == CONTAINER_STATE_FINISHING) {
4451 return;
4452 }
4453 VirtualActivityDisplay virtualActivityDisplay =
4454 (VirtualActivityDisplay) mActivityDisplay;
4455 if (virtualActivityDisplay == null) {
4456 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07004457 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004458 mActivityDisplay = virtualActivityDisplay;
4459 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004460 attachToDisplayLocked(virtualActivityDisplay, true);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004461 }
4462
4463 if (mSurface != null) {
4464 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004465 }
4466
Craig Mautner6985bad2014-04-21 15:22:06 -07004467 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004468 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004469 mStack.resumeTopActivityLocked(null);
4470 } else {
4471 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07004472 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004473 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07004474 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07004475 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004476 }
Craig Mautner6985bad2014-04-21 15:22:06 -07004477
Craig Mautnerd163e752014-06-13 17:18:47 -07004478 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004479
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004480 if (DEBUG_STACK) Slog.d(TAG_STACK,
4481 "setSurface: " + this + " to display=" + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07004482 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004483
Craig Mautner6985bad2014-04-21 15:22:06 -07004484 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07004485 boolean isAttachedLocked() {
4486 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07004487 }
4488
4489 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07004490 void setDrawn() {
4491 synchronized (mService) {
4492 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07004493 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07004494 }
4495 }
4496
Craig Mautner1b4bf852014-05-26 15:06:32 -07004497 // Never start a new task on an ActivityView if it isn't explicitly specified.
4498 @Override
4499 boolean isEligibleForNewTasks() {
4500 return false;
4501 }
4502
Craig Mautnerd163e752014-06-13 17:18:47 -07004503 private void setSurfaceIfReadyLocked() {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004504 if (DEBUG_STACK) Slog.v(TAG_STACK, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07004505 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
4506 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
4507 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
4508 mContainerState = CONTAINER_STATE_HAS_SURFACE;
4509 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004510 }
4511 }
4512
Craig Mautner4a1cb222013-12-04 16:14:06 -08004513 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
4514 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004515 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08004516 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08004517 int mDisplayId;
4518 Display mDisplay;
4519 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08004520
Craig Mautner4a1cb222013-12-04 16:14:06 -08004521 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
4522 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08004523 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004524
Jose Lima4b6c6692014-08-12 17:41:12 -07004525 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004526
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004527 ActivityDisplay() {
4528 }
Craig Mautner4504de52013-12-20 09:06:56 -08004529
Craig Mautner1a70a162014-09-13 12:09:31 -07004530 // After instantiation, check that mDisplay is not null before using this. The alternative
4531 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08004532 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07004533 final Display display = mDisplayManager.getDisplay(displayId);
4534 if (display == null) {
4535 return;
4536 }
4537 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08004538 }
4539
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004540 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08004541 mDisplay = display;
4542 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08004543 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08004544 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004545
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004546 void attachActivities(ActivityStack stack, boolean onTop) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004547 if (DEBUG_STACK) Slog.v(TAG_STACK,
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004548 "attachActivities: attaching " + stack + " to displayId=" + mDisplayId
4549 + " onTop=" + onTop);
4550 if (onTop) {
4551 mStacks.add(stack);
4552 } else {
4553 mStacks.add(0, stack);
4554 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08004555 }
4556
Craig Mautnere0a38842013-12-16 16:14:02 -08004557 void detachActivitiesLocked(ActivityStack stack) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004558 if (DEBUG_STACK) Slog.v(TAG_STACK, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08004559 + " from displayId=" + mDisplayId);
4560 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08004561 }
4562
4563 void getBounds(Point bounds) {
4564 mDisplay.getDisplayInfo(mDisplayInfo);
4565 bounds.x = mDisplayInfo.appWidth;
4566 bounds.y = mDisplayInfo.appHeight;
4567 }
Craig Mautner34b73df2014-01-12 21:11:08 -08004568
Jose Lima4b6c6692014-08-12 17:41:12 -07004569 void setVisibleBehindActivity(ActivityRecord r) {
4570 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004571 }
4572
Jose Lima4b6c6692014-08-12 17:41:12 -07004573 boolean hasVisibleBehindActivity() {
4574 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07004575 }
4576
Craig Mautner34b73df2014-01-12 21:11:08 -08004577 @Override
4578 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004579 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
4580 }
4581 }
4582
4583 class VirtualActivityDisplay extends ActivityDisplay {
4584 VirtualDisplay mVirtualDisplay;
4585
Craig Mautner6985bad2014-04-21 15:22:06 -07004586 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004587 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07004588 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
4589 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
4590 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
4591 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07004592
4593 init(mVirtualDisplay.getDisplay());
4594
4595 mWindowManager.handleDisplayAdded(mDisplayId);
4596 }
4597
4598 void setSurface(Surface surface) {
4599 if (mVirtualDisplay != null) {
4600 mVirtualDisplay.setSurface(surface);
4601 }
4602 }
4603
4604 @Override
4605 void detachActivitiesLocked(ActivityStack stack) {
4606 super.detachActivitiesLocked(stack);
4607 if (mVirtualDisplay != null) {
4608 mVirtualDisplay.release();
4609 mVirtualDisplay = null;
4610 }
4611 }
4612
4613 @Override
4614 public String toString() {
4615 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004616 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004617 }
Jose Lima58e66d62014-05-27 20:00:27 -07004618
4619 private boolean isLeanbackOnlyDevice() {
4620 boolean onLeanbackOnly = false;
4621 try {
4622 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4623 PackageManager.FEATURE_LEANBACK_ONLY);
4624 } catch (RemoteException e) {
4625 // noop
4626 }
4627
4628 return onLeanbackOnly;
4629 }
Craig Mautner27084302013-03-25 08:05:25 -07004630}